blob: 147a7abd67b5ada6aa8aa685b0d526e722ffe989 [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;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100352 set_target_server(&p->sess->target, s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100353 sess = p->sess;
354 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200355 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100356 }
357 return xferred;
358}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359
360/* Sets server <s> down, notifies by all available means, recounts the
361 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200362 * possible to other servers. It automatically recomputes the number of
363 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100365void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100367 struct server *srv;
368 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369 int xferred;
370
Cyril Bontécd19e512010-01-31 22:34:03 +0100371 if (s->state & SRV_MAINTAIN) {
372 s->health = s->rise;
373 }
374
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100375 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100376 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200377 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200378
379 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100380 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100381 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200382
383 /* we might have sessions queued on this server and waiting for
384 * a connection. Those which are redispatchable will be queued
385 * to another server or to the proxy itself.
386 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100387 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100388
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200389 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100390
Cyril Bontécd19e512010-01-31 22:34:03 +0100391 if (s->state & SRV_MAINTAIN) {
392 chunk_printf(&msg,
393 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
394 s->proxy->id, s->id);
395 } else {
396 chunk_printf(&msg,
397 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
398 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100399
Cyril Bontécd19e512010-01-31 22:34:03 +0100400 server_status_printf(&msg, s,
401 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
402 xferred);
403 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100404 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200405
Willy Tarreau48494c02007-11-30 10:41:39 +0100406 /* we don't send an alert if the server was previously paused */
407 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100408 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100409 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100410 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200411
Willy Tarreaud64d2252010-10-17 17:16:42 +0200412 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100413 set_backend_down(s->proxy);
414
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200415 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100416
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100417 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100418 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100419 if (! (srv->state & SRV_MAINTAIN))
420 /* Only notify tracking servers that are not already in maintenance. */
421 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100423
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424 s->health = 0; /* failure */
425}
426
Cyril Bontécd19e512010-01-31 22:34:03 +0100427void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100428
429 struct server *srv;
430 struct chunk msg;
431 int xferred;
432
Cyril Bontécd19e512010-01-31 22:34:03 +0100433 if (s->state & SRV_MAINTAIN) {
434 s->health = s->rise;
435 }
436
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100437 if (s->health == s->rise || s->tracked) {
438 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
439 if (s->proxy->last_change < now.tv_sec) // ignore negative times
440 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
441 s->proxy->last_change = now.tv_sec;
442 }
443
444 if (s->last_change < now.tv_sec) // ignore negative times
445 s->down_time += now.tv_sec - s->last_change;
446
447 s->last_change = now.tv_sec;
448 s->state |= SRV_RUNNING;
449
450 if (s->slowstart > 0) {
451 s->state |= SRV_WARMINGUP;
452 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
453 /* For dynamic algorithms, start at the first step of the weight,
454 * without multiplying by BE_WEIGHT_SCALE.
455 */
456 s->eweight = s->uweight;
457 if (s->proxy->lbprm.update_server_eweight)
458 s->proxy->lbprm.update_server_eweight(s);
459 }
460 }
461 s->proxy->lbprm.set_server_status_up(s);
462
463 /* check if we can handle some connections queued at the proxy. We
464 * will take as many as we can handle.
465 */
466 xferred = check_for_pending(s);
467
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200468 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100469
Cyril Bontécd19e512010-01-31 22:34:03 +0100470 if (s->state & SRV_MAINTAIN) {
471 chunk_printf(&msg,
472 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
473 s->proxy->id, s->id);
474 } else {
475 chunk_printf(&msg,
476 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
477 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100478
Cyril Bontécd19e512010-01-31 22:34:03 +0100479 server_status_printf(&msg, s,
480 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
481 xferred);
482 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100483
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100484 Warning("%s.\n", trash);
485 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100486
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100487 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100488 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100489 if (! (srv->state & SRV_MAINTAIN))
490 /* Only notify tracking servers if they're not in maintenance. */
491 set_server_up(srv);
492
493 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100494 }
495
496 if (s->health >= s->rise)
497 s->health = s->rise + s->fall - 1; /* OK now */
498
499}
500
501static void set_server_disabled(struct server *s) {
502
503 struct server *srv;
504 struct chunk msg;
505 int xferred;
506
507 s->state |= SRV_GOINGDOWN;
508 s->proxy->lbprm.set_server_status_down(s);
509
510 /* we might have sessions queued on this server and waiting for
511 * a connection. Those which are redispatchable will be queued
512 * to another server or to the proxy itself.
513 */
514 xferred = redistribute_pending(s);
515
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200516 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100517
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200518 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100519 "Load-balancing on %sServer %s/%s is disabled",
520 s->state & SRV_BACKUP ? "Backup " : "",
521 s->proxy->id, s->id);
522
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200523 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200524 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100525 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100526
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100527 Warning("%s.\n", trash);
528 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100529
530 if (!s->proxy->srv_bck && !s->proxy->srv_act)
531 set_backend_down(s->proxy);
532
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100533 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100534 for(srv = s->tracknext; srv; srv = srv->tracknext)
535 set_server_disabled(srv);
536}
537
538static void set_server_enabled(struct server *s) {
539
540 struct server *srv;
541 struct chunk msg;
542 int xferred;
543
544 s->state &= ~SRV_GOINGDOWN;
545 s->proxy->lbprm.set_server_status_up(s);
546
547 /* check if we can handle some connections queued at the proxy. We
548 * will take as many as we can handle.
549 */
550 xferred = check_for_pending(s);
551
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200552 chunk_init(&msg, trash, sizeof(trash));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100553
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200554 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100555 "Load-balancing on %sServer %s/%s is enabled again",
556 s->state & SRV_BACKUP ? "Backup " : "",
557 s->proxy->id, s->id);
558
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200559 server_status_printf(&msg, s,
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200560 ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100561 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100562
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100563 Warning("%s.\n", trash);
564 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100565
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100566 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100567 for(srv = s->tracknext; srv; srv = srv->tracknext)
568 set_server_enabled(srv);
569}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100571void health_adjust(struct server *s, short status) {
572
573 int failed;
574 int expire;
575
576 /* return now if observing nor health check is not enabled */
577 if (!s->observe || !s->check)
578 return;
579
580 if (s->observe >= HANA_OBS_SIZE)
581 return;
582
583 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
584 return;
585
586 switch (analyze_statuses[status].lr[s->observe - 1]) {
587 case 1:
588 failed = 1;
589 break;
590
591 case 2:
592 failed = 0;
593 break;
594
595 default:
596 return;
597 }
598
599 if (!failed) {
600 /* good: clear consecutive_errors */
601 s->consecutive_errors = 0;
602 return;
603 }
604
605 s->consecutive_errors++;
606
607 if (s->consecutive_errors < s->consecutive_errors_limit)
608 return;
609
610 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
611 s->consecutive_errors, get_analyze_status(status));
612
613 switch (s->onerror) {
614 case HANA_ONERR_FASTINTER:
615 /* force fastinter - nothing to do here as all modes force it */
616 break;
617
618 case HANA_ONERR_SUDDTH:
619 /* simulate a pre-fatal failed health check */
620 if (s->health > s->rise)
621 s->health = s->rise + 1;
622
623 /* no break - fall through */
624
625 case HANA_ONERR_FAILCHK:
626 /* simulate a failed health check */
627 set_server_check_status(s, HCHK_STATUS_HANA, trash);
628
629 if (s->health > s->rise) {
630 s->health--; /* still good */
631 s->counters.failed_checks++;
632 }
633 else
634 set_server_down(s);
635
636 break;
637
638 case HANA_ONERR_MARKDWN:
639 /* mark server down */
640 s->health = s->rise;
641 set_server_check_status(s, HCHK_STATUS_HANA, trash);
642 set_server_down(s);
643
644 break;
645
646 default:
647 /* write a warning? */
648 break;
649 }
650
651 s->consecutive_errors = 0;
652 s->counters.failed_hana++;
653
654 if (s->fastinter) {
655 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
656 if (s->check->expire > expire)
657 s->check->expire = expire;
658 }
659}
660
Willy Tarreauef781042010-01-27 11:53:01 +0100661static int httpchk_build_status_header(struct server *s, char *buffer)
662{
663 int sv_state;
664 int ratio;
665 int hlen = 0;
666 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
667 "UP %d/%d", "UP",
668 "NOLB %d/%d", "NOLB",
669 "no check" };
670
671 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
672 hlen += 24;
673
674 if (!(s->state & SRV_CHECKED))
675 sv_state = 6; /* should obviously never happen */
676 else if (s->state & SRV_RUNNING) {
677 if (s->health == s->rise + s->fall - 1)
678 sv_state = 3; /* UP */
679 else
680 sv_state = 2; /* going down */
681
682 if (s->state & SRV_GOINGDOWN)
683 sv_state += 2;
684 } else {
685 if (s->health)
686 sv_state = 1; /* going up */
687 else
688 sv_state = 0; /* DOWN */
689 }
690
691 hlen += sprintf(buffer + hlen,
692 srv_hlt_st[sv_state],
693 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
694 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
695
696 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
697 s->proxy->id, s->id,
698 global.node,
699 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
700 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
701 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
702 s->nbpend);
703
704 if ((s->state & SRV_WARMINGUP) &&
705 now.tv_sec < s->last_change + s->slowstart &&
706 now.tv_sec >= s->last_change) {
707 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
708 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
709 }
710
711 buffer[hlen++] = '\r';
712 buffer[hlen++] = '\n';
713
714 return hlen;
715}
716
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717/*
718 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200719 * the connection acknowledgement. If the proxy requires L7 health-checks,
720 * it sends the request. In other cases, it calls set_server_check_status()
721 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200722 * The function itself returns 0 if it needs some polling before being called
723 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 */
Willy Tarreau83749182007-04-15 20:56:27 +0200725static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726{
Willy Tarreau6996e152007-04-30 14:37:43 +0200727 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728 struct task *t = fdtab[fd].owner;
729 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100731 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200732 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100733 int skerr, err = errno;
734 socklen_t lskerr = sizeof(skerr);
735
736 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
737 err = skerr;
738
739 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200740 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200741 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200742
743 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200744
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100745 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200747 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Hervé COMMOWICK698ae002010-01-12 09:25:13 +0100748 (s->proxy->options & PR_O_SMTP_CHK) ||
Willy Tarreau07a54902010-03-29 18:33:29 +0200749 (s->proxy->options2 & PR_O2_SSL3_CHK) ||
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200750 (s->proxy->options2 & PR_O2_MYSQL_CHK) ||
Rauf Kuliyev38b41562011-01-04 15:14:13 +0100751 (s->proxy->options2 & PR_O2_PGSQL_CHK) ||
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200752 (s->proxy->options2 & PR_O2_LDAP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100754 const char *check_req = s->proxy->check_req;
755 int check_len = s->proxy->check_len;
756
Willy Tarreauf3c69202006-07-09 16:42:34 +0200757 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758 * so we'll send the request, and won't wake the checker up now.
759 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200760
Willy Tarreau07a54902010-03-29 18:33:29 +0200761 if (s->proxy->options2 & PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200762 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200763 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200764 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
765 }
Willy Tarreaue9d87882010-01-27 11:28:42 +0100766 else if (s->proxy->options & PR_O_HTTP_CHK) {
767 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100768
769 if (s->proxy->options2 & PR_O2_CHK_SNDST)
770 check_len += httpchk_build_status_header(s, trash + check_len);
771
Willy Tarreaue9d87882010-01-27 11:28:42 +0100772 trash[check_len++] = '\r';
773 trash[check_len++] = '\n';
774 trash[check_len] = '\0';
775 check_req = trash;
776 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200777
Willy Tarreaue9d87882010-01-27 11:28:42 +0100778 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
779 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100780 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100781 if (s->proxy->timeout.check)
782 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200783 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200784 goto out_nowake;
785 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200786 else if (ret == 0 || errno == EAGAIN)
787 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200788 else {
789 switch (errno) {
790 case ECONNREFUSED:
791 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200792 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200793 break;
794
795 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200796 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200797 }
798
Willy Tarreau6996e152007-04-30 14:37:43 +0200799 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200800 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 }
802 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200803 /* We have no data to send to check the connection, and
804 * getsockopt() will not inform us whether the connection
805 * is still pending. So we'll reuse connect() to check the
806 * state of the socket. This has the advantage of givig us
807 * the following info :
808 * - error
809 * - connecting (EALREADY, EINPROGRESS)
810 * - connected (EISCONN, 0)
811 */
812
David du Colombier6f5ccb12011-03-10 22:26:24 +0100813 struct sockaddr_storage sa;
Willy Tarreau6996e152007-04-30 14:37:43 +0200814
David du Colombier6f5ccb12011-03-10 22:26:24 +0100815 if (is_addr(&s->check_addr))
816 sa = s->check_addr;
817 else
818 sa = s->addr;
819
820 switch (s->check_addr.ss_family) {
821 case AF_INET:
822 ((struct sockaddr_in *)&sa)->sin_port = htons(s->check_port);
823 break;
824 case AF_INET6:
825 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(s->check_port);
826 break;
827 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200828
829 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
830 errno = 0;
831
832 if (errno == EALREADY || errno == EINPROGRESS)
833 goto out_poll;
834
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200835 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200836 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200837 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200838 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200839
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200841 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200842 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 }
844 }
Willy Tarreau83749182007-04-15 20:56:27 +0200845 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200846 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200847 out_nowake:
848 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100849 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200850 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200851 out_poll:
852 /* The connection is still pending. We'll have to poll it
853 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100854 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200855 return 0;
856 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200857 fdtab[fd].state = FD_STERROR;
858 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859}
860
861
862/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200863 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200864 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
865 * set_server_check_status() to update s->check_status, s->check_duration
866 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200867
868 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
869 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
870 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
871 * response to an SSL HELLO (the principle is that this is enough to
872 * distinguish between an SSL server and a pure TCP relay). All other cases will
873 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
874 * etc.
875 *
876 * The function returns 0 if it needs to be called again after some polling,
877 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878 */
Willy Tarreau83749182007-04-15 20:56:27 +0200879static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200880{
Willy Tarreau83749182007-04-15 20:56:27 +0200881 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100882 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883 struct task *t = fdtab[fd].owner;
884 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200885 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100886 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200887 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200888
Willy Tarreau659d7bc2010-03-16 21:14:41 +0100889 if (unlikely((s->result & SRV_CHK_ERROR) || (fdtab[fd].state == FD_STERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200890 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200891 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200892 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200893
Willy Tarreau83749182007-04-15 20:56:27 +0200894 goto out_wakeup;
895 }
896
Willy Tarreau83749182007-04-15 20:56:27 +0200897 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
898 * but the connection was closed on the remote end. Fortunately, recv still
899 * works correctly and we don't need to do the getsockopt() on linux.
900 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000901
902 /* Set buffer to point to the end of the data already read, and check
903 * that there is free space remaining. If the buffer is full, proceed
904 * with running the checks without attempting another socket read.
905 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000906
Willy Tarreau03938182010-03-17 21:52:07 +0100907 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200908 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
909 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 +0100910 if (len <= 0)
911 break;
912 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000913
Willy Tarreau03938182010-03-17 21:52:07 +0100914 if (len == 0)
915 done = 1; /* connection hangup received */
916 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100917 /* Report network errors only if we got no other data. Otherwise
918 * we'll let the upper layers decide whether the response is OK
919 * or not. It is very common that an RST sent by the server is
920 * reported as an error just after the last data chunk.
921 */
Willy Tarreau03938182010-03-17 21:52:07 +0100922 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100923 if (!s->check_data_len) {
924 if (!(s->result & SRV_CHK_ERROR))
925 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
926 goto out_wakeup;
927 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928 }
929
Willy Tarreau03938182010-03-17 21:52:07 +0100930 /* Intermediate or complete response received.
931 * Terminate string in check_data buffer.
932 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200933 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100934 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100935 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100936 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100937 done = 1; /* buffer full, don't wait for more data */
938 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200939
Nick Chalk57b1bf72010-03-16 15:50:46 +0000940 /* Run the checks... */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100941 if (s->proxy->options & PR_O_HTTP_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100942 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
943 goto wait_more_data;
944
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100945 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000946 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
947 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
948 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
949 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
950 !isdigit((unsigned char) *(s->check_data + 11))) {
951 cut_crlf(s->check_data);
952 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200953
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100954 goto out_wakeup;
955 }
956
Nick Chalk57b1bf72010-03-16 15:50:46 +0000957 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000958 desc = ltrim(s->check_data + 12, ' ');
959
Willy Tarreaubd741542010-03-16 18:46:54 +0100960 if ((s->proxy->options & PR_O_DISABLE404) &&
961 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000962 /* 404 may be accepted as "stopping" only if the server was up */
963 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200964 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000965 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100966 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
967 /* Run content verification check... We know we have at least 13 chars */
968 if (!httpchk_expect(s, done))
969 goto wait_more_data;
970 }
971 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
972 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
973 cut_crlf(desc);
974 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
975 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000976 else {
977 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200978 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000979 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100980 }
Willy Tarreau07a54902010-03-29 18:33:29 +0200981 else if (s->proxy->options2 & PR_O2_SSL3_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100982 if (!done && s->check_data_len < 5)
983 goto wait_more_data;
984
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100985 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000986 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200987 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200988 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200989 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200990 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100991 else if (s->proxy->options & PR_O_SMTP_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +0100992 if (!done && s->check_data_len < strlen("000\r"))
993 goto wait_more_data;
994
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200995 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000996 if ((s->check_data_len < strlen("000\r")) ||
997 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
998 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
999 !isdigit((unsigned char) *(s->check_data + 2))) {
1000 cut_crlf(s->check_data);
1001 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001002
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001003 goto out_wakeup;
1004 }
1005
Nick Chalk57b1bf72010-03-16 15:50:46 +00001006 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001007
Nick Chalk57b1bf72010-03-16 15:50:46 +00001008 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001009 cut_crlf(desc);
1010
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001011 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001012 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001013 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001014 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001015 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau6996e152007-04-30 14:37:43 +02001016 }
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001017 else if (s->proxy->options2 & PR_O2_PGSQL_CHK) {
1018 if (!done && s->check_data_len < 9)
1019 goto wait_more_data;
1020
1021 if (s->check_data[0] == 'R') {
1022 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1023 }
1024 else {
1025 if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0))
1026 desc = &s->check_data[6];
1027 else
1028 desc = "PostgreSQL unknown error";
1029
1030 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1031 }
1032 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001033 else if (s->proxy->options2 & PR_O2_MYSQL_CHK) {
Willy Tarreau03938182010-03-17 21:52:07 +01001034 if (!done && s->check_data_len < 5)
1035 goto wait_more_data;
1036
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001037 if (s->proxy->check_len == 0) { // old mode
1038 if (*(s->check_data + 4) != '\xff') {
1039 /* We set the MySQL Version in description for information purpose
1040 * FIXME : it can be cool to use MySQL Version for other purpose,
1041 * like mark as down old MySQL server.
1042 */
1043 if (s->check_data_len > 51) {
1044 desc = ltrim(s->check_data + 5, ' ');
1045 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1046 }
1047 else {
1048 if (!done)
1049 goto wait_more_data;
1050 /* it seems we have a OK packet but without a valid length,
1051 * it must be a protocol error
1052 */
1053 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1054 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001055 }
1056 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001057 /* An error message is attached in the Error packet */
1058 desc = ltrim(s->check_data + 7, ' ');
1059 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1060 }
1061 } else {
1062 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1063 (((unsigned int) *(s->check_data + 1)) << 8) +
1064 (((unsigned int) *(s->check_data + 2)) << 16);
1065
1066 if (s->check_data_len == first_packet_len + 4) {
1067 /* MySQL Error packet always begin with field_count = 0xff */
1068 if (*(s->check_data + 4) != '\xff') {
1069 /* We have only one MySQL packet and it is a Handshake Initialization packet
1070 * but we need to have a second packet to know if it is alright
1071 */
1072 if (!done && s->check_data_len < first_packet_len + 5)
1073 goto wait_more_data;
1074 }
1075 else {
1076 /* We have only one packet and it is an Error packet,
1077 * an error message is attached, so we can display it
1078 */
1079 desc = &s->check_data[7];
1080 //Warning("onlyoneERR: %s\n", desc);
1081 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1082 }
1083 } else if (s->check_data_len > first_packet_len + 4) {
1084 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1085 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1086 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1087
1088 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1089 /* We have 2 packets and that's good */
1090 /* Check if the second packet is a MySQL Error packet or not */
1091 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1092 /* No error packet */
1093 /* We set the MySQL Version in description for information purpose */
1094 desc = &s->check_data[5];
1095 //Warning("2packetOK: %s\n", desc);
1096 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1097 }
1098 else {
1099 /* An error message is attached in the Error packet
1100 * so we can display it ! :)
1101 */
1102 desc = &s->check_data[first_packet_len+11];
1103 //Warning("2packetERR: %s\n", desc);
1104 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1105 }
1106 }
1107 }
1108 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001109 if (!done)
1110 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001111 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001112 * it must be a protocol error
1113 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001114 desc = &s->check_data[5];
1115 //Warning("protoerr: %s\n", desc);
1116 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001117 }
1118 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001119 }
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001120 else if (s->proxy->options2 & PR_O2_LDAP_CHK) {
1121 if (!done && s->check_data_len < 14)
1122 goto wait_more_data;
1123
1124 /* Check if the server speaks LDAP (ASN.1/BER)
1125 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1126 * http://tools.ietf.org/html/rfc4511
1127 */
1128
1129 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1130 * LDAPMessage: 0x30: SEQUENCE
1131 */
1132 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1133 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1134 }
1135 else {
1136 /* size of LDAPMessage */
1137 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1138
1139 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1140 * messageID: 0x02 0x01 0x01: INTEGER 1
1141 * protocolOp: 0x61: bindResponse
1142 */
1143 if ((msglen > 2) ||
1144 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1145 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1146
1147 goto out_wakeup;
1148 }
1149
1150 /* size of bindResponse */
1151 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1152
1153 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1154 * ldapResult: 0x0a 0x01: ENUMERATION
1155 */
1156 if ((msglen > 4) ||
1157 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1158 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1159
1160 goto out_wakeup;
1161 }
1162
1163 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1164 * resultCode
1165 */
1166 s->check_code = *(s->check_data + msglen + 9);
1167 if (s->check_code) {
1168 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1169 } else {
1170 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1171 }
1172 }
1173 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001174 else {
1175 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001176 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau23677902007-05-08 23:50:35 +02001177 }
Willy Tarreau83749182007-04-15 20:56:27 +02001178
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001179 out_wakeup:
1180 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001181 fdtab[fd].state = FD_STERROR;
1182
Nick Chalk57b1bf72010-03-16 15:50:46 +00001183 /* Reset the check buffer... */
1184 *s->check_data = '\0';
1185 s->check_data_len = 0;
1186
Willy Tarreau03938182010-03-17 21:52:07 +01001187 /* Close the connection... */
1188 shutdown(fd, SHUT_RDWR);
Willy Tarreauf161a342007-04-08 16:59:42 +02001189 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001190 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +01001191 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +02001192 return 1;
Willy Tarreau03938182010-03-17 21:52:07 +01001193
1194 wait_more_data:
1195 fdtab[fd].ev &= ~FD_POLL_IN;
1196 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197}
1198
1199/*
1200 * manages a server health-check. Returns
1201 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1202 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001203struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001204{
Willy Tarreaue3838802009-03-21 18:58:32 +01001205 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001206 struct server *s = t->context;
David du Colombier6f5ccb12011-03-10 22:26:24 +01001207 struct sockaddr_storage sa;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001208 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001209 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001210
1211 //fprintf(stderr, "process_chk: task=%p\n", t);
1212
1213 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001214 if (attempts++ > 0) {
1215 /* we always fail to create a server, let's stop insisting... */
1216 while (tick_is_expired(t->expire, now_ms))
1217 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1218 return t;
1219 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001220 fd = s->curfd;
1221 if (fd < 0) { /* no check currently running */
1222 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +01001223 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1224 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001225
1226 /* we don't send any health-checks when the proxy is stopped or when
1227 * the server should not be checked.
1228 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001229 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001230 while (tick_is_expired(t->expire, now_ms))
1231 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001232 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233 }
1234
1235 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001236 set_server_check_status(s, HCHK_STATUS_START, NULL);
David du Colombier6f5ccb12011-03-10 22:26:24 +01001237 if ((fd = socket(s->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238 if ((fd < global.maxsock) &&
1239 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1240 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1241 //fprintf(stderr, "process_chk: 3\n");
1242
Willy Tarreau9edd1612007-10-18 18:07:48 +02001243 if (s->proxy->options & PR_O_TCP_NOLING) {
1244 /* We don't want to useless data */
1245 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1246 }
David du Colombier6f5ccb12011-03-10 22:26:24 +01001247
1248 if (is_addr(&s->check_addr))
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001249 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001250 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001251 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001252 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001253 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001254
David du Colombier6f5ccb12011-03-10 22:26:24 +01001255 switch (s->check_addr.ss_family) {
1256 case AF_INET:
1257 /* we'll connect to the check port on the server */
1258 ((struct sockaddr_in *)&sa)->sin_port = htons(s->check_port);
1259 break;
1260 case AF_INET6:
1261 /* we'll connect to the check port on the server */
1262 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(s->check_port);
1263 break;
1264 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001265 /* allow specific binding :
1266 * - server-specific at first
1267 * - proxy-specific next
1268 */
1269 if (s->state & SRV_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001270 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001271 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001272
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001273#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001274 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001275 remote = &s->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001276 flags = 3;
1277 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001278#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001279#ifdef SO_BINDTODEVICE
1280 /* Note: this might fail if not CAP_NET_RAW */
1281 if (s->iface_name)
1282 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001283 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001284#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001285 if (s->sport_range) {
1286 int bind_attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +01001287 struct sockaddr_storage src;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001288
1289 ret = 1;
1290 src = s->source_addr;
1291
1292 do {
1293 /* note: in case of retry, we may have to release a previously
1294 * allocated port, hence this loop's construct.
1295 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001296 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1297 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001298
1299 if (!bind_attempts)
1300 break;
1301 bind_attempts--;
1302
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001303 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1304 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001305 break;
1306
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001307 fdinfo[fd].port_range = s->sport_range;
David du Colombier6f5ccb12011-03-10 22:26:24 +01001308
1309 switch (src.ss_family) {
1310 case AF_INET:
1311 ((struct sockaddr_in *)&src)->sin_port = htons(fdinfo[fd].local_port);
1312 break;
1313 case AF_INET6:
1314 ((struct sockaddr_in6 *)&src)->sin6_port = htons(fdinfo[fd].local_port);
1315 break;
1316 }
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001317
David du Colombier6f5ccb12011-03-10 22:26:24 +01001318 ret = tcp_bind_socket(fd, flags, &src, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001319 } while (ret != 0); /* binding NOK */
1320 }
1321 else {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001322 ret = tcp_bind_socket(fd, flags, &s->source_addr, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001323 }
1324
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001325 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001326 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001327 switch (ret) {
1328 case 1:
1329 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1330 s->proxy->id, s->id);
1331 break;
1332 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001333 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1334 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001335 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001336 }
1337 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001338 }
1339 else if (s->proxy->options & PR_O_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001340 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001341 int ret, flags = 0;
1342
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001343#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001344 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001345 remote = &s->proxy->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001346 flags = 3;
1347 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001348#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001349#ifdef SO_BINDTODEVICE
1350 /* Note: this might fail if not CAP_NET_RAW */
1351 if (s->proxy->iface_name)
1352 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001353 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001354#endif
David du Colombier6f5ccb12011-03-10 22:26:24 +01001355 ret = tcp_bind_socket(fd, flags, &s->proxy->source_addr, remote);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001356 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001357 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001358 switch (ret) {
1359 case 1:
1360 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1361 proxy_type_str(s->proxy), s->proxy->id);
1362 break;
1363 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001364 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1365 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001366 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001367 }
1368 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369 }
1370
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001371 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001372#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001373 /* disabling tcp quick ack now allows
1374 * the request to leave the machine with
1375 * the first ACK.
1376 */
1377 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001378 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001379#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001380 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
1381 /* OK, connection in progress or established */
1382
1383 //fprintf(stderr, "process_chk: 4\n");
1384
1385 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +02001386 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001387 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +02001388 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
1389 fdtab[fd].cb[DIR_RD].b = NULL;
1390 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
1391 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001392 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
1393 fdinfo[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001395 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001396 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001397#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001398 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001400 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1401 /* we allow up to min(inter, timeout.connect) for a connection
1402 * to establish but only when timeout.check is set
1403 * as it may be to short for a full check otherwise
1404 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001405 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001406
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001407 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1408 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1409 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001410 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001411 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412 }
1413 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001414 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001415
1416 switch (errno) {
1417 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1418 case ECONNREFUSED:
1419 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001420 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001421 break;
1422
1423 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001424 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001425 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426 }
1427 }
1428 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001429 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1430 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001431 close(fd); /* socket creation error */
1432 }
Willy Tarreaud11ad782011-03-09 20:38:33 +01001433 else
1434 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001435
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001436 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001437 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001438 while (tick_is_expired(t->expire, now_ms))
1439 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001440 goto new_chk; /* may be we should initialize a new check */
1441 }
1442
1443 /* here, we have seen a failure */
1444 if (s->health > s->rise) {
1445 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001446 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001447 }
1448 else
1449 set_server_down(s);
1450
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001451 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1452 /* we allow up to min(inter, timeout.connect) for a connection
1453 * to establish but only when timeout.check is set
1454 * as it may be to short for a full check otherwise
1455 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001456 while (tick_is_expired(t->expire, now_ms)) {
1457 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001458
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001459 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1460 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001461
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001462 if (s->proxy->timeout.check)
1463 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001464 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001465 goto new_chk;
1466 }
1467 else {
1468 //fprintf(stderr, "process_chk: 8\n");
1469 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001470 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001471 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001472
Willy Tarreau9909fc12007-11-30 17:42:05 +01001473 if (s->state & SRV_WARMINGUP) {
1474 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1475 s->state &= ~SRV_WARMINGUP;
1476 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1477 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1478 if (s->proxy->lbprm.update_server_eweight)
1479 s->proxy->lbprm.update_server_eweight(s);
1480 }
1481 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1482 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +01001483 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1484 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +01001485 s->eweight *= s->uweight;
1486 if (s->proxy->lbprm.update_server_eweight)
1487 s->proxy->lbprm.update_server_eweight(s);
1488 }
1489 /* probably that we can refill this server with a bit more connections */
1490 check_for_pending(s);
1491 }
1492
Willy Tarreau48494c02007-11-30 10:41:39 +01001493 /* we may have to add/remove this server from the LB group */
1494 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1495 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001496 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1497 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001498 else if (!(s->state & SRV_GOINGDOWN) &&
1499 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001500 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1501 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001502 }
1503
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001504 if (s->health < s->rise + s->fall - 1) {
1505 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001506
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001507 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001508 }
1509 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001510 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001511
1512 rv = 0;
1513 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001514 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001515 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001516 //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 +02001517 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001518 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001519 goto new_chk;
1520 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001521 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001522 if (!(s->result & SRV_CHK_ERROR)) {
1523 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001524 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001525 } else {
Willy Tarreau07a54902010-03-29 18:33:29 +02001526 if (s->proxy->options2 & PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001527 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001528 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001529 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001530 }
1531 }
1532
Willy Tarreaubaaee002006-06-26 02:48:02 +02001533 //fprintf(stderr, "process_chk: 10\n");
1534 /* failure or timeout detected */
1535 if (s->health > s->rise) {
1536 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001537 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001538 }
1539 else
1540 set_server_down(s);
1541 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001542 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001543
1544 rv = 0;
1545 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001546 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001547 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001548 //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 +02001549 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001550 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001551 goto new_chk;
1552 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001553 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001554 }
1555 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001556 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001557 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001558}
1559
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001560/*
1561 * Start health-check.
1562 * Returns 0 if OK, -1 if error, and prints the error in this case.
1563 */
1564int start_checks() {
1565
1566 struct proxy *px;
1567 struct server *s;
1568 struct task *t;
1569 int nbchk=0, mininter=0, srvpos=0;
1570
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001571 /* 1- count the checkers to run simultaneously.
1572 * We also determine the minimum interval among all of those which
1573 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1574 * will be used to spread their start-up date. Those which have
1575 * a shorter interval will start independantly and will not dictate
1576 * too short an interval for all others.
1577 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001578 for (px = proxy; px; px = px->next) {
1579 for (s = px->srv; s; s = s->next) {
1580 if (!(s->state & SRV_CHECKED))
1581 continue;
1582
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001583 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1584 (!mininter || mininter > srv_getinter(s)))
1585 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001586
1587 nbchk++;
1588 }
1589 }
1590
1591 if (!nbchk)
1592 return 0;
1593
1594 srand((unsigned)time(NULL));
1595
1596 /*
1597 * 2- start them as far as possible from each others. For this, we will
1598 * start them after their interval set to the min interval divided by
1599 * the number of servers, weighted by the server's position in the list.
1600 */
1601 for (px = proxy; px; px = px->next) {
1602 for (s = px->srv; s; s = s->next) {
1603 if (!(s->state & SRV_CHECKED))
1604 continue;
1605
Willy Tarreaua4613182009-03-21 18:13:21 +01001606 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001607 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1608 return -1;
1609 }
1610
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001611 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001612 t->process = process_chk;
1613 t->context = s;
1614
1615 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001616 t->expire = tick_add(now_ms,
1617 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1618 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001619 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001620 task_queue(t);
1621
1622 srvpos++;
1623 }
1624 }
1625 return 0;
1626}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001627
1628/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001629 * Perform content verification check on data in s->check_data buffer.
1630 * The buffer MUST be terminated by a null byte before calling this function.
1631 * Sets server status appropriately. The caller is responsible for ensuring
1632 * that the buffer contains at least 13 characters. If <done> is zero, we may
1633 * return 0 to indicate that data is required to decide of a match.
1634 */
1635static int httpchk_expect(struct server *s, int done)
1636{
1637 static char status_msg[] = "HTTP status check returned code <000>";
1638 char status_code[] = "000";
1639 char *contentptr;
1640 int crlf;
1641 int ret;
1642
1643 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1644 case PR_O2_EXP_STS:
1645 case PR_O2_EXP_RSTS:
1646 memcpy(status_code, s->check_data + 9, 3);
1647 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1648
1649 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1650 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1651 else
1652 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1653
1654 /* we necessarily have the response, so there are no partial failures */
1655 if (s->proxy->options2 & PR_O2_EXP_INV)
1656 ret = !ret;
1657
1658 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1659 break;
1660
1661 case PR_O2_EXP_STR:
1662 case PR_O2_EXP_RSTR:
1663 /* very simple response parser: ignore CR and only count consecutive LFs,
1664 * stop with contentptr pointing to first char after the double CRLF or
1665 * to '\0' if crlf < 2.
1666 */
1667 crlf = 0;
1668 for (contentptr = s->check_data; *contentptr; contentptr++) {
1669 if (crlf >= 2)
1670 break;
1671 if (*contentptr == '\r')
1672 continue;
1673 else if (*contentptr == '\n')
1674 crlf++;
1675 else
1676 crlf = 0;
1677 }
1678
1679 /* Check that response contains a body... */
1680 if (crlf < 2) {
1681 if (!done)
1682 return 0;
1683
1684 set_server_check_status(s, HCHK_STATUS_L7RSP,
1685 "HTTP content check could not find a response body");
1686 return 1;
1687 }
1688
1689 /* Check that response body is not empty... */
1690 if (*contentptr == '\0') {
1691 set_server_check_status(s, HCHK_STATUS_L7RSP,
1692 "HTTP content check found empty response body");
1693 return 1;
1694 }
1695
1696 /* Check the response content against the supplied string
1697 * or regex... */
1698 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1699 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1700 else
1701 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1702
1703 /* if we don't match, we may need to wait more */
1704 if (!ret && !done)
1705 return 0;
1706
1707 if (ret) {
1708 /* content matched */
1709 if (s->proxy->options2 & PR_O2_EXP_INV)
1710 set_server_check_status(s, HCHK_STATUS_L7RSP,
1711 "HTTP check matched unwanted content");
1712 else
1713 set_server_check_status(s, HCHK_STATUS_L7OKD,
1714 "HTTP content check matched");
1715 }
1716 else {
1717 if (s->proxy->options2 & PR_O2_EXP_INV)
1718 set_server_check_status(s, HCHK_STATUS_L7OKD,
1719 "HTTP check did not match unwanted content");
1720 else
1721 set_server_check_status(s, HCHK_STATUS_L7RSP,
1722 "HTTP content check did not match");
1723 }
1724 break;
1725 }
1726 return 1;
1727}
1728
1729/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001730 * Local variables:
1731 * c-indent-level: 8
1732 * c-basic-offset: 8
1733 * End:
1734 */