blob: 59d9514b22ff1eecba2e736ddcc8d179adc63f01 [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>
18#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
23#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040024#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090025#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020027#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <arpa/inet.h>
29
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
33#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020034#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Baptiste Assmann69e273f2013-12-11 00:52:19 +010039#ifdef USE_OPENSSL
40#include <types/ssl_sock.h>
41#include <proto/ssl_sock.h>
42#endif /* USE_OPENSSL */
43
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
49#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020050#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010051#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010052#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010053#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010054#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020055#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090057#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010058#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/task.h>
60
Willy Tarreaubd741542010-03-16 18:46:54 +010061static int httpchk_expect(struct server *s, int done);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020062static int tcpcheck_get_step_id(struct server *);
63static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010064
Simon Horman63a4a822012-03-19 07:24:41 +090065static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010066 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
67 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020068 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020069
Willy Tarreau23964182014-05-20 20:56:30 +020070 /* Below we have finished checks */
71 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010072 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010073
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010074 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010076 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
77 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
78 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020079
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010080 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
81 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
82 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020083
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010084 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
85 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020087 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020088
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010089 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
90 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
91 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090092
93 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
94 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
95 [HCHK_STATUS_PROCOK] = { CHK_RES_FAILED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020096};
97
Simon Horman63a4a822012-03-19 07:24:41 +090098static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010099 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
100
101 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
102 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
103
104 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
105 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
106 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
107 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
108
109 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
110 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
111 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
112};
113
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200114/*
115 * Convert check_status code to description
116 */
117const char *get_check_status_description(short check_status) {
118
119 const char *desc;
120
121 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200122 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200123 else
124 desc = NULL;
125
126 if (desc && *desc)
127 return desc;
128 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200129 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200130}
131
132/*
133 * Convert check_status code to short info
134 */
135const char *get_check_status_info(short check_status) {
136
137 const char *info;
138
139 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200140 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200141 else
142 info = NULL;
143
144 if (info && *info)
145 return info;
146 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200147 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200148}
149
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100150const char *get_analyze_status(short analyze_status) {
151
152 const char *desc;
153
154 if (analyze_status < HANA_STATUS_SIZE)
155 desc = analyze_statuses[analyze_status].desc;
156 else
157 desc = NULL;
158
159 if (desc && *desc)
160 return desc;
161 else
162 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
163}
164
Willy Tarreaua150cf12014-05-20 21:57:23 +0200165/* Builds a string containing some information about the health check's result.
166 * The output string is allocated from the trash chunks. If the check is NULL,
167 * NULL is returned. This is designed to be used when emitting logs about health
168 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200169 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200170static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200171{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200172 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200173
Willy Tarreaua150cf12014-05-20 21:57:23 +0200174 if (!check)
175 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200176
Willy Tarreaua150cf12014-05-20 21:57:23 +0200177 msg = get_trash_chunk();
178 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200179
Willy Tarreaua150cf12014-05-20 21:57:23 +0200180 if (check->status >= HCHK_STATUS_L57DATA)
181 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200182
Willy Tarreaua150cf12014-05-20 21:57:23 +0200183 if (*check->desc) {
184 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200185
Willy Tarreaua150cf12014-05-20 21:57:23 +0200186 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200187
Willy Tarreaua150cf12014-05-20 21:57:23 +0200188 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
189 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200190
Willy Tarreaua150cf12014-05-20 21:57:23 +0200191 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200192 }
193
Willy Tarreaua150cf12014-05-20 21:57:23 +0200194 if (check->duration >= 0)
195 chunk_appendf(msg, ", check duration: %ldms", check->duration);
196
197 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200198}
199
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200200/*
Simon Horman4a741432013-02-23 15:35:38 +0900201 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200202 * an adequate CHK_RES_* value. The new check->health is computed based
203 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200204 *
205 * Show information in logs about failed health check if server is UP
206 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200207 */
Simon Horman4a741432013-02-23 15:35:38 +0900208static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100209{
Simon Horman4a741432013-02-23 15:35:38 +0900210 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200211 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200212 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900213
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200214 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100215 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900216 check->desc[0] = '\0';
217 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200218 return;
219 }
220
Simon Horman4a741432013-02-23 15:35:38 +0900221 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200222 return;
223
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200224 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900225 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
226 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200227 } else
Simon Horman4a741432013-02-23 15:35:38 +0900228 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200229
Simon Horman4a741432013-02-23 15:35:38 +0900230 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200231 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900232 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200233
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100234 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900235 check->duration = -1;
236 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900238 check->duration = tv_ms_elapsed(&check->start, &now);
239 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200240 }
241
Willy Tarreau23964182014-05-20 20:56:30 +0200242 /* no change is expected if no state change occurred */
243 if (check->result == CHK_RES_NEUTRAL)
244 return;
245
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200246 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200247
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200248 switch (check->result) {
249 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200250 /* Failure to connect to the agent as a secondary check should not
251 * cause the server to be marked down.
252 */
253 if ((!(check->state & CHK_ST_AGENT) ||
254 (check->status >= HCHK_STATUS_L7TOUT)) &&
255 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200256 s->counters.failed_checks++;
257 report = 1;
258 check->health--;
259 if (check->health < check->rise)
260 check->health = 0;
261 }
262 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200263
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200264 case CHK_RES_PASSED:
265 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
266 if ((check->health < check->rise + check->fall - 1) &&
267 (check->result == CHK_RES_PASSED || check->health > 0)) {
268 report = 1;
269 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200270
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200271 if (check->health >= check->rise)
272 check->health = check->rise + check->fall - 1; /* OK now */
273 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200274
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200275 /* clear consecutive_errors if observing is enabled */
276 if (s->onerror)
277 s->consecutive_errors = 0;
278 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100279
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200280 default:
281 break;
282 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200283
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200284 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
285 (status != prev_status || report)) {
286 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200287 "%s check for %sserver %s/%s %s%s",
288 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200289 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100290 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100291 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200292 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200293
Willy Tarreaua150cf12014-05-20 21:57:23 +0200294 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200295
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100296 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200297 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
298 (check->health >= check->rise) ? check->fall : check->rise,
299 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200300
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100301 Warning("%s.\n", trash.str);
302 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200303 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200304}
305
Willy Tarreau4eec5472014-05-20 22:32:27 +0200306/* Marks the check <check>'s server down if the current check is already failed
307 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200308 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200309static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310{
Simon Horman4a741432013-02-23 15:35:38 +0900311 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900312
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200313 /* The agent secondary check should only cause a server to be marked
314 * as down if check->status is HCHK_STATUS_L7STS, which indicates
315 * that the agent returned "fail", "stopped" or "down".
316 * The implication here is that failure to connect to the agent
317 * as a secondary check should not cause the server to be marked
318 * down. */
319 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
320 return;
321
Willy Tarreau4eec5472014-05-20 22:32:27 +0200322 if (check->health > 0)
323 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100324
Willy Tarreau4eec5472014-05-20 22:32:27 +0200325 /* We only report a reason for the check if we did not do so previously */
326 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327}
328
Willy Tarreauaf549582014-05-16 17:37:50 +0200329/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200330 * it isn't in maintenance, it is not tracking a down server and other checks
331 * comply. The rule is simple : by default, a server is up, unless any of the
332 * following conditions is true :
333 * - health check failed (check->health < rise)
334 * - agent check failed (agent->health < rise)
335 * - the server tracks a down server (track && track->state == STOPPED)
336 * Note that if the server has a slowstart, it will switch to STARTING instead
337 * of RUNNING. Also, only the health checks support the nolb mode, so the
338 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200339 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200340static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200341{
Simon Horman4a741432013-02-23 15:35:38 +0900342 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100343
Willy Tarreauaf549582014-05-16 17:37:50 +0200344 if (s->admin & SRV_ADMF_MAINT)
345 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100346
Willy Tarreau3e048382014-05-21 10:30:54 +0200347 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200348 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100349
Willy Tarreau3e048382014-05-21 10:30:54 +0200350 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
351 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100352
Willy Tarreau3e048382014-05-21 10:30:54 +0200353 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
354 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200355
Willy Tarreau3e048382014-05-21 10:30:54 +0200356 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
357 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100358
Willy Tarreau3e048382014-05-21 10:30:54 +0200359 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 +0100360}
361
Willy Tarreaudb58b792014-05-21 13:57:23 +0200362/* Marks the check <check> as valid and tries to set its server into stopping mode
363 * if it was running or starting, and provided it isn't in maintenance and other
364 * checks comply. The conditions for the server to be marked in stopping mode are
365 * the same as for it to be turned up. Also, only the health checks support the
366 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200367 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200368static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200369{
Simon Horman4a741432013-02-23 15:35:38 +0900370 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100371
Willy Tarreauaf549582014-05-16 17:37:50 +0200372 if (s->admin & SRV_ADMF_MAINT)
373 return;
374
Willy Tarreaudb58b792014-05-21 13:57:23 +0200375 if (check->state & CHK_ST_AGENT)
376 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100377
Willy Tarreaudb58b792014-05-21 13:57:23 +0200378 if (s->track && s->track->state == SRV_ST_STOPPED)
379 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100380
Willy Tarreaudb58b792014-05-21 13:57:23 +0200381 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
382 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100383
Willy Tarreaudb58b792014-05-21 13:57:23 +0200384 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
385 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100386
Willy Tarreaudb58b792014-05-21 13:57:23 +0200387 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 +0100388}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100390/* note: use health_adjust() only, which first checks that the observe mode is
391 * enabled.
392 */
393void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100394{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100395 int failed;
396 int expire;
397
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100398 if (s->observe >= HANA_OBS_SIZE)
399 return;
400
Willy Tarreaubb956662013-01-24 00:37:39 +0100401 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100402 return;
403
404 switch (analyze_statuses[status].lr[s->observe - 1]) {
405 case 1:
406 failed = 1;
407 break;
408
409 case 2:
410 failed = 0;
411 break;
412
413 default:
414 return;
415 }
416
417 if (!failed) {
418 /* good: clear consecutive_errors */
419 s->consecutive_errors = 0;
420 return;
421 }
422
423 s->consecutive_errors++;
424
425 if (s->consecutive_errors < s->consecutive_errors_limit)
426 return;
427
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100428 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
429 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100430
431 switch (s->onerror) {
432 case HANA_ONERR_FASTINTER:
433 /* force fastinter - nothing to do here as all modes force it */
434 break;
435
436 case HANA_ONERR_SUDDTH:
437 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900438 if (s->check.health > s->check.rise)
439 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100440
441 /* no break - fall through */
442
443 case HANA_ONERR_FAILCHK:
444 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900445 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200446 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100447 break;
448
449 case HANA_ONERR_MARKDWN:
450 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900451 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900452 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200453 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100454 break;
455
456 default:
457 /* write a warning? */
458 break;
459 }
460
461 s->consecutive_errors = 0;
462 s->counters.failed_hana++;
463
Simon Horman66183002013-02-23 10:16:43 +0900464 if (s->check.fastinter) {
465 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300466 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200467 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300468 /* requeue check task with new expire */
469 task_queue(s->check.task);
470 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100471 }
472}
473
Willy Tarreaua1dab552014-04-14 15:04:54 +0200474static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100475{
476 int sv_state;
477 int ratio;
478 int hlen = 0;
479 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
480 "UP %d/%d", "UP",
481 "NOLB %d/%d", "NOLB",
482 "no check" };
483
484 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
485 hlen += 24;
486
Willy Tarreauff5ae352013-12-11 20:36:34 +0100487 if (!(s->check.state & CHK_ST_ENABLED))
488 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200489 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900490 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100491 sv_state = 3; /* UP */
492 else
493 sv_state = 2; /* going down */
494
Willy Tarreau892337c2014-05-13 23:41:20 +0200495 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100496 sv_state += 2;
497 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900498 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100499 sv_state = 1; /* going up */
500 else
501 sv_state = 0; /* DOWN */
502 }
503
Willy Tarreaua1dab552014-04-14 15:04:54 +0200504 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100505 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200506 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
507 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100508
Willy Tarreaua1dab552014-04-14 15:04:54 +0200509 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 +0100510 s->proxy->id, s->id,
511 global.node,
512 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
513 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
514 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
515 s->nbpend);
516
Willy Tarreau892337c2014-05-13 23:41:20 +0200517 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100518 now.tv_sec < s->last_change + s->slowstart &&
519 now.tv_sec >= s->last_change) {
520 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200521 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100522 }
523
524 buffer[hlen++] = '\r';
525 buffer[hlen++] = '\n';
526
527 return hlen;
528}
529
Willy Tarreau20a18342013-12-05 00:31:46 +0100530/* Check the connection. If an error has already been reported or the socket is
531 * closed, keep errno intact as it is supposed to contain the valid error code.
532 * If no error is reported, check the socket's error queue using getsockopt().
533 * Warning, this must be done only once when returning from poll, and never
534 * after an I/O error was attempted, otherwise the error queue might contain
535 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
536 * socket. Returns non-zero if an error was reported, zero if everything is
537 * clean (including a properly closed socket).
538 */
539static int retrieve_errno_from_socket(struct connection *conn)
540{
541 int skerr;
542 socklen_t lskerr = sizeof(skerr);
543
544 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
545 return 1;
546
Willy Tarreau3c728722014-01-23 13:50:42 +0100547 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100548 return 0;
549
550 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
551 errno = skerr;
552
553 if (errno == EAGAIN)
554 errno = 0;
555
556 if (!errno) {
557 /* we could not retrieve an error, that does not mean there is
558 * none. Just don't change anything and only report the prior
559 * error if any.
560 */
561 if (conn->flags & CO_FL_ERROR)
562 return 1;
563 else
564 return 0;
565 }
566
567 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
568 return 1;
569}
570
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100571/* Try to collect as much information as possible on the connection status,
572 * and adjust the server status accordingly. It may make use of <errno_bck>
573 * if non-null when the caller is absolutely certain of its validity (eg:
574 * checked just after a syscall). If the caller doesn't have a valid errno,
575 * it can pass zero, and retrieve_errno_from_socket() will be called to try
576 * to extract errno from the socket. If no error is reported, it will consider
577 * the <expired> flag. This is intended to be used when a connection error was
578 * reported in conn->flags or when a timeout was reported in <expired>. The
579 * function takes care of not updating a server status which was already set.
580 * All situations where at least one of <expired> or CO_FL_ERROR are set
581 * produce a status.
582 */
583static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
584{
585 struct check *check = conn->owner;
586 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200587 struct chunk *chk;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100588
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100589 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100590 return;
591
592 errno = errno_bck;
593 if (!errno || errno == EAGAIN)
594 retrieve_errno_from_socket(conn);
595
596 if (!(conn->flags & CO_FL_ERROR) && !expired)
597 return;
598
599 /* we'll try to build a meaningful error message depending on the
600 * context of the error possibly present in conn->err_code, and the
601 * socket error possibly collected above. This is useful to know the
602 * exact step of the L6 layer (eg: SSL handshake).
603 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200604 chk = get_trash_chunk();
605
606 if (check->type == PR_O2_TCPCHK_CHK) {
607 chunk_printf(chk, " at step %d of tcp-check", tcpcheck_get_step_id(check->server));
608 /* we were looking for a string */
Baptiste Assmann69e273f2013-12-11 00:52:19 +0100609 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
610 chunk_appendf(chk, " (connect)");
611 }
612 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200613 if (check->current_step->string)
614 chunk_appendf(chk, " (string '%s')", check->current_step->string);
615 else if (check->current_step->expect_regex)
616 chunk_appendf(chk, " (expect regex)");
617 }
618 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
619 chunk_appendf(chk, " (send)");
620 }
621 }
622
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100623 if (conn->err_code) {
624 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200625 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100626 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200627 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100628 err_msg = trash.str;
629 }
630 else {
631 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200632 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100633 err_msg = trash.str;
634 }
635 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200636 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100637 }
638 }
639
640 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
641 /* L4 not established (yet) */
642 if (conn->flags & CO_FL_ERROR)
643 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
644 else if (expired)
645 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
646 }
647 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
648 /* L6 not established (yet) */
649 if (conn->flags & CO_FL_ERROR)
650 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
651 else if (expired)
652 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
653 }
654 else if (conn->flags & CO_FL_ERROR) {
655 /* I/O error after connection was established and before we could diagnose */
656 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
657 }
658 else if (expired) {
659 /* connection established but expired check */
660 if (check->type == PR_O2_SSL3_CHK)
661 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
662 else /* HTTP, SMTP, ... */
663 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
664 }
665
666 return;
667}
668
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669/*
670 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200671 * the connection acknowledgement. If the proxy requires L7 health-checks,
672 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900673 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200675static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676{
Simon Horman4a741432013-02-23 15:35:38 +0900677 struct check *check = conn->owner;
678 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900679 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200680
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100681 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100682 goto out_wakeup;
683
Willy Tarreau310987a2014-01-22 19:46:33 +0100684 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100685 return;
686
Willy Tarreau20a18342013-12-05 00:31:46 +0100687 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100688 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100689 __conn_data_stop_both(conn);
690 goto out_wakeup;
691 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100692
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100693 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100694 /* if the output is closed, we can't do anything */
695 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100696 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100697 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200698 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200699
Willy Tarreau06559ac2013-12-05 01:53:08 +0100700 /* here, we know that the connection is established. That's enough for
701 * a pure TCP check.
702 */
703 if (!check->type)
704 goto out_wakeup;
705
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200706 if (check->type == PR_O2_TCPCHK_CHK) {
707 tcpcheck_main(conn);
708 return;
709 }
710
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100711 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100712 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100713 if (conn->flags & CO_FL_ERROR) {
714 chk_report_conn_err(conn, errno, 0);
715 __conn_data_stop_both(conn);
716 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100718 if (check->bo->o)
719 return;
720 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200721
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100722 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
723 if (s->proxy->timeout.check) {
724 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
725 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100727 goto out_nowake;
728
Willy Tarreau83749182007-04-15 20:56:27 +0200729 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200730 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200731 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200732 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733}
734
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200736 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200737 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900738 * set_server_check_status() to update check->status, check->duration
739 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200740
741 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
742 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
743 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
744 * response to an SSL HELLO (the principle is that this is enough to
745 * distinguish between an SSL server and a pure TCP relay). All other cases will
746 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
747 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200749static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750{
Simon Horman4a741432013-02-23 15:35:38 +0900751 struct check *check = conn->owner;
752 struct server *s = check->server;
753 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200754 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100755 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200756 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200757
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100758 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200759 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200760
Willy Tarreau310987a2014-01-22 19:46:33 +0100761 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200762 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200763
764 if (check->type == PR_O2_TCPCHK_CHK) {
765 tcpcheck_main(conn);
766 return;
767 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200768
Willy Tarreau83749182007-04-15 20:56:27 +0200769 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
770 * but the connection was closed on the remote end. Fortunately, recv still
771 * works correctly and we don't need to do the getsockopt() on linux.
772 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000773
774 /* Set buffer to point to the end of the data already read, and check
775 * that there is free space remaining. If the buffer is full, proceed
776 * with running the checks without attempting another socket read.
777 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000778
Willy Tarreau03938182010-03-17 21:52:07 +0100779 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000780
Simon Horman4a741432013-02-23 15:35:38 +0900781 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200782 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100783 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900784 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200785 /* Report network errors only if we got no other data. Otherwise
786 * we'll let the upper layers decide whether the response is OK
787 * or not. It is very common that an RST sent by the server is
788 * reported as an error just after the last data chunk.
789 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100790 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100791 goto out_wakeup;
792 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793 }
794
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100795
Willy Tarreau03938182010-03-17 21:52:07 +0100796 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900797 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100798 */
Simon Horman4a741432013-02-23 15:35:38 +0900799 if (check->bi->i < check->bi->size)
800 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100801 else {
Simon Horman4a741432013-02-23 15:35:38 +0900802 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100803 done = 1; /* buffer full, don't wait for more data */
804 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200805
Nick Chalk57b1bf72010-03-16 15:50:46 +0000806 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900807 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200808 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900809 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100810 goto wait_more_data;
811
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100812 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900813 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
814 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
815 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
816 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
817 !isdigit((unsigned char) *(check->bi->data + 11))) {
818 cut_crlf(check->bi->data);
819 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200820
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100821 goto out_wakeup;
822 }
823
Simon Horman4a741432013-02-23 15:35:38 +0900824 check->code = str2uic(check->bi->data + 9);
825 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000826
Willy Tarreaubd741542010-03-16 18:46:54 +0100827 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200828 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000829 /* 404 may be accepted as "stopping" only if the server was up */
830 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900831 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000832 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100833 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
834 /* Run content verification check... We know we have at least 13 chars */
835 if (!httpchk_expect(s, done))
836 goto wait_more_data;
837 }
838 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900839 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100840 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900841 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100842 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000843 else {
844 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900845 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000846 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200847 break;
848
849 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900850 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100851 goto wait_more_data;
852
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100853 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900854 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
855 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200856 else
Simon Horman4a741432013-02-23 15:35:38 +0900857 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200858 break;
859
860 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900861 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100862 goto wait_more_data;
863
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200864 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900865 if ((check->bi->i < strlen("000\r")) ||
866 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
867 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
868 !isdigit((unsigned char) *(check->bi->data + 2))) {
869 cut_crlf(check->bi->data);
870 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200871
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200872 goto out_wakeup;
873 }
874
Simon Horman4a741432013-02-23 15:35:38 +0900875 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200876
Simon Horman4a741432013-02-23 15:35:38 +0900877 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200878 cut_crlf(desc);
879
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100880 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900881 if (*check->bi->data == '2')
882 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200883 else
Simon Horman4a741432013-02-23 15:35:38 +0900884 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200885 break;
886
Simon Hormana2b9dad2013-02-12 10:45:54 +0900887 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100888 int status = HCHK_STATUS_CHECKED;
889 const char *hs = NULL; /* health status */
890 const char *as = NULL; /* admin status */
891 const char *ps = NULL; /* performance status */
892 const char *err = NULL; /* first error to report */
893 const char *wrn = NULL; /* first warning to report */
894 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900895
Willy Tarreau81f5d942013-12-09 20:51:51 +0100896 /* We're getting an agent check response. The agent could
897 * have been disabled in the mean time with a long check
898 * still pending. It is important that we ignore the whole
899 * response.
900 */
901 if (!(check->server->agent.state & CHK_ST_ENABLED))
902 break;
903
904 /* The agent supports strings made of a single line ended by the
905 * first CR ('\r') or LF ('\n'). This line is composed of words
906 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
907 * line may optionally contained a description of a state change
908 * after a sharp ('#'), which is only considered if a health state
909 * is announced.
910 *
911 * Words may be composed of :
912 * - a numeric weight suffixed by the percent character ('%').
913 * - a health status among "up", "down", "stopped", and "fail".
914 * - an admin status among "ready", "drain", "maint".
915 *
916 * These words may appear in any order. If multiple words of the
917 * same category appear, the last one wins.
918 */
919
Willy Tarreau9809b782013-12-11 21:40:11 +0100920 p = check->bi->data;
921 while (*p && *p != '\n' && *p != '\r')
922 p++;
923
924 if (!*p) {
925 if (!done)
926 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900927
Willy Tarreau9809b782013-12-11 21:40:11 +0100928 /* at least inform the admin that the agent is mis-behaving */
929 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
930 break;
931 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100932
Willy Tarreau9809b782013-12-11 21:40:11 +0100933 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100934 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900935
Willy Tarreau81f5d942013-12-09 20:51:51 +0100936 while (*cmd) {
937 /* look for next word */
938 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
939 cmd++;
940 continue;
941 }
Simon Horman671b6f02013-11-25 10:46:39 +0900942
Willy Tarreau81f5d942013-12-09 20:51:51 +0100943 if (*cmd == '#') {
944 /* this is the beginning of a health status description,
945 * skip the sharp and blanks.
946 */
947 cmd++;
948 while (*cmd == '\t' || *cmd == ' ')
949 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900950 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900951 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100952
953 /* find the end of the word so that we have a null-terminated
954 * word between <cmd> and <p>.
955 */
956 p = cmd + 1;
957 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
958 p++;
959 if (*p)
960 *p++ = 0;
961
962 /* first, health statuses */
963 if (strcasecmp(cmd, "up") == 0) {
964 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900965 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100966 hs = cmd;
967 }
968 else if (strcasecmp(cmd, "down") == 0) {
969 check->health = 0;
970 status = HCHK_STATUS_L7STS;
971 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900972 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100973 else if (strcasecmp(cmd, "stopped") == 0) {
974 check->health = 0;
975 status = HCHK_STATUS_L7STS;
976 hs = cmd;
977 }
978 else if (strcasecmp(cmd, "fail") == 0) {
979 check->health = 0;
980 status = HCHK_STATUS_L7STS;
981 hs = cmd;
982 }
983 /* admin statuses */
984 else if (strcasecmp(cmd, "ready") == 0) {
985 as = cmd;
986 }
987 else if (strcasecmp(cmd, "drain") == 0) {
988 as = cmd;
989 }
990 else if (strcasecmp(cmd, "maint") == 0) {
991 as = cmd;
992 }
993 /* else try to parse a weight here and keep the last one */
994 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
995 ps = cmd;
996 }
997 else {
998 /* keep a copy of the first error */
999 if (!err)
1000 err = cmd;
1001 }
1002 /* skip to next word */
1003 cmd = p;
1004 }
1005 /* here, cmd points either to \0 or to the beginning of a
1006 * description. Skip possible leading spaces.
1007 */
1008 while (*cmd == ' ' || *cmd == '\n')
1009 cmd++;
1010
1011 /* First, update the admin status so that we avoid sending other
1012 * possibly useless warnings and can also update the health if
1013 * present after going back up.
1014 */
1015 if (as) {
1016 if (strcasecmp(as, "drain") == 0)
1017 srv_adm_set_drain(check->server);
1018 else if (strcasecmp(as, "maint") == 0)
1019 srv_adm_set_maint(check->server);
1020 else
1021 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001022 }
1023
Willy Tarreau81f5d942013-12-09 20:51:51 +01001024 /* now change weights */
1025 if (ps) {
1026 const char *msg;
1027
1028 msg = server_parse_weight_change_request(s, ps);
1029 if (!wrn || !*wrn)
1030 wrn = msg;
1031 }
1032
1033 /* and finally health status */
1034 if (hs) {
1035 /* We'll report some of the warnings and errors we have
1036 * here. Down reports are critical, we leave them untouched.
1037 * Lack of report, or report of 'UP' leaves the room for
1038 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001039 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001040 const char *msg = cmd;
1041 struct chunk *t;
1042
1043 if (!*msg || status == HCHK_STATUS_L7OKD) {
1044 if (err && *err)
1045 msg = err;
1046 else if (wrn && *wrn)
1047 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001048 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001049
1050 t = get_trash_chunk();
1051 chunk_printf(t, "via agent : %s%s%s%s",
1052 hs, *msg ? " (" : "",
1053 msg, *msg ? ")" : "");
1054
1055 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001056 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001057 else if (err && *err) {
1058 /* No status change but we'd like to report something odd.
1059 * Just report the current state and copy the message.
1060 */
1061 chunk_printf(&trash, "agent reports an error : %s", err);
1062 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001063
Willy Tarreau81f5d942013-12-09 20:51:51 +01001064 }
1065 else if (wrn && *wrn) {
1066 /* No status change but we'd like to report something odd.
1067 * Just report the current state and copy the message.
1068 */
1069 chunk_printf(&trash, "agent warns : %s", wrn);
1070 set_server_check_status(check, status/*check->status*/, trash.str);
1071 }
1072 else
1073 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001074 break;
1075 }
1076
Willy Tarreau1620ec32011-08-06 17:05:02 +02001077 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001078 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001079 goto wait_more_data;
1080
Simon Horman4a741432013-02-23 15:35:38 +09001081 if (check->bi->data[0] == 'R') {
1082 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001083 }
1084 else {
Simon Horman4a741432013-02-23 15:35:38 +09001085 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1086 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001087 else
1088 desc = "PostgreSQL unknown error";
1089
Simon Horman4a741432013-02-23 15:35:38 +09001090 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001091 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001092 break;
1093
1094 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001095 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001096 goto wait_more_data;
1097
Simon Horman4a741432013-02-23 15:35:38 +09001098 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1099 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001100 }
1101 else {
Simon Horman4a741432013-02-23 15:35:38 +09001102 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001103 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001104 break;
1105
1106 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001107 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001108 goto wait_more_data;
1109
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001110 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001111 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001112 /* We set the MySQL Version in description for information purpose
1113 * FIXME : it can be cool to use MySQL Version for other purpose,
1114 * like mark as down old MySQL server.
1115 */
Simon Horman4a741432013-02-23 15:35:38 +09001116 if (check->bi->i > 51) {
1117 desc = ltrim(check->bi->data + 5, ' ');
1118 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001119 }
1120 else {
1121 if (!done)
1122 goto wait_more_data;
1123 /* it seems we have a OK packet but without a valid length,
1124 * it must be a protocol error
1125 */
Simon Horman4a741432013-02-23 15:35:38 +09001126 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001127 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001128 }
1129 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001130 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001131 desc = ltrim(check->bi->data + 7, ' ');
1132 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001133 }
1134 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001135 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1136 (((unsigned int) *(check->bi->data + 1)) << 8) +
1137 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001138
Simon Horman4a741432013-02-23 15:35:38 +09001139 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001140 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001141 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001142 /* We have only one MySQL packet and it is a Handshake Initialization packet
1143 * but we need to have a second packet to know if it is alright
1144 */
Simon Horman4a741432013-02-23 15:35:38 +09001145 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001146 goto wait_more_data;
1147 }
1148 else {
1149 /* We have only one packet and it is an Error packet,
1150 * an error message is attached, so we can display it
1151 */
Simon Horman4a741432013-02-23 15:35:38 +09001152 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001153 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001154 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001155 }
Simon Horman4a741432013-02-23 15:35:38 +09001156 } else if (check->bi->i > first_packet_len + 4) {
1157 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1158 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1159 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001160
Simon Horman4a741432013-02-23 15:35:38 +09001161 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001162 /* We have 2 packets and that's good */
1163 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001164 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001165 /* No error packet */
1166 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001167 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001168 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001169 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001170 }
1171 else {
1172 /* An error message is attached in the Error packet
1173 * so we can display it ! :)
1174 */
Simon Horman4a741432013-02-23 15:35:38 +09001175 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001176 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001177 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001178 }
1179 }
1180 }
1181 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001182 if (!done)
1183 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001184 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001185 * it must be a protocol error
1186 */
Simon Horman4a741432013-02-23 15:35:38 +09001187 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001188 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001189 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001190 }
1191 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001192 break;
1193
1194 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001195 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001196 goto wait_more_data;
1197
1198 /* Check if the server speaks LDAP (ASN.1/BER)
1199 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1200 * http://tools.ietf.org/html/rfc4511
1201 */
1202
1203 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1204 * LDAPMessage: 0x30: SEQUENCE
1205 */
Simon Horman4a741432013-02-23 15:35:38 +09001206 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1207 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001208 }
1209 else {
1210 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001211 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001212
1213 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1214 * messageID: 0x02 0x01 0x01: INTEGER 1
1215 * protocolOp: 0x61: bindResponse
1216 */
1217 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001218 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1219 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001220
1221 goto out_wakeup;
1222 }
1223
1224 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001225 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001226
1227 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1228 * ldapResult: 0x0a 0x01: ENUMERATION
1229 */
1230 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001231 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1232 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001233
1234 goto out_wakeup;
1235 }
1236
1237 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1238 * resultCode
1239 */
Simon Horman4a741432013-02-23 15:35:38 +09001240 check->code = *(check->bi->data + msglen + 9);
1241 if (check->code) {
1242 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 +02001243 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001244 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001245 }
1246 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001247 break;
1248
1249 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001250 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001251 break;
1252 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001253
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001254 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001255 /* collect possible new errors */
1256 if (conn->flags & CO_FL_ERROR)
1257 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001258
Nick Chalk57b1bf72010-03-16 15:50:46 +00001259 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001260 *check->bi->data = '\0';
1261 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001262
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001263 /* Close the connection... We absolutely want to perform a hard close
1264 * and reset the connection if some data are pending, otherwise we end
1265 * up with many TIME_WAITs and eat all the source port range quickly.
1266 * To avoid sending RSTs all the time, we first try to drain pending
1267 * data.
1268 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001269 if (conn->xprt && conn->xprt->shutw)
1270 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001271
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001272 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001273 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001274 conn->flags |= CO_FL_ERROR;
1275
Willy Tarreaua522f802012-11-23 08:56:35 +01001276 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001277 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001278 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001279
1280 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001281 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282}
1283
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001284/*
1285 * This function is used only for server health-checks. It handles connection
1286 * status updates including errors. If necessary, it wakes the check task up.
1287 * It always returns 0.
1288 */
1289static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001290{
Simon Horman4a741432013-02-23 15:35:38 +09001291 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001292
Willy Tarreau6c560da2012-11-24 11:14:45 +01001293 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001294 /* We may get error reports bypassing the I/O handlers, typically
1295 * the case when sending a pure TCP check which fails, then the I/O
1296 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001297 * main processing task so let's simply wake it up. If we get here,
1298 * we expect errno to still be valid.
1299 */
1300 chk_report_conn_err(conn, errno, 0);
1301
Willy Tarreau2d351b62013-12-05 02:36:25 +01001302 __conn_data_stop_both(conn);
1303 task_wakeup(check->task, TASK_WOKEN_IO);
1304 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001305 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1306 /* we may get here if only a connection probe was required : we
1307 * don't have any data to send nor anything expected in response,
1308 * so the completion of the connection establishment is enough.
1309 */
1310 task_wakeup(check->task, TASK_WOKEN_IO);
1311 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001312
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001313 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001314 /* We're here because nobody wants to handle the error, so we
1315 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001316 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001317 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001318 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001319 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001320 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001321}
1322
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001323struct data_cb check_conn_cb = {
1324 .recv = event_srv_chk_r,
1325 .send = event_srv_chk_w,
1326 .wake = wake_srv_chk,
1327};
1328
Willy Tarreaubaaee002006-06-26 02:48:02 +02001329/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001330 * updates the server's weight during a warmup stage. Once the final weight is
1331 * reached, the task automatically stops. Note that any server status change
1332 * must have updated s->last_change accordingly.
1333 */
1334static struct task *server_warmup(struct task *t)
1335{
1336 struct server *s = t->context;
1337
1338 /* by default, plan on stopping the task */
1339 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001340 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001341 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001342 return t;
1343
Willy Tarreau892337c2014-05-13 23:41:20 +02001344 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001345 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001346
1347 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001348 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001349
1350 /* get back there in 1 second or 1/20th of the slowstart interval,
1351 * whichever is greater, resulting in small 5% steps.
1352 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001353 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001354 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1355 return t;
1356}
1357
1358/*
Simon Horman98637e52014-06-20 12:30:16 +09001359 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001360 *
1361 * It can return one of :
1362 * - SN_ERR_NONE if everything's OK and tcpcheck_main() was not called
1363 * - SN_ERR_UP if if everything's OK and tcpcheck_main() was called
1364 * - SN_ERR_SRVTO if there are no more servers
1365 * - SN_ERR_SRVCL if the connection was refused by the server
1366 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1367 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1368 * - SN_ERR_INTERNAL for any other purely internal errors
1369 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1370 * Note that we try to prevent the network stack from sending the ACK during the
1371 * connect() when a pure TCP check is used (without PROXY protocol).
1372 */
Simon Horman98637e52014-06-20 12:30:16 +09001373static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001374{
1375 struct check *check = t->context;
1376 struct server *s = check->server;
1377 struct connection *conn = check->conn;
1378 struct protocol *proto;
1379 int ret;
1380
1381 /* tcpcheck send/expect initialisation */
1382 if (check->type == PR_O2_TCPCHK_CHK)
1383 check->current_step = NULL;
1384
1385 /* prepare the check buffer.
1386 * This should not be used if check is the secondary agent check
1387 * of a server as s->proxy->check_req will relate to the
1388 * configuration of the primary check. Similarly, tcp-check uses
1389 * its own strings.
1390 */
1391 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1392 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1393
1394 /* we want to check if this host replies to HTTP or SSLv3 requests
1395 * so we'll send the request, and won't wake the checker up now.
1396 */
1397 if ((check->type) == PR_O2_SSL3_CHK) {
1398 /* SSL requires that we put Unix time in the request */
1399 int gmt_time = htonl(date.tv_sec);
1400 memcpy(check->bo->data + 11, &gmt_time, 4);
1401 }
1402 else if ((check->type) == PR_O2_HTTP_CHK) {
1403 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1404 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
1405 bo_putstr(check->bo, "\r\n");
1406 *check->bo->p = '\0'; /* to make gdb output easier to read */
1407 }
1408 }
1409
1410 /* prepare a new connection */
1411 conn_init(conn);
1412 conn_prepare(conn, s->check_common.proto, s->check_common.xprt);
1413 conn_attach(conn, check, &check_conn_cb);
1414 conn->target = &s->obj_type;
1415
1416 /* no client address */
1417 clear_addr(&conn->addr.from);
1418
1419 if (is_addr(&s->check_common.addr)) {
1420
1421 /* we'll connect to the check addr specified on the server */
1422 conn->addr.to = s->check_common.addr;
1423 proto = s->check_common.proto;
1424 }
1425 else {
1426 /* we'll connect to the addr on the server */
1427 conn->addr.to = s->addr;
1428 proto = s->proto;
1429 }
1430
1431 if (check->port) {
1432 set_host_port(&conn->addr.to, check->port);
1433 }
1434
1435 if (check->type == PR_O2_TCPCHK_CHK) {
1436 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1437 /* if first step is a 'connect', then tcpcheck_main must run it */
1438 if (r->action == TCPCHK_ACT_CONNECT) {
1439 tcpcheck_main(conn);
1440 return SN_ERR_UP;
1441 }
1442 }
1443
1444 ret = SN_ERR_INTERNAL;
1445 if (proto->connect)
1446 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
1447 conn->flags |= CO_FL_WAKE_DATA;
1448 if (s->check.send_proxy) {
1449 conn->send_proxy_ofs = 1;
1450 conn->flags |= CO_FL_SEND_PROXY;
1451 }
1452
1453 return ret;
1454}
1455
Simon Horman98637e52014-06-20 12:30:16 +09001456static struct list pid_list = LIST_HEAD_INIT(pid_list);
1457static struct pool_head *pool2_pid_list;
1458
1459void block_sigchld(void)
1460{
1461 sigset_t set;
1462 sigemptyset(&set);
1463 sigaddset(&set, SIGCHLD);
1464 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1465}
1466
1467void unblock_sigchld(void)
1468{
1469 sigset_t set;
1470 sigemptyset(&set);
1471 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1472}
1473
1474/* Call with SIGCHLD blocked */
1475static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1476{
1477 struct pid_list *elem;
1478 struct check *check = t->context;
1479
1480 elem = pool_alloc2(pool2_pid_list);
1481 if (!elem)
1482 return NULL;
1483 elem->pid = pid;
1484 elem->t = t;
1485 elem->exited = 0;
1486 check->curpid = elem;
1487 LIST_INIT(&elem->list);
1488 LIST_ADD(&pid_list, &elem->list);
1489 return elem;
1490}
1491
1492/* Blocks blocks and then unblocks SIGCHLD */
1493static void pid_list_del(struct pid_list *elem)
1494{
1495 struct check *check;
1496
1497 if (!elem)
1498 return;
1499
1500 block_sigchld();
1501 LIST_DEL(&elem->list);
1502 unblock_sigchld();
1503 if (!elem->exited)
1504 kill(elem->pid, SIGTERM);
1505
1506 check = elem->t->context;
1507 check->curpid = NULL;
1508 pool_free2(pool2_pid_list, elem);
1509}
1510
1511/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1512static void pid_list_expire(pid_t pid, int status)
1513{
1514 struct pid_list *elem;
1515
1516 list_for_each_entry(elem, &pid_list, list) {
1517 if (elem->pid == pid) {
1518 elem->t->expire = now_ms;
1519 elem->status = status;
1520 elem->exited = 1;
1521 return;
1522 }
1523 }
1524}
1525
1526static void sigchld_handler(int signal)
1527{
1528 pid_t pid;
1529 int status;
1530 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1531 pid_list_expire(pid, status);
1532}
1533
1534static int init_pid_list(void) {
1535 struct sigaction action = {
1536 .sa_handler = sigchld_handler,
1537 .sa_flags = SA_NOCLDSTOP
1538 };
1539
1540 if (pool2_pid_list != NULL)
1541 /* Nothing to do */
1542 return 0;
1543
1544 if (sigaction(SIGCHLD, &action, NULL)) {
1545 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1546 strerror(errno));
1547 return 1;
1548 }
1549
1550 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1551 if (pool2_pid_list == NULL) {
1552 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1553 strerror(errno));
1554 return 1;
1555 }
1556
1557 return 0;
1558}
1559
1560
1561static int prepare_external_check(struct check *check)
1562{
1563 struct server *s = check->server;
1564 struct proxy *px = s->proxy;
1565 struct listener *listener = NULL, *l;
1566 int i;
1567 const char *err_fmt = "Starting [%s:%s] check: out of memory.\n";
1568 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
1569 char host[46];
1570 char serv[6];
1571
1572 list_for_each_entry(l, &px->conf.listeners, by_fe)
1573 /* Use the first INET, INET6 or UNIX listener */
1574 if (l->addr.ss_family == AF_INET ||
1575 l->addr.ss_family == AF_INET6 ||
1576 l->addr.ss_family == AF_UNIX) {
1577 listener = l;
1578 break;
1579 }
1580
1581 if (!listener) {
1582 err_fmt = "Starting [%s:%s] check: no listener.\n";
1583 goto err;
1584 }
1585
1586 check->curpid = NULL;
1587
1588 check->envp = calloc(2, sizeof(check->argv));
1589 if (!check->envp)
1590 goto err;
1591 check->envp[0] = malloc(strlen("PATH=") + strlen(path) + 1);
1592 if (!check->envp[0])
1593 goto err;
1594 strcpy(check->envp[0], "PATH=");
1595 strcpy(check->envp[0] + strlen(check->envp[0]), path);
1596 check->envp[1] = NULL;
1597
1598 check->argv = calloc(6, sizeof(check->argv));
1599 if (!check->argv)
1600 goto err;
1601
1602 check->argv[0] = px->check_command;
1603
1604 if (listener->addr.ss_family == AF_INET ||
1605 listener->addr.ss_family == AF_INET6) {
1606 addr_to_str(&listener->addr, host, sizeof(host));
1607 check->argv[1] = strdup(host);
1608 port_to_str(&listener->addr, serv, sizeof(serv));
1609 check->argv[2] = strdup(serv);
1610 } else if (listener->addr.ss_family == AF_UNIX) {
1611 const struct sockaddr_un *un;
1612
1613 un = (struct sockaddr_un *)&listener->addr;
1614 check->argv[1] = strdup(un->sun_path);
1615 check->argv[2] = strdup("NOT_USED");
1616 } else {
1617 goto err;
1618 }
1619
1620 addr_to_str(&s->addr, host, sizeof(host));
1621 check->argv[3] = strdup(host);
1622 port_to_str(&s->addr, serv, sizeof(serv));
1623 check->argv[4] = strdup(serv);
1624
1625 for (i = 0; i < 5; i++)
1626 if (!check->argv[i])
1627 goto err;
1628
1629 return 0;
1630err:
1631 if (check->envp) {
1632 free(check->envp[1]);
1633 free(check->envp);
1634 check->envp = NULL;
1635 }
1636
1637 if (check->argv) {
1638 for (i = 1; i < 5; i++)
1639 free(check->argv[i]);
1640 free(check->argv);
1641 check->argv = NULL;
1642 }
1643 Alert(err_fmt, px->id, s->id);
1644 return -1;
1645}
1646
Simon Hormanb00d17a2014-06-13 16:18:16 +09001647/*
Simon Horman98637e52014-06-20 12:30:16 +09001648 * establish a server health-check that makes use of a process.
1649 *
1650 * It can return one of :
1651 * - SN_ERR_NONE if everything's OK
1652 * - SN_ERR_SRVTO if there are no more servers
1653 * - SN_ERR_SRVCL if the connection was refused by the server
1654 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1655 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1656 * - SN_ERR_INTERNAL for any other purely internal errors
1657 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1658 *
1659 * Blocks and then unblocks SIGCHLD
1660 */
1661static int connect_proc_chk(struct task *t)
1662{
1663 struct check *check = t->context;
1664 struct server *s = check->server;
1665 struct proxy *px = s->proxy;
1666 int status;
1667 pid_t pid;
1668
1669 if (!check->argv) {
1670 status = prepare_external_check(check);
1671 if (status < 0)
1672 return SN_ERR_RESOURCE;
1673 }
1674
1675 status = SN_ERR_RESOURCE;
1676
1677 block_sigchld();
1678
1679 pid = fork();
1680 if (pid < 0) {
1681 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1682 strerror(errno));
1683 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1684 goto out;
1685 }
1686 if (pid == 0) {
1687 /* Child */
1688 extern char **environ;
1689 environ = check->envp;
1690 execvp(px->check_command, check->argv);
1691 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1692 strerror(errno));
1693 exit(-1);
1694 }
1695
1696 /* Parent */
1697 if (check->result == CHK_RES_UNKNOWN) {
1698 if (pid_list_add(pid, t) != NULL) {
1699 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1700
1701 if (px->timeout.check && px->timeout.connect) {
1702 int t_con = tick_add(now_ms, px->timeout.connect);
1703 t->expire = tick_first(t->expire, t_con);
1704 }
1705 status = SN_ERR_NONE;
1706 goto out;
1707 }
1708 else {
1709 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1710 }
1711 kill(pid, SIGTERM); /* process creation error */
1712 }
1713 else
1714 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1715
1716out:
1717 unblock_sigchld();
1718 return status;
1719}
1720
1721/*
1722 * establish a server health-check.
1723 *
1724 * It can return one of :
1725 * - SN_ERR_NONE if everything's OK
1726 * - SN_ERR_SRVTO if there are no more servers
1727 * - SN_ERR_SRVCL if the connection was refused by the server
1728 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1729 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1730 * - SN_ERR_INTERNAL for any other purely internal errors
1731 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1732 */
1733static int connect_chk(struct task *t)
1734{
1735 struct check *check = t->context;
1736
1737 if (check->type == PR_O2_EXT_CHK)
1738 return connect_proc_chk(t);
1739 return connect_conn_chk(t);
1740}
1741
1742/*
1743 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001744 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1745 */
Simon Horman98637e52014-06-20 12:30:16 +09001746static struct task *process_chk_proc(struct task *t)
1747{
1748 struct check *check = t->context;
1749 struct server *s = check->server;
1750 struct connection *conn = check->conn;
1751 int rv;
1752 int ret;
1753 int expired = tick_is_expired(t->expire, now_ms);
1754
1755 if (!(check->state & CHK_ST_INPROGRESS)) {
1756 /* no check currently running */
1757 if (!expired) /* woke up too early */
1758 return t;
1759
1760 /* we don't send any health-checks when the proxy is
1761 * stopped, the server should not be checked or the check
1762 * is disabled.
1763 */
1764 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1765 s->proxy->state == PR_STSTOPPED)
1766 goto reschedule;
1767
1768 /* we'll initiate a new check */
1769 set_server_check_status(check, HCHK_STATUS_START, NULL);
1770
1771 check->state |= CHK_ST_INPROGRESS;
1772
1773 ret = connect_chk(t);
1774 switch (ret) {
1775 case SN_ERR_UP:
1776 return t;
1777 case SN_ERR_NONE:
1778 /* we allow up to min(inter, timeout.connect) for a connection
1779 * to establish but only when timeout.check is set
1780 * as it may be to short for a full check otherwise
1781 */
1782 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1783
1784 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1785 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1786 t->expire = tick_first(t->expire, t_con);
1787 }
1788
1789 goto reschedule;
1790
1791 case SN_ERR_SRVTO: /* ETIMEDOUT */
1792 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1793 conn->flags |= CO_FL_ERROR;
1794 chk_report_conn_err(conn, errno, 0);
1795 break;
1796 case SN_ERR_PRXCOND:
1797 case SN_ERR_RESOURCE:
1798 case SN_ERR_INTERNAL:
1799 conn->flags |= CO_FL_ERROR;
1800 chk_report_conn_err(conn, 0, 0);
1801 break;
1802 }
1803
1804 /* here, we have seen a synchronous error, no fd was allocated */
1805
1806 check->state &= ~CHK_ST_INPROGRESS;
1807 check_notify_failure(check);
1808
1809 /* we allow up to min(inter, timeout.connect) for a connection
1810 * to establish but only when timeout.check is set
1811 * as it may be to short for a full check otherwise
1812 */
1813 while (tick_is_expired(t->expire, now_ms)) {
1814 int t_con;
1815
1816 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1817 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1818
1819 if (s->proxy->timeout.check)
1820 t->expire = tick_first(t->expire, t_con);
1821 }
1822 }
1823 else {
1824 /* there was a test running.
1825 * First, let's check whether there was an uncaught error,
1826 * which can happen on connect timeout or error.
1827 */
1828 if (check->result == CHK_RES_UNKNOWN) {
1829 /* good connection is enough for pure TCP check */
1830 struct pid_list *elem = check->curpid;
1831 int status = HCHK_STATUS_UNKNOWN;
1832
1833 if (elem->exited) {
1834 status = elem->status; /* Save in case the process exits between use below */
1835 if (!WIFEXITED(status))
1836 check->code = -1;
1837 else
1838 check->code = WEXITSTATUS(status);
1839 if (!WIFEXITED(status) || WEXITSTATUS(status))
1840 status = HCHK_STATUS_PROCERR;
1841 else
1842 status = HCHK_STATUS_PROCOK;
1843 } else if (expired) {
1844 status = HCHK_STATUS_PROCTOUT;
1845 Warning("kill %d\n", elem->pid);
1846 kill(elem->pid, SIGTERM);
1847 }
1848 set_server_check_status(check, status, NULL);
1849 }
1850
1851 if (check->result == CHK_RES_FAILED) {
1852 /* a failure or timeout detected */
1853 check_notify_failure(check);
1854 }
1855 else if (check->result == CHK_RES_CONDPASS) {
1856 /* check is OK but asks for stopping mode */
1857 check_notify_stopping(check);
1858 }
1859 else if (check->result == CHK_RES_PASSED) {
1860 /* a success was detected */
1861 check_notify_success(check);
1862 }
1863 check->state &= ~CHK_ST_INPROGRESS;
1864
1865 pid_list_del(check->curpid);
1866
1867 rv = 0;
1868 if (global.spread_checks > 0) {
1869 rv = srv_getinter(check) * global.spread_checks / 100;
1870 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1871 }
1872 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1873 }
1874
1875 reschedule:
1876 while (tick_is_expired(t->expire, now_ms))
1877 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1878 return t;
1879}
1880
1881/*
1882 * manages a server health-check that uses a connection. Returns
1883 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1884 */
1885static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001886{
Simon Horman4a741432013-02-23 15:35:38 +09001887 struct check *check = t->context;
1888 struct server *s = check->server;
1889 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001890 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001891 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001892 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001893
Willy Tarreau2c115e52013-12-11 19:41:16 +01001894 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001895 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001896 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001897 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001898
Simon Horman671b6f02013-11-25 10:46:39 +09001899 /* we don't send any health-checks when the proxy is
1900 * stopped, the server should not be checked or the check
1901 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001902 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001903 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001904 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001905 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001906
1907 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001908 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001909
Willy Tarreau2c115e52013-12-11 19:41:16 +01001910 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001911 check->bi->p = check->bi->data;
1912 check->bi->i = 0;
1913 check->bo->p = check->bo->data;
1914 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001915
Simon Hormanb00d17a2014-06-13 16:18:16 +09001916 ret = connect_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001917 switch (ret) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001918 case SN_ERR_UP:
1919 return t;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001920 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001921 /* we allow up to min(inter, timeout.connect) for a connection
1922 * to establish but only when timeout.check is set
1923 * as it may be to short for a full check otherwise
1924 */
Simon Horman4a741432013-02-23 15:35:38 +09001925 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001926
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001927 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1928 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1929 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001930 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001931
1932 if (check->type)
1933 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1934
Willy Tarreau5a78f362012-11-23 12:47:05 +01001935 goto reschedule;
1936
1937 case SN_ERR_SRVTO: /* ETIMEDOUT */
1938 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001939 conn->flags |= CO_FL_ERROR;
1940 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001941 break;
1942 case SN_ERR_PRXCOND:
1943 case SN_ERR_RESOURCE:
1944 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001945 conn->flags |= CO_FL_ERROR;
1946 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001947 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001948 }
1949
Willy Tarreau5a78f362012-11-23 12:47:05 +01001950 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001951
Willy Tarreau2c115e52013-12-11 19:41:16 +01001952 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02001953 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001954
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001955 /* we allow up to min(inter, timeout.connect) for a connection
1956 * to establish but only when timeout.check is set
1957 * as it may be to short for a full check otherwise
1958 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001959 while (tick_is_expired(t->expire, now_ms)) {
1960 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001961
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001962 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001963 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001964
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001965 if (s->proxy->timeout.check)
1966 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001967 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001968 }
1969 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001970 /* there was a test running.
1971 * First, let's check whether there was an uncaught error,
1972 * which can happen on connect timeout or error.
1973 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09001974 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001975 /* good connection is enough for pure TCP check */
1976 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001977 if (check->use_ssl)
1978 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001979 else
Simon Horman4a741432013-02-23 15:35:38 +09001980 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001981 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001982 else if ((conn->flags & CO_FL_ERROR) || expired) {
1983 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001984 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001985 else
1986 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001987 }
1988
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001989 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001990 if (conn->xprt) {
1991 /* The check was aborted and the connection was not yet closed.
1992 * This can happen upon timeout, or when an external event such
1993 * as a failed response coupled with "observe layer7" caused the
1994 * server state to be suddenly changed.
1995 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001996 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001997 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001998 }
1999
Willy Tarreauaf549582014-05-16 17:37:50 +02002000 if (check->result == CHK_RES_FAILED) {
2001 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002002 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002003 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002004 else if (check->result == CHK_RES_CONDPASS) {
2005 /* check is OK but asks for stopping mode */
2006 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002007 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002008 else if (check->result == CHK_RES_PASSED) {
2009 /* a success was detected */
2010 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002012 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002013
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002014 rv = 0;
2015 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002016 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002017 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002018 }
Simon Horman4a741432013-02-23 15:35:38 +09002019 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002020 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002021
2022 reschedule:
2023 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002024 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002025 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002026 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002027}
2028
Simon Horman98637e52014-06-20 12:30:16 +09002029/*
2030 * manages a server health-check. Returns
2031 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2032 */
2033static struct task *process_chk(struct task *t)
2034{
2035 struct check *check = t->context;
2036
2037 if (check->type == PR_O2_EXT_CHK)
2038 return process_chk_proc(t);
2039 return process_chk_conn(t);
2040}
2041
Simon Horman5c942422013-11-25 10:46:32 +09002042static int start_check_task(struct check *check, int mininter,
2043 int nbcheck, int srvpos)
2044{
2045 struct task *t;
2046 /* task for the check */
2047 if ((t = task_new()) == NULL) {
2048 Alert("Starting [%s:%s] check: out of memory.\n",
2049 check->server->proxy->id, check->server->id);
2050 return 0;
2051 }
2052
2053 check->task = t;
2054 t->process = process_chk;
2055 t->context = check;
2056
Willy Tarreau1746eec2014-04-25 10:46:47 +02002057 if (mininter < srv_getinter(check))
2058 mininter = srv_getinter(check);
2059
2060 if (global.max_spread_checks && mininter > global.max_spread_checks)
2061 mininter = global.max_spread_checks;
2062
Simon Horman5c942422013-11-25 10:46:32 +09002063 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002064 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002065 check->start = now;
2066 task_queue(t);
2067
2068 return 1;
2069}
2070
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002071/*
2072 * Start health-check.
2073 * Returns 0 if OK, -1 if error, and prints the error in this case.
2074 */
2075int start_checks() {
2076
2077 struct proxy *px;
2078 struct server *s;
2079 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002080 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002081
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002082 /* 1- count the checkers to run simultaneously.
2083 * We also determine the minimum interval among all of those which
2084 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2085 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002086 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002087 * too short an interval for all others.
2088 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002089 for (px = proxy; px; px = px->next) {
2090 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002091 if (s->slowstart) {
2092 if ((t = task_new()) == NULL) {
2093 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2094 return -1;
2095 }
2096 /* We need a warmup task that will be called when the server
2097 * state switches from down to up.
2098 */
2099 s->warmup = t;
2100 t->process = server_warmup;
2101 t->context = s;
2102 t->expire = TICK_ETERNITY;
2103 }
2104
Willy Tarreaud8514a22013-12-11 21:10:14 +01002105 if (s->check.state & CHK_ST_CONFIGURED) {
2106 nbcheck++;
2107 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2108 (!mininter || mininter > srv_getinter(&s->check)))
2109 mininter = srv_getinter(&s->check);
2110 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002111
Willy Tarreaud8514a22013-12-11 21:10:14 +01002112 if (s->agent.state & CHK_ST_CONFIGURED) {
2113 nbcheck++;
2114 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2115 (!mininter || mininter > srv_getinter(&s->agent)))
2116 mininter = srv_getinter(&s->agent);
2117 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002118 }
2119 }
2120
Simon Horman4a741432013-02-23 15:35:38 +09002121 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002122 return 0;
2123
2124 srand((unsigned)time(NULL));
2125
2126 /*
2127 * 2- start them as far as possible from each others. For this, we will
2128 * start them after their interval set to the min interval divided by
2129 * the number of servers, weighted by the server's position in the list.
2130 */
2131 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002132 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2133 if (init_pid_list()) {
2134 Alert("Starting [%s] check: out of memory.\n", px->id);
2135 return -1;
2136 }
2137 }
2138
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002139 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002140 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002141 if (s->check.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002142 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2143 return -1;
2144 srvpos++;
2145 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002146
Simon Hormand60d6912013-11-25 10:46:36 +09002147 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002148 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002149 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2150 return -1;
2151 }
2152 srvpos++;
2153 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002154 }
2155 }
2156 return 0;
2157}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002158
2159/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002160 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002161 * The buffer MUST be terminated by a null byte before calling this function.
2162 * Sets server status appropriately. The caller is responsible for ensuring
2163 * that the buffer contains at least 13 characters. If <done> is zero, we may
2164 * return 0 to indicate that data is required to decide of a match.
2165 */
2166static int httpchk_expect(struct server *s, int done)
2167{
2168 static char status_msg[] = "HTTP status check returned code <000>";
2169 char status_code[] = "000";
2170 char *contentptr;
2171 int crlf;
2172 int ret;
2173
2174 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2175 case PR_O2_EXP_STS:
2176 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002177 memcpy(status_code, s->check.bi->data + 9, 3);
2178 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002179
2180 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2181 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2182 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002183 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002184
2185 /* we necessarily have the response, so there are no partial failures */
2186 if (s->proxy->options2 & PR_O2_EXP_INV)
2187 ret = !ret;
2188
Simon Horman4a741432013-02-23 15:35:38 +09002189 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002190 break;
2191
2192 case PR_O2_EXP_STR:
2193 case PR_O2_EXP_RSTR:
2194 /* very simple response parser: ignore CR and only count consecutive LFs,
2195 * stop with contentptr pointing to first char after the double CRLF or
2196 * to '\0' if crlf < 2.
2197 */
2198 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002199 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002200 if (crlf >= 2)
2201 break;
2202 if (*contentptr == '\r')
2203 continue;
2204 else if (*contentptr == '\n')
2205 crlf++;
2206 else
2207 crlf = 0;
2208 }
2209
2210 /* Check that response contains a body... */
2211 if (crlf < 2) {
2212 if (!done)
2213 return 0;
2214
Simon Horman4a741432013-02-23 15:35:38 +09002215 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002216 "HTTP content check could not find a response body");
2217 return 1;
2218 }
2219
2220 /* Check that response body is not empty... */
2221 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002222 if (!done)
2223 return 0;
2224
Simon Horman4a741432013-02-23 15:35:38 +09002225 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002226 "HTTP content check found empty response body");
2227 return 1;
2228 }
2229
2230 /* Check the response content against the supplied string
2231 * or regex... */
2232 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2233 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2234 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002235 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002236
2237 /* if we don't match, we may need to wait more */
2238 if (!ret && !done)
2239 return 0;
2240
2241 if (ret) {
2242 /* content matched */
2243 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002244 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002245 "HTTP check matched unwanted content");
2246 else
Simon Horman4a741432013-02-23 15:35:38 +09002247 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002248 "HTTP content check matched");
2249 }
2250 else {
2251 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002252 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002253 "HTTP check did not match unwanted content");
2254 else
Simon Horman4a741432013-02-23 15:35:38 +09002255 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002256 "HTTP content check did not match");
2257 }
2258 break;
2259 }
2260 return 1;
2261}
2262
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002263/*
2264 * return the id of a step in a send/expect session
2265 */
2266static int tcpcheck_get_step_id(struct server *s)
2267{
2268 struct tcpcheck_rule *cur = NULL, *next = NULL;
2269 int i = 0;
2270
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002271 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002272
2273 /* no step => first step */
2274 if (cur == NULL)
2275 return 1;
2276
2277 /* increment i until current step */
2278 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
2279 if (next->list.p == &cur->list)
2280 break;
2281 ++i;
2282 }
2283
2284 return i;
2285}
2286
2287static void tcpcheck_main(struct connection *conn)
2288{
2289 char *contentptr;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002290 struct list *head = NULL;
2291 struct tcpcheck_rule *cur = NULL;
2292 int done = 0, ret = 0;
2293
2294 struct check *check = conn->owner;
2295 struct server *s = check->server;
2296 struct task *t = check->task;
2297
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002298 /*
2299 * don't do anything until the connection is established but if we're running
2300 * first step which must be a connect
2301 */
2302 if (check->current_step && (!(conn->flags & CO_FL_CONNECTED))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002303 /* update expire time, should be done by process_chk */
2304 /* we allow up to min(inter, timeout.connect) for a connection
2305 * to establish but only when timeout.check is set
2306 * as it may be to short for a full check otherwise
2307 */
2308 while (tick_is_expired(t->expire, now_ms)) {
2309 int t_con;
2310
2311 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2312 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2313
2314 if (s->proxy->timeout.check)
2315 t->expire = tick_first(t->expire, t_con);
2316 }
2317 return;
2318 }
2319
2320 /* here, we know that the connection is established */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002321 if (check->result != CHK_RES_UNKNOWN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002322 goto out_end_tcpcheck;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002323
2324 /* head is be the first element of the double chained list */
2325 head = &s->proxy->tcpcheck_rules;
2326
2327 /* no step means first step
2328 * initialisation */
2329 if (check->current_step == NULL) {
2330 check->bo->p = check->bo->data;
2331 check->bo->o = 0;
2332 check->bi->p = check->bi->data;
2333 check->bi->i = 0;
2334 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2335 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2336 if (s->proxy->timeout.check)
2337 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2338 }
2339 /* keep on processing step */
2340 else {
2341 cur = check->current_step;
2342 }
2343
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002344 if (conn->flags & CO_FL_HANDSHAKE)
2345 return;
2346
2347 /* It's only the rules which will enable send/recv */
2348 __conn_data_stop_both(conn);
2349
Willy Tarreauabca5b62013-12-06 14:19:25 +01002350 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002351 /* we have to try to flush the output buffer before reading, at the end,
2352 * or if we're about to send a string that does not fit in the remaining space.
2353 */
2354 if (check->bo->o &&
2355 (&cur->list == head ||
2356 check->current_step->action != TCPCHK_ACT_SEND ||
2357 check->current_step->string_len >= buffer_total_space(check->bo))) {
2358
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002359 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002360 if (conn->flags & CO_FL_ERROR) {
2361 chk_report_conn_err(conn, errno, 0);
2362 __conn_data_stop_both(conn);
2363 goto out_end_tcpcheck;
2364 }
2365 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002366 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002367 }
2368
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002369 /* did we reach the end ? If so, let's check that everything was sent */
2370 if (&cur->list == head) {
2371 if (check->bo->o)
2372 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002373 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002374 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002375
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002376 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2377 struct protocol *proto;
2378 struct xprt_ops *xprt;
2379
2380 /* mark the step as started */
2381 check->last_started_step = check->current_step;
2382 /* first, shut existing connection */
2383 conn_force_close(conn);
2384
2385 /* prepare new connection */
2386 /* initialization */
2387 conn_init(conn);
2388 conn_attach(conn, check, &check_conn_cb);
2389 conn->target = &s->obj_type;
2390
2391 /* no client address */
2392 clear_addr(&conn->addr.from);
2393
Willy Tarreau640556c2014-05-09 23:38:15 +02002394 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002395 /* we'll connect to the check addr specified on the server */
2396 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002397 proto = s->check_common.proto;
2398 }
2399 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002400 /* we'll connect to the addr on the server */
2401 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002402 proto = s->proto;
2403 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002404
2405 /* port */
2406 if (check->current_step->port)
2407 set_host_port(&conn->addr.to, check->current_step->port);
2408 else if (check->port)
2409 set_host_port(&conn->addr.to, check->port);
2410
2411#ifdef USE_OPENSSL
2412 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2413 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002414 }
2415 else {
2416 xprt = &raw_sock;
2417 }
2418#else /* USE_OPENSSL */
2419 xprt = &raw_sock;
2420#endif /* USE_OPENSSL */
2421 conn_prepare(conn, proto, xprt);
2422
2423 ret = SN_ERR_INTERNAL;
2424 if (proto->connect)
2425 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
2426 conn->flags |= CO_FL_WAKE_DATA;
2427 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2428 conn->send_proxy_ofs = 1;
2429 conn->flags |= CO_FL_SEND_PROXY;
2430 }
2431
2432 /* It can return one of :
2433 * - SN_ERR_NONE if everything's OK
2434 * - SN_ERR_SRVTO if there are no more servers
2435 * - SN_ERR_SRVCL if the connection was refused by the server
2436 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2437 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2438 * - SN_ERR_INTERNAL for any other purely internal errors
2439 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2440 * Note that we try to prevent the network stack from sending the ACK during the
2441 * connect() when a pure TCP check is used (without PROXY protocol).
2442 */
2443 switch (ret) {
2444 case SN_ERR_NONE:
2445 /* we allow up to min(inter, timeout.connect) for a connection
2446 * to establish but only when timeout.check is set
2447 * as it may be to short for a full check otherwise
2448 */
2449 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2450
2451 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2452 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2453 t->expire = tick_first(t->expire, t_con);
2454 }
2455 break;
2456 case SN_ERR_SRVTO: /* ETIMEDOUT */
2457 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2458 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2459 tcpcheck_get_step_id(s), strerror(errno));
2460 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2461 goto out_end_tcpcheck;
2462 case SN_ERR_PRXCOND:
2463 case SN_ERR_RESOURCE:
2464 case SN_ERR_INTERNAL:
2465 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2466 tcpcheck_get_step_id(s));
2467 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2468 goto out_end_tcpcheck;
2469 }
2470
2471 /* allow next rule */
2472 cur = (struct tcpcheck_rule *)cur->list.n;
2473 check->current_step = cur;
2474
2475 /* don't do anything until the connection is established */
2476 if (!(conn->flags & CO_FL_CONNECTED)) {
2477 /* update expire time, should be done by process_chk */
2478 /* we allow up to min(inter, timeout.connect) for a connection
2479 * to establish but only when timeout.check is set
2480 * as it may be to short for a full check otherwise
2481 */
2482 while (tick_is_expired(t->expire, now_ms)) {
2483 int t_con;
2484
2485 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2486 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2487
2488 if (s->proxy->timeout.check)
2489 t->expire = tick_first(t->expire, t_con);
2490 }
2491 return;
2492 }
2493
2494 } /* end 'connect' */
2495 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2496 /* mark the step as started */
2497 check->last_started_step = check->current_step;
2498
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002499 /* reset the read buffer */
2500 if (*check->bi->data != '\0') {
2501 *check->bi->data = '\0';
2502 check->bi->i = 0;
2503 }
2504
2505 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2506 conn->flags |= CO_FL_ERROR;
2507 chk_report_conn_err(conn, 0, 0);
2508 goto out_end_tcpcheck;
2509 }
2510
Willy Tarreauabca5b62013-12-06 14:19:25 +01002511 if (check->current_step->string_len >= check->bo->size) {
2512 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2513 check->current_step->string_len, check->bo->size,
2514 tcpcheck_get_step_id(s));
2515 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2516 goto out_end_tcpcheck;
2517 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002518
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002519 /* do not try to send if there is no space */
2520 if (check->current_step->string_len >= buffer_total_space(check->bo))
2521 continue;
2522
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002523 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2524 *check->bo->p = '\0'; /* to make gdb output easier to read */
2525
Willy Tarreauabca5b62013-12-06 14:19:25 +01002526 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002527 cur = (struct tcpcheck_rule *)cur->list.n;
2528 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002529 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002530 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002531 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002532 goto out_end_tcpcheck;
2533
Willy Tarreau310987a2014-01-22 19:46:33 +01002534 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002535 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2536 done = 1;
2537 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2538 /* Report network errors only if we got no other data. Otherwise
2539 * we'll let the upper layers decide whether the response is OK
2540 * or not. It is very common that an RST sent by the server is
2541 * reported as an error just after the last data chunk.
2542 */
2543 chk_report_conn_err(conn, errno, 0);
2544 goto out_end_tcpcheck;
2545 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002546 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002547 else
2548 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002549 }
2550
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002551 /* mark the step as started */
2552 check->last_started_step = check->current_step;
2553
2554
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002555 /* Intermediate or complete response received.
2556 * Terminate string in check->bi->data buffer.
2557 */
2558 if (check->bi->i < check->bi->size) {
2559 check->bi->data[check->bi->i] = '\0';
2560 }
2561 else {
2562 check->bi->data[check->bi->i - 1] = '\0';
2563 done = 1; /* buffer full, don't wait for more data */
2564 }
2565
2566 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002567
2568 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002569 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002570 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002571 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002572
2573 /* empty response */
2574 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2575 tcpcheck_get_step_id(s));
2576 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2577
2578 goto out_end_tcpcheck;
2579 }
2580
2581 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002582 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002583
Willy Tarreaua970c282013-12-06 12:47:19 +01002584 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002585 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002586 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002587 else if (cur->expect_regex != NULL)
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002588 ret = regex_exec(cur->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002589
2590 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002591 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002592
2593 /* matched */
2594 if (ret) {
2595 /* matched but we did not want to => ERROR */
2596 if (cur->inverse) {
2597 /* we were looking for a string */
2598 if (cur->string != NULL) {
2599 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2600 cur->string, tcpcheck_get_step_id(s));
2601 }
2602 else {
2603 /* we were looking for a regex */
2604 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2605 tcpcheck_get_step_id(s));
2606 }
2607 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2608 goto out_end_tcpcheck;
2609 }
2610 /* matched and was supposed to => OK, next step */
2611 else {
2612 cur = (struct tcpcheck_rule*)cur->list.n;
2613 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002614 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002615 goto tcpcheck_expect;
2616 __conn_data_stop_recv(conn);
2617 }
2618 }
2619 else {
2620 /* not matched */
2621 /* not matched and was not supposed to => OK, next step */
2622 if (cur->inverse) {
2623 cur = (struct tcpcheck_rule*)cur->list.n;
2624 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002625 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002626 goto tcpcheck_expect;
2627 __conn_data_stop_recv(conn);
2628 }
2629 /* not matched but was supposed to => ERROR */
2630 else {
2631 /* we were looking for a string */
2632 if (cur->string != NULL) {
2633 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2634 cur->string, tcpcheck_get_step_id(s));
2635 }
2636 else {
2637 /* we were looking for a regex */
2638 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2639 tcpcheck_get_step_id(s));
2640 }
2641 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2642 goto out_end_tcpcheck;
2643 }
2644 }
2645 } /* end expect */
2646 } /* end loop over double chained step list */
2647
2648 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2649 goto out_end_tcpcheck;
2650
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002651 out_need_io:
2652 if (check->bo->o)
2653 __conn_data_want_send(conn);
2654
2655 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2656 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002657 return;
2658
2659 out_end_tcpcheck:
2660 /* collect possible new errors */
2661 if (conn->flags & CO_FL_ERROR)
2662 chk_report_conn_err(conn, 0, 0);
2663
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002664 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002665 check->current_step = NULL;
2666
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002667 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002668 conn->flags |= CO_FL_ERROR;
2669
2670 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002671
2672 return;
2673}
2674
2675
Willy Tarreaubd741542010-03-16 18:46:54 +01002676/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002677 * Local variables:
2678 * c-indent-level: 8
2679 * c-basic-offset: 8
2680 * End:
2681 */