blob: 66247149fae8b708f57682b0e7ba15c58809d178 [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);
Simon Horman41f58762015-01-30 11:22:56 +09001440 conn_prepare(conn, check->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
Simon Horman41f58762015-01-30 11:22:56 +09001447 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001448
1449 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001450 conn->addr.to = check->addr;
1451 proto = check->proto;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001452 }
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/*
Simon Horman98637e52014-06-20 12:30:16 +09001827 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001828 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1829 */
Simon Horman98637e52014-06-20 12:30:16 +09001830static struct task *process_chk_proc(struct task *t)
1831{
1832 struct check *check = t->context;
1833 struct server *s = check->server;
1834 struct connection *conn = check->conn;
1835 int rv;
1836 int ret;
1837 int expired = tick_is_expired(t->expire, now_ms);
1838
1839 if (!(check->state & CHK_ST_INPROGRESS)) {
1840 /* no check currently running */
1841 if (!expired) /* woke up too early */
1842 return t;
1843
1844 /* we don't send any health-checks when the proxy is
1845 * stopped, the server should not be checked or the check
1846 * is disabled.
1847 */
1848 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1849 s->proxy->state == PR_STSTOPPED)
1850 goto reschedule;
1851
1852 /* we'll initiate a new check */
1853 set_server_check_status(check, HCHK_STATUS_START, NULL);
1854
1855 check->state |= CHK_ST_INPROGRESS;
1856
Simon Hormandbf70192015-01-30 11:22:53 +09001857 ret = connect_proc_chk(t);
Simon Horman98637e52014-06-20 12:30:16 +09001858 switch (ret) {
1859 case SN_ERR_UP:
1860 return t;
1861 case SN_ERR_NONE:
1862 /* we allow up to min(inter, timeout.connect) for a connection
1863 * to establish but only when timeout.check is set
1864 * as it may be to short for a full check otherwise
1865 */
1866 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1867
1868 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1869 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1870 t->expire = tick_first(t->expire, t_con);
1871 }
1872
1873 goto reschedule;
1874
1875 case SN_ERR_SRVTO: /* ETIMEDOUT */
1876 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1877 conn->flags |= CO_FL_ERROR;
1878 chk_report_conn_err(conn, errno, 0);
1879 break;
1880 case SN_ERR_PRXCOND:
1881 case SN_ERR_RESOURCE:
1882 case SN_ERR_INTERNAL:
1883 conn->flags |= CO_FL_ERROR;
1884 chk_report_conn_err(conn, 0, 0);
1885 break;
1886 }
1887
1888 /* here, we have seen a synchronous error, no fd was allocated */
1889
1890 check->state &= ~CHK_ST_INPROGRESS;
1891 check_notify_failure(check);
1892
1893 /* we allow up to min(inter, timeout.connect) for a connection
1894 * to establish but only when timeout.check is set
1895 * as it may be to short for a full check otherwise
1896 */
1897 while (tick_is_expired(t->expire, now_ms)) {
1898 int t_con;
1899
1900 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1901 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1902
1903 if (s->proxy->timeout.check)
1904 t->expire = tick_first(t->expire, t_con);
1905 }
1906 }
1907 else {
1908 /* there was a test running.
1909 * First, let's check whether there was an uncaught error,
1910 * which can happen on connect timeout or error.
1911 */
1912 if (check->result == CHK_RES_UNKNOWN) {
1913 /* good connection is enough for pure TCP check */
1914 struct pid_list *elem = check->curpid;
1915 int status = HCHK_STATUS_UNKNOWN;
1916
1917 if (elem->exited) {
1918 status = elem->status; /* Save in case the process exits between use below */
1919 if (!WIFEXITED(status))
1920 check->code = -1;
1921 else
1922 check->code = WEXITSTATUS(status);
1923 if (!WIFEXITED(status) || WEXITSTATUS(status))
1924 status = HCHK_STATUS_PROCERR;
1925 else
1926 status = HCHK_STATUS_PROCOK;
1927 } else if (expired) {
1928 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001929 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001930 kill(elem->pid, SIGTERM);
1931 }
1932 set_server_check_status(check, status, NULL);
1933 }
1934
1935 if (check->result == CHK_RES_FAILED) {
1936 /* a failure or timeout detected */
1937 check_notify_failure(check);
1938 }
1939 else if (check->result == CHK_RES_CONDPASS) {
1940 /* check is OK but asks for stopping mode */
1941 check_notify_stopping(check);
1942 }
1943 else if (check->result == CHK_RES_PASSED) {
1944 /* a success was detected */
1945 check_notify_success(check);
1946 }
1947 check->state &= ~CHK_ST_INPROGRESS;
1948
1949 pid_list_del(check->curpid);
1950
1951 rv = 0;
1952 if (global.spread_checks > 0) {
1953 rv = srv_getinter(check) * global.spread_checks / 100;
1954 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1955 }
1956 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1957 }
1958
1959 reschedule:
1960 while (tick_is_expired(t->expire, now_ms))
1961 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1962 return t;
1963}
1964
1965/*
1966 * manages a server health-check that uses a connection. Returns
1967 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1968 */
1969static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001970{
Simon Horman4a741432013-02-23 15:35:38 +09001971 struct check *check = t->context;
1972 struct server *s = check->server;
1973 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001974 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001975 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001976 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001977
Willy Tarreau2c115e52013-12-11 19:41:16 +01001978 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001979 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001980 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001981 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001982
Simon Horman671b6f02013-11-25 10:46:39 +09001983 /* we don't send any health-checks when the proxy is
1984 * stopped, the server should not be checked or the check
1985 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001986 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001987 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001988 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001989 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001990
1991 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001992 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001993
Willy Tarreau2c115e52013-12-11 19:41:16 +01001994 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001995 check->bi->p = check->bi->data;
1996 check->bi->i = 0;
1997 check->bo->p = check->bo->data;
1998 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001999
Simon Hormandbf70192015-01-30 11:22:53 +09002000 ret = connect_conn_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002001 switch (ret) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09002002 case SN_ERR_UP:
2003 return t;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002004 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002005 /* we allow up to min(inter, timeout.connect) for a connection
2006 * to establish but only when timeout.check is set
2007 * as it may be to short for a full check otherwise
2008 */
Simon Horman4a741432013-02-23 15:35:38 +09002009 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002010
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002011 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2012 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2013 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002014 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002015
2016 if (check->type)
2017 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2018
Willy Tarreau5a78f362012-11-23 12:47:05 +01002019 goto reschedule;
2020
2021 case SN_ERR_SRVTO: /* ETIMEDOUT */
2022 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002023 conn->flags |= CO_FL_ERROR;
2024 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002025 break;
2026 case SN_ERR_PRXCOND:
2027 case SN_ERR_RESOURCE:
2028 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002029 conn->flags |= CO_FL_ERROR;
2030 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002031 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002032 }
2033
Willy Tarreau5a78f362012-11-23 12:47:05 +01002034 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002035
Willy Tarreau2c115e52013-12-11 19:41:16 +01002036 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002037 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002038
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002039 /* we allow up to min(inter, timeout.connect) for a connection
2040 * to establish but only when timeout.check is set
2041 * as it may be to short for a full check otherwise
2042 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002043 while (tick_is_expired(t->expire, now_ms)) {
2044 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002045
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002046 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002047 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002048
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002049 if (s->proxy->timeout.check)
2050 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002051 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002052 }
2053 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002054 /* there was a test running.
2055 * First, let's check whether there was an uncaught error,
2056 * which can happen on connect timeout or error.
2057 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002058 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002059 /* good connection is enough for pure TCP check */
2060 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002061 if (check->use_ssl)
2062 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002063 else
Simon Horman4a741432013-02-23 15:35:38 +09002064 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002065 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002066 else if ((conn->flags & CO_FL_ERROR) || expired) {
2067 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002068 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002069 else
2070 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002071 }
2072
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002073 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002074 if (conn->xprt) {
2075 /* The check was aborted and the connection was not yet closed.
2076 * This can happen upon timeout, or when an external event such
2077 * as a failed response coupled with "observe layer7" caused the
2078 * server state to be suddenly changed.
2079 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01002080 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002081 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002082 }
2083
Willy Tarreauaf549582014-05-16 17:37:50 +02002084 if (check->result == CHK_RES_FAILED) {
2085 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002086 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002087 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002088 else if (check->result == CHK_RES_CONDPASS) {
2089 /* check is OK but asks for stopping mode */
2090 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002091 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002092 else if (check->result == CHK_RES_PASSED) {
2093 /* a success was detected */
2094 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002095 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002096 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002097
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002098 rv = 0;
2099 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002100 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002101 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002102 }
Simon Horman4a741432013-02-23 15:35:38 +09002103 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002104 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002105
2106 reschedule:
2107 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002108 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002109 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002110 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002111}
2112
Simon Horman98637e52014-06-20 12:30:16 +09002113/*
2114 * manages a server health-check. Returns
2115 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2116 */
2117static struct task *process_chk(struct task *t)
2118{
2119 struct check *check = t->context;
2120
2121 if (check->type == PR_O2_EXT_CHK)
2122 return process_chk_proc(t);
2123 return process_chk_conn(t);
2124}
2125
Simon Horman5c942422013-11-25 10:46:32 +09002126static int start_check_task(struct check *check, int mininter,
2127 int nbcheck, int srvpos)
2128{
2129 struct task *t;
2130 /* task for the check */
2131 if ((t = task_new()) == NULL) {
2132 Alert("Starting [%s:%s] check: out of memory.\n",
2133 check->server->proxy->id, check->server->id);
2134 return 0;
2135 }
2136
2137 check->task = t;
2138 t->process = process_chk;
2139 t->context = check;
2140
Willy Tarreau1746eec2014-04-25 10:46:47 +02002141 if (mininter < srv_getinter(check))
2142 mininter = srv_getinter(check);
2143
2144 if (global.max_spread_checks && mininter > global.max_spread_checks)
2145 mininter = global.max_spread_checks;
2146
Simon Horman5c942422013-11-25 10:46:32 +09002147 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002148 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002149 check->start = now;
2150 task_queue(t);
2151
2152 return 1;
2153}
2154
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002155/*
2156 * Start health-check.
2157 * Returns 0 if OK, -1 if error, and prints the error in this case.
2158 */
2159int start_checks() {
2160
2161 struct proxy *px;
2162 struct server *s;
2163 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002164 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002165
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002166 /* 1- count the checkers to run simultaneously.
2167 * We also determine the minimum interval among all of those which
2168 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2169 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002170 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002171 * too short an interval for all others.
2172 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002173 for (px = proxy; px; px = px->next) {
2174 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002175 if (s->slowstart) {
2176 if ((t = task_new()) == NULL) {
2177 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2178 return -1;
2179 }
2180 /* We need a warmup task that will be called when the server
2181 * state switches from down to up.
2182 */
2183 s->warmup = t;
2184 t->process = server_warmup;
2185 t->context = s;
2186 t->expire = TICK_ETERNITY;
2187 }
2188
Willy Tarreaud8514a22013-12-11 21:10:14 +01002189 if (s->check.state & CHK_ST_CONFIGURED) {
2190 nbcheck++;
2191 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2192 (!mininter || mininter > srv_getinter(&s->check)))
2193 mininter = srv_getinter(&s->check);
2194 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002195
Willy Tarreaud8514a22013-12-11 21:10:14 +01002196 if (s->agent.state & CHK_ST_CONFIGURED) {
2197 nbcheck++;
2198 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2199 (!mininter || mininter > srv_getinter(&s->agent)))
2200 mininter = srv_getinter(&s->agent);
2201 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002202 }
2203 }
2204
Simon Horman4a741432013-02-23 15:35:38 +09002205 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002206 return 0;
2207
2208 srand((unsigned)time(NULL));
2209
2210 /*
2211 * 2- start them as far as possible from each others. For this, we will
2212 * start them after their interval set to the min interval divided by
2213 * the number of servers, weighted by the server's position in the list.
2214 */
2215 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002216 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2217 if (init_pid_list()) {
2218 Alert("Starting [%s] check: out of memory.\n", px->id);
2219 return -1;
2220 }
2221 }
2222
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002223 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002224 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002225 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002226 if (s->check.type == PR_O2_EXT_CHK) {
2227 if (!prepare_external_check(&s->check))
2228 return -1;
2229 }
Simon Hormand60d6912013-11-25 10:46:36 +09002230 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2231 return -1;
2232 srvpos++;
2233 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002234
Simon Hormand60d6912013-11-25 10:46:36 +09002235 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002236 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002237 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2238 return -1;
2239 }
2240 srvpos++;
2241 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002242 }
2243 }
2244 return 0;
2245}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002246
2247/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002248 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002249 * The buffer MUST be terminated by a null byte before calling this function.
2250 * Sets server status appropriately. The caller is responsible for ensuring
2251 * that the buffer contains at least 13 characters. If <done> is zero, we may
2252 * return 0 to indicate that data is required to decide of a match.
2253 */
2254static int httpchk_expect(struct server *s, int done)
2255{
2256 static char status_msg[] = "HTTP status check returned code <000>";
2257 char status_code[] = "000";
2258 char *contentptr;
2259 int crlf;
2260 int ret;
2261
2262 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2263 case PR_O2_EXP_STS:
2264 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002265 memcpy(status_code, s->check.bi->data + 9, 3);
2266 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002267
2268 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2269 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2270 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002271 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002272
2273 /* we necessarily have the response, so there are no partial failures */
2274 if (s->proxy->options2 & PR_O2_EXP_INV)
2275 ret = !ret;
2276
Simon Horman4a741432013-02-23 15:35:38 +09002277 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002278 break;
2279
2280 case PR_O2_EXP_STR:
2281 case PR_O2_EXP_RSTR:
2282 /* very simple response parser: ignore CR and only count consecutive LFs,
2283 * stop with contentptr pointing to first char after the double CRLF or
2284 * to '\0' if crlf < 2.
2285 */
2286 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002287 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002288 if (crlf >= 2)
2289 break;
2290 if (*contentptr == '\r')
2291 continue;
2292 else if (*contentptr == '\n')
2293 crlf++;
2294 else
2295 crlf = 0;
2296 }
2297
2298 /* Check that response contains a body... */
2299 if (crlf < 2) {
2300 if (!done)
2301 return 0;
2302
Simon Horman4a741432013-02-23 15:35:38 +09002303 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002304 "HTTP content check could not find a response body");
2305 return 1;
2306 }
2307
2308 /* Check that response body is not empty... */
2309 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002310 if (!done)
2311 return 0;
2312
Simon Horman4a741432013-02-23 15:35:38 +09002313 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002314 "HTTP content check found empty response body");
2315 return 1;
2316 }
2317
2318 /* Check the response content against the supplied string
2319 * or regex... */
2320 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2321 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2322 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002323 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002324
2325 /* if we don't match, we may need to wait more */
2326 if (!ret && !done)
2327 return 0;
2328
2329 if (ret) {
2330 /* content matched */
2331 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002332 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002333 "HTTP check matched unwanted content");
2334 else
Simon Horman4a741432013-02-23 15:35:38 +09002335 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002336 "HTTP content check matched");
2337 }
2338 else {
2339 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002340 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002341 "HTTP check did not match unwanted content");
2342 else
Simon Horman4a741432013-02-23 15:35:38 +09002343 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002344 "HTTP content check did not match");
2345 }
2346 break;
2347 }
2348 return 1;
2349}
2350
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002351/*
2352 * return the id of a step in a send/expect session
2353 */
2354static int tcpcheck_get_step_id(struct server *s)
2355{
2356 struct tcpcheck_rule *cur = NULL, *next = NULL;
2357 int i = 0;
2358
Willy Tarreau213c6782014-10-02 14:51:02 +02002359 /* not even started anything yet => step 0 = initial connect */
2360 if (!s->check.current_step)
2361 return 0;
2362
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002363 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002364
2365 /* no step => first step */
2366 if (cur == NULL)
2367 return 1;
2368
2369 /* increment i until current step */
2370 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
2371 if (next->list.p == &cur->list)
2372 break;
2373 ++i;
2374 }
2375
2376 return i;
2377}
2378
2379static void tcpcheck_main(struct connection *conn)
2380{
2381 char *contentptr;
Willy Tarreauf3d34822014-12-08 12:11:28 +01002382 struct tcpcheck_rule *cur, *next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002383 int done = 0, ret = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002384 struct check *check = conn->owner;
2385 struct server *s = check->server;
2386 struct task *t = check->task;
Willy Tarreauef953952014-10-02 14:30:14 +02002387 struct list *head = &s->proxy->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002388
Willy Tarreauef953952014-10-02 14:30:14 +02002389 /* here, we know that the check is complete or that it failed */
2390 if (check->result != CHK_RES_UNKNOWN)
2391 goto out_end_tcpcheck;
2392
2393 /* We have 4 possibilities here :
2394 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2395 * connection attempt was made yet ;
2396 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2397 * does not exist (no rule), so a connection attempt was made
2398 * before coming here.
2399 * 3. we're coming back after having started with step 1, so we may
2400 * be waiting for a connection attempt to complete.
2401 * 4. the connection + handshake are complete
2402 *
2403 * #2 and #3 are quite similar, we want both the connection and the
2404 * handshake to complete before going any further. Thus we must always
2405 * wait for a connection to complete unless we're before and existing
2406 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002407 */
Willy Tarreauef953952014-10-02 14:30:14 +02002408 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
2409 (check->current_step || LIST_ISEMPTY(head))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002410 /* we allow up to min(inter, timeout.connect) for a connection
2411 * to establish but only when timeout.check is set
2412 * as it may be to short for a full check otherwise
2413 */
2414 while (tick_is_expired(t->expire, now_ms)) {
2415 int t_con;
2416
2417 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2418 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2419
2420 if (s->proxy->timeout.check)
2421 t->expire = tick_first(t->expire, t_con);
2422 }
2423 return;
2424 }
2425
Willy Tarreauef953952014-10-02 14:30:14 +02002426 /* special case: option tcp-check with no rule, a connect is enough */
2427 if (LIST_ISEMPTY(head)) {
2428 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002429 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002430 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002431
Willy Tarreau213c6782014-10-02 14:51:02 +02002432 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002433 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002434 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002435 check->bo->p = check->bo->data;
2436 check->bo->o = 0;
2437 check->bi->p = check->bi->data;
2438 check->bi->i = 0;
2439 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2440 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2441 if (s->proxy->timeout.check)
2442 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2443 }
2444 /* keep on processing step */
2445 else {
2446 cur = check->current_step;
2447 }
2448
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002449 /* It's only the rules which will enable send/recv */
2450 __conn_data_stop_both(conn);
2451
Willy Tarreauabca5b62013-12-06 14:19:25 +01002452 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002453 /* we have to try to flush the output buffer before reading, at the end,
2454 * or if we're about to send a string that does not fit in the remaining space.
2455 */
2456 if (check->bo->o &&
2457 (&cur->list == head ||
2458 check->current_step->action != TCPCHK_ACT_SEND ||
2459 check->current_step->string_len >= buffer_total_space(check->bo))) {
2460
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002461 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002462 if (conn->flags & CO_FL_ERROR) {
2463 chk_report_conn_err(conn, errno, 0);
2464 __conn_data_stop_both(conn);
2465 goto out_end_tcpcheck;
2466 }
2467 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002468 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002469 }
2470
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002471 /* did we reach the end ? If so, let's check that everything was sent */
2472 if (&cur->list == head) {
2473 if (check->bo->o)
2474 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002475 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002476 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002477
Willy Tarreauf3d34822014-12-08 12:11:28 +01002478 /* have 'next' point to the next rule or NULL if we're on the last one */
2479 next = (struct tcpcheck_rule *)cur->list.n;
2480 if (&next->list == head)
2481 next = NULL;
2482
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002483 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2484 struct protocol *proto;
2485 struct xprt_ops *xprt;
2486
2487 /* mark the step as started */
2488 check->last_started_step = check->current_step;
2489 /* first, shut existing connection */
2490 conn_force_close(conn);
2491
2492 /* prepare new connection */
2493 /* initialization */
2494 conn_init(conn);
2495 conn_attach(conn, check, &check_conn_cb);
2496 conn->target = &s->obj_type;
2497
2498 /* no client address */
2499 clear_addr(&conn->addr.from);
2500
Simon Horman41f58762015-01-30 11:22:56 +09002501 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002502 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002503 conn->addr.to = check->addr;
2504 proto = check->proto;
Willy Tarreau640556c2014-05-09 23:38:15 +02002505 }
2506 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002507 /* we'll connect to the addr on the server */
2508 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002509 proto = s->proto;
2510 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002511
2512 /* port */
2513 if (check->current_step->port)
2514 set_host_port(&conn->addr.to, check->current_step->port);
2515 else if (check->port)
2516 set_host_port(&conn->addr.to, check->port);
2517
2518#ifdef USE_OPENSSL
2519 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2520 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002521 }
2522 else {
2523 xprt = &raw_sock;
2524 }
2525#else /* USE_OPENSSL */
2526 xprt = &raw_sock;
2527#endif /* USE_OPENSSL */
2528 conn_prepare(conn, proto, xprt);
2529
2530 ret = SN_ERR_INTERNAL;
2531 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002532 ret = proto->connect(conn,
2533 1 /* I/O polling is always needed */,
2534 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002535 conn->flags |= CO_FL_WAKE_DATA;
2536 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2537 conn->send_proxy_ofs = 1;
2538 conn->flags |= CO_FL_SEND_PROXY;
2539 }
2540
2541 /* It can return one of :
2542 * - SN_ERR_NONE if everything's OK
2543 * - SN_ERR_SRVTO if there are no more servers
2544 * - SN_ERR_SRVCL if the connection was refused by the server
2545 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2546 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2547 * - SN_ERR_INTERNAL for any other purely internal errors
2548 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2549 * Note that we try to prevent the network stack from sending the ACK during the
2550 * connect() when a pure TCP check is used (without PROXY protocol).
2551 */
2552 switch (ret) {
2553 case SN_ERR_NONE:
2554 /* we allow up to min(inter, timeout.connect) for a connection
2555 * to establish but only when timeout.check is set
2556 * as it may be to short for a full check otherwise
2557 */
2558 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2559
2560 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2561 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2562 t->expire = tick_first(t->expire, t_con);
2563 }
2564 break;
2565 case SN_ERR_SRVTO: /* ETIMEDOUT */
2566 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2567 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2568 tcpcheck_get_step_id(s), strerror(errno));
2569 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2570 goto out_end_tcpcheck;
2571 case SN_ERR_PRXCOND:
2572 case SN_ERR_RESOURCE:
2573 case SN_ERR_INTERNAL:
2574 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2575 tcpcheck_get_step_id(s));
2576 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2577 goto out_end_tcpcheck;
2578 }
2579
2580 /* allow next rule */
2581 cur = (struct tcpcheck_rule *)cur->list.n;
2582 check->current_step = cur;
2583
2584 /* don't do anything until the connection is established */
2585 if (!(conn->flags & CO_FL_CONNECTED)) {
2586 /* update expire time, should be done by process_chk */
2587 /* we allow up to min(inter, timeout.connect) for a connection
2588 * to establish but only when timeout.check is set
2589 * as it may be to short for a full check otherwise
2590 */
2591 while (tick_is_expired(t->expire, now_ms)) {
2592 int t_con;
2593
2594 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2595 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2596
2597 if (s->proxy->timeout.check)
2598 t->expire = tick_first(t->expire, t_con);
2599 }
2600 return;
2601 }
2602
2603 } /* end 'connect' */
2604 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2605 /* mark the step as started */
2606 check->last_started_step = check->current_step;
2607
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002608 /* reset the read buffer */
2609 if (*check->bi->data != '\0') {
2610 *check->bi->data = '\0';
2611 check->bi->i = 0;
2612 }
2613
2614 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2615 conn->flags |= CO_FL_ERROR;
2616 chk_report_conn_err(conn, 0, 0);
2617 goto out_end_tcpcheck;
2618 }
2619
Willy Tarreauabca5b62013-12-06 14:19:25 +01002620 if (check->current_step->string_len >= check->bo->size) {
2621 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2622 check->current_step->string_len, check->bo->size,
2623 tcpcheck_get_step_id(s));
2624 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2625 goto out_end_tcpcheck;
2626 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002627
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002628 /* do not try to send if there is no space */
2629 if (check->current_step->string_len >= buffer_total_space(check->bo))
2630 continue;
2631
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002632 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2633 *check->bo->p = '\0'; /* to make gdb output easier to read */
2634
Willy Tarreauabca5b62013-12-06 14:19:25 +01002635 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002636 cur = (struct tcpcheck_rule *)cur->list.n;
2637 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002638 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002639 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002640 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002641 goto out_end_tcpcheck;
2642
Willy Tarreau310987a2014-01-22 19:46:33 +01002643 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002644 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2645 done = 1;
2646 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2647 /* Report network errors only if we got no other data. Otherwise
2648 * we'll let the upper layers decide whether the response is OK
2649 * or not. It is very common that an RST sent by the server is
2650 * reported as an error just after the last data chunk.
2651 */
2652 chk_report_conn_err(conn, errno, 0);
2653 goto out_end_tcpcheck;
2654 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002655 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002656 else
2657 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002658 }
2659
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002660 /* mark the step as started */
2661 check->last_started_step = check->current_step;
2662
2663
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002664 /* Intermediate or complete response received.
2665 * Terminate string in check->bi->data buffer.
2666 */
2667 if (check->bi->i < check->bi->size) {
2668 check->bi->data[check->bi->i] = '\0';
2669 }
2670 else {
2671 check->bi->data[check->bi->i - 1] = '\0';
2672 done = 1; /* buffer full, don't wait for more data */
2673 }
2674
2675 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002676
2677 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002678 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002679 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002680 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002681
2682 /* empty response */
2683 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2684 tcpcheck_get_step_id(s));
2685 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2686
2687 goto out_end_tcpcheck;
2688 }
2689
2690 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002691 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002692
Willy Tarreaua970c282013-12-06 12:47:19 +01002693 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002694 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002695 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002696 else if (cur->expect_regex != NULL)
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002697 ret = regex_exec(cur->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002698
2699 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002700 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002701
2702 /* matched */
2703 if (ret) {
2704 /* matched but we did not want to => ERROR */
2705 if (cur->inverse) {
2706 /* we were looking for a string */
2707 if (cur->string != NULL) {
2708 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2709 cur->string, tcpcheck_get_step_id(s));
2710 }
2711 else {
2712 /* we were looking for a regex */
2713 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2714 tcpcheck_get_step_id(s));
2715 }
2716 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2717 goto out_end_tcpcheck;
2718 }
2719 /* matched and was supposed to => OK, next step */
2720 else {
2721 cur = (struct tcpcheck_rule*)cur->list.n;
2722 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002723 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002724 goto tcpcheck_expect;
2725 __conn_data_stop_recv(conn);
2726 }
2727 }
2728 else {
2729 /* not matched */
2730 /* not matched and was not supposed to => OK, next step */
2731 if (cur->inverse) {
2732 cur = (struct tcpcheck_rule*)cur->list.n;
2733 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002734 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002735 goto tcpcheck_expect;
2736 __conn_data_stop_recv(conn);
2737 }
2738 /* not matched but was supposed to => ERROR */
2739 else {
2740 /* we were looking for a string */
2741 if (cur->string != NULL) {
2742 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2743 cur->string, tcpcheck_get_step_id(s));
2744 }
2745 else {
2746 /* we were looking for a regex */
2747 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2748 tcpcheck_get_step_id(s));
2749 }
2750 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2751 goto out_end_tcpcheck;
2752 }
2753 }
2754 } /* end expect */
2755 } /* end loop over double chained step list */
2756
2757 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2758 goto out_end_tcpcheck;
2759
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002760 out_need_io:
2761 if (check->bo->o)
2762 __conn_data_want_send(conn);
2763
2764 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2765 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002766 return;
2767
2768 out_end_tcpcheck:
2769 /* collect possible new errors */
2770 if (conn->flags & CO_FL_ERROR)
2771 chk_report_conn_err(conn, 0, 0);
2772
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002773 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002774 check->current_step = NULL;
2775
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002776 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002777 conn->flags |= CO_FL_ERROR;
2778
2779 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002780
2781 return;
2782}
2783
Simon Hormanb1900d52015-01-30 11:22:54 +09002784const char *init_check(struct check *check, int type)
2785{
2786 check->type = type;
2787
2788 /* Allocate buffer for requests... */
2789 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
2790 return "out of memory while allocating check buffer";
2791 }
2792 check->bi->size = global.tune.chksize;
2793
2794 /* Allocate buffer for responses... */
2795 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
2796 return "out of memory while allocating check buffer";
2797 }
2798 check->bo->size = global.tune.chksize;
2799
2800 /* Allocate buffer for partial results... */
2801 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
2802 return "out of memory while allocating check connection";
2803 }
2804
2805 check->conn->t.sock.fd = -1; /* no agent in progress yet */
2806
2807 return NULL;
2808}
2809
Simon Hormanbfb5d332015-01-30 11:22:55 +09002810void free_check(struct check *check)
2811{
2812 free(check->bi);
2813 free(check->bo);
2814 free(check->conn);
2815}
2816
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002817
Willy Tarreaubd741542010-03-16 18:46:54 +01002818/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002819 * Local variables:
2820 * c-indent-level: 8
2821 * c-basic-offset: 8
2822 * End:
2823 */