blob: a23fd41e9100f728968fc25faf0d336959ef731f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
18#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
23#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040024#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020026#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <arpa/inet.h>
28
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020032#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
35#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020038#include <proto/checks.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010039#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/fd.h>
41#include <proto/log.h>
42#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020043#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010044#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010045#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010046#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/server.h>
48#include <proto/task.h>
49
Willy Tarreaubd741542010-03-16 18:46:54 +010050static int httpchk_expect(struct server *s, int done);
51
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020052const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
53 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
54 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
55 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020056
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010057 [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" },
58
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020059 [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020060
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020061 [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" },
62 [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" },
63 [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020064
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020065 [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" },
66 [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" },
67 [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020068
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020069 [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" },
70 [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020071
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020072 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020073
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020074 [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" },
75 [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
76 [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020077};
78
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010079const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
80 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
81
82 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
83 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
84
85 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
86 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
87 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
88 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
89
90 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
91 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
92 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
93};
94
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020095/*
96 * Convert check_status code to description
97 */
98const char *get_check_status_description(short check_status) {
99
100 const char *desc;
101
102 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200103 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200104 else
105 desc = NULL;
106
107 if (desc && *desc)
108 return desc;
109 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200110 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200111}
112
113/*
114 * Convert check_status code to short info
115 */
116const char *get_check_status_info(short check_status) {
117
118 const char *info;
119
120 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200121 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200122 else
123 info = NULL;
124
125 if (info && *info)
126 return info;
127 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200128 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200129}
130
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100131const char *get_analyze_status(short analyze_status) {
132
133 const char *desc;
134
135 if (analyze_status < HANA_STATUS_SIZE)
136 desc = analyze_statuses[analyze_status].desc;
137 else
138 desc = NULL;
139
140 if (desc && *desc)
141 return desc;
142 else
143 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
144}
145
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200146#define SSP_O_HCHK 0x0002
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200147
148static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
149
Krzysztof Piotr Oledzki5f5b7d22010-01-11 11:13:39 +0100150 if (s->tracked)
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200151 chunk_printf(msg, " via %s/%s",
152 s->tracked->proxy->id, s->tracked->id);
153
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200154 if (options & SSP_O_HCHK) {
155 chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status));
156
157 if (s->check_status >= HCHK_STATUS_L57DATA)
158 chunk_printf(msg, ", code: %d", s->check_code);
159
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200160 if (*s->check_desc) {
161 struct chunk src;
162
163 chunk_printf(msg, ", info: \"");
164
165 chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc));
166 chunk_asciiencode(msg, &src, '"');
167
168 chunk_printf(msg, "\"");
169 }
170
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100171 if (s->check_duration >= 0)
172 chunk_printf(msg, ", check duration: %ldms", s->check_duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200173 }
174
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200175 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100176 if (!(s->state & SRV_RUNNING))
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200177 chunk_printf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100178 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200179 s->proxy->srv_act, s->proxy->srv_bck,
180 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
181 s->cur_sess, xferred, s->nbpend);
182 else
183 chunk_printf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100184 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200185 s->proxy->srv_act, s->proxy->srv_bck,
186 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
187 xferred, s->nbpend);
188 }
189}
190
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200191/*
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200192 * Set s->check_status, update s->check_duration and fill s->result with
193 * an adequate SRV_CHK_* value.
194 *
195 * Show information in logs about failed health check if server is UP
196 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200197 */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200198static void set_server_check_status(struct server *s, short status, char *desc) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200199
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200200 struct chunk msg;
201
202 if (status == HCHK_STATUS_START) {
203 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200204 s->check_desc[0] = '\0';
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200205 s->check_start = now;
206 return;
207 }
208
209 if (!s->check_status)
210 return;
211
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200212 if (desc && *desc) {
213 strncpy(s->check_desc, desc, HCHK_DESC_LEN-1);
214 s->check_desc[HCHK_DESC_LEN-1] = '\0';
215 } else
216 s->check_desc[0] = '\0';
217
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200218 s->check_status = status;
219 if (check_statuses[status].result)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100220 s->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200221
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100222 if (status == HCHK_STATUS_HANA)
223 s->check_duration = -1;
224 else if (!tv_iszero(&s->check_start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200225 /* set_server_check_status() may be called more than once */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200226 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
227 tv_zero(&s->check_start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200228 }
229
230 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
231 (((s->health != 0) && (s->result & SRV_CHK_ERROR)) ||
232 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) ||
233 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
234 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
235
236 int health, rise, fall, state;
237
238 chunk_init(&msg, trash, sizeof(trash));
239
240 /* FIXME begin: calculate local version of the health/rise/fall/state */
241 health = s->health;
242 rise = s->rise;
243 fall = s->fall;
244 state = s->state;
245
246 if (s->result & SRV_CHK_ERROR) {
247 if (health > rise) {
248 health--; /* still good */
249 } else {
250 if (health == rise)
251 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
252
253 health = 0;
254 }
255 }
256
257 if (s->result & SRV_CHK_RUNNING) {
258 if (health < rise + fall - 1) {
259 health++; /* was bad, stays for a while */
260
261 if (health == rise)
262 state |= SRV_RUNNING;
263
264 if (health >= rise)
265 health = rise + fall - 1; /* OK now */
266 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100267
268 /* clear consecutive_errors if observing is enabled */
269 if (s->onerror)
270 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200271 }
272 /* FIXME end: calculate local version of the health/rise/fall/state */
273
274 chunk_printf(&msg,
275 "Health check for %sserver %s/%s %s%s",
276 s->state & SRV_BACKUP ? "backup " : "",
277 s->proxy->id, s->id,
278 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
279 (s->result & SRV_CHK_RUNNING)?"succeeded":"failed");
280
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200281 server_status_printf(&msg, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200282
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100283 chunk_printf(&msg, ", status: %d/%d %s",
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200284 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
285 (state & SRV_RUNNING) ? (fall) : (rise),
286 (state & SRV_RUNNING)?"UP":"DOWN");
287
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100288 Warning("%s.\n", trash);
289 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200290 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200291}
292
Willy Tarreau48494c02007-11-30 10:41:39 +0100293/* sends a log message when a backend goes down, and also sets last
294 * change date.
295 */
296static void set_backend_down(struct proxy *be)
297{
298 be->last_change = now.tv_sec;
299 be->down_trans++;
300
301 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
302 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
303}
304
305/* Redistribute pending connections when a server goes down. The number of
306 * connections redistributed is returned.
307 */
308static int redistribute_pending(struct server *s)
309{
310 struct pendconn *pc, *pc_bck, *pc_end;
311 int xferred = 0;
312
313 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
314 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100315 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
316 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100317 /* The REDISP option was specified. We will ignore
318 * cookie and force to balance or use the dispatcher.
319 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100320
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100321 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100322 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100323
Willy Tarreau48494c02007-11-30 10:41:39 +0100324 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200325 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100326 xferred++;
327 }
328 }
329 return xferred;
330}
331
332/* Check for pending connections at the backend, and assign some of them to
333 * the server coming up. The server's weight is checked before being assigned
334 * connections it may not be able to handle. The total number of transferred
335 * connections is returned.
336 */
337static int check_for_pending(struct server *s)
338{
339 int xferred;
340
341 if (!s->eweight)
342 return 0;
343
344 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
345 struct session *sess;
346 struct pendconn *p;
347
348 p = pendconn_from_px(s->proxy);
349 if (!p)
350 break;
351 p->sess->srv = s;
352 sess = p->sess;
353 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200354 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100355 }
356 return xferred;
357}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358
359/* Sets server <s> down, notifies by all available means, recounts the
360 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200361 * possible to other servers. It automatically recomputes the number of
362 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100364void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100366 struct server *srv;
367 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 int xferred;
369
Cyril Bontécd19e512010-01-31 22:34:03 +0100370 if (s->state & SRV_MAINTAIN) {
371 s->health = s->rise;
372 }
373
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100374 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100375 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200376 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200377
378 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100379 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100380 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381
382 /* we might have sessions queued on this server and waiting for
383 * a connection. Those which are redispatchable will be queued
384 * to another server or to the proxy itself.
385 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100386 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100387
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200388 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100389
Cyril Bontécd19e512010-01-31 22:34:03 +0100390 if (s->state & SRV_MAINTAIN) {
391 chunk_printf(&msg,
392 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
393 s->proxy->id, s->id);
394 } else {
395 chunk_printf(&msg,
396 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
397 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100398
Cyril Bontécd19e512010-01-31 22:34:03 +0100399 server_status_printf(&msg, s,
400 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
401 xferred);
402 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100403 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200404
Willy Tarreau48494c02007-11-30 10:41:39 +0100405 /* we don't send an alert if the server was previously paused */
406 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100407 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100408 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100409 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200410
Willy Tarreaud64d2252010-10-17 17:16:42 +0200411 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100412 set_backend_down(s->proxy);
413
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200414 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100415
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100416 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100417 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100418 if (! (srv->state & SRV_MAINTAIN))
419 /* Only notify tracking servers that are not already in maintenance. */
420 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200421 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100422
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423 s->health = 0; /* failure */
424}
425
Cyril Bontécd19e512010-01-31 22:34:03 +0100426void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100427
428 struct server *srv;
429 struct chunk msg;
430 int xferred;
431
Cyril Bontécd19e512010-01-31 22:34:03 +0100432 if (s->state & SRV_MAINTAIN) {
433 s->health = s->rise;
434 }
435
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100436 if (s->health == s->rise || s->tracked) {
437 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
438 if (s->proxy->last_change < now.tv_sec) // ignore negative times
439 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
440 s->proxy->last_change = now.tv_sec;
441 }
442
443 if (s->last_change < now.tv_sec) // ignore negative times
444 s->down_time += now.tv_sec - s->last_change;
445
446 s->last_change = now.tv_sec;
447 s->state |= SRV_RUNNING;
448
449 if (s->slowstart > 0) {
450 s->state |= SRV_WARMINGUP;
451 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
452 /* For dynamic algorithms, start at the first step of the weight,
453 * without multiplying by BE_WEIGHT_SCALE.
454 */
455 s->eweight = s->uweight;
456 if (s->proxy->lbprm.update_server_eweight)
457 s->proxy->lbprm.update_server_eweight(s);
458 }
459 }
460 s->proxy->lbprm.set_server_status_up(s);
461
462 /* check if we can handle some connections queued at the proxy. We
463 * will take as many as we can handle.
464 */
465 xferred = check_for_pending(s);
466
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200467 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100468
Cyril Bontécd19e512010-01-31 22:34:03 +0100469 if (s->state & SRV_MAINTAIN) {
470 chunk_printf(&msg,
471 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
472 s->proxy->id, s->id);
473 } else {
474 chunk_printf(&msg,
475 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
476 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100477
Cyril Bontécd19e512010-01-31 22:34:03 +0100478 server_status_printf(&msg, s,
479 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
480 xferred);
481 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100482
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100483 Warning("%s.\n", trash);
484 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100485
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100486 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100487 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100488 if (! (srv->state & SRV_MAINTAIN))
489 /* Only notify tracking servers if they're not in maintenance. */
490 set_server_up(srv);
491
492 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100493 }
494
495 if (s->health >= s->rise)
496 s->health = s->rise + s->fall - 1; /* OK now */
497
498}
499
500static void set_server_disabled(struct server *s) {
501
502 struct server *srv;
503 struct chunk msg;
504 int xferred;
505
506 s->state |= SRV_GOINGDOWN;
507 s->proxy->lbprm.set_server_status_down(s);
508
509 /* we might have sessions queued on this server and waiting for
510 * a connection. Those which are redispatchable will be queued
511 * to another server or to the proxy itself.
512 */
513 xferred = redistribute_pending(s);
514
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200515 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100516
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200517 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100518 "Load-balancing on %sServer %s/%s is disabled",
519 s->state & SRV_BACKUP ? "Backup " : "",
520 s->proxy->id, s->id);
521
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200522 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200523 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100524 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100525
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100526 Warning("%s.\n", trash);
527 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100528
529 if (!s->proxy->srv_bck && !s->proxy->srv_act)
530 set_backend_down(s->proxy);
531
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)
534 set_server_disabled(srv);
535}
536
537static void set_server_enabled(struct server *s) {
538
539 struct server *srv;
540 struct chunk msg;
541 int xferred;
542
543 s->state &= ~SRV_GOINGDOWN;
544 s->proxy->lbprm.set_server_status_up(s);
545
546 /* check if we can handle some connections queued at the proxy. We
547 * will take as many as we can handle.
548 */
549 xferred = check_for_pending(s);
550
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200551 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100552
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200553 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100554 "Load-balancing on %sServer %s/%s is enabled again",
555 s->state & SRV_BACKUP ? "Backup " : "",
556 s->proxy->id, s->id);
557
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200558 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200559 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100560 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100561
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100562 Warning("%s.\n", trash);
563 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100564
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100565 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100566 for(srv = s->tracknext; srv; srv = srv->tracknext)
567 set_server_enabled(srv);
568}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100570void health_adjust(struct server *s, short status) {
571
572 int failed;
573 int expire;
574
575 /* return now if observing nor health check is not enabled */
576 if (!s->observe || !s->check)
577 return;
578
579 if (s->observe >= HANA_OBS_SIZE)
580 return;
581
582 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
583 return;
584
585 switch (analyze_statuses[status].lr[s->observe - 1]) {
586 case 1:
587 failed = 1;
588 break;
589
590 case 2:
591 failed = 0;
592 break;
593
594 default:
595 return;
596 }
597
598 if (!failed) {
599 /* good: clear consecutive_errors */
600 s->consecutive_errors = 0;
601 return;
602 }
603
604 s->consecutive_errors++;
605
606 if (s->consecutive_errors < s->consecutive_errors_limit)
607 return;
608
609 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
610 s->consecutive_errors, get_analyze_status(status));
611
612 switch (s->onerror) {
613 case HANA_ONERR_FASTINTER:
614 /* force fastinter - nothing to do here as all modes force it */
615 break;
616
617 case HANA_ONERR_SUDDTH:
618 /* simulate a pre-fatal failed health check */
619 if (s->health > s->rise)
620 s->health = s->rise + 1;
621
622 /* no break - fall through */
623
624 case HANA_ONERR_FAILCHK:
625 /* simulate a failed health check */
626 set_server_check_status(s, HCHK_STATUS_HANA, trash);
627
628 if (s->health > s->rise) {
629 s->health--; /* still good */
630 s->counters.failed_checks++;
631 }
632 else
633 set_server_down(s);
634
635 break;
636
637 case HANA_ONERR_MARKDWN:
638 /* mark server down */
639 s->health = s->rise;
640 set_server_check_status(s, HCHK_STATUS_HANA, trash);
641 set_server_down(s);
642
643 break;
644
645 default:
646 /* write a warning? */
647 break;
648 }
649
650 s->consecutive_errors = 0;
651 s->counters.failed_hana++;
652
653 if (s->fastinter) {
654 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
655 if (s->check->expire > expire)
656 s->check->expire = expire;
657 }
658}
659
Willy Tarreauef781042010-01-27 11:53:01 +0100660static int httpchk_build_status_header(struct server *s, char *buffer)
661{
662 int sv_state;
663 int ratio;
664 int hlen = 0;
665 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
666 "UP %d/%d", "UP",
667 "NOLB %d/%d", "NOLB",
668 "no check" };
669
670 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
671 hlen += 24;
672
673 if (!(s->state & SRV_CHECKED))
674 sv_state = 6; /* should obviously never happen */
675 else if (s->state & SRV_RUNNING) {
676 if (s->health == s->rise + s->fall - 1)
677 sv_state = 3; /* UP */
678 else
679 sv_state = 2; /* going down */
680
681 if (s->state & SRV_GOINGDOWN)
682 sv_state += 2;
683 } else {
684 if (s->health)
685 sv_state = 1; /* going up */
686 else
687 sv_state = 0; /* DOWN */
688 }
689
690 hlen += sprintf(buffer + hlen,
691 srv_hlt_st[sv_state],
692 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
693 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
694
695 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
696 s->proxy->id, s->id,
697 global.node,
698 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
699 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
700 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
701 s->nbpend);
702
703 if ((s->state & SRV_WARMINGUP) &&
704 now.tv_sec < s->last_change + s->slowstart &&
705 now.tv_sec >= s->last_change) {
706 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
707 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
708 }
709
710 buffer[hlen++] = '\r';
711 buffer[hlen++] = '\n';
712
713 return hlen;
714}
715
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716/*
717 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200718 * the connection acknowledgement. If the proxy requires L7 health-checks,
719 * it sends the request. In other cases, it calls set_server_check_status()
720 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200721 * The function itself returns 0 if it needs some polling before being called
722 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 */
Willy Tarreau83749182007-04-15 20:56:27 +0200724static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725{
Willy Tarreau6996e152007-04-30 14:37:43 +0200726 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 struct task *t = fdtab[fd].owner;
728 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100730 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200731 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100732 int skerr, err = errno;
733 socklen_t lskerr = sizeof(skerr);
734
735 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
736 err = skerr;
737
738 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200739 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200740 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200741
742 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200743
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100744 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200746 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Hervé COMMOWICK698ae002010-01-12 09:25:13 +0100747 (s->proxy->options & PR_O_SMTP_CHK) ||
Willy Tarreau07a54902010-03-29 18:33:29 +0200748 (s->proxy->options2 & PR_O2_SSL3_CHK) ||
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200749 (s->proxy->options2 & PR_O2_MYSQL_CHK) ||
750 (s->proxy->options2 & PR_O2_LDAP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100752 const char *check_req = s->proxy->check_req;
753 int check_len = s->proxy->check_len;
754
Willy Tarreauf3c69202006-07-09 16:42:34 +0200755 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756 * so we'll send the request, and won't wake the checker up now.
757 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200758
Willy Tarreau07a54902010-03-29 18:33:29 +0200759 if (s->proxy->options2 & PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200760 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200761 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200762 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
763 }
Willy Tarreaue9d87882010-01-27 11:28:42 +0100764 else if (s->proxy->options & PR_O_HTTP_CHK) {
765 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100766
767 if (s->proxy->options2 & PR_O2_CHK_SNDST)
768 check_len += httpchk_build_status_header(s, trash + check_len);
769
Willy Tarreaue9d87882010-01-27 11:28:42 +0100770 trash[check_len++] = '\r';
771 trash[check_len++] = '\n';
772 trash[check_len] = '\0';
773 check_req = trash;
774 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200775
Willy Tarreaue9d87882010-01-27 11:28:42 +0100776 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
777 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100778 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100779 if (s->proxy->timeout.check)
780 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200781 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200782 goto out_nowake;
783 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200784 else if (ret == 0 || errno == EAGAIN)
785 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200786 else {
787 switch (errno) {
788 case ECONNREFUSED:
789 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200790 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200791 break;
792
793 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200794 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200795 }
796
Willy Tarreau6996e152007-04-30 14:37:43 +0200797 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200798 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 }
800 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200801 /* We have no data to send to check the connection, and
802 * getsockopt() will not inform us whether the connection
803 * is still pending. So we'll reuse connect() to check the
804 * state of the socket. This has the advantage of givig us
805 * the following info :
806 * - error
807 * - connecting (EALREADY, EINPROGRESS)
808 * - connected (EISCONN, 0)
809 */
810
811 struct sockaddr_in sa;
812
813 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
814 sa.sin_port = htons(s->check_port);
815
816 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
817 errno = 0;
818
819 if (errno == EALREADY || errno == EINPROGRESS)
820 goto out_poll;
821
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200822 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200823 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200824 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200825 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200826
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200828 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200829 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 }
831 }
Willy Tarreau83749182007-04-15 20:56:27 +0200832 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200833 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200834 out_nowake:
835 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100836 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200837 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200838 out_poll:
839 /* The connection is still pending. We'll have to poll it
840 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100841 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200842 return 0;
843 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200844 fdtab[fd].state = FD_STERROR;
845 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846}
847
848
849/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200850 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200851 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
852 * set_server_check_status() to update s->check_status, s->check_duration
853 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200854
855 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
856 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
857 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
858 * response to an SSL HELLO (the principle is that this is enough to
859 * distinguish between an SSL server and a pure TCP relay). All other cases will
860 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
861 * etc.
862 *
863 * The function returns 0 if it needs to be called again after some polling,
864 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200865 */
Willy Tarreau83749182007-04-15 20:56:27 +0200866static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867{
Willy Tarreau83749182007-04-15 20:56:27 +0200868 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100869 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870 struct task *t = fdtab[fd].owner;
871 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200872 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100873 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200874 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200875
Willy Tarreau659d7bc2010-03-16 21:14:41 +0100876 if (unlikely((s->result & SRV_CHK_ERROR) || (fdtab[fd].state == FD_STERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200877 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200878 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200879 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200880
Willy Tarreau83749182007-04-15 20:56:27 +0200881 goto out_wakeup;
882 }
883
Willy Tarreau83749182007-04-15 20:56:27 +0200884 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
885 * but the connection was closed on the remote end. Fortunately, recv still
886 * works correctly and we don't need to do the getsockopt() on linux.
887 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000888
889 /* Set buffer to point to the end of the data already read, and check
890 * that there is free space remaining. If the buffer is full, proceed
891 * with running the checks without attempting another socket read.
892 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000893
Willy Tarreau03938182010-03-17 21:52:07 +0100894 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200895 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
896 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 +0100897 if (len <= 0)
898 break;
899 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000900
Willy Tarreau03938182010-03-17 21:52:07 +0100901 if (len == 0)
902 done = 1; /* connection hangup received */
903 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100904 /* Report network errors only if we got no other data. Otherwise
905 * we'll let the upper layers decide whether the response is OK
906 * or not. It is very common that an RST sent by the server is
907 * reported as an error just after the last data chunk.
908 */
Willy Tarreau03938182010-03-17 21:52:07 +0100909 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100910 if (!s->check_data_len) {
911 if (!(s->result & SRV_CHK_ERROR))
912 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
913 goto out_wakeup;
914 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 }
916
Willy Tarreau03938182010-03-17 21:52:07 +0100917 /* Intermediate or complete response received.
918 * Terminate string in check_data buffer.
919 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200920 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100921 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100922 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100923 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100924 done = 1; /* buffer full, don't wait for more data */
925 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200926
Nick Chalk57b1bf72010-03-16 15:50:46 +0000927 /* Run the checks... */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100928 if (s->proxy->options & PR_O_HTTP_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100929 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
930 goto wait_more_data;
931
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100932 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000933 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
934 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
935 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
936 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
937 !isdigit((unsigned char) *(s->check_data + 11))) {
938 cut_crlf(s->check_data);
939 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200940
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100941 goto out_wakeup;
942 }
943
Nick Chalk57b1bf72010-03-16 15:50:46 +0000944 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000945 desc = ltrim(s->check_data + 12, ' ');
946
Willy Tarreaubd741542010-03-16 18:46:54 +0100947 if ((s->proxy->options & PR_O_DISABLE404) &&
948 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000949 /* 404 may be accepted as "stopping" only if the server was up */
950 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200951 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000952 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100953 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
954 /* Run content verification check... We know we have at least 13 chars */
955 if (!httpchk_expect(s, done))
956 goto wait_more_data;
957 }
958 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
959 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
960 cut_crlf(desc);
961 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
962 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000963 else {
964 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200965 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000966 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100967 }
Willy Tarreau07a54902010-03-29 18:33:29 +0200968 else if (s->proxy->options2 & PR_O2_SSL3_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100969 if (!done && s->check_data_len < 5)
970 goto wait_more_data;
971
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100972 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000973 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200974 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200975 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200976 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200977 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100978 else if (s->proxy->options & PR_O_SMTP_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100979 if (!done && s->check_data_len < strlen("000\r"))
980 goto wait_more_data;
981
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200982 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000983 if ((s->check_data_len < strlen("000\r")) ||
984 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
985 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
986 !isdigit((unsigned char) *(s->check_data + 2))) {
987 cut_crlf(s->check_data);
988 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200989
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200990 goto out_wakeup;
991 }
992
Nick Chalk57b1bf72010-03-16 15:50:46 +0000993 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200994
Nick Chalk57b1bf72010-03-16 15:50:46 +0000995 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200996 cut_crlf(desc);
997
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100998 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000999 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001000 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001001 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001002 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau6996e152007-04-30 14:37:43 +02001003 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001004 else if (s->proxy->options2 & PR_O2_MYSQL_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +01001005 if (!done && s->check_data_len < 5)
1006 goto wait_more_data;
1007
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001008 if (s->proxy->check_len == 0) { // old mode
1009 if (*(s->check_data + 4) != '\xff') {
1010 /* We set the MySQL Version in description for information purpose
1011 * FIXME : it can be cool to use MySQL Version for other purpose,
1012 * like mark as down old MySQL server.
1013 */
1014 if (s->check_data_len > 51) {
1015 desc = ltrim(s->check_data + 5, ' ');
1016 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1017 }
1018 else {
1019 if (!done)
1020 goto wait_more_data;
1021 /* it seems we have a OK packet but without a valid length,
1022 * it must be a protocol error
1023 */
1024 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1025 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001026 }
1027 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001028 /* An error message is attached in the Error packet */
1029 desc = ltrim(s->check_data + 7, ' ');
1030 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1031 }
1032 } else {
1033 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1034 (((unsigned int) *(s->check_data + 1)) << 8) +
1035 (((unsigned int) *(s->check_data + 2)) << 16);
1036
1037 if (s->check_data_len == first_packet_len + 4) {
1038 /* MySQL Error packet always begin with field_count = 0xff */
1039 if (*(s->check_data + 4) != '\xff') {
1040 /* We have only one MySQL packet and it is a Handshake Initialization packet
1041 * but we need to have a second packet to know if it is alright
1042 */
1043 if (!done && s->check_data_len < first_packet_len + 5)
1044 goto wait_more_data;
1045 }
1046 else {
1047 /* We have only one packet and it is an Error packet,
1048 * an error message is attached, so we can display it
1049 */
1050 desc = &s->check_data[7];
1051 //Warning("onlyoneERR: %s\n", desc);
1052 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1053 }
1054 } else if (s->check_data_len > first_packet_len + 4) {
1055 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1056 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1057 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1058
1059 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1060 /* We have 2 packets and that's good */
1061 /* Check if the second packet is a MySQL Error packet or not */
1062 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1063 /* No error packet */
1064 /* We set the MySQL Version in description for information purpose */
1065 desc = &s->check_data[5];
1066 //Warning("2packetOK: %s\n", desc);
1067 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1068 }
1069 else {
1070 /* An error message is attached in the Error packet
1071 * so we can display it ! :)
1072 */
1073 desc = &s->check_data[first_packet_len+11];
1074 //Warning("2packetERR: %s\n", desc);
1075 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1076 }
1077 }
1078 }
1079 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001080 if (!done)
1081 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001082 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001083 * it must be a protocol error
1084 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001085 desc = &s->check_data[5];
1086 //Warning("protoerr: %s\n", desc);
1087 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001088 }
1089 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001090 }
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001091 else if (s->proxy->options2 & PR_O2_LDAP_CHK) {
1092 if (!done && s->check_data_len < 14)
1093 goto wait_more_data;
1094
1095 /* Check if the server speaks LDAP (ASN.1/BER)
1096 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1097 * http://tools.ietf.org/html/rfc4511
1098 */
1099
1100 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1101 * LDAPMessage: 0x30: SEQUENCE
1102 */
1103 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1104 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1105 }
1106 else {
1107 /* size of LDAPMessage */
1108 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1109
1110 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1111 * messageID: 0x02 0x01 0x01: INTEGER 1
1112 * protocolOp: 0x61: bindResponse
1113 */
1114 if ((msglen > 2) ||
1115 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1116 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1117
1118 goto out_wakeup;
1119 }
1120
1121 /* size of bindResponse */
1122 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1123
1124 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1125 * ldapResult: 0x0a 0x01: ENUMERATION
1126 */
1127 if ((msglen > 4) ||
1128 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1129 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1130
1131 goto out_wakeup;
1132 }
1133
1134 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1135 * resultCode
1136 */
1137 s->check_code = *(s->check_data + msglen + 9);
1138 if (s->check_code) {
1139 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1140 } else {
1141 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1142 }
1143 }
1144 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001145 else {
1146 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001147 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau23677902007-05-08 23:50:35 +02001148 }
Willy Tarreau83749182007-04-15 20:56:27 +02001149
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001150 out_wakeup:
1151 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001152 fdtab[fd].state = FD_STERROR;
1153
Nick Chalk57b1bf72010-03-16 15:50:46 +00001154 /* Reset the check buffer... */
1155 *s->check_data = '\0';
1156 s->check_data_len = 0;
1157
Willy Tarreau03938182010-03-17 21:52:07 +01001158 /* Close the connection... */
1159 shutdown(fd, SHUT_RDWR);
Willy Tarreauf161a342007-04-08 16:59:42 +02001160 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001161 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +01001162 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +02001163 return 1;
Willy Tarreau03938182010-03-17 21:52:07 +01001164
1165 wait_more_data:
1166 fdtab[fd].ev &= ~FD_POLL_IN;
1167 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001168}
1169
1170/*
1171 * manages a server health-check. Returns
1172 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1173 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001174struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175{
Willy Tarreaue3838802009-03-21 18:58:32 +01001176 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001177 struct server *s = t->context;
1178 struct sockaddr_in sa;
1179 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001180 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001181
1182 //fprintf(stderr, "process_chk: task=%p\n", t);
1183
1184 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001185 if (attempts++ > 0) {
1186 /* we always fail to create a server, let's stop insisting... */
1187 while (tick_is_expired(t->expire, now_ms))
1188 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1189 return t;
1190 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001191 fd = s->curfd;
1192 if (fd < 0) { /* no check currently running */
1193 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +01001194 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1195 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001196
1197 /* we don't send any health-checks when the proxy is stopped or when
1198 * the server should not be checked.
1199 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001200 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001201 while (tick_is_expired(t->expire, now_ms))
1202 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001203 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001204 }
1205
1206 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001207 set_server_check_status(s, HCHK_STATUS_START, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001208 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
1209 if ((fd < global.maxsock) &&
1210 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1211 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1212 //fprintf(stderr, "process_chk: 3\n");
1213
Willy Tarreau9edd1612007-10-18 18:07:48 +02001214 if (s->proxy->options & PR_O_TCP_NOLING) {
1215 /* We don't want to useless data */
1216 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1217 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001218
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001219 if (s->check_addr.sin_addr.s_addr)
1220 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001221 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001222 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001223 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001224 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001225
Willy Tarreaubaaee002006-06-26 02:48:02 +02001226 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 sa.sin_port = htons(s->check_port);
1228
1229 /* allow specific binding :
1230 * - server-specific at first
1231 * - proxy-specific next
1232 */
1233 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001234 struct sockaddr_in *remote = NULL;
1235 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001236
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001237#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001238 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
1239 remote = (struct sockaddr_in *)&s->tproxy_addr;
1240 flags = 3;
1241 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001242#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001243#ifdef SO_BINDTODEVICE
1244 /* Note: this might fail if not CAP_NET_RAW */
1245 if (s->iface_name)
1246 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001247 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001248#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001249 if (s->sport_range) {
1250 int bind_attempts = 10; /* should be more than enough to find a spare port */
1251 struct sockaddr_in src;
1252
1253 ret = 1;
1254 src = s->source_addr;
1255
1256 do {
1257 /* note: in case of retry, we may have to release a previously
1258 * allocated port, hence this loop's construct.
1259 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001260 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1261 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001262
1263 if (!bind_attempts)
1264 break;
1265 bind_attempts--;
1266
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001267 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1268 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001269 break;
1270
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001271 fdinfo[fd].port_range = s->sport_range;
1272 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001273
1274 ret = tcpv4_bind_socket(fd, flags, &src, remote);
1275 } while (ret != 0); /* binding NOK */
1276 }
1277 else {
1278 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
1279 }
1280
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001281 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001282 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001283 switch (ret) {
1284 case 1:
1285 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1286 s->proxy->id, s->id);
1287 break;
1288 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001289 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1290 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001291 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001292 }
1293 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001294 }
1295 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001296 struct sockaddr_in *remote = NULL;
1297 int ret, flags = 0;
1298
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001299#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001300 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001301 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
1302 flags = 3;
1303 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001304#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001305#ifdef SO_BINDTODEVICE
1306 /* Note: this might fail if not CAP_NET_RAW */
1307 if (s->proxy->iface_name)
1308 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001309 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001310#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001311 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
1312 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001313 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001314 switch (ret) {
1315 case 1:
1316 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1317 proxy_type_str(s->proxy), s->proxy->id);
1318 break;
1319 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001320 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1321 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001322 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001323 }
1324 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001325 }
1326
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001327 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001328#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001329 /* disabling tcp quick ack now allows
1330 * the request to leave the machine with
1331 * the first ACK.
1332 */
1333 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001334 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001335#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001336 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
1337 /* OK, connection in progress or established */
1338
1339 //fprintf(stderr, "process_chk: 4\n");
1340
1341 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +02001342 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001343 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +02001344 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
1345 fdtab[fd].cb[DIR_RD].b = NULL;
1346 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
1347 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001348 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
1349 fdinfo[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001350 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001351 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001352 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001354 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001355#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001356 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1357 /* we allow up to min(inter, timeout.connect) for a connection
1358 * to establish but only when timeout.check is set
1359 * as it may be to short for a full check otherwise
1360 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001361 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001362
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001363 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1364 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1365 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001366 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001367 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001368 }
1369 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001370 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001371
1372 switch (errno) {
1373 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1374 case ECONNREFUSED:
1375 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001376 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001377 break;
1378
1379 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001380 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001381 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001382 }
1383 }
1384 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001385 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1386 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001387 close(fd); /* socket creation error */
1388 }
1389
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001390 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001391 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001392 while (tick_is_expired(t->expire, now_ms))
1393 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394 goto new_chk; /* may be we should initialize a new check */
1395 }
1396
1397 /* here, we have seen a failure */
1398 if (s->health > s->rise) {
1399 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001400 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401 }
1402 else
1403 set_server_down(s);
1404
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001405 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1406 /* we allow up to min(inter, timeout.connect) for a connection
1407 * to establish but only when timeout.check is set
1408 * as it may be to short for a full check otherwise
1409 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001410 while (tick_is_expired(t->expire, now_ms)) {
1411 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001412
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001413 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1414 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001415
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001416 if (s->proxy->timeout.check)
1417 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001418 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419 goto new_chk;
1420 }
1421 else {
1422 //fprintf(stderr, "process_chk: 8\n");
1423 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001424 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001425 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001426
Willy Tarreau9909fc12007-11-30 17:42:05 +01001427 if (s->state & SRV_WARMINGUP) {
1428 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1429 s->state &= ~SRV_WARMINGUP;
1430 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1431 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1432 if (s->proxy->lbprm.update_server_eweight)
1433 s->proxy->lbprm.update_server_eweight(s);
1434 }
1435 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1436 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +01001437 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1438 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +01001439 s->eweight *= s->uweight;
1440 if (s->proxy->lbprm.update_server_eweight)
1441 s->proxy->lbprm.update_server_eweight(s);
1442 }
1443 /* probably that we can refill this server with a bit more connections */
1444 check_for_pending(s);
1445 }
1446
Willy Tarreau48494c02007-11-30 10:41:39 +01001447 /* we may have to add/remove this server from the LB group */
1448 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1449 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001450 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1451 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001452 else if (!(s->state & SRV_GOINGDOWN) &&
1453 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001454 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1455 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001456 }
1457
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001458 if (s->health < s->rise + s->fall - 1) {
1459 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001460
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001461 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001462 }
1463 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001464 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001465
1466 rv = 0;
1467 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001468 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001469 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001470 //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 +02001471 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001472 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001473 goto new_chk;
1474 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001475 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001476 if (!(s->result & SRV_CHK_ERROR)) {
1477 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001478 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001479 } else {
Willy Tarreau07a54902010-03-29 18:33:29 +02001480 if (s->proxy->options2 & PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001481 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001482 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001483 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001484 }
1485 }
1486
Willy Tarreaubaaee002006-06-26 02:48:02 +02001487 //fprintf(stderr, "process_chk: 10\n");
1488 /* failure or timeout detected */
1489 if (s->health > s->rise) {
1490 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001491 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001492 }
1493 else
1494 set_server_down(s);
1495 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001496 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001497
1498 rv = 0;
1499 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001500 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001501 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001502 //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 +02001503 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001504 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001505 goto new_chk;
1506 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001507 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001508 }
1509 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001510 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001511 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001512}
1513
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001514/*
1515 * Start health-check.
1516 * Returns 0 if OK, -1 if error, and prints the error in this case.
1517 */
1518int start_checks() {
1519
1520 struct proxy *px;
1521 struct server *s;
1522 struct task *t;
1523 int nbchk=0, mininter=0, srvpos=0;
1524
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001525 /* 1- count the checkers to run simultaneously.
1526 * We also determine the minimum interval among all of those which
1527 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1528 * will be used to spread their start-up date. Those which have
1529 * a shorter interval will start independantly and will not dictate
1530 * too short an interval for all others.
1531 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001532 for (px = proxy; px; px = px->next) {
1533 for (s = px->srv; s; s = s->next) {
1534 if (!(s->state & SRV_CHECKED))
1535 continue;
1536
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001537 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1538 (!mininter || mininter > srv_getinter(s)))
1539 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001540
1541 nbchk++;
1542 }
1543 }
1544
1545 if (!nbchk)
1546 return 0;
1547
1548 srand((unsigned)time(NULL));
1549
1550 /*
1551 * 2- start them as far as possible from each others. For this, we will
1552 * start them after their interval set to the min interval divided by
1553 * the number of servers, weighted by the server's position in the list.
1554 */
1555 for (px = proxy; px; px = px->next) {
1556 for (s = px->srv; s; s = s->next) {
1557 if (!(s->state & SRV_CHECKED))
1558 continue;
1559
Willy Tarreaua4613182009-03-21 18:13:21 +01001560 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001561 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1562 return -1;
1563 }
1564
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001565 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001566 t->process = process_chk;
1567 t->context = s;
1568
1569 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001570 t->expire = tick_add(now_ms,
1571 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1572 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001573 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001574 task_queue(t);
1575
1576 srvpos++;
1577 }
1578 }
1579 return 0;
1580}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001581
1582/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001583 * Perform content verification check on data in s->check_data buffer.
1584 * The buffer MUST be terminated by a null byte before calling this function.
1585 * Sets server status appropriately. The caller is responsible for ensuring
1586 * that the buffer contains at least 13 characters. If <done> is zero, we may
1587 * return 0 to indicate that data is required to decide of a match.
1588 */
1589static int httpchk_expect(struct server *s, int done)
1590{
1591 static char status_msg[] = "HTTP status check returned code <000>";
1592 char status_code[] = "000";
1593 char *contentptr;
1594 int crlf;
1595 int ret;
1596
1597 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1598 case PR_O2_EXP_STS:
1599 case PR_O2_EXP_RSTS:
1600 memcpy(status_code, s->check_data + 9, 3);
1601 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1602
1603 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1604 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1605 else
1606 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1607
1608 /* we necessarily have the response, so there are no partial failures */
1609 if (s->proxy->options2 & PR_O2_EXP_INV)
1610 ret = !ret;
1611
1612 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1613 break;
1614
1615 case PR_O2_EXP_STR:
1616 case PR_O2_EXP_RSTR:
1617 /* very simple response parser: ignore CR and only count consecutive LFs,
1618 * stop with contentptr pointing to first char after the double CRLF or
1619 * to '\0' if crlf < 2.
1620 */
1621 crlf = 0;
1622 for (contentptr = s->check_data; *contentptr; contentptr++) {
1623 if (crlf >= 2)
1624 break;
1625 if (*contentptr == '\r')
1626 continue;
1627 else if (*contentptr == '\n')
1628 crlf++;
1629 else
1630 crlf = 0;
1631 }
1632
1633 /* Check that response contains a body... */
1634 if (crlf < 2) {
1635 if (!done)
1636 return 0;
1637
1638 set_server_check_status(s, HCHK_STATUS_L7RSP,
1639 "HTTP content check could not find a response body");
1640 return 1;
1641 }
1642
1643 /* Check that response body is not empty... */
1644 if (*contentptr == '\0') {
1645 set_server_check_status(s, HCHK_STATUS_L7RSP,
1646 "HTTP content check found empty response body");
1647 return 1;
1648 }
1649
1650 /* Check the response content against the supplied string
1651 * or regex... */
1652 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1653 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1654 else
1655 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1656
1657 /* if we don't match, we may need to wait more */
1658 if (!ret && !done)
1659 return 0;
1660
1661 if (ret) {
1662 /* content matched */
1663 if (s->proxy->options2 & PR_O2_EXP_INV)
1664 set_server_check_status(s, HCHK_STATUS_L7RSP,
1665 "HTTP check matched unwanted content");
1666 else
1667 set_server_check_status(s, HCHK_STATUS_L7OKD,
1668 "HTTP content check matched");
1669 }
1670 else {
1671 if (s->proxy->options2 & PR_O2_EXP_INV)
1672 set_server_check_status(s, HCHK_STATUS_L7OKD,
1673 "HTTP check did not match unwanted content");
1674 else
1675 set_server_check_status(s, HCHK_STATUS_L7RSP,
1676 "HTTP content check did not match");
1677 }
1678 break;
1679 }
1680 return 1;
1681}
1682
1683/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001684 * Local variables:
1685 * c-indent-level: 8
1686 * c-basic-offset: 8
1687 * End:
1688 */