blob: 794793c51d09ba6849a58b1c8c00f341160fa29d [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;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001390 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001391
1392 /* tcpcheck send/expect initialisation */
1393 if (check->type == PR_O2_TCPCHK_CHK)
1394 check->current_step = NULL;
1395
1396 /* prepare the check buffer.
1397 * This should not be used if check is the secondary agent check
1398 * of a server as s->proxy->check_req will relate to the
1399 * configuration of the primary check. Similarly, tcp-check uses
1400 * its own strings.
1401 */
1402 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1403 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1404
1405 /* we want to check if this host replies to HTTP or SSLv3 requests
1406 * so we'll send the request, and won't wake the checker up now.
1407 */
1408 if ((check->type) == PR_O2_SSL3_CHK) {
1409 /* SSL requires that we put Unix time in the request */
1410 int gmt_time = htonl(date.tv_sec);
1411 memcpy(check->bo->data + 11, &gmt_time, 4);
1412 }
1413 else if ((check->type) == PR_O2_HTTP_CHK) {
1414 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1415 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
1416 bo_putstr(check->bo, "\r\n");
1417 *check->bo->p = '\0'; /* to make gdb output easier to read */
1418 }
1419 }
1420
1421 /* prepare a new connection */
1422 conn_init(conn);
Cyril Bonté9ce13112014-11-15 22:41:27 +01001423 conn_prepare(conn, s->check_common.proto, check->xprt);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001424 conn_attach(conn, check, &check_conn_cb);
1425 conn->target = &s->obj_type;
1426
1427 /* no client address */
1428 clear_addr(&conn->addr.from);
1429
1430 if (is_addr(&s->check_common.addr)) {
1431
1432 /* we'll connect to the check addr specified on the server */
1433 conn->addr.to = s->check_common.addr;
1434 proto = s->check_common.proto;
1435 }
1436 else {
1437 /* we'll connect to the addr on the server */
1438 conn->addr.to = s->addr;
1439 proto = s->proto;
1440 }
1441
1442 if (check->port) {
1443 set_host_port(&conn->addr.to, check->port);
1444 }
1445
Willy Tarreauf3d34822014-12-08 12:11:28 +01001446 /* only plain tcp-check supports quick ACK */
1447 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1448
Willy Tarreaud2a49592014-12-08 11:52:28 +01001449 if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(&s->proxy->tcpcheck_rules)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001450 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1451 /* if first step is a 'connect', then tcpcheck_main must run it */
1452 if (r->action == TCPCHK_ACT_CONNECT) {
1453 tcpcheck_main(conn);
1454 return SN_ERR_UP;
1455 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001456 if (r->action == TCPCHK_ACT_EXPECT)
1457 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001458 }
1459
1460 ret = SN_ERR_INTERNAL;
1461 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001462 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001463 conn->flags |= CO_FL_WAKE_DATA;
1464 if (s->check.send_proxy) {
1465 conn->send_proxy_ofs = 1;
1466 conn->flags |= CO_FL_SEND_PROXY;
1467 }
1468
1469 return ret;
1470}
1471
Simon Horman98637e52014-06-20 12:30:16 +09001472static struct list pid_list = LIST_HEAD_INIT(pid_list);
1473static struct pool_head *pool2_pid_list;
1474
1475void block_sigchld(void)
1476{
1477 sigset_t set;
1478 sigemptyset(&set);
1479 sigaddset(&set, SIGCHLD);
1480 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1481}
1482
1483void unblock_sigchld(void)
1484{
1485 sigset_t set;
1486 sigemptyset(&set);
1487 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1488}
1489
1490/* Call with SIGCHLD blocked */
1491static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1492{
1493 struct pid_list *elem;
1494 struct check *check = t->context;
1495
1496 elem = pool_alloc2(pool2_pid_list);
1497 if (!elem)
1498 return NULL;
1499 elem->pid = pid;
1500 elem->t = t;
1501 elem->exited = 0;
1502 check->curpid = elem;
1503 LIST_INIT(&elem->list);
1504 LIST_ADD(&pid_list, &elem->list);
1505 return elem;
1506}
1507
1508/* Blocks blocks and then unblocks SIGCHLD */
1509static void pid_list_del(struct pid_list *elem)
1510{
1511 struct check *check;
1512
1513 if (!elem)
1514 return;
1515
1516 block_sigchld();
1517 LIST_DEL(&elem->list);
1518 unblock_sigchld();
1519 if (!elem->exited)
1520 kill(elem->pid, SIGTERM);
1521
1522 check = elem->t->context;
1523 check->curpid = NULL;
1524 pool_free2(pool2_pid_list, elem);
1525}
1526
1527/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1528static void pid_list_expire(pid_t pid, int status)
1529{
1530 struct pid_list *elem;
1531
1532 list_for_each_entry(elem, &pid_list, list) {
1533 if (elem->pid == pid) {
1534 elem->t->expire = now_ms;
1535 elem->status = status;
1536 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001537 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001538 return;
1539 }
1540 }
1541}
1542
1543static void sigchld_handler(int signal)
1544{
1545 pid_t pid;
1546 int status;
1547 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1548 pid_list_expire(pid, status);
1549}
1550
1551static int init_pid_list(void) {
1552 struct sigaction action = {
1553 .sa_handler = sigchld_handler,
1554 .sa_flags = SA_NOCLDSTOP
1555 };
1556
1557 if (pool2_pid_list != NULL)
1558 /* Nothing to do */
1559 return 0;
1560
1561 if (sigaction(SIGCHLD, &action, NULL)) {
1562 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1563 strerror(errno));
1564 return 1;
1565 }
1566
1567 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1568 if (pool2_pid_list == NULL) {
1569 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1570 strerror(errno));
1571 return 1;
1572 }
1573
1574 return 0;
1575}
1576
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001577#define EXTCHECK_ARG_MAX 5
1578#define EXTCHECK_ENV_ROOM 16
1579
1580#define EXTCHECK_ADD_ENV(check, envname, value, idx) { if (external_check_add_env(check, envname, value, idx)) goto err; }
1581
1582/*
1583 * helper function to allocate enough space for new environment variables used
1584 * by external checks.
1585 */
1586static int external_check_add_env(struct check *check, const char *envname, const char *value, int *idx)
1587{
1588 int len, ret;
1589
1590 if (*idx % EXTCHECK_ENV_ROOM == 0) {
1591 /* Allocate enough space to store new environment variables */
1592 char **tmp = realloc(check->envp, (EXTCHECK_ENV_ROOM * ((*idx / EXTCHECK_ENV_ROOM) + 1) + 1) * sizeof(check->envp));
1593 if (tmp == NULL) {
1594 Alert("Failed to allocate memory for new environment variables. Aborting\n");
1595 return 1;
1596 } else {
1597 check->envp = tmp;
1598 }
1599 }
1600 /* Instead of sending NOT_USED, sending an empty value is preferable */
1601 if (strcmp(value, "NOT_USED") == 0) {
1602 value = "";
1603 }
1604 len = strlen(envname) + strlen(value) + 1;
1605 check->envp[*idx] = malloc(len + 1);
1606 check->envp[*idx + 1] = NULL;
1607 if (!check->envp[*idx]) {
1608 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1609 return 1;
1610 }
1611 ret = snprintf(check->envp[*idx], len + 1, "%s=%s", envname, value);
1612 if (ret < 0) {
1613 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1614 return 1;
1615 }
1616 else if (ret != len) {
1617 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1618 return 1;
1619 }
1620 (*idx)++;
1621 return 0;
1622}
Simon Horman98637e52014-06-20 12:30:16 +09001623
1624static int prepare_external_check(struct check *check)
1625{
1626 struct server *s = check->server;
1627 struct proxy *px = s->proxy;
1628 struct listener *listener = NULL, *l;
1629 int i;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001630 int envidx = 0;
Simon Horman98637e52014-06-20 12:30:16 +09001631 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001632 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001633
1634 list_for_each_entry(l, &px->conf.listeners, by_fe)
1635 /* Use the first INET, INET6 or UNIX listener */
1636 if (l->addr.ss_family == AF_INET ||
1637 l->addr.ss_family == AF_INET6 ||
1638 l->addr.ss_family == AF_UNIX) {
1639 listener = l;
1640 break;
1641 }
1642
Simon Horman98637e52014-06-20 12:30:16 +09001643 check->curpid = NULL;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001644 check->envp = NULL;
Simon Horman98637e52014-06-20 12:30:16 +09001645
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001646 check->argv = calloc(EXTCHECK_ARG_MAX + 1, sizeof(check->argv));
Simon Horman98637e52014-06-20 12:30:16 +09001647 if (!check->argv)
1648 goto err;
1649
1650 check->argv[0] = px->check_command;
1651
Cyril Bonté777be862014-12-02 21:21:35 +01001652 if (!listener) {
1653 check->argv[1] = strdup("NOT_USED");
1654 check->argv[2] = strdup("NOT_USED");
1655 }
1656 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001657 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001658 addr_to_str(&listener->addr, buf, sizeof(buf));
1659 check->argv[1] = strdup(buf);
1660 port_to_str(&listener->addr, buf, sizeof(buf));
1661 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001662 }
1663 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001664 const struct sockaddr_un *un;
1665
1666 un = (struct sockaddr_un *)&listener->addr;
1667 check->argv[1] = strdup(un->sun_path);
1668 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001669 }
1670 else {
Simon Horman98637e52014-06-20 12:30:16 +09001671 goto err;
1672 }
1673
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001674 addr_to_str(&s->addr, buf, sizeof(buf));
1675 check->argv[3] = strdup(buf);
1676 port_to_str(&s->addr, buf, sizeof(buf));
1677 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001678
1679 for (i = 0; i < 5; i++)
1680 if (!check->argv[i])
1681 goto err;
1682
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001683 EXTCHECK_ADD_ENV(check, "PATH", path, &envidx);
1684 /* Add proxy environment variables */
1685 EXTCHECK_ADD_ENV(check, "HAPROXY_PROXY_NAME", px->id, &envidx);
1686 EXTCHECK_ADD_ENV(check, "HAPROXY_PROXY_ID", ultoa_r(px->uuid, buf, sizeof(buf)), &envidx);
1687 EXTCHECK_ADD_ENV(check, "HAPROXY_PROXY_ADDR", check->argv[1], &envidx);
1688 EXTCHECK_ADD_ENV(check, "HAPROXY_PROXY_PORT", check->argv[2], &envidx);
1689 /* Add server environment variables */
1690 EXTCHECK_ADD_ENV(check, "HAPROXY_SERVER_NAME", s->id, &envidx);
1691 EXTCHECK_ADD_ENV(check, "HAPROXY_SERVER_ID", ultoa_r(s->puid, buf, sizeof(buf)), &envidx);
1692 EXTCHECK_ADD_ENV(check, "HAPROXY_SERVER_ADDR", check->argv[3], &envidx);
1693 EXTCHECK_ADD_ENV(check, "HAPROXY_SERVER_PORT", check->argv[4], &envidx);
1694 EXTCHECK_ADD_ENV(check, "HAPROXY_SERVER_MAXCONN", ultoa_r(s->maxconn, buf, sizeof(buf)), &envidx);
1695 EXTCHECK_ADD_ENV(check, "HAPROXY_SERVER_CURCONN", ultoa_r(s->cur_sess, buf, sizeof(buf)), &envidx);
1696
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001697 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001698err:
1699 if (check->envp) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001700 for (i = 0; i < envidx; i++)
1701 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001702 free(check->envp);
1703 check->envp = NULL;
1704 }
1705
1706 if (check->argv) {
1707 for (i = 1; i < 5; i++)
1708 free(check->argv[i]);
1709 free(check->argv);
1710 check->argv = NULL;
1711 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001712 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001713}
1714
Simon Hormanb00d17a2014-06-13 16:18:16 +09001715/*
Simon Horman98637e52014-06-20 12:30:16 +09001716 * establish a server health-check that makes use of a process.
1717 *
1718 * It can return one of :
1719 * - SN_ERR_NONE if everything's OK
1720 * - SN_ERR_SRVTO if there are no more servers
1721 * - SN_ERR_SRVCL if the connection was refused by the server
1722 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1723 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1724 * - SN_ERR_INTERNAL for any other purely internal errors
1725 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1726 *
1727 * Blocks and then unblocks SIGCHLD
1728 */
1729static int connect_proc_chk(struct task *t)
1730{
1731 struct check *check = t->context;
1732 struct server *s = check->server;
1733 struct proxy *px = s->proxy;
1734 int status;
1735 pid_t pid;
1736
Simon Horman98637e52014-06-20 12:30:16 +09001737 status = SN_ERR_RESOURCE;
1738
1739 block_sigchld();
1740
1741 pid = fork();
1742 if (pid < 0) {
1743 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1744 strerror(errno));
1745 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1746 goto out;
1747 }
1748 if (pid == 0) {
1749 /* Child */
1750 extern char **environ;
1751 environ = check->envp;
1752 execvp(px->check_command, check->argv);
1753 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1754 strerror(errno));
1755 exit(-1);
1756 }
1757
1758 /* Parent */
1759 if (check->result == CHK_RES_UNKNOWN) {
1760 if (pid_list_add(pid, t) != NULL) {
1761 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1762
1763 if (px->timeout.check && px->timeout.connect) {
1764 int t_con = tick_add(now_ms, px->timeout.connect);
1765 t->expire = tick_first(t->expire, t_con);
1766 }
1767 status = SN_ERR_NONE;
1768 goto out;
1769 }
1770 else {
1771 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1772 }
1773 kill(pid, SIGTERM); /* process creation error */
1774 }
1775 else
1776 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1777
1778out:
1779 unblock_sigchld();
1780 return status;
1781}
1782
1783/*
1784 * establish a server health-check.
1785 *
1786 * It can return one of :
1787 * - SN_ERR_NONE if everything's OK
1788 * - SN_ERR_SRVTO if there are no more servers
1789 * - SN_ERR_SRVCL if the connection was refused by the server
1790 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1791 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1792 * - SN_ERR_INTERNAL for any other purely internal errors
1793 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1794 */
1795static int connect_chk(struct task *t)
1796{
1797 struct check *check = t->context;
1798
1799 if (check->type == PR_O2_EXT_CHK)
1800 return connect_proc_chk(t);
1801 return connect_conn_chk(t);
1802}
1803
1804/*
1805 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001806 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1807 */
Simon Horman98637e52014-06-20 12:30:16 +09001808static struct task *process_chk_proc(struct task *t)
1809{
1810 struct check *check = t->context;
1811 struct server *s = check->server;
1812 struct connection *conn = check->conn;
1813 int rv;
1814 int ret;
1815 int expired = tick_is_expired(t->expire, now_ms);
1816
1817 if (!(check->state & CHK_ST_INPROGRESS)) {
1818 /* no check currently running */
1819 if (!expired) /* woke up too early */
1820 return t;
1821
1822 /* we don't send any health-checks when the proxy is
1823 * stopped, the server should not be checked or the check
1824 * is disabled.
1825 */
1826 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1827 s->proxy->state == PR_STSTOPPED)
1828 goto reschedule;
1829
1830 /* we'll initiate a new check */
1831 set_server_check_status(check, HCHK_STATUS_START, NULL);
1832
1833 check->state |= CHK_ST_INPROGRESS;
1834
1835 ret = connect_chk(t);
1836 switch (ret) {
1837 case SN_ERR_UP:
1838 return t;
1839 case SN_ERR_NONE:
1840 /* we allow up to min(inter, timeout.connect) for a connection
1841 * to establish but only when timeout.check is set
1842 * as it may be to short for a full check otherwise
1843 */
1844 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1845
1846 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1847 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1848 t->expire = tick_first(t->expire, t_con);
1849 }
1850
1851 goto reschedule;
1852
1853 case SN_ERR_SRVTO: /* ETIMEDOUT */
1854 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1855 conn->flags |= CO_FL_ERROR;
1856 chk_report_conn_err(conn, errno, 0);
1857 break;
1858 case SN_ERR_PRXCOND:
1859 case SN_ERR_RESOURCE:
1860 case SN_ERR_INTERNAL:
1861 conn->flags |= CO_FL_ERROR;
1862 chk_report_conn_err(conn, 0, 0);
1863 break;
1864 }
1865
1866 /* here, we have seen a synchronous error, no fd was allocated */
1867
1868 check->state &= ~CHK_ST_INPROGRESS;
1869 check_notify_failure(check);
1870
1871 /* we allow up to min(inter, timeout.connect) for a connection
1872 * to establish but only when timeout.check is set
1873 * as it may be to short for a full check otherwise
1874 */
1875 while (tick_is_expired(t->expire, now_ms)) {
1876 int t_con;
1877
1878 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1879 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1880
1881 if (s->proxy->timeout.check)
1882 t->expire = tick_first(t->expire, t_con);
1883 }
1884 }
1885 else {
1886 /* there was a test running.
1887 * First, let's check whether there was an uncaught error,
1888 * which can happen on connect timeout or error.
1889 */
1890 if (check->result == CHK_RES_UNKNOWN) {
1891 /* good connection is enough for pure TCP check */
1892 struct pid_list *elem = check->curpid;
1893 int status = HCHK_STATUS_UNKNOWN;
1894
1895 if (elem->exited) {
1896 status = elem->status; /* Save in case the process exits between use below */
1897 if (!WIFEXITED(status))
1898 check->code = -1;
1899 else
1900 check->code = WEXITSTATUS(status);
1901 if (!WIFEXITED(status) || WEXITSTATUS(status))
1902 status = HCHK_STATUS_PROCERR;
1903 else
1904 status = HCHK_STATUS_PROCOK;
1905 } else if (expired) {
1906 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001907 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001908 kill(elem->pid, SIGTERM);
1909 }
1910 set_server_check_status(check, status, NULL);
1911 }
1912
1913 if (check->result == CHK_RES_FAILED) {
1914 /* a failure or timeout detected */
1915 check_notify_failure(check);
1916 }
1917 else if (check->result == CHK_RES_CONDPASS) {
1918 /* check is OK but asks for stopping mode */
1919 check_notify_stopping(check);
1920 }
1921 else if (check->result == CHK_RES_PASSED) {
1922 /* a success was detected */
1923 check_notify_success(check);
1924 }
1925 check->state &= ~CHK_ST_INPROGRESS;
1926
1927 pid_list_del(check->curpid);
1928
1929 rv = 0;
1930 if (global.spread_checks > 0) {
1931 rv = srv_getinter(check) * global.spread_checks / 100;
1932 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1933 }
1934 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1935 }
1936
1937 reschedule:
1938 while (tick_is_expired(t->expire, now_ms))
1939 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1940 return t;
1941}
1942
1943/*
1944 * manages a server health-check that uses a connection. Returns
1945 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1946 */
1947static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001948{
Simon Horman4a741432013-02-23 15:35:38 +09001949 struct check *check = t->context;
1950 struct server *s = check->server;
1951 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001952 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001953 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001954 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001955
Willy Tarreau2c115e52013-12-11 19:41:16 +01001956 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001957 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001958 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001959 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001960
Simon Horman671b6f02013-11-25 10:46:39 +09001961 /* we don't send any health-checks when the proxy is
1962 * stopped, the server should not be checked or the check
1963 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001965 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001966 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001967 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001968
1969 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001970 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001971
Willy Tarreau2c115e52013-12-11 19:41:16 +01001972 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001973 check->bi->p = check->bi->data;
1974 check->bi->i = 0;
1975 check->bo->p = check->bo->data;
1976 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001977
Simon Hormanb00d17a2014-06-13 16:18:16 +09001978 ret = connect_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001979 switch (ret) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001980 case SN_ERR_UP:
1981 return t;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001982 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001983 /* we allow up to min(inter, timeout.connect) for a connection
1984 * to establish but only when timeout.check is set
1985 * as it may be to short for a full check otherwise
1986 */
Simon Horman4a741432013-02-23 15:35:38 +09001987 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001988
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001989 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1990 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1991 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001992 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001993
1994 if (check->type)
1995 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1996
Willy Tarreau5a78f362012-11-23 12:47:05 +01001997 goto reschedule;
1998
1999 case SN_ERR_SRVTO: /* ETIMEDOUT */
2000 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002001 conn->flags |= CO_FL_ERROR;
2002 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002003 break;
2004 case SN_ERR_PRXCOND:
2005 case SN_ERR_RESOURCE:
2006 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002007 conn->flags |= CO_FL_ERROR;
2008 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002009 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002010 }
2011
Willy Tarreau5a78f362012-11-23 12:47:05 +01002012 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002013
Willy Tarreau2c115e52013-12-11 19:41:16 +01002014 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002015 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002016
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002017 /* we allow up to min(inter, timeout.connect) for a connection
2018 * to establish but only when timeout.check is set
2019 * as it may be to short for a full check otherwise
2020 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002021 while (tick_is_expired(t->expire, now_ms)) {
2022 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002023
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002024 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002025 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002026
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002027 if (s->proxy->timeout.check)
2028 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002029 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002030 }
2031 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002032 /* there was a test running.
2033 * First, let's check whether there was an uncaught error,
2034 * which can happen on connect timeout or error.
2035 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002036 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002037 /* good connection is enough for pure TCP check */
2038 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002039 if (check->use_ssl)
2040 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002041 else
Simon Horman4a741432013-02-23 15:35:38 +09002042 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002043 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002044 else if ((conn->flags & CO_FL_ERROR) || expired) {
2045 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002046 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002047 else
2048 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002049 }
2050
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002051 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002052 if (conn->xprt) {
2053 /* The check was aborted and the connection was not yet closed.
2054 * This can happen upon timeout, or when an external event such
2055 * as a failed response coupled with "observe layer7" caused the
2056 * server state to be suddenly changed.
2057 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01002058 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002059 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002060 }
2061
Willy Tarreauaf549582014-05-16 17:37:50 +02002062 if (check->result == CHK_RES_FAILED) {
2063 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002064 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002065 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002066 else if (check->result == CHK_RES_CONDPASS) {
2067 /* check is OK but asks for stopping mode */
2068 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002069 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002070 else if (check->result == CHK_RES_PASSED) {
2071 /* a success was detected */
2072 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002073 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002074 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002075
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002076 rv = 0;
2077 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002078 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002079 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002080 }
Simon Horman4a741432013-02-23 15:35:38 +09002081 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002083
2084 reschedule:
2085 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002086 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002087 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002088 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002089}
2090
Simon Horman98637e52014-06-20 12:30:16 +09002091/*
2092 * manages a server health-check. Returns
2093 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2094 */
2095static struct task *process_chk(struct task *t)
2096{
2097 struct check *check = t->context;
2098
2099 if (check->type == PR_O2_EXT_CHK)
2100 return process_chk_proc(t);
2101 return process_chk_conn(t);
2102}
2103
Simon Horman5c942422013-11-25 10:46:32 +09002104static int start_check_task(struct check *check, int mininter,
2105 int nbcheck, int srvpos)
2106{
2107 struct task *t;
2108 /* task for the check */
2109 if ((t = task_new()) == NULL) {
2110 Alert("Starting [%s:%s] check: out of memory.\n",
2111 check->server->proxy->id, check->server->id);
2112 return 0;
2113 }
2114
2115 check->task = t;
2116 t->process = process_chk;
2117 t->context = check;
2118
Willy Tarreau1746eec2014-04-25 10:46:47 +02002119 if (mininter < srv_getinter(check))
2120 mininter = srv_getinter(check);
2121
2122 if (global.max_spread_checks && mininter > global.max_spread_checks)
2123 mininter = global.max_spread_checks;
2124
Simon Horman5c942422013-11-25 10:46:32 +09002125 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002126 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002127 check->start = now;
2128 task_queue(t);
2129
2130 return 1;
2131}
2132
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002133/*
2134 * Start health-check.
2135 * Returns 0 if OK, -1 if error, and prints the error in this case.
2136 */
2137int start_checks() {
2138
2139 struct proxy *px;
2140 struct server *s;
2141 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002142 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002143
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002144 /* 1- count the checkers to run simultaneously.
2145 * We also determine the minimum interval among all of those which
2146 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2147 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002148 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002149 * too short an interval for all others.
2150 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002151 for (px = proxy; px; px = px->next) {
2152 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002153 if (s->slowstart) {
2154 if ((t = task_new()) == NULL) {
2155 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2156 return -1;
2157 }
2158 /* We need a warmup task that will be called when the server
2159 * state switches from down to up.
2160 */
2161 s->warmup = t;
2162 t->process = server_warmup;
2163 t->context = s;
2164 t->expire = TICK_ETERNITY;
2165 }
2166
Willy Tarreaud8514a22013-12-11 21:10:14 +01002167 if (s->check.state & CHK_ST_CONFIGURED) {
2168 nbcheck++;
2169 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2170 (!mininter || mininter > srv_getinter(&s->check)))
2171 mininter = srv_getinter(&s->check);
2172 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002173
Willy Tarreaud8514a22013-12-11 21:10:14 +01002174 if (s->agent.state & CHK_ST_CONFIGURED) {
2175 nbcheck++;
2176 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2177 (!mininter || mininter > srv_getinter(&s->agent)))
2178 mininter = srv_getinter(&s->agent);
2179 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002180 }
2181 }
2182
Simon Horman4a741432013-02-23 15:35:38 +09002183 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002184 return 0;
2185
2186 srand((unsigned)time(NULL));
2187
2188 /*
2189 * 2- start them as far as possible from each others. For this, we will
2190 * start them after their interval set to the min interval divided by
2191 * the number of servers, weighted by the server's position in the list.
2192 */
2193 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002194 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2195 if (init_pid_list()) {
2196 Alert("Starting [%s] check: out of memory.\n", px->id);
2197 return -1;
2198 }
2199 }
2200
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002201 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002202 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002203 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002204 if (s->check.type == PR_O2_EXT_CHK) {
2205 if (!prepare_external_check(&s->check))
2206 return -1;
2207 }
Simon Hormand60d6912013-11-25 10:46:36 +09002208 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2209 return -1;
2210 srvpos++;
2211 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002212
Simon Hormand60d6912013-11-25 10:46:36 +09002213 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002214 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002215 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2216 return -1;
2217 }
2218 srvpos++;
2219 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002220 }
2221 }
2222 return 0;
2223}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224
2225/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002226 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002227 * The buffer MUST be terminated by a null byte before calling this function.
2228 * Sets server status appropriately. The caller is responsible for ensuring
2229 * that the buffer contains at least 13 characters. If <done> is zero, we may
2230 * return 0 to indicate that data is required to decide of a match.
2231 */
2232static int httpchk_expect(struct server *s, int done)
2233{
2234 static char status_msg[] = "HTTP status check returned code <000>";
2235 char status_code[] = "000";
2236 char *contentptr;
2237 int crlf;
2238 int ret;
2239
2240 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2241 case PR_O2_EXP_STS:
2242 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002243 memcpy(status_code, s->check.bi->data + 9, 3);
2244 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002245
2246 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2247 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2248 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002249 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002250
2251 /* we necessarily have the response, so there are no partial failures */
2252 if (s->proxy->options2 & PR_O2_EXP_INV)
2253 ret = !ret;
2254
Simon Horman4a741432013-02-23 15:35:38 +09002255 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002256 break;
2257
2258 case PR_O2_EXP_STR:
2259 case PR_O2_EXP_RSTR:
2260 /* very simple response parser: ignore CR and only count consecutive LFs,
2261 * stop with contentptr pointing to first char after the double CRLF or
2262 * to '\0' if crlf < 2.
2263 */
2264 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002265 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002266 if (crlf >= 2)
2267 break;
2268 if (*contentptr == '\r')
2269 continue;
2270 else if (*contentptr == '\n')
2271 crlf++;
2272 else
2273 crlf = 0;
2274 }
2275
2276 /* Check that response contains a body... */
2277 if (crlf < 2) {
2278 if (!done)
2279 return 0;
2280
Simon Horman4a741432013-02-23 15:35:38 +09002281 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002282 "HTTP content check could not find a response body");
2283 return 1;
2284 }
2285
2286 /* Check that response body is not empty... */
2287 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002288 if (!done)
2289 return 0;
2290
Simon Horman4a741432013-02-23 15:35:38 +09002291 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002292 "HTTP content check found empty response body");
2293 return 1;
2294 }
2295
2296 /* Check the response content against the supplied string
2297 * or regex... */
2298 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2299 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2300 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002301 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002302
2303 /* if we don't match, we may need to wait more */
2304 if (!ret && !done)
2305 return 0;
2306
2307 if (ret) {
2308 /* content matched */
2309 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002310 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002311 "HTTP check matched unwanted content");
2312 else
Simon Horman4a741432013-02-23 15:35:38 +09002313 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002314 "HTTP content check matched");
2315 }
2316 else {
2317 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002318 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002319 "HTTP check did not match unwanted content");
2320 else
Simon Horman4a741432013-02-23 15:35:38 +09002321 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002322 "HTTP content check did not match");
2323 }
2324 break;
2325 }
2326 return 1;
2327}
2328
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002329/*
2330 * return the id of a step in a send/expect session
2331 */
2332static int tcpcheck_get_step_id(struct server *s)
2333{
2334 struct tcpcheck_rule *cur = NULL, *next = NULL;
2335 int i = 0;
2336
Willy Tarreau213c6782014-10-02 14:51:02 +02002337 /* not even started anything yet => step 0 = initial connect */
2338 if (!s->check.current_step)
2339 return 0;
2340
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002341 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002342
2343 /* no step => first step */
2344 if (cur == NULL)
2345 return 1;
2346
2347 /* increment i until current step */
2348 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
2349 if (next->list.p == &cur->list)
2350 break;
2351 ++i;
2352 }
2353
2354 return i;
2355}
2356
2357static void tcpcheck_main(struct connection *conn)
2358{
2359 char *contentptr;
Willy Tarreauf3d34822014-12-08 12:11:28 +01002360 struct tcpcheck_rule *cur, *next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002361 int done = 0, ret = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002362 struct check *check = conn->owner;
2363 struct server *s = check->server;
2364 struct task *t = check->task;
Willy Tarreauef953952014-10-02 14:30:14 +02002365 struct list *head = &s->proxy->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002366
Willy Tarreauef953952014-10-02 14:30:14 +02002367 /* here, we know that the check is complete or that it failed */
2368 if (check->result != CHK_RES_UNKNOWN)
2369 goto out_end_tcpcheck;
2370
2371 /* We have 4 possibilities here :
2372 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2373 * connection attempt was made yet ;
2374 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2375 * does not exist (no rule), so a connection attempt was made
2376 * before coming here.
2377 * 3. we're coming back after having started with step 1, so we may
2378 * be waiting for a connection attempt to complete.
2379 * 4. the connection + handshake are complete
2380 *
2381 * #2 and #3 are quite similar, we want both the connection and the
2382 * handshake to complete before going any further. Thus we must always
2383 * wait for a connection to complete unless we're before and existing
2384 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002385 */
Willy Tarreauef953952014-10-02 14:30:14 +02002386 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
2387 (check->current_step || LIST_ISEMPTY(head))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002388 /* we allow up to min(inter, timeout.connect) for a connection
2389 * to establish but only when timeout.check is set
2390 * as it may be to short for a full check otherwise
2391 */
2392 while (tick_is_expired(t->expire, now_ms)) {
2393 int t_con;
2394
2395 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2396 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2397
2398 if (s->proxy->timeout.check)
2399 t->expire = tick_first(t->expire, t_con);
2400 }
2401 return;
2402 }
2403
Willy Tarreauef953952014-10-02 14:30:14 +02002404 /* special case: option tcp-check with no rule, a connect is enough */
2405 if (LIST_ISEMPTY(head)) {
2406 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002407 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002408 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002409
Willy Tarreau213c6782014-10-02 14:51:02 +02002410 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002411 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002412 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002413 check->bo->p = check->bo->data;
2414 check->bo->o = 0;
2415 check->bi->p = check->bi->data;
2416 check->bi->i = 0;
2417 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2418 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2419 if (s->proxy->timeout.check)
2420 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2421 }
2422 /* keep on processing step */
2423 else {
2424 cur = check->current_step;
2425 }
2426
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002427 /* It's only the rules which will enable send/recv */
2428 __conn_data_stop_both(conn);
2429
Willy Tarreauabca5b62013-12-06 14:19:25 +01002430 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002431 /* we have to try to flush the output buffer before reading, at the end,
2432 * or if we're about to send a string that does not fit in the remaining space.
2433 */
2434 if (check->bo->o &&
2435 (&cur->list == head ||
2436 check->current_step->action != TCPCHK_ACT_SEND ||
2437 check->current_step->string_len >= buffer_total_space(check->bo))) {
2438
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002439 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002440 if (conn->flags & CO_FL_ERROR) {
2441 chk_report_conn_err(conn, errno, 0);
2442 __conn_data_stop_both(conn);
2443 goto out_end_tcpcheck;
2444 }
2445 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002446 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002447 }
2448
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002449 /* did we reach the end ? If so, let's check that everything was sent */
2450 if (&cur->list == head) {
2451 if (check->bo->o)
2452 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002453 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002454 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002455
Willy Tarreauf3d34822014-12-08 12:11:28 +01002456 /* have 'next' point to the next rule or NULL if we're on the last one */
2457 next = (struct tcpcheck_rule *)cur->list.n;
2458 if (&next->list == head)
2459 next = NULL;
2460
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002461 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2462 struct protocol *proto;
2463 struct xprt_ops *xprt;
2464
2465 /* mark the step as started */
2466 check->last_started_step = check->current_step;
2467 /* first, shut existing connection */
2468 conn_force_close(conn);
2469
2470 /* prepare new connection */
2471 /* initialization */
2472 conn_init(conn);
2473 conn_attach(conn, check, &check_conn_cb);
2474 conn->target = &s->obj_type;
2475
2476 /* no client address */
2477 clear_addr(&conn->addr.from);
2478
Willy Tarreau640556c2014-05-09 23:38:15 +02002479 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002480 /* we'll connect to the check addr specified on the server */
2481 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002482 proto = s->check_common.proto;
2483 }
2484 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002485 /* we'll connect to the addr on the server */
2486 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002487 proto = s->proto;
2488 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002489
2490 /* port */
2491 if (check->current_step->port)
2492 set_host_port(&conn->addr.to, check->current_step->port);
2493 else if (check->port)
2494 set_host_port(&conn->addr.to, check->port);
2495
2496#ifdef USE_OPENSSL
2497 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2498 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002499 }
2500 else {
2501 xprt = &raw_sock;
2502 }
2503#else /* USE_OPENSSL */
2504 xprt = &raw_sock;
2505#endif /* USE_OPENSSL */
2506 conn_prepare(conn, proto, xprt);
2507
2508 ret = SN_ERR_INTERNAL;
2509 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002510 ret = proto->connect(conn,
2511 1 /* I/O polling is always needed */,
2512 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002513 conn->flags |= CO_FL_WAKE_DATA;
2514 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2515 conn->send_proxy_ofs = 1;
2516 conn->flags |= CO_FL_SEND_PROXY;
2517 }
2518
2519 /* It can return one of :
2520 * - SN_ERR_NONE if everything's OK
2521 * - SN_ERR_SRVTO if there are no more servers
2522 * - SN_ERR_SRVCL if the connection was refused by the server
2523 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2524 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2525 * - SN_ERR_INTERNAL for any other purely internal errors
2526 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2527 * Note that we try to prevent the network stack from sending the ACK during the
2528 * connect() when a pure TCP check is used (without PROXY protocol).
2529 */
2530 switch (ret) {
2531 case SN_ERR_NONE:
2532 /* we allow up to min(inter, timeout.connect) for a connection
2533 * to establish but only when timeout.check is set
2534 * as it may be to short for a full check otherwise
2535 */
2536 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2537
2538 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2539 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2540 t->expire = tick_first(t->expire, t_con);
2541 }
2542 break;
2543 case SN_ERR_SRVTO: /* ETIMEDOUT */
2544 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2545 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2546 tcpcheck_get_step_id(s), strerror(errno));
2547 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2548 goto out_end_tcpcheck;
2549 case SN_ERR_PRXCOND:
2550 case SN_ERR_RESOURCE:
2551 case SN_ERR_INTERNAL:
2552 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2553 tcpcheck_get_step_id(s));
2554 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2555 goto out_end_tcpcheck;
2556 }
2557
2558 /* allow next rule */
2559 cur = (struct tcpcheck_rule *)cur->list.n;
2560 check->current_step = cur;
2561
2562 /* don't do anything until the connection is established */
2563 if (!(conn->flags & CO_FL_CONNECTED)) {
2564 /* update expire time, should be done by process_chk */
2565 /* we allow up to min(inter, timeout.connect) for a connection
2566 * to establish but only when timeout.check is set
2567 * as it may be to short for a full check otherwise
2568 */
2569 while (tick_is_expired(t->expire, now_ms)) {
2570 int t_con;
2571
2572 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2573 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2574
2575 if (s->proxy->timeout.check)
2576 t->expire = tick_first(t->expire, t_con);
2577 }
2578 return;
2579 }
2580
2581 } /* end 'connect' */
2582 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2583 /* mark the step as started */
2584 check->last_started_step = check->current_step;
2585
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002586 /* reset the read buffer */
2587 if (*check->bi->data != '\0') {
2588 *check->bi->data = '\0';
2589 check->bi->i = 0;
2590 }
2591
2592 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2593 conn->flags |= CO_FL_ERROR;
2594 chk_report_conn_err(conn, 0, 0);
2595 goto out_end_tcpcheck;
2596 }
2597
Willy Tarreauabca5b62013-12-06 14:19:25 +01002598 if (check->current_step->string_len >= check->bo->size) {
2599 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2600 check->current_step->string_len, check->bo->size,
2601 tcpcheck_get_step_id(s));
2602 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2603 goto out_end_tcpcheck;
2604 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002605
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002606 /* do not try to send if there is no space */
2607 if (check->current_step->string_len >= buffer_total_space(check->bo))
2608 continue;
2609
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002610 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2611 *check->bo->p = '\0'; /* to make gdb output easier to read */
2612
Willy Tarreauabca5b62013-12-06 14:19:25 +01002613 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002614 cur = (struct tcpcheck_rule *)cur->list.n;
2615 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002616 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002617 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002618 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002619 goto out_end_tcpcheck;
2620
Willy Tarreau310987a2014-01-22 19:46:33 +01002621 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002622 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2623 done = 1;
2624 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2625 /* Report network errors only if we got no other data. Otherwise
2626 * we'll let the upper layers decide whether the response is OK
2627 * or not. It is very common that an RST sent by the server is
2628 * reported as an error just after the last data chunk.
2629 */
2630 chk_report_conn_err(conn, errno, 0);
2631 goto out_end_tcpcheck;
2632 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002633 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002634 else
2635 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002636 }
2637
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002638 /* mark the step as started */
2639 check->last_started_step = check->current_step;
2640
2641
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002642 /* Intermediate or complete response received.
2643 * Terminate string in check->bi->data buffer.
2644 */
2645 if (check->bi->i < check->bi->size) {
2646 check->bi->data[check->bi->i] = '\0';
2647 }
2648 else {
2649 check->bi->data[check->bi->i - 1] = '\0';
2650 done = 1; /* buffer full, don't wait for more data */
2651 }
2652
2653 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002654
2655 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002656 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002657 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002658 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002659
2660 /* empty response */
2661 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2662 tcpcheck_get_step_id(s));
2663 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2664
2665 goto out_end_tcpcheck;
2666 }
2667
2668 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002669 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002670
Willy Tarreaua970c282013-12-06 12:47:19 +01002671 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002672 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002673 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002674 else if (cur->expect_regex != NULL)
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002675 ret = regex_exec(cur->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002676
2677 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002678 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002679
2680 /* matched */
2681 if (ret) {
2682 /* matched but we did not want to => ERROR */
2683 if (cur->inverse) {
2684 /* we were looking for a string */
2685 if (cur->string != NULL) {
2686 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2687 cur->string, tcpcheck_get_step_id(s));
2688 }
2689 else {
2690 /* we were looking for a regex */
2691 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2692 tcpcheck_get_step_id(s));
2693 }
2694 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2695 goto out_end_tcpcheck;
2696 }
2697 /* matched and was supposed to => OK, next step */
2698 else {
2699 cur = (struct tcpcheck_rule*)cur->list.n;
2700 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002701 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002702 goto tcpcheck_expect;
2703 __conn_data_stop_recv(conn);
2704 }
2705 }
2706 else {
2707 /* not matched */
2708 /* not matched and was not supposed to => OK, next step */
2709 if (cur->inverse) {
2710 cur = (struct tcpcheck_rule*)cur->list.n;
2711 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002712 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002713 goto tcpcheck_expect;
2714 __conn_data_stop_recv(conn);
2715 }
2716 /* not matched but was supposed to => ERROR */
2717 else {
2718 /* we were looking for a string */
2719 if (cur->string != NULL) {
2720 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2721 cur->string, tcpcheck_get_step_id(s));
2722 }
2723 else {
2724 /* we were looking for a regex */
2725 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2726 tcpcheck_get_step_id(s));
2727 }
2728 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2729 goto out_end_tcpcheck;
2730 }
2731 }
2732 } /* end expect */
2733 } /* end loop over double chained step list */
2734
2735 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2736 goto out_end_tcpcheck;
2737
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002738 out_need_io:
2739 if (check->bo->o)
2740 __conn_data_want_send(conn);
2741
2742 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2743 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002744 return;
2745
2746 out_end_tcpcheck:
2747 /* collect possible new errors */
2748 if (conn->flags & CO_FL_ERROR)
2749 chk_report_conn_err(conn, 0, 0);
2750
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002751 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002752 check->current_step = NULL;
2753
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002754 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002755 conn->flags |= CO_FL_ERROR;
2756
2757 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002758
2759 return;
2760}
2761
2762
Willy Tarreaubd741542010-03-16 18:46:54 +01002763/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002764 * Local variables:
2765 * c-indent-level: 8
2766 * c-basic-offset: 8
2767 * End:
2768 */