blob: cf1c59e4c0d767c7b41e40429b3ad4b745eaa940 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
Willy Tarreau9b39dc52014-07-08 00:54:10 +020018#include <signal.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090019#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020023#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <unistd.h>
25#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090027#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020029#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <arpa/inet.h>
31
Willy Tarreauc7e42382012-08-24 19:22:53 +020032#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/compat.h>
34#include <common/config.h>
35#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020036#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
Christopher Fauletcfda8472017-10-20 15:40:23 +020038#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
40#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020041#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
44#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <proto/stats.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 Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/server.h>
Willy Tarreau48d6bf22016-06-21 16:27:34 +020056#include <proto/signal.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010057#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020059#include <proto/log.h>
60#include <proto/dns.h>
61#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Olivier Houchard9130a962017-10-17 17:33:43 +020063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif /* USE_OPENSSL */
66
Willy Tarreaubd741542010-03-16 18:46:54 +010067static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090068static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020069static char * tcpcheck_get_step_comment(struct check *, int);
Willy Tarreau6bdcab02017-10-04 18:41:00 +020070static int tcpcheck_main(struct check *);
Willy Tarreaubd741542010-03-16 18:46:54 +010071
Christopher Faulet31dff9b2017-10-23 15:45:20 +020072static struct pool_head *pool2_email_alert = NULL;
73static struct pool_head *pool2_tcpcheck_rule = NULL;
74
75
Simon Horman63a4a822012-03-19 07:24:41 +090076static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010077 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
78 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020079 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020080
Willy Tarreau23964182014-05-20 20:56:30 +020081 /* Below we have finished checks */
82 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010084
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010085 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
88 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
89 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020090
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010091 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
92 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
93 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020094
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010095 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
96 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020097
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020098 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100100 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
101 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
102 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900103
104 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
105 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200106 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200107};
108
Cyril Bontéac92a062014-12-27 22:28:38 +0100109const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
110 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
116 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
117 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
118 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
119 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
120 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
121};
122
Simon Horman63a4a822012-03-19 07:24:41 +0900123static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100124 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
125
126 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
127 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
128
129 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
130 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
131 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
132 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
133
134 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
135 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
136 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
137};
138
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200139/*
140 * Convert check_status code to description
141 */
142const char *get_check_status_description(short check_status) {
143
144 const char *desc;
145
146 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200147 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200148 else
149 desc = NULL;
150
151 if (desc && *desc)
152 return desc;
153 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200154 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200155}
156
157/*
158 * Convert check_status code to short info
159 */
160const char *get_check_status_info(short check_status) {
161
162 const char *info;
163
164 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200165 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200166 else
167 info = NULL;
168
169 if (info && *info)
170 return info;
171 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200172 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200173}
174
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100175const char *get_analyze_status(short analyze_status) {
176
177 const char *desc;
178
179 if (analyze_status < HANA_STATUS_SIZE)
180 desc = analyze_statuses[analyze_status].desc;
181 else
182 desc = NULL;
183
184 if (desc && *desc)
185 return desc;
186 else
187 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
188}
189
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200190/*
Simon Horman4a741432013-02-23 15:35:38 +0900191 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200192 * an adequate CHK_RES_* value. The new check->health is computed based
193 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200194 *
195 * Show information in logs about failed health check if server is UP
196 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200197 */
Simon Horman4a741432013-02-23 15:35:38 +0900198static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100199{
Simon Horman4a741432013-02-23 15:35:38 +0900200 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200201 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200202 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900203
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200204 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100205 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900206 check->desc[0] = '\0';
207 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200208 return;
209 }
210
Simon Horman4a741432013-02-23 15:35:38 +0900211 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200212 return;
213
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200214 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900215 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
216 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200217 } else
Simon Horman4a741432013-02-23 15:35:38 +0900218 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200219
Simon Horman4a741432013-02-23 15:35:38 +0900220 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200221 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900222 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200223
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100224 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900225 check->duration = -1;
226 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200227 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900228 check->duration = tv_ms_elapsed(&check->start, &now);
229 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200230 }
231
Willy Tarreau23964182014-05-20 20:56:30 +0200232 /* no change is expected if no state change occurred */
233 if (check->result == CHK_RES_NEUTRAL)
234 return;
235
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200236 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200238 switch (check->result) {
239 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200240 /* Failure to connect to the agent as a secondary check should not
241 * cause the server to be marked down.
242 */
243 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900244 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200245 (check->health >= check->rise)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +0200246 HA_ATOMIC_ADD(&s->counters.failed_checks, 1);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200247 report = 1;
248 check->health--;
249 if (check->health < check->rise)
250 check->health = 0;
251 }
252 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200253
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200254 case CHK_RES_PASSED:
255 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
256 if ((check->health < check->rise + check->fall - 1) &&
257 (check->result == CHK_RES_PASSED || check->health > 0)) {
258 report = 1;
259 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200260
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200261 if (check->health >= check->rise)
262 check->health = check->rise + check->fall - 1; /* OK now */
263 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200264
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200265 /* clear consecutive_errors if observing is enabled */
266 if (s->onerror)
267 s->consecutive_errors = 0;
268 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100269
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200270 default:
271 break;
272 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200273
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200274 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
275 (status != prev_status || report)) {
276 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200277 "%s check for %sserver %s/%s %s%s",
278 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200279 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100280 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100281 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200282 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200283
Emeric Brun5a133512017-10-19 14:42:30 +0200284 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100286 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200287 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
288 (check->health >= check->rise) ? check->fall : check->rise,
289 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200290
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100291 Warning("%s.\n", trash.str);
292 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900293 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200294 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200295}
296
Willy Tarreau4eec5472014-05-20 22:32:27 +0200297/* Marks the check <check>'s server down if the current check is already failed
298 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200300static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301{
Simon Horman4a741432013-02-23 15:35:38 +0900302 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900303
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200304 /* The agent secondary check should only cause a server to be marked
305 * as down if check->status is HCHK_STATUS_L7STS, which indicates
306 * that the agent returned "fail", "stopped" or "down".
307 * The implication here is that failure to connect to the agent
308 * as a secondary check should not cause the server to be marked
309 * down. */
310 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
311 return;
312
Willy Tarreau4eec5472014-05-20 22:32:27 +0200313 if (check->health > 0)
314 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100315
Willy Tarreau4eec5472014-05-20 22:32:27 +0200316 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200317 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318}
319
Willy Tarreauaf549582014-05-16 17:37:50 +0200320/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200321 * it isn't in maintenance, it is not tracking a down server and other checks
322 * comply. The rule is simple : by default, a server is up, unless any of the
323 * following conditions is true :
324 * - health check failed (check->health < rise)
325 * - agent check failed (agent->health < rise)
326 * - the server tracks a down server (track && track->state == STOPPED)
327 * Note that if the server has a slowstart, it will switch to STARTING instead
328 * of RUNNING. Also, only the health checks support the nolb mode, so the
329 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200330 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200331static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200332{
Simon Horman4a741432013-02-23 15:35:38 +0900333 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100334
Emeric Brun52a91d32017-08-31 14:41:55 +0200335 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200336 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100337
Emeric Brun52a91d32017-08-31 14:41:55 +0200338 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200339 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100340
Willy Tarreau3e048382014-05-21 10:30:54 +0200341 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
342 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100343
Willy Tarreau3e048382014-05-21 10:30:54 +0200344 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
345 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200346
Emeric Brun52a91d32017-08-31 14:41:55 +0200347 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200348 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100349
Emeric Brun5a133512017-10-19 14:42:30 +0200350 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100351}
352
Willy Tarreaudb58b792014-05-21 13:57:23 +0200353/* Marks the check <check> as valid and tries to set its server into stopping mode
354 * if it was running or starting, and provided it isn't in maintenance and other
355 * checks comply. The conditions for the server to be marked in stopping mode are
356 * the same as for it to be turned up. Also, only the health checks support the
357 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200358 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200359static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200360{
Simon Horman4a741432013-02-23 15:35:38 +0900361 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100362
Emeric Brun52a91d32017-08-31 14:41:55 +0200363 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200364 return;
365
Willy Tarreaudb58b792014-05-21 13:57:23 +0200366 if (check->state & CHK_ST_AGENT)
367 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100368
Emeric Brun52a91d32017-08-31 14:41:55 +0200369 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200370 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100371
Willy Tarreaudb58b792014-05-21 13:57:23 +0200372 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
373 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100374
Willy Tarreaudb58b792014-05-21 13:57:23 +0200375 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
376 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100377
Emeric Brun5a133512017-10-19 14:42:30 +0200378 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100379}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200380
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100381/* note: use health_adjust() only, which first checks that the observe mode is
382 * enabled.
383 */
384void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100385{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100386 int failed;
387 int expire;
388
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100389 if (s->observe >= HANA_OBS_SIZE)
390 return;
391
Willy Tarreaubb956662013-01-24 00:37:39 +0100392 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100393 return;
394
395 switch (analyze_statuses[status].lr[s->observe - 1]) {
396 case 1:
397 failed = 1;
398 break;
399
400 case 2:
401 failed = 0;
402 break;
403
404 default:
405 return;
406 }
407
408 if (!failed) {
409 /* good: clear consecutive_errors */
410 s->consecutive_errors = 0;
411 return;
412 }
413
Christopher Faulet29f77e82017-06-08 14:04:45 +0200414 HA_ATOMIC_ADD(&s->consecutive_errors, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100415
416 if (s->consecutive_errors < s->consecutive_errors_limit)
417 return;
418
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100419 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
420 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100421
422 switch (s->onerror) {
423 case HANA_ONERR_FASTINTER:
424 /* force fastinter - nothing to do here as all modes force it */
425 break;
426
427 case HANA_ONERR_SUDDTH:
428 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900429 if (s->check.health > s->check.rise)
430 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100431
432 /* no break - fall through */
433
434 case HANA_ONERR_FAILCHK:
435 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900436 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200437 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100438 break;
439
440 case HANA_ONERR_MARKDWN:
441 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900442 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900443 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200444 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100445 break;
446
447 default:
448 /* write a warning? */
449 break;
450 }
451
452 s->consecutive_errors = 0;
Christopher Faulet29f77e82017-06-08 14:04:45 +0200453 HA_ATOMIC_ADD(&s->counters.failed_hana, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100454
Simon Horman66183002013-02-23 10:16:43 +0900455 if (s->check.fastinter) {
456 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300457 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200458 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300459 /* requeue check task with new expire */
460 task_queue(s->check.task);
461 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100462 }
463}
464
Willy Tarreaua1dab552014-04-14 15:04:54 +0200465static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100466{
467 int sv_state;
468 int ratio;
469 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800470 char addr[46];
471 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100472 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
473 "UP %d/%d", "UP",
474 "NOLB %d/%d", "NOLB",
475 "no check" };
476
477 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
478 hlen += 24;
479
Willy Tarreauff5ae352013-12-11 20:36:34 +0100480 if (!(s->check.state & CHK_ST_ENABLED))
481 sv_state = 6;
Emeric Brun52a91d32017-08-31 14:41:55 +0200482 else if (s->cur_state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900483 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100484 sv_state = 3; /* UP */
485 else
486 sv_state = 2; /* going down */
487
Emeric Brun52a91d32017-08-31 14:41:55 +0200488 if (s->cur_state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100489 sv_state += 2;
490 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900491 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100492 sv_state = 1; /* going up */
493 else
494 sv_state = 0; /* DOWN */
495 }
496
Willy Tarreaua1dab552014-04-14 15:04:54 +0200497 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100498 srv_hlt_st[sv_state],
Emeric Brun52a91d32017-08-31 14:41:55 +0200499 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
500 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100501
Joseph Lynch514061c2015-01-15 17:52:59 -0800502 addr_to_str(&s->addr, addr, sizeof(addr));
Willy Tarreau04276f32017-01-06 17:41:29 +0100503 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
504 snprintf(port, sizeof(port), "%u", s->svc_port);
505 else
506 *port = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800507
508 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
509 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100510 global.node,
Emeric Brun52a91d32017-08-31 14:41:55 +0200511 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreauef781042010-01-27 11:53:01 +0100512 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
513 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
514 s->nbpend);
515
Emeric Brun52a91d32017-08-31 14:41:55 +0200516 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100517 now.tv_sec < s->last_change + s->slowstart &&
518 now.tv_sec >= s->last_change) {
519 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200520 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100521 }
522
523 buffer[hlen++] = '\r';
524 buffer[hlen++] = '\n';
525
526 return hlen;
527}
528
Willy Tarreau20a18342013-12-05 00:31:46 +0100529/* Check the connection. If an error has already been reported or the socket is
530 * closed, keep errno intact as it is supposed to contain the valid error code.
531 * If no error is reported, check the socket's error queue using getsockopt().
532 * Warning, this must be done only once when returning from poll, and never
533 * after an I/O error was attempted, otherwise the error queue might contain
534 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
535 * socket. Returns non-zero if an error was reported, zero if everything is
536 * clean (including a properly closed socket).
537 */
538static int retrieve_errno_from_socket(struct connection *conn)
539{
540 int skerr;
541 socklen_t lskerr = sizeof(skerr);
542
543 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
544 return 1;
545
Willy Tarreau3c728722014-01-23 13:50:42 +0100546 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100547 return 0;
548
Willy Tarreau585744b2017-08-24 14:31:19 +0200549 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100550 errno = skerr;
551
552 if (errno == EAGAIN)
553 errno = 0;
554
555 if (!errno) {
556 /* we could not retrieve an error, that does not mean there is
557 * none. Just don't change anything and only report the prior
558 * error if any.
559 */
560 if (conn->flags & CO_FL_ERROR)
561 return 1;
562 else
563 return 0;
564 }
565
566 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
567 return 1;
568}
569
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100570/* Try to collect as much information as possible on the connection status,
571 * and adjust the server status accordingly. It may make use of <errno_bck>
572 * if non-null when the caller is absolutely certain of its validity (eg:
573 * checked just after a syscall). If the caller doesn't have a valid errno,
574 * it can pass zero, and retrieve_errno_from_socket() will be called to try
575 * to extract errno from the socket. If no error is reported, it will consider
576 * the <expired> flag. This is intended to be used when a connection error was
577 * reported in conn->flags or when a timeout was reported in <expired>. The
578 * function takes care of not updating a server status which was already set.
579 * All situations where at least one of <expired> or CO_FL_ERROR are set
580 * produce a status.
581 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200582static void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100583{
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200584 struct connection *conn = check->conn;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100585 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200586 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200587 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200588 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100589
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100590 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100591 return;
592
593 errno = errno_bck;
Willy Tarreau00149122017-10-04 18:05:01 +0200594 if (conn && (!errno || errno == EAGAIN))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100595 retrieve_errno_from_socket(conn);
596
Willy Tarreau00149122017-10-04 18:05:01 +0200597 if (conn && !(conn->flags & CO_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100598 return;
599
600 /* we'll try to build a meaningful error message depending on the
601 * context of the error possibly present in conn->err_code, and the
602 * socket error possibly collected above. This is useful to know the
603 * exact step of the L6 layer (eg: SSL handshake).
604 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200605 chk = get_trash_chunk();
606
607 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900608 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200609 if (!step)
610 chunk_printf(chk, " at initial connection step of tcp-check");
611 else {
612 chunk_printf(chk, " at step %d of tcp-check", step);
613 /* we were looking for a string */
614 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
615 if (check->last_started_step->port)
616 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
617 else
618 chunk_appendf(chk, " (connect)");
619 }
620 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
621 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200622 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200623 else if (check->last_started_step->expect_regex)
624 chunk_appendf(chk, " (expect regex)");
625 }
626 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
627 chunk_appendf(chk, " (send)");
628 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200629
630 comment = tcpcheck_get_step_comment(check, step);
631 if (comment)
632 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200633 }
634 }
635
Willy Tarreau00149122017-10-04 18:05:01 +0200636 if (conn && conn->err_code) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100637 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200638 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100639 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200640 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100641 err_msg = trash.str;
642 }
643 else {
644 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200645 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100646 err_msg = trash.str;
647 }
648 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200649 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100650 }
651 }
652
Willy Tarreau00149122017-10-04 18:05:01 +0200653 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200654 /* NOTE: this is reported after <fall> tries */
655 chunk_printf(chk, "No port available for the TCP connection");
656 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
657 }
658
Willy Tarreau00149122017-10-04 18:05:01 +0200659 if (!conn) {
660 /* connection allocation error before the connection was established */
661 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
662 }
663 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100664 /* L4 not established (yet) */
665 if (conn->flags & CO_FL_ERROR)
666 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
667 else if (expired)
668 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200669
670 /*
671 * might be due to a server IP change.
672 * Let's trigger a DNS resolution if none are currently running.
673 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200674 dns_trigger_resolution(check->server->dns_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200675
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100676 }
677 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
678 /* L6 not established (yet) */
679 if (conn->flags & CO_FL_ERROR)
680 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
681 else if (expired)
682 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
683 }
684 else if (conn->flags & CO_FL_ERROR) {
685 /* I/O error after connection was established and before we could diagnose */
686 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
687 }
688 else if (expired) {
689 /* connection established but expired check */
690 if (check->type == PR_O2_SSL3_CHK)
691 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
692 else /* HTTP, SMTP, ... */
693 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
694 }
695
696 return;
697}
698
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699/*
700 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200701 * the connection acknowledgement. If the proxy requires L7 health-checks,
702 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900703 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200704 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200705static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706{
Simon Horman4a741432013-02-23 15:35:38 +0900707 struct check *check = conn->owner;
708 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900709 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200710
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100711 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100712 goto out_wakeup;
713
Willy Tarreau310987a2014-01-22 19:46:33 +0100714 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100715 return;
716
Willy Tarreau20a18342013-12-05 00:31:46 +0100717 if (retrieve_errno_from_socket(conn)) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200718 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200719 __conn_xprt_stop_both(conn);
Willy Tarreau20a18342013-12-05 00:31:46 +0100720 goto out_wakeup;
721 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100722
Willy Tarreaubbae3f02017-08-30 09:59:52 +0200723 if (conn->flags & CO_FL_SOCK_WR_SH) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100724 /* if the output is closed, we can't do anything */
725 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200726 chk_report_conn_err(check, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100727 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200728 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200729
Willy Tarreau06559ac2013-12-05 01:53:08 +0100730 /* here, we know that the connection is established. That's enough for
731 * a pure TCP check.
732 */
733 if (!check->type)
734 goto out_wakeup;
735
Willy Tarreauc09572f2017-10-04 11:58:22 +0200736 /* wake() will take care of calling tcpcheck_main() */
737 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200738 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200739
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100740 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100741 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100742 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200743 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200744 __conn_xprt_stop_both(conn);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100745 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100747 if (check->bo->o)
748 return;
749 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200750
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100751 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
752 if (s->proxy->timeout.check) {
753 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
754 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100756 goto out_nowake;
757
Willy Tarreau83749182007-04-15 20:56:27 +0200758 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200759 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200760 out_nowake:
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200761 __conn_xprt_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762}
763
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200765 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200766 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900767 * set_server_check_status() to update check->status, check->duration
768 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200769
770 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
771 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
772 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
773 * response to an SSL HELLO (the principle is that this is enough to
774 * distinguish between an SSL server and a pure TCP relay). All other cases will
775 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
776 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200778static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779{
Simon Horman4a741432013-02-23 15:35:38 +0900780 struct check *check = conn->owner;
781 struct server *s = check->server;
782 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200783 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100784 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200785 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200786
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100787 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200788 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200789
Willy Tarreau310987a2014-01-22 19:46:33 +0100790 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200791 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200792
Willy Tarreauc09572f2017-10-04 11:58:22 +0200793 /* wake() will take care of calling tcpcheck_main() */
794 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200795 return;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200796
Willy Tarreau83749182007-04-15 20:56:27 +0200797 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
798 * but the connection was closed on the remote end. Fortunately, recv still
799 * works correctly and we don't need to do the getsockopt() on linux.
800 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000801
802 /* Set buffer to point to the end of the data already read, and check
803 * that there is free space remaining. If the buffer is full, proceed
804 * with running the checks without attempting another socket read.
805 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000806
Willy Tarreau03938182010-03-17 21:52:07 +0100807 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000808
Simon Horman4a741432013-02-23 15:35:38 +0900809 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreau54e917c2017-08-30 07:35:35 +0200810 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100811 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900812 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200813 /* Report network errors only if we got no other data. Otherwise
814 * we'll let the upper layers decide whether the response is OK
815 * or not. It is very common that an RST sent by the server is
816 * reported as an error just after the last data chunk.
817 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200818 chk_report_conn_err(check, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100819 goto out_wakeup;
820 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200821 }
822
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100823
Willy Tarreau03938182010-03-17 21:52:07 +0100824 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900825 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100826 */
Simon Horman4a741432013-02-23 15:35:38 +0900827 if (check->bi->i < check->bi->size)
828 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100829 else {
Simon Horman4a741432013-02-23 15:35:38 +0900830 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100831 done = 1; /* buffer full, don't wait for more data */
832 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200833
Nick Chalk57b1bf72010-03-16 15:50:46 +0000834 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900835 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200836 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900837 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100838 goto wait_more_data;
839
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100840 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900841 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
842 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
843 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
844 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
845 !isdigit((unsigned char) *(check->bi->data + 11))) {
846 cut_crlf(check->bi->data);
847 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200848
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100849 goto out_wakeup;
850 }
851
Simon Horman4a741432013-02-23 15:35:38 +0900852 check->code = str2uic(check->bi->data + 9);
853 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000854
Willy Tarreaubd741542010-03-16 18:46:54 +0100855 if ((s->proxy->options & PR_O_DISABLE404) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200856 (s->next_state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000857 /* 404 may be accepted as "stopping" only if the server was up */
858 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900859 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000860 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100861 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
862 /* Run content verification check... We know we have at least 13 chars */
863 if (!httpchk_expect(s, done))
864 goto wait_more_data;
865 }
866 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900867 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100868 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900869 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100870 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000871 else {
872 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900873 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000874 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200875 break;
876
877 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900878 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100879 goto wait_more_data;
880
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100881 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900882 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
883 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200884 else
Simon Horman4a741432013-02-23 15:35:38 +0900885 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200886 break;
887
888 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900889 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100890 goto wait_more_data;
891
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200892 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900893 if ((check->bi->i < strlen("000\r")) ||
894 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
895 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
896 !isdigit((unsigned char) *(check->bi->data + 2))) {
897 cut_crlf(check->bi->data);
898 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200899
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200900 goto out_wakeup;
901 }
902
Simon Horman4a741432013-02-23 15:35:38 +0900903 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200904
Simon Horman4a741432013-02-23 15:35:38 +0900905 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200906 cut_crlf(desc);
907
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100908 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900909 if (*check->bi->data == '2')
910 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200911 else
Simon Horman4a741432013-02-23 15:35:38 +0900912 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200913 break;
914
Simon Hormana2b9dad2013-02-12 10:45:54 +0900915 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100916 int status = HCHK_STATUS_CHECKED;
917 const char *hs = NULL; /* health status */
918 const char *as = NULL; /* admin status */
919 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200920 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100921 const char *err = NULL; /* first error to report */
922 const char *wrn = NULL; /* first warning to report */
923 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900924
Willy Tarreau81f5d942013-12-09 20:51:51 +0100925 /* We're getting an agent check response. The agent could
926 * have been disabled in the mean time with a long check
927 * still pending. It is important that we ignore the whole
928 * response.
929 */
930 if (!(check->server->agent.state & CHK_ST_ENABLED))
931 break;
932
933 /* The agent supports strings made of a single line ended by the
934 * first CR ('\r') or LF ('\n'). This line is composed of words
935 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
936 * line may optionally contained a description of a state change
937 * after a sharp ('#'), which is only considered if a health state
938 * is announced.
939 *
940 * Words may be composed of :
941 * - a numeric weight suffixed by the percent character ('%').
942 * - a health status among "up", "down", "stopped", and "fail".
943 * - an admin status among "ready", "drain", "maint".
944 *
945 * These words may appear in any order. If multiple words of the
946 * same category appear, the last one wins.
947 */
948
Willy Tarreau9809b782013-12-11 21:40:11 +0100949 p = check->bi->data;
950 while (*p && *p != '\n' && *p != '\r')
951 p++;
952
953 if (!*p) {
954 if (!done)
955 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900956
Willy Tarreau9809b782013-12-11 21:40:11 +0100957 /* at least inform the admin that the agent is mis-behaving */
958 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
959 break;
960 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100961
Willy Tarreau9809b782013-12-11 21:40:11 +0100962 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100963 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900964
Willy Tarreau81f5d942013-12-09 20:51:51 +0100965 while (*cmd) {
966 /* look for next word */
967 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
968 cmd++;
969 continue;
970 }
Simon Horman671b6f02013-11-25 10:46:39 +0900971
Willy Tarreau81f5d942013-12-09 20:51:51 +0100972 if (*cmd == '#') {
973 /* this is the beginning of a health status description,
974 * skip the sharp and blanks.
975 */
976 cmd++;
977 while (*cmd == '\t' || *cmd == ' ')
978 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900979 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900980 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100981
982 /* find the end of the word so that we have a null-terminated
983 * word between <cmd> and <p>.
984 */
985 p = cmd + 1;
986 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
987 p++;
988 if (*p)
989 *p++ = 0;
990
991 /* first, health statuses */
992 if (strcasecmp(cmd, "up") == 0) {
993 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900994 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100995 hs = cmd;
996 }
997 else if (strcasecmp(cmd, "down") == 0) {
998 check->health = 0;
999 status = HCHK_STATUS_L7STS;
1000 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001001 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001002 else if (strcasecmp(cmd, "stopped") == 0) {
1003 check->health = 0;
1004 status = HCHK_STATUS_L7STS;
1005 hs = cmd;
1006 }
1007 else if (strcasecmp(cmd, "fail") == 0) {
1008 check->health = 0;
1009 status = HCHK_STATUS_L7STS;
1010 hs = cmd;
1011 }
1012 /* admin statuses */
1013 else if (strcasecmp(cmd, "ready") == 0) {
1014 as = cmd;
1015 }
1016 else if (strcasecmp(cmd, "drain") == 0) {
1017 as = cmd;
1018 }
1019 else if (strcasecmp(cmd, "maint") == 0) {
1020 as = cmd;
1021 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001022 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001023 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1024 ps = cmd;
1025 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001026 /* try to parse a maxconn here */
1027 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1028 cs = cmd;
1029 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001030 else {
1031 /* keep a copy of the first error */
1032 if (!err)
1033 err = cmd;
1034 }
1035 /* skip to next word */
1036 cmd = p;
1037 }
1038 /* here, cmd points either to \0 or to the beginning of a
1039 * description. Skip possible leading spaces.
1040 */
1041 while (*cmd == ' ' || *cmd == '\n')
1042 cmd++;
1043
1044 /* First, update the admin status so that we avoid sending other
1045 * possibly useless warnings and can also update the health if
1046 * present after going back up.
1047 */
1048 if (as) {
1049 if (strcasecmp(as, "drain") == 0)
1050 srv_adm_set_drain(check->server);
1051 else if (strcasecmp(as, "maint") == 0)
1052 srv_adm_set_maint(check->server);
1053 else
1054 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001055 }
1056
Willy Tarreau81f5d942013-12-09 20:51:51 +01001057 /* now change weights */
1058 if (ps) {
1059 const char *msg;
1060
1061 msg = server_parse_weight_change_request(s, ps);
1062 if (!wrn || !*wrn)
1063 wrn = msg;
1064 }
1065
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001066 if (cs) {
1067 const char *msg;
1068
1069 cs += strlen("maxconn:");
1070
1071 msg = server_parse_maxconn_change_request(s, cs);
1072 if (!wrn || !*wrn)
1073 wrn = msg;
1074 }
1075
Willy Tarreau81f5d942013-12-09 20:51:51 +01001076 /* and finally health status */
1077 if (hs) {
1078 /* We'll report some of the warnings and errors we have
1079 * here. Down reports are critical, we leave them untouched.
1080 * Lack of report, or report of 'UP' leaves the room for
1081 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001082 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001083 const char *msg = cmd;
1084 struct chunk *t;
1085
1086 if (!*msg || status == HCHK_STATUS_L7OKD) {
1087 if (err && *err)
1088 msg = err;
1089 else if (wrn && *wrn)
1090 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001091 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001092
1093 t = get_trash_chunk();
1094 chunk_printf(t, "via agent : %s%s%s%s",
1095 hs, *msg ? " (" : "",
1096 msg, *msg ? ")" : "");
1097
1098 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001099 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001100 else if (err && *err) {
1101 /* No status change but we'd like to report something odd.
1102 * Just report the current state and copy the message.
1103 */
1104 chunk_printf(&trash, "agent reports an error : %s", err);
1105 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001106
Willy Tarreau81f5d942013-12-09 20:51:51 +01001107 }
1108 else if (wrn && *wrn) {
1109 /* No status change but we'd like to report something odd.
1110 * Just report the current state and copy the message.
1111 */
1112 chunk_printf(&trash, "agent warns : %s", wrn);
1113 set_server_check_status(check, status/*check->status*/, trash.str);
1114 }
1115 else
1116 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001117 break;
1118 }
1119
Willy Tarreau1620ec32011-08-06 17:05:02 +02001120 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001121 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001122 goto wait_more_data;
1123
Simon Horman4a741432013-02-23 15:35:38 +09001124 if (check->bi->data[0] == 'R') {
1125 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001126 }
1127 else {
Simon Horman4a741432013-02-23 15:35:38 +09001128 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1129 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001130 else
1131 desc = "PostgreSQL unknown error";
1132
Simon Horman4a741432013-02-23 15:35:38 +09001133 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001134 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001135 break;
1136
1137 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001138 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001139 goto wait_more_data;
1140
Simon Horman4a741432013-02-23 15:35:38 +09001141 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1142 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001143 }
1144 else {
Simon Horman4a741432013-02-23 15:35:38 +09001145 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001146 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001147 break;
1148
1149 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001150 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001151 goto wait_more_data;
1152
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001153 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001154 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001155 /* We set the MySQL Version in description for information purpose
1156 * FIXME : it can be cool to use MySQL Version for other purpose,
1157 * like mark as down old MySQL server.
1158 */
Simon Horman4a741432013-02-23 15:35:38 +09001159 if (check->bi->i > 51) {
1160 desc = ltrim(check->bi->data + 5, ' ');
1161 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001162 }
1163 else {
1164 if (!done)
1165 goto wait_more_data;
1166 /* it seems we have a OK packet but without a valid length,
1167 * it must be a protocol error
1168 */
Simon Horman4a741432013-02-23 15:35:38 +09001169 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001170 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001171 }
1172 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001173 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001174 desc = ltrim(check->bi->data + 7, ' ');
1175 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001176 }
1177 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001178 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1179 (((unsigned int) *(check->bi->data + 1)) << 8) +
1180 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001181
Simon Horman4a741432013-02-23 15:35:38 +09001182 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001183 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001184 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001185 /* We have only one MySQL packet and it is a Handshake Initialization packet
1186 * but we need to have a second packet to know if it is alright
1187 */
Simon Horman4a741432013-02-23 15:35:38 +09001188 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001189 goto wait_more_data;
1190 }
1191 else {
1192 /* We have only one packet and it is an Error packet,
1193 * an error message is attached, so we can display it
1194 */
Simon Horman4a741432013-02-23 15:35:38 +09001195 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001196 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001197 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001198 }
Simon Horman4a741432013-02-23 15:35:38 +09001199 } else if (check->bi->i > first_packet_len + 4) {
1200 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1201 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1202 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001203
Simon Horman4a741432013-02-23 15:35:38 +09001204 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001205 /* We have 2 packets and that's good */
1206 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001207 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001208 /* No error packet */
1209 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001210 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001211 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001212 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001213 }
1214 else {
1215 /* An error message is attached in the Error packet
1216 * so we can display it ! :)
1217 */
Simon Horman4a741432013-02-23 15:35:38 +09001218 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001219 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001220 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001221 }
1222 }
1223 }
1224 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001225 if (!done)
1226 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001227 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001228 * it must be a protocol error
1229 */
Simon Horman4a741432013-02-23 15:35:38 +09001230 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001231 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001232 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001233 }
1234 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001235 break;
1236
1237 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001238 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001239 goto wait_more_data;
1240
1241 /* Check if the server speaks LDAP (ASN.1/BER)
1242 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1243 * http://tools.ietf.org/html/rfc4511
1244 */
1245
1246 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1247 * LDAPMessage: 0x30: SEQUENCE
1248 */
Simon Horman4a741432013-02-23 15:35:38 +09001249 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1250 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001251 }
1252 else {
1253 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001254 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001255
1256 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1257 * messageID: 0x02 0x01 0x01: INTEGER 1
1258 * protocolOp: 0x61: bindResponse
1259 */
1260 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001261 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1262 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001263
1264 goto out_wakeup;
1265 }
1266
1267 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001268 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001269
1270 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1271 * ldapResult: 0x0a 0x01: ENUMERATION
1272 */
1273 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001274 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1275 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001276
1277 goto out_wakeup;
1278 }
1279
1280 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1281 * resultCode
1282 */
Simon Horman4a741432013-02-23 15:35:38 +09001283 check->code = *(check->bi->data + msglen + 9);
1284 if (check->code) {
1285 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 +02001286 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001287 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001288 }
1289 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001290 break;
1291
Christopher Fauletba7bc162016-11-07 21:07:38 +01001292 case PR_O2_SPOP_CHK: {
1293 unsigned int framesz;
1294 char err[HCHK_DESC_LEN];
1295
1296 if (!done && check->bi->i < 4)
1297 goto wait_more_data;
1298
1299 memcpy(&framesz, check->bi->data, 4);
1300 framesz = ntohl(framesz);
1301
1302 if (!done && check->bi->i < (4+framesz))
1303 goto wait_more_data;
1304
Christopher Faulet8ef75252017-02-20 22:56:03 +01001305 if (!spoe_handle_healthcheck_response(check->bi->data+4, framesz, err, HCHK_DESC_LEN-1))
Christopher Fauletba7bc162016-11-07 21:07:38 +01001306 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1307 else
1308 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1309 break;
1310 }
1311
Willy Tarreau1620ec32011-08-06 17:05:02 +02001312 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001313 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001314 break;
1315 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001316
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001317 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001318 /* collect possible new errors */
1319 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001320 chk_report_conn_err(check, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001321
Nick Chalk57b1bf72010-03-16 15:50:46 +00001322 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001323 *check->bi->data = '\0';
1324 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001325
Steven Davidovitz544d4812017-03-08 11:06:20 -08001326 /* Close the connection... We still attempt to nicely close if,
1327 * for instance, SSL needs to send a "close notify." Later, we perform
1328 * a hard close and reset the connection if some data are pending,
1329 * otherwise we end up with many TIME_WAITs and eat all the source port
1330 * range quickly. To avoid sending RSTs all the time, we first try to
1331 * drain pending data.
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001332 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001333 __conn_xprt_stop_both(conn);
1334 conn_xprt_shutw(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001335
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001336 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001337 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001338 conn->flags |= CO_FL_ERROR;
1339
Willy Tarreaufdccded2008-08-29 18:19:04 +02001340 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001341 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001342
1343 wait_more_data:
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001344 __conn_xprt_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001345}
1346
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001347/*
1348 * This function is used only for server health-checks. It handles connection
1349 * status updates including errors. If necessary, it wakes the check task up.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001350 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1351 * connection (eg: reconnect).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001352 */
1353static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001354{
Simon Horman4a741432013-02-23 15:35:38 +09001355 struct check *check = conn->owner;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001356 int ret = 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001357
Willy Tarreauc09572f2017-10-04 11:58:22 +02001358 /* we may have to make progress on the TCP checks */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001359 if (check->type == PR_O2_TCPCHK_CHK) {
1360 ret = tcpcheck_main(check);
Willy Tarreau00149122017-10-04 18:05:01 +02001361 conn = check->conn;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001362 }
Willy Tarreauc09572f2017-10-04 11:58:22 +02001363
Willy Tarreau6c560da2012-11-24 11:14:45 +01001364 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001365 /* We may get error reports bypassing the I/O handlers, typically
1366 * the case when sending a pure TCP check which fails, then the I/O
1367 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001368 * main processing task so let's simply wake it up. If we get here,
1369 * we expect errno to still be valid.
1370 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001371 chk_report_conn_err(check, errno, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001372
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001373 __conn_xprt_stop_both(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001374 task_wakeup(check->task, TASK_WOKEN_IO);
1375 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001376 else if (!(conn->flags & (CO_FL_XPRT_RD_ENA|CO_FL_XPRT_WR_ENA|CO_FL_HANDSHAKE))) {
Willy Tarreau3be293f2014-02-05 18:31:24 +01001377 /* we may get here if only a connection probe was required : we
1378 * don't have any data to send nor anything expected in response,
1379 * so the completion of the connection establishment is enough.
1380 */
1381 task_wakeup(check->task, TASK_WOKEN_IO);
1382 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001383
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001384 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001385 /* We're here because nobody wants to handle the error, so we
1386 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001387 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001388 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02001389 conn_full_close(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001390 ret = -1;
Willy Tarreau2d351b62013-12-05 02:36:25 +01001391 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001392
1393 /* if a connection got replaced, we must absolutely prevent the connection
1394 * handler from touching its fd, and perform the FD polling updates ourselves
1395 */
1396 if (ret < 0)
1397 conn_cond_update_polling(conn);
1398
1399 return ret;
Willy Tarreau20bea422012-07-06 12:00:49 +02001400}
1401
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001402struct data_cb check_conn_cb = {
1403 .recv = event_srv_chk_r,
1404 .send = event_srv_chk_w,
1405 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001406 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001407};
1408
Willy Tarreaubaaee002006-06-26 02:48:02 +02001409/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001410 * updates the server's weight during a warmup stage. Once the final weight is
1411 * reached, the task automatically stops. Note that any server status change
1412 * must have updated s->last_change accordingly.
1413 */
1414static struct task *server_warmup(struct task *t)
1415{
1416 struct server *s = t->context;
1417
1418 /* by default, plan on stopping the task */
1419 t->expire = TICK_ETERNITY;
Emeric Brun52a91d32017-08-31 14:41:55 +02001420 if ((s->next_admin & SRV_ADMF_MAINT) ||
1421 (s->next_state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001422 return t;
1423
Willy Tarreau892337c2014-05-13 23:41:20 +02001424 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001425 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001426
1427 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001428 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001429
1430 /* get back there in 1 second or 1/20th of the slowstart interval,
1431 * whichever is greater, resulting in small 5% steps.
1432 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001433 if (s->next_state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001434 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1435 return t;
1436}
1437
Willy Tarreau894c6422017-10-04 15:58:52 +02001438/* returns the first NON-COMMENT tcp-check rule from list <list> or NULL if
1439 * none was found.
1440 */
1441static struct tcpcheck_rule *get_first_tcpcheck_rule(struct list *list)
1442{
1443 struct tcpcheck_rule *r;
1444
1445 list_for_each_entry(r, list, list) {
1446 if (r->action != TCPCHK_ACT_COMMENT)
1447 return r;
1448 }
1449 return NULL;
1450}
1451
Willy Tarreau2e993902011-10-31 11:53:20 +01001452/*
Simon Horman98637e52014-06-20 12:30:16 +09001453 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001454 *
1455 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001456 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1457 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1458 * - SF_ERR_SRVTO if there are no more servers
1459 * - SF_ERR_SRVCL if the connection was refused by the server
1460 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1461 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1462 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001463 * - SF_ERR_CHK_PORT if no port could be found to run a health check on an AF_INET* socket
Tim Düsterhus4896c442016-11-29 02:15:19 +01001464 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001465 * Note that we try to prevent the network stack from sending the ACK during the
1466 * connect() when a pure TCP check is used (without PROXY protocol).
1467 */
Simon Horman98637e52014-06-20 12:30:16 +09001468static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001469{
1470 struct check *check = t->context;
1471 struct server *s = check->server;
1472 struct connection *conn = check->conn;
1473 struct protocol *proto;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001474 struct tcpcheck_rule *tcp_rule = NULL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001475 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001476 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001477
Willy Tarreau00149122017-10-04 18:05:01 +02001478 /* we cannot have a connection here */
1479 if (conn)
1480 return SF_ERR_INTERNAL;
1481
Simon Hormanb00d17a2014-06-13 16:18:16 +09001482 /* tcpcheck send/expect initialisation */
Willy Tarreauf411cce2017-10-04 16:21:19 +02001483 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001484 check->current_step = NULL;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001485 tcp_rule = get_first_tcpcheck_rule(check->tcpcheck_rules);
1486 }
Simon Hormanb00d17a2014-06-13 16:18:16 +09001487
1488 /* prepare the check buffer.
1489 * This should not be used if check is the secondary agent check
1490 * of a server as s->proxy->check_req will relate to the
1491 * configuration of the primary check. Similarly, tcp-check uses
1492 * its own strings.
1493 */
1494 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1495 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1496
1497 /* we want to check if this host replies to HTTP or SSLv3 requests
1498 * so we'll send the request, and won't wake the checker up now.
1499 */
1500 if ((check->type) == PR_O2_SSL3_CHK) {
1501 /* SSL requires that we put Unix time in the request */
1502 int gmt_time = htonl(date.tv_sec);
1503 memcpy(check->bo->data + 11, &gmt_time, 4);
1504 }
1505 else if ((check->type) == PR_O2_HTTP_CHK) {
1506 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1507 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001508 /* prevent HTTP keep-alive when "http-check expect" is used */
1509 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1510 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001511 bo_putstr(check->bo, "\r\n");
1512 *check->bo->p = '\0'; /* to make gdb output easier to read */
1513 }
1514 }
1515
James Brown55f9ff12015-10-21 18:19:05 -07001516 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1517 bo_putblk(check->bo, check->send_string, check->send_string_len);
1518 }
1519
Willy Tarreauf411cce2017-10-04 16:21:19 +02001520 /* for tcp-checks, the initial connection setup is handled separately as
1521 * it may be sent to a specific port and not to the server's.
1522 */
1523 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_CONNECT) {
1524 tcpcheck_main(check);
1525 return SF_ERR_UP;
1526 }
1527
Simon Hormanb00d17a2014-06-13 16:18:16 +09001528 /* prepare a new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02001529 conn = check->conn = conn_new();
1530 if (!check->conn)
1531 return SF_ERR_RESOURCE;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001532
Simon Horman41f58762015-01-30 11:22:56 +09001533 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001534 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001535 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001536 }
1537 else {
1538 /* we'll connect to the addr on the server */
1539 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001540 }
1541
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001542 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1543 int i = 0;
1544
1545 i = srv_check_healthcheck_port(check);
1546 if (i == 0) {
1547 conn->owner = check;
1548 return SF_ERR_CHK_PORT;
1549 }
1550
1551 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001552 }
1553
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001554 proto = protocol_by_family(conn->addr.to.ss_family);
1555
1556 conn_prepare(conn, proto, check->xprt);
1557 conn_attach(conn, check, &check_conn_cb);
1558 conn->target = &s->obj_type;
1559
1560 /* no client address */
1561 clear_addr(&conn->addr.from);
1562
Willy Tarreauf3d34822014-12-08 12:11:28 +01001563 /* only plain tcp-check supports quick ACK */
1564 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1565
Willy Tarreauf411cce2017-10-04 16:21:19 +02001566 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_EXPECT)
1567 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001568
Willy Tarreaue7dff022015-04-03 01:14:29 +02001569 ret = SF_ERR_INTERNAL;
Olivier Houchardb68fda42017-08-04 18:39:01 +02001570 if (proto && proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001571 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Olivier Houchard9130a962017-10-17 17:33:43 +02001572#ifdef USE_OPENSSL
1573 if (s->check.sni)
1574 ssl_sock_set_servername(conn, s->check.sni);
1575#endif
Willy Tarreauf4949772017-05-06 08:45:28 +02001576 if (s->check.send_proxy && !(check->state & CHK_ST_AGENT)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001577 conn->send_proxy_ofs = 1;
1578 conn->flags |= CO_FL_SEND_PROXY;
1579 }
1580
1581 return ret;
1582}
1583
Simon Horman98637e52014-06-20 12:30:16 +09001584static struct list pid_list = LIST_HEAD_INIT(pid_list);
1585static struct pool_head *pool2_pid_list;
Christopher Fauletcfda8472017-10-20 15:40:23 +02001586#ifdef USE_THREAD
1587HA_SPINLOCK_T pid_list_lock;
1588#endif
Simon Horman98637e52014-06-20 12:30:16 +09001589
1590void block_sigchld(void)
1591{
1592 sigset_t set;
1593 sigemptyset(&set);
1594 sigaddset(&set, SIGCHLD);
Willy Tarreauebc92442016-06-21 17:29:46 +02001595 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001596}
1597
1598void unblock_sigchld(void)
1599{
1600 sigset_t set;
1601 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001602 sigaddset(&set, SIGCHLD);
1603 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001604}
1605
Simon Horman98637e52014-06-20 12:30:16 +09001606static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1607{
1608 struct pid_list *elem;
1609 struct check *check = t->context;
1610
1611 elem = pool_alloc2(pool2_pid_list);
1612 if (!elem)
1613 return NULL;
1614 elem->pid = pid;
1615 elem->t = t;
1616 elem->exited = 0;
1617 check->curpid = elem;
1618 LIST_INIT(&elem->list);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001619
1620 SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001621 LIST_ADD(&pid_list, &elem->list);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001622 SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
1623
Simon Horman98637e52014-06-20 12:30:16 +09001624 return elem;
1625}
1626
Simon Horman98637e52014-06-20 12:30:16 +09001627static void pid_list_del(struct pid_list *elem)
1628{
1629 struct check *check;
1630
1631 if (!elem)
1632 return;
1633
Christopher Fauletcfda8472017-10-20 15:40:23 +02001634 SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001635 LIST_DEL(&elem->list);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001636 SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
1637
Simon Horman98637e52014-06-20 12:30:16 +09001638 if (!elem->exited)
1639 kill(elem->pid, SIGTERM);
1640
1641 check = elem->t->context;
1642 check->curpid = NULL;
1643 pool_free2(pool2_pid_list, elem);
1644}
1645
1646/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1647static void pid_list_expire(pid_t pid, int status)
1648{
1649 struct pid_list *elem;
1650
Christopher Fauletcfda8472017-10-20 15:40:23 +02001651 SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001652 list_for_each_entry(elem, &pid_list, list) {
1653 if (elem->pid == pid) {
1654 elem->t->expire = now_ms;
1655 elem->status = status;
1656 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001657 task_wakeup(elem->t, TASK_WOKEN_IO);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001658 break;
Simon Horman98637e52014-06-20 12:30:16 +09001659 }
1660 }
Christopher Fauletcfda8472017-10-20 15:40:23 +02001661 SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001662}
1663
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001664static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001665{
1666 pid_t pid;
1667 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001668
Simon Horman98637e52014-06-20 12:30:16 +09001669 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1670 pid_list_expire(pid, status);
1671}
1672
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001673static int init_pid_list(void)
1674{
Simon Horman98637e52014-06-20 12:30:16 +09001675 if (pool2_pid_list != NULL)
1676 /* Nothing to do */
1677 return 0;
1678
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001679 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Simon Horman98637e52014-06-20 12:30:16 +09001680 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1681 strerror(errno));
1682 return 1;
1683 }
1684
1685 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1686 if (pool2_pid_list == NULL) {
1687 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1688 strerror(errno));
1689 return 1;
1690 }
1691
Christopher Fauletcfda8472017-10-20 15:40:23 +02001692 SPIN_INIT(&pid_list_lock);
1693
Simon Horman98637e52014-06-20 12:30:16 +09001694 return 0;
1695}
1696
Cyril Bontéac92a062014-12-27 22:28:38 +01001697/* helper macro to set an environment variable and jump to a specific label on failure. */
1698#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001699
1700/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001701 * helper function to allocate enough memory to store an environment variable.
1702 * It will also check that the environment variable is updatable, and silently
1703 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001704 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001705static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001706{
1707 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001708 char *envname;
1709 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001710
Cyril Bontéac92a062014-12-27 22:28:38 +01001711 if (idx < 0 || idx >= EXTCHK_SIZE) {
1712 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1713 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001714 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001715
1716 envname = extcheck_envs[idx].name;
1717 vmaxlen = extcheck_envs[idx].vmaxlen;
1718
1719 /* Check if the environment variable is already set, and silently reject
1720 * the update if this one is not updatable. */
1721 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1722 return 0;
1723
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001724 /* Instead of sending NOT_USED, sending an empty value is preferable */
1725 if (strcmp(value, "NOT_USED") == 0) {
1726 value = "";
1727 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001728
1729 len = strlen(envname) + 1;
1730 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1731 len += strlen(value);
1732 else
1733 len += vmaxlen;
1734
1735 if (!check->envp[idx])
1736 check->envp[idx] = malloc(len + 1);
1737
1738 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001739 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1740 return 1;
1741 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001742 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001743 if (ret < 0) {
1744 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1745 return 1;
1746 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001747 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001748 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1749 return 1;
1750 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001751 return 0;
1752}
Simon Horman98637e52014-06-20 12:30:16 +09001753
1754static int prepare_external_check(struct check *check)
1755{
1756 struct server *s = check->server;
1757 struct proxy *px = s->proxy;
1758 struct listener *listener = NULL, *l;
1759 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001760 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001761 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001762
1763 list_for_each_entry(l, &px->conf.listeners, by_fe)
1764 /* Use the first INET, INET6 or UNIX listener */
1765 if (l->addr.ss_family == AF_INET ||
1766 l->addr.ss_family == AF_INET6 ||
1767 l->addr.ss_family == AF_UNIX) {
1768 listener = l;
1769 break;
1770 }
1771
Simon Horman98637e52014-06-20 12:30:16 +09001772 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001773 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1774 if (!check->envp) {
1775 Alert("Failed to allocate memory for environment variables. Aborting\n");
1776 goto err;
1777 }
Simon Horman98637e52014-06-20 12:30:16 +09001778
Cyril Bontéac92a062014-12-27 22:28:38 +01001779 check->argv = calloc(6, sizeof(char *));
1780 if (!check->argv) {
1781 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001782 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001783 }
Simon Horman98637e52014-06-20 12:30:16 +09001784
1785 check->argv[0] = px->check_command;
1786
Cyril Bonté777be862014-12-02 21:21:35 +01001787 if (!listener) {
1788 check->argv[1] = strdup("NOT_USED");
1789 check->argv[2] = strdup("NOT_USED");
1790 }
1791 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001792 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001793 addr_to_str(&listener->addr, buf, sizeof(buf));
1794 check->argv[1] = strdup(buf);
1795 port_to_str(&listener->addr, buf, sizeof(buf));
1796 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001797 }
1798 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001799 const struct sockaddr_un *un;
1800
1801 un = (struct sockaddr_un *)&listener->addr;
1802 check->argv[1] = strdup(un->sun_path);
1803 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001804 }
1805 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001806 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001807 goto err;
1808 }
1809
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001810 addr_to_str(&s->addr, buf, sizeof(buf));
1811 check->argv[3] = strdup(buf);
Willy Tarreau04276f32017-01-06 17:41:29 +01001812
1813 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1814 snprintf(buf, sizeof(buf), "%u", s->svc_port);
1815 else
1816 *buf = 0;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001817 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001818
Cyril Bontéac92a062014-12-27 22:28:38 +01001819 for (i = 0; i < 5; i++) {
1820 if (!check->argv[i]) {
1821 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001822 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001823 }
1824 }
Simon Horman98637e52014-06-20 12:30:16 +09001825
Cyril Bontéac92a062014-12-27 22:28:38 +01001826 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001827 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001828 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1829 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1830 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1831 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001832 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001833 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1834 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1835 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1836 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1837 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1838 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1839
1840 /* Ensure that we don't leave any hole in check->envp */
1841 for (i = 0; i < EXTCHK_SIZE; i++)
1842 if (!check->envp[i])
1843 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001844
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001845 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001846err:
1847 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001848 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001849 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001850 free(check->envp);
1851 check->envp = NULL;
1852 }
1853
1854 if (check->argv) {
1855 for (i = 1; i < 5; i++)
1856 free(check->argv[i]);
1857 free(check->argv);
1858 check->argv = NULL;
1859 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001860 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001861}
1862
Simon Hormanb00d17a2014-06-13 16:18:16 +09001863/*
Simon Horman98637e52014-06-20 12:30:16 +09001864 * establish a server health-check that makes use of a process.
1865 *
1866 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001867 * - SF_ERR_NONE if everything's OK
Willy Tarreaue7dff022015-04-03 01:14:29 +02001868 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
Tim Düsterhus4896c442016-11-29 02:15:19 +01001869 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001870 *
1871 * Blocks and then unblocks SIGCHLD
1872 */
1873static int connect_proc_chk(struct task *t)
1874{
Cyril Bontéac92a062014-12-27 22:28:38 +01001875 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001876 struct check *check = t->context;
1877 struct server *s = check->server;
1878 struct proxy *px = s->proxy;
1879 int status;
1880 pid_t pid;
1881
Willy Tarreaue7dff022015-04-03 01:14:29 +02001882 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001883
1884 block_sigchld();
1885
1886 pid = fork();
1887 if (pid < 0) {
1888 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1889 strerror(errno));
1890 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1891 goto out;
1892 }
1893 if (pid == 0) {
1894 /* Child */
1895 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001896 int fd;
1897
1898 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1899 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1900
1901 while (fd < global.rlimit_nofile)
1902 close(fd++);
1903
Simon Horman98637e52014-06-20 12:30:16 +09001904 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001905 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001906 execvp(px->check_command, check->argv);
1907 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1908 strerror(errno));
1909 exit(-1);
1910 }
1911
1912 /* Parent */
1913 if (check->result == CHK_RES_UNKNOWN) {
1914 if (pid_list_add(pid, t) != NULL) {
1915 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1916
1917 if (px->timeout.check && px->timeout.connect) {
1918 int t_con = tick_add(now_ms, px->timeout.connect);
1919 t->expire = tick_first(t->expire, t_con);
1920 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001921 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001922 goto out;
1923 }
1924 else {
1925 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1926 }
1927 kill(pid, SIGTERM); /* process creation error */
1928 }
1929 else
1930 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1931
1932out:
1933 unblock_sigchld();
1934 return status;
1935}
1936
1937/*
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001938 * manages a server health-check that uses an external process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001939 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1940 */
Simon Horman98637e52014-06-20 12:30:16 +09001941static struct task *process_chk_proc(struct task *t)
1942{
1943 struct check *check = t->context;
1944 struct server *s = check->server;
Simon Horman98637e52014-06-20 12:30:16 +09001945 int rv;
1946 int ret;
1947 int expired = tick_is_expired(t->expire, now_ms);
1948
1949 if (!(check->state & CHK_ST_INPROGRESS)) {
1950 /* no check currently running */
1951 if (!expired) /* woke up too early */
1952 return t;
1953
1954 /* we don't send any health-checks when the proxy is
1955 * stopped, the server should not be checked or the check
1956 * is disabled.
1957 */
1958 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1959 s->proxy->state == PR_STSTOPPED)
1960 goto reschedule;
1961
1962 /* we'll initiate a new check */
1963 set_server_check_status(check, HCHK_STATUS_START, NULL);
1964
1965 check->state |= CHK_ST_INPROGRESS;
1966
Simon Hormandbf70192015-01-30 11:22:53 +09001967 ret = connect_proc_chk(t);
Willy Tarreaud7c3fbd2017-10-04 15:19:26 +02001968 if (ret == SF_ERR_NONE) {
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001969 /* the process was forked, we allow up to min(inter,
1970 * timeout.connect) for it to report its status, but
1971 * only when timeout.check is set as it may be to short
1972 * for a full check otherwise.
Simon Horman98637e52014-06-20 12:30:16 +09001973 */
1974 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1975
1976 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1977 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1978 t->expire = tick_first(t->expire, t_con);
1979 }
1980
1981 goto reschedule;
Simon Horman98637e52014-06-20 12:30:16 +09001982 }
1983
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001984 /* here, we failed to start the check */
Simon Horman98637e52014-06-20 12:30:16 +09001985
1986 check->state &= ~CHK_ST_INPROGRESS;
1987 check_notify_failure(check);
1988
1989 /* we allow up to min(inter, timeout.connect) for a connection
1990 * to establish but only when timeout.check is set
1991 * as it may be to short for a full check otherwise
1992 */
1993 while (tick_is_expired(t->expire, now_ms)) {
1994 int t_con;
1995
1996 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1997 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1998
1999 if (s->proxy->timeout.check)
2000 t->expire = tick_first(t->expire, t_con);
2001 }
2002 }
2003 else {
2004 /* there was a test running.
2005 * First, let's check whether there was an uncaught error,
2006 * which can happen on connect timeout or error.
2007 */
2008 if (check->result == CHK_RES_UNKNOWN) {
2009 /* good connection is enough for pure TCP check */
2010 struct pid_list *elem = check->curpid;
2011 int status = HCHK_STATUS_UNKNOWN;
2012
2013 if (elem->exited) {
2014 status = elem->status; /* Save in case the process exits between use below */
2015 if (!WIFEXITED(status))
2016 check->code = -1;
2017 else
2018 check->code = WEXITSTATUS(status);
2019 if (!WIFEXITED(status) || WEXITSTATUS(status))
2020 status = HCHK_STATUS_PROCERR;
2021 else
2022 status = HCHK_STATUS_PROCOK;
2023 } else if (expired) {
2024 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02002025 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002026 kill(elem->pid, SIGTERM);
2027 }
2028 set_server_check_status(check, status, NULL);
2029 }
2030
2031 if (check->result == CHK_RES_FAILED) {
2032 /* a failure or timeout detected */
2033 check_notify_failure(check);
2034 }
2035 else if (check->result == CHK_RES_CONDPASS) {
2036 /* check is OK but asks for stopping mode */
2037 check_notify_stopping(check);
2038 }
2039 else if (check->result == CHK_RES_PASSED) {
2040 /* a success was detected */
2041 check_notify_success(check);
2042 }
2043 check->state &= ~CHK_ST_INPROGRESS;
2044
2045 pid_list_del(check->curpid);
2046
2047 rv = 0;
2048 if (global.spread_checks > 0) {
2049 rv = srv_getinter(check) * global.spread_checks / 100;
2050 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2051 }
2052 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2053 }
2054
2055 reschedule:
2056 while (tick_is_expired(t->expire, now_ms))
2057 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2058 return t;
2059}
2060
2061/*
2062 * manages a server health-check that uses a connection. Returns
2063 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2064 */
2065static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002066{
Simon Horman4a741432013-02-23 15:35:38 +09002067 struct check *check = t->context;
2068 struct server *s = check->server;
2069 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002070 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002071 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002072 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002073
Willy Tarreau2c115e52013-12-11 19:41:16 +01002074 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002075 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002076 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002077 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002078
Simon Horman671b6f02013-11-25 10:46:39 +09002079 /* we don't send any health-checks when the proxy is
2080 * stopped, the server should not be checked or the check
2081 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002083 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002084 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002085 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002086
2087 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002088 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002089
Willy Tarreau2c115e52013-12-11 19:41:16 +01002090 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002091 check->bi->p = check->bi->data;
2092 check->bi->i = 0;
2093 check->bo->p = check->bo->data;
2094 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002095
Simon Hormandbf70192015-01-30 11:22:53 +09002096 ret = connect_conn_chk(t);
Willy Tarreau00149122017-10-04 18:05:01 +02002097 conn = check->conn;
2098
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002099 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002100 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002101 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002102 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002103 /* we allow up to min(inter, timeout.connect) for a connection
2104 * to establish but only when timeout.check is set
2105 * as it may be to short for a full check otherwise
2106 */
Simon Horman4a741432013-02-23 15:35:38 +09002107 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002108
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002109 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2110 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2111 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002112 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002113
2114 if (check->type)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002115 conn_xprt_want_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau06559ac2013-12-05 01:53:08 +01002116
Willy Tarreau5a78f362012-11-23 12:47:05 +01002117 goto reschedule;
2118
Willy Tarreaue7dff022015-04-03 01:14:29 +02002119 case SF_ERR_SRVTO: /* ETIMEDOUT */
2120 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau00149122017-10-04 18:05:01 +02002121 if (conn)
2122 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002123 chk_report_conn_err(check, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002124 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002125 /* should share same code than cases below */
2126 case SF_ERR_CHK_PORT:
2127 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002128 case SF_ERR_PRXCOND:
2129 case SF_ERR_RESOURCE:
2130 case SF_ERR_INTERNAL:
Willy Tarreau00149122017-10-04 18:05:01 +02002131 if (conn)
2132 conn->flags |= CO_FL_ERROR;
2133 chk_report_conn_err(check, conn ? 0 : ENOMEM, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002134 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 }
2136
Willy Tarreau5a78f362012-11-23 12:47:05 +01002137 /* here, we have seen a synchronous error, no fd was allocated */
Olivier Houchard390485a2017-10-24 19:03:30 +02002138 if (conn) {
2139 conn_free(conn);
2140 check->conn = conn = NULL;
2141 }
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002142
Willy Tarreau2c115e52013-12-11 19:41:16 +01002143 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002144 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002145
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002146 /* we allow up to min(inter, timeout.connect) for a connection
2147 * to establish but only when timeout.check is set
2148 * as it may be to short for a full check otherwise
2149 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002150 while (tick_is_expired(t->expire, now_ms)) {
2151 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002152
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002153 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002154 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002155
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002156 if (s->proxy->timeout.check)
2157 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002158 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159 }
2160 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002161 /* there was a test running.
2162 * First, let's check whether there was an uncaught error,
2163 * which can happen on connect timeout or error.
2164 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002165 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002166 /* good connection is enough for pure TCP check */
2167 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002168 if (check->use_ssl)
2169 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002170 else
Simon Horman4a741432013-02-23 15:35:38 +09002171 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002172 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002173 else if ((conn->flags & CO_FL_ERROR) || expired) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002174 chk_report_conn_err(check, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002175 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002176 else
2177 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002178 }
2179
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002180 /* check complete or aborted */
Willy Tarreau00149122017-10-04 18:05:01 +02002181 if (conn && conn->xprt) {
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002182 /* The check was aborted and the connection was not yet closed.
2183 * This can happen upon timeout, or when an external event such
2184 * as a failed response coupled with "observe layer7" caused the
2185 * server state to be suddenly changed.
2186 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002187 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02002188 conn_full_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002189 }
2190
Willy Tarreau00149122017-10-04 18:05:01 +02002191 if (conn) {
2192 conn_free(conn);
2193 check->conn = conn = NULL;
2194 }
2195
Willy Tarreauaf549582014-05-16 17:37:50 +02002196 if (check->result == CHK_RES_FAILED) {
2197 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002198 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002199 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002200 else if (check->result == CHK_RES_CONDPASS) {
2201 /* check is OK but asks for stopping mode */
2202 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002203 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002204 else if (check->result == CHK_RES_PASSED) {
2205 /* a success was detected */
2206 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002207 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002208 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002209
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002210 rv = 0;
2211 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002212 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002213 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002214 }
Simon Horman4a741432013-02-23 15:35:38 +09002215 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002216 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002217
2218 reschedule:
2219 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002220 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002221 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002222 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002223}
2224
Simon Horman98637e52014-06-20 12:30:16 +09002225/*
2226 * manages a server health-check. Returns
2227 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2228 */
2229static struct task *process_chk(struct task *t)
2230{
2231 struct check *check = t->context;
2232
2233 if (check->type == PR_O2_EXT_CHK)
2234 return process_chk_proc(t);
2235 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002236
Simon Horman98637e52014-06-20 12:30:16 +09002237}
2238
Simon Horman5c942422013-11-25 10:46:32 +09002239static int start_check_task(struct check *check, int mininter,
2240 int nbcheck, int srvpos)
2241{
2242 struct task *t;
2243 /* task for the check */
Emeric Brunc60def82017-09-27 14:59:38 +02002244 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Simon Horman5c942422013-11-25 10:46:32 +09002245 Alert("Starting [%s:%s] check: out of memory.\n",
2246 check->server->proxy->id, check->server->id);
2247 return 0;
2248 }
2249
2250 check->task = t;
2251 t->process = process_chk;
2252 t->context = check;
2253
Willy Tarreau1746eec2014-04-25 10:46:47 +02002254 if (mininter < srv_getinter(check))
2255 mininter = srv_getinter(check);
2256
2257 if (global.max_spread_checks && mininter > global.max_spread_checks)
2258 mininter = global.max_spread_checks;
2259
Simon Horman5c942422013-11-25 10:46:32 +09002260 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002261 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002262 check->start = now;
2263 task_queue(t);
2264
2265 return 1;
2266}
2267
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002268/*
2269 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002270 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002271 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002272static int start_checks()
2273{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002274
2275 struct proxy *px;
2276 struct server *s;
2277 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002278 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002279
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002280 /* 1- count the checkers to run simultaneously.
2281 * We also determine the minimum interval among all of those which
2282 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2283 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002284 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002285 * too short an interval for all others.
2286 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002287 for (px = proxy; px; px = px->next) {
2288 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002289 if (s->slowstart) {
Emeric Brunc60def82017-09-27 14:59:38 +02002290 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002291 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002292 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002293 }
2294 /* We need a warmup task that will be called when the server
2295 * state switches from down to up.
2296 */
2297 s->warmup = t;
2298 t->process = server_warmup;
2299 t->context = s;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002300 /* server can be in this state only because of */
Emeric Brun52a91d32017-08-31 14:41:55 +02002301 if (s->next_state == SRV_ST_STARTING)
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002302 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20)));
Willy Tarreaue7b73482013-11-21 11:50:50 +01002303 }
2304
Willy Tarreaud8514a22013-12-11 21:10:14 +01002305 if (s->check.state & CHK_ST_CONFIGURED) {
2306 nbcheck++;
2307 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2308 (!mininter || mininter > srv_getinter(&s->check)))
2309 mininter = srv_getinter(&s->check);
2310 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002311
Willy Tarreaud8514a22013-12-11 21:10:14 +01002312 if (s->agent.state & CHK_ST_CONFIGURED) {
2313 nbcheck++;
2314 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2315 (!mininter || mininter > srv_getinter(&s->agent)))
2316 mininter = srv_getinter(&s->agent);
2317 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002318 }
2319 }
2320
Simon Horman4a741432013-02-23 15:35:38 +09002321 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002322 return 0;
2323
2324 srand((unsigned)time(NULL));
2325
2326 /*
2327 * 2- start them as far as possible from each others. For this, we will
2328 * start them after their interval set to the min interval divided by
2329 * the number of servers, weighted by the server's position in the list.
2330 */
2331 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002332 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2333 if (init_pid_list()) {
2334 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002335 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002336 }
2337 }
2338
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002339 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002340 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002341 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002342 if (s->check.type == PR_O2_EXT_CHK) {
2343 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002344 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002345 }
Simon Hormand60d6912013-11-25 10:46:36 +09002346 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002347 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002348 srvpos++;
2349 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002350
Simon Hormand60d6912013-11-25 10:46:36 +09002351 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002352 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002353 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002354 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002355 }
2356 srvpos++;
2357 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002358 }
2359 }
2360 return 0;
2361}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002362
2363/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002364 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002365 * The buffer MUST be terminated by a null byte before calling this function.
2366 * Sets server status appropriately. The caller is responsible for ensuring
2367 * that the buffer contains at least 13 characters. If <done> is zero, we may
2368 * return 0 to indicate that data is required to decide of a match.
2369 */
2370static int httpchk_expect(struct server *s, int done)
2371{
2372 static char status_msg[] = "HTTP status check returned code <000>";
2373 char status_code[] = "000";
2374 char *contentptr;
2375 int crlf;
2376 int ret;
2377
2378 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2379 case PR_O2_EXP_STS:
2380 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002381 memcpy(status_code, s->check.bi->data + 9, 3);
2382 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002383
2384 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2385 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2386 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002387 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002388
2389 /* we necessarily have the response, so there are no partial failures */
2390 if (s->proxy->options2 & PR_O2_EXP_INV)
2391 ret = !ret;
2392
Simon Horman4a741432013-02-23 15:35:38 +09002393 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002394 break;
2395
2396 case PR_O2_EXP_STR:
2397 case PR_O2_EXP_RSTR:
2398 /* very simple response parser: ignore CR and only count consecutive LFs,
2399 * stop with contentptr pointing to first char after the double CRLF or
2400 * to '\0' if crlf < 2.
2401 */
2402 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002403 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002404 if (crlf >= 2)
2405 break;
2406 if (*contentptr == '\r')
2407 continue;
2408 else if (*contentptr == '\n')
2409 crlf++;
2410 else
2411 crlf = 0;
2412 }
2413
2414 /* Check that response contains a body... */
2415 if (crlf < 2) {
2416 if (!done)
2417 return 0;
2418
Simon Horman4a741432013-02-23 15:35:38 +09002419 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002420 "HTTP content check could not find a response body");
2421 return 1;
2422 }
2423
2424 /* Check that response body is not empty... */
2425 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002426 if (!done)
2427 return 0;
2428
Simon Horman4a741432013-02-23 15:35:38 +09002429 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002430 "HTTP content check found empty response body");
2431 return 1;
2432 }
2433
2434 /* Check the response content against the supplied string
2435 * or regex... */
2436 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2437 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2438 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002439 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002440
2441 /* if we don't match, we may need to wait more */
2442 if (!ret && !done)
2443 return 0;
2444
2445 if (ret) {
2446 /* content matched */
2447 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002448 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002449 "HTTP check matched unwanted content");
2450 else
Simon Horman4a741432013-02-23 15:35:38 +09002451 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002452 "HTTP content check matched");
2453 }
2454 else {
2455 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002456 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002457 "HTTP check did not match unwanted content");
2458 else
Simon Horman4a741432013-02-23 15:35:38 +09002459 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002460 "HTTP content check did not match");
2461 }
2462 break;
2463 }
2464 return 1;
2465}
2466
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002467/*
2468 * return the id of a step in a send/expect session
2469 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002470static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002471{
2472 struct tcpcheck_rule *cur = NULL, *next = NULL;
2473 int i = 0;
2474
Willy Tarreau213c6782014-10-02 14:51:02 +02002475 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002476 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002477 return 0;
2478
Simon Hormane16c1b32015-01-30 11:22:57 +09002479 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002480
2481 /* no step => first step */
2482 if (cur == NULL)
2483 return 1;
2484
2485 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002486 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002487 if (next->list.p == &cur->list)
2488 break;
2489 ++i;
2490 }
2491
2492 return i;
2493}
2494
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002495/*
2496 * return the latest known comment before (including) the given stepid
2497 * returns NULL if no comment found
2498 */
2499static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2500{
2501 struct tcpcheck_rule *cur = NULL;
2502 char *ret = NULL;
2503 int i = 0;
2504
2505 /* not even started anything yet, return latest comment found before any action */
2506 if (!check->current_step) {
2507 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2508 if (cur->action == TCPCHK_ACT_COMMENT)
2509 ret = cur->comment;
2510 else
2511 goto return_comment;
2512 }
2513 }
2514
2515 i = 1;
2516 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2517 if (cur->comment)
2518 ret = cur->comment;
2519
2520 if (i >= stepid)
2521 goto return_comment;
2522
2523 ++i;
2524 }
2525
2526 return_comment:
2527 return ret;
2528}
2529
Willy Tarreaube74b882017-10-04 16:22:49 +02002530/* proceed with next steps for the TCP checks <check>. Note that this is called
2531 * both from the connection's wake() callback and from the check scheduling task.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002532 * It returns 0 on normal cases, or <0 if a close() has happened on an existing
2533 * connection, presenting the risk of an fd replacement.
Willy Tarreaube74b882017-10-04 16:22:49 +02002534 */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002535static int tcpcheck_main(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002536{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002537 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002538 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002539 int done = 0, ret = 0, step = 0;
Willy Tarreaube74b882017-10-04 16:22:49 +02002540 struct connection *conn = check->conn;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002541 struct server *s = check->server;
2542 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002543 struct list *head = check->tcpcheck_rules;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002544 int retcode = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002545
Willy Tarreauef953952014-10-02 14:30:14 +02002546 /* here, we know that the check is complete or that it failed */
2547 if (check->result != CHK_RES_UNKNOWN)
2548 goto out_end_tcpcheck;
2549
2550 /* We have 4 possibilities here :
2551 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
Willy Tarreau00149122017-10-04 18:05:01 +02002552 * connection attempt was made yet ; conn==NULL;current_step==NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002553 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2554 * does not exist (no rule), so a connection attempt was made
Willy Tarreau00149122017-10-04 18:05:01 +02002555 * before coming here, conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002556 * 3. we're coming back after having started with step 1, so we may
Willy Tarreau00149122017-10-04 18:05:01 +02002557 * be waiting for a connection attempt to complete. conn!=NULL.
2558 * 4. the connection + handshake are complete. conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002559 *
2560 * #2 and #3 are quite similar, we want both the connection and the
2561 * handshake to complete before going any further. Thus we must always
2562 * wait for a connection to complete unless we're before and existing
2563 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002564 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002565
2566 /* find first rule and skip comments */
2567 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2568 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2569 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2570
Willy Tarreau00149122017-10-04 18:05:01 +02002571 if ((check->current_step || &next->list == head) &&
2572 (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002573 /* we allow up to min(inter, timeout.connect) for a connection
2574 * to establish but only when timeout.check is set
2575 * as it may be to short for a full check otherwise
2576 */
2577 while (tick_is_expired(t->expire, now_ms)) {
2578 int t_con;
2579
2580 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2581 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2582
2583 if (s->proxy->timeout.check)
2584 t->expire = tick_first(t->expire, t_con);
2585 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002586 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002587 }
2588
Willy Tarreauef953952014-10-02 14:30:14 +02002589 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002590 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002591 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002592 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002593 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002594
Willy Tarreau213c6782014-10-02 14:51:02 +02002595 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002596 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002597 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002598 check->bo->p = check->bo->data;
2599 check->bo->o = 0;
2600 check->bi->p = check->bi->data;
2601 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002602 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002603 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2604 if (s->proxy->timeout.check)
2605 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2606 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002607
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002608 /* It's only the rules which will enable send/recv */
Willy Tarreau00149122017-10-04 18:05:01 +02002609 if (conn)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002610 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002611
Willy Tarreauabca5b62013-12-06 14:19:25 +01002612 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002613 /* We have to try to flush the output buffer before reading, at
2614 * the end, or if we're about to send a string that does not fit
2615 * in the remaining space. That explains why we break out of the
Willy Tarreau00149122017-10-04 18:05:01 +02002616 * loop after this control. If we have data, conn is valid.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002617 */
2618 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002619 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002620 check->current_step->action != TCPCHK_ACT_SEND ||
2621 check->current_step->string_len >= buffer_total_space(check->bo))) {
2622
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002623 __conn_xprt_want_send(conn);
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002624 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002625 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002626 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002627 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002628 goto out_end_tcpcheck;
2629 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002630 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002631 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002632 }
2633
Willy Tarreau263013d2015-05-13 11:59:14 +02002634 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002635 break;
2636
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002637 /* have 'next' point to the next rule or NULL if we're on the
2638 * last one, connect() needs this.
2639 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002640 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002641
2642 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002643 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002644 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002645
2646 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002647 if (&next->list == head)
2648 next = NULL;
2649
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002650 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2651 struct protocol *proto;
2652 struct xprt_ops *xprt;
2653
Willy Tarreau00149122017-10-04 18:05:01 +02002654 /* For a connect action we'll create a new connection.
2655 * We may also have to kill a previous one. But we don't
2656 * want to leave *without* a connection if we came here
2657 * from the connection layer, hence with a connection.
2658 * Thus we'll proceed in the following order :
2659 * 1: close but not release previous connection
2660 * 2: try to get a new connection
2661 * 3: release and replace the old one on success
2662 */
2663 if (check->conn) {
Willy Tarreau402dbc12017-10-05 17:53:13 +02002664 conn_full_close(check->conn);
Willy Tarreau00149122017-10-04 18:05:01 +02002665 retcode = -1; /* do not reuse the fd! */
2666 }
2667
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002668 /* mark the step as started */
2669 check->last_started_step = check->current_step;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002670
2671 /* prepare new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02002672 conn = conn_new();
2673 if (!conn) {
2674 step = tcpcheck_get_step_id(check);
2675 chunk_printf(&trash, "TCPCHK error allocating connection at step %d", step);
2676 comment = tcpcheck_get_step_comment(check, step);
2677 if (comment)
2678 chunk_appendf(&trash, " comment: '%s'", comment);
2679 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2680 check->current_step = NULL;
2681 return retcode;
2682 }
2683
2684 if (check->conn)
2685 conn_free(check->conn);
2686 check->conn = conn;
2687
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002688 conn_attach(conn, check, &check_conn_cb);
2689 conn->target = &s->obj_type;
2690
2691 /* no client address */
2692 clear_addr(&conn->addr.from);
2693
Simon Horman41f58762015-01-30 11:22:56 +09002694 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002695 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002696 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002697 }
2698 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002699 /* we'll connect to the addr on the server */
2700 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002701 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002702 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002703
2704 /* port */
2705 if (check->current_step->port)
2706 set_host_port(&conn->addr.to, check->current_step->port);
2707 else if (check->port)
2708 set_host_port(&conn->addr.to, check->port);
2709
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002710 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002711 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002712 }
2713 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002714 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002715 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002716 conn_prepare(conn, proto, xprt);
2717
Willy Tarreaue7dff022015-04-03 01:14:29 +02002718 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002719 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002720 ret = proto->connect(conn,
2721 1 /* I/O polling is always needed */,
2722 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002723 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2724 conn->send_proxy_ofs = 1;
2725 conn->flags |= CO_FL_SEND_PROXY;
2726 }
2727
2728 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002729 * - SF_ERR_NONE if everything's OK
2730 * - SF_ERR_SRVTO if there are no more servers
2731 * - SF_ERR_SRVCL if the connection was refused by the server
2732 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2733 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2734 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002735 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002736 * Note that we try to prevent the network stack from sending the ACK during the
2737 * connect() when a pure TCP check is used (without PROXY protocol).
2738 */
2739 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002740 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002741 /* we allow up to min(inter, timeout.connect) for a connection
2742 * to establish but only when timeout.check is set
2743 * as it may be to short for a full check otherwise
2744 */
2745 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2746
2747 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2748 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2749 t->expire = tick_first(t->expire, t_con);
2750 }
2751 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002752 case SF_ERR_SRVTO: /* ETIMEDOUT */
2753 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002754 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002755 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002756 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002757 comment = tcpcheck_get_step_comment(check, step);
2758 if (comment)
2759 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002760 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2761 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002762 case SF_ERR_PRXCOND:
2763 case SF_ERR_RESOURCE:
2764 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002765 step = tcpcheck_get_step_id(check);
2766 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002767 comment = tcpcheck_get_step_comment(check, step);
2768 if (comment)
2769 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002770 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2771 goto out_end_tcpcheck;
2772 }
2773
2774 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002775 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002776
2777 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002778 while (&check->current_step->list != head &&
2779 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002780 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002781
Willy Tarreauf2c87352015-05-13 12:08:21 +02002782 if (&check->current_step->list == head)
2783 break;
2784
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002785 /* don't do anything until the connection is established */
2786 if (!(conn->flags & CO_FL_CONNECTED)) {
2787 /* update expire time, should be done by process_chk */
2788 /* we allow up to min(inter, timeout.connect) for a connection
2789 * to establish but only when timeout.check is set
2790 * as it may be to short for a full check otherwise
2791 */
2792 while (tick_is_expired(t->expire, now_ms)) {
2793 int t_con;
2794
2795 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2796 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2797
2798 if (s->proxy->timeout.check)
2799 t->expire = tick_first(t->expire, t_con);
2800 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002801 return retcode;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002802 }
2803
2804 } /* end 'connect' */
2805 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2806 /* mark the step as started */
2807 check->last_started_step = check->current_step;
2808
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002809 /* reset the read buffer */
2810 if (*check->bi->data != '\0') {
2811 *check->bi->data = '\0';
2812 check->bi->i = 0;
2813 }
2814
Willy Tarreaubbae3f02017-08-30 09:59:52 +02002815 if (conn->flags & CO_FL_SOCK_WR_SH) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002816 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002817 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002818 goto out_end_tcpcheck;
2819 }
2820
Willy Tarreauabca5b62013-12-06 14:19:25 +01002821 if (check->current_step->string_len >= check->bo->size) {
2822 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2823 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002824 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002825 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2826 goto out_end_tcpcheck;
2827 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002828
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002829 /* do not try to send if there is no space */
2830 if (check->current_step->string_len >= buffer_total_space(check->bo))
2831 continue;
2832
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002833 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2834 *check->bo->p = '\0'; /* to make gdb output easier to read */
2835
Willy Tarreauabca5b62013-12-06 14:19:25 +01002836 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002837 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002838
2839 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002840 while (&check->current_step->list != head &&
2841 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002842 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002843
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002844 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002845 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002846 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002847 goto out_end_tcpcheck;
2848
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002849 __conn_xprt_want_recv(conn);
Willy Tarreau310987a2014-01-22 19:46:33 +01002850 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreau54e917c2017-08-30 07:35:35 +02002851 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002852 done = 1;
2853 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2854 /* Report network errors only if we got no other data. Otherwise
2855 * we'll let the upper layers decide whether the response is OK
2856 * or not. It is very common that an RST sent by the server is
2857 * reported as an error just after the last data chunk.
2858 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002859 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002860 goto out_end_tcpcheck;
2861 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002862 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002863 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002864 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002865 }
2866
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002867 /* mark the step as started */
2868 check->last_started_step = check->current_step;
2869
2870
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002871 /* Intermediate or complete response received.
2872 * Terminate string in check->bi->data buffer.
2873 */
2874 if (check->bi->i < check->bi->size) {
2875 check->bi->data[check->bi->i] = '\0';
2876 }
2877 else {
2878 check->bi->data[check->bi->i - 1] = '\0';
2879 done = 1; /* buffer full, don't wait for more data */
2880 }
2881
2882 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002883
2884 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002885 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002886 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002887 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002888
2889 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002890 step = tcpcheck_get_step_id(check);
2891 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002892 comment = tcpcheck_get_step_comment(check, step);
2893 if (comment)
2894 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002895 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2896
2897 goto out_end_tcpcheck;
2898 }
2899
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002900 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002901 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002902
Willy Tarreaua970c282013-12-06 12:47:19 +01002903 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002904 if (check->current_step->string != NULL)
2905 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2906 else if (check->current_step->expect_regex != NULL)
2907 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002908
2909 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002910 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002911
2912 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002913 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002914 if (ret) {
2915 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002916 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002917 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002918 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002919 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002920 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002921 }
2922 else {
2923 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002924 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002925 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002926 comment = tcpcheck_get_step_comment(check, step);
2927 if (comment)
2928 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002929 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2930 goto out_end_tcpcheck;
2931 }
2932 /* matched and was supposed to => OK, next step */
2933 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002934 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002935 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002936
2937 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002938 while (&check->current_step->list != head &&
2939 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002940 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002941
Willy Tarreauf2c87352015-05-13 12:08:21 +02002942 if (&check->current_step->list == head)
2943 break;
2944
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002945 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002946 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002947 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002948 }
2949 }
2950 else {
2951 /* not matched */
2952 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002953 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002954 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002955 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002956
2957 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002958 while (&check->current_step->list != head &&
2959 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002960 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002961
Willy Tarreauf2c87352015-05-13 12:08:21 +02002962 if (&check->current_step->list == head)
2963 break;
2964
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002965 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002966 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002967 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002968 }
2969 /* not matched but was supposed to => ERROR */
2970 else {
2971 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002972 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002973 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002974 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002975 }
2976 else {
2977 /* we were looking for a regex */
2978 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002979 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002980 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002981 comment = tcpcheck_get_step_comment(check, step);
2982 if (comment)
2983 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002984 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2985 goto out_end_tcpcheck;
2986 }
2987 }
2988 } /* end expect */
2989 } /* end loop over double chained step list */
2990
Willy Tarreau263013d2015-05-13 11:59:14 +02002991 /* We're waiting for some I/O to complete, we've reached the end of the
2992 * rules, or both. Do what we have to do, otherwise we're done.
2993 */
2994 if (&check->current_step->list == head && !check->bo->o) {
2995 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2996 goto out_end_tcpcheck;
2997 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002998
Willy Tarreau53c5a042015-05-13 11:38:17 +02002999 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003000 if (check->bo->o)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003001 __conn_xprt_want_send(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003002
Willy Tarreau53c5a042015-05-13 11:38:17 +02003003 if (&check->current_step->list != head &&
3004 check->current_step->action == TCPCHK_ACT_EXPECT)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003005 __conn_xprt_want_recv(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003006 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003007
3008 out_end_tcpcheck:
3009 /* collect possible new errors */
3010 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02003011 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003012
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003013 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003014 check->current_step = NULL;
3015
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003016 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003017 conn->flags |= CO_FL_ERROR;
3018
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003019 __conn_xprt_stop_both(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003020 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003021}
3022
Simon Hormanb1900d52015-01-30 11:22:54 +09003023const char *init_check(struct check *check, int type)
3024{
3025 check->type = type;
3026
3027 /* Allocate buffer for requests... */
3028 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3029 return "out of memory while allocating check buffer";
3030 }
3031 check->bi->size = global.tune.chksize;
3032
3033 /* Allocate buffer for responses... */
3034 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3035 return "out of memory while allocating check buffer";
3036 }
3037 check->bo->size = global.tune.chksize;
3038
Simon Hormanb1900d52015-01-30 11:22:54 +09003039 return NULL;
3040}
3041
Simon Hormanbfb5d332015-01-30 11:22:55 +09003042void free_check(struct check *check)
3043{
3044 free(check->bi);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003045 check->bi = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003046 free(check->bo);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003047 check->bo = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003048 free(check->conn);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003049 check->conn = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003050}
3051
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003052void email_alert_free(struct email_alert *alert)
3053{
3054 struct tcpcheck_rule *rule, *back;
3055
3056 if (!alert)
3057 return;
3058
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003059 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list) {
3060 LIST_DEL(&rule->list);
3061 free(rule->comment);
3062 free(rule->string);
3063 if (rule->expect_regex)
3064 regex_free(rule->expect_regex);
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003065 pool_free2(pool2_tcpcheck_rule, rule);
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003066 }
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003067 pool_free2(pool2_email_alert, alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003068}
3069
3070static struct task *process_email_alert(struct task *t)
3071{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003072 struct check *check = t->context;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003073 struct email_alertq *q;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003074 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003075
3076 q = container_of(check, typeof(*q), check);
3077
Christopher Faulet0108bb32017-10-20 21:34:32 +02003078 while (1) {
3079 if (!(check->state & CHK_ST_ENABLED)) {
3080 if (LIST_ISEMPTY(&q->email_alerts)) {
3081 /* All alerts processed, queue the task */
3082 t->expire = TICK_ETERNITY;
3083 task_queue(t);
3084 return t;
3085 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003086
3087 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003088 LIST_DEL(&alert->list);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003089 t->expire = now_ms;
3090 check->server = alert->srv;
3091 check->tcpcheck_rules = &alert->tcpcheck_rules;
3092 check->status = HCHK_STATUS_INI;
3093 check->state |= CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003094 }
3095
Christopher Faulet0108bb32017-10-20 21:34:32 +02003096 process_chk(t);
3097 if (check->state & CHK_ST_INPROGRESS)
3098 break;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003099
3100 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3101 email_alert_free(alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003102 check->tcpcheck_rules = NULL;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003103 check->server = NULL;
3104 check->state &= ~CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003105 }
3106 return t;
3107}
3108
Christopher Faulet0108bb32017-10-20 21:34:32 +02003109/* Initializes mailer alerts for the proxy <p> using <mls> parameters.
3110 *
3111 * The function returns 1 in success case, otherwise, it returns 0 and err is
3112 * filled.
3113 */
3114int init_email_alert(struct mailers *mls, struct proxy *p, char **err)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003115{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003116 struct mailer *mailer;
3117 struct email_alertq *queues;
3118 const char *err_str;
3119 int i = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003120
Christopher Faulet0108bb32017-10-20 21:34:32 +02003121 if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) {
3122 memprintf(err, "out of memory while allocating mailer alerts queues");
3123 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003124 }
3125
Christopher Faulet0108bb32017-10-20 21:34:32 +02003126 for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) {
3127 struct email_alertq *q = &queues[i];
3128 struct check *check = &q->check;
3129 struct task *t;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003130
3131 LIST_INIT(&q->email_alerts);
3132
Christopher Faulet0108bb32017-10-20 21:34:32 +02003133 check->inter = mls->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003134 check->rise = DEF_AGENT_RISETIME;
3135 check->fall = DEF_AGENT_FALLTIME;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003136 if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) {
3137 memprintf(err, "%s", err_str);
3138 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003139 }
3140
3141 check->xprt = mailer->xprt;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003142 check->addr = mailer->addr;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003143 if (!get_host_port(&mailer->addr))
3144 /* Default to submission port */
3145 check->port = 587;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003146 //check->server = s;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003147
Emeric Brunc60def82017-09-27 14:59:38 +02003148 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003149 memprintf(err, "out of memory while allocating mailer alerts task");
3150 goto error;
3151 }
3152
3153 check->task = t;
3154 t->process = process_email_alert;
3155 t->context = check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003156
Christopher Faulet0108bb32017-10-20 21:34:32 +02003157 /* check this in one ms */
3158 t->expire = TICK_ETERNITY;
3159 check->start = now;
3160 task_queue(t);
3161 }
3162
3163 mls->users++;
3164 free(p->email_alert.mailers.name);
3165 p->email_alert.mailers.m = mls;
3166 p->email_alert.queues = queues;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003167 return 0;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003168
3169 error:
3170 for (i = 0; i < mls->count; i++) {
3171 struct email_alertq *q = &queues[i];
3172 struct check *check = &q->check;
3173
3174 if (check->task) {
3175 task_delete(check->task);
3176 task_free(check->task);
3177 check->task = NULL;
3178 }
3179 free_check(check);
3180 }
3181 free(queues);
3182 return 1;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003183}
3184
3185
3186static int add_tcpcheck_expect_str(struct list *list, const char *str)
3187{
3188 struct tcpcheck_rule *tcpcheck;
3189
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003190 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003191 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003192 memset(tcpcheck, 0, sizeof(*tcpcheck));
3193 tcpcheck->action = TCPCHK_ACT_EXPECT;
3194 tcpcheck->string = strdup(str);
3195 tcpcheck->expect_regex = NULL;
3196 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003197 if (!tcpcheck->string) {
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003198 pool_free2(pool2_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003199 return 0;
3200 }
3201
3202 LIST_ADDQ(list, &tcpcheck->list);
3203 return 1;
3204}
3205
3206static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3207{
3208 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003209 const char *in;
3210 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003211 int i;
3212
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003213 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003214 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003215 memset(tcpcheck, 0, sizeof(*tcpcheck));
3216 tcpcheck->action = TCPCHK_ACT_SEND;
3217 tcpcheck->expect_regex = NULL;
3218 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003219 tcpcheck->string_len = 0;
3220 for (i = 0; strs[i]; i++)
3221 tcpcheck->string_len += strlen(strs[i]);
3222
3223 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3224 if (!tcpcheck->string) {
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003225 pool_free2(pool2_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003226 return 0;
3227 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003228
Willy Tarreau64345aa2016-08-10 19:29:09 +02003229 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003230 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003231 for (in = strs[i]; (*dst = *in++); dst++);
3232 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003233
3234 LIST_ADDQ(list, &tcpcheck->list);
3235 return 1;
3236}
3237
Christopher Faulet0108bb32017-10-20 21:34:32 +02003238static int enqueue_one_email_alert(struct proxy *p, struct server *s,
3239 struct email_alertq *q, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003240{
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003241 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003242 struct tcpcheck_rule *tcpcheck;
3243 struct check *check = &q->check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003244
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003245 if ((alert = pool_alloc2(pool2_email_alert)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003246 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003247 LIST_INIT(&alert->list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003248 LIST_INIT(&alert->tcpcheck_rules);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003249 alert->srv = s;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003250
3251 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003252 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003253 memset(tcpcheck, 0, sizeof(*tcpcheck));
3254 tcpcheck->action = TCPCHK_ACT_CONNECT;
3255 tcpcheck->comment = NULL;
3256 tcpcheck->string = NULL;
3257 tcpcheck->expect_regex = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003258 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3259
3260 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3261 goto error;
3262
3263 {
3264 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3265 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3266 goto error;
3267 }
3268
3269 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3270 goto error;
3271
3272 {
3273 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3274 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3275 goto error;
3276 }
3277
3278 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3279 goto error;
3280
3281 {
3282 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3283 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3284 goto error;
3285 }
3286
3287 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3288 goto error;
3289
3290 {
3291 const char * const strs[2] = { "DATA\r\n" };
3292 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3293 goto error;
3294 }
3295
3296 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3297 goto error;
3298
3299 {
3300 struct tm tm;
3301 char datestr[48];
3302 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003303 "From: ", p->email_alert.from, "\r\n",
3304 "To: ", p->email_alert.to, "\r\n",
3305 "Date: ", datestr, "\r\n",
3306 "Subject: [HAproxy Alert] ", msg, "\r\n",
3307 "\r\n",
3308 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003309 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003310 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003311 NULL
3312 };
3313
3314 get_localtime(date.tv_sec, &tm);
3315
3316 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3317 goto error;
3318 }
3319
3320 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3321 goto error;
3322 }
3323
3324 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3325 goto error;
3326
3327 {
3328 const char * const strs[2] = { "QUIT\r\n" };
3329 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3330 goto error;
3331 }
3332
3333 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3334 goto error;
3335
Christopher Faulet0108bb32017-10-20 21:34:32 +02003336 task_wakeup(check->task, TASK_WOKEN_MSG);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003337 LIST_ADDQ(&q->email_alerts, &alert->list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003338 return 1;
3339
3340error:
3341 email_alert_free(alert);
3342 return 0;
3343}
3344
Christopher Faulet0108bb32017-10-20 21:34:32 +02003345static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003346{
3347 int i;
3348 struct mailer *mailer;
3349
3350 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3351 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003352 if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003353 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3354 return;
3355 }
3356 }
3357
3358 return;
3359}
3360
3361/*
3362 * Send email alert if configured.
3363 */
Simon Horman64e34162015-02-06 11:11:57 +09003364void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003365{
3366 va_list argp;
3367 char buf[1024];
3368 int len;
3369 struct proxy *p = s->proxy;
3370
Christopher Faulet0108bb32017-10-20 21:34:32 +02003371 if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003372 return;
3373
3374 va_start(argp, format);
3375 len = vsnprintf(buf, sizeof(buf), format, argp);
3376 va_end(argp);
3377
Thierry FOURNIER62c8a212017-02-09 12:19:27 +01003378 if (len < 0 || len >= sizeof(buf)) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003379 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003380 return;
3381 }
3382
Christopher Faulet0108bb32017-10-20 21:34:32 +02003383 enqueue_email_alert(p, s, buf);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003384}
3385
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003386/*
3387 * Return value:
3388 * the port to be used for the health check
3389 * 0 in case no port could be found for the check
3390 */
3391int srv_check_healthcheck_port(struct check *chk)
3392{
3393 int i = 0;
3394 struct server *srv = NULL;
3395
3396 srv = chk->server;
3397
3398 /* If neither a port nor an addr was specified and no check transport
3399 * layer is forced, then the transport layer used by the checks is the
3400 * same as for the production traffic. Otherwise we use raw_sock by
3401 * default, unless one is specified.
3402 */
3403 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003404 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003405 chk->send_proxy |= (srv->pp_opts);
3406 }
3407
3408 /* by default, we use the health check port ocnfigured */
3409 if (chk->port > 0)
3410 return chk->port;
3411
3412 /* try to get the port from check_core.addr if check.port not set */
3413 i = get_host_port(&chk->addr);
3414 if (i > 0)
3415 return i;
3416
3417 /* try to get the port from server address */
3418 /* prevent MAPPORTS from working at this point, since checks could
3419 * not be performed in such case (MAPPORTS impose a relative ports
3420 * based on live traffic)
3421 */
3422 if (srv->flags & SRV_F_MAPPORTS)
3423 return 0;
Willy Tarreau04276f32017-01-06 17:41:29 +01003424
3425 i = srv->svc_port; /* by default */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003426 if (i > 0)
3427 return i;
3428
3429 return 0;
3430}
3431
Willy Tarreau865c5142016-12-21 20:04:48 +01003432__attribute__((constructor))
3433static void __check_init(void)
3434{
3435 hap_register_post_check(start_checks);
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003436
3437 pool2_email_alert = create_pool("email_alert", sizeof(struct email_alert), MEM_F_SHARED);
3438 pool2_tcpcheck_rule = create_pool("tcpcheck_rule", sizeof(struct tcpcheck_rule), MEM_F_SHARED);
Willy Tarreau865c5142016-12-21 20:04:48 +01003439}
3440
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003441
Willy Tarreaubd741542010-03-16 18:46:54 +01003442/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003443 * Local variables:
3444 * c-indent-level: 8
3445 * c-basic-offset: 8
3446 * End:
3447 */