blob: 521de1d4d2dc73b144738497fbee162d539cd376 [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>
Willy Tarreau9e000c62011-03-10 14:03:36 +010048#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/task.h>
50
Willy Tarreaubd741542010-03-16 18:46:54 +010051static int httpchk_expect(struct server *s, int done);
52
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020053const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
54 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
55 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
56 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020057
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010058 [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" },
59
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020060 [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020061
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020062 [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" },
63 [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" },
64 [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020065
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020066 [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" },
67 [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" },
68 [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020069
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020070 [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" },
71 [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020073 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020074
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020075 [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" },
76 [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
77 [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020078};
79
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010080const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
81 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
82
83 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
84 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
85
86 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
87 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
88 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
89 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
90
91 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
92 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
93 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
94};
95
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020096/*
97 * Convert check_status code to description
98 */
99const char *get_check_status_description(short check_status) {
100
101 const char *desc;
102
103 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200104 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200105 else
106 desc = NULL;
107
108 if (desc && *desc)
109 return desc;
110 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200111 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200112}
113
114/*
115 * Convert check_status code to short info
116 */
117const char *get_check_status_info(short check_status) {
118
119 const char *info;
120
121 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200122 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200123 else
124 info = NULL;
125
126 if (info && *info)
127 return info;
128 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200129 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200130}
131
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100132const char *get_analyze_status(short analyze_status) {
133
134 const char *desc;
135
136 if (analyze_status < HANA_STATUS_SIZE)
137 desc = analyze_statuses[analyze_status].desc;
138 else
139 desc = NULL;
140
141 if (desc && *desc)
142 return desc;
143 else
144 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
145}
146
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200147#define SSP_O_HCHK 0x0002
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200148
149static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
150
Krzysztof Piotr Oledzki5f5b7d22010-01-11 11:13:39 +0100151 if (s->tracked)
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200152 chunk_printf(msg, " via %s/%s",
153 s->tracked->proxy->id, s->tracked->id);
154
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200155 if (options & SSP_O_HCHK) {
156 chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status));
157
158 if (s->check_status >= HCHK_STATUS_L57DATA)
159 chunk_printf(msg, ", code: %d", s->check_code);
160
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200161 if (*s->check_desc) {
162 struct chunk src;
163
164 chunk_printf(msg, ", info: \"");
165
166 chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc));
167 chunk_asciiencode(msg, &src, '"');
168
169 chunk_printf(msg, "\"");
170 }
171
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100172 if (s->check_duration >= 0)
173 chunk_printf(msg, ", check duration: %ldms", s->check_duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200174 }
175
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200176 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100177 if (!(s->state & SRV_RUNNING))
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200178 chunk_printf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100179 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200180 s->proxy->srv_act, s->proxy->srv_bck,
181 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
182 s->cur_sess, xferred, s->nbpend);
183 else
184 chunk_printf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100185 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200186 s->proxy->srv_act, s->proxy->srv_bck,
187 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
188 xferred, s->nbpend);
189 }
190}
191
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200192/*
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200193 * Set s->check_status, update s->check_duration and fill s->result with
194 * an adequate SRV_CHK_* value.
195 *
196 * Show information in logs about failed health check if server is UP
197 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200198 */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200199static void set_server_check_status(struct server *s, short status, char *desc) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200200
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200201 struct chunk msg;
202
203 if (status == HCHK_STATUS_START) {
204 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200205 s->check_desc[0] = '\0';
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200206 s->check_start = now;
207 return;
208 }
209
210 if (!s->check_status)
211 return;
212
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200213 if (desc && *desc) {
214 strncpy(s->check_desc, desc, HCHK_DESC_LEN-1);
215 s->check_desc[HCHK_DESC_LEN-1] = '\0';
216 } else
217 s->check_desc[0] = '\0';
218
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200219 s->check_status = status;
220 if (check_statuses[status].result)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100221 s->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200222
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100223 if (status == HCHK_STATUS_HANA)
224 s->check_duration = -1;
225 else if (!tv_iszero(&s->check_start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200226 /* set_server_check_status() may be called more than once */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200227 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
228 tv_zero(&s->check_start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200229 }
230
231 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
232 (((s->health != 0) && (s->result & SRV_CHK_ERROR)) ||
233 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) ||
234 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
235 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
236
237 int health, rise, fall, state;
238
239 chunk_init(&msg, trash, sizeof(trash));
240
241 /* FIXME begin: calculate local version of the health/rise/fall/state */
242 health = s->health;
243 rise = s->rise;
244 fall = s->fall;
245 state = s->state;
246
247 if (s->result & SRV_CHK_ERROR) {
248 if (health > rise) {
249 health--; /* still good */
250 } else {
251 if (health == rise)
252 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
253
254 health = 0;
255 }
256 }
257
258 if (s->result & SRV_CHK_RUNNING) {
259 if (health < rise + fall - 1) {
260 health++; /* was bad, stays for a while */
261
262 if (health == rise)
263 state |= SRV_RUNNING;
264
265 if (health >= rise)
266 health = rise + fall - 1; /* OK now */
267 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100268
269 /* clear consecutive_errors if observing is enabled */
270 if (s->onerror)
271 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200272 }
273 /* FIXME end: calculate local version of the health/rise/fall/state */
274
275 chunk_printf(&msg,
276 "Health check for %sserver %s/%s %s%s",
277 s->state & SRV_BACKUP ? "backup " : "",
278 s->proxy->id, s->id,
279 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
280 (s->result & SRV_CHK_RUNNING)?"succeeded":"failed");
281
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200282 server_status_printf(&msg, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200283
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100284 chunk_printf(&msg, ", status: %d/%d %s",
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
286 (state & SRV_RUNNING) ? (fall) : (rise),
287 (state & SRV_RUNNING)?"UP":"DOWN");
288
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100289 Warning("%s.\n", trash);
290 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200291 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200292}
293
Willy Tarreau48494c02007-11-30 10:41:39 +0100294/* sends a log message when a backend goes down, and also sets last
295 * change date.
296 */
297static void set_backend_down(struct proxy *be)
298{
299 be->last_change = now.tv_sec;
300 be->down_trans++;
301
302 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
303 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
304}
305
306/* Redistribute pending connections when a server goes down. The number of
307 * connections redistributed is returned.
308 */
309static int redistribute_pending(struct server *s)
310{
311 struct pendconn *pc, *pc_bck, *pc_end;
312 int xferred = 0;
313
314 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
315 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100316 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
317 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100318 /* The REDISP option was specified. We will ignore
319 * cookie and force to balance or use the dispatcher.
320 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100321
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100322 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100323 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100324
Willy Tarreau48494c02007-11-30 10:41:39 +0100325 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200326 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100327 xferred++;
328 }
329 }
330 return xferred;
331}
332
333/* Check for pending connections at the backend, and assign some of them to
334 * the server coming up. The server's weight is checked before being assigned
335 * connections it may not be able to handle. The total number of transferred
336 * connections is returned.
337 */
338static int check_for_pending(struct server *s)
339{
340 int xferred;
341
342 if (!s->eweight)
343 return 0;
344
345 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
346 struct session *sess;
347 struct pendconn *p;
348
349 p = pendconn_from_px(s->proxy);
350 if (!p)
351 break;
352 p->sess->srv = s;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100353 set_target_server(&p->sess->target, s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100354 sess = p->sess;
355 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200356 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100357 }
358 return xferred;
359}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360
361/* Sets server <s> down, notifies by all available means, recounts the
362 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200363 * possible to other servers. It automatically recomputes the number of
364 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100366void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100368 struct server *srv;
369 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370 int xferred;
371
Cyril Bontécd19e512010-01-31 22:34:03 +0100372 if (s->state & SRV_MAINTAIN) {
373 s->health = s->rise;
374 }
375
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100376 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100377 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200378 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200379
380 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100381 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100382 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383
384 /* we might have sessions queued on this server and waiting for
385 * a connection. Those which are redispatchable will be queued
386 * to another server or to the proxy itself.
387 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100388 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100389
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200390 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100391
Cyril Bontécd19e512010-01-31 22:34:03 +0100392 if (s->state & SRV_MAINTAIN) {
393 chunk_printf(&msg,
394 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
395 s->proxy->id, s->id);
396 } else {
397 chunk_printf(&msg,
398 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
399 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100400
Cyril Bontécd19e512010-01-31 22:34:03 +0100401 server_status_printf(&msg, s,
402 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
403 xferred);
404 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100405 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200406
Willy Tarreau48494c02007-11-30 10:41:39 +0100407 /* we don't send an alert if the server was previously paused */
408 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100409 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100410 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100411 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200412
Willy Tarreaud64d2252010-10-17 17:16:42 +0200413 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100414 set_backend_down(s->proxy);
415
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200416 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100417
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100418 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100419 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100420 if (! (srv->state & SRV_MAINTAIN))
421 /* Only notify tracking servers that are not already in maintenance. */
422 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100424
Willy Tarreaubaaee002006-06-26 02:48:02 +0200425 s->health = 0; /* failure */
426}
427
Cyril Bontécd19e512010-01-31 22:34:03 +0100428void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100429
430 struct server *srv;
431 struct chunk msg;
432 int xferred;
433
Cyril Bontécd19e512010-01-31 22:34:03 +0100434 if (s->state & SRV_MAINTAIN) {
435 s->health = s->rise;
436 }
437
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100438 if (s->health == s->rise || s->tracked) {
439 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
440 if (s->proxy->last_change < now.tv_sec) // ignore negative times
441 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
442 s->proxy->last_change = now.tv_sec;
443 }
444
445 if (s->last_change < now.tv_sec) // ignore negative times
446 s->down_time += now.tv_sec - s->last_change;
447
448 s->last_change = now.tv_sec;
449 s->state |= SRV_RUNNING;
450
451 if (s->slowstart > 0) {
452 s->state |= SRV_WARMINGUP;
453 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
454 /* For dynamic algorithms, start at the first step of the weight,
455 * without multiplying by BE_WEIGHT_SCALE.
456 */
457 s->eweight = s->uweight;
458 if (s->proxy->lbprm.update_server_eweight)
459 s->proxy->lbprm.update_server_eweight(s);
460 }
461 }
462 s->proxy->lbprm.set_server_status_up(s);
463
464 /* check if we can handle some connections queued at the proxy. We
465 * will take as many as we can handle.
466 */
467 xferred = check_for_pending(s);
468
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200469 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100470
Cyril Bontécd19e512010-01-31 22:34:03 +0100471 if (s->state & SRV_MAINTAIN) {
472 chunk_printf(&msg,
473 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
474 s->proxy->id, s->id);
475 } else {
476 chunk_printf(&msg,
477 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
478 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100479
Cyril Bontécd19e512010-01-31 22:34:03 +0100480 server_status_printf(&msg, s,
481 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
482 xferred);
483 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100484
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100485 Warning("%s.\n", trash);
486 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100487
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100488 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100489 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100490 if (! (srv->state & SRV_MAINTAIN))
491 /* Only notify tracking servers if they're not in maintenance. */
492 set_server_up(srv);
493
494 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100495 }
496
497 if (s->health >= s->rise)
498 s->health = s->rise + s->fall - 1; /* OK now */
499
500}
501
502static void set_server_disabled(struct server *s) {
503
504 struct server *srv;
505 struct chunk msg;
506 int xferred;
507
508 s->state |= SRV_GOINGDOWN;
509 s->proxy->lbprm.set_server_status_down(s);
510
511 /* we might have sessions queued on this server and waiting for
512 * a connection. Those which are redispatchable will be queued
513 * to another server or to the proxy itself.
514 */
515 xferred = redistribute_pending(s);
516
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200517 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100518
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200519 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100520 "Load-balancing on %sServer %s/%s is disabled",
521 s->state & SRV_BACKUP ? "Backup " : "",
522 s->proxy->id, s->id);
523
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200524 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200525 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100526 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100527
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100528 Warning("%s.\n", trash);
529 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100530
531 if (!s->proxy->srv_bck && !s->proxy->srv_act)
532 set_backend_down(s->proxy);
533
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100534 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100535 for(srv = s->tracknext; srv; srv = srv->tracknext)
536 set_server_disabled(srv);
537}
538
539static void set_server_enabled(struct server *s) {
540
541 struct server *srv;
542 struct chunk msg;
543 int xferred;
544
545 s->state &= ~SRV_GOINGDOWN;
546 s->proxy->lbprm.set_server_status_up(s);
547
548 /* check if we can handle some connections queued at the proxy. We
549 * will take as many as we can handle.
550 */
551 xferred = check_for_pending(s);
552
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200553 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100554
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200555 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100556 "Load-balancing on %sServer %s/%s is enabled again",
557 s->state & SRV_BACKUP ? "Backup " : "",
558 s->proxy->id, s->id);
559
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200560 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200561 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100562 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100563
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100564 Warning("%s.\n", trash);
565 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100566
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100567 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100568 for(srv = s->tracknext; srv; srv = srv->tracknext)
569 set_server_enabled(srv);
570}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100572void health_adjust(struct server *s, short status) {
573
574 int failed;
575 int expire;
576
577 /* return now if observing nor health check is not enabled */
578 if (!s->observe || !s->check)
579 return;
580
581 if (s->observe >= HANA_OBS_SIZE)
582 return;
583
584 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
585 return;
586
587 switch (analyze_statuses[status].lr[s->observe - 1]) {
588 case 1:
589 failed = 1;
590 break;
591
592 case 2:
593 failed = 0;
594 break;
595
596 default:
597 return;
598 }
599
600 if (!failed) {
601 /* good: clear consecutive_errors */
602 s->consecutive_errors = 0;
603 return;
604 }
605
606 s->consecutive_errors++;
607
608 if (s->consecutive_errors < s->consecutive_errors_limit)
609 return;
610
611 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
612 s->consecutive_errors, get_analyze_status(status));
613
614 switch (s->onerror) {
615 case HANA_ONERR_FASTINTER:
616 /* force fastinter - nothing to do here as all modes force it */
617 break;
618
619 case HANA_ONERR_SUDDTH:
620 /* simulate a pre-fatal failed health check */
621 if (s->health > s->rise)
622 s->health = s->rise + 1;
623
624 /* no break - fall through */
625
626 case HANA_ONERR_FAILCHK:
627 /* simulate a failed health check */
628 set_server_check_status(s, HCHK_STATUS_HANA, trash);
629
630 if (s->health > s->rise) {
631 s->health--; /* still good */
632 s->counters.failed_checks++;
633 }
634 else
635 set_server_down(s);
636
637 break;
638
639 case HANA_ONERR_MARKDWN:
640 /* mark server down */
641 s->health = s->rise;
642 set_server_check_status(s, HCHK_STATUS_HANA, trash);
643 set_server_down(s);
644
645 break;
646
647 default:
648 /* write a warning? */
649 break;
650 }
651
652 s->consecutive_errors = 0;
653 s->counters.failed_hana++;
654
655 if (s->fastinter) {
656 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
657 if (s->check->expire > expire)
658 s->check->expire = expire;
659 }
660}
661
Willy Tarreauef781042010-01-27 11:53:01 +0100662static int httpchk_build_status_header(struct server *s, char *buffer)
663{
664 int sv_state;
665 int ratio;
666 int hlen = 0;
667 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
668 "UP %d/%d", "UP",
669 "NOLB %d/%d", "NOLB",
670 "no check" };
671
672 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
673 hlen += 24;
674
675 if (!(s->state & SRV_CHECKED))
676 sv_state = 6; /* should obviously never happen */
677 else if (s->state & SRV_RUNNING) {
678 if (s->health == s->rise + s->fall - 1)
679 sv_state = 3; /* UP */
680 else
681 sv_state = 2; /* going down */
682
683 if (s->state & SRV_GOINGDOWN)
684 sv_state += 2;
685 } else {
686 if (s->health)
687 sv_state = 1; /* going up */
688 else
689 sv_state = 0; /* DOWN */
690 }
691
692 hlen += sprintf(buffer + hlen,
693 srv_hlt_st[sv_state],
694 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
695 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
696
697 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
698 s->proxy->id, s->id,
699 global.node,
700 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
701 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
702 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
703 s->nbpend);
704
705 if ((s->state & SRV_WARMINGUP) &&
706 now.tv_sec < s->last_change + s->slowstart &&
707 now.tv_sec >= s->last_change) {
708 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
709 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
710 }
711
712 buffer[hlen++] = '\r';
713 buffer[hlen++] = '\n';
714
715 return hlen;
716}
717
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718/*
719 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200720 * the connection acknowledgement. If the proxy requires L7 health-checks,
721 * it sends the request. In other cases, it calls set_server_check_status()
722 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200723 * The function itself returns 0 if it needs some polling before being called
724 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 */
Willy Tarreau83749182007-04-15 20:56:27 +0200726static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727{
Willy Tarreau6996e152007-04-30 14:37:43 +0200728 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 struct task *t = fdtab[fd].owner;
730 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100732 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200733 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100734 int skerr, err = errno;
735 socklen_t lskerr = sizeof(skerr);
736
737 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
738 err = skerr;
739
740 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200741 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200742 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200743
744 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200745
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100746 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200748 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Hervé COMMOWICK698ae002010-01-12 09:25:13 +0100749 (s->proxy->options & PR_O_SMTP_CHK) ||
Willy Tarreau07a54902010-03-29 18:33:29 +0200750 (s->proxy->options2 & PR_O2_SSL3_CHK) ||
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200751 (s->proxy->options2 & PR_O2_MYSQL_CHK) ||
Rauf Kuliyev38b41562011-01-04 15:14:13 +0100752 (s->proxy->options2 & PR_O2_PGSQL_CHK) ||
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200753 (s->proxy->options2 & PR_O2_LDAP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100755 const char *check_req = s->proxy->check_req;
756 int check_len = s->proxy->check_len;
757
Willy Tarreauf3c69202006-07-09 16:42:34 +0200758 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 * so we'll send the request, and won't wake the checker up now.
760 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200761
Willy Tarreau07a54902010-03-29 18:33:29 +0200762 if (s->proxy->options2 & PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200763 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200764 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200765 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
766 }
Willy Tarreaue9d87882010-01-27 11:28:42 +0100767 else if (s->proxy->options & PR_O_HTTP_CHK) {
768 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100769
770 if (s->proxy->options2 & PR_O2_CHK_SNDST)
771 check_len += httpchk_build_status_header(s, trash + check_len);
772
Willy Tarreaue9d87882010-01-27 11:28:42 +0100773 trash[check_len++] = '\r';
774 trash[check_len++] = '\n';
775 trash[check_len] = '\0';
776 check_req = trash;
777 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200778
Willy Tarreaue9d87882010-01-27 11:28:42 +0100779 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
780 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100781 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100782 if (s->proxy->timeout.check)
783 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200784 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200785 goto out_nowake;
786 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200787 else if (ret == 0 || errno == EAGAIN)
788 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200789 else {
790 switch (errno) {
791 case ECONNREFUSED:
792 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200793 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200794 break;
795
796 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200797 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200798 }
799
Willy Tarreau6996e152007-04-30 14:37:43 +0200800 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200801 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802 }
803 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200804 /* We have no data to send to check the connection, and
805 * getsockopt() will not inform us whether the connection
806 * is still pending. So we'll reuse connect() to check the
807 * state of the socket. This has the advantage of givig us
808 * the following info :
809 * - error
810 * - connecting (EALREADY, EINPROGRESS)
811 * - connected (EISCONN, 0)
812 */
813
814 struct sockaddr_in sa;
815
816 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
817 sa.sin_port = htons(s->check_port);
818
819 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
820 errno = 0;
821
822 if (errno == EALREADY || errno == EINPROGRESS)
823 goto out_poll;
824
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200825 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200826 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200827 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200828 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200829
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200831 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200832 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 }
834 }
Willy Tarreau83749182007-04-15 20:56:27 +0200835 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200836 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200837 out_nowake:
838 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100839 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200840 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200841 out_poll:
842 /* The connection is still pending. We'll have to poll it
843 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100844 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200845 return 0;
846 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200847 fdtab[fd].state = FD_STERROR;
848 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849}
850
851
852/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200853 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200854 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
855 * set_server_check_status() to update s->check_status, s->check_duration
856 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200857
858 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
859 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
860 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
861 * response to an SSL HELLO (the principle is that this is enough to
862 * distinguish between an SSL server and a pure TCP relay). All other cases will
863 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
864 * etc.
865 *
866 * The function returns 0 if it needs to be called again after some polling,
867 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200868 */
Willy Tarreau83749182007-04-15 20:56:27 +0200869static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870{
Willy Tarreau83749182007-04-15 20:56:27 +0200871 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100872 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 struct task *t = fdtab[fd].owner;
874 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200875 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100876 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200877 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200878
Willy Tarreau659d7bc2010-03-16 21:14:41 +0100879 if (unlikely((s->result & SRV_CHK_ERROR) || (fdtab[fd].state == FD_STERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200880 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200881 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200882 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200883
Willy Tarreau83749182007-04-15 20:56:27 +0200884 goto out_wakeup;
885 }
886
Willy Tarreau83749182007-04-15 20:56:27 +0200887 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
888 * but the connection was closed on the remote end. Fortunately, recv still
889 * works correctly and we don't need to do the getsockopt() on linux.
890 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000891
892 /* Set buffer to point to the end of the data already read, and check
893 * that there is free space remaining. If the buffer is full, proceed
894 * with running the checks without attempting another socket read.
895 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000896
Willy Tarreau03938182010-03-17 21:52:07 +0100897 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200898 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
899 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 +0100900 if (len <= 0)
901 break;
902 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000903
Willy Tarreau03938182010-03-17 21:52:07 +0100904 if (len == 0)
905 done = 1; /* connection hangup received */
906 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100907 /* Report network errors only if we got no other data. Otherwise
908 * we'll let the upper layers decide whether the response is OK
909 * or not. It is very common that an RST sent by the server is
910 * reported as an error just after the last data chunk.
911 */
Willy Tarreau03938182010-03-17 21:52:07 +0100912 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100913 if (!s->check_data_len) {
914 if (!(s->result & SRV_CHK_ERROR))
915 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
916 goto out_wakeup;
917 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918 }
919
Willy Tarreau03938182010-03-17 21:52:07 +0100920 /* Intermediate or complete response received.
921 * Terminate string in check_data buffer.
922 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200923 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100924 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100925 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100926 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100927 done = 1; /* buffer full, don't wait for more data */
928 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200929
Nick Chalk57b1bf72010-03-16 15:50:46 +0000930 /* Run the checks... */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100931 if (s->proxy->options & PR_O_HTTP_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100932 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
933 goto wait_more_data;
934
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100935 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000936 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
937 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
938 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
939 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
940 !isdigit((unsigned char) *(s->check_data + 11))) {
941 cut_crlf(s->check_data);
942 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200943
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100944 goto out_wakeup;
945 }
946
Nick Chalk57b1bf72010-03-16 15:50:46 +0000947 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000948 desc = ltrim(s->check_data + 12, ' ');
949
Willy Tarreaubd741542010-03-16 18:46:54 +0100950 if ((s->proxy->options & PR_O_DISABLE404) &&
951 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000952 /* 404 may be accepted as "stopping" only if the server was up */
953 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200954 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000955 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100956 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
957 /* Run content verification check... We know we have at least 13 chars */
958 if (!httpchk_expect(s, done))
959 goto wait_more_data;
960 }
961 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
962 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
963 cut_crlf(desc);
964 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
965 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000966 else {
967 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200968 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000969 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100970 }
Willy Tarreau07a54902010-03-29 18:33:29 +0200971 else if (s->proxy->options2 & PR_O2_SSL3_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100972 if (!done && s->check_data_len < 5)
973 goto wait_more_data;
974
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100975 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000976 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200977 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200978 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200979 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200980 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100981 else if (s->proxy->options & PR_O_SMTP_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100982 if (!done && s->check_data_len < strlen("000\r"))
983 goto wait_more_data;
984
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200985 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000986 if ((s->check_data_len < strlen("000\r")) ||
987 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
988 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
989 !isdigit((unsigned char) *(s->check_data + 2))) {
990 cut_crlf(s->check_data);
991 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200992
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200993 goto out_wakeup;
994 }
995
Nick Chalk57b1bf72010-03-16 15:50:46 +0000996 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200997
Nick Chalk57b1bf72010-03-16 15:50:46 +0000998 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200999 cut_crlf(desc);
1000
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001001 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001002 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001003 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001004 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001005 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau6996e152007-04-30 14:37:43 +02001006 }
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001007 else if (s->proxy->options2 & PR_O2_PGSQL_CHK) {
1008 if (!done && s->check_data_len < 9)
1009 goto wait_more_data;
1010
1011 if (s->check_data[0] == 'R') {
1012 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1013 }
1014 else {
1015 if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0))
1016 desc = &s->check_data[6];
1017 else
1018 desc = "PostgreSQL unknown error";
1019
1020 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1021 }
1022 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001023 else if (s->proxy->options2 & PR_O2_MYSQL_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +01001024 if (!done && s->check_data_len < 5)
1025 goto wait_more_data;
1026
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001027 if (s->proxy->check_len == 0) { // old mode
1028 if (*(s->check_data + 4) != '\xff') {
1029 /* We set the MySQL Version in description for information purpose
1030 * FIXME : it can be cool to use MySQL Version for other purpose,
1031 * like mark as down old MySQL server.
1032 */
1033 if (s->check_data_len > 51) {
1034 desc = ltrim(s->check_data + 5, ' ');
1035 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1036 }
1037 else {
1038 if (!done)
1039 goto wait_more_data;
1040 /* it seems we have a OK packet but without a valid length,
1041 * it must be a protocol error
1042 */
1043 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1044 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001045 }
1046 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001047 /* An error message is attached in the Error packet */
1048 desc = ltrim(s->check_data + 7, ' ');
1049 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1050 }
1051 } else {
1052 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1053 (((unsigned int) *(s->check_data + 1)) << 8) +
1054 (((unsigned int) *(s->check_data + 2)) << 16);
1055
1056 if (s->check_data_len == first_packet_len + 4) {
1057 /* MySQL Error packet always begin with field_count = 0xff */
1058 if (*(s->check_data + 4) != '\xff') {
1059 /* We have only one MySQL packet and it is a Handshake Initialization packet
1060 * but we need to have a second packet to know if it is alright
1061 */
1062 if (!done && s->check_data_len < first_packet_len + 5)
1063 goto wait_more_data;
1064 }
1065 else {
1066 /* We have only one packet and it is an Error packet,
1067 * an error message is attached, so we can display it
1068 */
1069 desc = &s->check_data[7];
1070 //Warning("onlyoneERR: %s\n", desc);
1071 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1072 }
1073 } else if (s->check_data_len > first_packet_len + 4) {
1074 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1075 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1076 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1077
1078 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1079 /* We have 2 packets and that's good */
1080 /* Check if the second packet is a MySQL Error packet or not */
1081 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1082 /* No error packet */
1083 /* We set the MySQL Version in description for information purpose */
1084 desc = &s->check_data[5];
1085 //Warning("2packetOK: %s\n", desc);
1086 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1087 }
1088 else {
1089 /* An error message is attached in the Error packet
1090 * so we can display it ! :)
1091 */
1092 desc = &s->check_data[first_packet_len+11];
1093 //Warning("2packetERR: %s\n", desc);
1094 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1095 }
1096 }
1097 }
1098 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001099 if (!done)
1100 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001101 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001102 * it must be a protocol error
1103 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001104 desc = &s->check_data[5];
1105 //Warning("protoerr: %s\n", desc);
1106 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001107 }
1108 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001109 }
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001110 else if (s->proxy->options2 & PR_O2_LDAP_CHK) {
1111 if (!done && s->check_data_len < 14)
1112 goto wait_more_data;
1113
1114 /* Check if the server speaks LDAP (ASN.1/BER)
1115 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1116 * http://tools.ietf.org/html/rfc4511
1117 */
1118
1119 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1120 * LDAPMessage: 0x30: SEQUENCE
1121 */
1122 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1123 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1124 }
1125 else {
1126 /* size of LDAPMessage */
1127 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1128
1129 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1130 * messageID: 0x02 0x01 0x01: INTEGER 1
1131 * protocolOp: 0x61: bindResponse
1132 */
1133 if ((msglen > 2) ||
1134 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1135 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1136
1137 goto out_wakeup;
1138 }
1139
1140 /* size of bindResponse */
1141 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1142
1143 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1144 * ldapResult: 0x0a 0x01: ENUMERATION
1145 */
1146 if ((msglen > 4) ||
1147 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1148 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1149
1150 goto out_wakeup;
1151 }
1152
1153 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1154 * resultCode
1155 */
1156 s->check_code = *(s->check_data + msglen + 9);
1157 if (s->check_code) {
1158 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1159 } else {
1160 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1161 }
1162 }
1163 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001164 else {
1165 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001166 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau23677902007-05-08 23:50:35 +02001167 }
Willy Tarreau83749182007-04-15 20:56:27 +02001168
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001169 out_wakeup:
1170 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171 fdtab[fd].state = FD_STERROR;
1172
Nick Chalk57b1bf72010-03-16 15:50:46 +00001173 /* Reset the check buffer... */
1174 *s->check_data = '\0';
1175 s->check_data_len = 0;
1176
Willy Tarreau03938182010-03-17 21:52:07 +01001177 /* Close the connection... */
1178 shutdown(fd, SHUT_RDWR);
Willy Tarreauf161a342007-04-08 16:59:42 +02001179 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001180 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +01001181 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +02001182 return 1;
Willy Tarreau03938182010-03-17 21:52:07 +01001183
1184 wait_more_data:
1185 fdtab[fd].ev &= ~FD_POLL_IN;
1186 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001187}
1188
1189/*
1190 * manages a server health-check. Returns
1191 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1192 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001193struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194{
Willy Tarreaue3838802009-03-21 18:58:32 +01001195 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001196 struct server *s = t->context;
1197 struct sockaddr_in sa;
1198 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001199 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200
1201 //fprintf(stderr, "process_chk: task=%p\n", t);
1202
1203 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001204 if (attempts++ > 0) {
1205 /* we always fail to create a server, let's stop insisting... */
1206 while (tick_is_expired(t->expire, now_ms))
1207 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1208 return t;
1209 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001210 fd = s->curfd;
1211 if (fd < 0) { /* no check currently running */
1212 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +01001213 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1214 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001215
1216 /* we don't send any health-checks when the proxy is stopped or when
1217 * the server should not be checked.
1218 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001219 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001220 while (tick_is_expired(t->expire, now_ms))
1221 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001222 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001223 }
1224
1225 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001226 set_server_check_status(s, HCHK_STATUS_START, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
1228 if ((fd < global.maxsock) &&
1229 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1230 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1231 //fprintf(stderr, "process_chk: 3\n");
1232
Willy Tarreau9edd1612007-10-18 18:07:48 +02001233 if (s->proxy->options & PR_O_TCP_NOLING) {
1234 /* We don't want to useless data */
1235 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1236 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001237
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001238 if (s->check_addr.sin_addr.s_addr)
1239 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001240 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001241 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001242 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001243 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001244
Willy Tarreaubaaee002006-06-26 02:48:02 +02001245 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246 sa.sin_port = htons(s->check_port);
1247
1248 /* allow specific binding :
1249 * - server-specific at first
1250 * - proxy-specific next
1251 */
1252 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001253 struct sockaddr_in *remote = NULL;
1254 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001255
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001256#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001257 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
1258 remote = (struct sockaddr_in *)&s->tproxy_addr;
1259 flags = 3;
1260 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001261#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001262#ifdef SO_BINDTODEVICE
1263 /* Note: this might fail if not CAP_NET_RAW */
1264 if (s->iface_name)
1265 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001266 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001267#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001268 if (s->sport_range) {
1269 int bind_attempts = 10; /* should be more than enough to find a spare port */
1270 struct sockaddr_in src;
1271
1272 ret = 1;
1273 src = s->source_addr;
1274
1275 do {
1276 /* note: in case of retry, we may have to release a previously
1277 * allocated port, hence this loop's construct.
1278 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001279 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1280 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001281
1282 if (!bind_attempts)
1283 break;
1284 bind_attempts--;
1285
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001286 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1287 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001288 break;
1289
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001290 fdinfo[fd].port_range = s->sport_range;
1291 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001292
1293 ret = tcpv4_bind_socket(fd, flags, &src, remote);
1294 } while (ret != 0); /* binding NOK */
1295 }
1296 else {
1297 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
1298 }
1299
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001300 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001301 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001302 switch (ret) {
1303 case 1:
1304 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1305 s->proxy->id, s->id);
1306 break;
1307 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001308 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1309 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001310 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001311 }
1312 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 }
1314 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001315 struct sockaddr_in *remote = NULL;
1316 int ret, flags = 0;
1317
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001318#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001319 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001320 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
1321 flags = 3;
1322 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001323#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001324#ifdef SO_BINDTODEVICE
1325 /* Note: this might fail if not CAP_NET_RAW */
1326 if (s->proxy->iface_name)
1327 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001328 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001329#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001330 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
1331 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001332 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001333 switch (ret) {
1334 case 1:
1335 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1336 proxy_type_str(s->proxy), s->proxy->id);
1337 break;
1338 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001339 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1340 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001341 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001342 }
1343 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001344 }
1345
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001346 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001347#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001348 /* disabling tcp quick ack now allows
1349 * the request to leave the machine with
1350 * the first ACK.
1351 */
1352 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001353 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001354#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001355 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
1356 /* OK, connection in progress or established */
1357
1358 //fprintf(stderr, "process_chk: 4\n");
1359
1360 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +02001361 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001362 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +02001363 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
1364 fdtab[fd].cb[DIR_RD].b = NULL;
1365 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
1366 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001367 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
1368 fdinfo[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001370 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001371 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001372#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001373 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001374#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001375 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1376 /* we allow up to min(inter, timeout.connect) for a connection
1377 * to establish but only when timeout.check is set
1378 * as it may be to short for a full check otherwise
1379 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001380 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001381
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001382 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1383 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1384 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001385 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001386 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001387 }
1388 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001389 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001390
1391 switch (errno) {
1392 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1393 case ECONNREFUSED:
1394 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001395 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001396 break;
1397
1398 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001399 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001400 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401 }
1402 }
1403 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001404 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1405 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001406 close(fd); /* socket creation error */
1407 }
1408
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001409 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001410 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001411 while (tick_is_expired(t->expire, now_ms))
1412 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001413 goto new_chk; /* may be we should initialize a new check */
1414 }
1415
1416 /* here, we have seen a failure */
1417 if (s->health > s->rise) {
1418 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001419 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420 }
1421 else
1422 set_server_down(s);
1423
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001424 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1425 /* we allow up to min(inter, timeout.connect) for a connection
1426 * to establish but only when timeout.check is set
1427 * as it may be to short for a full check otherwise
1428 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001429 while (tick_is_expired(t->expire, now_ms)) {
1430 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001431
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001432 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1433 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001434
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001435 if (s->proxy->timeout.check)
1436 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001437 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001438 goto new_chk;
1439 }
1440 else {
1441 //fprintf(stderr, "process_chk: 8\n");
1442 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001443 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001444 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001445
Willy Tarreau9909fc12007-11-30 17:42:05 +01001446 if (s->state & SRV_WARMINGUP) {
1447 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1448 s->state &= ~SRV_WARMINGUP;
1449 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1450 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1451 if (s->proxy->lbprm.update_server_eweight)
1452 s->proxy->lbprm.update_server_eweight(s);
1453 }
1454 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1455 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +01001456 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1457 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +01001458 s->eweight *= s->uweight;
1459 if (s->proxy->lbprm.update_server_eweight)
1460 s->proxy->lbprm.update_server_eweight(s);
1461 }
1462 /* probably that we can refill this server with a bit more connections */
1463 check_for_pending(s);
1464 }
1465
Willy Tarreau48494c02007-11-30 10:41:39 +01001466 /* we may have to add/remove this server from the LB group */
1467 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1468 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001469 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1470 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001471 else if (!(s->state & SRV_GOINGDOWN) &&
1472 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001473 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1474 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001475 }
1476
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001477 if (s->health < s->rise + s->fall - 1) {
1478 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001479
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001480 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001481 }
1482 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001483 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001484
1485 rv = 0;
1486 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001487 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001488 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001489 //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 +02001490 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001491 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001492 goto new_chk;
1493 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001494 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001495 if (!(s->result & SRV_CHK_ERROR)) {
1496 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001497 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001498 } else {
Willy Tarreau07a54902010-03-29 18:33:29 +02001499 if (s->proxy->options2 & PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001500 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001501 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001502 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001503 }
1504 }
1505
Willy Tarreaubaaee002006-06-26 02:48:02 +02001506 //fprintf(stderr, "process_chk: 10\n");
1507 /* failure or timeout detected */
1508 if (s->health > s->rise) {
1509 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001510 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001511 }
1512 else
1513 set_server_down(s);
1514 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001515 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001516
1517 rv = 0;
1518 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001519 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001520 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001521 //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 +02001522 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001523 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001524 goto new_chk;
1525 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001526 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001527 }
1528 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001529 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001530 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531}
1532
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001533/*
1534 * Start health-check.
1535 * Returns 0 if OK, -1 if error, and prints the error in this case.
1536 */
1537int start_checks() {
1538
1539 struct proxy *px;
1540 struct server *s;
1541 struct task *t;
1542 int nbchk=0, mininter=0, srvpos=0;
1543
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001544 /* 1- count the checkers to run simultaneously.
1545 * We also determine the minimum interval among all of those which
1546 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1547 * will be used to spread their start-up date. Those which have
1548 * a shorter interval will start independantly and will not dictate
1549 * too short an interval for all others.
1550 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001551 for (px = proxy; px; px = px->next) {
1552 for (s = px->srv; s; s = s->next) {
1553 if (!(s->state & SRV_CHECKED))
1554 continue;
1555
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001556 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1557 (!mininter || mininter > srv_getinter(s)))
1558 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001559
1560 nbchk++;
1561 }
1562 }
1563
1564 if (!nbchk)
1565 return 0;
1566
1567 srand((unsigned)time(NULL));
1568
1569 /*
1570 * 2- start them as far as possible from each others. For this, we will
1571 * start them after their interval set to the min interval divided by
1572 * the number of servers, weighted by the server's position in the list.
1573 */
1574 for (px = proxy; px; px = px->next) {
1575 for (s = px->srv; s; s = s->next) {
1576 if (!(s->state & SRV_CHECKED))
1577 continue;
1578
Willy Tarreaua4613182009-03-21 18:13:21 +01001579 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001580 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1581 return -1;
1582 }
1583
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001584 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001585 t->process = process_chk;
1586 t->context = s;
1587
1588 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001589 t->expire = tick_add(now_ms,
1590 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1591 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001592 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001593 task_queue(t);
1594
1595 srvpos++;
1596 }
1597 }
1598 return 0;
1599}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001600
1601/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001602 * Perform content verification check on data in s->check_data buffer.
1603 * The buffer MUST be terminated by a null byte before calling this function.
1604 * Sets server status appropriately. The caller is responsible for ensuring
1605 * that the buffer contains at least 13 characters. If <done> is zero, we may
1606 * return 0 to indicate that data is required to decide of a match.
1607 */
1608static int httpchk_expect(struct server *s, int done)
1609{
1610 static char status_msg[] = "HTTP status check returned code <000>";
1611 char status_code[] = "000";
1612 char *contentptr;
1613 int crlf;
1614 int ret;
1615
1616 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1617 case PR_O2_EXP_STS:
1618 case PR_O2_EXP_RSTS:
1619 memcpy(status_code, s->check_data + 9, 3);
1620 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1621
1622 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1623 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1624 else
1625 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1626
1627 /* we necessarily have the response, so there are no partial failures */
1628 if (s->proxy->options2 & PR_O2_EXP_INV)
1629 ret = !ret;
1630
1631 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1632 break;
1633
1634 case PR_O2_EXP_STR:
1635 case PR_O2_EXP_RSTR:
1636 /* very simple response parser: ignore CR and only count consecutive LFs,
1637 * stop with contentptr pointing to first char after the double CRLF or
1638 * to '\0' if crlf < 2.
1639 */
1640 crlf = 0;
1641 for (contentptr = s->check_data; *contentptr; contentptr++) {
1642 if (crlf >= 2)
1643 break;
1644 if (*contentptr == '\r')
1645 continue;
1646 else if (*contentptr == '\n')
1647 crlf++;
1648 else
1649 crlf = 0;
1650 }
1651
1652 /* Check that response contains a body... */
1653 if (crlf < 2) {
1654 if (!done)
1655 return 0;
1656
1657 set_server_check_status(s, HCHK_STATUS_L7RSP,
1658 "HTTP content check could not find a response body");
1659 return 1;
1660 }
1661
1662 /* Check that response body is not empty... */
1663 if (*contentptr == '\0') {
1664 set_server_check_status(s, HCHK_STATUS_L7RSP,
1665 "HTTP content check found empty response body");
1666 return 1;
1667 }
1668
1669 /* Check the response content against the supplied string
1670 * or regex... */
1671 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1672 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1673 else
1674 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1675
1676 /* if we don't match, we may need to wait more */
1677 if (!ret && !done)
1678 return 0;
1679
1680 if (ret) {
1681 /* content matched */
1682 if (s->proxy->options2 & PR_O2_EXP_INV)
1683 set_server_check_status(s, HCHK_STATUS_L7RSP,
1684 "HTTP check matched unwanted content");
1685 else
1686 set_server_check_status(s, HCHK_STATUS_L7OKD,
1687 "HTTP content check matched");
1688 }
1689 else {
1690 if (s->proxy->options2 & PR_O2_EXP_INV)
1691 set_server_check_status(s, HCHK_STATUS_L7OKD,
1692 "HTTP check did not match unwanted content");
1693 else
1694 set_server_check_status(s, HCHK_STATUS_L7RSP,
1695 "HTTP content check did not match");
1696 }
1697 break;
1698 }
1699 return 1;
1700}
1701
1702/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001703 * Local variables:
1704 * c-indent-level: 8
1705 * c-basic-offset: 8
1706 * End:
1707 */