blob: feca96e2f867c7813d475d23303d1bc54dcd3eb5 [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));
1430 bo_putstr(check->bo, "\r\n");
1431 *check->bo->p = '\0'; /* to make gdb output easier to read */
1432 }
1433 }
1434
1435 /* prepare a new connection */
1436 conn_init(conn);
Cyril Bonté9ce13112014-11-15 22:41:27 +01001437 conn_prepare(conn, s->check_common.proto, check->xprt);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001438 conn_attach(conn, check, &check_conn_cb);
1439 conn->target = &s->obj_type;
1440
1441 /* no client address */
1442 clear_addr(&conn->addr.from);
1443
1444 if (is_addr(&s->check_common.addr)) {
1445
1446 /* we'll connect to the check addr specified on the server */
1447 conn->addr.to = s->check_common.addr;
1448 proto = s->check_common.proto;
1449 }
1450 else {
1451 /* we'll connect to the addr on the server */
1452 conn->addr.to = s->addr;
1453 proto = s->proto;
1454 }
1455
1456 if (check->port) {
1457 set_host_port(&conn->addr.to, check->port);
1458 }
1459
Willy Tarreauf3d34822014-12-08 12:11:28 +01001460 /* only plain tcp-check supports quick ACK */
1461 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1462
Willy Tarreaud2a49592014-12-08 11:52:28 +01001463 if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(&s->proxy->tcpcheck_rules)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001464 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1465 /* if first step is a 'connect', then tcpcheck_main must run it */
1466 if (r->action == TCPCHK_ACT_CONNECT) {
1467 tcpcheck_main(conn);
1468 return SN_ERR_UP;
1469 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001470 if (r->action == TCPCHK_ACT_EXPECT)
1471 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001472 }
1473
1474 ret = SN_ERR_INTERNAL;
1475 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001476 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001477 conn->flags |= CO_FL_WAKE_DATA;
1478 if (s->check.send_proxy) {
1479 conn->send_proxy_ofs = 1;
1480 conn->flags |= CO_FL_SEND_PROXY;
1481 }
1482
1483 return ret;
1484}
1485
Simon Horman98637e52014-06-20 12:30:16 +09001486static struct list pid_list = LIST_HEAD_INIT(pid_list);
1487static struct pool_head *pool2_pid_list;
1488
1489void block_sigchld(void)
1490{
1491 sigset_t set;
1492 sigemptyset(&set);
1493 sigaddset(&set, SIGCHLD);
1494 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1495}
1496
1497void unblock_sigchld(void)
1498{
1499 sigset_t set;
1500 sigemptyset(&set);
1501 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1502}
1503
1504/* Call with SIGCHLD blocked */
1505static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1506{
1507 struct pid_list *elem;
1508 struct check *check = t->context;
1509
1510 elem = pool_alloc2(pool2_pid_list);
1511 if (!elem)
1512 return NULL;
1513 elem->pid = pid;
1514 elem->t = t;
1515 elem->exited = 0;
1516 check->curpid = elem;
1517 LIST_INIT(&elem->list);
1518 LIST_ADD(&pid_list, &elem->list);
1519 return elem;
1520}
1521
1522/* Blocks blocks and then unblocks SIGCHLD */
1523static void pid_list_del(struct pid_list *elem)
1524{
1525 struct check *check;
1526
1527 if (!elem)
1528 return;
1529
1530 block_sigchld();
1531 LIST_DEL(&elem->list);
1532 unblock_sigchld();
1533 if (!elem->exited)
1534 kill(elem->pid, SIGTERM);
1535
1536 check = elem->t->context;
1537 check->curpid = NULL;
1538 pool_free2(pool2_pid_list, elem);
1539}
1540
1541/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1542static void pid_list_expire(pid_t pid, int status)
1543{
1544 struct pid_list *elem;
1545
1546 list_for_each_entry(elem, &pid_list, list) {
1547 if (elem->pid == pid) {
1548 elem->t->expire = now_ms;
1549 elem->status = status;
1550 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001551 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001552 return;
1553 }
1554 }
1555}
1556
1557static void sigchld_handler(int signal)
1558{
1559 pid_t pid;
1560 int status;
1561 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1562 pid_list_expire(pid, status);
1563}
1564
1565static int init_pid_list(void) {
1566 struct sigaction action = {
1567 .sa_handler = sigchld_handler,
1568 .sa_flags = SA_NOCLDSTOP
1569 };
1570
1571 if (pool2_pid_list != NULL)
1572 /* Nothing to do */
1573 return 0;
1574
1575 if (sigaction(SIGCHLD, &action, NULL)) {
1576 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1577 strerror(errno));
1578 return 1;
1579 }
1580
1581 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1582 if (pool2_pid_list == NULL) {
1583 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1584 strerror(errno));
1585 return 1;
1586 }
1587
1588 return 0;
1589}
1590
Cyril Bontéac92a062014-12-27 22:28:38 +01001591/* helper macro to set an environment variable and jump to a specific label on failure. */
1592#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001593
1594/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001595 * helper function to allocate enough memory to store an environment variable.
1596 * It will also check that the environment variable is updatable, and silently
1597 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001598 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001599static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001600{
1601 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001602 char *envname;
1603 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001604
Cyril Bontéac92a062014-12-27 22:28:38 +01001605 if (idx < 0 || idx >= EXTCHK_SIZE) {
1606 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1607 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001608 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001609
1610 envname = extcheck_envs[idx].name;
1611 vmaxlen = extcheck_envs[idx].vmaxlen;
1612
1613 /* Check if the environment variable is already set, and silently reject
1614 * the update if this one is not updatable. */
1615 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1616 return 0;
1617
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001618 /* Instead of sending NOT_USED, sending an empty value is preferable */
1619 if (strcmp(value, "NOT_USED") == 0) {
1620 value = "";
1621 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001622
1623 len = strlen(envname) + 1;
1624 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1625 len += strlen(value);
1626 else
1627 len += vmaxlen;
1628
1629 if (!check->envp[idx])
1630 check->envp[idx] = malloc(len + 1);
1631
1632 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001633 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1634 return 1;
1635 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001636 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001637 if (ret < 0) {
1638 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1639 return 1;
1640 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001641 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001642 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1643 return 1;
1644 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001645 return 0;
1646}
Simon Horman98637e52014-06-20 12:30:16 +09001647
1648static int prepare_external_check(struct check *check)
1649{
1650 struct server *s = check->server;
1651 struct proxy *px = s->proxy;
1652 struct listener *listener = NULL, *l;
1653 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001654 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001655 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001656
1657 list_for_each_entry(l, &px->conf.listeners, by_fe)
1658 /* Use the first INET, INET6 or UNIX listener */
1659 if (l->addr.ss_family == AF_INET ||
1660 l->addr.ss_family == AF_INET6 ||
1661 l->addr.ss_family == AF_UNIX) {
1662 listener = l;
1663 break;
1664 }
1665
Simon Horman98637e52014-06-20 12:30:16 +09001666 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001667 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1668 if (!check->envp) {
1669 Alert("Failed to allocate memory for environment variables. Aborting\n");
1670 goto err;
1671 }
Simon Horman98637e52014-06-20 12:30:16 +09001672
Cyril Bontéac92a062014-12-27 22:28:38 +01001673 check->argv = calloc(6, sizeof(char *));
1674 if (!check->argv) {
1675 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001676 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001677 }
Simon Horman98637e52014-06-20 12:30:16 +09001678
1679 check->argv[0] = px->check_command;
1680
Cyril Bonté777be862014-12-02 21:21:35 +01001681 if (!listener) {
1682 check->argv[1] = strdup("NOT_USED");
1683 check->argv[2] = strdup("NOT_USED");
1684 }
1685 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001686 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001687 addr_to_str(&listener->addr, buf, sizeof(buf));
1688 check->argv[1] = strdup(buf);
1689 port_to_str(&listener->addr, buf, sizeof(buf));
1690 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001691 }
1692 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001693 const struct sockaddr_un *un;
1694
1695 un = (struct sockaddr_un *)&listener->addr;
1696 check->argv[1] = strdup(un->sun_path);
1697 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001698 }
1699 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001700 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001701 goto err;
1702 }
1703
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001704 addr_to_str(&s->addr, buf, sizeof(buf));
1705 check->argv[3] = strdup(buf);
1706 port_to_str(&s->addr, buf, sizeof(buf));
1707 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001708
Cyril Bontéac92a062014-12-27 22:28:38 +01001709 for (i = 0; i < 5; i++) {
1710 if (!check->argv[i]) {
1711 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001712 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001713 }
1714 }
Simon Horman98637e52014-06-20 12:30:16 +09001715
Cyril Bontéac92a062014-12-27 22:28:38 +01001716 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001717 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001718 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1719 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1720 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1721 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001722 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001723 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1724 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1725 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1726 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1727 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1728 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1729
1730 /* Ensure that we don't leave any hole in check->envp */
1731 for (i = 0; i < EXTCHK_SIZE; i++)
1732 if (!check->envp[i])
1733 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001734
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001735 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001736err:
1737 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001738 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001739 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001740 free(check->envp);
1741 check->envp = NULL;
1742 }
1743
1744 if (check->argv) {
1745 for (i = 1; i < 5; i++)
1746 free(check->argv[i]);
1747 free(check->argv);
1748 check->argv = NULL;
1749 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001750 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001751}
1752
Simon Hormanb00d17a2014-06-13 16:18:16 +09001753/*
Simon Horman98637e52014-06-20 12:30:16 +09001754 * establish a server health-check that makes use of a process.
1755 *
1756 * It can return one of :
1757 * - SN_ERR_NONE if everything's OK
1758 * - SN_ERR_SRVTO if there are no more servers
1759 * - SN_ERR_SRVCL if the connection was refused by the server
1760 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1761 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1762 * - SN_ERR_INTERNAL for any other purely internal errors
1763 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1764 *
1765 * Blocks and then unblocks SIGCHLD
1766 */
1767static int connect_proc_chk(struct task *t)
1768{
Cyril Bontéac92a062014-12-27 22:28:38 +01001769 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001770 struct check *check = t->context;
1771 struct server *s = check->server;
1772 struct proxy *px = s->proxy;
1773 int status;
1774 pid_t pid;
1775
Simon Horman98637e52014-06-20 12:30:16 +09001776 status = SN_ERR_RESOURCE;
1777
1778 block_sigchld();
1779
1780 pid = fork();
1781 if (pid < 0) {
1782 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1783 strerror(errno));
1784 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1785 goto out;
1786 }
1787 if (pid == 0) {
1788 /* Child */
1789 extern char **environ;
1790 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001791 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001792 execvp(px->check_command, check->argv);
1793 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1794 strerror(errno));
1795 exit(-1);
1796 }
1797
1798 /* Parent */
1799 if (check->result == CHK_RES_UNKNOWN) {
1800 if (pid_list_add(pid, t) != NULL) {
1801 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1802
1803 if (px->timeout.check && px->timeout.connect) {
1804 int t_con = tick_add(now_ms, px->timeout.connect);
1805 t->expire = tick_first(t->expire, t_con);
1806 }
1807 status = SN_ERR_NONE;
1808 goto out;
1809 }
1810 else {
1811 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1812 }
1813 kill(pid, SIGTERM); /* process creation error */
1814 }
1815 else
1816 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1817
1818out:
1819 unblock_sigchld();
1820 return status;
1821}
1822
1823/*
1824 * establish a server health-check.
1825 *
1826 * It can return one of :
1827 * - SN_ERR_NONE if everything's OK
1828 * - SN_ERR_SRVTO if there are no more servers
1829 * - SN_ERR_SRVCL if the connection was refused by the server
1830 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1831 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1832 * - SN_ERR_INTERNAL for any other purely internal errors
1833 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1834 */
1835static int connect_chk(struct task *t)
1836{
1837 struct check *check = t->context;
1838
1839 if (check->type == PR_O2_EXT_CHK)
1840 return connect_proc_chk(t);
1841 return connect_conn_chk(t);
1842}
1843
1844/*
1845 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001846 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1847 */
Simon Horman98637e52014-06-20 12:30:16 +09001848static struct task *process_chk_proc(struct task *t)
1849{
1850 struct check *check = t->context;
1851 struct server *s = check->server;
1852 struct connection *conn = check->conn;
1853 int rv;
1854 int ret;
1855 int expired = tick_is_expired(t->expire, now_ms);
1856
1857 if (!(check->state & CHK_ST_INPROGRESS)) {
1858 /* no check currently running */
1859 if (!expired) /* woke up too early */
1860 return t;
1861
1862 /* we don't send any health-checks when the proxy is
1863 * stopped, the server should not be checked or the check
1864 * is disabled.
1865 */
1866 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1867 s->proxy->state == PR_STSTOPPED)
1868 goto reschedule;
1869
1870 /* we'll initiate a new check */
1871 set_server_check_status(check, HCHK_STATUS_START, NULL);
1872
1873 check->state |= CHK_ST_INPROGRESS;
1874
1875 ret = connect_chk(t);
1876 switch (ret) {
1877 case SN_ERR_UP:
1878 return t;
1879 case SN_ERR_NONE:
1880 /* we allow up to min(inter, timeout.connect) for a connection
1881 * to establish but only when timeout.check is set
1882 * as it may be to short for a full check otherwise
1883 */
1884 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1885
1886 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1887 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1888 t->expire = tick_first(t->expire, t_con);
1889 }
1890
1891 goto reschedule;
1892
1893 case SN_ERR_SRVTO: /* ETIMEDOUT */
1894 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1895 conn->flags |= CO_FL_ERROR;
1896 chk_report_conn_err(conn, errno, 0);
1897 break;
1898 case SN_ERR_PRXCOND:
1899 case SN_ERR_RESOURCE:
1900 case SN_ERR_INTERNAL:
1901 conn->flags |= CO_FL_ERROR;
1902 chk_report_conn_err(conn, 0, 0);
1903 break;
1904 }
1905
1906 /* here, we have seen a synchronous error, no fd was allocated */
1907
1908 check->state &= ~CHK_ST_INPROGRESS;
1909 check_notify_failure(check);
1910
1911 /* we allow up to min(inter, timeout.connect) for a connection
1912 * to establish but only when timeout.check is set
1913 * as it may be to short for a full check otherwise
1914 */
1915 while (tick_is_expired(t->expire, now_ms)) {
1916 int t_con;
1917
1918 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1919 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1920
1921 if (s->proxy->timeout.check)
1922 t->expire = tick_first(t->expire, t_con);
1923 }
1924 }
1925 else {
1926 /* there was a test running.
1927 * First, let's check whether there was an uncaught error,
1928 * which can happen on connect timeout or error.
1929 */
1930 if (check->result == CHK_RES_UNKNOWN) {
1931 /* good connection is enough for pure TCP check */
1932 struct pid_list *elem = check->curpid;
1933 int status = HCHK_STATUS_UNKNOWN;
1934
1935 if (elem->exited) {
1936 status = elem->status; /* Save in case the process exits between use below */
1937 if (!WIFEXITED(status))
1938 check->code = -1;
1939 else
1940 check->code = WEXITSTATUS(status);
1941 if (!WIFEXITED(status) || WEXITSTATUS(status))
1942 status = HCHK_STATUS_PROCERR;
1943 else
1944 status = HCHK_STATUS_PROCOK;
1945 } else if (expired) {
1946 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001947 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001948 kill(elem->pid, SIGTERM);
1949 }
1950 set_server_check_status(check, status, NULL);
1951 }
1952
1953 if (check->result == CHK_RES_FAILED) {
1954 /* a failure or timeout detected */
1955 check_notify_failure(check);
1956 }
1957 else if (check->result == CHK_RES_CONDPASS) {
1958 /* check is OK but asks for stopping mode */
1959 check_notify_stopping(check);
1960 }
1961 else if (check->result == CHK_RES_PASSED) {
1962 /* a success was detected */
1963 check_notify_success(check);
1964 }
1965 check->state &= ~CHK_ST_INPROGRESS;
1966
1967 pid_list_del(check->curpid);
1968
1969 rv = 0;
1970 if (global.spread_checks > 0) {
1971 rv = srv_getinter(check) * global.spread_checks / 100;
1972 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1973 }
1974 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1975 }
1976
1977 reschedule:
1978 while (tick_is_expired(t->expire, now_ms))
1979 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1980 return t;
1981}
1982
1983/*
1984 * manages a server health-check that uses a connection. Returns
1985 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1986 */
1987static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001988{
Simon Horman4a741432013-02-23 15:35:38 +09001989 struct check *check = t->context;
1990 struct server *s = check->server;
1991 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001992 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001993 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001994 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001995
Willy Tarreau2c115e52013-12-11 19:41:16 +01001996 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001997 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001998 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001999 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002000
Simon Horman671b6f02013-11-25 10:46:39 +09002001 /* we don't send any health-checks when the proxy is
2002 * stopped, the server should not be checked or the check
2003 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002004 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002005 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002006 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002007 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002008
2009 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002010 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002011
Willy Tarreau2c115e52013-12-11 19:41:16 +01002012 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002013 check->bi->p = check->bi->data;
2014 check->bi->i = 0;
2015 check->bo->p = check->bo->data;
2016 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002017
Simon Hormanb00d17a2014-06-13 16:18:16 +09002018 ret = connect_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002019 switch (ret) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09002020 case SN_ERR_UP:
2021 return t;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002022 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002023 /* we allow up to min(inter, timeout.connect) for a connection
2024 * to establish but only when timeout.check is set
2025 * as it may be to short for a full check otherwise
2026 */
Simon Horman4a741432013-02-23 15:35:38 +09002027 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002028
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002029 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2030 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2031 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002032 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002033
2034 if (check->type)
2035 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2036
Willy Tarreau5a78f362012-11-23 12:47:05 +01002037 goto reschedule;
2038
2039 case SN_ERR_SRVTO: /* ETIMEDOUT */
2040 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002041 conn->flags |= CO_FL_ERROR;
2042 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002043 break;
2044 case SN_ERR_PRXCOND:
2045 case SN_ERR_RESOURCE:
2046 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002047 conn->flags |= CO_FL_ERROR;
2048 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002049 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002050 }
2051
Willy Tarreau5a78f362012-11-23 12:47:05 +01002052 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002053
Willy Tarreau2c115e52013-12-11 19:41:16 +01002054 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002055 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002057 /* we allow up to min(inter, timeout.connect) for a connection
2058 * to establish but only when timeout.check is set
2059 * as it may be to short for a full check otherwise
2060 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002061 while (tick_is_expired(t->expire, now_ms)) {
2062 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002063
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002064 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002065 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002066
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002067 if (s->proxy->timeout.check)
2068 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002069 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002070 }
2071 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002072 /* there was a test running.
2073 * First, let's check whether there was an uncaught error,
2074 * which can happen on connect timeout or error.
2075 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002076 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002077 /* good connection is enough for pure TCP check */
2078 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002079 if (check->use_ssl)
2080 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002081 else
Simon Horman4a741432013-02-23 15:35:38 +09002082 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002083 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002084 else if ((conn->flags & CO_FL_ERROR) || expired) {
2085 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002086 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002087 else
2088 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002089 }
2090
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002091 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002092 if (conn->xprt) {
2093 /* The check was aborted and the connection was not yet closed.
2094 * This can happen upon timeout, or when an external event such
2095 * as a failed response coupled with "observe layer7" caused the
2096 * server state to be suddenly changed.
2097 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01002098 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002099 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002100 }
2101
Willy Tarreauaf549582014-05-16 17:37:50 +02002102 if (check->result == CHK_RES_FAILED) {
2103 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002104 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002105 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002106 else if (check->result == CHK_RES_CONDPASS) {
2107 /* check is OK but asks for stopping mode */
2108 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002109 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002110 else if (check->result == CHK_RES_PASSED) {
2111 /* a success was detected */
2112 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002113 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002114 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002115
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002116 rv = 0;
2117 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002118 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002119 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002120 }
Simon Horman4a741432013-02-23 15:35:38 +09002121 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002122 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002123
2124 reschedule:
2125 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002126 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002127 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002128 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002129}
2130
Simon Horman98637e52014-06-20 12:30:16 +09002131/*
2132 * manages a server health-check. Returns
2133 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2134 */
2135static struct task *process_chk(struct task *t)
2136{
2137 struct check *check = t->context;
2138
2139 if (check->type == PR_O2_EXT_CHK)
2140 return process_chk_proc(t);
2141 return process_chk_conn(t);
2142}
2143
Simon Horman5c942422013-11-25 10:46:32 +09002144static int start_check_task(struct check *check, int mininter,
2145 int nbcheck, int srvpos)
2146{
2147 struct task *t;
2148 /* task for the check */
2149 if ((t = task_new()) == NULL) {
2150 Alert("Starting [%s:%s] check: out of memory.\n",
2151 check->server->proxy->id, check->server->id);
2152 return 0;
2153 }
2154
2155 check->task = t;
2156 t->process = process_chk;
2157 t->context = check;
2158
Willy Tarreau1746eec2014-04-25 10:46:47 +02002159 if (mininter < srv_getinter(check))
2160 mininter = srv_getinter(check);
2161
2162 if (global.max_spread_checks && mininter > global.max_spread_checks)
2163 mininter = global.max_spread_checks;
2164
Simon Horman5c942422013-11-25 10:46:32 +09002165 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002166 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002167 check->start = now;
2168 task_queue(t);
2169
2170 return 1;
2171}
2172
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002173/*
2174 * Start health-check.
2175 * Returns 0 if OK, -1 if error, and prints the error in this case.
2176 */
2177int start_checks() {
2178
2179 struct proxy *px;
2180 struct server *s;
2181 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002182 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002183
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002184 /* 1- count the checkers to run simultaneously.
2185 * We also determine the minimum interval among all of those which
2186 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2187 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002188 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002189 * too short an interval for all others.
2190 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002191 for (px = proxy; px; px = px->next) {
2192 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002193 if (s->slowstart) {
2194 if ((t = task_new()) == NULL) {
2195 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2196 return -1;
2197 }
2198 /* We need a warmup task that will be called when the server
2199 * state switches from down to up.
2200 */
2201 s->warmup = t;
2202 t->process = server_warmup;
2203 t->context = s;
2204 t->expire = TICK_ETERNITY;
2205 }
2206
Willy Tarreaud8514a22013-12-11 21:10:14 +01002207 if (s->check.state & CHK_ST_CONFIGURED) {
2208 nbcheck++;
2209 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2210 (!mininter || mininter > srv_getinter(&s->check)))
2211 mininter = srv_getinter(&s->check);
2212 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002213
Willy Tarreaud8514a22013-12-11 21:10:14 +01002214 if (s->agent.state & CHK_ST_CONFIGURED) {
2215 nbcheck++;
2216 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2217 (!mininter || mininter > srv_getinter(&s->agent)))
2218 mininter = srv_getinter(&s->agent);
2219 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002220 }
2221 }
2222
Simon Horman4a741432013-02-23 15:35:38 +09002223 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002224 return 0;
2225
2226 srand((unsigned)time(NULL));
2227
2228 /*
2229 * 2- start them as far as possible from each others. For this, we will
2230 * start them after their interval set to the min interval divided by
2231 * the number of servers, weighted by the server's position in the list.
2232 */
2233 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002234 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2235 if (init_pid_list()) {
2236 Alert("Starting [%s] check: out of memory.\n", px->id);
2237 return -1;
2238 }
2239 }
2240
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002241 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002242 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002243 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002244 if (s->check.type == PR_O2_EXT_CHK) {
2245 if (!prepare_external_check(&s->check))
2246 return -1;
2247 }
Simon Hormand60d6912013-11-25 10:46:36 +09002248 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2249 return -1;
2250 srvpos++;
2251 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002252
Simon Hormand60d6912013-11-25 10:46:36 +09002253 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002254 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002255 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2256 return -1;
2257 }
2258 srvpos++;
2259 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002260 }
2261 }
2262 return 0;
2263}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002264
2265/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002266 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002267 * The buffer MUST be terminated by a null byte before calling this function.
2268 * Sets server status appropriately. The caller is responsible for ensuring
2269 * that the buffer contains at least 13 characters. If <done> is zero, we may
2270 * return 0 to indicate that data is required to decide of a match.
2271 */
2272static int httpchk_expect(struct server *s, int done)
2273{
2274 static char status_msg[] = "HTTP status check returned code <000>";
2275 char status_code[] = "000";
2276 char *contentptr;
2277 int crlf;
2278 int ret;
2279
2280 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2281 case PR_O2_EXP_STS:
2282 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002283 memcpy(status_code, s->check.bi->data + 9, 3);
2284 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002285
2286 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2287 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2288 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002289 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002290
2291 /* we necessarily have the response, so there are no partial failures */
2292 if (s->proxy->options2 & PR_O2_EXP_INV)
2293 ret = !ret;
2294
Simon Horman4a741432013-02-23 15:35:38 +09002295 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002296 break;
2297
2298 case PR_O2_EXP_STR:
2299 case PR_O2_EXP_RSTR:
2300 /* very simple response parser: ignore CR and only count consecutive LFs,
2301 * stop with contentptr pointing to first char after the double CRLF or
2302 * to '\0' if crlf < 2.
2303 */
2304 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002305 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002306 if (crlf >= 2)
2307 break;
2308 if (*contentptr == '\r')
2309 continue;
2310 else if (*contentptr == '\n')
2311 crlf++;
2312 else
2313 crlf = 0;
2314 }
2315
2316 /* Check that response contains a body... */
2317 if (crlf < 2) {
2318 if (!done)
2319 return 0;
2320
Simon Horman4a741432013-02-23 15:35:38 +09002321 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002322 "HTTP content check could not find a response body");
2323 return 1;
2324 }
2325
2326 /* Check that response body is not empty... */
2327 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002328 if (!done)
2329 return 0;
2330
Simon Horman4a741432013-02-23 15:35:38 +09002331 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002332 "HTTP content check found empty response body");
2333 return 1;
2334 }
2335
2336 /* Check the response content against the supplied string
2337 * or regex... */
2338 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2339 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2340 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002341 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002342
2343 /* if we don't match, we may need to wait more */
2344 if (!ret && !done)
2345 return 0;
2346
2347 if (ret) {
2348 /* content matched */
2349 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002350 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002351 "HTTP check matched unwanted content");
2352 else
Simon Horman4a741432013-02-23 15:35:38 +09002353 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002354 "HTTP content check matched");
2355 }
2356 else {
2357 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002358 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002359 "HTTP check did not match unwanted content");
2360 else
Simon Horman4a741432013-02-23 15:35:38 +09002361 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002362 "HTTP content check did not match");
2363 }
2364 break;
2365 }
2366 return 1;
2367}
2368
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002369/*
2370 * return the id of a step in a send/expect session
2371 */
2372static int tcpcheck_get_step_id(struct server *s)
2373{
2374 struct tcpcheck_rule *cur = NULL, *next = NULL;
2375 int i = 0;
2376
Willy Tarreau213c6782014-10-02 14:51:02 +02002377 /* not even started anything yet => step 0 = initial connect */
2378 if (!s->check.current_step)
2379 return 0;
2380
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002381 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002382
2383 /* no step => first step */
2384 if (cur == NULL)
2385 return 1;
2386
2387 /* increment i until current step */
2388 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
2389 if (next->list.p == &cur->list)
2390 break;
2391 ++i;
2392 }
2393
2394 return i;
2395}
2396
2397static void tcpcheck_main(struct connection *conn)
2398{
2399 char *contentptr;
Willy Tarreauf3d34822014-12-08 12:11:28 +01002400 struct tcpcheck_rule *cur, *next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002401 int done = 0, ret = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002402 struct check *check = conn->owner;
2403 struct server *s = check->server;
2404 struct task *t = check->task;
Willy Tarreauef953952014-10-02 14:30:14 +02002405 struct list *head = &s->proxy->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002406
Willy Tarreauef953952014-10-02 14:30:14 +02002407 /* here, we know that the check is complete or that it failed */
2408 if (check->result != CHK_RES_UNKNOWN)
2409 goto out_end_tcpcheck;
2410
2411 /* We have 4 possibilities here :
2412 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2413 * connection attempt was made yet ;
2414 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2415 * does not exist (no rule), so a connection attempt was made
2416 * before coming here.
2417 * 3. we're coming back after having started with step 1, so we may
2418 * be waiting for a connection attempt to complete.
2419 * 4. the connection + handshake are complete
2420 *
2421 * #2 and #3 are quite similar, we want both the connection and the
2422 * handshake to complete before going any further. Thus we must always
2423 * wait for a connection to complete unless we're before and existing
2424 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002425 */
Willy Tarreauef953952014-10-02 14:30:14 +02002426 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
2427 (check->current_step || LIST_ISEMPTY(head))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002428 /* we allow up to min(inter, timeout.connect) for a connection
2429 * to establish but only when timeout.check is set
2430 * as it may be to short for a full check otherwise
2431 */
2432 while (tick_is_expired(t->expire, now_ms)) {
2433 int t_con;
2434
2435 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2436 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2437
2438 if (s->proxy->timeout.check)
2439 t->expire = tick_first(t->expire, t_con);
2440 }
2441 return;
2442 }
2443
Willy Tarreauef953952014-10-02 14:30:14 +02002444 /* special case: option tcp-check with no rule, a connect is enough */
2445 if (LIST_ISEMPTY(head)) {
2446 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002447 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002448 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002449
Willy Tarreau213c6782014-10-02 14:51:02 +02002450 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002451 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002452 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002453 check->bo->p = check->bo->data;
2454 check->bo->o = 0;
2455 check->bi->p = check->bi->data;
2456 check->bi->i = 0;
2457 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2458 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2459 if (s->proxy->timeout.check)
2460 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2461 }
2462 /* keep on processing step */
2463 else {
2464 cur = check->current_step;
2465 }
2466
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002467 /* It's only the rules which will enable send/recv */
2468 __conn_data_stop_both(conn);
2469
Willy Tarreauabca5b62013-12-06 14:19:25 +01002470 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002471 /* we have to try to flush the output buffer before reading, at the end,
2472 * or if we're about to send a string that does not fit in the remaining space.
2473 */
2474 if (check->bo->o &&
2475 (&cur->list == head ||
2476 check->current_step->action != TCPCHK_ACT_SEND ||
2477 check->current_step->string_len >= buffer_total_space(check->bo))) {
2478
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002479 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002480 if (conn->flags & CO_FL_ERROR) {
2481 chk_report_conn_err(conn, errno, 0);
2482 __conn_data_stop_both(conn);
2483 goto out_end_tcpcheck;
2484 }
2485 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002486 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002487 }
2488
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002489 /* did we reach the end ? If so, let's check that everything was sent */
2490 if (&cur->list == head) {
2491 if (check->bo->o)
2492 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002493 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002494 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002495
Willy Tarreauf3d34822014-12-08 12:11:28 +01002496 /* have 'next' point to the next rule or NULL if we're on the last one */
2497 next = (struct tcpcheck_rule *)cur->list.n;
2498 if (&next->list == head)
2499 next = NULL;
2500
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002501 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2502 struct protocol *proto;
2503 struct xprt_ops *xprt;
2504
2505 /* mark the step as started */
2506 check->last_started_step = check->current_step;
2507 /* first, shut existing connection */
2508 conn_force_close(conn);
2509
2510 /* prepare new connection */
2511 /* initialization */
2512 conn_init(conn);
2513 conn_attach(conn, check, &check_conn_cb);
2514 conn->target = &s->obj_type;
2515
2516 /* no client address */
2517 clear_addr(&conn->addr.from);
2518
Willy Tarreau640556c2014-05-09 23:38:15 +02002519 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002520 /* we'll connect to the check addr specified on the server */
2521 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002522 proto = s->check_common.proto;
2523 }
2524 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002525 /* we'll connect to the addr on the server */
2526 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002527 proto = s->proto;
2528 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002529
2530 /* port */
2531 if (check->current_step->port)
2532 set_host_port(&conn->addr.to, check->current_step->port);
2533 else if (check->port)
2534 set_host_port(&conn->addr.to, check->port);
2535
2536#ifdef USE_OPENSSL
2537 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2538 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002539 }
2540 else {
2541 xprt = &raw_sock;
2542 }
2543#else /* USE_OPENSSL */
2544 xprt = &raw_sock;
2545#endif /* USE_OPENSSL */
2546 conn_prepare(conn, proto, xprt);
2547
2548 ret = SN_ERR_INTERNAL;
2549 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002550 ret = proto->connect(conn,
2551 1 /* I/O polling is always needed */,
2552 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002553 conn->flags |= CO_FL_WAKE_DATA;
2554 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2555 conn->send_proxy_ofs = 1;
2556 conn->flags |= CO_FL_SEND_PROXY;
2557 }
2558
2559 /* It can return one of :
2560 * - SN_ERR_NONE if everything's OK
2561 * - SN_ERR_SRVTO if there are no more servers
2562 * - SN_ERR_SRVCL if the connection was refused by the server
2563 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2564 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2565 * - SN_ERR_INTERNAL for any other purely internal errors
2566 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2567 * Note that we try to prevent the network stack from sending the ACK during the
2568 * connect() when a pure TCP check is used (without PROXY protocol).
2569 */
2570 switch (ret) {
2571 case SN_ERR_NONE:
2572 /* we allow up to min(inter, timeout.connect) for a connection
2573 * to establish but only when timeout.check is set
2574 * as it may be to short for a full check otherwise
2575 */
2576 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2577
2578 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2579 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2580 t->expire = tick_first(t->expire, t_con);
2581 }
2582 break;
2583 case SN_ERR_SRVTO: /* ETIMEDOUT */
2584 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2585 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2586 tcpcheck_get_step_id(s), strerror(errno));
2587 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2588 goto out_end_tcpcheck;
2589 case SN_ERR_PRXCOND:
2590 case SN_ERR_RESOURCE:
2591 case SN_ERR_INTERNAL:
2592 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2593 tcpcheck_get_step_id(s));
2594 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2595 goto out_end_tcpcheck;
2596 }
2597
2598 /* allow next rule */
2599 cur = (struct tcpcheck_rule *)cur->list.n;
2600 check->current_step = cur;
2601
2602 /* don't do anything until the connection is established */
2603 if (!(conn->flags & CO_FL_CONNECTED)) {
2604 /* update expire time, should be done by process_chk */
2605 /* we allow up to min(inter, timeout.connect) for a connection
2606 * to establish but only when timeout.check is set
2607 * as it may be to short for a full check otherwise
2608 */
2609 while (tick_is_expired(t->expire, now_ms)) {
2610 int t_con;
2611
2612 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2613 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2614
2615 if (s->proxy->timeout.check)
2616 t->expire = tick_first(t->expire, t_con);
2617 }
2618 return;
2619 }
2620
2621 } /* end 'connect' */
2622 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2623 /* mark the step as started */
2624 check->last_started_step = check->current_step;
2625
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002626 /* reset the read buffer */
2627 if (*check->bi->data != '\0') {
2628 *check->bi->data = '\0';
2629 check->bi->i = 0;
2630 }
2631
2632 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2633 conn->flags |= CO_FL_ERROR;
2634 chk_report_conn_err(conn, 0, 0);
2635 goto out_end_tcpcheck;
2636 }
2637
Willy Tarreauabca5b62013-12-06 14:19:25 +01002638 if (check->current_step->string_len >= check->bo->size) {
2639 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2640 check->current_step->string_len, check->bo->size,
2641 tcpcheck_get_step_id(s));
2642 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2643 goto out_end_tcpcheck;
2644 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002645
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002646 /* do not try to send if there is no space */
2647 if (check->current_step->string_len >= buffer_total_space(check->bo))
2648 continue;
2649
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002650 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2651 *check->bo->p = '\0'; /* to make gdb output easier to read */
2652
Willy Tarreauabca5b62013-12-06 14:19:25 +01002653 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002654 cur = (struct tcpcheck_rule *)cur->list.n;
2655 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002656 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002657 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002658 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002659 goto out_end_tcpcheck;
2660
Willy Tarreau310987a2014-01-22 19:46:33 +01002661 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002662 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2663 done = 1;
2664 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2665 /* Report network errors only if we got no other data. Otherwise
2666 * we'll let the upper layers decide whether the response is OK
2667 * or not. It is very common that an RST sent by the server is
2668 * reported as an error just after the last data chunk.
2669 */
2670 chk_report_conn_err(conn, errno, 0);
2671 goto out_end_tcpcheck;
2672 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002673 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002674 else
2675 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002676 }
2677
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002678 /* mark the step as started */
2679 check->last_started_step = check->current_step;
2680
2681
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002682 /* Intermediate or complete response received.
2683 * Terminate string in check->bi->data buffer.
2684 */
2685 if (check->bi->i < check->bi->size) {
2686 check->bi->data[check->bi->i] = '\0';
2687 }
2688 else {
2689 check->bi->data[check->bi->i - 1] = '\0';
2690 done = 1; /* buffer full, don't wait for more data */
2691 }
2692
2693 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002694
2695 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002696 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002697 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002698 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002699
2700 /* empty response */
2701 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2702 tcpcheck_get_step_id(s));
2703 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2704
2705 goto out_end_tcpcheck;
2706 }
2707
2708 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002709 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002710
Willy Tarreaua970c282013-12-06 12:47:19 +01002711 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002712 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002713 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002714 else if (cur->expect_regex != NULL)
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002715 ret = regex_exec(cur->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002716
2717 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002718 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002719
2720 /* matched */
2721 if (ret) {
2722 /* matched but we did not want to => ERROR */
2723 if (cur->inverse) {
2724 /* we were looking for a string */
2725 if (cur->string != NULL) {
2726 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2727 cur->string, tcpcheck_get_step_id(s));
2728 }
2729 else {
2730 /* we were looking for a regex */
2731 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2732 tcpcheck_get_step_id(s));
2733 }
2734 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2735 goto out_end_tcpcheck;
2736 }
2737 /* matched and was supposed to => OK, next step */
2738 else {
2739 cur = (struct tcpcheck_rule*)cur->list.n;
2740 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002741 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002742 goto tcpcheck_expect;
2743 __conn_data_stop_recv(conn);
2744 }
2745 }
2746 else {
2747 /* not matched */
2748 /* not matched and was not supposed to => OK, next step */
2749 if (cur->inverse) {
2750 cur = (struct tcpcheck_rule*)cur->list.n;
2751 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002752 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002753 goto tcpcheck_expect;
2754 __conn_data_stop_recv(conn);
2755 }
2756 /* not matched but was supposed to => ERROR */
2757 else {
2758 /* we were looking for a string */
2759 if (cur->string != NULL) {
2760 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2761 cur->string, tcpcheck_get_step_id(s));
2762 }
2763 else {
2764 /* we were looking for a regex */
2765 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2766 tcpcheck_get_step_id(s));
2767 }
2768 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2769 goto out_end_tcpcheck;
2770 }
2771 }
2772 } /* end expect */
2773 } /* end loop over double chained step list */
2774
2775 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2776 goto out_end_tcpcheck;
2777
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002778 out_need_io:
2779 if (check->bo->o)
2780 __conn_data_want_send(conn);
2781
2782 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2783 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002784 return;
2785
2786 out_end_tcpcheck:
2787 /* collect possible new errors */
2788 if (conn->flags & CO_FL_ERROR)
2789 chk_report_conn_err(conn, 0, 0);
2790
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002791 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002792 check->current_step = NULL;
2793
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002794 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002795 conn->flags |= CO_FL_ERROR;
2796
2797 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002798
2799 return;
2800}
2801
2802
Willy Tarreaubd741542010-03-16 18:46:54 +01002803/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804 * Local variables:
2805 * c-indent-level: 8
2806 * c-basic-offset: 8
2807 * End:
2808 */