blob: 18faa9bbf6609791661878a7d8996535d386e8ad [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>
Willy Tarreau9b39dc52014-07-08 00:54:10 +020018#include <signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020020#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020022#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023#include <unistd.h>
24#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040025#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090026#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020028#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <arpa/inet.h>
30
Willy Tarreauc7e42382012-08-24 19:22:53 +020031#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020032#include <common/compat.h>
33#include <common/config.h>
34#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020035#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020036#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
Baptiste Assmann69e273f2013-12-11 00:52:19 +010040#ifdef USE_OPENSSL
41#include <types/ssl_sock.h>
42#include <proto/ssl_sock.h>
43#endif /* USE_OPENSSL */
44
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020046#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
49#include <proto/log.h>
50#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020051#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010052#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010053#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010054#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010055#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020056#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090058#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010059#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/task.h>
61
Willy Tarreaubd741542010-03-16 18:46:54 +010062static int httpchk_expect(struct server *s, int done);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020063static int tcpcheck_get_step_id(struct server *);
64static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010065
Simon Horman63a4a822012-03-19 07:24:41 +090066static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010067 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
68 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020069 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020070
Willy Tarreau23964182014-05-20 20:56:30 +020071 /* Below we have finished checks */
72 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010073 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010074
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010075 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010077 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
78 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
79 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020080
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010081 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
82 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
83 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020084
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010085 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
86 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020087
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020088 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020089
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010090 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
91 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
92 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090093
94 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
95 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
96 [HCHK_STATUS_PROCOK] = { CHK_RES_FAILED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020097};
98
Simon Horman63a4a822012-03-19 07:24:41 +090099static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100100 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
101
102 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
103 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
104
105 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
106 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
107 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
108 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
109
110 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
111 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
112 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
113};
114
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200115/*
116 * Convert check_status code to description
117 */
118const char *get_check_status_description(short check_status) {
119
120 const char *desc;
121
122 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200123 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200124 else
125 desc = NULL;
126
127 if (desc && *desc)
128 return desc;
129 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200130 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200131}
132
133/*
134 * Convert check_status code to short info
135 */
136const char *get_check_status_info(short check_status) {
137
138 const char *info;
139
140 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200141 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200142 else
143 info = NULL;
144
145 if (info && *info)
146 return info;
147 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200148 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200149}
150
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100151const char *get_analyze_status(short analyze_status) {
152
153 const char *desc;
154
155 if (analyze_status < HANA_STATUS_SIZE)
156 desc = analyze_statuses[analyze_status].desc;
157 else
158 desc = NULL;
159
160 if (desc && *desc)
161 return desc;
162 else
163 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
164}
165
Willy Tarreaua150cf12014-05-20 21:57:23 +0200166/* Builds a string containing some information about the health check's result.
167 * The output string is allocated from the trash chunks. If the check is NULL,
168 * NULL is returned. This is designed to be used when emitting logs about health
169 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200170 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200171static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200172{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200173 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200174
Willy Tarreaua150cf12014-05-20 21:57:23 +0200175 if (!check)
176 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200177
Willy Tarreaua150cf12014-05-20 21:57:23 +0200178 msg = get_trash_chunk();
179 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200180
Willy Tarreaua150cf12014-05-20 21:57:23 +0200181 if (check->status >= HCHK_STATUS_L57DATA)
182 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200183
Willy Tarreaua150cf12014-05-20 21:57:23 +0200184 if (*check->desc) {
185 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200186
Willy Tarreaua150cf12014-05-20 21:57:23 +0200187 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200188
Willy Tarreaua150cf12014-05-20 21:57:23 +0200189 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
190 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200191
Willy Tarreaua150cf12014-05-20 21:57:23 +0200192 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200193 }
194
Willy Tarreaua150cf12014-05-20 21:57:23 +0200195 if (check->duration >= 0)
196 chunk_appendf(msg, ", check duration: %ldms", check->duration);
197
198 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200199}
200
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200201/*
Simon Horman4a741432013-02-23 15:35:38 +0900202 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200203 * an adequate CHK_RES_* value. The new check->health is computed based
204 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200205 *
206 * Show information in logs about failed health check if server is UP
207 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200208 */
Simon Horman4a741432013-02-23 15:35:38 +0900209static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100210{
Simon Horman4a741432013-02-23 15:35:38 +0900211 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200212 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200213 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900214
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200215 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100216 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900217 check->desc[0] = '\0';
218 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200219 return;
220 }
221
Simon Horman4a741432013-02-23 15:35:38 +0900222 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200223 return;
224
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200225 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900226 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
227 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200228 } else
Simon Horman4a741432013-02-23 15:35:38 +0900229 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200230
Simon Horman4a741432013-02-23 15:35:38 +0900231 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200232 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900233 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200234
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100235 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900236 check->duration = -1;
237 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200238 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900239 check->duration = tv_ms_elapsed(&check->start, &now);
240 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200241 }
242
Willy Tarreau23964182014-05-20 20:56:30 +0200243 /* no change is expected if no state change occurred */
244 if (check->result == CHK_RES_NEUTRAL)
245 return;
246
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200247 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200248
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200249 switch (check->result) {
250 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200251 /* Failure to connect to the agent as a secondary check should not
252 * cause the server to be marked down.
253 */
254 if ((!(check->state & CHK_ST_AGENT) ||
255 (check->status >= HCHK_STATUS_L7TOUT)) &&
256 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200257 s->counters.failed_checks++;
258 report = 1;
259 check->health--;
260 if (check->health < check->rise)
261 check->health = 0;
262 }
263 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200264
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200265 case CHK_RES_PASSED:
266 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
267 if ((check->health < check->rise + check->fall - 1) &&
268 (check->result == CHK_RES_PASSED || check->health > 0)) {
269 report = 1;
270 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200271
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200272 if (check->health >= check->rise)
273 check->health = check->rise + check->fall - 1; /* OK now */
274 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200275
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200276 /* clear consecutive_errors if observing is enabled */
277 if (s->onerror)
278 s->consecutive_errors = 0;
279 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100280
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200281 default:
282 break;
283 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200284
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200285 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
286 (status != prev_status || report)) {
287 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200288 "%s check for %sserver %s/%s %s%s",
289 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200290 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100291 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100292 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200293 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200294
Willy Tarreaua150cf12014-05-20 21:57:23 +0200295 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200296
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100297 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200298 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
299 (check->health >= check->rise) ? check->fall : check->rise,
300 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200301
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100302 Warning("%s.\n", trash.str);
303 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200304 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200305}
306
Willy Tarreau4eec5472014-05-20 22:32:27 +0200307/* Marks the check <check>'s server down if the current check is already failed
308 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200310static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200311{
Simon Horman4a741432013-02-23 15:35:38 +0900312 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900313
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200314 /* The agent secondary check should only cause a server to be marked
315 * as down if check->status is HCHK_STATUS_L7STS, which indicates
316 * that the agent returned "fail", "stopped" or "down".
317 * The implication here is that failure to connect to the agent
318 * as a secondary check should not cause the server to be marked
319 * down. */
320 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
321 return;
322
Willy Tarreau4eec5472014-05-20 22:32:27 +0200323 if (check->health > 0)
324 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100325
Willy Tarreau4eec5472014-05-20 22:32:27 +0200326 /* We only report a reason for the check if we did not do so previously */
327 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328}
329
Willy Tarreauaf549582014-05-16 17:37:50 +0200330/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200331 * it isn't in maintenance, it is not tracking a down server and other checks
332 * comply. The rule is simple : by default, a server is up, unless any of the
333 * following conditions is true :
334 * - health check failed (check->health < rise)
335 * - agent check failed (agent->health < rise)
336 * - the server tracks a down server (track && track->state == STOPPED)
337 * Note that if the server has a slowstart, it will switch to STARTING instead
338 * of RUNNING. Also, only the health checks support the nolb mode, so the
339 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200340 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200341static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200342{
Simon Horman4a741432013-02-23 15:35:38 +0900343 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100344
Willy Tarreauaf549582014-05-16 17:37:50 +0200345 if (s->admin & SRV_ADMF_MAINT)
346 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100347
Willy Tarreau3e048382014-05-21 10:30:54 +0200348 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200349 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100350
Willy Tarreau3e048382014-05-21 10:30:54 +0200351 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
352 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100353
Willy Tarreau3e048382014-05-21 10:30:54 +0200354 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
355 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200356
Willy Tarreau3e048382014-05-21 10:30:54 +0200357 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
358 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100359
Willy Tarreau3e048382014-05-21 10:30:54 +0200360 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 +0100361}
362
Willy Tarreaudb58b792014-05-21 13:57:23 +0200363/* Marks the check <check> as valid and tries to set its server into stopping mode
364 * if it was running or starting, and provided it isn't in maintenance and other
365 * checks comply. The conditions for the server to be marked in stopping mode are
366 * the same as for it to be turned up. Also, only the health checks support the
367 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200368 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200369static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200370{
Simon Horman4a741432013-02-23 15:35:38 +0900371 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100372
Willy Tarreauaf549582014-05-16 17:37:50 +0200373 if (s->admin & SRV_ADMF_MAINT)
374 return;
375
Willy Tarreaudb58b792014-05-21 13:57:23 +0200376 if (check->state & CHK_ST_AGENT)
377 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100378
Willy Tarreaudb58b792014-05-21 13:57:23 +0200379 if (s->track && s->track->state == SRV_ST_STOPPED)
380 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100381
Willy Tarreaudb58b792014-05-21 13:57:23 +0200382 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
383 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100384
Willy Tarreaudb58b792014-05-21 13:57:23 +0200385 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
386 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100387
Willy Tarreaudb58b792014-05-21 13:57:23 +0200388 srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100389}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100391/* note: use health_adjust() only, which first checks that the observe mode is
392 * enabled.
393 */
394void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100395{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100396 int failed;
397 int expire;
398
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100399 if (s->observe >= HANA_OBS_SIZE)
400 return;
401
Willy Tarreaubb956662013-01-24 00:37:39 +0100402 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100403 return;
404
405 switch (analyze_statuses[status].lr[s->observe - 1]) {
406 case 1:
407 failed = 1;
408 break;
409
410 case 2:
411 failed = 0;
412 break;
413
414 default:
415 return;
416 }
417
418 if (!failed) {
419 /* good: clear consecutive_errors */
420 s->consecutive_errors = 0;
421 return;
422 }
423
424 s->consecutive_errors++;
425
426 if (s->consecutive_errors < s->consecutive_errors_limit)
427 return;
428
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100429 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
430 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100431
432 switch (s->onerror) {
433 case HANA_ONERR_FASTINTER:
434 /* force fastinter - nothing to do here as all modes force it */
435 break;
436
437 case HANA_ONERR_SUDDTH:
438 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900439 if (s->check.health > s->check.rise)
440 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100441
442 /* no break - fall through */
443
444 case HANA_ONERR_FAILCHK:
445 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900446 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200447 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100448 break;
449
450 case HANA_ONERR_MARKDWN:
451 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900452 s->check.health = s->check.rise;
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 default:
458 /* write a warning? */
459 break;
460 }
461
462 s->consecutive_errors = 0;
463 s->counters.failed_hana++;
464
Simon Horman66183002013-02-23 10:16:43 +0900465 if (s->check.fastinter) {
466 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300467 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200468 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300469 /* requeue check task with new expire */
470 task_queue(s->check.task);
471 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100472 }
473}
474
Willy Tarreaua1dab552014-04-14 15:04:54 +0200475static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100476{
477 int sv_state;
478 int ratio;
479 int hlen = 0;
480 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
481 "UP %d/%d", "UP",
482 "NOLB %d/%d", "NOLB",
483 "no check" };
484
485 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
486 hlen += 24;
487
Willy Tarreauff5ae352013-12-11 20:36:34 +0100488 if (!(s->check.state & CHK_ST_ENABLED))
489 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200490 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900491 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100492 sv_state = 3; /* UP */
493 else
494 sv_state = 2; /* going down */
495
Willy Tarreau892337c2014-05-13 23:41:20 +0200496 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100497 sv_state += 2;
498 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900499 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100500 sv_state = 1; /* going up */
501 else
502 sv_state = 0; /* DOWN */
503 }
504
Willy Tarreaua1dab552014-04-14 15:04:54 +0200505 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100506 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200507 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
508 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100509
Willy Tarreaua1dab552014-04-14 15:04:54 +0200510 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 +0100511 s->proxy->id, s->id,
512 global.node,
513 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
514 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
515 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
516 s->nbpend);
517
Willy Tarreau892337c2014-05-13 23:41:20 +0200518 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100519 now.tv_sec < s->last_change + s->slowstart &&
520 now.tv_sec >= s->last_change) {
521 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200522 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100523 }
524
525 buffer[hlen++] = '\r';
526 buffer[hlen++] = '\n';
527
528 return hlen;
529}
530
Willy Tarreau20a18342013-12-05 00:31:46 +0100531/* Check the connection. If an error has already been reported or the socket is
532 * closed, keep errno intact as it is supposed to contain the valid error code.
533 * If no error is reported, check the socket's error queue using getsockopt().
534 * Warning, this must be done only once when returning from poll, and never
535 * after an I/O error was attempted, otherwise the error queue might contain
536 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
537 * socket. Returns non-zero if an error was reported, zero if everything is
538 * clean (including a properly closed socket).
539 */
540static int retrieve_errno_from_socket(struct connection *conn)
541{
542 int skerr;
543 socklen_t lskerr = sizeof(skerr);
544
545 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
546 return 1;
547
Willy Tarreau3c728722014-01-23 13:50:42 +0100548 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100549 return 0;
550
551 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
552 errno = skerr;
553
554 if (errno == EAGAIN)
555 errno = 0;
556
557 if (!errno) {
558 /* we could not retrieve an error, that does not mean there is
559 * none. Just don't change anything and only report the prior
560 * error if any.
561 */
562 if (conn->flags & CO_FL_ERROR)
563 return 1;
564 else
565 return 0;
566 }
567
568 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
569 return 1;
570}
571
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100572/* Try to collect as much information as possible on the connection status,
573 * and adjust the server status accordingly. It may make use of <errno_bck>
574 * if non-null when the caller is absolutely certain of its validity (eg:
575 * checked just after a syscall). If the caller doesn't have a valid errno,
576 * it can pass zero, and retrieve_errno_from_socket() will be called to try
577 * to extract errno from the socket. If no error is reported, it will consider
578 * the <expired> flag. This is intended to be used when a connection error was
579 * reported in conn->flags or when a timeout was reported in <expired>. The
580 * function takes care of not updating a server status which was already set.
581 * All situations where at least one of <expired> or CO_FL_ERROR are set
582 * produce a status.
583 */
584static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
585{
586 struct check *check = conn->owner;
587 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200588 struct chunk *chk;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100589
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100590 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100591 return;
592
593 errno = errno_bck;
594 if (!errno || errno == EAGAIN)
595 retrieve_errno_from_socket(conn);
596
597 if (!(conn->flags & CO_FL_ERROR) && !expired)
598 return;
599
600 /* we'll try to build a meaningful error message depending on the
601 * context of the error possibly present in conn->err_code, and the
602 * socket error possibly collected above. This is useful to know the
603 * exact step of the L6 layer (eg: SSL handshake).
604 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200605 chk = get_trash_chunk();
606
607 if (check->type == PR_O2_TCPCHK_CHK) {
608 chunk_printf(chk, " at step %d of tcp-check", tcpcheck_get_step_id(check->server));
609 /* we were looking for a string */
Baptiste Assmann69e273f2013-12-11 00:52:19 +0100610 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
611 chunk_appendf(chk, " (connect)");
612 }
613 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200614 if (check->current_step->string)
615 chunk_appendf(chk, " (string '%s')", check->current_step->string);
616 else if (check->current_step->expect_regex)
617 chunk_appendf(chk, " (expect regex)");
618 }
619 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
620 chunk_appendf(chk, " (send)");
621 }
622 }
623
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100624 if (conn->err_code) {
625 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200626 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100627 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200628 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100629 err_msg = trash.str;
630 }
631 else {
632 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200633 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100634 err_msg = trash.str;
635 }
636 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200637 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100638 }
639 }
640
641 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
642 /* L4 not established (yet) */
643 if (conn->flags & CO_FL_ERROR)
644 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
645 else if (expired)
646 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
647 }
648 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
649 /* L6 not established (yet) */
650 if (conn->flags & CO_FL_ERROR)
651 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
652 else if (expired)
653 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
654 }
655 else if (conn->flags & CO_FL_ERROR) {
656 /* I/O error after connection was established and before we could diagnose */
657 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
658 }
659 else if (expired) {
660 /* connection established but expired check */
661 if (check->type == PR_O2_SSL3_CHK)
662 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
663 else /* HTTP, SMTP, ... */
664 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
665 }
666
667 return;
668}
669
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670/*
671 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200672 * the connection acknowledgement. If the proxy requires L7 health-checks,
673 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900674 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200676static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677{
Simon Horman4a741432013-02-23 15:35:38 +0900678 struct check *check = conn->owner;
679 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900680 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200681
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100682 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100683 goto out_wakeup;
684
Willy Tarreau310987a2014-01-22 19:46:33 +0100685 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100686 return;
687
Willy Tarreau20a18342013-12-05 00:31:46 +0100688 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100689 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100690 __conn_data_stop_both(conn);
691 goto out_wakeup;
692 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100693
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100694 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100695 /* if the output is closed, we can't do anything */
696 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100697 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100698 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200699 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200700
Willy Tarreau06559ac2013-12-05 01:53:08 +0100701 /* here, we know that the connection is established. That's enough for
702 * a pure TCP check.
703 */
704 if (!check->type)
705 goto out_wakeup;
706
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200707 if (check->type == PR_O2_TCPCHK_CHK) {
708 tcpcheck_main(conn);
709 return;
710 }
711
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100712 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100713 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100714 if (conn->flags & CO_FL_ERROR) {
715 chk_report_conn_err(conn, errno, 0);
716 __conn_data_stop_both(conn);
717 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100719 if (check->bo->o)
720 return;
721 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200722
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100723 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
724 if (s->proxy->timeout.check) {
725 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
726 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100728 goto out_nowake;
729
Willy Tarreau83749182007-04-15 20:56:27 +0200730 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200731 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200732 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200733 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734}
735
Willy Tarreaubaaee002006-06-26 02:48:02 +0200736/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200737 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200738 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900739 * set_server_check_status() to update check->status, check->duration
740 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200741
742 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
743 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
744 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
745 * response to an SSL HELLO (the principle is that this is enough to
746 * distinguish between an SSL server and a pure TCP relay). All other cases will
747 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
748 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200750static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751{
Simon Horman4a741432013-02-23 15:35:38 +0900752 struct check *check = conn->owner;
753 struct server *s = check->server;
754 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200755 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100756 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200757 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200758
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100759 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200760 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200761
Willy Tarreau310987a2014-01-22 19:46:33 +0100762 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200763 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200764
765 if (check->type == PR_O2_TCPCHK_CHK) {
766 tcpcheck_main(conn);
767 return;
768 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200769
Willy Tarreau83749182007-04-15 20:56:27 +0200770 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
771 * but the connection was closed on the remote end. Fortunately, recv still
772 * works correctly and we don't need to do the getsockopt() on linux.
773 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000774
775 /* Set buffer to point to the end of the data already read, and check
776 * that there is free space remaining. If the buffer is full, proceed
777 * with running the checks without attempting another socket read.
778 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000779
Willy Tarreau03938182010-03-17 21:52:07 +0100780 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000781
Simon Horman4a741432013-02-23 15:35:38 +0900782 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200783 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100784 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900785 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200786 /* Report network errors only if we got no other data. Otherwise
787 * we'll let the upper layers decide whether the response is OK
788 * or not. It is very common that an RST sent by the server is
789 * reported as an error just after the last data chunk.
790 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100791 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100792 goto out_wakeup;
793 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794 }
795
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100796
Willy Tarreau03938182010-03-17 21:52:07 +0100797 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900798 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100799 */
Simon Horman4a741432013-02-23 15:35:38 +0900800 if (check->bi->i < check->bi->size)
801 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100802 else {
Simon Horman4a741432013-02-23 15:35:38 +0900803 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100804 done = 1; /* buffer full, don't wait for more data */
805 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200806
Nick Chalk57b1bf72010-03-16 15:50:46 +0000807 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900808 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200809 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900810 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100811 goto wait_more_data;
812
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100813 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900814 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
815 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
816 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
817 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
818 !isdigit((unsigned char) *(check->bi->data + 11))) {
819 cut_crlf(check->bi->data);
820 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200821
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100822 goto out_wakeup;
823 }
824
Simon Horman4a741432013-02-23 15:35:38 +0900825 check->code = str2uic(check->bi->data + 9);
826 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000827
Willy Tarreaubd741542010-03-16 18:46:54 +0100828 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200829 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000830 /* 404 may be accepted as "stopping" only if the server was up */
831 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900832 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000833 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100834 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
835 /* Run content verification check... We know we have at least 13 chars */
836 if (!httpchk_expect(s, done))
837 goto wait_more_data;
838 }
839 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900840 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100841 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900842 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100843 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000844 else {
845 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900846 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000847 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200848 break;
849
850 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900851 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100852 goto wait_more_data;
853
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100854 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900855 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
856 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200857 else
Simon Horman4a741432013-02-23 15:35:38 +0900858 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200859 break;
860
861 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900862 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100863 goto wait_more_data;
864
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200865 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900866 if ((check->bi->i < strlen("000\r")) ||
867 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
868 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
869 !isdigit((unsigned char) *(check->bi->data + 2))) {
870 cut_crlf(check->bi->data);
871 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200872
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200873 goto out_wakeup;
874 }
875
Simon Horman4a741432013-02-23 15:35:38 +0900876 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200877
Simon Horman4a741432013-02-23 15:35:38 +0900878 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200879 cut_crlf(desc);
880
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100881 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900882 if (*check->bi->data == '2')
883 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200884 else
Simon Horman4a741432013-02-23 15:35:38 +0900885 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200886 break;
887
Simon Hormana2b9dad2013-02-12 10:45:54 +0900888 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100889 int status = HCHK_STATUS_CHECKED;
890 const char *hs = NULL; /* health status */
891 const char *as = NULL; /* admin status */
892 const char *ps = NULL; /* performance status */
893 const char *err = NULL; /* first error to report */
894 const char *wrn = NULL; /* first warning to report */
895 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900896
Willy Tarreau81f5d942013-12-09 20:51:51 +0100897 /* We're getting an agent check response. The agent could
898 * have been disabled in the mean time with a long check
899 * still pending. It is important that we ignore the whole
900 * response.
901 */
902 if (!(check->server->agent.state & CHK_ST_ENABLED))
903 break;
904
905 /* The agent supports strings made of a single line ended by the
906 * first CR ('\r') or LF ('\n'). This line is composed of words
907 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
908 * line may optionally contained a description of a state change
909 * after a sharp ('#'), which is only considered if a health state
910 * is announced.
911 *
912 * Words may be composed of :
913 * - a numeric weight suffixed by the percent character ('%').
914 * - a health status among "up", "down", "stopped", and "fail".
915 * - an admin status among "ready", "drain", "maint".
916 *
917 * These words may appear in any order. If multiple words of the
918 * same category appear, the last one wins.
919 */
920
Willy Tarreau9809b782013-12-11 21:40:11 +0100921 p = check->bi->data;
922 while (*p && *p != '\n' && *p != '\r')
923 p++;
924
925 if (!*p) {
926 if (!done)
927 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900928
Willy Tarreau9809b782013-12-11 21:40:11 +0100929 /* at least inform the admin that the agent is mis-behaving */
930 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
931 break;
932 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100933
Willy Tarreau9809b782013-12-11 21:40:11 +0100934 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100935 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900936
Willy Tarreau81f5d942013-12-09 20:51:51 +0100937 while (*cmd) {
938 /* look for next word */
939 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
940 cmd++;
941 continue;
942 }
Simon Horman671b6f02013-11-25 10:46:39 +0900943
Willy Tarreau81f5d942013-12-09 20:51:51 +0100944 if (*cmd == '#') {
945 /* this is the beginning of a health status description,
946 * skip the sharp and blanks.
947 */
948 cmd++;
949 while (*cmd == '\t' || *cmd == ' ')
950 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900951 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900952 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100953
954 /* find the end of the word so that we have a null-terminated
955 * word between <cmd> and <p>.
956 */
957 p = cmd + 1;
958 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
959 p++;
960 if (*p)
961 *p++ = 0;
962
963 /* first, health statuses */
964 if (strcasecmp(cmd, "up") == 0) {
965 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900966 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100967 hs = cmd;
968 }
969 else if (strcasecmp(cmd, "down") == 0) {
970 check->health = 0;
971 status = HCHK_STATUS_L7STS;
972 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900973 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100974 else if (strcasecmp(cmd, "stopped") == 0) {
975 check->health = 0;
976 status = HCHK_STATUS_L7STS;
977 hs = cmd;
978 }
979 else if (strcasecmp(cmd, "fail") == 0) {
980 check->health = 0;
981 status = HCHK_STATUS_L7STS;
982 hs = cmd;
983 }
984 /* admin statuses */
985 else if (strcasecmp(cmd, "ready") == 0) {
986 as = cmd;
987 }
988 else if (strcasecmp(cmd, "drain") == 0) {
989 as = cmd;
990 }
991 else if (strcasecmp(cmd, "maint") == 0) {
992 as = cmd;
993 }
994 /* else try to parse a weight here and keep the last one */
995 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
996 ps = cmd;
997 }
998 else {
999 /* keep a copy of the first error */
1000 if (!err)
1001 err = cmd;
1002 }
1003 /* skip to next word */
1004 cmd = p;
1005 }
1006 /* here, cmd points either to \0 or to the beginning of a
1007 * description. Skip possible leading spaces.
1008 */
1009 while (*cmd == ' ' || *cmd == '\n')
1010 cmd++;
1011
1012 /* First, update the admin status so that we avoid sending other
1013 * possibly useless warnings and can also update the health if
1014 * present after going back up.
1015 */
1016 if (as) {
1017 if (strcasecmp(as, "drain") == 0)
1018 srv_adm_set_drain(check->server);
1019 else if (strcasecmp(as, "maint") == 0)
1020 srv_adm_set_maint(check->server);
1021 else
1022 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001023 }
1024
Willy Tarreau81f5d942013-12-09 20:51:51 +01001025 /* now change weights */
1026 if (ps) {
1027 const char *msg;
1028
1029 msg = server_parse_weight_change_request(s, ps);
1030 if (!wrn || !*wrn)
1031 wrn = msg;
1032 }
1033
1034 /* and finally health status */
1035 if (hs) {
1036 /* We'll report some of the warnings and errors we have
1037 * here. Down reports are critical, we leave them untouched.
1038 * Lack of report, or report of 'UP' leaves the room for
1039 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001040 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001041 const char *msg = cmd;
1042 struct chunk *t;
1043
1044 if (!*msg || status == HCHK_STATUS_L7OKD) {
1045 if (err && *err)
1046 msg = err;
1047 else if (wrn && *wrn)
1048 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001049 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001050
1051 t = get_trash_chunk();
1052 chunk_printf(t, "via agent : %s%s%s%s",
1053 hs, *msg ? " (" : "",
1054 msg, *msg ? ")" : "");
1055
1056 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001057 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001058 else if (err && *err) {
1059 /* No status change but we'd like to report something odd.
1060 * Just report the current state and copy the message.
1061 */
1062 chunk_printf(&trash, "agent reports an error : %s", err);
1063 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001064
Willy Tarreau81f5d942013-12-09 20:51:51 +01001065 }
1066 else if (wrn && *wrn) {
1067 /* No status change but we'd like to report something odd.
1068 * Just report the current state and copy the message.
1069 */
1070 chunk_printf(&trash, "agent warns : %s", wrn);
1071 set_server_check_status(check, status/*check->status*/, trash.str);
1072 }
1073 else
1074 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001075 break;
1076 }
1077
Willy Tarreau1620ec32011-08-06 17:05:02 +02001078 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001079 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001080 goto wait_more_data;
1081
Simon Horman4a741432013-02-23 15:35:38 +09001082 if (check->bi->data[0] == 'R') {
1083 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001084 }
1085 else {
Simon Horman4a741432013-02-23 15:35:38 +09001086 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1087 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001088 else
1089 desc = "PostgreSQL unknown error";
1090
Simon Horman4a741432013-02-23 15:35:38 +09001091 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001092 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001093 break;
1094
1095 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001096 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001097 goto wait_more_data;
1098
Simon Horman4a741432013-02-23 15:35:38 +09001099 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1100 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001101 }
1102 else {
Simon Horman4a741432013-02-23 15:35:38 +09001103 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001104 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001105 break;
1106
1107 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001108 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001109 goto wait_more_data;
1110
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001111 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001112 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001113 /* We set the MySQL Version in description for information purpose
1114 * FIXME : it can be cool to use MySQL Version for other purpose,
1115 * like mark as down old MySQL server.
1116 */
Simon Horman4a741432013-02-23 15:35:38 +09001117 if (check->bi->i > 51) {
1118 desc = ltrim(check->bi->data + 5, ' ');
1119 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001120 }
1121 else {
1122 if (!done)
1123 goto wait_more_data;
1124 /* it seems we have a OK packet but without a valid length,
1125 * it must be a protocol error
1126 */
Simon Horman4a741432013-02-23 15:35:38 +09001127 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001128 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001129 }
1130 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001131 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001132 desc = ltrim(check->bi->data + 7, ' ');
1133 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001134 }
1135 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001136 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1137 (((unsigned int) *(check->bi->data + 1)) << 8) +
1138 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001139
Simon Horman4a741432013-02-23 15:35:38 +09001140 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001141 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001142 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001143 /* We have only one MySQL packet and it is a Handshake Initialization packet
1144 * but we need to have a second packet to know if it is alright
1145 */
Simon Horman4a741432013-02-23 15:35:38 +09001146 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001147 goto wait_more_data;
1148 }
1149 else {
1150 /* We have only one packet and it is an Error packet,
1151 * an error message is attached, so we can display it
1152 */
Simon Horman4a741432013-02-23 15:35:38 +09001153 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001154 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001155 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001156 }
Simon Horman4a741432013-02-23 15:35:38 +09001157 } else if (check->bi->i > first_packet_len + 4) {
1158 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1159 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1160 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001161
Simon Horman4a741432013-02-23 15:35:38 +09001162 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001163 /* We have 2 packets and that's good */
1164 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001165 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001166 /* No error packet */
1167 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001168 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001169 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001170 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001171 }
1172 else {
1173 /* An error message is attached in the Error packet
1174 * so we can display it ! :)
1175 */
Simon Horman4a741432013-02-23 15:35:38 +09001176 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001177 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001178 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001179 }
1180 }
1181 }
1182 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001183 if (!done)
1184 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001185 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001186 * it must be a protocol error
1187 */
Simon Horman4a741432013-02-23 15:35:38 +09001188 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001189 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001190 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001191 }
1192 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001193 break;
1194
1195 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001196 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001197 goto wait_more_data;
1198
1199 /* Check if the server speaks LDAP (ASN.1/BER)
1200 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1201 * http://tools.ietf.org/html/rfc4511
1202 */
1203
1204 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1205 * LDAPMessage: 0x30: SEQUENCE
1206 */
Simon Horman4a741432013-02-23 15:35:38 +09001207 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1208 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001209 }
1210 else {
1211 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001212 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001213
1214 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1215 * messageID: 0x02 0x01 0x01: INTEGER 1
1216 * protocolOp: 0x61: bindResponse
1217 */
1218 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001219 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1220 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001221
1222 goto out_wakeup;
1223 }
1224
1225 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001226 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001227
1228 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1229 * ldapResult: 0x0a 0x01: ENUMERATION
1230 */
1231 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001232 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1233 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001234
1235 goto out_wakeup;
1236 }
1237
1238 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1239 * resultCode
1240 */
Simon Horman4a741432013-02-23 15:35:38 +09001241 check->code = *(check->bi->data + msglen + 9);
1242 if (check->code) {
1243 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 +02001244 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001245 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001246 }
1247 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001248 break;
1249
1250 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001251 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001252 break;
1253 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001254
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001255 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001256 /* collect possible new errors */
1257 if (conn->flags & CO_FL_ERROR)
1258 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001259
Nick Chalk57b1bf72010-03-16 15:50:46 +00001260 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001261 *check->bi->data = '\0';
1262 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001263
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001264 /* Close the connection... We absolutely want to perform a hard close
1265 * and reset the connection if some data are pending, otherwise we end
1266 * up with many TIME_WAITs and eat all the source port range quickly.
1267 * To avoid sending RSTs all the time, we first try to drain pending
1268 * data.
1269 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001270 if (conn->xprt && conn->xprt->shutw)
1271 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001272
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001273 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001274 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001275 conn->flags |= CO_FL_ERROR;
1276
Willy Tarreaua522f802012-11-23 08:56:35 +01001277 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001278 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001279 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001280
1281 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001282 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001283}
1284
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001285/*
1286 * This function is used only for server health-checks. It handles connection
1287 * status updates including errors. If necessary, it wakes the check task up.
1288 * It always returns 0.
1289 */
1290static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001291{
Simon Horman4a741432013-02-23 15:35:38 +09001292 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001293
Willy Tarreau6c560da2012-11-24 11:14:45 +01001294 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001295 /* We may get error reports bypassing the I/O handlers, typically
1296 * the case when sending a pure TCP check which fails, then the I/O
1297 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001298 * main processing task so let's simply wake it up. If we get here,
1299 * we expect errno to still be valid.
1300 */
1301 chk_report_conn_err(conn, errno, 0);
1302
Willy Tarreau2d351b62013-12-05 02:36:25 +01001303 __conn_data_stop_both(conn);
1304 task_wakeup(check->task, TASK_WOKEN_IO);
1305 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001306 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1307 /* we may get here if only a connection probe was required : we
1308 * don't have any data to send nor anything expected in response,
1309 * so the completion of the connection establishment is enough.
1310 */
1311 task_wakeup(check->task, TASK_WOKEN_IO);
1312 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001313
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001314 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001315 /* We're here because nobody wants to handle the error, so we
1316 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001317 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001318 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001319 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001320 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001321 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001322}
1323
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001324struct data_cb check_conn_cb = {
1325 .recv = event_srv_chk_r,
1326 .send = event_srv_chk_w,
1327 .wake = wake_srv_chk,
1328};
1329
Willy Tarreaubaaee002006-06-26 02:48:02 +02001330/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001331 * updates the server's weight during a warmup stage. Once the final weight is
1332 * reached, the task automatically stops. Note that any server status change
1333 * must have updated s->last_change accordingly.
1334 */
1335static struct task *server_warmup(struct task *t)
1336{
1337 struct server *s = t->context;
1338
1339 /* by default, plan on stopping the task */
1340 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001341 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001342 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001343 return t;
1344
Willy Tarreau892337c2014-05-13 23:41:20 +02001345 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001346 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001347
1348 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001349 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001350
1351 /* get back there in 1 second or 1/20th of the slowstart interval,
1352 * whichever is greater, resulting in small 5% steps.
1353 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001354 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001355 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1356 return t;
1357}
1358
1359/*
Simon Horman98637e52014-06-20 12:30:16 +09001360 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001361 *
1362 * It can return one of :
1363 * - SN_ERR_NONE if everything's OK and tcpcheck_main() was not called
1364 * - SN_ERR_UP if if everything's OK and tcpcheck_main() was called
1365 * - SN_ERR_SRVTO if there are no more servers
1366 * - SN_ERR_SRVCL if the connection was refused by the server
1367 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1368 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1369 * - SN_ERR_INTERNAL for any other purely internal errors
1370 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1371 * Note that we try to prevent the network stack from sending the ACK during the
1372 * connect() when a pure TCP check is used (without PROXY protocol).
1373 */
Simon Horman98637e52014-06-20 12:30:16 +09001374static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001375{
1376 struct check *check = t->context;
1377 struct server *s = check->server;
1378 struct connection *conn = check->conn;
1379 struct protocol *proto;
1380 int ret;
1381
1382 /* tcpcheck send/expect initialisation */
1383 if (check->type == PR_O2_TCPCHK_CHK)
1384 check->current_step = NULL;
1385
1386 /* prepare the check buffer.
1387 * This should not be used if check is the secondary agent check
1388 * of a server as s->proxy->check_req will relate to the
1389 * configuration of the primary check. Similarly, tcp-check uses
1390 * its own strings.
1391 */
1392 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1393 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1394
1395 /* we want to check if this host replies to HTTP or SSLv3 requests
1396 * so we'll send the request, and won't wake the checker up now.
1397 */
1398 if ((check->type) == PR_O2_SSL3_CHK) {
1399 /* SSL requires that we put Unix time in the request */
1400 int gmt_time = htonl(date.tv_sec);
1401 memcpy(check->bo->data + 11, &gmt_time, 4);
1402 }
1403 else if ((check->type) == PR_O2_HTTP_CHK) {
1404 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1405 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
1406 bo_putstr(check->bo, "\r\n");
1407 *check->bo->p = '\0'; /* to make gdb output easier to read */
1408 }
1409 }
1410
1411 /* prepare a new connection */
1412 conn_init(conn);
1413 conn_prepare(conn, s->check_common.proto, s->check_common.xprt);
1414 conn_attach(conn, check, &check_conn_cb);
1415 conn->target = &s->obj_type;
1416
1417 /* no client address */
1418 clear_addr(&conn->addr.from);
1419
1420 if (is_addr(&s->check_common.addr)) {
1421
1422 /* we'll connect to the check addr specified on the server */
1423 conn->addr.to = s->check_common.addr;
1424 proto = s->check_common.proto;
1425 }
1426 else {
1427 /* we'll connect to the addr on the server */
1428 conn->addr.to = s->addr;
1429 proto = s->proto;
1430 }
1431
1432 if (check->port) {
1433 set_host_port(&conn->addr.to, check->port);
1434 }
1435
1436 if (check->type == PR_O2_TCPCHK_CHK) {
1437 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1438 /* if first step is a 'connect', then tcpcheck_main must run it */
1439 if (r->action == TCPCHK_ACT_CONNECT) {
1440 tcpcheck_main(conn);
1441 return SN_ERR_UP;
1442 }
1443 }
1444
1445 ret = SN_ERR_INTERNAL;
1446 if (proto->connect)
1447 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
1448 conn->flags |= CO_FL_WAKE_DATA;
1449 if (s->check.send_proxy) {
1450 conn->send_proxy_ofs = 1;
1451 conn->flags |= CO_FL_SEND_PROXY;
1452 }
1453
1454 return ret;
1455}
1456
Simon Horman98637e52014-06-20 12:30:16 +09001457static struct list pid_list = LIST_HEAD_INIT(pid_list);
1458static struct pool_head *pool2_pid_list;
1459
1460void block_sigchld(void)
1461{
1462 sigset_t set;
1463 sigemptyset(&set);
1464 sigaddset(&set, SIGCHLD);
1465 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1466}
1467
1468void unblock_sigchld(void)
1469{
1470 sigset_t set;
1471 sigemptyset(&set);
1472 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1473}
1474
1475/* Call with SIGCHLD blocked */
1476static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1477{
1478 struct pid_list *elem;
1479 struct check *check = t->context;
1480
1481 elem = pool_alloc2(pool2_pid_list);
1482 if (!elem)
1483 return NULL;
1484 elem->pid = pid;
1485 elem->t = t;
1486 elem->exited = 0;
1487 check->curpid = elem;
1488 LIST_INIT(&elem->list);
1489 LIST_ADD(&pid_list, &elem->list);
1490 return elem;
1491}
1492
1493/* Blocks blocks and then unblocks SIGCHLD */
1494static void pid_list_del(struct pid_list *elem)
1495{
1496 struct check *check;
1497
1498 if (!elem)
1499 return;
1500
1501 block_sigchld();
1502 LIST_DEL(&elem->list);
1503 unblock_sigchld();
1504 if (!elem->exited)
1505 kill(elem->pid, SIGTERM);
1506
1507 check = elem->t->context;
1508 check->curpid = NULL;
1509 pool_free2(pool2_pid_list, elem);
1510}
1511
1512/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1513static void pid_list_expire(pid_t pid, int status)
1514{
1515 struct pid_list *elem;
1516
1517 list_for_each_entry(elem, &pid_list, list) {
1518 if (elem->pid == pid) {
1519 elem->t->expire = now_ms;
1520 elem->status = status;
1521 elem->exited = 1;
1522 return;
1523 }
1524 }
1525}
1526
1527static void sigchld_handler(int signal)
1528{
1529 pid_t pid;
1530 int status;
1531 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1532 pid_list_expire(pid, status);
1533}
1534
1535static int init_pid_list(void) {
1536 struct sigaction action = {
1537 .sa_handler = sigchld_handler,
1538 .sa_flags = SA_NOCLDSTOP
1539 };
1540
1541 if (pool2_pid_list != NULL)
1542 /* Nothing to do */
1543 return 0;
1544
1545 if (sigaction(SIGCHLD, &action, NULL)) {
1546 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1547 strerror(errno));
1548 return 1;
1549 }
1550
1551 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1552 if (pool2_pid_list == NULL) {
1553 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1554 strerror(errno));
1555 return 1;
1556 }
1557
1558 return 0;
1559}
1560
1561
1562static int prepare_external_check(struct check *check)
1563{
1564 struct server *s = check->server;
1565 struct proxy *px = s->proxy;
1566 struct listener *listener = NULL, *l;
1567 int i;
1568 const char *err_fmt = "Starting [%s:%s] check: out of memory.\n";
1569 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
1570 char host[46];
1571 char serv[6];
1572
1573 list_for_each_entry(l, &px->conf.listeners, by_fe)
1574 /* Use the first INET, INET6 or UNIX listener */
1575 if (l->addr.ss_family == AF_INET ||
1576 l->addr.ss_family == AF_INET6 ||
1577 l->addr.ss_family == AF_UNIX) {
1578 listener = l;
1579 break;
1580 }
1581
1582 if (!listener) {
1583 err_fmt = "Starting [%s:%s] check: no listener.\n";
1584 goto err;
1585 }
1586
1587 check->curpid = NULL;
1588
1589 check->envp = calloc(2, sizeof(check->argv));
1590 if (!check->envp)
1591 goto err;
1592 check->envp[0] = malloc(strlen("PATH=") + strlen(path) + 1);
1593 if (!check->envp[0])
1594 goto err;
1595 strcpy(check->envp[0], "PATH=");
1596 strcpy(check->envp[0] + strlen(check->envp[0]), path);
1597 check->envp[1] = NULL;
1598
1599 check->argv = calloc(6, sizeof(check->argv));
1600 if (!check->argv)
1601 goto err;
1602
1603 check->argv[0] = px->check_command;
1604
1605 if (listener->addr.ss_family == AF_INET ||
1606 listener->addr.ss_family == AF_INET6) {
1607 addr_to_str(&listener->addr, host, sizeof(host));
1608 check->argv[1] = strdup(host);
1609 port_to_str(&listener->addr, serv, sizeof(serv));
1610 check->argv[2] = strdup(serv);
1611 } else if (listener->addr.ss_family == AF_UNIX) {
1612 const struct sockaddr_un *un;
1613
1614 un = (struct sockaddr_un *)&listener->addr;
1615 check->argv[1] = strdup(un->sun_path);
1616 check->argv[2] = strdup("NOT_USED");
1617 } else {
1618 goto err;
1619 }
1620
1621 addr_to_str(&s->addr, host, sizeof(host));
1622 check->argv[3] = strdup(host);
1623 port_to_str(&s->addr, serv, sizeof(serv));
1624 check->argv[4] = strdup(serv);
1625
1626 for (i = 0; i < 5; i++)
1627 if (!check->argv[i])
1628 goto err;
1629
1630 return 0;
1631err:
1632 if (check->envp) {
1633 free(check->envp[1]);
1634 free(check->envp);
1635 check->envp = NULL;
1636 }
1637
1638 if (check->argv) {
1639 for (i = 1; i < 5; i++)
1640 free(check->argv[i]);
1641 free(check->argv);
1642 check->argv = NULL;
1643 }
1644 Alert(err_fmt, px->id, s->id);
1645 return -1;
1646}
1647
Simon Hormanb00d17a2014-06-13 16:18:16 +09001648/*
Simon Horman98637e52014-06-20 12:30:16 +09001649 * establish a server health-check that makes use of a process.
1650 *
1651 * It can return one of :
1652 * - SN_ERR_NONE if everything's OK
1653 * - SN_ERR_SRVTO if there are no more servers
1654 * - SN_ERR_SRVCL if the connection was refused by the server
1655 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1656 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1657 * - SN_ERR_INTERNAL for any other purely internal errors
1658 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1659 *
1660 * Blocks and then unblocks SIGCHLD
1661 */
1662static int connect_proc_chk(struct task *t)
1663{
1664 struct check *check = t->context;
1665 struct server *s = check->server;
1666 struct proxy *px = s->proxy;
1667 int status;
1668 pid_t pid;
1669
1670 if (!check->argv) {
1671 status = prepare_external_check(check);
1672 if (status < 0)
1673 return SN_ERR_RESOURCE;
1674 }
1675
1676 status = SN_ERR_RESOURCE;
1677
1678 block_sigchld();
1679
1680 pid = fork();
1681 if (pid < 0) {
1682 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1683 strerror(errno));
1684 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1685 goto out;
1686 }
1687 if (pid == 0) {
1688 /* Child */
1689 extern char **environ;
1690 environ = check->envp;
1691 execvp(px->check_command, check->argv);
1692 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1693 strerror(errno));
1694 exit(-1);
1695 }
1696
1697 /* Parent */
1698 if (check->result == CHK_RES_UNKNOWN) {
1699 if (pid_list_add(pid, t) != NULL) {
1700 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1701
1702 if (px->timeout.check && px->timeout.connect) {
1703 int t_con = tick_add(now_ms, px->timeout.connect);
1704 t->expire = tick_first(t->expire, t_con);
1705 }
1706 status = SN_ERR_NONE;
1707 goto out;
1708 }
1709 else {
1710 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1711 }
1712 kill(pid, SIGTERM); /* process creation error */
1713 }
1714 else
1715 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1716
1717out:
1718 unblock_sigchld();
1719 return status;
1720}
1721
1722/*
1723 * establish a server health-check.
1724 *
1725 * It can return one of :
1726 * - SN_ERR_NONE if everything's OK
1727 * - SN_ERR_SRVTO if there are no more servers
1728 * - SN_ERR_SRVCL if the connection was refused by the server
1729 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1730 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1731 * - SN_ERR_INTERNAL for any other purely internal errors
1732 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1733 */
1734static int connect_chk(struct task *t)
1735{
1736 struct check *check = t->context;
1737
1738 if (check->type == PR_O2_EXT_CHK)
1739 return connect_proc_chk(t);
1740 return connect_conn_chk(t);
1741}
1742
1743/*
1744 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001745 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1746 */
Simon Horman98637e52014-06-20 12:30:16 +09001747static struct task *process_chk_proc(struct task *t)
1748{
1749 struct check *check = t->context;
1750 struct server *s = check->server;
1751 struct connection *conn = check->conn;
1752 int rv;
1753 int ret;
1754 int expired = tick_is_expired(t->expire, now_ms);
1755
1756 if (!(check->state & CHK_ST_INPROGRESS)) {
1757 /* no check currently running */
1758 if (!expired) /* woke up too early */
1759 return t;
1760
1761 /* we don't send any health-checks when the proxy is
1762 * stopped, the server should not be checked or the check
1763 * is disabled.
1764 */
1765 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1766 s->proxy->state == PR_STSTOPPED)
1767 goto reschedule;
1768
1769 /* we'll initiate a new check */
1770 set_server_check_status(check, HCHK_STATUS_START, NULL);
1771
1772 check->state |= CHK_ST_INPROGRESS;
1773
1774 ret = connect_chk(t);
1775 switch (ret) {
1776 case SN_ERR_UP:
1777 return t;
1778 case SN_ERR_NONE:
1779 /* we allow up to min(inter, timeout.connect) for a connection
1780 * to establish but only when timeout.check is set
1781 * as it may be to short for a full check otherwise
1782 */
1783 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1784
1785 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1786 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1787 t->expire = tick_first(t->expire, t_con);
1788 }
1789
1790 goto reschedule;
1791
1792 case SN_ERR_SRVTO: /* ETIMEDOUT */
1793 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1794 conn->flags |= CO_FL_ERROR;
1795 chk_report_conn_err(conn, errno, 0);
1796 break;
1797 case SN_ERR_PRXCOND:
1798 case SN_ERR_RESOURCE:
1799 case SN_ERR_INTERNAL:
1800 conn->flags |= CO_FL_ERROR;
1801 chk_report_conn_err(conn, 0, 0);
1802 break;
1803 }
1804
1805 /* here, we have seen a synchronous error, no fd was allocated */
1806
1807 check->state &= ~CHK_ST_INPROGRESS;
1808 check_notify_failure(check);
1809
1810 /* we allow up to min(inter, timeout.connect) for a connection
1811 * to establish but only when timeout.check is set
1812 * as it may be to short for a full check otherwise
1813 */
1814 while (tick_is_expired(t->expire, now_ms)) {
1815 int t_con;
1816
1817 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1818 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1819
1820 if (s->proxy->timeout.check)
1821 t->expire = tick_first(t->expire, t_con);
1822 }
1823 }
1824 else {
1825 /* there was a test running.
1826 * First, let's check whether there was an uncaught error,
1827 * which can happen on connect timeout or error.
1828 */
1829 if (check->result == CHK_RES_UNKNOWN) {
1830 /* good connection is enough for pure TCP check */
1831 struct pid_list *elem = check->curpid;
1832 int status = HCHK_STATUS_UNKNOWN;
1833
1834 if (elem->exited) {
1835 status = elem->status; /* Save in case the process exits between use below */
1836 if (!WIFEXITED(status))
1837 check->code = -1;
1838 else
1839 check->code = WEXITSTATUS(status);
1840 if (!WIFEXITED(status) || WEXITSTATUS(status))
1841 status = HCHK_STATUS_PROCERR;
1842 else
1843 status = HCHK_STATUS_PROCOK;
1844 } else if (expired) {
1845 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001846 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001847 kill(elem->pid, SIGTERM);
1848 }
1849 set_server_check_status(check, status, NULL);
1850 }
1851
1852 if (check->result == CHK_RES_FAILED) {
1853 /* a failure or timeout detected */
1854 check_notify_failure(check);
1855 }
1856 else if (check->result == CHK_RES_CONDPASS) {
1857 /* check is OK but asks for stopping mode */
1858 check_notify_stopping(check);
1859 }
1860 else if (check->result == CHK_RES_PASSED) {
1861 /* a success was detected */
1862 check_notify_success(check);
1863 }
1864 check->state &= ~CHK_ST_INPROGRESS;
1865
1866 pid_list_del(check->curpid);
1867
1868 rv = 0;
1869 if (global.spread_checks > 0) {
1870 rv = srv_getinter(check) * global.spread_checks / 100;
1871 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1872 }
1873 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1874 }
1875
1876 reschedule:
1877 while (tick_is_expired(t->expire, now_ms))
1878 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1879 return t;
1880}
1881
1882/*
1883 * manages a server health-check that uses a connection. Returns
1884 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1885 */
1886static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001887{
Simon Horman4a741432013-02-23 15:35:38 +09001888 struct check *check = t->context;
1889 struct server *s = check->server;
1890 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001891 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001892 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001893 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001894
Willy Tarreau2c115e52013-12-11 19:41:16 +01001895 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001896 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001897 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001898 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001899
Simon Horman671b6f02013-11-25 10:46:39 +09001900 /* we don't send any health-checks when the proxy is
1901 * stopped, the server should not be checked or the check
1902 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001903 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001904 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001905 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001906 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001907
1908 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001909 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001910
Willy Tarreau2c115e52013-12-11 19:41:16 +01001911 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001912 check->bi->p = check->bi->data;
1913 check->bi->i = 0;
1914 check->bo->p = check->bo->data;
1915 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001916
Simon Hormanb00d17a2014-06-13 16:18:16 +09001917 ret = connect_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001918 switch (ret) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001919 case SN_ERR_UP:
1920 return t;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001921 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001922 /* we allow up to min(inter, timeout.connect) for a connection
1923 * to establish but only when timeout.check is set
1924 * as it may be to short for a full check otherwise
1925 */
Simon Horman4a741432013-02-23 15:35:38 +09001926 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001927
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001928 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1929 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1930 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001931 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001932
1933 if (check->type)
1934 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1935
Willy Tarreau5a78f362012-11-23 12:47:05 +01001936 goto reschedule;
1937
1938 case SN_ERR_SRVTO: /* ETIMEDOUT */
1939 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001940 conn->flags |= CO_FL_ERROR;
1941 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001942 break;
1943 case SN_ERR_PRXCOND:
1944 case SN_ERR_RESOURCE:
1945 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001946 conn->flags |= CO_FL_ERROR;
1947 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001948 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001949 }
1950
Willy Tarreau5a78f362012-11-23 12:47:05 +01001951 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001952
Willy Tarreau2c115e52013-12-11 19:41:16 +01001953 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02001954 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001955
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001956 /* we allow up to min(inter, timeout.connect) for a connection
1957 * to establish but only when timeout.check is set
1958 * as it may be to short for a full check otherwise
1959 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001960 while (tick_is_expired(t->expire, now_ms)) {
1961 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001962
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001963 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001964 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001965
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001966 if (s->proxy->timeout.check)
1967 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001968 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001969 }
1970 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001971 /* there was a test running.
1972 * First, let's check whether there was an uncaught error,
1973 * which can happen on connect timeout or error.
1974 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09001975 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001976 /* good connection is enough for pure TCP check */
1977 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001978 if (check->use_ssl)
1979 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001980 else
Simon Horman4a741432013-02-23 15:35:38 +09001981 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001982 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001983 else if ((conn->flags & CO_FL_ERROR) || expired) {
1984 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001985 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001986 else
1987 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001988 }
1989
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001990 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001991 if (conn->xprt) {
1992 /* The check was aborted and the connection was not yet closed.
1993 * This can happen upon timeout, or when an external event such
1994 * as a failed response coupled with "observe layer7" caused the
1995 * server state to be suddenly changed.
1996 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001997 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001998 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001999 }
2000
Willy Tarreauaf549582014-05-16 17:37:50 +02002001 if (check->result == CHK_RES_FAILED) {
2002 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002003 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002004 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002005 else if (check->result == CHK_RES_CONDPASS) {
2006 /* check is OK but asks for stopping mode */
2007 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002008 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002009 else if (check->result == CHK_RES_PASSED) {
2010 /* a success was detected */
2011 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002012 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002013 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002014
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002015 rv = 0;
2016 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002017 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002018 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002019 }
Simon Horman4a741432013-02-23 15:35:38 +09002020 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002021 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002022
2023 reschedule:
2024 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002025 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002026 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002027 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002028}
2029
Simon Horman98637e52014-06-20 12:30:16 +09002030/*
2031 * manages a server health-check. Returns
2032 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2033 */
2034static struct task *process_chk(struct task *t)
2035{
2036 struct check *check = t->context;
2037
2038 if (check->type == PR_O2_EXT_CHK)
2039 return process_chk_proc(t);
2040 return process_chk_conn(t);
2041}
2042
Simon Horman5c942422013-11-25 10:46:32 +09002043static int start_check_task(struct check *check, int mininter,
2044 int nbcheck, int srvpos)
2045{
2046 struct task *t;
2047 /* task for the check */
2048 if ((t = task_new()) == NULL) {
2049 Alert("Starting [%s:%s] check: out of memory.\n",
2050 check->server->proxy->id, check->server->id);
2051 return 0;
2052 }
2053
2054 check->task = t;
2055 t->process = process_chk;
2056 t->context = check;
2057
Willy Tarreau1746eec2014-04-25 10:46:47 +02002058 if (mininter < srv_getinter(check))
2059 mininter = srv_getinter(check);
2060
2061 if (global.max_spread_checks && mininter > global.max_spread_checks)
2062 mininter = global.max_spread_checks;
2063
Simon Horman5c942422013-11-25 10:46:32 +09002064 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002065 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002066 check->start = now;
2067 task_queue(t);
2068
2069 return 1;
2070}
2071
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002072/*
2073 * Start health-check.
2074 * Returns 0 if OK, -1 if error, and prints the error in this case.
2075 */
2076int start_checks() {
2077
2078 struct proxy *px;
2079 struct server *s;
2080 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002081 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002082
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002083 /* 1- count the checkers to run simultaneously.
2084 * We also determine the minimum interval among all of those which
2085 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2086 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002087 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002088 * too short an interval for all others.
2089 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002090 for (px = proxy; px; px = px->next) {
2091 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002092 if (s->slowstart) {
2093 if ((t = task_new()) == NULL) {
2094 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2095 return -1;
2096 }
2097 /* We need a warmup task that will be called when the server
2098 * state switches from down to up.
2099 */
2100 s->warmup = t;
2101 t->process = server_warmup;
2102 t->context = s;
2103 t->expire = TICK_ETERNITY;
2104 }
2105
Willy Tarreaud8514a22013-12-11 21:10:14 +01002106 if (s->check.state & CHK_ST_CONFIGURED) {
2107 nbcheck++;
2108 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2109 (!mininter || mininter > srv_getinter(&s->check)))
2110 mininter = srv_getinter(&s->check);
2111 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002112
Willy Tarreaud8514a22013-12-11 21:10:14 +01002113 if (s->agent.state & CHK_ST_CONFIGURED) {
2114 nbcheck++;
2115 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2116 (!mininter || mininter > srv_getinter(&s->agent)))
2117 mininter = srv_getinter(&s->agent);
2118 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002119 }
2120 }
2121
Simon Horman4a741432013-02-23 15:35:38 +09002122 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002123 return 0;
2124
2125 srand((unsigned)time(NULL));
2126
2127 /*
2128 * 2- start them as far as possible from each others. For this, we will
2129 * start them after their interval set to the min interval divided by
2130 * the number of servers, weighted by the server's position in the list.
2131 */
2132 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002133 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2134 if (init_pid_list()) {
2135 Alert("Starting [%s] check: out of memory.\n", px->id);
2136 return -1;
2137 }
2138 }
2139
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002140 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002141 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002142 if (s->check.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002143 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2144 return -1;
2145 srvpos++;
2146 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002147
Simon Hormand60d6912013-11-25 10:46:36 +09002148 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002149 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002150 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2151 return -1;
2152 }
2153 srvpos++;
2154 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002155 }
2156 }
2157 return 0;
2158}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159
2160/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002161 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002162 * The buffer MUST be terminated by a null byte before calling this function.
2163 * Sets server status appropriately. The caller is responsible for ensuring
2164 * that the buffer contains at least 13 characters. If <done> is zero, we may
2165 * return 0 to indicate that data is required to decide of a match.
2166 */
2167static int httpchk_expect(struct server *s, int done)
2168{
2169 static char status_msg[] = "HTTP status check returned code <000>";
2170 char status_code[] = "000";
2171 char *contentptr;
2172 int crlf;
2173 int ret;
2174
2175 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2176 case PR_O2_EXP_STS:
2177 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002178 memcpy(status_code, s->check.bi->data + 9, 3);
2179 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002180
2181 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2182 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2183 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002184 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002185
2186 /* we necessarily have the response, so there are no partial failures */
2187 if (s->proxy->options2 & PR_O2_EXP_INV)
2188 ret = !ret;
2189
Simon Horman4a741432013-02-23 15:35:38 +09002190 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002191 break;
2192
2193 case PR_O2_EXP_STR:
2194 case PR_O2_EXP_RSTR:
2195 /* very simple response parser: ignore CR and only count consecutive LFs,
2196 * stop with contentptr pointing to first char after the double CRLF or
2197 * to '\0' if crlf < 2.
2198 */
2199 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002200 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002201 if (crlf >= 2)
2202 break;
2203 if (*contentptr == '\r')
2204 continue;
2205 else if (*contentptr == '\n')
2206 crlf++;
2207 else
2208 crlf = 0;
2209 }
2210
2211 /* Check that response contains a body... */
2212 if (crlf < 2) {
2213 if (!done)
2214 return 0;
2215
Simon Horman4a741432013-02-23 15:35:38 +09002216 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002217 "HTTP content check could not find a response body");
2218 return 1;
2219 }
2220
2221 /* Check that response body is not empty... */
2222 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002223 if (!done)
2224 return 0;
2225
Simon Horman4a741432013-02-23 15:35:38 +09002226 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002227 "HTTP content check found empty response body");
2228 return 1;
2229 }
2230
2231 /* Check the response content against the supplied string
2232 * or regex... */
2233 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2234 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2235 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002236 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002237
2238 /* if we don't match, we may need to wait more */
2239 if (!ret && !done)
2240 return 0;
2241
2242 if (ret) {
2243 /* content matched */
2244 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002245 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002246 "HTTP check matched unwanted content");
2247 else
Simon Horman4a741432013-02-23 15:35:38 +09002248 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002249 "HTTP content check matched");
2250 }
2251 else {
2252 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002253 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002254 "HTTP check did not match unwanted content");
2255 else
Simon Horman4a741432013-02-23 15:35:38 +09002256 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002257 "HTTP content check did not match");
2258 }
2259 break;
2260 }
2261 return 1;
2262}
2263
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002264/*
2265 * return the id of a step in a send/expect session
2266 */
2267static int tcpcheck_get_step_id(struct server *s)
2268{
2269 struct tcpcheck_rule *cur = NULL, *next = NULL;
2270 int i = 0;
2271
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002272 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002273
2274 /* no step => first step */
2275 if (cur == NULL)
2276 return 1;
2277
2278 /* increment i until current step */
2279 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
2280 if (next->list.p == &cur->list)
2281 break;
2282 ++i;
2283 }
2284
2285 return i;
2286}
2287
2288static void tcpcheck_main(struct connection *conn)
2289{
2290 char *contentptr;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002291 struct list *head = NULL;
2292 struct tcpcheck_rule *cur = NULL;
2293 int done = 0, ret = 0;
2294
2295 struct check *check = conn->owner;
2296 struct server *s = check->server;
2297 struct task *t = check->task;
2298
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002299 /*
2300 * don't do anything until the connection is established but if we're running
2301 * first step which must be a connect
2302 */
2303 if (check->current_step && (!(conn->flags & CO_FL_CONNECTED))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002304 /* update expire time, should be done by process_chk */
2305 /* we allow up to min(inter, timeout.connect) for a connection
2306 * to establish but only when timeout.check is set
2307 * as it may be to short for a full check otherwise
2308 */
2309 while (tick_is_expired(t->expire, now_ms)) {
2310 int t_con;
2311
2312 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2313 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2314
2315 if (s->proxy->timeout.check)
2316 t->expire = tick_first(t->expire, t_con);
2317 }
2318 return;
2319 }
2320
2321 /* here, we know that the connection is established */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002322 if (check->result != CHK_RES_UNKNOWN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002323 goto out_end_tcpcheck;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002324
2325 /* head is be the first element of the double chained list */
2326 head = &s->proxy->tcpcheck_rules;
2327
2328 /* no step means first step
2329 * initialisation */
2330 if (check->current_step == NULL) {
2331 check->bo->p = check->bo->data;
2332 check->bo->o = 0;
2333 check->bi->p = check->bi->data;
2334 check->bi->i = 0;
2335 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2336 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2337 if (s->proxy->timeout.check)
2338 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2339 }
2340 /* keep on processing step */
2341 else {
2342 cur = check->current_step;
2343 }
2344
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002345 if (conn->flags & CO_FL_HANDSHAKE)
2346 return;
2347
2348 /* It's only the rules which will enable send/recv */
2349 __conn_data_stop_both(conn);
2350
Willy Tarreauabca5b62013-12-06 14:19:25 +01002351 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002352 /* we have to try to flush the output buffer before reading, at the end,
2353 * or if we're about to send a string that does not fit in the remaining space.
2354 */
2355 if (check->bo->o &&
2356 (&cur->list == head ||
2357 check->current_step->action != TCPCHK_ACT_SEND ||
2358 check->current_step->string_len >= buffer_total_space(check->bo))) {
2359
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002360 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002361 if (conn->flags & CO_FL_ERROR) {
2362 chk_report_conn_err(conn, errno, 0);
2363 __conn_data_stop_both(conn);
2364 goto out_end_tcpcheck;
2365 }
2366 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002367 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002368 }
2369
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002370 /* did we reach the end ? If so, let's check that everything was sent */
2371 if (&cur->list == head) {
2372 if (check->bo->o)
2373 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002374 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002375 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002376
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002377 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2378 struct protocol *proto;
2379 struct xprt_ops *xprt;
2380
2381 /* mark the step as started */
2382 check->last_started_step = check->current_step;
2383 /* first, shut existing connection */
2384 conn_force_close(conn);
2385
2386 /* prepare new connection */
2387 /* initialization */
2388 conn_init(conn);
2389 conn_attach(conn, check, &check_conn_cb);
2390 conn->target = &s->obj_type;
2391
2392 /* no client address */
2393 clear_addr(&conn->addr.from);
2394
Willy Tarreau640556c2014-05-09 23:38:15 +02002395 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002396 /* we'll connect to the check addr specified on the server */
2397 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002398 proto = s->check_common.proto;
2399 }
2400 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002401 /* we'll connect to the addr on the server */
2402 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002403 proto = s->proto;
2404 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002405
2406 /* port */
2407 if (check->current_step->port)
2408 set_host_port(&conn->addr.to, check->current_step->port);
2409 else if (check->port)
2410 set_host_port(&conn->addr.to, check->port);
2411
2412#ifdef USE_OPENSSL
2413 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2414 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002415 }
2416 else {
2417 xprt = &raw_sock;
2418 }
2419#else /* USE_OPENSSL */
2420 xprt = &raw_sock;
2421#endif /* USE_OPENSSL */
2422 conn_prepare(conn, proto, xprt);
2423
2424 ret = SN_ERR_INTERNAL;
2425 if (proto->connect)
2426 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
2427 conn->flags |= CO_FL_WAKE_DATA;
2428 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2429 conn->send_proxy_ofs = 1;
2430 conn->flags |= CO_FL_SEND_PROXY;
2431 }
2432
2433 /* It can return one of :
2434 * - SN_ERR_NONE if everything's OK
2435 * - SN_ERR_SRVTO if there are no more servers
2436 * - SN_ERR_SRVCL if the connection was refused by the server
2437 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2438 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2439 * - SN_ERR_INTERNAL for any other purely internal errors
2440 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2441 * Note that we try to prevent the network stack from sending the ACK during the
2442 * connect() when a pure TCP check is used (without PROXY protocol).
2443 */
2444 switch (ret) {
2445 case SN_ERR_NONE:
2446 /* we allow up to min(inter, timeout.connect) for a connection
2447 * to establish but only when timeout.check is set
2448 * as it may be to short for a full check otherwise
2449 */
2450 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2451
2452 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2453 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2454 t->expire = tick_first(t->expire, t_con);
2455 }
2456 break;
2457 case SN_ERR_SRVTO: /* ETIMEDOUT */
2458 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2459 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2460 tcpcheck_get_step_id(s), strerror(errno));
2461 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2462 goto out_end_tcpcheck;
2463 case SN_ERR_PRXCOND:
2464 case SN_ERR_RESOURCE:
2465 case SN_ERR_INTERNAL:
2466 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2467 tcpcheck_get_step_id(s));
2468 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2469 goto out_end_tcpcheck;
2470 }
2471
2472 /* allow next rule */
2473 cur = (struct tcpcheck_rule *)cur->list.n;
2474 check->current_step = cur;
2475
2476 /* don't do anything until the connection is established */
2477 if (!(conn->flags & CO_FL_CONNECTED)) {
2478 /* update expire time, should be done by process_chk */
2479 /* we allow up to min(inter, timeout.connect) for a connection
2480 * to establish but only when timeout.check is set
2481 * as it may be to short for a full check otherwise
2482 */
2483 while (tick_is_expired(t->expire, now_ms)) {
2484 int t_con;
2485
2486 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2487 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2488
2489 if (s->proxy->timeout.check)
2490 t->expire = tick_first(t->expire, t_con);
2491 }
2492 return;
2493 }
2494
2495 } /* end 'connect' */
2496 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2497 /* mark the step as started */
2498 check->last_started_step = check->current_step;
2499
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002500 /* reset the read buffer */
2501 if (*check->bi->data != '\0') {
2502 *check->bi->data = '\0';
2503 check->bi->i = 0;
2504 }
2505
2506 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2507 conn->flags |= CO_FL_ERROR;
2508 chk_report_conn_err(conn, 0, 0);
2509 goto out_end_tcpcheck;
2510 }
2511
Willy Tarreauabca5b62013-12-06 14:19:25 +01002512 if (check->current_step->string_len >= check->bo->size) {
2513 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2514 check->current_step->string_len, check->bo->size,
2515 tcpcheck_get_step_id(s));
2516 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2517 goto out_end_tcpcheck;
2518 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002519
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002520 /* do not try to send if there is no space */
2521 if (check->current_step->string_len >= buffer_total_space(check->bo))
2522 continue;
2523
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002524 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2525 *check->bo->p = '\0'; /* to make gdb output easier to read */
2526
Willy Tarreauabca5b62013-12-06 14:19:25 +01002527 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002528 cur = (struct tcpcheck_rule *)cur->list.n;
2529 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002530 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002531 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002532 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002533 goto out_end_tcpcheck;
2534
Willy Tarreau310987a2014-01-22 19:46:33 +01002535 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002536 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2537 done = 1;
2538 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2539 /* Report network errors only if we got no other data. Otherwise
2540 * we'll let the upper layers decide whether the response is OK
2541 * or not. It is very common that an RST sent by the server is
2542 * reported as an error just after the last data chunk.
2543 */
2544 chk_report_conn_err(conn, errno, 0);
2545 goto out_end_tcpcheck;
2546 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002547 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002548 else
2549 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002550 }
2551
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002552 /* mark the step as started */
2553 check->last_started_step = check->current_step;
2554
2555
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002556 /* Intermediate or complete response received.
2557 * Terminate string in check->bi->data buffer.
2558 */
2559 if (check->bi->i < check->bi->size) {
2560 check->bi->data[check->bi->i] = '\0';
2561 }
2562 else {
2563 check->bi->data[check->bi->i - 1] = '\0';
2564 done = 1; /* buffer full, don't wait for more data */
2565 }
2566
2567 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002568
2569 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002570 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002571 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002572 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002573
2574 /* empty response */
2575 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2576 tcpcheck_get_step_id(s));
2577 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2578
2579 goto out_end_tcpcheck;
2580 }
2581
2582 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002583 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002584
Willy Tarreaua970c282013-12-06 12:47:19 +01002585 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002586 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002587 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002588 else if (cur->expect_regex != NULL)
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002589 ret = regex_exec(cur->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002590
2591 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002592 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002593
2594 /* matched */
2595 if (ret) {
2596 /* matched but we did not want to => ERROR */
2597 if (cur->inverse) {
2598 /* we were looking for a string */
2599 if (cur->string != NULL) {
2600 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2601 cur->string, tcpcheck_get_step_id(s));
2602 }
2603 else {
2604 /* we were looking for a regex */
2605 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2606 tcpcheck_get_step_id(s));
2607 }
2608 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2609 goto out_end_tcpcheck;
2610 }
2611 /* matched and was supposed to => OK, next step */
2612 else {
2613 cur = (struct tcpcheck_rule*)cur->list.n;
2614 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002615 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002616 goto tcpcheck_expect;
2617 __conn_data_stop_recv(conn);
2618 }
2619 }
2620 else {
2621 /* not matched */
2622 /* not matched and was not supposed to => OK, next step */
2623 if (cur->inverse) {
2624 cur = (struct tcpcheck_rule*)cur->list.n;
2625 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002626 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002627 goto tcpcheck_expect;
2628 __conn_data_stop_recv(conn);
2629 }
2630 /* not matched but was supposed to => ERROR */
2631 else {
2632 /* we were looking for a string */
2633 if (cur->string != NULL) {
2634 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2635 cur->string, tcpcheck_get_step_id(s));
2636 }
2637 else {
2638 /* we were looking for a regex */
2639 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2640 tcpcheck_get_step_id(s));
2641 }
2642 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2643 goto out_end_tcpcheck;
2644 }
2645 }
2646 } /* end expect */
2647 } /* end loop over double chained step list */
2648
2649 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2650 goto out_end_tcpcheck;
2651
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002652 out_need_io:
2653 if (check->bo->o)
2654 __conn_data_want_send(conn);
2655
2656 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2657 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002658 return;
2659
2660 out_end_tcpcheck:
2661 /* collect possible new errors */
2662 if (conn->flags & CO_FL_ERROR)
2663 chk_report_conn_err(conn, 0, 0);
2664
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002665 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002666 check->current_step = NULL;
2667
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002668 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002669 conn->flags |= CO_FL_ERROR;
2670
2671 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002672
2673 return;
2674}
2675
2676
Willy Tarreaubd741542010-03-16 18:46:54 +01002677/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002678 * Local variables:
2679 * c-indent-level: 8
2680 * c-basic-offset: 8
2681 * End:
2682 */