blob: 15a3c40a906982abcecd82cf1bb38750ab9f7549 [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" },
Cyril Bonté77010d82014-08-07 01:55:37 +020096 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "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 Tarreau213c6782014-10-02 14:51:02 +0200589 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100590
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100591 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100592 return;
593
594 errno = errno_bck;
595 if (!errno || errno == EAGAIN)
596 retrieve_errno_from_socket(conn);
597
598 if (!(conn->flags & CO_FL_ERROR) && !expired)
599 return;
600
601 /* we'll try to build a meaningful error message depending on the
602 * context of the error possibly present in conn->err_code, and the
603 * socket error possibly collected above. This is useful to know the
604 * exact step of the L6 layer (eg: SSL handshake).
605 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200606 chk = get_trash_chunk();
607
608 if (check->type == PR_O2_TCPCHK_CHK) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200609 step = tcpcheck_get_step_id(check->server);
610 if (!step)
611 chunk_printf(chk, " at initial connection step of tcp-check");
612 else {
613 chunk_printf(chk, " at step %d of tcp-check", step);
614 /* we were looking for a string */
615 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
616 if (check->last_started_step->port)
617 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
618 else
619 chunk_appendf(chk, " (connect)");
620 }
621 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
622 if (check->last_started_step->string)
623 chunk_appendf(chk, " (string '%s')", check->last_started_step->string);
624 else if (check->last_started_step->expect_regex)
625 chunk_appendf(chk, " (expect regex)");
626 }
627 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
628 chunk_appendf(chk, " (send)");
629 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200630 }
631 }
632
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100633 if (conn->err_code) {
634 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200635 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100636 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200637 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100638 err_msg = trash.str;
639 }
640 else {
641 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200642 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100643 err_msg = trash.str;
644 }
645 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200646 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100647 }
648 }
649
650 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
651 /* L4 not established (yet) */
652 if (conn->flags & CO_FL_ERROR)
653 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
654 else if (expired)
655 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
656 }
657 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
658 /* L6 not established (yet) */
659 if (conn->flags & CO_FL_ERROR)
660 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
661 else if (expired)
662 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
663 }
664 else if (conn->flags & CO_FL_ERROR) {
665 /* I/O error after connection was established and before we could diagnose */
666 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
667 }
668 else if (expired) {
669 /* connection established but expired check */
670 if (check->type == PR_O2_SSL3_CHK)
671 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
672 else /* HTTP, SMTP, ... */
673 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
674 }
675
676 return;
677}
678
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679/*
680 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200681 * the connection acknowledgement. If the proxy requires L7 health-checks,
682 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900683 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200685static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686{
Simon Horman4a741432013-02-23 15:35:38 +0900687 struct check *check = conn->owner;
688 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900689 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200690
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100691 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100692 goto out_wakeup;
693
Willy Tarreau310987a2014-01-22 19:46:33 +0100694 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100695 return;
696
Willy Tarreau20a18342013-12-05 00:31:46 +0100697 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100698 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100699 __conn_data_stop_both(conn);
700 goto out_wakeup;
701 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100702
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100703 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100704 /* if the output is closed, we can't do anything */
705 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100706 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100707 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200708 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200709
Willy Tarreau06559ac2013-12-05 01:53:08 +0100710 /* here, we know that the connection is established. That's enough for
711 * a pure TCP check.
712 */
713 if (!check->type)
714 goto out_wakeup;
715
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200716 if (check->type == PR_O2_TCPCHK_CHK) {
717 tcpcheck_main(conn);
718 return;
719 }
720
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100721 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100722 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100723 if (conn->flags & CO_FL_ERROR) {
724 chk_report_conn_err(conn, errno, 0);
725 __conn_data_stop_both(conn);
726 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100728 if (check->bo->o)
729 return;
730 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200731
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100732 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
733 if (s->proxy->timeout.check) {
734 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
735 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200736 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100737 goto out_nowake;
738
Willy Tarreau83749182007-04-15 20:56:27 +0200739 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200740 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200741 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200742 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743}
744
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200746 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200747 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900748 * set_server_check_status() to update check->status, check->duration
749 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200750
751 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
752 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
753 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
754 * response to an SSL HELLO (the principle is that this is enough to
755 * distinguish between an SSL server and a pure TCP relay). All other cases will
756 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
757 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200759static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760{
Simon Horman4a741432013-02-23 15:35:38 +0900761 struct check *check = conn->owner;
762 struct server *s = check->server;
763 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200764 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100765 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200766 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200767
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100768 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200769 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200770
Willy Tarreau310987a2014-01-22 19:46:33 +0100771 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200772 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200773
774 if (check->type == PR_O2_TCPCHK_CHK) {
775 tcpcheck_main(conn);
776 return;
777 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200778
Willy Tarreau83749182007-04-15 20:56:27 +0200779 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
780 * but the connection was closed on the remote end. Fortunately, recv still
781 * works correctly and we don't need to do the getsockopt() on linux.
782 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000783
784 /* Set buffer to point to the end of the data already read, and check
785 * that there is free space remaining. If the buffer is full, proceed
786 * with running the checks without attempting another socket read.
787 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000788
Willy Tarreau03938182010-03-17 21:52:07 +0100789 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000790
Simon Horman4a741432013-02-23 15:35:38 +0900791 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200792 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100793 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900794 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200795 /* Report network errors only if we got no other data. Otherwise
796 * we'll let the upper layers decide whether the response is OK
797 * or not. It is very common that an RST sent by the server is
798 * reported as an error just after the last data chunk.
799 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100800 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100801 goto out_wakeup;
802 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 }
804
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100805
Willy Tarreau03938182010-03-17 21:52:07 +0100806 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900807 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100808 */
Simon Horman4a741432013-02-23 15:35:38 +0900809 if (check->bi->i < check->bi->size)
810 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100811 else {
Simon Horman4a741432013-02-23 15:35:38 +0900812 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100813 done = 1; /* buffer full, don't wait for more data */
814 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200815
Nick Chalk57b1bf72010-03-16 15:50:46 +0000816 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900817 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200818 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900819 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100820 goto wait_more_data;
821
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100822 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900823 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
824 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
825 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
826 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
827 !isdigit((unsigned char) *(check->bi->data + 11))) {
828 cut_crlf(check->bi->data);
829 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200830
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100831 goto out_wakeup;
832 }
833
Simon Horman4a741432013-02-23 15:35:38 +0900834 check->code = str2uic(check->bi->data + 9);
835 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000836
Willy Tarreaubd741542010-03-16 18:46:54 +0100837 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200838 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000839 /* 404 may be accepted as "stopping" only if the server was up */
840 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900841 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000842 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100843 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
844 /* Run content verification check... We know we have at least 13 chars */
845 if (!httpchk_expect(s, done))
846 goto wait_more_data;
847 }
848 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900849 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100850 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900851 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100852 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000853 else {
854 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900855 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000856 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200857 break;
858
859 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900860 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100861 goto wait_more_data;
862
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100863 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900864 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
865 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200866 else
Simon Horman4a741432013-02-23 15:35:38 +0900867 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200868 break;
869
870 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900871 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100872 goto wait_more_data;
873
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200874 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900875 if ((check->bi->i < strlen("000\r")) ||
876 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
877 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
878 !isdigit((unsigned char) *(check->bi->data + 2))) {
879 cut_crlf(check->bi->data);
880 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200881
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200882 goto out_wakeup;
883 }
884
Simon Horman4a741432013-02-23 15:35:38 +0900885 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200886
Simon Horman4a741432013-02-23 15:35:38 +0900887 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200888 cut_crlf(desc);
889
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100890 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900891 if (*check->bi->data == '2')
892 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200893 else
Simon Horman4a741432013-02-23 15:35:38 +0900894 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200895 break;
896
Simon Hormana2b9dad2013-02-12 10:45:54 +0900897 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100898 int status = HCHK_STATUS_CHECKED;
899 const char *hs = NULL; /* health status */
900 const char *as = NULL; /* admin status */
901 const char *ps = NULL; /* performance status */
902 const char *err = NULL; /* first error to report */
903 const char *wrn = NULL; /* first warning to report */
904 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900905
Willy Tarreau81f5d942013-12-09 20:51:51 +0100906 /* We're getting an agent check response. The agent could
907 * have been disabled in the mean time with a long check
908 * still pending. It is important that we ignore the whole
909 * response.
910 */
911 if (!(check->server->agent.state & CHK_ST_ENABLED))
912 break;
913
914 /* The agent supports strings made of a single line ended by the
915 * first CR ('\r') or LF ('\n'). This line is composed of words
916 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
917 * line may optionally contained a description of a state change
918 * after a sharp ('#'), which is only considered if a health state
919 * is announced.
920 *
921 * Words may be composed of :
922 * - a numeric weight suffixed by the percent character ('%').
923 * - a health status among "up", "down", "stopped", and "fail".
924 * - an admin status among "ready", "drain", "maint".
925 *
926 * These words may appear in any order. If multiple words of the
927 * same category appear, the last one wins.
928 */
929
Willy Tarreau9809b782013-12-11 21:40:11 +0100930 p = check->bi->data;
931 while (*p && *p != '\n' && *p != '\r')
932 p++;
933
934 if (!*p) {
935 if (!done)
936 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900937
Willy Tarreau9809b782013-12-11 21:40:11 +0100938 /* at least inform the admin that the agent is mis-behaving */
939 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
940 break;
941 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100942
Willy Tarreau9809b782013-12-11 21:40:11 +0100943 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100944 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900945
Willy Tarreau81f5d942013-12-09 20:51:51 +0100946 while (*cmd) {
947 /* look for next word */
948 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
949 cmd++;
950 continue;
951 }
Simon Horman671b6f02013-11-25 10:46:39 +0900952
Willy Tarreau81f5d942013-12-09 20:51:51 +0100953 if (*cmd == '#') {
954 /* this is the beginning of a health status description,
955 * skip the sharp and blanks.
956 */
957 cmd++;
958 while (*cmd == '\t' || *cmd == ' ')
959 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900960 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900961 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100962
963 /* find the end of the word so that we have a null-terminated
964 * word between <cmd> and <p>.
965 */
966 p = cmd + 1;
967 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
968 p++;
969 if (*p)
970 *p++ = 0;
971
972 /* first, health statuses */
973 if (strcasecmp(cmd, "up") == 0) {
974 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900975 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100976 hs = cmd;
977 }
978 else if (strcasecmp(cmd, "down") == 0) {
979 check->health = 0;
980 status = HCHK_STATUS_L7STS;
981 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900982 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100983 else if (strcasecmp(cmd, "stopped") == 0) {
984 check->health = 0;
985 status = HCHK_STATUS_L7STS;
986 hs = cmd;
987 }
988 else if (strcasecmp(cmd, "fail") == 0) {
989 check->health = 0;
990 status = HCHK_STATUS_L7STS;
991 hs = cmd;
992 }
993 /* admin statuses */
994 else if (strcasecmp(cmd, "ready") == 0) {
995 as = cmd;
996 }
997 else if (strcasecmp(cmd, "drain") == 0) {
998 as = cmd;
999 }
1000 else if (strcasecmp(cmd, "maint") == 0) {
1001 as = cmd;
1002 }
1003 /* else try to parse a weight here and keep the last one */
1004 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1005 ps = cmd;
1006 }
1007 else {
1008 /* keep a copy of the first error */
1009 if (!err)
1010 err = cmd;
1011 }
1012 /* skip to next word */
1013 cmd = p;
1014 }
1015 /* here, cmd points either to \0 or to the beginning of a
1016 * description. Skip possible leading spaces.
1017 */
1018 while (*cmd == ' ' || *cmd == '\n')
1019 cmd++;
1020
1021 /* First, update the admin status so that we avoid sending other
1022 * possibly useless warnings and can also update the health if
1023 * present after going back up.
1024 */
1025 if (as) {
1026 if (strcasecmp(as, "drain") == 0)
1027 srv_adm_set_drain(check->server);
1028 else if (strcasecmp(as, "maint") == 0)
1029 srv_adm_set_maint(check->server);
1030 else
1031 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001032 }
1033
Willy Tarreau81f5d942013-12-09 20:51:51 +01001034 /* now change weights */
1035 if (ps) {
1036 const char *msg;
1037
1038 msg = server_parse_weight_change_request(s, ps);
1039 if (!wrn || !*wrn)
1040 wrn = msg;
1041 }
1042
1043 /* and finally health status */
1044 if (hs) {
1045 /* We'll report some of the warnings and errors we have
1046 * here. Down reports are critical, we leave them untouched.
1047 * Lack of report, or report of 'UP' leaves the room for
1048 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001049 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001050 const char *msg = cmd;
1051 struct chunk *t;
1052
1053 if (!*msg || status == HCHK_STATUS_L7OKD) {
1054 if (err && *err)
1055 msg = err;
1056 else if (wrn && *wrn)
1057 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001058 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001059
1060 t = get_trash_chunk();
1061 chunk_printf(t, "via agent : %s%s%s%s",
1062 hs, *msg ? " (" : "",
1063 msg, *msg ? ")" : "");
1064
1065 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001066 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001067 else if (err && *err) {
1068 /* No status change but we'd like to report something odd.
1069 * Just report the current state and copy the message.
1070 */
1071 chunk_printf(&trash, "agent reports an error : %s", err);
1072 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001073
Willy Tarreau81f5d942013-12-09 20:51:51 +01001074 }
1075 else if (wrn && *wrn) {
1076 /* No status change but we'd like to report something odd.
1077 * Just report the current state and copy the message.
1078 */
1079 chunk_printf(&trash, "agent warns : %s", wrn);
1080 set_server_check_status(check, status/*check->status*/, trash.str);
1081 }
1082 else
1083 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001084 break;
1085 }
1086
Willy Tarreau1620ec32011-08-06 17:05:02 +02001087 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001088 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001089 goto wait_more_data;
1090
Simon Horman4a741432013-02-23 15:35:38 +09001091 if (check->bi->data[0] == 'R') {
1092 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001093 }
1094 else {
Simon Horman4a741432013-02-23 15:35:38 +09001095 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1096 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001097 else
1098 desc = "PostgreSQL unknown error";
1099
Simon Horman4a741432013-02-23 15:35:38 +09001100 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001101 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001102 break;
1103
1104 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001105 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001106 goto wait_more_data;
1107
Simon Horman4a741432013-02-23 15:35:38 +09001108 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1109 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001110 }
1111 else {
Simon Horman4a741432013-02-23 15:35:38 +09001112 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001113 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001114 break;
1115
1116 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001117 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001118 goto wait_more_data;
1119
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001120 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001121 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001122 /* We set the MySQL Version in description for information purpose
1123 * FIXME : it can be cool to use MySQL Version for other purpose,
1124 * like mark as down old MySQL server.
1125 */
Simon Horman4a741432013-02-23 15:35:38 +09001126 if (check->bi->i > 51) {
1127 desc = ltrim(check->bi->data + 5, ' ');
1128 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001129 }
1130 else {
1131 if (!done)
1132 goto wait_more_data;
1133 /* it seems we have a OK packet but without a valid length,
1134 * it must be a protocol error
1135 */
Simon Horman4a741432013-02-23 15:35:38 +09001136 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001137 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001138 }
1139 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001140 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001141 desc = ltrim(check->bi->data + 7, ' ');
1142 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001143 }
1144 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001145 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1146 (((unsigned int) *(check->bi->data + 1)) << 8) +
1147 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001148
Simon Horman4a741432013-02-23 15:35:38 +09001149 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001150 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001151 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001152 /* We have only one MySQL packet and it is a Handshake Initialization packet
1153 * but we need to have a second packet to know if it is alright
1154 */
Simon Horman4a741432013-02-23 15:35:38 +09001155 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001156 goto wait_more_data;
1157 }
1158 else {
1159 /* We have only one packet and it is an Error packet,
1160 * an error message is attached, so we can display it
1161 */
Simon Horman4a741432013-02-23 15:35:38 +09001162 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001163 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001164 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001165 }
Simon Horman4a741432013-02-23 15:35:38 +09001166 } else if (check->bi->i > first_packet_len + 4) {
1167 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1168 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1169 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001170
Simon Horman4a741432013-02-23 15:35:38 +09001171 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001172 /* We have 2 packets and that's good */
1173 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001174 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001175 /* No error packet */
1176 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001177 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001178 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001179 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001180 }
1181 else {
1182 /* An error message is attached in the Error packet
1183 * so we can display it ! :)
1184 */
Simon Horman4a741432013-02-23 15:35:38 +09001185 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001186 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001187 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001188 }
1189 }
1190 }
1191 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001192 if (!done)
1193 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001194 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001195 * it must be a protocol error
1196 */
Simon Horman4a741432013-02-23 15:35:38 +09001197 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001198 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001199 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001200 }
1201 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001202 break;
1203
1204 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001205 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001206 goto wait_more_data;
1207
1208 /* Check if the server speaks LDAP (ASN.1/BER)
1209 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1210 * http://tools.ietf.org/html/rfc4511
1211 */
1212
1213 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1214 * LDAPMessage: 0x30: SEQUENCE
1215 */
Simon Horman4a741432013-02-23 15:35:38 +09001216 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1217 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001218 }
1219 else {
1220 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001221 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001222
1223 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1224 * messageID: 0x02 0x01 0x01: INTEGER 1
1225 * protocolOp: 0x61: bindResponse
1226 */
1227 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001228 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1229 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001230
1231 goto out_wakeup;
1232 }
1233
1234 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001235 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001236
1237 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1238 * ldapResult: 0x0a 0x01: ENUMERATION
1239 */
1240 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001241 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1242 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001243
1244 goto out_wakeup;
1245 }
1246
1247 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1248 * resultCode
1249 */
Simon Horman4a741432013-02-23 15:35:38 +09001250 check->code = *(check->bi->data + msglen + 9);
1251 if (check->code) {
1252 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 +02001253 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001254 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001255 }
1256 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001257 break;
1258
1259 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001260 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001261 break;
1262 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001263
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001264 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001265 /* collect possible new errors */
1266 if (conn->flags & CO_FL_ERROR)
1267 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268
Nick Chalk57b1bf72010-03-16 15:50:46 +00001269 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001270 *check->bi->data = '\0';
1271 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001272
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001273 /* Close the connection... We absolutely want to perform a hard close
1274 * and reset the connection if some data are pending, otherwise we end
1275 * up with many TIME_WAITs and eat all the source port range quickly.
1276 * To avoid sending RSTs all the time, we first try to drain pending
1277 * data.
1278 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001279 if (conn->xprt && conn->xprt->shutw)
1280 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001281
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001282 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001283 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001284 conn->flags |= CO_FL_ERROR;
1285
Willy Tarreaua522f802012-11-23 08:56:35 +01001286 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001287 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001288 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001289
1290 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001291 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001292}
1293
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001294/*
1295 * This function is used only for server health-checks. It handles connection
1296 * status updates including errors. If necessary, it wakes the check task up.
1297 * It always returns 0.
1298 */
1299static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001300{
Simon Horman4a741432013-02-23 15:35:38 +09001301 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001302
Willy Tarreau6c560da2012-11-24 11:14:45 +01001303 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001304 /* We may get error reports bypassing the I/O handlers, typically
1305 * the case when sending a pure TCP check which fails, then the I/O
1306 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001307 * main processing task so let's simply wake it up. If we get here,
1308 * we expect errno to still be valid.
1309 */
1310 chk_report_conn_err(conn, errno, 0);
1311
Willy Tarreau2d351b62013-12-05 02:36:25 +01001312 __conn_data_stop_both(conn);
1313 task_wakeup(check->task, TASK_WOKEN_IO);
1314 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001315 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1316 /* we may get here if only a connection probe was required : we
1317 * don't have any data to send nor anything expected in response,
1318 * so the completion of the connection establishment is enough.
1319 */
1320 task_wakeup(check->task, TASK_WOKEN_IO);
1321 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001322
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001323 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001324 /* We're here because nobody wants to handle the error, so we
1325 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001326 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001327 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001328 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001329 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001330 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001331}
1332
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001333struct data_cb check_conn_cb = {
1334 .recv = event_srv_chk_r,
1335 .send = event_srv_chk_w,
1336 .wake = wake_srv_chk,
1337};
1338
Willy Tarreaubaaee002006-06-26 02:48:02 +02001339/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001340 * updates the server's weight during a warmup stage. Once the final weight is
1341 * reached, the task automatically stops. Note that any server status change
1342 * must have updated s->last_change accordingly.
1343 */
1344static struct task *server_warmup(struct task *t)
1345{
1346 struct server *s = t->context;
1347
1348 /* by default, plan on stopping the task */
1349 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001350 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001351 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001352 return t;
1353
Willy Tarreau892337c2014-05-13 23:41:20 +02001354 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001355 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001356
1357 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001358 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001359
1360 /* get back there in 1 second or 1/20th of the slowstart interval,
1361 * whichever is greater, resulting in small 5% steps.
1362 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001363 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001364 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1365 return t;
1366}
1367
1368/*
Simon Horman98637e52014-06-20 12:30:16 +09001369 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001370 *
1371 * It can return one of :
1372 * - SN_ERR_NONE if everything's OK and tcpcheck_main() was not called
1373 * - SN_ERR_UP if if everything's OK and tcpcheck_main() was called
1374 * - SN_ERR_SRVTO if there are no more servers
1375 * - SN_ERR_SRVCL if the connection was refused by the server
1376 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1377 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1378 * - SN_ERR_INTERNAL for any other purely internal errors
1379 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1380 * Note that we try to prevent the network stack from sending the ACK during the
1381 * connect() when a pure TCP check is used (without PROXY protocol).
1382 */
Simon Horman98637e52014-06-20 12:30:16 +09001383static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001384{
1385 struct check *check = t->context;
1386 struct server *s = check->server;
1387 struct connection *conn = check->conn;
1388 struct protocol *proto;
1389 int ret;
1390
1391 /* tcpcheck send/expect initialisation */
1392 if (check->type == PR_O2_TCPCHK_CHK)
1393 check->current_step = NULL;
1394
1395 /* prepare the check buffer.
1396 * This should not be used if check is the secondary agent check
1397 * of a server as s->proxy->check_req will relate to the
1398 * configuration of the primary check. Similarly, tcp-check uses
1399 * its own strings.
1400 */
1401 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1402 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1403
1404 /* we want to check if this host replies to HTTP or SSLv3 requests
1405 * so we'll send the request, and won't wake the checker up now.
1406 */
1407 if ((check->type) == PR_O2_SSL3_CHK) {
1408 /* SSL requires that we put Unix time in the request */
1409 int gmt_time = htonl(date.tv_sec);
1410 memcpy(check->bo->data + 11, &gmt_time, 4);
1411 }
1412 else if ((check->type) == PR_O2_HTTP_CHK) {
1413 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1414 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
1415 bo_putstr(check->bo, "\r\n");
1416 *check->bo->p = '\0'; /* to make gdb output easier to read */
1417 }
1418 }
1419
1420 /* prepare a new connection */
1421 conn_init(conn);
1422 conn_prepare(conn, s->check_common.proto, s->check_common.xprt);
1423 conn_attach(conn, check, &check_conn_cb);
1424 conn->target = &s->obj_type;
1425
1426 /* no client address */
1427 clear_addr(&conn->addr.from);
1428
1429 if (is_addr(&s->check_common.addr)) {
1430
1431 /* we'll connect to the check addr specified on the server */
1432 conn->addr.to = s->check_common.addr;
1433 proto = s->check_common.proto;
1434 }
1435 else {
1436 /* we'll connect to the addr on the server */
1437 conn->addr.to = s->addr;
1438 proto = s->proto;
1439 }
1440
1441 if (check->port) {
1442 set_host_port(&conn->addr.to, check->port);
1443 }
1444
1445 if (check->type == PR_O2_TCPCHK_CHK) {
1446 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1447 /* if first step is a 'connect', then tcpcheck_main must run it */
1448 if (r->action == TCPCHK_ACT_CONNECT) {
1449 tcpcheck_main(conn);
1450 return SN_ERR_UP;
1451 }
1452 }
1453
1454 ret = SN_ERR_INTERNAL;
1455 if (proto->connect)
1456 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
1457 conn->flags |= CO_FL_WAKE_DATA;
1458 if (s->check.send_proxy) {
1459 conn->send_proxy_ofs = 1;
1460 conn->flags |= CO_FL_SEND_PROXY;
1461 }
1462
1463 return ret;
1464}
1465
Simon Horman98637e52014-06-20 12:30:16 +09001466static struct list pid_list = LIST_HEAD_INIT(pid_list);
1467static struct pool_head *pool2_pid_list;
1468
1469void block_sigchld(void)
1470{
1471 sigset_t set;
1472 sigemptyset(&set);
1473 sigaddset(&set, SIGCHLD);
1474 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1475}
1476
1477void unblock_sigchld(void)
1478{
1479 sigset_t set;
1480 sigemptyset(&set);
1481 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1482}
1483
1484/* Call with SIGCHLD blocked */
1485static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1486{
1487 struct pid_list *elem;
1488 struct check *check = t->context;
1489
1490 elem = pool_alloc2(pool2_pid_list);
1491 if (!elem)
1492 return NULL;
1493 elem->pid = pid;
1494 elem->t = t;
1495 elem->exited = 0;
1496 check->curpid = elem;
1497 LIST_INIT(&elem->list);
1498 LIST_ADD(&pid_list, &elem->list);
1499 return elem;
1500}
1501
1502/* Blocks blocks and then unblocks SIGCHLD */
1503static void pid_list_del(struct pid_list *elem)
1504{
1505 struct check *check;
1506
1507 if (!elem)
1508 return;
1509
1510 block_sigchld();
1511 LIST_DEL(&elem->list);
1512 unblock_sigchld();
1513 if (!elem->exited)
1514 kill(elem->pid, SIGTERM);
1515
1516 check = elem->t->context;
1517 check->curpid = NULL;
1518 pool_free2(pool2_pid_list, elem);
1519}
1520
1521/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1522static void pid_list_expire(pid_t pid, int status)
1523{
1524 struct pid_list *elem;
1525
1526 list_for_each_entry(elem, &pid_list, list) {
1527 if (elem->pid == pid) {
1528 elem->t->expire = now_ms;
1529 elem->status = status;
1530 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001531 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001532 return;
1533 }
1534 }
1535}
1536
1537static void sigchld_handler(int signal)
1538{
1539 pid_t pid;
1540 int status;
1541 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1542 pid_list_expire(pid, status);
1543}
1544
1545static int init_pid_list(void) {
1546 struct sigaction action = {
1547 .sa_handler = sigchld_handler,
1548 .sa_flags = SA_NOCLDSTOP
1549 };
1550
1551 if (pool2_pid_list != NULL)
1552 /* Nothing to do */
1553 return 0;
1554
1555 if (sigaction(SIGCHLD, &action, NULL)) {
1556 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1557 strerror(errno));
1558 return 1;
1559 }
1560
1561 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1562 if (pool2_pid_list == NULL) {
1563 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1564 strerror(errno));
1565 return 1;
1566 }
1567
1568 return 0;
1569}
1570
1571
1572static int prepare_external_check(struct check *check)
1573{
1574 struct server *s = check->server;
1575 struct proxy *px = s->proxy;
1576 struct listener *listener = NULL, *l;
1577 int i;
1578 const char *err_fmt = "Starting [%s:%s] check: out of memory.\n";
1579 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
1580 char host[46];
1581 char serv[6];
1582
1583 list_for_each_entry(l, &px->conf.listeners, by_fe)
1584 /* Use the first INET, INET6 or UNIX listener */
1585 if (l->addr.ss_family == AF_INET ||
1586 l->addr.ss_family == AF_INET6 ||
1587 l->addr.ss_family == AF_UNIX) {
1588 listener = l;
1589 break;
1590 }
1591
1592 if (!listener) {
1593 err_fmt = "Starting [%s:%s] check: no listener.\n";
1594 goto err;
1595 }
1596
1597 check->curpid = NULL;
1598
1599 check->envp = calloc(2, sizeof(check->argv));
1600 if (!check->envp)
1601 goto err;
1602 check->envp[0] = malloc(strlen("PATH=") + strlen(path) + 1);
1603 if (!check->envp[0])
1604 goto err;
1605 strcpy(check->envp[0], "PATH=");
1606 strcpy(check->envp[0] + strlen(check->envp[0]), path);
1607 check->envp[1] = NULL;
1608
1609 check->argv = calloc(6, sizeof(check->argv));
1610 if (!check->argv)
1611 goto err;
1612
1613 check->argv[0] = px->check_command;
1614
1615 if (listener->addr.ss_family == AF_INET ||
1616 listener->addr.ss_family == AF_INET6) {
1617 addr_to_str(&listener->addr, host, sizeof(host));
1618 check->argv[1] = strdup(host);
1619 port_to_str(&listener->addr, serv, sizeof(serv));
1620 check->argv[2] = strdup(serv);
1621 } else if (listener->addr.ss_family == AF_UNIX) {
1622 const struct sockaddr_un *un;
1623
1624 un = (struct sockaddr_un *)&listener->addr;
1625 check->argv[1] = strdup(un->sun_path);
1626 check->argv[2] = strdup("NOT_USED");
1627 } else {
1628 goto err;
1629 }
1630
1631 addr_to_str(&s->addr, host, sizeof(host));
1632 check->argv[3] = strdup(host);
1633 port_to_str(&s->addr, serv, sizeof(serv));
1634 check->argv[4] = strdup(serv);
1635
1636 for (i = 0; i < 5; i++)
1637 if (!check->argv[i])
1638 goto err;
1639
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001640 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001641err:
1642 if (check->envp) {
1643 free(check->envp[1]);
1644 free(check->envp);
1645 check->envp = NULL;
1646 }
1647
1648 if (check->argv) {
1649 for (i = 1; i < 5; i++)
1650 free(check->argv[i]);
1651 free(check->argv);
1652 check->argv = NULL;
1653 }
1654 Alert(err_fmt, px->id, s->id);
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001655 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001656}
1657
Simon Hormanb00d17a2014-06-13 16:18:16 +09001658/*
Simon Horman98637e52014-06-20 12:30:16 +09001659 * establish a server health-check that makes use of a process.
1660 *
1661 * It can return one of :
1662 * - SN_ERR_NONE if everything's OK
1663 * - SN_ERR_SRVTO if there are no more servers
1664 * - SN_ERR_SRVCL if the connection was refused by the server
1665 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1666 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1667 * - SN_ERR_INTERNAL for any other purely internal errors
1668 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1669 *
1670 * Blocks and then unblocks SIGCHLD
1671 */
1672static int connect_proc_chk(struct task *t)
1673{
1674 struct check *check = t->context;
1675 struct server *s = check->server;
1676 struct proxy *px = s->proxy;
1677 int status;
1678 pid_t pid;
1679
Simon Horman98637e52014-06-20 12:30:16 +09001680 status = SN_ERR_RESOURCE;
1681
1682 block_sigchld();
1683
1684 pid = fork();
1685 if (pid < 0) {
1686 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1687 strerror(errno));
1688 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1689 goto out;
1690 }
1691 if (pid == 0) {
1692 /* Child */
1693 extern char **environ;
1694 environ = check->envp;
1695 execvp(px->check_command, check->argv);
1696 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1697 strerror(errno));
1698 exit(-1);
1699 }
1700
1701 /* Parent */
1702 if (check->result == CHK_RES_UNKNOWN) {
1703 if (pid_list_add(pid, t) != NULL) {
1704 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1705
1706 if (px->timeout.check && px->timeout.connect) {
1707 int t_con = tick_add(now_ms, px->timeout.connect);
1708 t->expire = tick_first(t->expire, t_con);
1709 }
1710 status = SN_ERR_NONE;
1711 goto out;
1712 }
1713 else {
1714 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1715 }
1716 kill(pid, SIGTERM); /* process creation error */
1717 }
1718 else
1719 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1720
1721out:
1722 unblock_sigchld();
1723 return status;
1724}
1725
1726/*
1727 * establish a server health-check.
1728 *
1729 * It can return one of :
1730 * - SN_ERR_NONE if everything's OK
1731 * - SN_ERR_SRVTO if there are no more servers
1732 * - SN_ERR_SRVCL if the connection was refused by the server
1733 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1734 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1735 * - SN_ERR_INTERNAL for any other purely internal errors
1736 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1737 */
1738static int connect_chk(struct task *t)
1739{
1740 struct check *check = t->context;
1741
1742 if (check->type == PR_O2_EXT_CHK)
1743 return connect_proc_chk(t);
1744 return connect_conn_chk(t);
1745}
1746
1747/*
1748 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001749 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1750 */
Simon Horman98637e52014-06-20 12:30:16 +09001751static struct task *process_chk_proc(struct task *t)
1752{
1753 struct check *check = t->context;
1754 struct server *s = check->server;
1755 struct connection *conn = check->conn;
1756 int rv;
1757 int ret;
1758 int expired = tick_is_expired(t->expire, now_ms);
1759
1760 if (!(check->state & CHK_ST_INPROGRESS)) {
1761 /* no check currently running */
1762 if (!expired) /* woke up too early */
1763 return t;
1764
1765 /* we don't send any health-checks when the proxy is
1766 * stopped, the server should not be checked or the check
1767 * is disabled.
1768 */
1769 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1770 s->proxy->state == PR_STSTOPPED)
1771 goto reschedule;
1772
1773 /* we'll initiate a new check */
1774 set_server_check_status(check, HCHK_STATUS_START, NULL);
1775
1776 check->state |= CHK_ST_INPROGRESS;
1777
1778 ret = connect_chk(t);
1779 switch (ret) {
1780 case SN_ERR_UP:
1781 return t;
1782 case SN_ERR_NONE:
1783 /* we allow up to min(inter, timeout.connect) for a connection
1784 * to establish but only when timeout.check is set
1785 * as it may be to short for a full check otherwise
1786 */
1787 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1788
1789 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1790 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1791 t->expire = tick_first(t->expire, t_con);
1792 }
1793
1794 goto reschedule;
1795
1796 case SN_ERR_SRVTO: /* ETIMEDOUT */
1797 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1798 conn->flags |= CO_FL_ERROR;
1799 chk_report_conn_err(conn, errno, 0);
1800 break;
1801 case SN_ERR_PRXCOND:
1802 case SN_ERR_RESOURCE:
1803 case SN_ERR_INTERNAL:
1804 conn->flags |= CO_FL_ERROR;
1805 chk_report_conn_err(conn, 0, 0);
1806 break;
1807 }
1808
1809 /* here, we have seen a synchronous error, no fd was allocated */
1810
1811 check->state &= ~CHK_ST_INPROGRESS;
1812 check_notify_failure(check);
1813
1814 /* we allow up to min(inter, timeout.connect) for a connection
1815 * to establish but only when timeout.check is set
1816 * as it may be to short for a full check otherwise
1817 */
1818 while (tick_is_expired(t->expire, now_ms)) {
1819 int t_con;
1820
1821 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1822 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1823
1824 if (s->proxy->timeout.check)
1825 t->expire = tick_first(t->expire, t_con);
1826 }
1827 }
1828 else {
1829 /* there was a test running.
1830 * First, let's check whether there was an uncaught error,
1831 * which can happen on connect timeout or error.
1832 */
1833 if (check->result == CHK_RES_UNKNOWN) {
1834 /* good connection is enough for pure TCP check */
1835 struct pid_list *elem = check->curpid;
1836 int status = HCHK_STATUS_UNKNOWN;
1837
1838 if (elem->exited) {
1839 status = elem->status; /* Save in case the process exits between use below */
1840 if (!WIFEXITED(status))
1841 check->code = -1;
1842 else
1843 check->code = WEXITSTATUS(status);
1844 if (!WIFEXITED(status) || WEXITSTATUS(status))
1845 status = HCHK_STATUS_PROCERR;
1846 else
1847 status = HCHK_STATUS_PROCOK;
1848 } else if (expired) {
1849 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001850 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001851 kill(elem->pid, SIGTERM);
1852 }
1853 set_server_check_status(check, status, NULL);
1854 }
1855
1856 if (check->result == CHK_RES_FAILED) {
1857 /* a failure or timeout detected */
1858 check_notify_failure(check);
1859 }
1860 else if (check->result == CHK_RES_CONDPASS) {
1861 /* check is OK but asks for stopping mode */
1862 check_notify_stopping(check);
1863 }
1864 else if (check->result == CHK_RES_PASSED) {
1865 /* a success was detected */
1866 check_notify_success(check);
1867 }
1868 check->state &= ~CHK_ST_INPROGRESS;
1869
1870 pid_list_del(check->curpid);
1871
1872 rv = 0;
1873 if (global.spread_checks > 0) {
1874 rv = srv_getinter(check) * global.spread_checks / 100;
1875 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1876 }
1877 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1878 }
1879
1880 reschedule:
1881 while (tick_is_expired(t->expire, now_ms))
1882 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1883 return t;
1884}
1885
1886/*
1887 * manages a server health-check that uses a connection. Returns
1888 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1889 */
1890static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001891{
Simon Horman4a741432013-02-23 15:35:38 +09001892 struct check *check = t->context;
1893 struct server *s = check->server;
1894 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001895 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001896 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001897 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001898
Willy Tarreau2c115e52013-12-11 19:41:16 +01001899 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001900 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001901 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001902 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001903
Simon Horman671b6f02013-11-25 10:46:39 +09001904 /* we don't send any health-checks when the proxy is
1905 * stopped, the server should not be checked or the check
1906 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001907 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001908 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001909 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001910 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001911
1912 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001913 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001914
Willy Tarreau2c115e52013-12-11 19:41:16 +01001915 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001916 check->bi->p = check->bi->data;
1917 check->bi->i = 0;
1918 check->bo->p = check->bo->data;
1919 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001920
Simon Hormanb00d17a2014-06-13 16:18:16 +09001921 ret = connect_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001922 switch (ret) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001923 case SN_ERR_UP:
1924 return t;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001925 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001926 /* we allow up to min(inter, timeout.connect) for a connection
1927 * to establish but only when timeout.check is set
1928 * as it may be to short for a full check otherwise
1929 */
Simon Horman4a741432013-02-23 15:35:38 +09001930 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001931
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001932 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1933 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1934 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001935 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001936
1937 if (check->type)
1938 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1939
Willy Tarreau5a78f362012-11-23 12:47:05 +01001940 goto reschedule;
1941
1942 case SN_ERR_SRVTO: /* ETIMEDOUT */
1943 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001944 conn->flags |= CO_FL_ERROR;
1945 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001946 break;
1947 case SN_ERR_PRXCOND:
1948 case SN_ERR_RESOURCE:
1949 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001950 conn->flags |= CO_FL_ERROR;
1951 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001952 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001953 }
1954
Willy Tarreau5a78f362012-11-23 12:47:05 +01001955 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001956
Willy Tarreau2c115e52013-12-11 19:41:16 +01001957 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02001958 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001959
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001960 /* we allow up to min(inter, timeout.connect) for a connection
1961 * to establish but only when timeout.check is set
1962 * as it may be to short for a full check otherwise
1963 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001964 while (tick_is_expired(t->expire, now_ms)) {
1965 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001966
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001967 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001968 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001969
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001970 if (s->proxy->timeout.check)
1971 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001972 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001973 }
1974 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001975 /* there was a test running.
1976 * First, let's check whether there was an uncaught error,
1977 * which can happen on connect timeout or error.
1978 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09001979 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001980 /* good connection is enough for pure TCP check */
1981 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001982 if (check->use_ssl)
1983 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001984 else
Simon Horman4a741432013-02-23 15:35:38 +09001985 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001986 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001987 else if ((conn->flags & CO_FL_ERROR) || expired) {
1988 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001989 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001990 else
1991 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001992 }
1993
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001994 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001995 if (conn->xprt) {
1996 /* The check was aborted and the connection was not yet closed.
1997 * This can happen upon timeout, or when an external event such
1998 * as a failed response coupled with "observe layer7" caused the
1999 * server state to be suddenly changed.
2000 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01002001 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002002 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002003 }
2004
Willy Tarreauaf549582014-05-16 17:37:50 +02002005 if (check->result == CHK_RES_FAILED) {
2006 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002007 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002008 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002009 else if (check->result == CHK_RES_CONDPASS) {
2010 /* check is OK but asks for stopping mode */
2011 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002012 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002013 else if (check->result == CHK_RES_PASSED) {
2014 /* a success was detected */
2015 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002016 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002017 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002018
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002019 rv = 0;
2020 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002021 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002022 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002023 }
Simon Horman4a741432013-02-23 15:35:38 +09002024 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002025 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002026
2027 reschedule:
2028 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002029 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002030 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002031 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002032}
2033
Simon Horman98637e52014-06-20 12:30:16 +09002034/*
2035 * manages a server health-check. Returns
2036 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2037 */
2038static struct task *process_chk(struct task *t)
2039{
2040 struct check *check = t->context;
2041
2042 if (check->type == PR_O2_EXT_CHK)
2043 return process_chk_proc(t);
2044 return process_chk_conn(t);
2045}
2046
Simon Horman5c942422013-11-25 10:46:32 +09002047static int start_check_task(struct check *check, int mininter,
2048 int nbcheck, int srvpos)
2049{
2050 struct task *t;
2051 /* task for the check */
2052 if ((t = task_new()) == NULL) {
2053 Alert("Starting [%s:%s] check: out of memory.\n",
2054 check->server->proxy->id, check->server->id);
2055 return 0;
2056 }
2057
2058 check->task = t;
2059 t->process = process_chk;
2060 t->context = check;
2061
Willy Tarreau1746eec2014-04-25 10:46:47 +02002062 if (mininter < srv_getinter(check))
2063 mininter = srv_getinter(check);
2064
2065 if (global.max_spread_checks && mininter > global.max_spread_checks)
2066 mininter = global.max_spread_checks;
2067
Simon Horman5c942422013-11-25 10:46:32 +09002068 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002069 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002070 check->start = now;
2071 task_queue(t);
2072
2073 return 1;
2074}
2075
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002076/*
2077 * Start health-check.
2078 * Returns 0 if OK, -1 if error, and prints the error in this case.
2079 */
2080int start_checks() {
2081
2082 struct proxy *px;
2083 struct server *s;
2084 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002085 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002086
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002087 /* 1- count the checkers to run simultaneously.
2088 * We also determine the minimum interval among all of those which
2089 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2090 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002091 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002092 * too short an interval for all others.
2093 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002094 for (px = proxy; px; px = px->next) {
2095 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002096 if (s->slowstart) {
2097 if ((t = task_new()) == NULL) {
2098 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2099 return -1;
2100 }
2101 /* We need a warmup task that will be called when the server
2102 * state switches from down to up.
2103 */
2104 s->warmup = t;
2105 t->process = server_warmup;
2106 t->context = s;
2107 t->expire = TICK_ETERNITY;
2108 }
2109
Willy Tarreaud8514a22013-12-11 21:10:14 +01002110 if (s->check.state & CHK_ST_CONFIGURED) {
2111 nbcheck++;
2112 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2113 (!mininter || mininter > srv_getinter(&s->check)))
2114 mininter = srv_getinter(&s->check);
2115 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002116
Willy Tarreaud8514a22013-12-11 21:10:14 +01002117 if (s->agent.state & CHK_ST_CONFIGURED) {
2118 nbcheck++;
2119 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2120 (!mininter || mininter > srv_getinter(&s->agent)))
2121 mininter = srv_getinter(&s->agent);
2122 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002123 }
2124 }
2125
Simon Horman4a741432013-02-23 15:35:38 +09002126 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002127 return 0;
2128
2129 srand((unsigned)time(NULL));
2130
2131 /*
2132 * 2- start them as far as possible from each others. For this, we will
2133 * start them after their interval set to the min interval divided by
2134 * the number of servers, weighted by the server's position in the list.
2135 */
2136 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002137 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2138 if (init_pid_list()) {
2139 Alert("Starting [%s] check: out of memory.\n", px->id);
2140 return -1;
2141 }
2142 }
2143
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002144 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002145 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002146 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002147 if (s->check.type == PR_O2_EXT_CHK) {
2148 if (!prepare_external_check(&s->check))
2149 return -1;
2150 }
Simon Hormand60d6912013-11-25 10:46:36 +09002151 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2152 return -1;
2153 srvpos++;
2154 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002155
Simon Hormand60d6912013-11-25 10:46:36 +09002156 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002157 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002158 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2159 return -1;
2160 }
2161 srvpos++;
2162 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002163 }
2164 }
2165 return 0;
2166}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002167
2168/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002169 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002170 * The buffer MUST be terminated by a null byte before calling this function.
2171 * Sets server status appropriately. The caller is responsible for ensuring
2172 * that the buffer contains at least 13 characters. If <done> is zero, we may
2173 * return 0 to indicate that data is required to decide of a match.
2174 */
2175static int httpchk_expect(struct server *s, int done)
2176{
2177 static char status_msg[] = "HTTP status check returned code <000>";
2178 char status_code[] = "000";
2179 char *contentptr;
2180 int crlf;
2181 int ret;
2182
2183 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2184 case PR_O2_EXP_STS:
2185 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002186 memcpy(status_code, s->check.bi->data + 9, 3);
2187 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002188
2189 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2190 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2191 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002192 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002193
2194 /* we necessarily have the response, so there are no partial failures */
2195 if (s->proxy->options2 & PR_O2_EXP_INV)
2196 ret = !ret;
2197
Simon Horman4a741432013-02-23 15:35:38 +09002198 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002199 break;
2200
2201 case PR_O2_EXP_STR:
2202 case PR_O2_EXP_RSTR:
2203 /* very simple response parser: ignore CR and only count consecutive LFs,
2204 * stop with contentptr pointing to first char after the double CRLF or
2205 * to '\0' if crlf < 2.
2206 */
2207 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002208 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002209 if (crlf >= 2)
2210 break;
2211 if (*contentptr == '\r')
2212 continue;
2213 else if (*contentptr == '\n')
2214 crlf++;
2215 else
2216 crlf = 0;
2217 }
2218
2219 /* Check that response contains a body... */
2220 if (crlf < 2) {
2221 if (!done)
2222 return 0;
2223
Simon Horman4a741432013-02-23 15:35:38 +09002224 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002225 "HTTP content check could not find a response body");
2226 return 1;
2227 }
2228
2229 /* Check that response body is not empty... */
2230 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002231 if (!done)
2232 return 0;
2233
Simon Horman4a741432013-02-23 15:35:38 +09002234 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002235 "HTTP content check found empty response body");
2236 return 1;
2237 }
2238
2239 /* Check the response content against the supplied string
2240 * or regex... */
2241 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2242 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2243 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002244 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002245
2246 /* if we don't match, we may need to wait more */
2247 if (!ret && !done)
2248 return 0;
2249
2250 if (ret) {
2251 /* content matched */
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_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002254 "HTTP check matched unwanted content");
2255 else
Simon Horman4a741432013-02-23 15:35:38 +09002256 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002257 "HTTP content check matched");
2258 }
2259 else {
2260 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002261 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002262 "HTTP check did not match unwanted content");
2263 else
Simon Horman4a741432013-02-23 15:35:38 +09002264 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002265 "HTTP content check did not match");
2266 }
2267 break;
2268 }
2269 return 1;
2270}
2271
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002272/*
2273 * return the id of a step in a send/expect session
2274 */
2275static int tcpcheck_get_step_id(struct server *s)
2276{
2277 struct tcpcheck_rule *cur = NULL, *next = NULL;
2278 int i = 0;
2279
Willy Tarreau213c6782014-10-02 14:51:02 +02002280 /* not even started anything yet => step 0 = initial connect */
2281 if (!s->check.current_step)
2282 return 0;
2283
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002284 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002285
2286 /* no step => first step */
2287 if (cur == NULL)
2288 return 1;
2289
2290 /* increment i until current step */
2291 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
2292 if (next->list.p == &cur->list)
2293 break;
2294 ++i;
2295 }
2296
2297 return i;
2298}
2299
2300static void tcpcheck_main(struct connection *conn)
2301{
2302 char *contentptr;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002303 struct tcpcheck_rule *cur = NULL;
2304 int done = 0, ret = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002305 struct check *check = conn->owner;
2306 struct server *s = check->server;
2307 struct task *t = check->task;
Willy Tarreauef953952014-10-02 14:30:14 +02002308 struct list *head = &s->proxy->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002309
Willy Tarreauef953952014-10-02 14:30:14 +02002310 /* here, we know that the check is complete or that it failed */
2311 if (check->result != CHK_RES_UNKNOWN)
2312 goto out_end_tcpcheck;
2313
2314 /* We have 4 possibilities here :
2315 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2316 * connection attempt was made yet ;
2317 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2318 * does not exist (no rule), so a connection attempt was made
2319 * before coming here.
2320 * 3. we're coming back after having started with step 1, so we may
2321 * be waiting for a connection attempt to complete.
2322 * 4. the connection + handshake are complete
2323 *
2324 * #2 and #3 are quite similar, we want both the connection and the
2325 * handshake to complete before going any further. Thus we must always
2326 * wait for a connection to complete unless we're before and existing
2327 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002328 */
Willy Tarreauef953952014-10-02 14:30:14 +02002329 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
2330 (check->current_step || LIST_ISEMPTY(head))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002331 /* we allow up to min(inter, timeout.connect) for a connection
2332 * to establish but only when timeout.check is set
2333 * as it may be to short for a full check otherwise
2334 */
2335 while (tick_is_expired(t->expire, now_ms)) {
2336 int t_con;
2337
2338 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2339 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2340
2341 if (s->proxy->timeout.check)
2342 t->expire = tick_first(t->expire, t_con);
2343 }
2344 return;
2345 }
2346
Willy Tarreauef953952014-10-02 14:30:14 +02002347 /* special case: option tcp-check with no rule, a connect is enough */
2348 if (LIST_ISEMPTY(head)) {
2349 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002350 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002351 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002352
Willy Tarreau213c6782014-10-02 14:51:02 +02002353 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002354 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002355 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002356 check->bo->p = check->bo->data;
2357 check->bo->o = 0;
2358 check->bi->p = check->bi->data;
2359 check->bi->i = 0;
2360 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2361 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2362 if (s->proxy->timeout.check)
2363 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2364 }
2365 /* keep on processing step */
2366 else {
2367 cur = check->current_step;
2368 }
2369
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002370 /* It's only the rules which will enable send/recv */
2371 __conn_data_stop_both(conn);
2372
Willy Tarreauabca5b62013-12-06 14:19:25 +01002373 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002374 /* we have to try to flush the output buffer before reading, at the end,
2375 * or if we're about to send a string that does not fit in the remaining space.
2376 */
2377 if (check->bo->o &&
2378 (&cur->list == head ||
2379 check->current_step->action != TCPCHK_ACT_SEND ||
2380 check->current_step->string_len >= buffer_total_space(check->bo))) {
2381
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002382 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002383 if (conn->flags & CO_FL_ERROR) {
2384 chk_report_conn_err(conn, errno, 0);
2385 __conn_data_stop_both(conn);
2386 goto out_end_tcpcheck;
2387 }
2388 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002389 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002390 }
2391
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002392 /* did we reach the end ? If so, let's check that everything was sent */
2393 if (&cur->list == head) {
2394 if (check->bo->o)
2395 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002396 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002397 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002398
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002399 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2400 struct protocol *proto;
2401 struct xprt_ops *xprt;
2402
2403 /* mark the step as started */
2404 check->last_started_step = check->current_step;
2405 /* first, shut existing connection */
2406 conn_force_close(conn);
2407
2408 /* prepare new connection */
2409 /* initialization */
2410 conn_init(conn);
2411 conn_attach(conn, check, &check_conn_cb);
2412 conn->target = &s->obj_type;
2413
2414 /* no client address */
2415 clear_addr(&conn->addr.from);
2416
Willy Tarreau640556c2014-05-09 23:38:15 +02002417 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002418 /* we'll connect to the check addr specified on the server */
2419 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002420 proto = s->check_common.proto;
2421 }
2422 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002423 /* we'll connect to the addr on the server */
2424 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002425 proto = s->proto;
2426 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002427
2428 /* port */
2429 if (check->current_step->port)
2430 set_host_port(&conn->addr.to, check->current_step->port);
2431 else if (check->port)
2432 set_host_port(&conn->addr.to, check->port);
2433
2434#ifdef USE_OPENSSL
2435 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2436 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002437 }
2438 else {
2439 xprt = &raw_sock;
2440 }
2441#else /* USE_OPENSSL */
2442 xprt = &raw_sock;
2443#endif /* USE_OPENSSL */
2444 conn_prepare(conn, proto, xprt);
2445
2446 ret = SN_ERR_INTERNAL;
2447 if (proto->connect)
2448 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
2449 conn->flags |= CO_FL_WAKE_DATA;
2450 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2451 conn->send_proxy_ofs = 1;
2452 conn->flags |= CO_FL_SEND_PROXY;
2453 }
2454
2455 /* It can return one of :
2456 * - SN_ERR_NONE if everything's OK
2457 * - SN_ERR_SRVTO if there are no more servers
2458 * - SN_ERR_SRVCL if the connection was refused by the server
2459 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2460 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2461 * - SN_ERR_INTERNAL for any other purely internal errors
2462 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2463 * Note that we try to prevent the network stack from sending the ACK during the
2464 * connect() when a pure TCP check is used (without PROXY protocol).
2465 */
2466 switch (ret) {
2467 case SN_ERR_NONE:
2468 /* we allow up to min(inter, timeout.connect) for a connection
2469 * to establish but only when timeout.check is set
2470 * as it may be to short for a full check otherwise
2471 */
2472 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2473
2474 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2475 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2476 t->expire = tick_first(t->expire, t_con);
2477 }
2478 break;
2479 case SN_ERR_SRVTO: /* ETIMEDOUT */
2480 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2481 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2482 tcpcheck_get_step_id(s), strerror(errno));
2483 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2484 goto out_end_tcpcheck;
2485 case SN_ERR_PRXCOND:
2486 case SN_ERR_RESOURCE:
2487 case SN_ERR_INTERNAL:
2488 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2489 tcpcheck_get_step_id(s));
2490 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2491 goto out_end_tcpcheck;
2492 }
2493
2494 /* allow next rule */
2495 cur = (struct tcpcheck_rule *)cur->list.n;
2496 check->current_step = cur;
2497
2498 /* don't do anything until the connection is established */
2499 if (!(conn->flags & CO_FL_CONNECTED)) {
2500 /* update expire time, should be done by process_chk */
2501 /* we allow up to min(inter, timeout.connect) for a connection
2502 * to establish but only when timeout.check is set
2503 * as it may be to short for a full check otherwise
2504 */
2505 while (tick_is_expired(t->expire, now_ms)) {
2506 int t_con;
2507
2508 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2509 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2510
2511 if (s->proxy->timeout.check)
2512 t->expire = tick_first(t->expire, t_con);
2513 }
2514 return;
2515 }
2516
2517 } /* end 'connect' */
2518 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2519 /* mark the step as started */
2520 check->last_started_step = check->current_step;
2521
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002522 /* reset the read buffer */
2523 if (*check->bi->data != '\0') {
2524 *check->bi->data = '\0';
2525 check->bi->i = 0;
2526 }
2527
2528 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2529 conn->flags |= CO_FL_ERROR;
2530 chk_report_conn_err(conn, 0, 0);
2531 goto out_end_tcpcheck;
2532 }
2533
Willy Tarreauabca5b62013-12-06 14:19:25 +01002534 if (check->current_step->string_len >= check->bo->size) {
2535 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2536 check->current_step->string_len, check->bo->size,
2537 tcpcheck_get_step_id(s));
2538 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2539 goto out_end_tcpcheck;
2540 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002541
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002542 /* do not try to send if there is no space */
2543 if (check->current_step->string_len >= buffer_total_space(check->bo))
2544 continue;
2545
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002546 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2547 *check->bo->p = '\0'; /* to make gdb output easier to read */
2548
Willy Tarreauabca5b62013-12-06 14:19:25 +01002549 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002550 cur = (struct tcpcheck_rule *)cur->list.n;
2551 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002552 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002553 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002554 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002555 goto out_end_tcpcheck;
2556
Willy Tarreau310987a2014-01-22 19:46:33 +01002557 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002558 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2559 done = 1;
2560 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2561 /* Report network errors only if we got no other data. Otherwise
2562 * we'll let the upper layers decide whether the response is OK
2563 * or not. It is very common that an RST sent by the server is
2564 * reported as an error just after the last data chunk.
2565 */
2566 chk_report_conn_err(conn, errno, 0);
2567 goto out_end_tcpcheck;
2568 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002569 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002570 else
2571 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002572 }
2573
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002574 /* mark the step as started */
2575 check->last_started_step = check->current_step;
2576
2577
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002578 /* Intermediate or complete response received.
2579 * Terminate string in check->bi->data buffer.
2580 */
2581 if (check->bi->i < check->bi->size) {
2582 check->bi->data[check->bi->i] = '\0';
2583 }
2584 else {
2585 check->bi->data[check->bi->i - 1] = '\0';
2586 done = 1; /* buffer full, don't wait for more data */
2587 }
2588
2589 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002590
2591 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002592 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002593 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002594 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002595
2596 /* empty response */
2597 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2598 tcpcheck_get_step_id(s));
2599 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2600
2601 goto out_end_tcpcheck;
2602 }
2603
2604 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002605 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002606
Willy Tarreaua970c282013-12-06 12:47:19 +01002607 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002608 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002609 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002610 else if (cur->expect_regex != NULL)
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002611 ret = regex_exec(cur->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002612
2613 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002614 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002615
2616 /* matched */
2617 if (ret) {
2618 /* matched but we did not want to => ERROR */
2619 if (cur->inverse) {
2620 /* we were looking for a string */
2621 if (cur->string != NULL) {
2622 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2623 cur->string, tcpcheck_get_step_id(s));
2624 }
2625 else {
2626 /* we were looking for a regex */
2627 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2628 tcpcheck_get_step_id(s));
2629 }
2630 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2631 goto out_end_tcpcheck;
2632 }
2633 /* matched and was supposed to => OK, next step */
2634 else {
2635 cur = (struct tcpcheck_rule*)cur->list.n;
2636 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002637 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002638 goto tcpcheck_expect;
2639 __conn_data_stop_recv(conn);
2640 }
2641 }
2642 else {
2643 /* not matched */
2644 /* not matched and was not supposed to => OK, next step */
2645 if (cur->inverse) {
2646 cur = (struct tcpcheck_rule*)cur->list.n;
2647 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002648 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002649 goto tcpcheck_expect;
2650 __conn_data_stop_recv(conn);
2651 }
2652 /* not matched but was supposed to => ERROR */
2653 else {
2654 /* we were looking for a string */
2655 if (cur->string != NULL) {
2656 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2657 cur->string, tcpcheck_get_step_id(s));
2658 }
2659 else {
2660 /* we were looking for a regex */
2661 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2662 tcpcheck_get_step_id(s));
2663 }
2664 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2665 goto out_end_tcpcheck;
2666 }
2667 }
2668 } /* end expect */
2669 } /* end loop over double chained step list */
2670
2671 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2672 goto out_end_tcpcheck;
2673
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002674 out_need_io:
2675 if (check->bo->o)
2676 __conn_data_want_send(conn);
2677
2678 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2679 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002680 return;
2681
2682 out_end_tcpcheck:
2683 /* collect possible new errors */
2684 if (conn->flags & CO_FL_ERROR)
2685 chk_report_conn_err(conn, 0, 0);
2686
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002687 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002688 check->current_step = NULL;
2689
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002690 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002691 conn->flags |= CO_FL_ERROR;
2692
2693 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002694
2695 return;
2696}
2697
2698
Willy Tarreaubd741542010-03-16 18:46:54 +01002699/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002700 * Local variables:
2701 * c-indent-level: 8
2702 * c-basic-offset: 8
2703 * End:
2704 */