blob: 1b5b7319bd4daa6b8a93eccb11717aa5dd4172b1 [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
Cyril Bontéac92a062014-12-27 22:28:38 +010099const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
100 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
101 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
102 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
103 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
104 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
105 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
106 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
107 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
108 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
111};
112
Simon Horman63a4a822012-03-19 07:24:41 +0900113static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100114 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
115
116 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
117 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
118
119 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
120 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
121 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
122 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
123
124 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
125 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
126 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
127};
128
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200129/*
130 * Convert check_status code to description
131 */
132const char *get_check_status_description(short check_status) {
133
134 const char *desc;
135
136 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200137 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200138 else
139 desc = NULL;
140
141 if (desc && *desc)
142 return desc;
143 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200144 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200145}
146
147/*
148 * Convert check_status code to short info
149 */
150const char *get_check_status_info(short check_status) {
151
152 const char *info;
153
154 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200155 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200156 else
157 info = NULL;
158
159 if (info && *info)
160 return info;
161 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200162 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200163}
164
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100165const char *get_analyze_status(short analyze_status) {
166
167 const char *desc;
168
169 if (analyze_status < HANA_STATUS_SIZE)
170 desc = analyze_statuses[analyze_status].desc;
171 else
172 desc = NULL;
173
174 if (desc && *desc)
175 return desc;
176 else
177 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
178}
179
Willy Tarreaua150cf12014-05-20 21:57:23 +0200180/* Builds a string containing some information about the health check's result.
181 * The output string is allocated from the trash chunks. If the check is NULL,
182 * NULL is returned. This is designed to be used when emitting logs about health
183 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200184 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200185static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200186{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200187 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200188
Willy Tarreaua150cf12014-05-20 21:57:23 +0200189 if (!check)
190 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200191
Willy Tarreaua150cf12014-05-20 21:57:23 +0200192 msg = get_trash_chunk();
193 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200194
Willy Tarreaua150cf12014-05-20 21:57:23 +0200195 if (check->status >= HCHK_STATUS_L57DATA)
196 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200197
Willy Tarreaua150cf12014-05-20 21:57:23 +0200198 if (*check->desc) {
199 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200200
Willy Tarreaua150cf12014-05-20 21:57:23 +0200201 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200202
Willy Tarreaua150cf12014-05-20 21:57:23 +0200203 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
204 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200205
Willy Tarreaua150cf12014-05-20 21:57:23 +0200206 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200207 }
208
Willy Tarreaua150cf12014-05-20 21:57:23 +0200209 if (check->duration >= 0)
210 chunk_appendf(msg, ", check duration: %ldms", check->duration);
211
212 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200213}
214
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200215/*
Simon Horman4a741432013-02-23 15:35:38 +0900216 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200217 * an adequate CHK_RES_* value. The new check->health is computed based
218 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200219 *
220 * Show information in logs about failed health check if server is UP
221 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200222 */
Simon Horman4a741432013-02-23 15:35:38 +0900223static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100224{
Simon Horman4a741432013-02-23 15:35:38 +0900225 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200226 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200227 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900228
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200229 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100230 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900231 check->desc[0] = '\0';
232 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200233 return;
234 }
235
Simon Horman4a741432013-02-23 15:35:38 +0900236 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237 return;
238
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200239 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900240 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
241 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200242 } else
Simon Horman4a741432013-02-23 15:35:38 +0900243 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200244
Simon Horman4a741432013-02-23 15:35:38 +0900245 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200246 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900247 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200248
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100249 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900250 check->duration = -1;
251 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200252 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900253 check->duration = tv_ms_elapsed(&check->start, &now);
254 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200255 }
256
Willy Tarreau23964182014-05-20 20:56:30 +0200257 /* no change is expected if no state change occurred */
258 if (check->result == CHK_RES_NEUTRAL)
259 return;
260
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200261 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200262
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200263 switch (check->result) {
264 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200265 /* Failure to connect to the agent as a secondary check should not
266 * cause the server to be marked down.
267 */
268 if ((!(check->state & CHK_ST_AGENT) ||
269 (check->status >= HCHK_STATUS_L7TOUT)) &&
270 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200271 s->counters.failed_checks++;
272 report = 1;
273 check->health--;
274 if (check->health < check->rise)
275 check->health = 0;
276 }
277 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200278
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200279 case CHK_RES_PASSED:
280 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
281 if ((check->health < check->rise + check->fall - 1) &&
282 (check->result == CHK_RES_PASSED || check->health > 0)) {
283 report = 1;
284 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200286 if (check->health >= check->rise)
287 check->health = check->rise + check->fall - 1; /* OK now */
288 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200289
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200290 /* clear consecutive_errors if observing is enabled */
291 if (s->onerror)
292 s->consecutive_errors = 0;
293 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100294
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200295 default:
296 break;
297 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200298
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200299 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
300 (status != prev_status || report)) {
301 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200302 "%s check for %sserver %s/%s %s%s",
303 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200304 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100305 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100306 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200307 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200308
Willy Tarreaua150cf12014-05-20 21:57:23 +0200309 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200310
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100311 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200312 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
313 (check->health >= check->rise) ? check->fall : check->rise,
314 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200315
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100316 Warning("%s.\n", trash.str);
317 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200318 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200319}
320
Willy Tarreau4eec5472014-05-20 22:32:27 +0200321/* Marks the check <check>'s server down if the current check is already failed
322 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200324static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325{
Simon Horman4a741432013-02-23 15:35:38 +0900326 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900327
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200328 /* The agent secondary check should only cause a server to be marked
329 * as down if check->status is HCHK_STATUS_L7STS, which indicates
330 * that the agent returned "fail", "stopped" or "down".
331 * The implication here is that failure to connect to the agent
332 * as a secondary check should not cause the server to be marked
333 * down. */
334 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
335 return;
336
Willy Tarreau4eec5472014-05-20 22:32:27 +0200337 if (check->health > 0)
338 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100339
Willy Tarreau4eec5472014-05-20 22:32:27 +0200340 /* We only report a reason for the check if we did not do so previously */
341 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342}
343
Willy Tarreauaf549582014-05-16 17:37:50 +0200344/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200345 * it isn't in maintenance, it is not tracking a down server and other checks
346 * comply. The rule is simple : by default, a server is up, unless any of the
347 * following conditions is true :
348 * - health check failed (check->health < rise)
349 * - agent check failed (agent->health < rise)
350 * - the server tracks a down server (track && track->state == STOPPED)
351 * Note that if the server has a slowstart, it will switch to STARTING instead
352 * of RUNNING. Also, only the health checks support the nolb mode, so the
353 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200354 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200355static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200356{
Simon Horman4a741432013-02-23 15:35:38 +0900357 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100358
Willy Tarreauaf549582014-05-16 17:37:50 +0200359 if (s->admin & SRV_ADMF_MAINT)
360 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100361
Willy Tarreau3e048382014-05-21 10:30:54 +0200362 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200363 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100364
Willy Tarreau3e048382014-05-21 10:30:54 +0200365 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
366 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100367
Willy Tarreau3e048382014-05-21 10:30:54 +0200368 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
369 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200370
Willy Tarreau3e048382014-05-21 10:30:54 +0200371 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
372 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100373
Willy Tarreau3e048382014-05-21 10:30:54 +0200374 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 +0100375}
376
Willy Tarreaudb58b792014-05-21 13:57:23 +0200377/* Marks the check <check> as valid and tries to set its server into stopping mode
378 * if it was running or starting, and provided it isn't in maintenance and other
379 * checks comply. The conditions for the server to be marked in stopping mode are
380 * the same as for it to be turned up. Also, only the health checks support the
381 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200382 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200383static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200384{
Simon Horman4a741432013-02-23 15:35:38 +0900385 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100386
Willy Tarreauaf549582014-05-16 17:37:50 +0200387 if (s->admin & SRV_ADMF_MAINT)
388 return;
389
Willy Tarreaudb58b792014-05-21 13:57:23 +0200390 if (check->state & CHK_ST_AGENT)
391 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100392
Willy Tarreaudb58b792014-05-21 13:57:23 +0200393 if (s->track && s->track->state == SRV_ST_STOPPED)
394 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100395
Willy Tarreaudb58b792014-05-21 13:57:23 +0200396 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
397 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100398
Willy Tarreaudb58b792014-05-21 13:57:23 +0200399 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
400 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100401
Willy Tarreaudb58b792014-05-21 13:57:23 +0200402 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 +0100403}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100405/* note: use health_adjust() only, which first checks that the observe mode is
406 * enabled.
407 */
408void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100409{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100410 int failed;
411 int expire;
412
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100413 if (s->observe >= HANA_OBS_SIZE)
414 return;
415
Willy Tarreaubb956662013-01-24 00:37:39 +0100416 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100417 return;
418
419 switch (analyze_statuses[status].lr[s->observe - 1]) {
420 case 1:
421 failed = 1;
422 break;
423
424 case 2:
425 failed = 0;
426 break;
427
428 default:
429 return;
430 }
431
432 if (!failed) {
433 /* good: clear consecutive_errors */
434 s->consecutive_errors = 0;
435 return;
436 }
437
438 s->consecutive_errors++;
439
440 if (s->consecutive_errors < s->consecutive_errors_limit)
441 return;
442
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100443 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
444 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100445
446 switch (s->onerror) {
447 case HANA_ONERR_FASTINTER:
448 /* force fastinter - nothing to do here as all modes force it */
449 break;
450
451 case HANA_ONERR_SUDDTH:
452 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900453 if (s->check.health > s->check.rise)
454 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100455
456 /* no break - fall through */
457
458 case HANA_ONERR_FAILCHK:
459 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900460 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200461 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100462 break;
463
464 case HANA_ONERR_MARKDWN:
465 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900466 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900467 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200468 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100469 break;
470
471 default:
472 /* write a warning? */
473 break;
474 }
475
476 s->consecutive_errors = 0;
477 s->counters.failed_hana++;
478
Simon Horman66183002013-02-23 10:16:43 +0900479 if (s->check.fastinter) {
480 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300481 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200482 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300483 /* requeue check task with new expire */
484 task_queue(s->check.task);
485 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100486 }
487}
488
Willy Tarreaua1dab552014-04-14 15:04:54 +0200489static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100490{
491 int sv_state;
492 int ratio;
493 int hlen = 0;
494 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
495 "UP %d/%d", "UP",
496 "NOLB %d/%d", "NOLB",
497 "no check" };
498
499 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
500 hlen += 24;
501
Willy Tarreauff5ae352013-12-11 20:36:34 +0100502 if (!(s->check.state & CHK_ST_ENABLED))
503 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200504 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900505 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100506 sv_state = 3; /* UP */
507 else
508 sv_state = 2; /* going down */
509
Willy Tarreau892337c2014-05-13 23:41:20 +0200510 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100511 sv_state += 2;
512 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900513 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100514 sv_state = 1; /* going up */
515 else
516 sv_state = 0; /* DOWN */
517 }
518
Willy Tarreaua1dab552014-04-14 15:04:54 +0200519 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100520 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200521 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
522 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100523
Willy Tarreaua1dab552014-04-14 15:04:54 +0200524 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 +0100525 s->proxy->id, s->id,
526 global.node,
527 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
528 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
529 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
530 s->nbpend);
531
Willy Tarreau892337c2014-05-13 23:41:20 +0200532 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100533 now.tv_sec < s->last_change + s->slowstart &&
534 now.tv_sec >= s->last_change) {
535 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200536 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100537 }
538
539 buffer[hlen++] = '\r';
540 buffer[hlen++] = '\n';
541
542 return hlen;
543}
544
Willy Tarreau20a18342013-12-05 00:31:46 +0100545/* Check the connection. If an error has already been reported or the socket is
546 * closed, keep errno intact as it is supposed to contain the valid error code.
547 * If no error is reported, check the socket's error queue using getsockopt().
548 * Warning, this must be done only once when returning from poll, and never
549 * after an I/O error was attempted, otherwise the error queue might contain
550 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
551 * socket. Returns non-zero if an error was reported, zero if everything is
552 * clean (including a properly closed socket).
553 */
554static int retrieve_errno_from_socket(struct connection *conn)
555{
556 int skerr;
557 socklen_t lskerr = sizeof(skerr);
558
559 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
560 return 1;
561
Willy Tarreau3c728722014-01-23 13:50:42 +0100562 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100563 return 0;
564
565 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
566 errno = skerr;
567
568 if (errno == EAGAIN)
569 errno = 0;
570
571 if (!errno) {
572 /* we could not retrieve an error, that does not mean there is
573 * none. Just don't change anything and only report the prior
574 * error if any.
575 */
576 if (conn->flags & CO_FL_ERROR)
577 return 1;
578 else
579 return 0;
580 }
581
582 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
583 return 1;
584}
585
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100586/* Try to collect as much information as possible on the connection status,
587 * and adjust the server status accordingly. It may make use of <errno_bck>
588 * if non-null when the caller is absolutely certain of its validity (eg:
589 * checked just after a syscall). If the caller doesn't have a valid errno,
590 * it can pass zero, and retrieve_errno_from_socket() will be called to try
591 * to extract errno from the socket. If no error is reported, it will consider
592 * the <expired> flag. This is intended to be used when a connection error was
593 * reported in conn->flags or when a timeout was reported in <expired>. The
594 * function takes care of not updating a server status which was already set.
595 * All situations where at least one of <expired> or CO_FL_ERROR are set
596 * produce a status.
597 */
598static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
599{
600 struct check *check = conn->owner;
601 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200602 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200603 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100604
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100605 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100606 return;
607
608 errno = errno_bck;
609 if (!errno || errno == EAGAIN)
610 retrieve_errno_from_socket(conn);
611
612 if (!(conn->flags & CO_FL_ERROR) && !expired)
613 return;
614
615 /* we'll try to build a meaningful error message depending on the
616 * context of the error possibly present in conn->err_code, and the
617 * socket error possibly collected above. This is useful to know the
618 * exact step of the L6 layer (eg: SSL handshake).
619 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200620 chk = get_trash_chunk();
621
622 if (check->type == PR_O2_TCPCHK_CHK) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200623 step = tcpcheck_get_step_id(check->server);
624 if (!step)
625 chunk_printf(chk, " at initial connection step of tcp-check");
626 else {
627 chunk_printf(chk, " at step %d of tcp-check", step);
628 /* we were looking for a string */
629 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
630 if (check->last_started_step->port)
631 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
632 else
633 chunk_appendf(chk, " (connect)");
634 }
635 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
636 if (check->last_started_step->string)
637 chunk_appendf(chk, " (string '%s')", check->last_started_step->string);
638 else if (check->last_started_step->expect_regex)
639 chunk_appendf(chk, " (expect regex)");
640 }
641 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
642 chunk_appendf(chk, " (send)");
643 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200644 }
645 }
646
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100647 if (conn->err_code) {
648 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200649 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100650 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200651 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100652 err_msg = trash.str;
653 }
654 else {
655 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200656 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100657 err_msg = trash.str;
658 }
659 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200660 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100661 }
662 }
663
664 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
665 /* L4 not established (yet) */
666 if (conn->flags & CO_FL_ERROR)
667 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
668 else if (expired)
669 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
670 }
671 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
672 /* L6 not established (yet) */
673 if (conn->flags & CO_FL_ERROR)
674 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
675 else if (expired)
676 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
677 }
678 else if (conn->flags & CO_FL_ERROR) {
679 /* I/O error after connection was established and before we could diagnose */
680 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
681 }
682 else if (expired) {
683 /* connection established but expired check */
684 if (check->type == PR_O2_SSL3_CHK)
685 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
686 else /* HTTP, SMTP, ... */
687 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
688 }
689
690 return;
691}
692
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693/*
694 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200695 * the connection acknowledgement. If the proxy requires L7 health-checks,
696 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900697 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200699static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700{
Simon Horman4a741432013-02-23 15:35:38 +0900701 struct check *check = conn->owner;
702 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900703 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200704
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100705 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100706 goto out_wakeup;
707
Willy Tarreau310987a2014-01-22 19:46:33 +0100708 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100709 return;
710
Willy Tarreau20a18342013-12-05 00:31:46 +0100711 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100712 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100713 __conn_data_stop_both(conn);
714 goto out_wakeup;
715 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100716
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100717 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100718 /* if the output is closed, we can't do anything */
719 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100720 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100721 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200722 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200723
Willy Tarreau06559ac2013-12-05 01:53:08 +0100724 /* here, we know that the connection is established. That's enough for
725 * a pure TCP check.
726 */
727 if (!check->type)
728 goto out_wakeup;
729
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200730 if (check->type == PR_O2_TCPCHK_CHK) {
731 tcpcheck_main(conn);
732 return;
733 }
734
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100735 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100736 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100737 if (conn->flags & CO_FL_ERROR) {
738 chk_report_conn_err(conn, errno, 0);
739 __conn_data_stop_both(conn);
740 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100742 if (check->bo->o)
743 return;
744 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200745
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100746 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
747 if (s->proxy->timeout.check) {
748 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
749 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100751 goto out_nowake;
752
Willy Tarreau83749182007-04-15 20:56:27 +0200753 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200754 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200755 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200756 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757}
758
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200760 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200761 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900762 * set_server_check_status() to update check->status, check->duration
763 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200764
765 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
766 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
767 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
768 * response to an SSL HELLO (the principle is that this is enough to
769 * distinguish between an SSL server and a pure TCP relay). All other cases will
770 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
771 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200773static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774{
Simon Horman4a741432013-02-23 15:35:38 +0900775 struct check *check = conn->owner;
776 struct server *s = check->server;
777 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200778 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100779 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200780 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200781
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100782 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200783 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200784
Willy Tarreau310987a2014-01-22 19:46:33 +0100785 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200786 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200787
788 if (check->type == PR_O2_TCPCHK_CHK) {
789 tcpcheck_main(conn);
790 return;
791 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200792
Willy Tarreau83749182007-04-15 20:56:27 +0200793 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
794 * but the connection was closed on the remote end. Fortunately, recv still
795 * works correctly and we don't need to do the getsockopt() on linux.
796 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000797
798 /* Set buffer to point to the end of the data already read, and check
799 * that there is free space remaining. If the buffer is full, proceed
800 * with running the checks without attempting another socket read.
801 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000802
Willy Tarreau03938182010-03-17 21:52:07 +0100803 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000804
Simon Horman4a741432013-02-23 15:35:38 +0900805 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200806 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100807 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900808 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200809 /* Report network errors only if we got no other data. Otherwise
810 * we'll let the upper layers decide whether the response is OK
811 * or not. It is very common that an RST sent by the server is
812 * reported as an error just after the last data chunk.
813 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100814 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100815 goto out_wakeup;
816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 }
818
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100819
Willy Tarreau03938182010-03-17 21:52:07 +0100820 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900821 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100822 */
Simon Horman4a741432013-02-23 15:35:38 +0900823 if (check->bi->i < check->bi->size)
824 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100825 else {
Simon Horman4a741432013-02-23 15:35:38 +0900826 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100827 done = 1; /* buffer full, don't wait for more data */
828 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200829
Nick Chalk57b1bf72010-03-16 15:50:46 +0000830 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900831 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200832 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900833 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100834 goto wait_more_data;
835
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100836 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900837 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
838 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
839 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
840 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
841 !isdigit((unsigned char) *(check->bi->data + 11))) {
842 cut_crlf(check->bi->data);
843 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200844
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100845 goto out_wakeup;
846 }
847
Simon Horman4a741432013-02-23 15:35:38 +0900848 check->code = str2uic(check->bi->data + 9);
849 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000850
Willy Tarreaubd741542010-03-16 18:46:54 +0100851 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200852 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000853 /* 404 may be accepted as "stopping" only if the server was up */
854 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900855 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000856 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100857 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
858 /* Run content verification check... We know we have at least 13 chars */
859 if (!httpchk_expect(s, done))
860 goto wait_more_data;
861 }
862 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900863 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100864 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900865 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100866 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000867 else {
868 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900869 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000870 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200871 break;
872
873 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900874 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100875 goto wait_more_data;
876
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100877 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900878 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
879 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200880 else
Simon Horman4a741432013-02-23 15:35:38 +0900881 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200882 break;
883
884 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900885 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100886 goto wait_more_data;
887
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200888 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900889 if ((check->bi->i < strlen("000\r")) ||
890 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
891 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
892 !isdigit((unsigned char) *(check->bi->data + 2))) {
893 cut_crlf(check->bi->data);
894 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200895
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200896 goto out_wakeup;
897 }
898
Simon Horman4a741432013-02-23 15:35:38 +0900899 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200900
Simon Horman4a741432013-02-23 15:35:38 +0900901 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200902 cut_crlf(desc);
903
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100904 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900905 if (*check->bi->data == '2')
906 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200907 else
Simon Horman4a741432013-02-23 15:35:38 +0900908 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200909 break;
910
Simon Hormana2b9dad2013-02-12 10:45:54 +0900911 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100912 int status = HCHK_STATUS_CHECKED;
913 const char *hs = NULL; /* health status */
914 const char *as = NULL; /* admin status */
915 const char *ps = NULL; /* performance status */
916 const char *err = NULL; /* first error to report */
917 const char *wrn = NULL; /* first warning to report */
918 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900919
Willy Tarreau81f5d942013-12-09 20:51:51 +0100920 /* We're getting an agent check response. The agent could
921 * have been disabled in the mean time with a long check
922 * still pending. It is important that we ignore the whole
923 * response.
924 */
925 if (!(check->server->agent.state & CHK_ST_ENABLED))
926 break;
927
928 /* The agent supports strings made of a single line ended by the
929 * first CR ('\r') or LF ('\n'). This line is composed of words
930 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
931 * line may optionally contained a description of a state change
932 * after a sharp ('#'), which is only considered if a health state
933 * is announced.
934 *
935 * Words may be composed of :
936 * - a numeric weight suffixed by the percent character ('%').
937 * - a health status among "up", "down", "stopped", and "fail".
938 * - an admin status among "ready", "drain", "maint".
939 *
940 * These words may appear in any order. If multiple words of the
941 * same category appear, the last one wins.
942 */
943
Willy Tarreau9809b782013-12-11 21:40:11 +0100944 p = check->bi->data;
945 while (*p && *p != '\n' && *p != '\r')
946 p++;
947
948 if (!*p) {
949 if (!done)
950 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900951
Willy Tarreau9809b782013-12-11 21:40:11 +0100952 /* at least inform the admin that the agent is mis-behaving */
953 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
954 break;
955 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100956
Willy Tarreau9809b782013-12-11 21:40:11 +0100957 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100958 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900959
Willy Tarreau81f5d942013-12-09 20:51:51 +0100960 while (*cmd) {
961 /* look for next word */
962 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
963 cmd++;
964 continue;
965 }
Simon Horman671b6f02013-11-25 10:46:39 +0900966
Willy Tarreau81f5d942013-12-09 20:51:51 +0100967 if (*cmd == '#') {
968 /* this is the beginning of a health status description,
969 * skip the sharp and blanks.
970 */
971 cmd++;
972 while (*cmd == '\t' || *cmd == ' ')
973 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900974 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900975 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100976
977 /* find the end of the word so that we have a null-terminated
978 * word between <cmd> and <p>.
979 */
980 p = cmd + 1;
981 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
982 p++;
983 if (*p)
984 *p++ = 0;
985
986 /* first, health statuses */
987 if (strcasecmp(cmd, "up") == 0) {
988 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900989 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100990 hs = cmd;
991 }
992 else if (strcasecmp(cmd, "down") == 0) {
993 check->health = 0;
994 status = HCHK_STATUS_L7STS;
995 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900996 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100997 else if (strcasecmp(cmd, "stopped") == 0) {
998 check->health = 0;
999 status = HCHK_STATUS_L7STS;
1000 hs = cmd;
1001 }
1002 else if (strcasecmp(cmd, "fail") == 0) {
1003 check->health = 0;
1004 status = HCHK_STATUS_L7STS;
1005 hs = cmd;
1006 }
1007 /* admin statuses */
1008 else if (strcasecmp(cmd, "ready") == 0) {
1009 as = cmd;
1010 }
1011 else if (strcasecmp(cmd, "drain") == 0) {
1012 as = cmd;
1013 }
1014 else if (strcasecmp(cmd, "maint") == 0) {
1015 as = cmd;
1016 }
1017 /* else try to parse a weight here and keep the last one */
1018 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1019 ps = cmd;
1020 }
1021 else {
1022 /* keep a copy of the first error */
1023 if (!err)
1024 err = cmd;
1025 }
1026 /* skip to next word */
1027 cmd = p;
1028 }
1029 /* here, cmd points either to \0 or to the beginning of a
1030 * description. Skip possible leading spaces.
1031 */
1032 while (*cmd == ' ' || *cmd == '\n')
1033 cmd++;
1034
1035 /* First, update the admin status so that we avoid sending other
1036 * possibly useless warnings and can also update the health if
1037 * present after going back up.
1038 */
1039 if (as) {
1040 if (strcasecmp(as, "drain") == 0)
1041 srv_adm_set_drain(check->server);
1042 else if (strcasecmp(as, "maint") == 0)
1043 srv_adm_set_maint(check->server);
1044 else
1045 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001046 }
1047
Willy Tarreau81f5d942013-12-09 20:51:51 +01001048 /* now change weights */
1049 if (ps) {
1050 const char *msg;
1051
1052 msg = server_parse_weight_change_request(s, ps);
1053 if (!wrn || !*wrn)
1054 wrn = msg;
1055 }
1056
1057 /* and finally health status */
1058 if (hs) {
1059 /* We'll report some of the warnings and errors we have
1060 * here. Down reports are critical, we leave them untouched.
1061 * Lack of report, or report of 'UP' leaves the room for
1062 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001063 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001064 const char *msg = cmd;
1065 struct chunk *t;
1066
1067 if (!*msg || status == HCHK_STATUS_L7OKD) {
1068 if (err && *err)
1069 msg = err;
1070 else if (wrn && *wrn)
1071 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001072 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001073
1074 t = get_trash_chunk();
1075 chunk_printf(t, "via agent : %s%s%s%s",
1076 hs, *msg ? " (" : "",
1077 msg, *msg ? ")" : "");
1078
1079 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001080 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001081 else if (err && *err) {
1082 /* No status change but we'd like to report something odd.
1083 * Just report the current state and copy the message.
1084 */
1085 chunk_printf(&trash, "agent reports an error : %s", err);
1086 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001087
Willy Tarreau81f5d942013-12-09 20:51:51 +01001088 }
1089 else if (wrn && *wrn) {
1090 /* No status change but we'd like to report something odd.
1091 * Just report the current state and copy the message.
1092 */
1093 chunk_printf(&trash, "agent warns : %s", wrn);
1094 set_server_check_status(check, status/*check->status*/, trash.str);
1095 }
1096 else
1097 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001098 break;
1099 }
1100
Willy Tarreau1620ec32011-08-06 17:05:02 +02001101 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001102 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001103 goto wait_more_data;
1104
Simon Horman4a741432013-02-23 15:35:38 +09001105 if (check->bi->data[0] == 'R') {
1106 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001107 }
1108 else {
Simon Horman4a741432013-02-23 15:35:38 +09001109 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1110 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001111 else
1112 desc = "PostgreSQL unknown error";
1113
Simon Horman4a741432013-02-23 15:35:38 +09001114 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001115 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001116 break;
1117
1118 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001119 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001120 goto wait_more_data;
1121
Simon Horman4a741432013-02-23 15:35:38 +09001122 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1123 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001124 }
1125 else {
Simon Horman4a741432013-02-23 15:35:38 +09001126 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001127 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001128 break;
1129
1130 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001131 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001132 goto wait_more_data;
1133
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001134 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001135 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001136 /* We set the MySQL Version in description for information purpose
1137 * FIXME : it can be cool to use MySQL Version for other purpose,
1138 * like mark as down old MySQL server.
1139 */
Simon Horman4a741432013-02-23 15:35:38 +09001140 if (check->bi->i > 51) {
1141 desc = ltrim(check->bi->data + 5, ' ');
1142 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001143 }
1144 else {
1145 if (!done)
1146 goto wait_more_data;
1147 /* it seems we have a OK packet but without a valid length,
1148 * it must be a protocol error
1149 */
Simon Horman4a741432013-02-23 15:35:38 +09001150 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001151 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001152 }
1153 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001154 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001155 desc = ltrim(check->bi->data + 7, ' ');
1156 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001157 }
1158 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001159 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1160 (((unsigned int) *(check->bi->data + 1)) << 8) +
1161 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001162
Simon Horman4a741432013-02-23 15:35:38 +09001163 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001164 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001165 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001166 /* We have only one MySQL packet and it is a Handshake Initialization packet
1167 * but we need to have a second packet to know if it is alright
1168 */
Simon Horman4a741432013-02-23 15:35:38 +09001169 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001170 goto wait_more_data;
1171 }
1172 else {
1173 /* We have only one packet and it is an Error packet,
1174 * an error message is attached, so we can display it
1175 */
Simon Horman4a741432013-02-23 15:35:38 +09001176 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001177 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001178 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001179 }
Simon Horman4a741432013-02-23 15:35:38 +09001180 } else if (check->bi->i > first_packet_len + 4) {
1181 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1182 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1183 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001184
Simon Horman4a741432013-02-23 15:35:38 +09001185 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001186 /* We have 2 packets and that's good */
1187 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001188 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001189 /* No error packet */
1190 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001191 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001192 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001193 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001194 }
1195 else {
1196 /* An error message is attached in the Error packet
1197 * so we can display it ! :)
1198 */
Simon Horman4a741432013-02-23 15:35:38 +09001199 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001200 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001201 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001202 }
1203 }
1204 }
1205 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001206 if (!done)
1207 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001208 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001209 * it must be a protocol error
1210 */
Simon Horman4a741432013-02-23 15:35:38 +09001211 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001212 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001213 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001214 }
1215 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001216 break;
1217
1218 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001219 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001220 goto wait_more_data;
1221
1222 /* Check if the server speaks LDAP (ASN.1/BER)
1223 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1224 * http://tools.ietf.org/html/rfc4511
1225 */
1226
1227 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1228 * LDAPMessage: 0x30: SEQUENCE
1229 */
Simon Horman4a741432013-02-23 15:35:38 +09001230 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1231 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001232 }
1233 else {
1234 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001235 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001236
1237 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1238 * messageID: 0x02 0x01 0x01: INTEGER 1
1239 * protocolOp: 0x61: bindResponse
1240 */
1241 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001242 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1243 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001244
1245 goto out_wakeup;
1246 }
1247
1248 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001249 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001250
1251 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1252 * ldapResult: 0x0a 0x01: ENUMERATION
1253 */
1254 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001255 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1256 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001257
1258 goto out_wakeup;
1259 }
1260
1261 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1262 * resultCode
1263 */
Simon Horman4a741432013-02-23 15:35:38 +09001264 check->code = *(check->bi->data + msglen + 9);
1265 if (check->code) {
1266 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 +02001267 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001268 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001269 }
1270 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001271 break;
1272
1273 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001274 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001275 break;
1276 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001277
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001278 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001279 /* collect possible new errors */
1280 if (conn->flags & CO_FL_ERROR)
1281 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282
Nick Chalk57b1bf72010-03-16 15:50:46 +00001283 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001284 *check->bi->data = '\0';
1285 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001286
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001287 /* Close the connection... We absolutely want to perform a hard close
1288 * and reset the connection if some data are pending, otherwise we end
1289 * up with many TIME_WAITs and eat all the source port range quickly.
1290 * To avoid sending RSTs all the time, we first try to drain pending
1291 * data.
1292 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001293 if (conn->xprt && conn->xprt->shutw)
1294 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001295
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001296 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001297 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001298 conn->flags |= CO_FL_ERROR;
1299
Willy Tarreaua522f802012-11-23 08:56:35 +01001300 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001301 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001302 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001303
1304 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001305 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306}
1307
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001308/*
1309 * This function is used only for server health-checks. It handles connection
1310 * status updates including errors. If necessary, it wakes the check task up.
1311 * It always returns 0.
1312 */
1313static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001314{
Simon Horman4a741432013-02-23 15:35:38 +09001315 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001316
Willy Tarreau6c560da2012-11-24 11:14:45 +01001317 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001318 /* We may get error reports bypassing the I/O handlers, typically
1319 * the case when sending a pure TCP check which fails, then the I/O
1320 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001321 * main processing task so let's simply wake it up. If we get here,
1322 * we expect errno to still be valid.
1323 */
1324 chk_report_conn_err(conn, errno, 0);
1325
Willy Tarreau2d351b62013-12-05 02:36:25 +01001326 __conn_data_stop_both(conn);
1327 task_wakeup(check->task, TASK_WOKEN_IO);
1328 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001329 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1330 /* we may get here if only a connection probe was required : we
1331 * don't have any data to send nor anything expected in response,
1332 * so the completion of the connection establishment is enough.
1333 */
1334 task_wakeup(check->task, TASK_WOKEN_IO);
1335 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001336
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001337 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001338 /* We're here because nobody wants to handle the error, so we
1339 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001340 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001341 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001342 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001343 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001344 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001345}
1346
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001347struct data_cb check_conn_cb = {
1348 .recv = event_srv_chk_r,
1349 .send = event_srv_chk_w,
1350 .wake = wake_srv_chk,
1351};
1352
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001354 * updates the server's weight during a warmup stage. Once the final weight is
1355 * reached, the task automatically stops. Note that any server status change
1356 * must have updated s->last_change accordingly.
1357 */
1358static struct task *server_warmup(struct task *t)
1359{
1360 struct server *s = t->context;
1361
1362 /* by default, plan on stopping the task */
1363 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001364 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001365 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001366 return t;
1367
Willy Tarreau892337c2014-05-13 23:41:20 +02001368 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001369 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001370
1371 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001372 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001373
1374 /* get back there in 1 second or 1/20th of the slowstart interval,
1375 * whichever is greater, resulting in small 5% steps.
1376 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001377 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001378 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1379 return t;
1380}
1381
1382/*
Simon Horman98637e52014-06-20 12:30:16 +09001383 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001384 *
1385 * It can return one of :
1386 * - SN_ERR_NONE if everything's OK and tcpcheck_main() was not called
1387 * - SN_ERR_UP if if everything's OK and tcpcheck_main() was called
1388 * - SN_ERR_SRVTO if there are no more servers
1389 * - SN_ERR_SRVCL if the connection was refused by the server
1390 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1391 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1392 * - SN_ERR_INTERNAL for any other purely internal errors
1393 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1394 * Note that we try to prevent the network stack from sending the ACK during the
1395 * connect() when a pure TCP check is used (without PROXY protocol).
1396 */
Simon Horman98637e52014-06-20 12:30:16 +09001397static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001398{
1399 struct check *check = t->context;
1400 struct server *s = check->server;
1401 struct connection *conn = check->conn;
1402 struct protocol *proto;
1403 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001404 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001405
1406 /* tcpcheck send/expect initialisation */
1407 if (check->type == PR_O2_TCPCHK_CHK)
1408 check->current_step = NULL;
1409
1410 /* prepare the check buffer.
1411 * This should not be used if check is the secondary agent check
1412 * of a server as s->proxy->check_req will relate to the
1413 * configuration of the primary check. Similarly, tcp-check uses
1414 * its own strings.
1415 */
1416 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1417 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1418
1419 /* we want to check if this host replies to HTTP or SSLv3 requests
1420 * so we'll send the request, and won't wake the checker up now.
1421 */
1422 if ((check->type) == PR_O2_SSL3_CHK) {
1423 /* SSL requires that we put Unix time in the request */
1424 int gmt_time = htonl(date.tv_sec);
1425 memcpy(check->bo->data + 11, &gmt_time, 4);
1426 }
1427 else if ((check->type) == PR_O2_HTTP_CHK) {
1428 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1429 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001430 /* prevent HTTP keep-alive when "http-check expect" is used */
1431 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1432 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001433 bo_putstr(check->bo, "\r\n");
1434 *check->bo->p = '\0'; /* to make gdb output easier to read */
1435 }
1436 }
1437
1438 /* prepare a new connection */
1439 conn_init(conn);
Cyril Bonté9ce13112014-11-15 22:41:27 +01001440 conn_prepare(conn, s->check_common.proto, check->xprt);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001441 conn_attach(conn, check, &check_conn_cb);
1442 conn->target = &s->obj_type;
1443
1444 /* no client address */
1445 clear_addr(&conn->addr.from);
1446
1447 if (is_addr(&s->check_common.addr)) {
1448
1449 /* we'll connect to the check addr specified on the server */
1450 conn->addr.to = s->check_common.addr;
1451 proto = s->check_common.proto;
1452 }
1453 else {
1454 /* we'll connect to the addr on the server */
1455 conn->addr.to = s->addr;
1456 proto = s->proto;
1457 }
1458
1459 if (check->port) {
1460 set_host_port(&conn->addr.to, check->port);
1461 }
1462
Willy Tarreauf3d34822014-12-08 12:11:28 +01001463 /* only plain tcp-check supports quick ACK */
1464 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1465
Willy Tarreaud2a49592014-12-08 11:52:28 +01001466 if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(&s->proxy->tcpcheck_rules)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001467 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1468 /* if first step is a 'connect', then tcpcheck_main must run it */
1469 if (r->action == TCPCHK_ACT_CONNECT) {
1470 tcpcheck_main(conn);
1471 return SN_ERR_UP;
1472 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001473 if (r->action == TCPCHK_ACT_EXPECT)
1474 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001475 }
1476
1477 ret = SN_ERR_INTERNAL;
1478 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001479 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001480 conn->flags |= CO_FL_WAKE_DATA;
1481 if (s->check.send_proxy) {
1482 conn->send_proxy_ofs = 1;
1483 conn->flags |= CO_FL_SEND_PROXY;
1484 }
1485
1486 return ret;
1487}
1488
Simon Horman98637e52014-06-20 12:30:16 +09001489static struct list pid_list = LIST_HEAD_INIT(pid_list);
1490static struct pool_head *pool2_pid_list;
1491
1492void block_sigchld(void)
1493{
1494 sigset_t set;
1495 sigemptyset(&set);
1496 sigaddset(&set, SIGCHLD);
1497 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1498}
1499
1500void unblock_sigchld(void)
1501{
1502 sigset_t set;
1503 sigemptyset(&set);
1504 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1505}
1506
1507/* Call with SIGCHLD blocked */
1508static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1509{
1510 struct pid_list *elem;
1511 struct check *check = t->context;
1512
1513 elem = pool_alloc2(pool2_pid_list);
1514 if (!elem)
1515 return NULL;
1516 elem->pid = pid;
1517 elem->t = t;
1518 elem->exited = 0;
1519 check->curpid = elem;
1520 LIST_INIT(&elem->list);
1521 LIST_ADD(&pid_list, &elem->list);
1522 return elem;
1523}
1524
1525/* Blocks blocks and then unblocks SIGCHLD */
1526static void pid_list_del(struct pid_list *elem)
1527{
1528 struct check *check;
1529
1530 if (!elem)
1531 return;
1532
1533 block_sigchld();
1534 LIST_DEL(&elem->list);
1535 unblock_sigchld();
1536 if (!elem->exited)
1537 kill(elem->pid, SIGTERM);
1538
1539 check = elem->t->context;
1540 check->curpid = NULL;
1541 pool_free2(pool2_pid_list, elem);
1542}
1543
1544/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1545static void pid_list_expire(pid_t pid, int status)
1546{
1547 struct pid_list *elem;
1548
1549 list_for_each_entry(elem, &pid_list, list) {
1550 if (elem->pid == pid) {
1551 elem->t->expire = now_ms;
1552 elem->status = status;
1553 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001554 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001555 return;
1556 }
1557 }
1558}
1559
1560static void sigchld_handler(int signal)
1561{
1562 pid_t pid;
1563 int status;
1564 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1565 pid_list_expire(pid, status);
1566}
1567
1568static int init_pid_list(void) {
1569 struct sigaction action = {
1570 .sa_handler = sigchld_handler,
1571 .sa_flags = SA_NOCLDSTOP
1572 };
1573
1574 if (pool2_pid_list != NULL)
1575 /* Nothing to do */
1576 return 0;
1577
1578 if (sigaction(SIGCHLD, &action, NULL)) {
1579 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1580 strerror(errno));
1581 return 1;
1582 }
1583
1584 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1585 if (pool2_pid_list == NULL) {
1586 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1587 strerror(errno));
1588 return 1;
1589 }
1590
1591 return 0;
1592}
1593
Cyril Bontéac92a062014-12-27 22:28:38 +01001594/* helper macro to set an environment variable and jump to a specific label on failure. */
1595#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001596
1597/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001598 * helper function to allocate enough memory to store an environment variable.
1599 * It will also check that the environment variable is updatable, and silently
1600 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001601 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001602static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001603{
1604 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001605 char *envname;
1606 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001607
Cyril Bontéac92a062014-12-27 22:28:38 +01001608 if (idx < 0 || idx >= EXTCHK_SIZE) {
1609 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1610 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001611 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001612
1613 envname = extcheck_envs[idx].name;
1614 vmaxlen = extcheck_envs[idx].vmaxlen;
1615
1616 /* Check if the environment variable is already set, and silently reject
1617 * the update if this one is not updatable. */
1618 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1619 return 0;
1620
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001621 /* Instead of sending NOT_USED, sending an empty value is preferable */
1622 if (strcmp(value, "NOT_USED") == 0) {
1623 value = "";
1624 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001625
1626 len = strlen(envname) + 1;
1627 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1628 len += strlen(value);
1629 else
1630 len += vmaxlen;
1631
1632 if (!check->envp[idx])
1633 check->envp[idx] = malloc(len + 1);
1634
1635 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001636 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1637 return 1;
1638 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001639 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001640 if (ret < 0) {
1641 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1642 return 1;
1643 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001644 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001645 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1646 return 1;
1647 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001648 return 0;
1649}
Simon Horman98637e52014-06-20 12:30:16 +09001650
1651static int prepare_external_check(struct check *check)
1652{
1653 struct server *s = check->server;
1654 struct proxy *px = s->proxy;
1655 struct listener *listener = NULL, *l;
1656 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001657 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001658 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001659
1660 list_for_each_entry(l, &px->conf.listeners, by_fe)
1661 /* Use the first INET, INET6 or UNIX listener */
1662 if (l->addr.ss_family == AF_INET ||
1663 l->addr.ss_family == AF_INET6 ||
1664 l->addr.ss_family == AF_UNIX) {
1665 listener = l;
1666 break;
1667 }
1668
Simon Horman98637e52014-06-20 12:30:16 +09001669 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001670 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1671 if (!check->envp) {
1672 Alert("Failed to allocate memory for environment variables. Aborting\n");
1673 goto err;
1674 }
Simon Horman98637e52014-06-20 12:30:16 +09001675
Cyril Bontéac92a062014-12-27 22:28:38 +01001676 check->argv = calloc(6, sizeof(char *));
1677 if (!check->argv) {
1678 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001679 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001680 }
Simon Horman98637e52014-06-20 12:30:16 +09001681
1682 check->argv[0] = px->check_command;
1683
Cyril Bonté777be862014-12-02 21:21:35 +01001684 if (!listener) {
1685 check->argv[1] = strdup("NOT_USED");
1686 check->argv[2] = strdup("NOT_USED");
1687 }
1688 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001689 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001690 addr_to_str(&listener->addr, buf, sizeof(buf));
1691 check->argv[1] = strdup(buf);
1692 port_to_str(&listener->addr, buf, sizeof(buf));
1693 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001694 }
1695 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001696 const struct sockaddr_un *un;
1697
1698 un = (struct sockaddr_un *)&listener->addr;
1699 check->argv[1] = strdup(un->sun_path);
1700 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001701 }
1702 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001703 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001704 goto err;
1705 }
1706
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001707 addr_to_str(&s->addr, buf, sizeof(buf));
1708 check->argv[3] = strdup(buf);
1709 port_to_str(&s->addr, buf, sizeof(buf));
1710 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001711
Cyril Bontéac92a062014-12-27 22:28:38 +01001712 for (i = 0; i < 5; i++) {
1713 if (!check->argv[i]) {
1714 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001715 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001716 }
1717 }
Simon Horman98637e52014-06-20 12:30:16 +09001718
Cyril Bontéac92a062014-12-27 22:28:38 +01001719 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001720 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001721 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1722 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1723 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1724 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001725 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001726 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1727 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1728 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1729 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1730 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1731 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1732
1733 /* Ensure that we don't leave any hole in check->envp */
1734 for (i = 0; i < EXTCHK_SIZE; i++)
1735 if (!check->envp[i])
1736 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001737
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001738 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001739err:
1740 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001741 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001742 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001743 free(check->envp);
1744 check->envp = NULL;
1745 }
1746
1747 if (check->argv) {
1748 for (i = 1; i < 5; i++)
1749 free(check->argv[i]);
1750 free(check->argv);
1751 check->argv = NULL;
1752 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001753 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001754}
1755
Simon Hormanb00d17a2014-06-13 16:18:16 +09001756/*
Simon Horman98637e52014-06-20 12:30:16 +09001757 * establish a server health-check that makes use of a process.
1758 *
1759 * It can return one of :
1760 * - SN_ERR_NONE if everything's OK
1761 * - SN_ERR_SRVTO if there are no more servers
1762 * - SN_ERR_SRVCL if the connection was refused by the server
1763 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1764 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1765 * - SN_ERR_INTERNAL for any other purely internal errors
1766 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1767 *
1768 * Blocks and then unblocks SIGCHLD
1769 */
1770static int connect_proc_chk(struct task *t)
1771{
Cyril Bontéac92a062014-12-27 22:28:38 +01001772 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001773 struct check *check = t->context;
1774 struct server *s = check->server;
1775 struct proxy *px = s->proxy;
1776 int status;
1777 pid_t pid;
1778
Simon Horman98637e52014-06-20 12:30:16 +09001779 status = SN_ERR_RESOURCE;
1780
1781 block_sigchld();
1782
1783 pid = fork();
1784 if (pid < 0) {
1785 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1786 strerror(errno));
1787 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1788 goto out;
1789 }
1790 if (pid == 0) {
1791 /* Child */
1792 extern char **environ;
1793 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001794 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001795 execvp(px->check_command, check->argv);
1796 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1797 strerror(errno));
1798 exit(-1);
1799 }
1800
1801 /* Parent */
1802 if (check->result == CHK_RES_UNKNOWN) {
1803 if (pid_list_add(pid, t) != NULL) {
1804 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1805
1806 if (px->timeout.check && px->timeout.connect) {
1807 int t_con = tick_add(now_ms, px->timeout.connect);
1808 t->expire = tick_first(t->expire, t_con);
1809 }
1810 status = SN_ERR_NONE;
1811 goto out;
1812 }
1813 else {
1814 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1815 }
1816 kill(pid, SIGTERM); /* process creation error */
1817 }
1818 else
1819 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1820
1821out:
1822 unblock_sigchld();
1823 return status;
1824}
1825
1826/*
1827 * establish a server health-check.
1828 *
1829 * It can return one of :
1830 * - SN_ERR_NONE if everything's OK
1831 * - SN_ERR_SRVTO if there are no more servers
1832 * - SN_ERR_SRVCL if the connection was refused by the server
1833 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1834 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1835 * - SN_ERR_INTERNAL for any other purely internal errors
1836 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1837 */
1838static int connect_chk(struct task *t)
1839{
1840 struct check *check = t->context;
1841
1842 if (check->type == PR_O2_EXT_CHK)
1843 return connect_proc_chk(t);
1844 return connect_conn_chk(t);
1845}
1846
1847/*
1848 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001849 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1850 */
Simon Horman98637e52014-06-20 12:30:16 +09001851static struct task *process_chk_proc(struct task *t)
1852{
1853 struct check *check = t->context;
1854 struct server *s = check->server;
1855 struct connection *conn = check->conn;
1856 int rv;
1857 int ret;
1858 int expired = tick_is_expired(t->expire, now_ms);
1859
1860 if (!(check->state & CHK_ST_INPROGRESS)) {
1861 /* no check currently running */
1862 if (!expired) /* woke up too early */
1863 return t;
1864
1865 /* we don't send any health-checks when the proxy is
1866 * stopped, the server should not be checked or the check
1867 * is disabled.
1868 */
1869 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1870 s->proxy->state == PR_STSTOPPED)
1871 goto reschedule;
1872
1873 /* we'll initiate a new check */
1874 set_server_check_status(check, HCHK_STATUS_START, NULL);
1875
1876 check->state |= CHK_ST_INPROGRESS;
1877
1878 ret = connect_chk(t);
1879 switch (ret) {
1880 case SN_ERR_UP:
1881 return t;
1882 case SN_ERR_NONE:
1883 /* we allow up to min(inter, timeout.connect) for a connection
1884 * to establish but only when timeout.check is set
1885 * as it may be to short for a full check otherwise
1886 */
1887 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1888
1889 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1890 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1891 t->expire = tick_first(t->expire, t_con);
1892 }
1893
1894 goto reschedule;
1895
1896 case SN_ERR_SRVTO: /* ETIMEDOUT */
1897 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1898 conn->flags |= CO_FL_ERROR;
1899 chk_report_conn_err(conn, errno, 0);
1900 break;
1901 case SN_ERR_PRXCOND:
1902 case SN_ERR_RESOURCE:
1903 case SN_ERR_INTERNAL:
1904 conn->flags |= CO_FL_ERROR;
1905 chk_report_conn_err(conn, 0, 0);
1906 break;
1907 }
1908
1909 /* here, we have seen a synchronous error, no fd was allocated */
1910
1911 check->state &= ~CHK_ST_INPROGRESS;
1912 check_notify_failure(check);
1913
1914 /* we allow up to min(inter, timeout.connect) for a connection
1915 * to establish but only when timeout.check is set
1916 * as it may be to short for a full check otherwise
1917 */
1918 while (tick_is_expired(t->expire, now_ms)) {
1919 int t_con;
1920
1921 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1922 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1923
1924 if (s->proxy->timeout.check)
1925 t->expire = tick_first(t->expire, t_con);
1926 }
1927 }
1928 else {
1929 /* there was a test running.
1930 * First, let's check whether there was an uncaught error,
1931 * which can happen on connect timeout or error.
1932 */
1933 if (check->result == CHK_RES_UNKNOWN) {
1934 /* good connection is enough for pure TCP check */
1935 struct pid_list *elem = check->curpid;
1936 int status = HCHK_STATUS_UNKNOWN;
1937
1938 if (elem->exited) {
1939 status = elem->status; /* Save in case the process exits between use below */
1940 if (!WIFEXITED(status))
1941 check->code = -1;
1942 else
1943 check->code = WEXITSTATUS(status);
1944 if (!WIFEXITED(status) || WEXITSTATUS(status))
1945 status = HCHK_STATUS_PROCERR;
1946 else
1947 status = HCHK_STATUS_PROCOK;
1948 } else if (expired) {
1949 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001950 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001951 kill(elem->pid, SIGTERM);
1952 }
1953 set_server_check_status(check, status, NULL);
1954 }
1955
1956 if (check->result == CHK_RES_FAILED) {
1957 /* a failure or timeout detected */
1958 check_notify_failure(check);
1959 }
1960 else if (check->result == CHK_RES_CONDPASS) {
1961 /* check is OK but asks for stopping mode */
1962 check_notify_stopping(check);
1963 }
1964 else if (check->result == CHK_RES_PASSED) {
1965 /* a success was detected */
1966 check_notify_success(check);
1967 }
1968 check->state &= ~CHK_ST_INPROGRESS;
1969
1970 pid_list_del(check->curpid);
1971
1972 rv = 0;
1973 if (global.spread_checks > 0) {
1974 rv = srv_getinter(check) * global.spread_checks / 100;
1975 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1976 }
1977 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1978 }
1979
1980 reschedule:
1981 while (tick_is_expired(t->expire, now_ms))
1982 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1983 return t;
1984}
1985
1986/*
1987 * manages a server health-check that uses a connection. Returns
1988 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1989 */
1990static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001991{
Simon Horman4a741432013-02-23 15:35:38 +09001992 struct check *check = t->context;
1993 struct server *s = check->server;
1994 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001995 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001996 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001997 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001998
Willy Tarreau2c115e52013-12-11 19:41:16 +01001999 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002000 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002001 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002002 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002003
Simon Horman671b6f02013-11-25 10:46:39 +09002004 /* we don't send any health-checks when the proxy is
2005 * stopped, the server should not be checked or the check
2006 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002007 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002008 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002009 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002010 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011
2012 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002013 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002014
Willy Tarreau2c115e52013-12-11 19:41:16 +01002015 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002016 check->bi->p = check->bi->data;
2017 check->bi->i = 0;
2018 check->bo->p = check->bo->data;
2019 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002020
Simon Hormanb00d17a2014-06-13 16:18:16 +09002021 ret = connect_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002022 switch (ret) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09002023 case SN_ERR_UP:
2024 return t;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002025 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002026 /* we allow up to min(inter, timeout.connect) for a connection
2027 * to establish but only when timeout.check is set
2028 * as it may be to short for a full check otherwise
2029 */
Simon Horman4a741432013-02-23 15:35:38 +09002030 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002031
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002032 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2033 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2034 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002035 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002036
2037 if (check->type)
2038 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2039
Willy Tarreau5a78f362012-11-23 12:47:05 +01002040 goto reschedule;
2041
2042 case SN_ERR_SRVTO: /* ETIMEDOUT */
2043 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002044 conn->flags |= CO_FL_ERROR;
2045 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002046 break;
2047 case SN_ERR_PRXCOND:
2048 case SN_ERR_RESOURCE:
2049 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002050 conn->flags |= CO_FL_ERROR;
2051 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002052 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002053 }
2054
Willy Tarreau5a78f362012-11-23 12:47:05 +01002055 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002056
Willy Tarreau2c115e52013-12-11 19:41:16 +01002057 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002058 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002059
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002060 /* we allow up to min(inter, timeout.connect) for a connection
2061 * to establish but only when timeout.check is set
2062 * as it may be to short for a full check otherwise
2063 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002064 while (tick_is_expired(t->expire, now_ms)) {
2065 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002066
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002067 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002068 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002069
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002070 if (s->proxy->timeout.check)
2071 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002072 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002073 }
2074 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002075 /* there was a test running.
2076 * First, let's check whether there was an uncaught error,
2077 * which can happen on connect timeout or error.
2078 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002079 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002080 /* good connection is enough for pure TCP check */
2081 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002082 if (check->use_ssl)
2083 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002084 else
Simon Horman4a741432013-02-23 15:35:38 +09002085 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002086 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002087 else if ((conn->flags & CO_FL_ERROR) || expired) {
2088 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002089 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002090 else
2091 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002092 }
2093
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002094 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002095 if (conn->xprt) {
2096 /* The check was aborted and the connection was not yet closed.
2097 * This can happen upon timeout, or when an external event such
2098 * as a failed response coupled with "observe layer7" caused the
2099 * server state to be suddenly changed.
2100 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01002101 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002102 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002103 }
2104
Willy Tarreauaf549582014-05-16 17:37:50 +02002105 if (check->result == CHK_RES_FAILED) {
2106 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002107 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002108 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002109 else if (check->result == CHK_RES_CONDPASS) {
2110 /* check is OK but asks for stopping mode */
2111 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002112 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002113 else if (check->result == CHK_RES_PASSED) {
2114 /* a success was detected */
2115 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002116 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002117 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002118
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002119 rv = 0;
2120 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002121 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002122 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002123 }
Simon Horman4a741432013-02-23 15:35:38 +09002124 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002125 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002126
2127 reschedule:
2128 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002129 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002130 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002131 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002132}
2133
Simon Horman98637e52014-06-20 12:30:16 +09002134/*
2135 * manages a server health-check. Returns
2136 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2137 */
2138static struct task *process_chk(struct task *t)
2139{
2140 struct check *check = t->context;
2141
2142 if (check->type == PR_O2_EXT_CHK)
2143 return process_chk_proc(t);
2144 return process_chk_conn(t);
2145}
2146
Simon Horman5c942422013-11-25 10:46:32 +09002147static int start_check_task(struct check *check, int mininter,
2148 int nbcheck, int srvpos)
2149{
2150 struct task *t;
2151 /* task for the check */
2152 if ((t = task_new()) == NULL) {
2153 Alert("Starting [%s:%s] check: out of memory.\n",
2154 check->server->proxy->id, check->server->id);
2155 return 0;
2156 }
2157
2158 check->task = t;
2159 t->process = process_chk;
2160 t->context = check;
2161
Willy Tarreau1746eec2014-04-25 10:46:47 +02002162 if (mininter < srv_getinter(check))
2163 mininter = srv_getinter(check);
2164
2165 if (global.max_spread_checks && mininter > global.max_spread_checks)
2166 mininter = global.max_spread_checks;
2167
Simon Horman5c942422013-11-25 10:46:32 +09002168 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002169 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002170 check->start = now;
2171 task_queue(t);
2172
2173 return 1;
2174}
2175
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002176/*
2177 * Start health-check.
2178 * Returns 0 if OK, -1 if error, and prints the error in this case.
2179 */
2180int start_checks() {
2181
2182 struct proxy *px;
2183 struct server *s;
2184 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002185 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002186
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002187 /* 1- count the checkers to run simultaneously.
2188 * We also determine the minimum interval among all of those which
2189 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2190 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002191 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002192 * too short an interval for all others.
2193 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002194 for (px = proxy; px; px = px->next) {
2195 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002196 if (s->slowstart) {
2197 if ((t = task_new()) == NULL) {
2198 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2199 return -1;
2200 }
2201 /* We need a warmup task that will be called when the server
2202 * state switches from down to up.
2203 */
2204 s->warmup = t;
2205 t->process = server_warmup;
2206 t->context = s;
2207 t->expire = TICK_ETERNITY;
2208 }
2209
Willy Tarreaud8514a22013-12-11 21:10:14 +01002210 if (s->check.state & CHK_ST_CONFIGURED) {
2211 nbcheck++;
2212 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2213 (!mininter || mininter > srv_getinter(&s->check)))
2214 mininter = srv_getinter(&s->check);
2215 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002216
Willy Tarreaud8514a22013-12-11 21:10:14 +01002217 if (s->agent.state & CHK_ST_CONFIGURED) {
2218 nbcheck++;
2219 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2220 (!mininter || mininter > srv_getinter(&s->agent)))
2221 mininter = srv_getinter(&s->agent);
2222 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002223 }
2224 }
2225
Simon Horman4a741432013-02-23 15:35:38 +09002226 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002227 return 0;
2228
2229 srand((unsigned)time(NULL));
2230
2231 /*
2232 * 2- start them as far as possible from each others. For this, we will
2233 * start them after their interval set to the min interval divided by
2234 * the number of servers, weighted by the server's position in the list.
2235 */
2236 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002237 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2238 if (init_pid_list()) {
2239 Alert("Starting [%s] check: out of memory.\n", px->id);
2240 return -1;
2241 }
2242 }
2243
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002244 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002245 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002246 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002247 if (s->check.type == PR_O2_EXT_CHK) {
2248 if (!prepare_external_check(&s->check))
2249 return -1;
2250 }
Simon Hormand60d6912013-11-25 10:46:36 +09002251 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2252 return -1;
2253 srvpos++;
2254 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002255
Simon Hormand60d6912013-11-25 10:46:36 +09002256 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002257 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002258 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2259 return -1;
2260 }
2261 srvpos++;
2262 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002263 }
2264 }
2265 return 0;
2266}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002267
2268/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002269 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002270 * The buffer MUST be terminated by a null byte before calling this function.
2271 * Sets server status appropriately. The caller is responsible for ensuring
2272 * that the buffer contains at least 13 characters. If <done> is zero, we may
2273 * return 0 to indicate that data is required to decide of a match.
2274 */
2275static int httpchk_expect(struct server *s, int done)
2276{
2277 static char status_msg[] = "HTTP status check returned code <000>";
2278 char status_code[] = "000";
2279 char *contentptr;
2280 int crlf;
2281 int ret;
2282
2283 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2284 case PR_O2_EXP_STS:
2285 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002286 memcpy(status_code, s->check.bi->data + 9, 3);
2287 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002288
2289 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2290 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2291 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002292 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002293
2294 /* we necessarily have the response, so there are no partial failures */
2295 if (s->proxy->options2 & PR_O2_EXP_INV)
2296 ret = !ret;
2297
Simon Horman4a741432013-02-23 15:35:38 +09002298 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002299 break;
2300
2301 case PR_O2_EXP_STR:
2302 case PR_O2_EXP_RSTR:
2303 /* very simple response parser: ignore CR and only count consecutive LFs,
2304 * stop with contentptr pointing to first char after the double CRLF or
2305 * to '\0' if crlf < 2.
2306 */
2307 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002308 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002309 if (crlf >= 2)
2310 break;
2311 if (*contentptr == '\r')
2312 continue;
2313 else if (*contentptr == '\n')
2314 crlf++;
2315 else
2316 crlf = 0;
2317 }
2318
2319 /* Check that response contains a body... */
2320 if (crlf < 2) {
2321 if (!done)
2322 return 0;
2323
Simon Horman4a741432013-02-23 15:35:38 +09002324 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002325 "HTTP content check could not find a response body");
2326 return 1;
2327 }
2328
2329 /* Check that response body is not empty... */
2330 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002331 if (!done)
2332 return 0;
2333
Simon Horman4a741432013-02-23 15:35:38 +09002334 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002335 "HTTP content check found empty response body");
2336 return 1;
2337 }
2338
2339 /* Check the response content against the supplied string
2340 * or regex... */
2341 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2342 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2343 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002344 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002345
2346 /* if we don't match, we may need to wait more */
2347 if (!ret && !done)
2348 return 0;
2349
2350 if (ret) {
2351 /* content matched */
2352 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002353 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002354 "HTTP check matched unwanted content");
2355 else
Simon Horman4a741432013-02-23 15:35:38 +09002356 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002357 "HTTP content check matched");
2358 }
2359 else {
2360 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002361 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002362 "HTTP check did not match unwanted content");
2363 else
Simon Horman4a741432013-02-23 15:35:38 +09002364 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002365 "HTTP content check did not match");
2366 }
2367 break;
2368 }
2369 return 1;
2370}
2371
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002372/*
2373 * return the id of a step in a send/expect session
2374 */
2375static int tcpcheck_get_step_id(struct server *s)
2376{
2377 struct tcpcheck_rule *cur = NULL, *next = NULL;
2378 int i = 0;
2379
Willy Tarreau213c6782014-10-02 14:51:02 +02002380 /* not even started anything yet => step 0 = initial connect */
2381 if (!s->check.current_step)
2382 return 0;
2383
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002384 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002385
2386 /* no step => first step */
2387 if (cur == NULL)
2388 return 1;
2389
2390 /* increment i until current step */
2391 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
2392 if (next->list.p == &cur->list)
2393 break;
2394 ++i;
2395 }
2396
2397 return i;
2398}
2399
2400static void tcpcheck_main(struct connection *conn)
2401{
2402 char *contentptr;
Willy Tarreauf3d34822014-12-08 12:11:28 +01002403 struct tcpcheck_rule *cur, *next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002404 int done = 0, ret = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002405 struct check *check = conn->owner;
2406 struct server *s = check->server;
2407 struct task *t = check->task;
Willy Tarreauef953952014-10-02 14:30:14 +02002408 struct list *head = &s->proxy->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002409
Willy Tarreauef953952014-10-02 14:30:14 +02002410 /* here, we know that the check is complete or that it failed */
2411 if (check->result != CHK_RES_UNKNOWN)
2412 goto out_end_tcpcheck;
2413
2414 /* We have 4 possibilities here :
2415 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2416 * connection attempt was made yet ;
2417 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2418 * does not exist (no rule), so a connection attempt was made
2419 * before coming here.
2420 * 3. we're coming back after having started with step 1, so we may
2421 * be waiting for a connection attempt to complete.
2422 * 4. the connection + handshake are complete
2423 *
2424 * #2 and #3 are quite similar, we want both the connection and the
2425 * handshake to complete before going any further. Thus we must always
2426 * wait for a connection to complete unless we're before and existing
2427 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002428 */
Willy Tarreauef953952014-10-02 14:30:14 +02002429 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
2430 (check->current_step || LIST_ISEMPTY(head))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002431 /* we allow up to min(inter, timeout.connect) for a connection
2432 * to establish but only when timeout.check is set
2433 * as it may be to short for a full check otherwise
2434 */
2435 while (tick_is_expired(t->expire, now_ms)) {
2436 int t_con;
2437
2438 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2439 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2440
2441 if (s->proxy->timeout.check)
2442 t->expire = tick_first(t->expire, t_con);
2443 }
2444 return;
2445 }
2446
Willy Tarreauef953952014-10-02 14:30:14 +02002447 /* special case: option tcp-check with no rule, a connect is enough */
2448 if (LIST_ISEMPTY(head)) {
2449 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002450 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002451 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002452
Willy Tarreau213c6782014-10-02 14:51:02 +02002453 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002454 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002455 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002456 check->bo->p = check->bo->data;
2457 check->bo->o = 0;
2458 check->bi->p = check->bi->data;
2459 check->bi->i = 0;
2460 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2461 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2462 if (s->proxy->timeout.check)
2463 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2464 }
2465 /* keep on processing step */
2466 else {
2467 cur = check->current_step;
2468 }
2469
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002470 /* It's only the rules which will enable send/recv */
2471 __conn_data_stop_both(conn);
2472
Willy Tarreauabca5b62013-12-06 14:19:25 +01002473 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002474 /* we have to try to flush the output buffer before reading, at the end,
2475 * or if we're about to send a string that does not fit in the remaining space.
2476 */
2477 if (check->bo->o &&
2478 (&cur->list == head ||
2479 check->current_step->action != TCPCHK_ACT_SEND ||
2480 check->current_step->string_len >= buffer_total_space(check->bo))) {
2481
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002482 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002483 if (conn->flags & CO_FL_ERROR) {
2484 chk_report_conn_err(conn, errno, 0);
2485 __conn_data_stop_both(conn);
2486 goto out_end_tcpcheck;
2487 }
2488 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002489 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002490 }
2491
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002492 /* did we reach the end ? If so, let's check that everything was sent */
2493 if (&cur->list == head) {
2494 if (check->bo->o)
2495 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002496 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002497 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002498
Willy Tarreauf3d34822014-12-08 12:11:28 +01002499 /* have 'next' point to the next rule or NULL if we're on the last one */
2500 next = (struct tcpcheck_rule *)cur->list.n;
2501 if (&next->list == head)
2502 next = NULL;
2503
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002504 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2505 struct protocol *proto;
2506 struct xprt_ops *xprt;
2507
2508 /* mark the step as started */
2509 check->last_started_step = check->current_step;
2510 /* first, shut existing connection */
2511 conn_force_close(conn);
2512
2513 /* prepare new connection */
2514 /* initialization */
2515 conn_init(conn);
2516 conn_attach(conn, check, &check_conn_cb);
2517 conn->target = &s->obj_type;
2518
2519 /* no client address */
2520 clear_addr(&conn->addr.from);
2521
Willy Tarreau640556c2014-05-09 23:38:15 +02002522 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002523 /* we'll connect to the check addr specified on the server */
2524 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002525 proto = s->check_common.proto;
2526 }
2527 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002528 /* we'll connect to the addr on the server */
2529 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002530 proto = s->proto;
2531 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002532
2533 /* port */
2534 if (check->current_step->port)
2535 set_host_port(&conn->addr.to, check->current_step->port);
2536 else if (check->port)
2537 set_host_port(&conn->addr.to, check->port);
2538
2539#ifdef USE_OPENSSL
2540 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2541 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002542 }
2543 else {
2544 xprt = &raw_sock;
2545 }
2546#else /* USE_OPENSSL */
2547 xprt = &raw_sock;
2548#endif /* USE_OPENSSL */
2549 conn_prepare(conn, proto, xprt);
2550
2551 ret = SN_ERR_INTERNAL;
2552 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002553 ret = proto->connect(conn,
2554 1 /* I/O polling is always needed */,
2555 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002556 conn->flags |= CO_FL_WAKE_DATA;
2557 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2558 conn->send_proxy_ofs = 1;
2559 conn->flags |= CO_FL_SEND_PROXY;
2560 }
2561
2562 /* It can return one of :
2563 * - SN_ERR_NONE if everything's OK
2564 * - SN_ERR_SRVTO if there are no more servers
2565 * - SN_ERR_SRVCL if the connection was refused by the server
2566 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2567 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2568 * - SN_ERR_INTERNAL for any other purely internal errors
2569 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2570 * Note that we try to prevent the network stack from sending the ACK during the
2571 * connect() when a pure TCP check is used (without PROXY protocol).
2572 */
2573 switch (ret) {
2574 case SN_ERR_NONE:
2575 /* we allow up to min(inter, timeout.connect) for a connection
2576 * to establish but only when timeout.check is set
2577 * as it may be to short for a full check otherwise
2578 */
2579 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2580
2581 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2582 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2583 t->expire = tick_first(t->expire, t_con);
2584 }
2585 break;
2586 case SN_ERR_SRVTO: /* ETIMEDOUT */
2587 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2588 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2589 tcpcheck_get_step_id(s), strerror(errno));
2590 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2591 goto out_end_tcpcheck;
2592 case SN_ERR_PRXCOND:
2593 case SN_ERR_RESOURCE:
2594 case SN_ERR_INTERNAL:
2595 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2596 tcpcheck_get_step_id(s));
2597 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2598 goto out_end_tcpcheck;
2599 }
2600
2601 /* allow next rule */
2602 cur = (struct tcpcheck_rule *)cur->list.n;
2603 check->current_step = cur;
2604
2605 /* don't do anything until the connection is established */
2606 if (!(conn->flags & CO_FL_CONNECTED)) {
2607 /* update expire time, should be done by process_chk */
2608 /* we allow up to min(inter, timeout.connect) for a connection
2609 * to establish but only when timeout.check is set
2610 * as it may be to short for a full check otherwise
2611 */
2612 while (tick_is_expired(t->expire, now_ms)) {
2613 int t_con;
2614
2615 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2616 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2617
2618 if (s->proxy->timeout.check)
2619 t->expire = tick_first(t->expire, t_con);
2620 }
2621 return;
2622 }
2623
2624 } /* end 'connect' */
2625 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2626 /* mark the step as started */
2627 check->last_started_step = check->current_step;
2628
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002629 /* reset the read buffer */
2630 if (*check->bi->data != '\0') {
2631 *check->bi->data = '\0';
2632 check->bi->i = 0;
2633 }
2634
2635 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2636 conn->flags |= CO_FL_ERROR;
2637 chk_report_conn_err(conn, 0, 0);
2638 goto out_end_tcpcheck;
2639 }
2640
Willy Tarreauabca5b62013-12-06 14:19:25 +01002641 if (check->current_step->string_len >= check->bo->size) {
2642 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2643 check->current_step->string_len, check->bo->size,
2644 tcpcheck_get_step_id(s));
2645 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2646 goto out_end_tcpcheck;
2647 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002648
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002649 /* do not try to send if there is no space */
2650 if (check->current_step->string_len >= buffer_total_space(check->bo))
2651 continue;
2652
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002653 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2654 *check->bo->p = '\0'; /* to make gdb output easier to read */
2655
Willy Tarreauabca5b62013-12-06 14:19:25 +01002656 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002657 cur = (struct tcpcheck_rule *)cur->list.n;
2658 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002659 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002660 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002661 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002662 goto out_end_tcpcheck;
2663
Willy Tarreau310987a2014-01-22 19:46:33 +01002664 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002665 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2666 done = 1;
2667 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2668 /* Report network errors only if we got no other data. Otherwise
2669 * we'll let the upper layers decide whether the response is OK
2670 * or not. It is very common that an RST sent by the server is
2671 * reported as an error just after the last data chunk.
2672 */
2673 chk_report_conn_err(conn, errno, 0);
2674 goto out_end_tcpcheck;
2675 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002676 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002677 else
2678 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002679 }
2680
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002681 /* mark the step as started */
2682 check->last_started_step = check->current_step;
2683
2684
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002685 /* Intermediate or complete response received.
2686 * Terminate string in check->bi->data buffer.
2687 */
2688 if (check->bi->i < check->bi->size) {
2689 check->bi->data[check->bi->i] = '\0';
2690 }
2691 else {
2692 check->bi->data[check->bi->i - 1] = '\0';
2693 done = 1; /* buffer full, don't wait for more data */
2694 }
2695
2696 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002697
2698 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002699 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002700 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002701 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002702
2703 /* empty response */
2704 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2705 tcpcheck_get_step_id(s));
2706 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2707
2708 goto out_end_tcpcheck;
2709 }
2710
2711 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002712 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002713
Willy Tarreaua970c282013-12-06 12:47:19 +01002714 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002715 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002716 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002717 else if (cur->expect_regex != NULL)
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002718 ret = regex_exec(cur->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002719
2720 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002721 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002722
2723 /* matched */
2724 if (ret) {
2725 /* matched but we did not want to => ERROR */
2726 if (cur->inverse) {
2727 /* we were looking for a string */
2728 if (cur->string != NULL) {
2729 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2730 cur->string, tcpcheck_get_step_id(s));
2731 }
2732 else {
2733 /* we were looking for a regex */
2734 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2735 tcpcheck_get_step_id(s));
2736 }
2737 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2738 goto out_end_tcpcheck;
2739 }
2740 /* matched and was supposed to => OK, next step */
2741 else {
2742 cur = (struct tcpcheck_rule*)cur->list.n;
2743 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002744 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002745 goto tcpcheck_expect;
2746 __conn_data_stop_recv(conn);
2747 }
2748 }
2749 else {
2750 /* not matched */
2751 /* not matched and was not supposed to => OK, next step */
2752 if (cur->inverse) {
2753 cur = (struct tcpcheck_rule*)cur->list.n;
2754 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002755 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002756 goto tcpcheck_expect;
2757 __conn_data_stop_recv(conn);
2758 }
2759 /* not matched but was supposed to => ERROR */
2760 else {
2761 /* we were looking for a string */
2762 if (cur->string != NULL) {
2763 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2764 cur->string, tcpcheck_get_step_id(s));
2765 }
2766 else {
2767 /* we were looking for a regex */
2768 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2769 tcpcheck_get_step_id(s));
2770 }
2771 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2772 goto out_end_tcpcheck;
2773 }
2774 }
2775 } /* end expect */
2776 } /* end loop over double chained step list */
2777
2778 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2779 goto out_end_tcpcheck;
2780
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002781 out_need_io:
2782 if (check->bo->o)
2783 __conn_data_want_send(conn);
2784
2785 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2786 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002787 return;
2788
2789 out_end_tcpcheck:
2790 /* collect possible new errors */
2791 if (conn->flags & CO_FL_ERROR)
2792 chk_report_conn_err(conn, 0, 0);
2793
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002794 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002795 check->current_step = NULL;
2796
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002797 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002798 conn->flags |= CO_FL_ERROR;
2799
2800 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002801
2802 return;
2803}
2804
2805
Willy Tarreaubd741542010-03-16 18:46:54 +01002806/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002807 * Local variables:
2808 * c-indent-level: 8
2809 * c-basic-offset: 8
2810 * End:
2811 */