blob: f63edef39bbbd3122534d557aa4abaf9ff78fa7b [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
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700361/* Shutdown all connections of a server. The caller must pass a termination
362 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
363 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900364 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700365static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900366{
367 struct session *session, *session_bck;
368
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200369 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
370 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700371 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900372}
373
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700374/* Shutdown all connections of all backup servers of a proxy. The caller must
375 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
376 * the reason for the shutdown.
377 */
378static void shutdown_backup_sessions(struct proxy *px, int why)
379{
380 struct server *srv;
381
382 for (srv = px->srv; srv != NULL; srv = srv->next)
383 if (srv->state & SRV_BACKUP)
384 shutdown_sessions(srv, why);
385}
386
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387/* Sets server <s> down, notifies by all available means, recounts the
388 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200389 * possible to other servers. It automatically recomputes the number of
390 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100392void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100394 struct server *srv;
395 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396 int xferred;
397
Cyril Bontécd19e512010-01-31 22:34:03 +0100398 if (s->state & SRV_MAINTAIN) {
399 s->health = s->rise;
400 }
401
Willy Tarreau44267702011-10-28 15:35:33 +0200402 if (s->health == s->rise || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100403 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200404 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200405
406 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100407 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200408 if (s->proxy->lbprm.set_server_status_down)
409 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900411 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700412 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900413
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 /* we might have sessions queued on this server and waiting for
415 * a connection. Those which are redispatchable will be queued
416 * to another server or to the proxy itself.
417 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100418 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100419
David du Colombier7af46052012-05-16 14:16:48 +0200420 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100421
Cyril Bontécd19e512010-01-31 22:34:03 +0100422 if (s->state & SRV_MAINTAIN) {
423 chunk_printf(&msg,
424 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
425 s->proxy->id, s->id);
426 } else {
427 chunk_printf(&msg,
428 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
429 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100430
Cyril Bontécd19e512010-01-31 22:34:03 +0100431 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200432 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Cyril Bontécd19e512010-01-31 22:34:03 +0100433 xferred);
434 }
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100435 Warning("%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200436
Willy Tarreau48494c02007-11-30 10:41:39 +0100437 /* we don't send an alert if the server was previously paused */
438 if (srv_was_paused)
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100439 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Willy Tarreau48494c02007-11-30 10:41:39 +0100440 else
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100441 send_log(s->proxy, LOG_ALERT, "%s.\n", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200442
Willy Tarreaud64d2252010-10-17 17:16:42 +0200443 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100444 set_backend_down(s->proxy);
445
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200446 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100447
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100448 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100449 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100450 if (! (srv->state & SRV_MAINTAIN))
451 /* Only notify tracking servers that are not already in maintenance. */
452 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100454
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 s->health = 0; /* failure */
456}
457
Cyril Bontécd19e512010-01-31 22:34:03 +0100458void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100459
460 struct server *srv;
461 struct chunk msg;
462 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100463 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100464
Cyril Bontécd19e512010-01-31 22:34:03 +0100465 if (s->state & SRV_MAINTAIN) {
466 s->health = s->rise;
467 }
468
Willy Tarreau44267702011-10-28 15:35:33 +0200469 if (s->health == s->rise || s->track) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100470 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
471 if (s->proxy->last_change < now.tv_sec) // ignore negative times
472 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
473 s->proxy->last_change = now.tv_sec;
474 }
475
476 if (s->last_change < now.tv_sec) // ignore negative times
477 s->down_time += now.tv_sec - s->last_change;
478
479 s->last_change = now.tv_sec;
480 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100481 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100482
483 if (s->slowstart > 0) {
484 s->state |= SRV_WARMINGUP;
485 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
486 /* For dynamic algorithms, start at the first step of the weight,
487 * without multiplying by BE_WEIGHT_SCALE.
488 */
489 s->eweight = s->uweight;
490 if (s->proxy->lbprm.update_server_eweight)
491 s->proxy->lbprm.update_server_eweight(s);
492 }
Willy Tarreau2e993902011-10-31 11:53:20 +0100493 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100494 }
Willy Tarreau9580d162012-05-19 19:07:40 +0200495 if (s->proxy->lbprm.set_server_status_up)
496 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100497
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700498 /* If the server is set with "on-marked-up shutdown-backup-sessions",
499 * and it's not a backup server and its effective weight is > 0,
500 * then it can accept new connections, so we shut down all sessions
501 * on all backup servers.
502 */
503 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
504 !(s->state & SRV_BACKUP) && s->eweight)
505 shutdown_backup_sessions(s->proxy, SN_ERR_UP);
506
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100507 /* check if we can handle some connections queued at the proxy. We
508 * will take as many as we can handle.
509 */
510 xferred = check_for_pending(s);
511
David du Colombier7af46052012-05-16 14:16:48 +0200512 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100513
Willy Tarreau45446782012-03-09 17:16:09 +0100514 if (old_state & SRV_MAINTAIN) {
Cyril Bontécd19e512010-01-31 22:34:03 +0100515 chunk_printf(&msg,
516 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
517 s->proxy->id, s->id);
518 } else {
519 chunk_printf(&msg,
520 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
521 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100522
Cyril Bontécd19e512010-01-31 22:34:03 +0100523 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200524 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Cyril Bontécd19e512010-01-31 22:34:03 +0100525 xferred);
526 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100527
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100528 Warning("%s.\n", trash);
529 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100530
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100531 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100532 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100533 if (! (srv->state & SRV_MAINTAIN))
534 /* Only notify tracking servers if they're not in maintenance. */
535 set_server_up(srv);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100536 }
537
538 if (s->health >= s->rise)
539 s->health = s->rise + s->fall - 1; /* OK now */
540
541}
542
543static void set_server_disabled(struct server *s) {
544
545 struct server *srv;
546 struct chunk msg;
547 int xferred;
548
549 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200550 if (s->proxy->lbprm.set_server_status_down)
551 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100552
553 /* we might have sessions queued on this server and waiting for
554 * a connection. Those which are redispatchable will be queued
555 * to another server or to the proxy itself.
556 */
557 xferred = redistribute_pending(s);
558
David du Colombier7af46052012-05-16 14:16:48 +0200559 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100560
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200561 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100562 "Load-balancing on %sServer %s/%s is disabled",
563 s->state & SRV_BACKUP ? "Backup " : "",
564 s->proxy->id, s->id);
565
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200566 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200567 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100568 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100569
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100570 Warning("%s.\n", trash);
571 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100572
573 if (!s->proxy->srv_bck && !s->proxy->srv_act)
574 set_backend_down(s->proxy);
575
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100576 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100577 for(srv = s->tracknext; srv; srv = srv->tracknext)
578 set_server_disabled(srv);
579}
580
581static void set_server_enabled(struct server *s) {
582
583 struct server *srv;
584 struct chunk msg;
585 int xferred;
586
587 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200588 if (s->proxy->lbprm.set_server_status_up)
589 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100590
591 /* check if we can handle some connections queued at the proxy. We
592 * will take as many as we can handle.
593 */
594 xferred = check_for_pending(s);
595
David du Colombier7af46052012-05-16 14:16:48 +0200596 chunk_init(&msg, trash, trashlen);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100597
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200598 chunk_printf(&msg,
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100599 "Load-balancing on %sServer %s/%s is enabled again",
600 s->state & SRV_BACKUP ? "Backup " : "",
601 s->proxy->id, s->id);
602
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200603 server_status_printf(&msg, s,
Willy Tarreau44267702011-10-28 15:35:33 +0200604 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0),
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100605 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100606
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100607 Warning("%s.\n", trash);
608 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100609
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100610 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100611 for(srv = s->tracknext; srv; srv = srv->tracknext)
612 set_server_enabled(srv);
613}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200614
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100615void health_adjust(struct server *s, short status) {
616
617 int failed;
618 int expire;
619
620 /* return now if observing nor health check is not enabled */
621 if (!s->observe || !s->check)
622 return;
623
624 if (s->observe >= HANA_OBS_SIZE)
625 return;
626
627 if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc)
628 return;
629
630 switch (analyze_statuses[status].lr[s->observe - 1]) {
631 case 1:
632 failed = 1;
633 break;
634
635 case 2:
636 failed = 0;
637 break;
638
639 default:
640 return;
641 }
642
643 if (!failed) {
644 /* good: clear consecutive_errors */
645 s->consecutive_errors = 0;
646 return;
647 }
648
649 s->consecutive_errors++;
650
651 if (s->consecutive_errors < s->consecutive_errors_limit)
652 return;
653
654 sprintf(trash, "Detected %d consecutive errors, last one was: %s",
655 s->consecutive_errors, get_analyze_status(status));
656
657 switch (s->onerror) {
658 case HANA_ONERR_FASTINTER:
659 /* force fastinter - nothing to do here as all modes force it */
660 break;
661
662 case HANA_ONERR_SUDDTH:
663 /* simulate a pre-fatal failed health check */
664 if (s->health > s->rise)
665 s->health = s->rise + 1;
666
667 /* no break - fall through */
668
669 case HANA_ONERR_FAILCHK:
670 /* simulate a failed health check */
671 set_server_check_status(s, HCHK_STATUS_HANA, trash);
672
673 if (s->health > s->rise) {
674 s->health--; /* still good */
675 s->counters.failed_checks++;
676 }
677 else
678 set_server_down(s);
679
680 break;
681
682 case HANA_ONERR_MARKDWN:
683 /* mark server down */
684 s->health = s->rise;
685 set_server_check_status(s, HCHK_STATUS_HANA, trash);
686 set_server_down(s);
687
688 break;
689
690 default:
691 /* write a warning? */
692 break;
693 }
694
695 s->consecutive_errors = 0;
696 s->counters.failed_hana++;
697
698 if (s->fastinter) {
699 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
700 if (s->check->expire > expire)
701 s->check->expire = expire;
702 }
703}
704
Willy Tarreauef781042010-01-27 11:53:01 +0100705static int httpchk_build_status_header(struct server *s, char *buffer)
706{
707 int sv_state;
708 int ratio;
709 int hlen = 0;
710 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
711 "UP %d/%d", "UP",
712 "NOLB %d/%d", "NOLB",
713 "no check" };
714
715 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
716 hlen += 24;
717
718 if (!(s->state & SRV_CHECKED))
719 sv_state = 6; /* should obviously never happen */
720 else if (s->state & SRV_RUNNING) {
721 if (s->health == s->rise + s->fall - 1)
722 sv_state = 3; /* UP */
723 else
724 sv_state = 2; /* going down */
725
726 if (s->state & SRV_GOINGDOWN)
727 sv_state += 2;
728 } else {
729 if (s->health)
730 sv_state = 1; /* going up */
731 else
732 sv_state = 0; /* DOWN */
733 }
734
735 hlen += sprintf(buffer + hlen,
736 srv_hlt_st[sv_state],
737 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
738 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
739
740 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
741 s->proxy->id, s->id,
742 global.node,
743 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
744 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
745 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
746 s->nbpend);
747
748 if ((s->state & SRV_WARMINGUP) &&
749 now.tv_sec < s->last_change + s->slowstart &&
750 now.tv_sec >= s->last_change) {
751 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
752 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
753 }
754
755 buffer[hlen++] = '\r';
756 buffer[hlen++] = '\n';
757
758 return hlen;
759}
760
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761/*
762 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200763 * the connection acknowledgement. If the proxy requires L7 health-checks,
764 * it sends the request. In other cases, it calls set_server_check_status()
765 * to set s->check_status, s->check_duration and s->result.
Willy Tarreau83749182007-04-15 20:56:27 +0200766 * The function itself returns 0 if it needs some polling before being called
767 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 */
Willy Tarreau83749182007-04-15 20:56:27 +0200769static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770{
Willy Tarreau6996e152007-04-30 14:37:43 +0200771 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 struct task *t = fdtab[fd].owner;
773 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774
Willy Tarreau505e34a2012-07-06 10:17:53 +0200775 if (unlikely((s->check_conn->flags & CO_FL_ERROR) || (fdtab[fd].ev & FD_POLL_ERR))) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100776 int skerr, err = errno;
777 socklen_t lskerr = sizeof(skerr);
778
779 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
780 err = skerr;
781
782 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200783 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200784 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200785
786 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200787
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100788 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200790 if (s->proxy->options2 & PR_O2_CHK_ANY) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 int ret;
Willy Tarreaue9d87882010-01-27 11:28:42 +0100792 const char *check_req = s->proxy->check_req;
793 int check_len = s->proxy->check_len;
794
Willy Tarreauf3c69202006-07-09 16:42:34 +0200795 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 * so we'll send the request, and won't wake the checker up now.
797 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200798
Willy Tarreau1620ec32011-08-06 17:05:02 +0200799 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) {
Willy Tarreauf3c69202006-07-09 16:42:34 +0200800 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200801 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200802 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
803 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200804 else if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK) {
Willy Tarreaue9d87882010-01-27 11:28:42 +0100805 memcpy(trash, check_req, check_len);
Willy Tarreauef781042010-01-27 11:53:01 +0100806
807 if (s->proxy->options2 & PR_O2_CHK_SNDST)
808 check_len += httpchk_build_status_header(s, trash + check_len);
809
Willy Tarreaue9d87882010-01-27 11:28:42 +0100810 trash[check_len++] = '\r';
811 trash[check_len++] = '\n';
812 trash[check_len] = '\0';
813 check_req = trash;
814 }
Willy Tarreauf3c69202006-07-09 16:42:34 +0200815
Willy Tarreaue9d87882010-01-27 11:28:42 +0100816 ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
817 if (ret == check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100818 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau1e44a492012-05-25 07:41:38 +0200819 if (s->proxy->timeout.check) {
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100820 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreau1e44a492012-05-25 07:41:38 +0200821 task_queue(t);
822 }
Willy Tarreauf161a342007-04-08 16:59:42 +0200823 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200824 goto out_nowake;
825 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200826 else if (ret == 0 || errno == EAGAIN)
827 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200828 else {
829 switch (errno) {
830 case ECONNREFUSED:
831 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200832 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200833 break;
834
835 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200836 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200837 }
838
Willy Tarreau6996e152007-04-30 14:37:43 +0200839 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200840 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 }
842 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200843 /* We have no data to send to check the connection, and
844 * getsockopt() will not inform us whether the connection
845 * is still pending. So we'll reuse connect() to check the
846 * state of the socket. This has the advantage of givig us
847 * the following info :
848 * - error
849 * - connecting (EALREADY, EINPROGRESS)
850 * - connected (EISCONN, 0)
851 */
852
David du Colombier6f5ccb12011-03-10 22:26:24 +0100853 struct sockaddr_storage sa;
Willy Tarreau6996e152007-04-30 14:37:43 +0200854
David du Colombier6f5ccb12011-03-10 22:26:24 +0100855 if (is_addr(&s->check_addr))
856 sa = s->check_addr;
857 else
858 sa = s->addr;
859
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200860 set_host_port(&sa, s->check_port);
Willy Tarreau6996e152007-04-30 14:37:43 +0200861
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200862 if (connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) == 0)
Willy Tarreau6996e152007-04-30 14:37:43 +0200863 errno = 0;
864
865 if (errno == EALREADY || errno == EINPROGRESS)
866 goto out_poll;
867
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200868 if (errno && errno != EISCONN) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200869 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau6996e152007-04-30 14:37:43 +0200870 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200871 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200872
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 /* good TCP connection is enough */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200874 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau6996e152007-04-30 14:37:43 +0200875 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200876 }
877 }
Willy Tarreau83749182007-04-15 20:56:27 +0200878 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200879 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200880 out_nowake:
881 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100882 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200883 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200884 out_poll:
885 /* The connection is still pending. We'll have to poll it
886 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100887 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200888 return 0;
889 out_error:
Willy Tarreau505e34a2012-07-06 10:17:53 +0200890 s->check_conn->flags |= CO_FL_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200891 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200892}
893
894
895/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200896 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200897 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
898 * set_server_check_status() to update s->check_status, s->check_duration
899 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200900
901 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
902 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
903 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
904 * response to an SSL HELLO (the principle is that this is enough to
905 * distinguish between an SSL server and a pure TCP relay). All other cases will
906 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
907 * etc.
908 *
909 * The function returns 0 if it needs to be called again after some polling,
910 * otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200911 */
Willy Tarreau83749182007-04-15 20:56:27 +0200912static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913{
Willy Tarreau83749182007-04-15 20:56:27 +0200914 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100915 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200916 struct task *t = fdtab[fd].owner;
917 struct server *s = t->context;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200918 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100919 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200920 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200921
Willy Tarreau505e34a2012-07-06 10:17:53 +0200922 if (unlikely((s->result & SRV_CHK_ERROR) || (s->check_conn->flags & CO_FL_ERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200923 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200924 if (!(s->result & SRV_CHK_ERROR))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200925 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200926
Willy Tarreau83749182007-04-15 20:56:27 +0200927 goto out_wakeup;
928 }
929
Willy Tarreau83749182007-04-15 20:56:27 +0200930 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
931 * but the connection was closed on the remote end. Fortunately, recv still
932 * works correctly and we don't need to do the getsockopt() on linux.
933 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000934
935 /* Set buffer to point to the end of the data already read, and check
936 * that there is free space remaining. If the buffer is full, proceed
937 * with running the checks without attempting another socket read.
938 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000939
Willy Tarreau03938182010-03-17 21:52:07 +0100940 done = 0;
Willy Tarreau43961d52010-10-04 20:39:20 +0200941 for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) {
942 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 +0100943 if (len <= 0)
944 break;
945 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000946
Willy Tarreau03938182010-03-17 21:52:07 +0100947 if (len == 0)
948 done = 1; /* connection hangup received */
949 else if (len < 0 && errno != EAGAIN) {
Willy Tarreauc1a07962010-03-16 20:55:43 +0100950 /* Report network errors only if we got no other data. Otherwise
951 * we'll let the upper layers decide whether the response is OK
952 * or not. It is very common that an RST sent by the server is
953 * reported as an error just after the last data chunk.
954 */
Willy Tarreau03938182010-03-17 21:52:07 +0100955 done = 1;
Willy Tarreauc1a07962010-03-16 20:55:43 +0100956 if (!s->check_data_len) {
957 if (!(s->result & SRV_CHK_ERROR))
958 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
959 goto out_wakeup;
960 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200961 }
962
Willy Tarreau03938182010-03-17 21:52:07 +0100963 /* Intermediate or complete response received.
964 * Terminate string in check_data buffer.
965 */
Willy Tarreau43961d52010-10-04 20:39:20 +0200966 if (s->check_data_len < global.tune.chksize)
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100967 s->check_data[s->check_data_len] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100968 else {
Willy Tarreau2c7ace02010-03-16 20:32:04 +0100969 s->check_data[s->check_data_len - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100970 done = 1; /* buffer full, don't wait for more data */
971 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200972
Nick Chalk57b1bf72010-03-16 15:50:46 +0000973 /* Run the checks... */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200974 switch (s->proxy->options2 & PR_O2_CHK_ANY) {
975 case PR_O2_HTTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +0100976 if (!done && s->check_data_len < strlen("HTTP/1.0 000\r"))
977 goto wait_more_data;
978
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100979 /* Check if the server speaks HTTP 1.X */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000980 if ((s->check_data_len < strlen("HTTP/1.0 000\r")) ||
981 (memcmp(s->check_data, "HTTP/1.", 7) != 0 ||
982 (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) ||
983 !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) ||
984 !isdigit((unsigned char) *(s->check_data + 11))) {
985 cut_crlf(s->check_data);
986 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200987
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100988 goto out_wakeup;
989 }
990
Nick Chalk57b1bf72010-03-16 15:50:46 +0000991 s->check_code = str2uic(s->check_data + 9);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000992 desc = ltrim(s->check_data + 12, ' ');
993
Willy Tarreaubd741542010-03-16 18:46:54 +0100994 if ((s->proxy->options & PR_O_DISABLE404) &&
995 (s->state & SRV_RUNNING) && (s->check_code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000996 /* 404 may be accepted as "stopping" only if the server was up */
997 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200998 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000999 }
Willy Tarreaubd741542010-03-16 18:46:54 +01001000 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
1001 /* Run content verification check... We know we have at least 13 chars */
1002 if (!httpchk_expect(s, done))
1003 goto wait_more_data;
1004 }
1005 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
1006 else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') {
1007 cut_crlf(desc);
1008 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1009 }
Nick Chalk57b1bf72010-03-16 15:50:46 +00001010 else {
1011 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001012 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001013 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001014 break;
1015
1016 case PR_O2_SSL3_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001017 if (!done && s->check_data_len < 5)
1018 goto wait_more_data;
1019
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001020 /* Check for SSLv3 alert or handshake */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001021 if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001022 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001023 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001024 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001025 break;
1026
1027 case PR_O2_SMTP_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001028 if (!done && s->check_data_len < strlen("000\r"))
1029 goto wait_more_data;
1030
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001031 /* Check if the server speaks SMTP */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001032 if ((s->check_data_len < strlen("000\r")) ||
1033 (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') ||
1034 !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) ||
1035 !isdigit((unsigned char) *(s->check_data + 2))) {
1036 cut_crlf(s->check_data);
1037 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001038
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001039 goto out_wakeup;
1040 }
1041
Nick Chalk57b1bf72010-03-16 15:50:46 +00001042 s->check_code = str2uic(s->check_data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001043
Nick Chalk57b1bf72010-03-16 15:50:46 +00001044 desc = ltrim(s->check_data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001045 cut_crlf(desc);
1046
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001047 /* Check for SMTP code 2xx (should be 250) */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001048 if (*s->check_data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001049 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001050 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001051 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001052 break;
1053
1054 case PR_O2_PGSQL_CHK:
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001055 if (!done && s->check_data_len < 9)
1056 goto wait_more_data;
1057
1058 if (s->check_data[0] == 'R') {
1059 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1060 }
1061 else {
1062 if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0))
1063 desc = &s->check_data[6];
1064 else
1065 desc = "PostgreSQL unknown error";
1066
1067 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1068 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001069 break;
1070
1071 case PR_O2_REDIS_CHK:
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001072 if (!done && s->check_data_len < 7)
1073 goto wait_more_data;
1074
1075 if (strcmp(s->check_data, "+PONG\r\n") == 0) {
1076 set_server_check_status(s, HCHK_STATUS_L7OKD, "Redis server is ok");
1077 }
1078 else {
1079 set_server_check_status(s, HCHK_STATUS_L7STS, s->check_data);
1080 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001081 break;
1082
1083 case PR_O2_MYSQL_CHK:
Willy Tarreau03938182010-03-17 21:52:07 +01001084 if (!done && s->check_data_len < 5)
1085 goto wait_more_data;
1086
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001087 if (s->proxy->check_len == 0) { // old mode
1088 if (*(s->check_data + 4) != '\xff') {
1089 /* We set the MySQL Version in description for information purpose
1090 * FIXME : it can be cool to use MySQL Version for other purpose,
1091 * like mark as down old MySQL server.
1092 */
1093 if (s->check_data_len > 51) {
1094 desc = ltrim(s->check_data + 5, ' ');
1095 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1096 }
1097 else {
1098 if (!done)
1099 goto wait_more_data;
1100 /* it seems we have a OK packet but without a valid length,
1101 * it must be a protocol error
1102 */
1103 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data);
1104 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001105 }
1106 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001107 /* An error message is attached in the Error packet */
1108 desc = ltrim(s->check_data + 7, ' ');
1109 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1110 }
1111 } else {
1112 unsigned int first_packet_len = ((unsigned int) *s->check_data) +
1113 (((unsigned int) *(s->check_data + 1)) << 8) +
1114 (((unsigned int) *(s->check_data + 2)) << 16);
1115
1116 if (s->check_data_len == first_packet_len + 4) {
1117 /* MySQL Error packet always begin with field_count = 0xff */
1118 if (*(s->check_data + 4) != '\xff') {
1119 /* We have only one MySQL packet and it is a Handshake Initialization packet
1120 * but we need to have a second packet to know if it is alright
1121 */
1122 if (!done && s->check_data_len < first_packet_len + 5)
1123 goto wait_more_data;
1124 }
1125 else {
1126 /* We have only one packet and it is an Error packet,
1127 * an error message is attached, so we can display it
1128 */
1129 desc = &s->check_data[7];
1130 //Warning("onlyoneERR: %s\n", desc);
1131 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1132 }
1133 } else if (s->check_data_len > first_packet_len + 4) {
1134 unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) +
1135 (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) +
1136 (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16);
1137
1138 if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) {
1139 /* We have 2 packets and that's good */
1140 /* Check if the second packet is a MySQL Error packet or not */
1141 if (*(s->check_data + first_packet_len + 8) != '\xff') {
1142 /* No error packet */
1143 /* We set the MySQL Version in description for information purpose */
1144 desc = &s->check_data[5];
1145 //Warning("2packetOK: %s\n", desc);
1146 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1147 }
1148 else {
1149 /* An error message is attached in the Error packet
1150 * so we can display it ! :)
1151 */
1152 desc = &s->check_data[first_packet_len+11];
1153 //Warning("2packetERR: %s\n", desc);
1154 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1155 }
1156 }
1157 }
1158 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001159 if (!done)
1160 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001161 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001162 * it must be a protocol error
1163 */
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001164 desc = &s->check_data[5];
1165 //Warning("protoerr: %s\n", desc);
1166 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001167 }
1168 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001169 break;
1170
1171 case PR_O2_LDAP_CHK:
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001172 if (!done && s->check_data_len < 14)
1173 goto wait_more_data;
1174
1175 /* Check if the server speaks LDAP (ASN.1/BER)
1176 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1177 * http://tools.ietf.org/html/rfc4511
1178 */
1179
1180 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1181 * LDAPMessage: 0x30: SEQUENCE
1182 */
1183 if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) {
1184 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1185 }
1186 else {
1187 /* size of LDAPMessage */
1188 msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0;
1189
1190 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1191 * messageID: 0x02 0x01 0x01: INTEGER 1
1192 * protocolOp: 0x61: bindResponse
1193 */
1194 if ((msglen > 2) ||
1195 (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1196 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1197
1198 goto out_wakeup;
1199 }
1200
1201 /* size of bindResponse */
1202 msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0;
1203
1204 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1205 * ldapResult: 0x0a 0x01: ENUMERATION
1206 */
1207 if ((msglen > 4) ||
1208 (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1209 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1210
1211 goto out_wakeup;
1212 }
1213
1214 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1215 * resultCode
1216 */
1217 s->check_code = *(s->check_data + msglen + 9);
1218 if (s->check_code) {
1219 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1220 } else {
1221 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1222 }
1223 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001224 break;
1225
1226 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001227 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001228 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001229 break;
1230 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001231
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001232 out_wakeup:
1233 if (s->result & SRV_CHK_ERROR)
Willy Tarreau505e34a2012-07-06 10:17:53 +02001234 s->check_conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001235
Nick Chalk57b1bf72010-03-16 15:50:46 +00001236 /* Reset the check buffer... */
1237 *s->check_data = '\0';
1238 s->check_data_len = 0;
1239
Willy Tarreau03938182010-03-17 21:52:07 +01001240 /* Close the connection... */
1241 shutdown(fd, SHUT_RDWR);
Willy Tarreauf161a342007-04-08 16:59:42 +02001242 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001243 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +01001244 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +02001245 return 1;
Willy Tarreau03938182010-03-17 21:52:07 +01001246
1247 wait_more_data:
1248 fdtab[fd].ev &= ~FD_POLL_IN;
1249 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001250}
1251
Willy Tarreau20bea422012-07-06 12:00:49 +02001252/* I/O call back for the health checks. Returns FD_WAIT_*. */
1253static int check_iocb(int fd)
1254{
1255 int ret = 0;
1256 int e;
1257
1258 if (!fdtab[fd].owner)
1259 return ret;
1260
1261 e = fdtab[fd].ev;
1262 if (e & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
1263 if (!event_srv_chk_r(fd))
1264 ret |= FD_WAIT_READ;
1265 if (e & (FD_POLL_OUT | FD_POLL_ERR))
1266 if (!event_srv_chk_w(fd))
1267 ret |= FD_WAIT_WRITE;
1268 return ret;
1269}
1270
Willy Tarreaubaaee002006-06-26 02:48:02 +02001271/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001272 * updates the server's weight during a warmup stage. Once the final weight is
1273 * reached, the task automatically stops. Note that any server status change
1274 * must have updated s->last_change accordingly.
1275 */
1276static struct task *server_warmup(struct task *t)
1277{
1278 struct server *s = t->context;
1279
1280 /* by default, plan on stopping the task */
1281 t->expire = TICK_ETERNITY;
1282 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1283 return t;
1284
1285 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1286 /* go to full throttle if the slowstart interval is reached */
1287 s->state &= ~SRV_WARMINGUP;
1288 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1289 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1290 if (s->proxy->lbprm.update_server_eweight)
1291 s->proxy->lbprm.update_server_eweight(s);
1292 }
1293 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1294 /* for dynamic algorithms, let's slowly update the weight */
1295 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1296 s->slowstart - 1) / s->slowstart;
1297 s->eweight *= s->uweight;
1298 if (s->proxy->lbprm.update_server_eweight)
1299 s->proxy->lbprm.update_server_eweight(s);
1300 }
1301 /* Note that static algorithms are already running at full throttle */
1302
1303 /* probably that we can refill this server with a bit more connections */
1304 check_for_pending(s);
1305
1306 /* get back there in 1 second or 1/20th of the slowstart interval,
1307 * whichever is greater, resulting in small 5% steps.
1308 */
1309 if (s->state & SRV_WARMINGUP)
1310 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1311 return t;
1312}
1313
1314/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 * manages a server health-check. Returns
1316 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1317 */
Simon Horman63a4a822012-03-19 07:24:41 +09001318static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001319{
Willy Tarreaue3838802009-03-21 18:58:32 +01001320 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001321 struct server *s = t->context;
David du Colombier6f5ccb12011-03-10 22:26:24 +01001322 struct sockaddr_storage sa;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001323 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001324 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001325
Willy Tarreaubaaee002006-06-26 02:48:02 +02001326 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +01001327 if (attempts++ > 0) {
1328 /* we always fail to create a server, let's stop insisting... */
1329 while (tick_is_expired(t->expire, now_ms))
1330 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
1331 return t;
1332 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001333 fd = s->curfd;
1334 if (fd < 0) { /* no check currently running */
Willy Tarreau26c25062009-03-08 09:38:41 +01001335 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
1336 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001337
1338 /* we don't send any health-checks when the proxy is stopped or when
1339 * the server should not be checked.
1340 */
Cyril Bontécd19e512010-01-31 22:34:03 +01001341 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001342 while (tick_is_expired(t->expire, now_ms))
1343 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +01001344 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001345 }
1346
1347 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001348 set_server_check_status(s, HCHK_STATUS_START, NULL);
David du Colombier6f5ccb12011-03-10 22:26:24 +01001349 if ((fd = socket(s->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001350 if ((fd < global.maxsock) &&
1351 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
1352 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
1353 //fprintf(stderr, "process_chk: 3\n");
1354
Willy Tarreau9edd1612007-10-18 18:07:48 +02001355 if (s->proxy->options & PR_O_TCP_NOLING) {
1356 /* We don't want to useless data */
1357 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1358 }
David du Colombier6f5ccb12011-03-10 22:26:24 +01001359
1360 if (is_addr(&s->check_addr))
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001361 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001362 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001363 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001364 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001365 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +02001366
Willy Tarreau48da04a2011-08-27 11:51:36 +02001367 set_host_port(&sa, s->check_port);
1368
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369 /* allow specific binding :
1370 * - server-specific at first
1371 * - proxy-specific next
1372 */
1373 if (s->state & SRV_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001374 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001375 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +01001376
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001377#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001378 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001379 remote = &s->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001380 flags = 3;
1381 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001382#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +01001383#ifdef SO_BINDTODEVICE
1384 /* Note: this might fail if not CAP_NET_RAW */
1385 if (s->iface_name)
1386 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001387 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +01001388#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001389 if (s->sport_range) {
1390 int bind_attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +01001391 struct sockaddr_storage src;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001392
1393 ret = 1;
1394 src = s->source_addr;
1395
1396 do {
1397 /* note: in case of retry, we may have to release a previously
1398 * allocated port, hence this loop's construct.
1399 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001400 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1401 fdinfo[fd].port_range = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001402
1403 if (!bind_attempts)
1404 break;
1405 bind_attempts--;
1406
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001407 fdinfo[fd].local_port = port_range_alloc_port(s->sport_range);
1408 if (!fdinfo[fd].local_port)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001409 break;
1410
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001411 fdinfo[fd].port_range = s->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001412 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001413
David du Colombier6f5ccb12011-03-10 22:26:24 +01001414 ret = tcp_bind_socket(fd, flags, &src, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001415 } while (ret != 0); /* binding NOK */
1416 }
1417 else {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001418 ret = tcp_bind_socket(fd, flags, &s->source_addr, remote);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001419 }
1420
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001421 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001422 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001423 switch (ret) {
1424 case 1:
1425 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
1426 s->proxy->id, s->id);
1427 break;
1428 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +01001429 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
1430 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001431 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001432 }
1433 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001434 }
1435 else if (s->proxy->options & PR_O_BIND_SRC) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001436 struct sockaddr_storage *remote = NULL;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001437 int ret, flags = 0;
1438
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001439#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +01001440 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01001441 remote = &s->proxy->tproxy_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001442 flags = 3;
1443 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +01001444#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001445#ifdef SO_BINDTODEVICE
1446 /* Note: this might fail if not CAP_NET_RAW */
1447 if (s->proxy->iface_name)
1448 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +01001449 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +01001450#endif
David du Colombier6f5ccb12011-03-10 22:26:24 +01001451 ret = tcp_bind_socket(fd, flags, &s->proxy->source_addr, remote);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001452 if (ret) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001453 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001454 switch (ret) {
1455 case 1:
1456 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
1457 proxy_type_str(s->proxy), s->proxy->id);
1458 break;
1459 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +01001460 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
1461 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001462 break;
Willy Tarreau163c5322006-11-14 16:18:41 +01001463 }
1464 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001465 }
1466
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001467 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001468#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +02001469 /* disabling tcp quick ack now allows
1470 * the request to leave the machine with
1471 * the first ACK.
1472 */
1473 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001474 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +02001475#endif
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +02001476 if ((connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) != -1) || (errno == EINPROGRESS)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001477 /* OK, connection in progress or established */
1478
1479 //fprintf(stderr, "process_chk: 4\n");
1480
1481 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau505e34a2012-07-06 10:17:53 +02001482 s->check_conn->flags = CO_FL_WAIT_L4_CONN; /* TCP connection pending */
Willy Tarreau7a966482007-04-15 10:58:02 +02001483 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001484 fdtab[fd].owner = t;
Willy Tarreau20bea422012-07-06 12:00:49 +02001485 fdtab[fd].iocb = &check_iocb;
Willy Tarreaufb14edc2009-06-14 15:24:37 +02001486 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +02001487 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001488#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +02001489 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001490#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001491 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1492 /* we allow up to min(inter, timeout.connect) for a connection
1493 * to establish but only when timeout.check is set
1494 * as it may be to short for a full check otherwise
1495 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001496 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001497
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001498 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1499 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1500 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +01001501 }
Willy Tarreau26c25062009-03-08 09:38:41 +01001502 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001503 }
1504 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001505 /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001506
1507 switch (errno) {
1508 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
1509 case ECONNREFUSED:
1510 case ENETUNREACH:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001511 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001512 break;
1513
1514 default:
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001515 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001516 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001517 }
1518 }
1519 }
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02001520 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
1521 fdinfo[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001522 close(fd); /* socket creation error */
1523 }
Willy Tarreaud11ad782011-03-09 20:38:33 +01001524 else
1525 set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001526
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001527 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001528 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001529 while (tick_is_expired(t->expire, now_ms))
1530 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531 goto new_chk; /* may be we should initialize a new check */
1532 }
1533
1534 /* here, we have seen a failure */
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
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001542 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
1543 /* we allow up to min(inter, timeout.connect) for a connection
1544 * to establish but only when timeout.check is set
1545 * as it may be to short for a full check otherwise
1546 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001547 while (tick_is_expired(t->expire, now_ms)) {
1548 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001549
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001550 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1551 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001552
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001553 if (s->proxy->timeout.check)
1554 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001555 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001556 goto new_chk;
1557 }
1558 else {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001559 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001560 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreau48494c02007-11-30 10:41:39 +01001561 /* we may have to add/remove this server from the LB group */
1562 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
1563 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001564 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
1565 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001566 else if (!(s->state & SRV_GOINGDOWN) &&
1567 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001568 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
1569 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001570 }
1571
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001572 if (s->health < s->rise + s->fall - 1) {
1573 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001574
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001575 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001576 }
1577 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001578 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001579
1580 rv = 0;
1581 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001582 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001583 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreau44ec0f02007-10-14 23:47:04 +02001584 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001585 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001586 goto new_chk;
1587 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001588 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001589 if (!(s->result & SRV_CHK_ERROR)) {
1590 if (!EV_FD_ISSET(fd, DIR_RD)) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001591 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001592 } else {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001593 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK)
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001594 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001595 else /* HTTP, SMTP */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001596 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001597 }
1598 }
1599
Willy Tarreaubaaee002006-06-26 02:48:02 +02001600 /* failure or timeout detected */
1601 if (s->health > s->rise) {
1602 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001603 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001604 }
1605 else
1606 set_server_down(s);
1607 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001608 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001609
1610 rv = 0;
1611 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001612 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001613 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001614 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001615 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001616 goto new_chk;
1617 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001618 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001619 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001620 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001621 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001622}
1623
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001624/*
1625 * Start health-check.
1626 * Returns 0 if OK, -1 if error, and prints the error in this case.
1627 */
1628int start_checks() {
1629
1630 struct proxy *px;
1631 struct server *s;
1632 struct task *t;
1633 int nbchk=0, mininter=0, srvpos=0;
1634
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001635 /* 1- count the checkers to run simultaneously.
1636 * We also determine the minimum interval among all of those which
1637 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1638 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001639 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001640 * too short an interval for all others.
1641 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001642 for (px = proxy; px; px = px->next) {
1643 for (s = px->srv; s; s = s->next) {
1644 if (!(s->state & SRV_CHECKED))
1645 continue;
1646
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001647 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1648 (!mininter || mininter > srv_getinter(s)))
1649 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001650
1651 nbchk++;
1652 }
1653 }
1654
1655 if (!nbchk)
1656 return 0;
1657
1658 srand((unsigned)time(NULL));
1659
1660 /*
1661 * 2- start them as far as possible from each others. For this, we will
1662 * start them after their interval set to the min interval divided by
1663 * the number of servers, weighted by the server's position in the list.
1664 */
1665 for (px = proxy; px; px = px->next) {
1666 for (s = px->srv; s; s = s->next) {
Willy Tarreau2e993902011-10-31 11:53:20 +01001667 if (s->slowstart) {
1668 if ((t = task_new()) == NULL) {
1669 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1670 return -1;
1671 }
1672 /* We need a warmup task that will be called when the server
1673 * state switches from down to up.
1674 */
1675 s->warmup = t;
1676 t->process = server_warmup;
1677 t->context = s;
1678 t->expire = TICK_ETERNITY;
1679 }
1680
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001681 if (!(s->state & SRV_CHECKED))
1682 continue;
1683
Willy Tarreau2e993902011-10-31 11:53:20 +01001684 /* one task for the checks */
Willy Tarreaua4613182009-03-21 18:13:21 +01001685 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001686 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1687 return -1;
1688 }
1689
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001690 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001691 t->process = process_chk;
1692 t->context = s;
1693
1694 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001695 t->expire = tick_add(now_ms,
1696 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1697 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001698 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001699 task_queue(t);
1700
1701 srvpos++;
1702 }
1703 }
1704 return 0;
1705}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001706
1707/*
Willy Tarreaubd741542010-03-16 18:46:54 +01001708 * Perform content verification check on data in s->check_data buffer.
1709 * The buffer MUST be terminated by a null byte before calling this function.
1710 * Sets server status appropriately. The caller is responsible for ensuring
1711 * that the buffer contains at least 13 characters. If <done> is zero, we may
1712 * return 0 to indicate that data is required to decide of a match.
1713 */
1714static int httpchk_expect(struct server *s, int done)
1715{
1716 static char status_msg[] = "HTTP status check returned code <000>";
1717 char status_code[] = "000";
1718 char *contentptr;
1719 int crlf;
1720 int ret;
1721
1722 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1723 case PR_O2_EXP_STS:
1724 case PR_O2_EXP_RSTS:
1725 memcpy(status_code, s->check_data + 9, 3);
1726 memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3);
1727
1728 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1729 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1730 else
1731 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1732
1733 /* we necessarily have the response, so there are no partial failures */
1734 if (s->proxy->options2 & PR_O2_EXP_INV)
1735 ret = !ret;
1736
1737 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1738 break;
1739
1740 case PR_O2_EXP_STR:
1741 case PR_O2_EXP_RSTR:
1742 /* very simple response parser: ignore CR and only count consecutive LFs,
1743 * stop with contentptr pointing to first char after the double CRLF or
1744 * to '\0' if crlf < 2.
1745 */
1746 crlf = 0;
1747 for (contentptr = s->check_data; *contentptr; contentptr++) {
1748 if (crlf >= 2)
1749 break;
1750 if (*contentptr == '\r')
1751 continue;
1752 else if (*contentptr == '\n')
1753 crlf++;
1754 else
1755 crlf = 0;
1756 }
1757
1758 /* Check that response contains a body... */
1759 if (crlf < 2) {
1760 if (!done)
1761 return 0;
1762
1763 set_server_check_status(s, HCHK_STATUS_L7RSP,
1764 "HTTP content check could not find a response body");
1765 return 1;
1766 }
1767
1768 /* Check that response body is not empty... */
1769 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001770 if (!done)
1771 return 0;
1772
Willy Tarreaubd741542010-03-16 18:46:54 +01001773 set_server_check_status(s, HCHK_STATUS_L7RSP,
1774 "HTTP content check found empty response body");
1775 return 1;
1776 }
1777
1778 /* Check the response content against the supplied string
1779 * or regex... */
1780 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1781 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1782 else
1783 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1784
1785 /* if we don't match, we may need to wait more */
1786 if (!ret && !done)
1787 return 0;
1788
1789 if (ret) {
1790 /* content matched */
1791 if (s->proxy->options2 & PR_O2_EXP_INV)
1792 set_server_check_status(s, HCHK_STATUS_L7RSP,
1793 "HTTP check matched unwanted content");
1794 else
1795 set_server_check_status(s, HCHK_STATUS_L7OKD,
1796 "HTTP content check matched");
1797 }
1798 else {
1799 if (s->proxy->options2 & PR_O2_EXP_INV)
1800 set_server_check_status(s, HCHK_STATUS_L7OKD,
1801 "HTTP check did not match unwanted content");
1802 else
1803 set_server_check_status(s, HCHK_STATUS_L7RSP,
1804 "HTTP content check did not match");
1805 }
1806 break;
1807 }
1808 return 1;
1809}
1810
1811/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001812 * Local variables:
1813 * c-indent-level: 8
1814 * c-basic-offset: 8
1815 * End:
1816 */