blob: 8f6b9ff918b21c2a528f8075e10f3c477c709a14 [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 Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020033#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Baptiste Assmann69e273f2013-12-11 00:52:19 +010038#ifdef USE_OPENSSL
39#include <types/ssl_sock.h>
40#include <proto/ssl_sock.h>
41#endif /* USE_OPENSSL */
42
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090045#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/fd.h>
47#include <proto/log.h>
48#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020049#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010050#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010051#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010052#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010053#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020054#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090056#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010057#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreaubd741542010-03-16 18:46:54 +010060static int httpchk_expect(struct server *s, int done);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020061static int tcpcheck_get_step_id(struct server *);
62static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010063
Simon Horman63a4a822012-03-19 07:24:41 +090064static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010065 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
66 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020067 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020068
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010069 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010070
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010071 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010073 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
74 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
75 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010077 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
78 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
79 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020080
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010081 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
82 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020083
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020084 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020085
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010086 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
87 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
88 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020089};
90
Simon Horman63a4a822012-03-19 07:24:41 +090091static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010092 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
93
94 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
95 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
96
97 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
98 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
99 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
100 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
101
102 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
103 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
104 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
105};
106
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200107/*
108 * Convert check_status code to description
109 */
110const char *get_check_status_description(short check_status) {
111
112 const char *desc;
113
114 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200115 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200116 else
117 desc = NULL;
118
119 if (desc && *desc)
120 return desc;
121 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200122 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200123}
124
125/*
126 * Convert check_status code to short info
127 */
128const char *get_check_status_info(short check_status) {
129
130 const char *info;
131
132 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200133 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200134 else
135 info = NULL;
136
137 if (info && *info)
138 return info;
139 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200140 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200141}
142
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100143const char *get_analyze_status(short analyze_status) {
144
145 const char *desc;
146
147 if (analyze_status < HANA_STATUS_SIZE)
148 desc = analyze_statuses[analyze_status].desc;
149 else
150 desc = NULL;
151
152 if (desc && *desc)
153 return desc;
154 else
155 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
156}
157
Willy Tarreaua150cf12014-05-20 21:57:23 +0200158/* Builds a string containing some information about the health check's result.
159 * The output string is allocated from the trash chunks. If the check is NULL,
160 * NULL is returned. This is designed to be used when emitting logs about health
161 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200162 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200163static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200164{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200165 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200166
Willy Tarreaua150cf12014-05-20 21:57:23 +0200167 if (!check)
168 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200169
Willy Tarreaua150cf12014-05-20 21:57:23 +0200170 msg = get_trash_chunk();
171 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200172
Willy Tarreaua150cf12014-05-20 21:57:23 +0200173 if (check->status >= HCHK_STATUS_L57DATA)
174 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200175
Willy Tarreaua150cf12014-05-20 21:57:23 +0200176 if (*check->desc) {
177 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200178
Willy Tarreaua150cf12014-05-20 21:57:23 +0200179 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200180
Willy Tarreaua150cf12014-05-20 21:57:23 +0200181 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
182 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200183
Willy Tarreaua150cf12014-05-20 21:57:23 +0200184 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200185 }
186
Willy Tarreaua150cf12014-05-20 21:57:23 +0200187 if (check->duration >= 0)
188 chunk_appendf(msg, ", check duration: %ldms", check->duration);
189
190 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200191}
192
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200193/*
Simon Horman4a741432013-02-23 15:35:38 +0900194 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200195 * an adequate CHK_RES_* value. The new check->health is computed based
196 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200197 *
198 * Show information in logs about failed health check if server is UP
199 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200200 */
Simon Horman4a741432013-02-23 15:35:38 +0900201static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100202{
Simon Horman4a741432013-02-23 15:35:38 +0900203 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200204 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200205 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900206
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200207 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100208 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900209 check->desc[0] = '\0';
210 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200211 return;
212 }
213
Simon Horman4a741432013-02-23 15:35:38 +0900214 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200215 return;
216
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200217 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900218 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
219 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200220 } else
Simon Horman4a741432013-02-23 15:35:38 +0900221 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200222
Simon Horman4a741432013-02-23 15:35:38 +0900223 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200224 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900225 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200226
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100227 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900228 check->duration = -1;
229 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200230 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900231 check->duration = tv_ms_elapsed(&check->start, &now);
232 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200233 }
234
Simon Horman2f1f9552013-11-25 10:46:37 +0900235 /* Failure to connect to the agent as a secondary check should not
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200236 * cause the server to be marked down.
237 */
Willy Tarreau33434322013-12-11 21:15:19 +0100238 if ((check->state & CHK_ST_AGENT) && check->status < HCHK_STATUS_L7TOUT)
Simon Horman2f1f9552013-11-25 10:46:37 +0900239 return;
240
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200241 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200242
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200243 switch (check->result) {
244 case CHK_RES_FAILED:
245 if (check->health >= check->rise) {
246 s->counters.failed_checks++;
247 report = 1;
248 check->health--;
249 if (check->health < check->rise)
250 check->health = 0;
251 }
252 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200253
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200254 case CHK_RES_PASSED:
255 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
256 if ((check->health < check->rise + check->fall - 1) &&
257 (check->result == CHK_RES_PASSED || check->health > 0)) {
258 report = 1;
259 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200260
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200261 if (check->health >= check->rise)
262 check->health = check->rise + check->fall - 1; /* OK now */
263 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200264
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200265 /* clear consecutive_errors if observing is enabled */
266 if (s->onerror)
267 s->consecutive_errors = 0;
268 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100269
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200270 default:
271 break;
272 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200273
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200274 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
275 (status != prev_status || report)) {
276 chunk_printf(&trash,
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100277 "Health check for %sserver %s/%s %s%s",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200278 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100279 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100280 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200281 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200282
Willy Tarreaua150cf12014-05-20 21:57:23 +0200283 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200284
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100285 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200286 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
287 (check->health >= check->rise) ? check->fall : check->rise,
288 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200289
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100290 Warning("%s.\n", trash.str);
291 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200292 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200293}
294
Willy Tarreau4eec5472014-05-20 22:32:27 +0200295/* Marks the check <check>'s server down if the current check is already failed
296 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200298static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299{
Simon Horman4a741432013-02-23 15:35:38 +0900300 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900301
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200302 /* The agent secondary check should only cause a server to be marked
303 * as down if check->status is HCHK_STATUS_L7STS, which indicates
304 * that the agent returned "fail", "stopped" or "down".
305 * The implication here is that failure to connect to the agent
306 * as a secondary check should not cause the server to be marked
307 * down. */
308 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
309 return;
310
Willy Tarreau4eec5472014-05-20 22:32:27 +0200311 if (check->health > 0)
312 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100313
Willy Tarreau4eec5472014-05-20 22:32:27 +0200314 /* We only report a reason for the check if we did not do so previously */
315 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316}
317
Willy Tarreauaf549582014-05-16 17:37:50 +0200318/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200319 * it isn't in maintenance, it is not tracking a down server and other checks
320 * comply. The rule is simple : by default, a server is up, unless any of the
321 * following conditions is true :
322 * - health check failed (check->health < rise)
323 * - agent check failed (agent->health < rise)
324 * - the server tracks a down server (track && track->state == STOPPED)
325 * Note that if the server has a slowstart, it will switch to STARTING instead
326 * of RUNNING. Also, only the health checks support the nolb mode, so the
327 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200328 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200329static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200330{
Simon Horman4a741432013-02-23 15:35:38 +0900331 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100332
Willy Tarreauaf549582014-05-16 17:37:50 +0200333 if (s->admin & SRV_ADMF_MAINT)
334 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100335
Willy Tarreau3e048382014-05-21 10:30:54 +0200336 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200337 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100338
Willy Tarreau3e048382014-05-21 10:30:54 +0200339 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
340 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100341
Willy Tarreau3e048382014-05-21 10:30:54 +0200342 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
343 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200344
Willy Tarreau3e048382014-05-21 10:30:54 +0200345 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
346 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100347
Willy Tarreau3e048382014-05-21 10:30:54 +0200348 srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100349}
350
Willy Tarreauaf549582014-05-16 17:37:50 +0200351/* Marks the check <check> as valid and tries to set its server into drain mode,
352 * provided it isn't in maintenance and other checks comply. Notifies by all
353 * available means, recounts the remaining servers on the proxy and tries to
354 * grab requests from the proxy. It automatically recomputes the number of
355 * servers, but not the map. Maintenance servers are ignored. Those that were
356 * not in perfect health are simply refreshed.
357 */
358static void check_set_server_drain(struct check *check)
359{
Simon Horman4a741432013-02-23 15:35:38 +0900360 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100361 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100362 int xferred;
363
Willy Tarreauaf549582014-05-16 17:37:50 +0200364 if (s->admin & SRV_ADMF_MAINT)
365 return;
366
Willy Tarreau892337c2014-05-13 23:41:20 +0200367 s->state = SRV_ST_STOPPING;
Willy Tarreau9580d162012-05-19 19:07:40 +0200368 if (s->proxy->lbprm.set_server_status_down)
369 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100370
371 /* we might have sessions queued on this server and waiting for
372 * a connection. Those which are redispatchable will be queued
373 * to another server or to the proxy itself.
374 */
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200375 xferred = pendconn_redistribute(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100376
Willy Tarreauaf549582014-05-16 17:37:50 +0200377 chunk_printf(&trash,
378 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100379 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100380
Willy Tarreaua150cf12014-05-20 21:57:23 +0200381 srv_append_status(&trash, s,
382 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL),
383 xferred, 0);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100384
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100385 Warning("%s.\n", trash.str);
386 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100387
388 if (!s->proxy->srv_bck && !s->proxy->srv_act)
389 set_backend_down(s->proxy);
390
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100391 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreauaf549582014-05-16 17:37:50 +0200392 check_set_server_drain(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100393
Willy Tarreauaf549582014-05-16 17:37:50 +0200394 if (check->health >= check->rise)
395 check->health = check->rise + check->fall - 1; /* OK now */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100396}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100398/* note: use health_adjust() only, which first checks that the observe mode is
399 * enabled.
400 */
401void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100402{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100403 int failed;
404 int expire;
405
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100406 if (s->observe >= HANA_OBS_SIZE)
407 return;
408
Willy Tarreaubb956662013-01-24 00:37:39 +0100409 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100410 return;
411
412 switch (analyze_statuses[status].lr[s->observe - 1]) {
413 case 1:
414 failed = 1;
415 break;
416
417 case 2:
418 failed = 0;
419 break;
420
421 default:
422 return;
423 }
424
425 if (!failed) {
426 /* good: clear consecutive_errors */
427 s->consecutive_errors = 0;
428 return;
429 }
430
431 s->consecutive_errors++;
432
433 if (s->consecutive_errors < s->consecutive_errors_limit)
434 return;
435
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100436 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
437 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100438
439 switch (s->onerror) {
440 case HANA_ONERR_FASTINTER:
441 /* force fastinter - nothing to do here as all modes force it */
442 break;
443
444 case HANA_ONERR_SUDDTH:
445 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900446 if (s->check.health > s->check.rise)
447 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100448
449 /* no break - fall through */
450
451 case HANA_ONERR_FAILCHK:
452 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900453 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200454 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100455 break;
456
457 case HANA_ONERR_MARKDWN:
458 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900459 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900460 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200461 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100462 break;
463
464 default:
465 /* write a warning? */
466 break;
467 }
468
469 s->consecutive_errors = 0;
470 s->counters.failed_hana++;
471
Simon Horman66183002013-02-23 10:16:43 +0900472 if (s->check.fastinter) {
473 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300474 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200475 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300476 /* requeue check task with new expire */
477 task_queue(s->check.task);
478 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100479 }
480}
481
Willy Tarreaua1dab552014-04-14 15:04:54 +0200482static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100483{
484 int sv_state;
485 int ratio;
486 int hlen = 0;
487 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
488 "UP %d/%d", "UP",
489 "NOLB %d/%d", "NOLB",
490 "no check" };
491
492 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
493 hlen += 24;
494
Willy Tarreauff5ae352013-12-11 20:36:34 +0100495 if (!(s->check.state & CHK_ST_ENABLED))
496 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200497 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900498 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100499 sv_state = 3; /* UP */
500 else
501 sv_state = 2; /* going down */
502
Willy Tarreau892337c2014-05-13 23:41:20 +0200503 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100504 sv_state += 2;
505 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900506 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100507 sv_state = 1; /* going up */
508 else
509 sv_state = 0; /* DOWN */
510 }
511
Willy Tarreaua1dab552014-04-14 15:04:54 +0200512 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100513 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200514 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
515 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100516
Willy Tarreaua1dab552014-04-14 15:04:54 +0200517 hlen += snprintf(buffer + hlen, size - hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
Willy Tarreauef781042010-01-27 11:53:01 +0100518 s->proxy->id, s->id,
519 global.node,
520 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
521 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
522 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
523 s->nbpend);
524
Willy Tarreau892337c2014-05-13 23:41:20 +0200525 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100526 now.tv_sec < s->last_change + s->slowstart &&
527 now.tv_sec >= s->last_change) {
528 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200529 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100530 }
531
532 buffer[hlen++] = '\r';
533 buffer[hlen++] = '\n';
534
535 return hlen;
536}
537
Willy Tarreau20a18342013-12-05 00:31:46 +0100538/* Check the connection. If an error has already been reported or the socket is
539 * closed, keep errno intact as it is supposed to contain the valid error code.
540 * If no error is reported, check the socket's error queue using getsockopt().
541 * Warning, this must be done only once when returning from poll, and never
542 * after an I/O error was attempted, otherwise the error queue might contain
543 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
544 * socket. Returns non-zero if an error was reported, zero if everything is
545 * clean (including a properly closed socket).
546 */
547static int retrieve_errno_from_socket(struct connection *conn)
548{
549 int skerr;
550 socklen_t lskerr = sizeof(skerr);
551
552 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
553 return 1;
554
Willy Tarreau3c728722014-01-23 13:50:42 +0100555 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100556 return 0;
557
558 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
559 errno = skerr;
560
561 if (errno == EAGAIN)
562 errno = 0;
563
564 if (!errno) {
565 /* we could not retrieve an error, that does not mean there is
566 * none. Just don't change anything and only report the prior
567 * error if any.
568 */
569 if (conn->flags & CO_FL_ERROR)
570 return 1;
571 else
572 return 0;
573 }
574
575 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
576 return 1;
577}
578
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100579/* Try to collect as much information as possible on the connection status,
580 * and adjust the server status accordingly. It may make use of <errno_bck>
581 * if non-null when the caller is absolutely certain of its validity (eg:
582 * checked just after a syscall). If the caller doesn't have a valid errno,
583 * it can pass zero, and retrieve_errno_from_socket() will be called to try
584 * to extract errno from the socket. If no error is reported, it will consider
585 * the <expired> flag. This is intended to be used when a connection error was
586 * reported in conn->flags or when a timeout was reported in <expired>. The
587 * function takes care of not updating a server status which was already set.
588 * All situations where at least one of <expired> or CO_FL_ERROR are set
589 * produce a status.
590 */
591static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
592{
593 struct check *check = conn->owner;
594 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200595 struct chunk *chk;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100596
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100597 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100598 return;
599
600 errno = errno_bck;
601 if (!errno || errno == EAGAIN)
602 retrieve_errno_from_socket(conn);
603
604 if (!(conn->flags & CO_FL_ERROR) && !expired)
605 return;
606
607 /* we'll try to build a meaningful error message depending on the
608 * context of the error possibly present in conn->err_code, and the
609 * socket error possibly collected above. This is useful to know the
610 * exact step of the L6 layer (eg: SSL handshake).
611 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200612 chk = get_trash_chunk();
613
614 if (check->type == PR_O2_TCPCHK_CHK) {
615 chunk_printf(chk, " at step %d of tcp-check", tcpcheck_get_step_id(check->server));
616 /* we were looking for a string */
Baptiste Assmann69e273f2013-12-11 00:52:19 +0100617 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
618 chunk_appendf(chk, " (connect)");
619 }
620 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200621 if (check->current_step->string)
622 chunk_appendf(chk, " (string '%s')", check->current_step->string);
623 else if (check->current_step->expect_regex)
624 chunk_appendf(chk, " (expect regex)");
625 }
626 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
627 chunk_appendf(chk, " (send)");
628 }
629 }
630
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100631 if (conn->err_code) {
632 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200633 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100634 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200635 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100636 err_msg = trash.str;
637 }
638 else {
639 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200640 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100641 err_msg = trash.str;
642 }
643 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200644 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100645 }
646 }
647
648 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
649 /* L4 not established (yet) */
650 if (conn->flags & CO_FL_ERROR)
651 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
652 else if (expired)
653 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
654 }
655 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
656 /* L6 not established (yet) */
657 if (conn->flags & CO_FL_ERROR)
658 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
659 else if (expired)
660 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
661 }
662 else if (conn->flags & CO_FL_ERROR) {
663 /* I/O error after connection was established and before we could diagnose */
664 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
665 }
666 else if (expired) {
667 /* connection established but expired check */
668 if (check->type == PR_O2_SSL3_CHK)
669 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
670 else /* HTTP, SMTP, ... */
671 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
672 }
673
674 return;
675}
676
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677/*
678 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200679 * the connection acknowledgement. If the proxy requires L7 health-checks,
680 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900681 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200683static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684{
Simon Horman4a741432013-02-23 15:35:38 +0900685 struct check *check = conn->owner;
686 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900687 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200688
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100689 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100690 goto out_wakeup;
691
Willy Tarreau310987a2014-01-22 19:46:33 +0100692 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100693 return;
694
Willy Tarreau20a18342013-12-05 00:31:46 +0100695 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100696 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100697 __conn_data_stop_both(conn);
698 goto out_wakeup;
699 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100700
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100701 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100702 /* if the output is closed, we can't do anything */
703 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100704 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100705 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200706 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200707
Willy Tarreau06559ac2013-12-05 01:53:08 +0100708 /* here, we know that the connection is established. That's enough for
709 * a pure TCP check.
710 */
711 if (!check->type)
712 goto out_wakeup;
713
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200714 if (check->type == PR_O2_TCPCHK_CHK) {
715 tcpcheck_main(conn);
716 return;
717 }
718
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100719 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100720 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100721 if (conn->flags & CO_FL_ERROR) {
722 chk_report_conn_err(conn, errno, 0);
723 __conn_data_stop_both(conn);
724 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100726 if (check->bo->o)
727 return;
728 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200729
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100730 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
731 if (s->proxy->timeout.check) {
732 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
733 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100735 goto out_nowake;
736
Willy Tarreau83749182007-04-15 20:56:27 +0200737 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200738 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200739 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200740 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741}
742
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200744 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200745 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900746 * set_server_check_status() to update check->status, check->duration
747 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200748
749 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
750 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
751 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
752 * response to an SSL HELLO (the principle is that this is enough to
753 * distinguish between an SSL server and a pure TCP relay). All other cases will
754 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
755 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200757static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758{
Simon Horman4a741432013-02-23 15:35:38 +0900759 struct check *check = conn->owner;
760 struct server *s = check->server;
761 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200762 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100763 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200764 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200765
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100766 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200767 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200768
Willy Tarreau310987a2014-01-22 19:46:33 +0100769 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200770 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200771
772 if (check->type == PR_O2_TCPCHK_CHK) {
773 tcpcheck_main(conn);
774 return;
775 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200776
Willy Tarreau83749182007-04-15 20:56:27 +0200777 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
778 * but the connection was closed on the remote end. Fortunately, recv still
779 * works correctly and we don't need to do the getsockopt() on linux.
780 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000781
782 /* Set buffer to point to the end of the data already read, and check
783 * that there is free space remaining. If the buffer is full, proceed
784 * with running the checks without attempting another socket read.
785 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000786
Willy Tarreau03938182010-03-17 21:52:07 +0100787 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000788
Simon Horman4a741432013-02-23 15:35:38 +0900789 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200790 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100791 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900792 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200793 /* Report network errors only if we got no other data. Otherwise
794 * we'll let the upper layers decide whether the response is OK
795 * or not. It is very common that an RST sent by the server is
796 * reported as an error just after the last data chunk.
797 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100798 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100799 goto out_wakeup;
800 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 }
802
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100803
Willy Tarreau03938182010-03-17 21:52:07 +0100804 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900805 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100806 */
Simon Horman4a741432013-02-23 15:35:38 +0900807 if (check->bi->i < check->bi->size)
808 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100809 else {
Simon Horman4a741432013-02-23 15:35:38 +0900810 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100811 done = 1; /* buffer full, don't wait for more data */
812 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200813
Nick Chalk57b1bf72010-03-16 15:50:46 +0000814 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900815 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200816 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900817 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100818 goto wait_more_data;
819
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100820 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900821 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
822 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
823 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
824 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
825 !isdigit((unsigned char) *(check->bi->data + 11))) {
826 cut_crlf(check->bi->data);
827 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200828
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100829 goto out_wakeup;
830 }
831
Simon Horman4a741432013-02-23 15:35:38 +0900832 check->code = str2uic(check->bi->data + 9);
833 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000834
Willy Tarreaubd741542010-03-16 18:46:54 +0100835 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200836 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000837 /* 404 may be accepted as "stopping" only if the server was up */
838 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900839 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000840 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100841 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
842 /* Run content verification check... We know we have at least 13 chars */
843 if (!httpchk_expect(s, done))
844 goto wait_more_data;
845 }
846 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900847 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100848 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900849 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100850 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000851 else {
852 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900853 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000854 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200855 break;
856
857 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900858 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100859 goto wait_more_data;
860
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100861 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900862 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
863 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200864 else
Simon Horman4a741432013-02-23 15:35:38 +0900865 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200866 break;
867
868 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900869 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100870 goto wait_more_data;
871
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200872 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900873 if ((check->bi->i < strlen("000\r")) ||
874 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
875 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
876 !isdigit((unsigned char) *(check->bi->data + 2))) {
877 cut_crlf(check->bi->data);
878 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200879
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200880 goto out_wakeup;
881 }
882
Simon Horman4a741432013-02-23 15:35:38 +0900883 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200884
Simon Horman4a741432013-02-23 15:35:38 +0900885 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200886 cut_crlf(desc);
887
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100888 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900889 if (*check->bi->data == '2')
890 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200891 else
Simon Horman4a741432013-02-23 15:35:38 +0900892 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200893 break;
894
Simon Hormana2b9dad2013-02-12 10:45:54 +0900895 case PR_O2_LB_AGENT_CHK: {
896 short status = HCHK_STATUS_L7RSP;
897 const char *desc = "Unknown feedback string";
898 const char *down_cmd = NULL;
Simon Horman671b6f02013-11-25 10:46:39 +0900899 int disabled;
Willy Tarreau9809b782013-12-11 21:40:11 +0100900 char *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900901
Willy Tarreau9809b782013-12-11 21:40:11 +0100902 /* get a complete line first */
903 p = check->bi->data;
904 while (*p && *p != '\n' && *p != '\r')
905 p++;
906
907 if (!*p) {
908 if (!done)
909 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900910
Willy Tarreau9809b782013-12-11 21:40:11 +0100911 /* at least inform the admin that the agent is mis-behaving */
912 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
913 break;
914 }
915 *p = 0;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900916
Simon Horman671b6f02013-11-25 10:46:39 +0900917 /*
918 * The agent may have been disabled after a check was
919 * initialised. If so, ignore weight changes and drain
920 * settings from the agent. Note that the setting is
921 * always present in the state of the agent the server,
922 * regardless of if the agent is being run as a primary or
923 * secondary check. That is, regardless of if the check
924 * parameter of this function is the agent or check field
925 * of the server.
926 */
Willy Tarreau2e10f5a2013-12-11 20:11:55 +0100927 disabled = !(check->server->agent.state & CHK_ST_ENABLED);
Simon Horman671b6f02013-11-25 10:46:39 +0900928
Simon Horman4a741432013-02-23 15:35:38 +0900929 if (strchr(check->bi->data, '%')) {
Simon Horman671b6f02013-11-25 10:46:39 +0900930 if (disabled)
931 break;
Simon Horman4a741432013-02-23 15:35:38 +0900932 desc = server_parse_weight_change_request(s, check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +0900933 if (!desc) {
934 status = HCHK_STATUS_L7OKD;
Simon Horman4a741432013-02-23 15:35:38 +0900935 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900936 }
Simon Horman4a741432013-02-23 15:35:38 +0900937 } else if (!strcasecmp(check->bi->data, "drain")) {
Simon Horman671b6f02013-11-25 10:46:39 +0900938 if (disabled)
939 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900940 desc = server_parse_weight_change_request(s, "0%");
941 if (!desc) {
942 desc = "drain";
943 status = HCHK_STATUS_L7OKD;
944 }
Simon Horman4a741432013-02-23 15:35:38 +0900945 } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +0900946 down_cmd = "down";
Simon Horman4a741432013-02-23 15:35:38 +0900947 } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +0900948 down_cmd = "stopped";
Simon Horman4a741432013-02-23 15:35:38 +0900949 } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +0900950 down_cmd = "fail";
951 }
952
953 if (down_cmd) {
Simon Horman4a741432013-02-23 15:35:38 +0900954 const char *end = check->bi->data + strlen(down_cmd);
Simon Hormana2b9dad2013-02-12 10:45:54 +0900955 /*
956 * The command keyword must terminated the string or
957 * be followed by a blank.
958 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +0100959 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +0900960 status = HCHK_STATUS_L7STS;
Simon Horman80fefae2013-11-25 10:46:34 +0900961 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900962 }
963 }
964
Simon Horman4a741432013-02-23 15:35:38 +0900965 set_server_check_status(check, status, desc);
Simon Hormana2b9dad2013-02-12 10:45:54 +0900966 break;
967 }
968
Willy Tarreau1620ec32011-08-06 17:05:02 +0200969 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900970 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +0100971 goto wait_more_data;
972
Simon Horman4a741432013-02-23 15:35:38 +0900973 if (check->bi->data[0] == 'R') {
974 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +0100975 }
976 else {
Simon Horman4a741432013-02-23 15:35:38 +0900977 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
978 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +0100979 else
980 desc = "PostgreSQL unknown error";
981
Simon Horman4a741432013-02-23 15:35:38 +0900982 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +0100983 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200984 break;
985
986 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900987 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +0200988 goto wait_more_data;
989
Simon Horman4a741432013-02-23 15:35:38 +0900990 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
991 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +0200992 }
993 else {
Simon Horman4a741432013-02-23 15:35:38 +0900994 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +0200995 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200996 break;
997
998 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900999 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001000 goto wait_more_data;
1001
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001002 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001003 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001004 /* We set the MySQL Version in description for information purpose
1005 * FIXME : it can be cool to use MySQL Version for other purpose,
1006 * like mark as down old MySQL server.
1007 */
Simon Horman4a741432013-02-23 15:35:38 +09001008 if (check->bi->i > 51) {
1009 desc = ltrim(check->bi->data + 5, ' ');
1010 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001011 }
1012 else {
1013 if (!done)
1014 goto wait_more_data;
1015 /* it seems we have a OK packet but without a valid length,
1016 * it must be a protocol error
1017 */
Simon Horman4a741432013-02-23 15:35:38 +09001018 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001019 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001020 }
1021 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001022 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001023 desc = ltrim(check->bi->data + 7, ' ');
1024 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001025 }
1026 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001027 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1028 (((unsigned int) *(check->bi->data + 1)) << 8) +
1029 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001030
Simon Horman4a741432013-02-23 15:35:38 +09001031 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001032 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001033 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001034 /* We have only one MySQL packet and it is a Handshake Initialization packet
1035 * but we need to have a second packet to know if it is alright
1036 */
Simon Horman4a741432013-02-23 15:35:38 +09001037 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001038 goto wait_more_data;
1039 }
1040 else {
1041 /* We have only one packet and it is an Error packet,
1042 * an error message is attached, so we can display it
1043 */
Simon Horman4a741432013-02-23 15:35:38 +09001044 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001045 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001046 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001047 }
Simon Horman4a741432013-02-23 15:35:38 +09001048 } else if (check->bi->i > first_packet_len + 4) {
1049 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1050 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1051 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001052
Simon Horman4a741432013-02-23 15:35:38 +09001053 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001054 /* We have 2 packets and that's good */
1055 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001056 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001057 /* No error packet */
1058 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001059 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001060 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001061 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001062 }
1063 else {
1064 /* An error message is attached in the Error packet
1065 * so we can display it ! :)
1066 */
Simon Horman4a741432013-02-23 15:35:38 +09001067 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001068 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001069 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001070 }
1071 }
1072 }
1073 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001074 if (!done)
1075 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001076 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001077 * it must be a protocol error
1078 */
Simon Horman4a741432013-02-23 15:35:38 +09001079 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001080 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001081 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001082 }
1083 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001084 break;
1085
1086 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001087 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001088 goto wait_more_data;
1089
1090 /* Check if the server speaks LDAP (ASN.1/BER)
1091 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1092 * http://tools.ietf.org/html/rfc4511
1093 */
1094
1095 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1096 * LDAPMessage: 0x30: SEQUENCE
1097 */
Simon Horman4a741432013-02-23 15:35:38 +09001098 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1099 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001100 }
1101 else {
1102 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001103 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001104
1105 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1106 * messageID: 0x02 0x01 0x01: INTEGER 1
1107 * protocolOp: 0x61: bindResponse
1108 */
1109 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001110 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1111 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001112
1113 goto out_wakeup;
1114 }
1115
1116 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001117 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001118
1119 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1120 * ldapResult: 0x0a 0x01: ENUMERATION
1121 */
1122 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001123 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1124 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001125
1126 goto out_wakeup;
1127 }
1128
1129 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1130 * resultCode
1131 */
Simon Horman4a741432013-02-23 15:35:38 +09001132 check->code = *(check->bi->data + msglen + 9);
1133 if (check->code) {
1134 set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001135 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001136 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001137 }
1138 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001139 break;
1140
1141 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001142 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001143 break;
1144 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001145
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001146 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001147 /* collect possible new errors */
1148 if (conn->flags & CO_FL_ERROR)
1149 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150
Nick Chalk57b1bf72010-03-16 15:50:46 +00001151 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001152 *check->bi->data = '\0';
1153 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001154
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001155 /* Close the connection... We absolutely want to perform a hard close
1156 * and reset the connection if some data are pending, otherwise we end
1157 * up with many TIME_WAITs and eat all the source port range quickly.
1158 * To avoid sending RSTs all the time, we first try to drain pending
1159 * data.
1160 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001161 if (conn->xprt && conn->xprt->shutw)
1162 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001163
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001164 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001165 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001166 conn->flags |= CO_FL_ERROR;
1167
Willy Tarreaua522f802012-11-23 08:56:35 +01001168 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001169 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001170 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001171
1172 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001173 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001174}
1175
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001176/*
1177 * This function is used only for server health-checks. It handles connection
1178 * status updates including errors. If necessary, it wakes the check task up.
1179 * It always returns 0.
1180 */
1181static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001182{
Simon Horman4a741432013-02-23 15:35:38 +09001183 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001184
Willy Tarreau6c560da2012-11-24 11:14:45 +01001185 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001186 /* We may get error reports bypassing the I/O handlers, typically
1187 * the case when sending a pure TCP check which fails, then the I/O
1188 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001189 * main processing task so let's simply wake it up. If we get here,
1190 * we expect errno to still be valid.
1191 */
1192 chk_report_conn_err(conn, errno, 0);
1193
Willy Tarreau2d351b62013-12-05 02:36:25 +01001194 __conn_data_stop_both(conn);
1195 task_wakeup(check->task, TASK_WOKEN_IO);
1196 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001197 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1198 /* we may get here if only a connection probe was required : we
1199 * don't have any data to send nor anything expected in response,
1200 * so the completion of the connection establishment is enough.
1201 */
1202 task_wakeup(check->task, TASK_WOKEN_IO);
1203 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001204
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001205 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001206 /* We're here because nobody wants to handle the error, so we
1207 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001208 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001209 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001210 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001211 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001212 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001213}
1214
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001215struct data_cb check_conn_cb = {
1216 .recv = event_srv_chk_r,
1217 .send = event_srv_chk_w,
1218 .wake = wake_srv_chk,
1219};
1220
Willy Tarreaubaaee002006-06-26 02:48:02 +02001221/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001222 * updates the server's weight during a warmup stage. Once the final weight is
1223 * reached, the task automatically stops. Note that any server status change
1224 * must have updated s->last_change accordingly.
1225 */
1226static struct task *server_warmup(struct task *t)
1227{
1228 struct server *s = t->context;
1229
1230 /* by default, plan on stopping the task */
1231 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001232 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001233 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001234 return t;
1235
Willy Tarreau892337c2014-05-13 23:41:20 +02001236 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001237 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001238
1239 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001240 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001241
1242 /* get back there in 1 second or 1/20th of the slowstart interval,
1243 * whichever is greater, resulting in small 5% steps.
1244 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001245 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001246 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1247 return t;
1248}
1249
1250/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001251 * manages a server health-check. Returns
1252 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1253 */
Simon Horman63a4a822012-03-19 07:24:41 +09001254static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001255{
Simon Horman4a741432013-02-23 15:35:38 +09001256 struct check *check = t->context;
1257 struct server *s = check->server;
1258 struct connection *conn = check->conn;
Willy Tarreau640556c2014-05-09 23:38:15 +02001259 struct protocol *proto;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001260 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001261 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001262 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263
Willy Tarreau2c115e52013-12-11 19:41:16 +01001264 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001265 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001266 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001267 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268
Simon Horman671b6f02013-11-25 10:46:39 +09001269 /* we don't send any health-checks when the proxy is
1270 * stopped, the server should not be checked or the check
1271 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001272 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001273 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001274 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001275 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001276
1277 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001278 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001279
Willy Tarreau2c115e52013-12-11 19:41:16 +01001280 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001281 check->bi->p = check->bi->data;
1282 check->bi->i = 0;
1283 check->bo->p = check->bo->data;
1284 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001285
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001286 /* tcpcheck send/expect initialisation */
1287 if (check->type == PR_O2_TCPCHK_CHK)
1288 check->current_step = NULL;
1289
1290 /* prepare the check buffer.
1291 * This should not be used if check is the secondary agent check
1292 * of a server as s->proxy->check_req will relate to the
1293 * configuration of the primary check. Similarly, tcp-check uses
1294 * its own strings.
1295 */
Willy Tarreau33434322013-12-11 21:15:19 +01001296 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
Simon Horman4a741432013-02-23 15:35:38 +09001297 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001298
1299 /* we want to check if this host replies to HTTP or SSLv3 requests
1300 * so we'll send the request, and won't wake the checker up now.
1301 */
Simon Horman4a741432013-02-23 15:35:38 +09001302 if ((check->type) == PR_O2_SSL3_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001303 /* SSL requires that we put Unix time in the request */
1304 int gmt_time = htonl(date.tv_sec);
Simon Horman4a741432013-02-23 15:35:38 +09001305 memcpy(check->bo->data + 11, &gmt_time, 4);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001306 }
Simon Horman4a741432013-02-23 15:35:38 +09001307 else if ((check->type) == PR_O2_HTTP_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001308 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Willy Tarreaua1dab552014-04-14 15:04:54 +02001309 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Simon Horman4a741432013-02-23 15:35:38 +09001310 bo_putstr(check->bo, "\r\n");
1311 *check->bo->p = '\0'; /* to make gdb output easier to read */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001312 }
1313 }
1314
1315 /* prepare a new connection */
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001316 conn_init(conn);
Willy Tarreau910c6aa2013-10-24 15:08:37 +02001317 conn_prepare(conn, s->check_common.proto, s->check_common.xprt);
Willy Tarreau7abddb52013-10-24 15:31:04 +02001318 conn_attach(conn, check, &check_conn_cb);
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001319 conn->target = &s->obj_type;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001320
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001321 /* no client address */
1322 clear_addr(&conn->addr.from);
1323
Willy Tarreau640556c2014-05-09 23:38:15 +02001324 if (is_addr(&s->check_common.addr)) {
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001325 /* we'll connect to the check addr specified on the server */
Simon Horman66183002013-02-23 10:16:43 +09001326 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02001327 proto = s->check_common.proto;
1328 }
1329 else {
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001330 /* we'll connect to the addr on the server */
1331 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02001332 proto = s->proto;
1333 }
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001334
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001335 if (check->port) {
1336 set_host_port(&conn->addr.to, check->port);
1337 }
1338
1339 if (check->type == PR_O2_TCPCHK_CHK) {
Baptiste Assmannf621bea2014-02-03 22:38:15 +01001340 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1341 /* if first step is a 'connect', then tcpcheck_main must run it */
1342 if (r->action == TCPCHK_ACT_CONNECT) {
1343 tcpcheck_main(conn);
1344 return t;
1345 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001346 }
1347
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001348
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001349 /* It can return one of :
1350 * - SN_ERR_NONE if everything's OK
1351 * - SN_ERR_SRVTO if there are no more servers
1352 * - SN_ERR_SRVCL if the connection was refused by the server
1353 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1354 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1355 * - SN_ERR_INTERNAL for any other purely internal errors
1356 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001357 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001358 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001359 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001360 ret = SN_ERR_INTERNAL;
Willy Tarreau640556c2014-05-09 23:38:15 +02001361 if (proto->connect)
1362 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001363 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau57cd3e42013-10-24 22:01:26 +02001364 if (s->check.send_proxy) {
1365 conn->send_proxy_ofs = 1;
1366 conn->flags |= CO_FL_SEND_PROXY;
1367 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001368
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001369 switch (ret) {
1370 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001371 /* we allow up to min(inter, timeout.connect) for a connection
1372 * to establish but only when timeout.check is set
1373 * as it may be to short for a full check otherwise
1374 */
Simon Horman4a741432013-02-23 15:35:38 +09001375 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001376
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001377 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1378 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1379 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001380 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001381
1382 if (check->type)
1383 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1384
Willy Tarreau5a78f362012-11-23 12:47:05 +01001385 goto reschedule;
1386
1387 case SN_ERR_SRVTO: /* ETIMEDOUT */
1388 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001389 conn->flags |= CO_FL_ERROR;
1390 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001391 break;
1392 case SN_ERR_PRXCOND:
1393 case SN_ERR_RESOURCE:
1394 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001395 conn->flags |= CO_FL_ERROR;
1396 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001397 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001398 }
1399
Willy Tarreau5a78f362012-11-23 12:47:05 +01001400 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001401
Willy Tarreau2c115e52013-12-11 19:41:16 +01001402 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02001403 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001405 /* we allow up to min(inter, timeout.connect) for a connection
1406 * to establish but only when timeout.check is set
1407 * as it may be to short for a full check otherwise
1408 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001409 while (tick_is_expired(t->expire, now_ms)) {
1410 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001411
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001412 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001413 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001414
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001415 if (s->proxy->timeout.check)
1416 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001417 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001418 }
1419 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001420 /* there was a test running.
1421 * First, let's check whether there was an uncaught error,
1422 * which can happen on connect timeout or error.
1423 */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001424 if (s->check.result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001425 /* good connection is enough for pure TCP check */
1426 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001427 if (check->use_ssl)
1428 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001429 else
Simon Horman4a741432013-02-23 15:35:38 +09001430 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001431 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001432 else if ((conn->flags & CO_FL_ERROR) || expired) {
1433 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001434 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001435 else
1436 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001437 }
1438
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001439 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001440 if (conn->xprt) {
1441 /* The check was aborted and the connection was not yet closed.
1442 * This can happen upon timeout, or when an external event such
1443 * as a failed response coupled with "observe layer7" caused the
1444 * server state to be suddenly changed.
1445 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001446 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001447 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001448 }
1449
Willy Tarreauaf549582014-05-16 17:37:50 +02001450 if (check->result == CHK_RES_FAILED) {
1451 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02001452 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02001453 }
1454 else if (check->result == CHK_RES_CONDPASS && s->state != SRV_ST_STOPPED && !(s->admin & SRV_ADMF_MAINT)) {
1455 /* check is OK but asks for drain mode */
Willy Tarreau7b1d47c2014-05-20 14:55:13 +02001456 if (check->health >= check->rise && check->server->state != SRV_ST_STOPPING)
Willy Tarreauaf549582014-05-16 17:37:50 +02001457 check_set_server_drain(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02001458 }
Willy Tarreau3e048382014-05-21 10:30:54 +02001459 else if (check->result == CHK_RES_PASSED) {
1460 /* a success was detected */
1461 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001462 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01001463 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001464
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001465 rv = 0;
1466 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001467 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001468 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001469 }
Simon Horman4a741432013-02-23 15:35:38 +09001470 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001471 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001472
1473 reschedule:
1474 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09001475 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001476 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001477 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001478}
1479
Simon Horman5c942422013-11-25 10:46:32 +09001480static int start_check_task(struct check *check, int mininter,
1481 int nbcheck, int srvpos)
1482{
1483 struct task *t;
1484 /* task for the check */
1485 if ((t = task_new()) == NULL) {
1486 Alert("Starting [%s:%s] check: out of memory.\n",
1487 check->server->proxy->id, check->server->id);
1488 return 0;
1489 }
1490
1491 check->task = t;
1492 t->process = process_chk;
1493 t->context = check;
1494
Willy Tarreau1746eec2014-04-25 10:46:47 +02001495 if (mininter < srv_getinter(check))
1496 mininter = srv_getinter(check);
1497
1498 if (global.max_spread_checks && mininter > global.max_spread_checks)
1499 mininter = global.max_spread_checks;
1500
Simon Horman5c942422013-11-25 10:46:32 +09001501 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02001502 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09001503 check->start = now;
1504 task_queue(t);
1505
1506 return 1;
1507}
1508
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001509/*
1510 * Start health-check.
1511 * Returns 0 if OK, -1 if error, and prints the error in this case.
1512 */
1513int start_checks() {
1514
1515 struct proxy *px;
1516 struct server *s;
1517 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09001518 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001519
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001520 /* 1- count the checkers to run simultaneously.
1521 * We also determine the minimum interval among all of those which
1522 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1523 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001524 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001525 * too short an interval for all others.
1526 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001527 for (px = proxy; px; px = px->next) {
1528 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01001529 if (s->slowstart) {
1530 if ((t = task_new()) == NULL) {
1531 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1532 return -1;
1533 }
1534 /* We need a warmup task that will be called when the server
1535 * state switches from down to up.
1536 */
1537 s->warmup = t;
1538 t->process = server_warmup;
1539 t->context = s;
1540 t->expire = TICK_ETERNITY;
1541 }
1542
Willy Tarreaud8514a22013-12-11 21:10:14 +01001543 if (s->check.state & CHK_ST_CONFIGURED) {
1544 nbcheck++;
1545 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1546 (!mininter || mininter > srv_getinter(&s->check)))
1547 mininter = srv_getinter(&s->check);
1548 }
Willy Tarreau15f39102013-12-11 20:41:18 +01001549
Willy Tarreaud8514a22013-12-11 21:10:14 +01001550 if (s->agent.state & CHK_ST_CONFIGURED) {
1551 nbcheck++;
1552 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1553 (!mininter || mininter > srv_getinter(&s->agent)))
1554 mininter = srv_getinter(&s->agent);
1555 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001556 }
1557 }
1558
Simon Horman4a741432013-02-23 15:35:38 +09001559 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001560 return 0;
1561
1562 srand((unsigned)time(NULL));
1563
1564 /*
1565 * 2- start them as far as possible from each others. For this, we will
1566 * start them after their interval set to the min interval divided by
1567 * the number of servers, weighted by the server's position in the list.
1568 */
1569 for (px = proxy; px; px = px->next) {
1570 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09001571 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01001572 if (s->check.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09001573 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1574 return -1;
1575 srvpos++;
1576 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001577
Simon Hormand60d6912013-11-25 10:46:36 +09001578 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01001579 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09001580 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1581 return -1;
1582 }
1583 srvpos++;
1584 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001585 }
1586 }
1587 return 0;
1588}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001589
1590/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001591 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001592 * The buffer MUST be terminated by a null byte before calling this function.
1593 * Sets server status appropriately. The caller is responsible for ensuring
1594 * that the buffer contains at least 13 characters. If <done> is zero, we may
1595 * return 0 to indicate that data is required to decide of a match.
1596 */
1597static int httpchk_expect(struct server *s, int done)
1598{
1599 static char status_msg[] = "HTTP status check returned code <000>";
1600 char status_code[] = "000";
1601 char *contentptr;
1602 int crlf;
1603 int ret;
1604
1605 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1606 case PR_O2_EXP_STS:
1607 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001608 memcpy(status_code, s->check.bi->data + 9, 3);
1609 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001610
1611 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1612 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1613 else
1614 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1615
1616 /* we necessarily have the response, so there are no partial failures */
1617 if (s->proxy->options2 & PR_O2_EXP_INV)
1618 ret = !ret;
1619
Simon Horman4a741432013-02-23 15:35:38 +09001620 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01001621 break;
1622
1623 case PR_O2_EXP_STR:
1624 case PR_O2_EXP_RSTR:
1625 /* very simple response parser: ignore CR and only count consecutive LFs,
1626 * stop with contentptr pointing to first char after the double CRLF or
1627 * to '\0' if crlf < 2.
1628 */
1629 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001630 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001631 if (crlf >= 2)
1632 break;
1633 if (*contentptr == '\r')
1634 continue;
1635 else if (*contentptr == '\n')
1636 crlf++;
1637 else
1638 crlf = 0;
1639 }
1640
1641 /* Check that response contains a body... */
1642 if (crlf < 2) {
1643 if (!done)
1644 return 0;
1645
Simon Horman4a741432013-02-23 15:35:38 +09001646 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001647 "HTTP content check could not find a response body");
1648 return 1;
1649 }
1650
1651 /* Check that response body is not empty... */
1652 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001653 if (!done)
1654 return 0;
1655
Simon Horman4a741432013-02-23 15:35:38 +09001656 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001657 "HTTP content check found empty response body");
1658 return 1;
1659 }
1660
1661 /* Check the response content against the supplied string
1662 * or regex... */
1663 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1664 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1665 else
1666 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1667
1668 /* if we don't match, we may need to wait more */
1669 if (!ret && !done)
1670 return 0;
1671
1672 if (ret) {
1673 /* content matched */
1674 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001675 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001676 "HTTP check matched unwanted content");
1677 else
Simon Horman4a741432013-02-23 15:35:38 +09001678 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001679 "HTTP content check matched");
1680 }
1681 else {
1682 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001683 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001684 "HTTP check did not match unwanted content");
1685 else
Simon Horman4a741432013-02-23 15:35:38 +09001686 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001687 "HTTP content check did not match");
1688 }
1689 break;
1690 }
1691 return 1;
1692}
1693
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001694/*
1695 * return the id of a step in a send/expect session
1696 */
1697static int tcpcheck_get_step_id(struct server *s)
1698{
1699 struct tcpcheck_rule *cur = NULL, *next = NULL;
1700 int i = 0;
1701
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001702 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001703
1704 /* no step => first step */
1705 if (cur == NULL)
1706 return 1;
1707
1708 /* increment i until current step */
1709 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
1710 if (next->list.p == &cur->list)
1711 break;
1712 ++i;
1713 }
1714
1715 return i;
1716}
1717
1718static void tcpcheck_main(struct connection *conn)
1719{
1720 char *contentptr;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001721 struct list *head = NULL;
1722 struct tcpcheck_rule *cur = NULL;
1723 int done = 0, ret = 0;
1724
1725 struct check *check = conn->owner;
1726 struct server *s = check->server;
1727 struct task *t = check->task;
1728
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001729 /*
1730 * don't do anything until the connection is established but if we're running
1731 * first step which must be a connect
1732 */
1733 if (check->current_step && (!(conn->flags & CO_FL_CONNECTED))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001734 /* update expire time, should be done by process_chk */
1735 /* we allow up to min(inter, timeout.connect) for a connection
1736 * to establish but only when timeout.check is set
1737 * as it may be to short for a full check otherwise
1738 */
1739 while (tick_is_expired(t->expire, now_ms)) {
1740 int t_con;
1741
1742 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1743 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1744
1745 if (s->proxy->timeout.check)
1746 t->expire = tick_first(t->expire, t_con);
1747 }
1748 return;
1749 }
1750
1751 /* here, we know that the connection is established */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001752 if (check->result != CHK_RES_UNKNOWN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001753 goto out_end_tcpcheck;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001754
1755 /* head is be the first element of the double chained list */
1756 head = &s->proxy->tcpcheck_rules;
1757
1758 /* no step means first step
1759 * initialisation */
1760 if (check->current_step == NULL) {
1761 check->bo->p = check->bo->data;
1762 check->bo->o = 0;
1763 check->bi->p = check->bi->data;
1764 check->bi->i = 0;
1765 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
1766 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1767 if (s->proxy->timeout.check)
1768 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
1769 }
1770 /* keep on processing step */
1771 else {
1772 cur = check->current_step;
1773 }
1774
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001775 if (conn->flags & CO_FL_HANDSHAKE)
1776 return;
1777
1778 /* It's only the rules which will enable send/recv */
1779 __conn_data_stop_both(conn);
1780
Willy Tarreauabca5b62013-12-06 14:19:25 +01001781 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001782 /* we have to try to flush the output buffer before reading, at the end,
1783 * or if we're about to send a string that does not fit in the remaining space.
1784 */
1785 if (check->bo->o &&
1786 (&cur->list == head ||
1787 check->current_step->action != TCPCHK_ACT_SEND ||
1788 check->current_step->string_len >= buffer_total_space(check->bo))) {
1789
Willy Tarreau1049b1f2014-02-02 01:51:17 +01001790 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001791 if (conn->flags & CO_FL_ERROR) {
1792 chk_report_conn_err(conn, errno, 0);
1793 __conn_data_stop_both(conn);
1794 goto out_end_tcpcheck;
1795 }
1796 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01001797 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001798 }
1799
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001800 /* did we reach the end ? If so, let's check that everything was sent */
1801 if (&cur->list == head) {
1802 if (check->bo->o)
1803 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01001804 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001805 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001806
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001807 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
1808 struct protocol *proto;
1809 struct xprt_ops *xprt;
1810
1811 /* mark the step as started */
1812 check->last_started_step = check->current_step;
1813 /* first, shut existing connection */
1814 conn_force_close(conn);
1815
1816 /* prepare new connection */
1817 /* initialization */
1818 conn_init(conn);
1819 conn_attach(conn, check, &check_conn_cb);
1820 conn->target = &s->obj_type;
1821
1822 /* no client address */
1823 clear_addr(&conn->addr.from);
1824
Willy Tarreau640556c2014-05-09 23:38:15 +02001825 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001826 /* we'll connect to the check addr specified on the server */
1827 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02001828 proto = s->check_common.proto;
1829 }
1830 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001831 /* we'll connect to the addr on the server */
1832 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02001833 proto = s->proto;
1834 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001835
1836 /* port */
1837 if (check->current_step->port)
1838 set_host_port(&conn->addr.to, check->current_step->port);
1839 else if (check->port)
1840 set_host_port(&conn->addr.to, check->port);
1841
1842#ifdef USE_OPENSSL
1843 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
1844 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001845 }
1846 else {
1847 xprt = &raw_sock;
1848 }
1849#else /* USE_OPENSSL */
1850 xprt = &raw_sock;
1851#endif /* USE_OPENSSL */
1852 conn_prepare(conn, proto, xprt);
1853
1854 ret = SN_ERR_INTERNAL;
1855 if (proto->connect)
1856 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
1857 conn->flags |= CO_FL_WAKE_DATA;
1858 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
1859 conn->send_proxy_ofs = 1;
1860 conn->flags |= CO_FL_SEND_PROXY;
1861 }
1862
1863 /* It can return one of :
1864 * - SN_ERR_NONE if everything's OK
1865 * - SN_ERR_SRVTO if there are no more servers
1866 * - SN_ERR_SRVCL if the connection was refused by the server
1867 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1868 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1869 * - SN_ERR_INTERNAL for any other purely internal errors
1870 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1871 * Note that we try to prevent the network stack from sending the ACK during the
1872 * connect() when a pure TCP check is used (without PROXY protocol).
1873 */
1874 switch (ret) {
1875 case SN_ERR_NONE:
1876 /* we allow up to min(inter, timeout.connect) for a connection
1877 * to establish but only when timeout.check is set
1878 * as it may be to short for a full check otherwise
1879 */
1880 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1881
1882 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1883 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1884 t->expire = tick_first(t->expire, t_con);
1885 }
1886 break;
1887 case SN_ERR_SRVTO: /* ETIMEDOUT */
1888 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1889 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
1890 tcpcheck_get_step_id(s), strerror(errno));
1891 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
1892 goto out_end_tcpcheck;
1893 case SN_ERR_PRXCOND:
1894 case SN_ERR_RESOURCE:
1895 case SN_ERR_INTERNAL:
1896 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
1897 tcpcheck_get_step_id(s));
1898 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
1899 goto out_end_tcpcheck;
1900 }
1901
1902 /* allow next rule */
1903 cur = (struct tcpcheck_rule *)cur->list.n;
1904 check->current_step = cur;
1905
1906 /* don't do anything until the connection is established */
1907 if (!(conn->flags & CO_FL_CONNECTED)) {
1908 /* update expire time, should be done by process_chk */
1909 /* we allow up to min(inter, timeout.connect) for a connection
1910 * to establish but only when timeout.check is set
1911 * as it may be to short for a full check otherwise
1912 */
1913 while (tick_is_expired(t->expire, now_ms)) {
1914 int t_con;
1915
1916 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1917 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1918
1919 if (s->proxy->timeout.check)
1920 t->expire = tick_first(t->expire, t_con);
1921 }
1922 return;
1923 }
1924
1925 } /* end 'connect' */
1926 else if (check->current_step->action == TCPCHK_ACT_SEND) {
1927 /* mark the step as started */
1928 check->last_started_step = check->current_step;
1929
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001930 /* reset the read buffer */
1931 if (*check->bi->data != '\0') {
1932 *check->bi->data = '\0';
1933 check->bi->i = 0;
1934 }
1935
1936 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
1937 conn->flags |= CO_FL_ERROR;
1938 chk_report_conn_err(conn, 0, 0);
1939 goto out_end_tcpcheck;
1940 }
1941
Willy Tarreauabca5b62013-12-06 14:19:25 +01001942 if (check->current_step->string_len >= check->bo->size) {
1943 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
1944 check->current_step->string_len, check->bo->size,
1945 tcpcheck_get_step_id(s));
1946 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
1947 goto out_end_tcpcheck;
1948 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001949
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001950 /* do not try to send if there is no space */
1951 if (check->current_step->string_len >= buffer_total_space(check->bo))
1952 continue;
1953
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001954 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
1955 *check->bo->p = '\0'; /* to make gdb output easier to read */
1956
Willy Tarreauabca5b62013-12-06 14:19:25 +01001957 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001958 cur = (struct tcpcheck_rule *)cur->list.n;
1959 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001960 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01001961 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001962 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001963 goto out_end_tcpcheck;
1964
Willy Tarreau310987a2014-01-22 19:46:33 +01001965 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001966 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
1967 done = 1;
1968 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
1969 /* Report network errors only if we got no other data. Otherwise
1970 * we'll let the upper layers decide whether the response is OK
1971 * or not. It is very common that an RST sent by the server is
1972 * reported as an error just after the last data chunk.
1973 */
1974 chk_report_conn_err(conn, errno, 0);
1975 goto out_end_tcpcheck;
1976 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001977 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001978 else
1979 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001980 }
1981
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001982 /* mark the step as started */
1983 check->last_started_step = check->current_step;
1984
1985
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001986 /* Intermediate or complete response received.
1987 * Terminate string in check->bi->data buffer.
1988 */
1989 if (check->bi->i < check->bi->size) {
1990 check->bi->data[check->bi->i] = '\0';
1991 }
1992 else {
1993 check->bi->data[check->bi->i - 1] = '\0';
1994 done = 1; /* buffer full, don't wait for more data */
1995 }
1996
1997 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001998
1999 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002000 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002001 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002002 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002003
2004 /* empty response */
2005 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2006 tcpcheck_get_step_id(s));
2007 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2008
2009 goto out_end_tcpcheck;
2010 }
2011
2012 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002013 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002014
Willy Tarreaua970c282013-12-06 12:47:19 +01002015 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002016 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002017 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002018 else if (cur->expect_regex != NULL)
2019 ret = regexec(cur->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
2020
2021 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002022 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002023
2024 /* matched */
2025 if (ret) {
2026 /* matched but we did not want to => ERROR */
2027 if (cur->inverse) {
2028 /* we were looking for a string */
2029 if (cur->string != NULL) {
2030 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2031 cur->string, tcpcheck_get_step_id(s));
2032 }
2033 else {
2034 /* we were looking for a regex */
2035 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2036 tcpcheck_get_step_id(s));
2037 }
2038 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2039 goto out_end_tcpcheck;
2040 }
2041 /* matched and was supposed to => OK, next step */
2042 else {
2043 cur = (struct tcpcheck_rule*)cur->list.n;
2044 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002045 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002046 goto tcpcheck_expect;
2047 __conn_data_stop_recv(conn);
2048 }
2049 }
2050 else {
2051 /* not matched */
2052 /* not matched and was not supposed to => OK, next step */
2053 if (cur->inverse) {
2054 cur = (struct tcpcheck_rule*)cur->list.n;
2055 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002056 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002057 goto tcpcheck_expect;
2058 __conn_data_stop_recv(conn);
2059 }
2060 /* not matched but was supposed to => ERROR */
2061 else {
2062 /* we were looking for a string */
2063 if (cur->string != NULL) {
2064 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2065 cur->string, tcpcheck_get_step_id(s));
2066 }
2067 else {
2068 /* we were looking for a regex */
2069 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2070 tcpcheck_get_step_id(s));
2071 }
2072 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2073 goto out_end_tcpcheck;
2074 }
2075 }
2076 } /* end expect */
2077 } /* end loop over double chained step list */
2078
2079 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2080 goto out_end_tcpcheck;
2081
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002082 out_need_io:
2083 if (check->bo->o)
2084 __conn_data_want_send(conn);
2085
2086 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2087 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002088 return;
2089
2090 out_end_tcpcheck:
2091 /* collect possible new errors */
2092 if (conn->flags & CO_FL_ERROR)
2093 chk_report_conn_err(conn, 0, 0);
2094
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002095 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002096 check->current_step = NULL;
2097
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002098 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002099 conn->flags |= CO_FL_ERROR;
2100
2101 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002102
2103 return;
2104}
2105
2106
Willy Tarreaubd741542010-03-16 18:46:54 +01002107/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002108 * Local variables:
2109 * c-indent-level: 8
2110 * c-basic-offset: 8
2111 * End:
2112 */