blob: 78ec4ad6dd7a2d5309054804fee24dd9cdbc11c2 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
18#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
23#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040024#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020026#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <arpa/inet.h>
28
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020032#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
35#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020038#include <proto/checks.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010039#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/fd.h>
41#include <proto/log.h>
42#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020043#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010044#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010045#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010046#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090048#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010049#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/task.h>
51
Willy Tarreaubd741542010-03-16 18:46:54 +010052static int httpchk_expect(struct server *s, int done);
53
Simon Horman63a4a822012-03-19 07:24:41 +090054static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020055 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
56 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
57 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020058
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010059 [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" },
60
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020061 [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020062
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020063 [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" },
64 [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" },
65 [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020066
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020067 [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" },
68 [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" },
69 [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020070
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020071 [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" },
72 [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020073
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020074 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020076 [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" },
77 [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
78 [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020079};
80
Simon Horman63a4a822012-03-19 07:24:41 +090081static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010082 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
83
84 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
85 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
86
87 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
88 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
89 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
90 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
91
92 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
93 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
94 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
95};
96
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020097/*
98 * Convert check_status code to description
99 */
100const char *get_check_status_description(short check_status) {
101
102 const char *desc;
103
104 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200105 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200106 else
107 desc = NULL;
108
109 if (desc && *desc)
110 return desc;
111 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200112 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200113}
114
115/*
116 * Convert check_status code to short info
117 */
118const char *get_check_status_info(short check_status) {
119
120 const char *info;
121
122 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200123 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200124 else
125 info = NULL;
126
127 if (info && *info)
128 return info;
129 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200130 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200131}
132
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100133const char *get_analyze_status(short analyze_status) {
134
135 const char *desc;
136
137 if (analyze_status < HANA_STATUS_SIZE)
138 desc = analyze_statuses[analyze_status].desc;
139 else
140 desc = NULL;
141
142 if (desc && *desc)
143 return desc;
144 else
145 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
146}
147
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200148#define SSP_O_HCHK 0x0002
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200149
150static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
151
Willy Tarreau44267702011-10-28 15:35:33 +0200152 if (s->track)
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200153 chunk_printf(msg, " via %s/%s",
Willy Tarreau44267702011-10-28 15:35:33 +0200154 s->track->proxy->id, s->track->id);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200155
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200156 if (options & SSP_O_HCHK) {
157 chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status));
158
159 if (s->check_status >= HCHK_STATUS_L57DATA)
160 chunk_printf(msg, ", code: %d", s->check_code);
161
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200162 if (*s->check_desc) {
163 struct chunk src;
164
165 chunk_printf(msg, ", info: \"");
166
167 chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc));
168 chunk_asciiencode(msg, &src, '"');
169
170 chunk_printf(msg, "\"");
171 }
172
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100173 if (s->check_duration >= 0)
174 chunk_printf(msg, ", check duration: %ldms", s->check_duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200175 }
176
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200177 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100178 if (!(s->state & SRV_RUNNING))
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200179 chunk_printf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100180 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200181 s->proxy->srv_act, s->proxy->srv_bck,
182 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
183 s->cur_sess, xferred, s->nbpend);
184 else
185 chunk_printf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100186 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200187 s->proxy->srv_act, s->proxy->srv_bck,
188 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
189 xferred, s->nbpend);
190 }
191}
192
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200193/*
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200194 * Set s->check_status, update s->check_duration and fill s->result with
195 * an adequate SRV_CHK_* value.
196 *
197 * Show information in logs about failed health check if server is UP
198 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200199 */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200200static void set_server_check_status(struct server *s, short status, char *desc) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200201
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200202 struct chunk msg;
203
204 if (status == HCHK_STATUS_START) {
205 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200206 s->check_desc[0] = '\0';
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200207 s->check_start = now;
208 return;
209 }
210
211 if (!s->check_status)
212 return;
213
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200214 if (desc && *desc) {
215 strncpy(s->check_desc, desc, HCHK_DESC_LEN-1);
216 s->check_desc[HCHK_DESC_LEN-1] = '\0';
217 } else
218 s->check_desc[0] = '\0';
219
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200220 s->check_status = status;
221 if (check_statuses[status].result)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100222 s->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200223
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100224 if (status == HCHK_STATUS_HANA)
225 s->check_duration = -1;
226 else if (!tv_iszero(&s->check_start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200227 /* set_server_check_status() may be called more than once */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200228 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
229 tv_zero(&s->check_start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200230 }
231
232 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
233 (((s->health != 0) && (s->result & SRV_CHK_ERROR)) ||
234 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) ||
235 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
236 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
237
238 int health, rise, fall, state;
239
David du Colombier7af46052012-05-16 14:16:48 +0200240 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200241
242 /* FIXME begin: calculate local version of the health/rise/fall/state */
243 health = s->health;
244 rise = s->rise;
245 fall = s->fall;
246 state = s->state;
247
248 if (s->result & SRV_CHK_ERROR) {
249 if (health > rise) {
250 health--; /* still good */
251 } else {
252 if (health == rise)
253 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
254
255 health = 0;
256 }
257 }
258
259 if (s->result & SRV_CHK_RUNNING) {
260 if (health < rise + fall - 1) {
261 health++; /* was bad, stays for a while */
262
263 if (health == rise)
264 state |= SRV_RUNNING;
265
266 if (health >= rise)
267 health = rise + fall - 1; /* OK now */
268 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100269
270 /* clear consecutive_errors if observing is enabled */
271 if (s->onerror)
272 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200273 }
274 /* FIXME end: calculate local version of the health/rise/fall/state */
275
276 chunk_printf(&msg,
277 "Health check for %sserver %s/%s %s%s",
278 s->state & SRV_BACKUP ? "backup " : "",
279 s->proxy->id, s->id,
280 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
281 (s->result & SRV_CHK_RUNNING)?"succeeded":"failed");
282
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200283 server_status_printf(&msg, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200284
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100285 chunk_printf(&msg, ", status: %d/%d %s",
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200286 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
287 (state & SRV_RUNNING) ? (fall) : (rise),
288 (state & SRV_RUNNING)?"UP":"DOWN");
289
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100290 Warning("%s.\n", trash);
291 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200292 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200293}
294
Willy Tarreau48494c02007-11-30 10:41:39 +0100295/* sends a log message when a backend goes down, and also sets last
296 * change date.
297 */
298static void set_backend_down(struct proxy *be)
299{
300 be->last_change = now.tv_sec;
301 be->down_trans++;
302
303 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
304 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
305}
306
307/* Redistribute pending connections when a server goes down. The number of
308 * connections redistributed is returned.
309 */
310static int redistribute_pending(struct server *s)
311{
312 struct pendconn *pc, *pc_bck, *pc_end;
313 int xferred = 0;
314
315 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
316 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100317 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
318 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100319 /* The REDISP option was specified. We will ignore
320 * cookie and force to balance or use the dispatcher.
321 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100322
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100323 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100324 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100325
Willy Tarreau48494c02007-11-30 10:41:39 +0100326 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200327 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100328 xferred++;
329 }
330 }
331 return xferred;
332}
333
334/* Check for pending connections at the backend, and assign some of them to
335 * the server coming up. The server's weight is checked before being assigned
336 * connections it may not be able to handle. The total number of transferred
337 * connections is returned.
338 */
339static int check_for_pending(struct server *s)
340{
341 int xferred;
342
343 if (!s->eweight)
344 return 0;
345
346 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
347 struct session *sess;
348 struct pendconn *p;
349
350 p = pendconn_from_px(s->proxy);
351 if (!p)
352 break;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100353 set_target_server(&p->sess->target, s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100354 sess = p->sess;
355 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200356 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100357 }
358 return xferred;
359}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900361/* Shutdown connections when their server goes down.
362 */
363static void shutdown_sessions(struct server *srv)
364{
365 struct session *session, *session_bck;
366
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200367 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
368 if (session->srv_conn == srv)
369 session_shutdown(session, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900370}
371
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372/* Sets server <s> down, notifies by all available means, recounts the
373 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200374 * possible to other servers. It automatically recomputes the number of
375 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100377void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100379 struct server *srv;
380 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381 int xferred;
382
Cyril Bontécd19e512010-01-31 22:34:03 +0100383 if (s->state & SRV_MAINTAIN) {
384 s->health = s->rise;
385 }
386
Willy Tarreau44267702011-10-28 15:35:33 +0200387 if (s->health == s->rise || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100388 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200389 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200390
391 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100392 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100393 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900395 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
396 shutdown_sessions(s);
397
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398 /* we might have sessions queued on this server and waiting for
399 * a connection. Those which are redispatchable will be queued
400 * to another server or to the proxy itself.
401 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100402 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100403
David du Colombier7af46052012-05-16 14:16:48 +0200404 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100405
Cyril Bontécd19e512010-01-31 22:34:03 +0100406 if (s->state & SRV_MAINTAIN) {
407 chunk_printf(&msg,
408 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
409 s->proxy->id, s->id);
410 } else {
411 chunk_printf(&msg,
412 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
413 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100414
Cyril Bontécd19e512010-01-31 22:34:03 +0100415 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200416 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Cyril Bontécd19e512010-01-31 22:34:03 +0100417 xferred);
418 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100419 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200420
Willy Tarreau48494c02007-11-30 10:41:39 +0100421 /* we don't send an alert if the server was previously paused */
422 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100423 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100424 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100425 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200426
Willy Tarreaud64d2252010-10-17 17:16:42 +0200427 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100428 set_backend_down(s->proxy);
429
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200430 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100431
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100432 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100433 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100434 if (! (srv->state & SRV_MAINTAIN))
435 /* Only notify tracking servers that are not already in maintenance. */
436 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100438
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 s->health = 0; /* failure */
440}
441
Cyril Bontécd19e512010-01-31 22:34:03 +0100442void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100443
444 struct server *srv;
445 struct chunk msg;
446 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100447 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100448
Cyril Bontécd19e512010-01-31 22:34:03 +0100449 if (s->state & SRV_MAINTAIN) {
450 s->health = s->rise;
451 }
452
Willy Tarreau44267702011-10-28 15:35:33 +0200453 if (s->health == s->rise || s->track) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100454 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
455 if (s->proxy->last_change < now.tv_sec) // ignore negative times
456 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
457 s->proxy->last_change = now.tv_sec;
458 }
459
460 if (s->last_change < now.tv_sec) // ignore negative times
461 s->down_time += now.tv_sec - s->last_change;
462
463 s->last_change = now.tv_sec;
464 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100465 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100466
467 if (s->slowstart > 0) {
468 s->state |= SRV_WARMINGUP;
469 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
470 /* For dynamic algorithms, start at the first step of the weight,
471 * without multiplying by BE_WEIGHT_SCALE.
472 */
473 s->eweight = s->uweight;
474 if (s->proxy->lbprm.update_server_eweight)
475 s->proxy->lbprm.update_server_eweight(s);
476 }
Willy Tarreau2e993902011-10-31 11:53:20 +0100477 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100478 }
479 s->proxy->lbprm.set_server_status_up(s);
480
481 /* check if we can handle some connections queued at the proxy. We
482 * will take as many as we can handle.
483 */
484 xferred = check_for_pending(s);
485
David du Colombier7af46052012-05-16 14:16:48 +0200486 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100487
Willy Tarreau45446782012-03-09 17:16:09 +0100488 if (old_state & SRV_MAINTAIN) {
Cyril Bontécd19e512010-01-31 22:34:03 +0100489 chunk_printf(&msg,
490 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
491 s->proxy->id, s->id);
492 } else {
493 chunk_printf(&msg,
494 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
495 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100496
Cyril Bontécd19e512010-01-31 22:34:03 +0100497 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200498 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Cyril Bontécd19e512010-01-31 22:34:03 +0100499 xferred);
500 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100501
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100502 Warning("%s.\n", trash);
503 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100504
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100505 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100506 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100507 if (! (srv->state & SRV_MAINTAIN))
508 /* Only notify tracking servers if they're not in maintenance. */
509 set_server_up(srv);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100510 }
511
512 if (s->health >= s->rise)
513 s->health = s->rise + s->fall - 1; /* OK now */
514
515}
516
517static void set_server_disabled(struct server *s) {
518
519 struct server *srv;
520 struct chunk msg;
521 int xferred;
522
523 s->state |= SRV_GOINGDOWN;
524 s->proxy->lbprm.set_server_status_down(s);
525
526 /* we might have sessions queued on this server and waiting for
527 * a connection. Those which are redispatchable will be queued
528 * to another server or to the proxy itself.
529 */
530 xferred = redistribute_pending(s);
531
David du Colombier7af46052012-05-16 14:16:48 +0200532 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100533
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200534 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100535 "Load-balancing on %sServer %s/%s is disabled",
536 s->state & SRV_BACKUP ? "Backup " : "",
537 s->proxy->id, s->id);
538
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200539 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200540 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100541 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100542
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100543 Warning("%s.\n", trash);
544 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100545
546 if (!s->proxy->srv_bck && !s->proxy->srv_act)
547 set_backend_down(s->proxy);
548
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100549 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100550 for(srv = s->tracknext; srv; srv = srv->tracknext)
551 set_server_disabled(srv);
552}
553
554static void set_server_enabled(struct server *s) {
555
556 struct server *srv;
557 struct chunk msg;
558 int xferred;
559
560 s->state &= ~SRV_GOINGDOWN;
561 s->proxy->lbprm.set_server_status_up(s);
562
563 /* check if we can handle some connections queued at the proxy. We
564 * will take as many as we can handle.
565 */
566 xferred = check_for_pending(s);
567
David du Colombier7af46052012-05-16 14:16:48 +0200568 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100569
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200570 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100571 "Load-balancing on %sServer %s/%s is enabled again",
572 s->state & SRV_BACKUP ? "Backup " : "",
573 s->proxy->id, s->id);
574
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200575 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200576 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100577 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100578
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100579 Warning("%s.\n", trash);
580 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100581
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100582 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100583 for(srv = s->tracknext; srv; srv = srv->tracknext)
584 set_server_enabled(srv);
585}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100587void health_adjust(struct server *s, short status) {
588
589 int failed;
590 int expire;
591
592 /* return now if observing nor health check is not enabled */
593 if (!s->observe || !s->check)
594 return;
595
596 if (s->observe >= HANA_OBS_SIZE)
597 return;
598
599 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
600 return;
601
602 switch (analyze_statuses[status].lr[s->observe - 1]) {
603 case 1:
604 failed = 1;
605 break;
606
607 case 2:
608 failed = 0;
609 break;
610
611 default:
612 return;
613 }
614
615 if (!failed) {
616 /* good: clear consecutive_errors */
617 s->consecutive_errors = 0;
618 return;
619 }
620
621 s->consecutive_errors++;
622
623 if (s->consecutive_errors < s->consecutive_errors_limit)
624 return;
625
626 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
627 s->consecutive_errors, get_analyze_status(status));
628
629 switch (s->onerror) {
630 case HANA_ONERR_FASTINTER:
631 /* force fastinter - nothing to do here as all modes force it */
632 break;
633
634 case HANA_ONERR_SUDDTH:
635 /* simulate a pre-fatal failed health check */
636 if (s->health > s->rise)
637 s->health = s->rise + 1;
638
639 /* no break - fall through */
640
641 case HANA_ONERR_FAILCHK:
642 /* simulate a failed health check */
643 set_server_check_status(s, HCHK_STATUS_HANA, trash);
644
645 if (s->health > s->rise) {
646 s->health--; /* still good */
647 s->counters.failed_checks++;
648 }
649 else
650 set_server_down(s);
651
652 break;
653
654 case HANA_ONERR_MARKDWN:
655 /* mark server down */
656 s->health = s->rise;
657 set_server_check_status(s, HCHK_STATUS_HANA, trash);
658 set_server_down(s);
659
660 break;
661
662 default:
663 /* write a warning? */
664 break;
665 }
666
667 s->consecutive_errors = 0;
668 s->counters.failed_hana++;
669
670 if (s->fastinter) {
671 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
672 if (s->check->expire > expire)
673 s->check->expire = expire;
674 }
675}
676
Willy Tarreauef781042010-01-27 11:53:01 +0100677static int httpchk_build_status_header(struct server *s, char *buffer)
678{
679 int sv_state;
680 int ratio;
681 int hlen = 0;
682 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
683 "UP %d/%d", "UP",
684 "NOLB %d/%d", "NOLB",
685 "no check" };
686
687 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
688 hlen += 24;
689
690 if (!(s->state & SRV_CHECKED))
691 sv_state = 6; /* should obviously never happen */
692 else if (s->state & SRV_RUNNING) {
693 if (s->health == s->rise + s->fall - 1)
694 sv_state = 3; /* UP */
695 else
696 sv_state = 2; /* going down */
697
698 if (s->state & SRV_GOINGDOWN)
699 sv_state += 2;
700 } else {
701 if (s->health)
702 sv_state = 1; /* going up */
703 else
704 sv_state = 0; /* DOWN */
705 }
706
707 hlen += sprintf(buffer + hlen,
708 srv_hlt_st[sv_state],
709 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
710 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
711
712 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
713 s->proxy->id, s->id,
714 global.node,
715 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
716 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
717 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
718 s->nbpend);
719
720 if ((s->state & SRV_WARMINGUP) &&
721 now.tv_sec < s->last_change + s->slowstart &&
722 now.tv_sec >= s->last_change) {
723 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
724 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
725 }
726
727 buffer[hlen++] = '\r';
728 buffer[hlen++] = '\n';
729
730 return hlen;
731}
732
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733/*
734 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200735 * the connection acknowledgement. If the proxy requires L7 health-checks,
736 * it sends the request. In other cases, it calls set_server_check_status()
737 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200738 * The function itself returns 0 if it needs some polling before being called
739 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740 */
Willy Tarreau83749182007-04-15 20:56:27 +0200741static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742{
Willy Tarreau6996e152007-04-30 14:37:43 +0200743 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744 struct task *t = fdtab[fd].owner;
745 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100747 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200748 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100749 int skerr, err = errno;
750 socklen_t lskerr = sizeof(skerr);
751
752 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
753 err = skerr;
754
755 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200756 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200757 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200758
759 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200760
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100761 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200763 if (s->proxy->options2 & PR_O2_CHK_ANY) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100765 const char *check_req = s->proxy->check_req;
766 int check_len = s->proxy->check_len;
767
Willy Tarreauf3c69202006-07-09 16:42:34 +0200768 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769 * so we'll send the request, and won't wake the checker up now.
770 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200771
Willy Tarreau1620ec32011-08-06 17:05:02 +0200772 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200773 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200774 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200775 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
776 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200777 else if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK) {
Willy Tarreaue9d87882010-01-27 11:28:42 +0100778 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100779
780 if (s->proxy->options2 & PR_O2_CHK_SNDST)
781 check_len += httpchk_build_status_header(s, trash + check_len);
782
Willy Tarreaue9d87882010-01-27 11:28:42 +0100783 trash[check_len++] = '\r';
784 trash[check_len++] = '\n';
785 trash[check_len] = '\0';
786 check_req = trash;
787 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200788
Willy Tarreaue9d87882010-01-27 11:28:42 +0100789 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
790 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100791 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100792 if (s->proxy->timeout.check)
793 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200794 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200795 goto out_nowake;
796 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200797 else if (ret == 0 || errno == EAGAIN)
798 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200799 else {
800 switch (errno) {
801 case ECONNREFUSED:
802 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200803 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200804 break;
805
806 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200807 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200808 }
809
Willy Tarreau6996e152007-04-30 14:37:43 +0200810 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200811 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 }
813 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200814 /* We have no data to send to check the connection, and
815 * getsockopt() will not inform us whether the connection
816 * is still pending. So we'll reuse connect() to check the
817 * state of the socket. This has the advantage of givig us
818 * the following info :
819 * - error
820 * - connecting (EALREADY, EINPROGRESS)
821 * - connected (EISCONN, 0)
822 */
823
David du Colombier6f5ccb12011-03-10 22:26:24 +0100824 struct sockaddr_storage sa;
Willy Tarreau6996e152007-04-30 14:37:43 +0200825
David du Colombier6f5ccb12011-03-10 22:26:24 +0100826 if (is_addr(&s->check_addr))
827 sa = s->check_addr;
828 else
829 sa = s->addr;
830
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200831 set_host_port(&sa, s->check_port);
Willy Tarreau6996e152007-04-30 14:37:43 +0200832
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200833 if (connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) == 0)
Willy Tarreau6996e152007-04-30 14:37:43 +0200834 errno = 0;
835
836 if (errno == EALREADY || errno == EINPROGRESS)
837 goto out_poll;
838
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200839 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200840 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200841 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200842 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200843
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200845 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200846 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 }
848 }
Willy Tarreau83749182007-04-15 20:56:27 +0200849 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200850 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200851 out_nowake:
852 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100853 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200854 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200855 out_poll:
856 /* The connection is still pending. We'll have to poll it
857 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100858 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200859 return 0;
860 out_error:
Willy Tarreau6996e152007-04-30 14:37:43 +0200861 fdtab[fd].state = FD_STERROR;
862 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863}
864
865
866/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200867 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200868 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
869 * set_server_check_status() to update s->check_status, s->check_duration
870 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200871
872 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
873 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
874 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
875 * response to an SSL HELLO (the principle is that this is enough to
876 * distinguish between an SSL server and a pure TCP relay). All other cases will
877 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
878 * etc.
879 *
880 * The function returns 0 if it needs to be called again after some polling,
881 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882 */
Willy Tarreau83749182007-04-15 20:56:27 +0200883static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200884{
Willy Tarreau83749182007-04-15 20:56:27 +0200885 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100886 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887 struct task *t = fdtab[fd].owner;
888 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200889 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100890 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200891 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200892
Willy Tarreau659d7bc2010-03-16 21:14:41 +0100893 if (unlikely((s->result & SRV_CHK_ERROR) || (fdtab[fd].state == FD_STERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200894 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200895 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200896 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200897
Willy Tarreau83749182007-04-15 20:56:27 +0200898 goto out_wakeup;
899 }
900
Willy Tarreau83749182007-04-15 20:56:27 +0200901 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
902 * but the connection was closed on the remote end. Fortunately, recv still
903 * works correctly and we don't need to do the getsockopt() on linux.
904 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000905
906 /* Set buffer to point to the end of the data already read, and check
907 * that there is free space remaining. If the buffer is full, proceed
908 * with running the checks without attempting another socket read.
909 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000910
Willy Tarreau03938182010-03-17 21:52:07 +0100911 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200912 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
913 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 +0100914 if (len <= 0)
915 break;
916 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000917
Willy Tarreau03938182010-03-17 21:52:07 +0100918 if (len == 0)
919 done = 1; /* connection hangup received */
920 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100921 /* Report network errors only if we got no other data. Otherwise
922 * we'll let the upper layers decide whether the response is OK
923 * or not. It is very common that an RST sent by the server is
924 * reported as an error just after the last data chunk.
925 */
Willy Tarreau03938182010-03-17 21:52:07 +0100926 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100927 if (!s->check_data_len) {
928 if (!(s->result & SRV_CHK_ERROR))
929 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
930 goto out_wakeup;
931 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 }
933
Willy Tarreau03938182010-03-17 21:52:07 +0100934 /* Intermediate or complete response received.
935 * Terminate string in check_data buffer.
936 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200937 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100938 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100939 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100940 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100941 done = 1; /* buffer full, don't wait for more data */
942 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200943
Nick Chalk57b1bf72010-03-16 15:50:46 +0000944 /* Run the checks... */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200945 switch (s->proxy->options2 & PR_O2_CHK_ANY) {
946 case PR_O2_HTTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100947 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
948 goto wait_more_data;
949
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100950 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000951 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
952 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
953 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
954 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
955 !isdigit((unsigned char) *(s->check_data + 11))) {
956 cut_crlf(s->check_data);
957 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200958
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100959 goto out_wakeup;
960 }
961
Nick Chalk57b1bf72010-03-16 15:50:46 +0000962 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000963 desc = ltrim(s->check_data + 12, ' ');
964
Willy Tarreaubd741542010-03-16 18:46:54 +0100965 if ((s->proxy->options & PR_O_DISABLE404) &&
966 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000967 /* 404 may be accepted as "stopping" only if the server was up */
968 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200969 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000970 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100971 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
972 /* Run content verification check... We know we have at least 13 chars */
973 if (!httpchk_expect(s, done))
974 goto wait_more_data;
975 }
976 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
977 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
978 cut_crlf(desc);
979 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
980 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000981 else {
982 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200983 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000984 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200985 break;
986
987 case PR_O2_SSL3_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100988 if (!done && s->check_data_len < 5)
989 goto wait_more_data;
990
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100991 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000992 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200993 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200994 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200995 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200996 break;
997
998 case PR_O2_SMTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100999 if (!done && s->check_data_len < strlen("000\r"))
1000 goto wait_more_data;
1001
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001002 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001003 if ((s->check_data_len < strlen("000\r")) ||
1004 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
1005 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
1006 !isdigit((unsigned char) *(s->check_data + 2))) {
1007 cut_crlf(s->check_data);
1008 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001009
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001010 goto out_wakeup;
1011 }
1012
Nick Chalk57b1bf72010-03-16 15:50:46 +00001013 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001014
Nick Chalk57b1bf72010-03-16 15:50:46 +00001015 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001016 cut_crlf(desc);
1017
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001018 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001019 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001020 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001021 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001022 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001023 break;
1024
1025 case PR_O2_PGSQL_CHK:
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001026 if (!done && s->check_data_len < 9)
1027 goto wait_more_data;
1028
1029 if (s->check_data[0] == 'R') {
1030 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1031 }
1032 else {
1033 if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0))
1034 desc = &s->check_data[6];
1035 else
1036 desc = "PostgreSQL unknown error";
1037
1038 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1039 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001040 break;
1041
1042 case PR_O2_REDIS_CHK:
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001043 if (!done && s->check_data_len < 7)
1044 goto wait_more_data;
1045
1046 if (strcmp(s->check_data, "+PONG\r\n") == 0) {
1047 set_server_check_status(s, HCHK_STATUS_L7OKD, "Redis server is ok");
1048 }
1049 else {
1050 set_server_check_status(s, HCHK_STATUS_L7STS, s->check_data);
1051 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001052 break;
1053
1054 case PR_O2_MYSQL_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001055 if (!done && s->check_data_len < 5)
1056 goto wait_more_data;
1057
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001058 if (s->proxy->check_len == 0) { // old mode
1059 if (*(s->check_data + 4) != '\xff') {
1060 /* We set the MySQL Version in description for information purpose
1061 * FIXME : it can be cool to use MySQL Version for other purpose,
1062 * like mark as down old MySQL server.
1063 */
1064 if (s->check_data_len > 51) {
1065 desc = ltrim(s->check_data + 5, ' ');
1066 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1067 }
1068 else {
1069 if (!done)
1070 goto wait_more_data;
1071 /* it seems we have a OK packet but without a valid length,
1072 * it must be a protocol error
1073 */
1074 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1075 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001076 }
1077 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001078 /* An error message is attached in the Error packet */
1079 desc = ltrim(s->check_data + 7, ' ');
1080 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1081 }
1082 } else {
1083 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1084 (((unsigned int) *(s->check_data + 1)) << 8) +
1085 (((unsigned int) *(s->check_data + 2)) << 16);
1086
1087 if (s->check_data_len == first_packet_len + 4) {
1088 /* MySQL Error packet always begin with field_count = 0xff */
1089 if (*(s->check_data + 4) != '\xff') {
1090 /* We have only one MySQL packet and it is a Handshake Initialization packet
1091 * but we need to have a second packet to know if it is alright
1092 */
1093 if (!done && s->check_data_len < first_packet_len + 5)
1094 goto wait_more_data;
1095 }
1096 else {
1097 /* We have only one packet and it is an Error packet,
1098 * an error message is attached, so we can display it
1099 */
1100 desc = &s->check_data[7];
1101 //Warning("onlyoneERR: %s\n", desc);
1102 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1103 }
1104 } else if (s->check_data_len > first_packet_len + 4) {
1105 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1106 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1107 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1108
1109 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1110 /* We have 2 packets and that's good */
1111 /* Check if the second packet is a MySQL Error packet or not */
1112 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1113 /* No error packet */
1114 /* We set the MySQL Version in description for information purpose */
1115 desc = &s->check_data[5];
1116 //Warning("2packetOK: %s\n", desc);
1117 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1118 }
1119 else {
1120 /* An error message is attached in the Error packet
1121 * so we can display it ! :)
1122 */
1123 desc = &s->check_data[first_packet_len+11];
1124 //Warning("2packetERR: %s\n", desc);
1125 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1126 }
1127 }
1128 }
1129 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001130 if (!done)
1131 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001132 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001133 * it must be a protocol error
1134 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001135 desc = &s->check_data[5];
1136 //Warning("protoerr: %s\n", desc);
1137 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001138 }
1139 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001140 break;
1141
1142 case PR_O2_LDAP_CHK:
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001143 if (!done && s->check_data_len < 14)
1144 goto wait_more_data;
1145
1146 /* Check if the server speaks LDAP (ASN.1/BER)
1147 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1148 * http://tools.ietf.org/html/rfc4511
1149 */
1150
1151 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1152 * LDAPMessage: 0x30: SEQUENCE
1153 */
1154 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1155 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1156 }
1157 else {
1158 /* size of LDAPMessage */
1159 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1160
1161 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1162 * messageID: 0x02 0x01 0x01: INTEGER 1
1163 * protocolOp: 0x61: bindResponse
1164 */
1165 if ((msglen > 2) ||
1166 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1167 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1168
1169 goto out_wakeup;
1170 }
1171
1172 /* size of bindResponse */
1173 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1174
1175 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1176 * ldapResult: 0x0a 0x01: ENUMERATION
1177 */
1178 if ((msglen > 4) ||
1179 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1180 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1181
1182 goto out_wakeup;
1183 }
1184
1185 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1186 * resultCode
1187 */
1188 s->check_code = *(s->check_data + msglen + 9);
1189 if (s->check_code) {
1190 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1191 } else {
1192 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1193 }
1194 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001195 break;
1196
1197 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001198 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001199 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001200 break;
1201 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001202
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001203 out_wakeup:
1204 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001205 fdtab[fd].state = FD_STERROR;
1206
Nick Chalk57b1bf72010-03-16 15:50:46 +00001207 /* Reset the check buffer... */
1208 *s->check_data = '\0';
1209 s->check_data_len = 0;
1210
Willy Tarreau03938182010-03-17 21:52:07 +01001211 /* Close the connection... */
1212 shutdown(fd, SHUT_RDWR);
Willy Tarreauf161a342007-04-08 16:59:42 +02001213 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001214 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +01001215 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +02001216 return 1;
Willy Tarreau03938182010-03-17 21:52:07 +01001217
1218 wait_more_data:
1219 fdtab[fd].ev &= ~FD_POLL_IN;
1220 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001221}
1222
1223/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001224 * updates the server's weight during a warmup stage. Once the final weight is
1225 * reached, the task automatically stops. Note that any server status change
1226 * must have updated s->last_change accordingly.
1227 */
1228static struct task *server_warmup(struct task *t)
1229{
1230 struct server *s = t->context;
1231
1232 /* by default, plan on stopping the task */
1233 t->expire = TICK_ETERNITY;
1234 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1235 return t;
1236
1237 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1238 /* go to full throttle if the slowstart interval is reached */
1239 s->state &= ~SRV_WARMINGUP;
1240 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1241 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1242 if (s->proxy->lbprm.update_server_eweight)
1243 s->proxy->lbprm.update_server_eweight(s);
1244 }
1245 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1246 /* for dynamic algorithms, let's slowly update the weight */
1247 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1248 s->slowstart - 1) / s->slowstart;
1249 s->eweight *= s->uweight;
1250 if (s->proxy->lbprm.update_server_eweight)
1251 s->proxy->lbprm.update_server_eweight(s);
1252 }
1253 /* Note that static algorithms are already running at full throttle */
1254
1255 /* probably that we can refill this server with a bit more connections */
1256 check_for_pending(s);
1257
1258 /* get back there in 1 second or 1/20th of the slowstart interval,
1259 * whichever is greater, resulting in small 5% steps.
1260 */
1261 if (s->state & SRV_WARMINGUP)
1262 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1263 return t;
1264}
1265
1266/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001267 * manages a server health-check. Returns
1268 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1269 */
Simon Horman63a4a822012-03-19 07:24:41 +09001270static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001271{
Willy Tarreaue3838802009-03-21 18:58:32 +01001272 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273 struct server *s = t->context;
David du Colombier6f5ccb12011-03-10 22:26:24 +01001274 struct sockaddr_storage sa;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001275 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001276 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001277
Willy Tarreaubaaee002006-06-26 02:48:02 +02001278 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001279 if (attempts++ > 0) {
1280 /* we always fail to create a server, let's stop insisting... */
1281 while (tick_is_expired(t->expire, now_ms))
1282 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1283 return t;
1284 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001285 fd = s->curfd;
1286 if (fd < 0) { /* no check currently running */
Willy Tarreau26c25062009-03-08 09:38:41 +01001287 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1288 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289
1290 /* we don't send any health-checks when the proxy is stopped or when
1291 * the server should not be checked.
1292 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001293 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001294 while (tick_is_expired(t->expire, now_ms))
1295 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001296 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297 }
1298
1299 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001300 set_server_check_status(s, HCHK_STATUS_START, NULL);
David du Colombier6f5ccb12011-03-10 22:26:24 +01001301 if ((fd = socket(s->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001302 if ((fd < global.maxsock) &&
1303 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1304 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1305 //fprintf(stderr, "process_chk: 3\n");
1306
Willy Tarreau9edd1612007-10-18 18:07:48 +02001307 if (s->proxy->options & PR_O_TCP_NOLING) {
1308 /* We don't want to useless data */
1309 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1310 }
David du Colombier6f5ccb12011-03-10 22:26:24 +01001311
1312 if (is_addr(&s->check_addr))
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001313 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001314 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001315 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001316 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001317 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001318
Willy Tarreau48da04a2011-08-27 11:51:36 +02001319 set_host_port(&sa, s->check_port);
1320
Willy Tarreaubaaee002006-06-26 02:48:02 +02001321 /* allow specific binding :
1322 * - server-specific at first
1323 * - proxy-specific next
1324 */
1325 if (s->state & SRV_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001326 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001327 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001328
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001329#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001330 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001331 remote = &s->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001332 flags = 3;
1333 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001334#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001335#ifdef SO_BINDTODEVICE
1336 /* Note: this might fail if not CAP_NET_RAW */
1337 if (s->iface_name)
1338 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001339 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001340#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001341 if (s->sport_range) {
1342 int bind_attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +01001343 struct sockaddr_storage src;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001344
1345 ret = 1;
1346 src = s->source_addr;
1347
1348 do {
1349 /* note: in case of retry, we may have to release a previously
1350 * allocated port, hence this loop's construct.
1351 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001352 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1353 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001354
1355 if (!bind_attempts)
1356 break;
1357 bind_attempts--;
1358
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001359 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1360 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001361 break;
1362
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001363 fdinfo[fd].port_range = s->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001364 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001365
David du Colombier6f5ccb12011-03-10 22:26:24 +01001366 ret = tcp_bind_socket(fd, flags, &src, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001367 } while (ret != 0); /* binding NOK */
1368 }
1369 else {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001370 ret = tcp_bind_socket(fd, flags, &s->source_addr, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001371 }
1372
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001373 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001374 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001375 switch (ret) {
1376 case 1:
1377 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1378 s->proxy->id, s->id);
1379 break;
1380 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001381 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1382 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001383 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001384 }
1385 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001386 }
1387 else if (s->proxy->options & PR_O_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001388 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001389 int ret, flags = 0;
1390
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001391#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001392 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001393 remote = &s->proxy->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001394 flags = 3;
1395 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001396#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001397#ifdef SO_BINDTODEVICE
1398 /* Note: this might fail if not CAP_NET_RAW */
1399 if (s->proxy->iface_name)
1400 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001401 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001402#endif
David du Colombier6f5ccb12011-03-10 22:26:24 +01001403 ret = tcp_bind_socket(fd, flags, &s->proxy->source_addr, remote);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001404 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001405 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001406 switch (ret) {
1407 case 1:
1408 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1409 proxy_type_str(s->proxy), s->proxy->id);
1410 break;
1411 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001412 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1413 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001414 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001415 }
1416 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417 }
1418
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001419 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001420#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001421 /* disabling tcp quick ack now allows
1422 * the request to leave the machine with
1423 * the first ACK.
1424 */
1425 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001426 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001427#endif
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +02001428 if ((connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) != -1) || (errno == EINPROGRESS)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001429 /* OK, connection in progress or established */
1430
1431 //fprintf(stderr, "process_chk: 4\n");
1432
1433 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +02001434 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001435 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +02001436 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
Willy Tarreau54469402006-07-29 16:59:06 +02001437 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001438 fdinfo[fd].peeraddr = (struct sockaddr *)&sa;
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +02001439 fdinfo[fd].peerlen = get_addr_len(&sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001440 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001441 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001442 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001443#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001444 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001445#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001446 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1447 /* we allow up to min(inter, timeout.connect) for a connection
1448 * to establish but only when timeout.check is set
1449 * as it may be to short for a full check otherwise
1450 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001451 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001452
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001453 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1454 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1455 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001456 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001457 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001458 }
1459 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001460 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001461
1462 switch (errno) {
1463 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1464 case ECONNREFUSED:
1465 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001466 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001467 break;
1468
1469 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001470 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001471 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001472 }
1473 }
1474 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001475 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1476 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001477 close(fd); /* socket creation error */
1478 }
Willy Tarreaud11ad782011-03-09 20:38:33 +01001479 else
1480 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001481
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001482 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001483 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001484 while (tick_is_expired(t->expire, now_ms))
1485 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001486 goto new_chk; /* may be we should initialize a new check */
1487 }
1488
1489 /* here, we have seen a failure */
1490 if (s->health > s->rise) {
1491 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001492 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001493 }
1494 else
1495 set_server_down(s);
1496
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001497 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1498 /* we allow up to min(inter, timeout.connect) for a connection
1499 * to establish but only when timeout.check is set
1500 * as it may be to short for a full check otherwise
1501 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001502 while (tick_is_expired(t->expire, now_ms)) {
1503 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001504
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001505 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1506 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001507
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001508 if (s->proxy->timeout.check)
1509 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001510 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001511 goto new_chk;
1512 }
1513 else {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001514 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001515 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreau48494c02007-11-30 10:41:39 +01001516 /* we may have to add/remove this server from the LB group */
1517 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1518 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001519 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1520 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001521 else if (!(s->state & SRV_GOINGDOWN) &&
1522 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001523 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1524 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001525 }
1526
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001527 if (s->health < s->rise + s->fall - 1) {
1528 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001529
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001530 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531 }
1532 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001533 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001534
1535 rv = 0;
1536 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001537 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001538 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001539 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001540 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001541 goto new_chk;
1542 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001543 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001544 if (!(s->result & SRV_CHK_ERROR)) {
1545 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001546 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001547 } else {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001548 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001549 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001550 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001551 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001552 }
1553 }
1554
Willy Tarreaubaaee002006-06-26 02:48:02 +02001555 /* failure or timeout detected */
1556 if (s->health > s->rise) {
1557 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001558 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001559 }
1560 else
1561 set_server_down(s);
1562 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001563 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001564
1565 rv = 0;
1566 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001567 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001568 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001569 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001570 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001571 goto new_chk;
1572 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001573 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001574 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001575 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001576 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001577}
1578
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001579/*
1580 * Start health-check.
1581 * Returns 0 if OK, -1 if error, and prints the error in this case.
1582 */
1583int start_checks() {
1584
1585 struct proxy *px;
1586 struct server *s;
1587 struct task *t;
1588 int nbchk=0, mininter=0, srvpos=0;
1589
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001590 /* 1- count the checkers to run simultaneously.
1591 * We also determine the minimum interval among all of those which
1592 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1593 * will be used to spread their start-up date. Those which have
1594 * a shorter interval will start independantly and will not dictate
1595 * too short an interval for all others.
1596 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001597 for (px = proxy; px; px = px->next) {
1598 for (s = px->srv; s; s = s->next) {
1599 if (!(s->state & SRV_CHECKED))
1600 continue;
1601
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001602 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1603 (!mininter || mininter > srv_getinter(s)))
1604 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001605
1606 nbchk++;
1607 }
1608 }
1609
1610 if (!nbchk)
1611 return 0;
1612
1613 srand((unsigned)time(NULL));
1614
1615 /*
1616 * 2- start them as far as possible from each others. For this, we will
1617 * start them after their interval set to the min interval divided by
1618 * the number of servers, weighted by the server's position in the list.
1619 */
1620 for (px = proxy; px; px = px->next) {
1621 for (s = px->srv; s; s = s->next) {
Willy Tarreau2e993902011-10-31 11:53:20 +01001622 if (s->slowstart) {
1623 if ((t = task_new()) == NULL) {
1624 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1625 return -1;
1626 }
1627 /* We need a warmup task that will be called when the server
1628 * state switches from down to up.
1629 */
1630 s->warmup = t;
1631 t->process = server_warmup;
1632 t->context = s;
1633 t->expire = TICK_ETERNITY;
1634 }
1635
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001636 if (!(s->state & SRV_CHECKED))
1637 continue;
1638
Willy Tarreau2e993902011-10-31 11:53:20 +01001639 /* one task for the checks */
Willy Tarreaua4613182009-03-21 18:13:21 +01001640 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001641 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1642 return -1;
1643 }
1644
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001645 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001646 t->process = process_chk;
1647 t->context = s;
1648
1649 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001650 t->expire = tick_add(now_ms,
1651 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1652 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001653 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001654 task_queue(t);
1655
1656 srvpos++;
1657 }
1658 }
1659 return 0;
1660}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001661
1662/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001663 * Perform content verification check on data in s->check_data buffer.
1664 * The buffer MUST be terminated by a null byte before calling this function.
1665 * Sets server status appropriately. The caller is responsible for ensuring
1666 * that the buffer contains at least 13 characters. If <done> is zero, we may
1667 * return 0 to indicate that data is required to decide of a match.
1668 */
1669static int httpchk_expect(struct server *s, int done)
1670{
1671 static char status_msg[] = "HTTP status check returned code <000>";
1672 char status_code[] = "000";
1673 char *contentptr;
1674 int crlf;
1675 int ret;
1676
1677 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1678 case PR_O2_EXP_STS:
1679 case PR_O2_EXP_RSTS:
1680 memcpy(status_code, s->check_data + 9, 3);
1681 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1682
1683 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1684 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1685 else
1686 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1687
1688 /* we necessarily have the response, so there are no partial failures */
1689 if (s->proxy->options2 & PR_O2_EXP_INV)
1690 ret = !ret;
1691
1692 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1693 break;
1694
1695 case PR_O2_EXP_STR:
1696 case PR_O2_EXP_RSTR:
1697 /* very simple response parser: ignore CR and only count consecutive LFs,
1698 * stop with contentptr pointing to first char after the double CRLF or
1699 * to '\0' if crlf < 2.
1700 */
1701 crlf = 0;
1702 for (contentptr = s->check_data; *contentptr; contentptr++) {
1703 if (crlf >= 2)
1704 break;
1705 if (*contentptr == '\r')
1706 continue;
1707 else if (*contentptr == '\n')
1708 crlf++;
1709 else
1710 crlf = 0;
1711 }
1712
1713 /* Check that response contains a body... */
1714 if (crlf < 2) {
1715 if (!done)
1716 return 0;
1717
1718 set_server_check_status(s, HCHK_STATUS_L7RSP,
1719 "HTTP content check could not find a response body");
1720 return 1;
1721 }
1722
1723 /* Check that response body is not empty... */
1724 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001725 if (!done)
1726 return 0;
1727
Willy Tarreaubd741542010-03-16 18:46:54 +01001728 set_server_check_status(s, HCHK_STATUS_L7RSP,
1729 "HTTP content check found empty response body");
1730 return 1;
1731 }
1732
1733 /* Check the response content against the supplied string
1734 * or regex... */
1735 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1736 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1737 else
1738 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1739
1740 /* if we don't match, we may need to wait more */
1741 if (!ret && !done)
1742 return 0;
1743
1744 if (ret) {
1745 /* content matched */
1746 if (s->proxy->options2 & PR_O2_EXP_INV)
1747 set_server_check_status(s, HCHK_STATUS_L7RSP,
1748 "HTTP check matched unwanted content");
1749 else
1750 set_server_check_status(s, HCHK_STATUS_L7OKD,
1751 "HTTP content check matched");
1752 }
1753 else {
1754 if (s->proxy->options2 & PR_O2_EXP_INV)
1755 set_server_check_status(s, HCHK_STATUS_L7OKD,
1756 "HTTP check did not match unwanted content");
1757 else
1758 set_server_check_status(s, HCHK_STATUS_L7RSP,
1759 "HTTP content check did not match");
1760 }
1761 break;
1762 }
1763 return 1;
1764}
1765
1766/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001767 * Local variables:
1768 * c-indent-level: 8
1769 * c-basic-offset: 8
1770 * End:
1771 */