blob: 0944c0e3bf579be73813ec39a057d3c45b2b4400 [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 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02001980 task_set_affinity(t, tid_bit);
Simon Horman98637e52014-06-20 12:30:16 +09001981 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 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002043 task_set_affinity(t, MAX_THREADS_MASK);
Simon Horman98637e52014-06-20 12:30:16 +09002044 check->state &= ~CHK_ST_INPROGRESS;
2045
2046 pid_list_del(check->curpid);
2047
2048 rv = 0;
2049 if (global.spread_checks > 0) {
2050 rv = srv_getinter(check) * global.spread_checks / 100;
2051 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2052 }
2053 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2054 }
2055
2056 reschedule:
2057 while (tick_is_expired(t->expire, now_ms))
2058 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2059 return t;
2060}
2061
2062/*
2063 * manages a server health-check that uses a connection. Returns
2064 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2065 */
2066static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002067{
Simon Horman4a741432013-02-23 15:35:38 +09002068 struct check *check = t->context;
2069 struct server *s = check->server;
2070 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002071 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002072 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002073 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074
Willy Tarreau2c115e52013-12-11 19:41:16 +01002075 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002076 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002077 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002078 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002079
Simon Horman671b6f02013-11-25 10:46:39 +09002080 /* we don't send any health-checks when the proxy is
2081 * stopped, the server should not be checked or the check
2082 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002083 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002084 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002085 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002086 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002087
2088 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002089 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002090
Willy Tarreau2c115e52013-12-11 19:41:16 +01002091 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002092 check->bi->p = check->bi->data;
2093 check->bi->i = 0;
2094 check->bo->p = check->bo->data;
2095 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002096
Simon Hormandbf70192015-01-30 11:22:53 +09002097 ret = connect_conn_chk(t);
Willy Tarreau00149122017-10-04 18:05:01 +02002098 conn = check->conn;
2099
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002100 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002101 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002102 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002103 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002104 /* we allow up to min(inter, timeout.connect) for a connection
2105 * to establish but only when timeout.check is set
2106 * as it may be to short for a full check otherwise
2107 */
Simon Horman4a741432013-02-23 15:35:38 +09002108 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002109
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002110 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2111 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2112 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002113 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002114
2115 if (check->type)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002116 conn_xprt_want_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau06559ac2013-12-05 01:53:08 +01002117
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002118 task_set_affinity(t, tid_bit);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002119 goto reschedule;
2120
Willy Tarreaue7dff022015-04-03 01:14:29 +02002121 case SF_ERR_SRVTO: /* ETIMEDOUT */
2122 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau00149122017-10-04 18:05:01 +02002123 if (conn)
2124 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002125 chk_report_conn_err(check, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002126 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002127 /* should share same code than cases below */
2128 case SF_ERR_CHK_PORT:
2129 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002130 case SF_ERR_PRXCOND:
2131 case SF_ERR_RESOURCE:
2132 case SF_ERR_INTERNAL:
Willy Tarreau00149122017-10-04 18:05:01 +02002133 if (conn)
2134 conn->flags |= CO_FL_ERROR;
2135 chk_report_conn_err(check, conn ? 0 : ENOMEM, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002136 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002137 }
2138
Willy Tarreau5a78f362012-11-23 12:47:05 +01002139 /* here, we have seen a synchronous error, no fd was allocated */
Olivier Houchard390485a2017-10-24 19:03:30 +02002140 if (conn) {
2141 conn_free(conn);
2142 check->conn = conn = NULL;
2143 }
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002144
Willy Tarreau2c115e52013-12-11 19:41:16 +01002145 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002146 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002147
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002148 /* we allow up to min(inter, timeout.connect) for a connection
2149 * to establish but only when timeout.check is set
2150 * as it may be to short for a full check otherwise
2151 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002152 while (tick_is_expired(t->expire, now_ms)) {
2153 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002154
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002155 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002156 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002157
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002158 if (s->proxy->timeout.check)
2159 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002160 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002161 }
2162 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002163 /* there was a test running.
2164 * First, let's check whether there was an uncaught error,
2165 * which can happen on connect timeout or error.
2166 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002167 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002168 /* good connection is enough for pure TCP check */
2169 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002170 if (check->use_ssl)
2171 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002172 else
Simon Horman4a741432013-02-23 15:35:38 +09002173 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002174 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002175 else if ((conn->flags & CO_FL_ERROR) || expired) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002176 chk_report_conn_err(check, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002177 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002178 else
2179 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002180 }
2181
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002182 /* check complete or aborted */
Willy Tarreau00149122017-10-04 18:05:01 +02002183 if (conn && conn->xprt) {
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002184 /* The check was aborted and the connection was not yet closed.
2185 * This can happen upon timeout, or when an external event such
2186 * as a failed response coupled with "observe layer7" caused the
2187 * server state to be suddenly changed.
2188 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002189 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02002190 conn_full_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002191 }
2192
Willy Tarreau00149122017-10-04 18:05:01 +02002193 if (conn) {
2194 conn_free(conn);
2195 check->conn = conn = NULL;
2196 }
2197
Willy Tarreauaf549582014-05-16 17:37:50 +02002198 if (check->result == CHK_RES_FAILED) {
2199 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002200 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002201 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002202 else if (check->result == CHK_RES_CONDPASS) {
2203 /* check is OK but asks for stopping mode */
2204 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002205 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002206 else if (check->result == CHK_RES_PASSED) {
2207 /* a success was detected */
2208 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002209 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002210 task_set_affinity(t, MAX_THREADS_MASK);
Willy Tarreau2c115e52013-12-11 19:41:16 +01002211 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002212
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002213 rv = 0;
2214 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002215 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002216 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002217 }
Simon Horman4a741432013-02-23 15:35:38 +09002218 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002219 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002220
2221 reschedule:
2222 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002223 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002224 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002225 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226}
2227
Simon Horman98637e52014-06-20 12:30:16 +09002228/*
2229 * manages a server health-check. Returns
2230 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2231 */
2232static struct task *process_chk(struct task *t)
2233{
2234 struct check *check = t->context;
2235
2236 if (check->type == PR_O2_EXT_CHK)
2237 return process_chk_proc(t);
2238 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002239
Simon Horman98637e52014-06-20 12:30:16 +09002240}
2241
Simon Horman5c942422013-11-25 10:46:32 +09002242static int start_check_task(struct check *check, int mininter,
2243 int nbcheck, int srvpos)
2244{
2245 struct task *t;
2246 /* task for the check */
Emeric Brunc60def82017-09-27 14:59:38 +02002247 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Simon Horman5c942422013-11-25 10:46:32 +09002248 Alert("Starting [%s:%s] check: out of memory.\n",
2249 check->server->proxy->id, check->server->id);
2250 return 0;
2251 }
2252
2253 check->task = t;
2254 t->process = process_chk;
2255 t->context = check;
2256
Willy Tarreau1746eec2014-04-25 10:46:47 +02002257 if (mininter < srv_getinter(check))
2258 mininter = srv_getinter(check);
2259
2260 if (global.max_spread_checks && mininter > global.max_spread_checks)
2261 mininter = global.max_spread_checks;
2262
Simon Horman5c942422013-11-25 10:46:32 +09002263 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002264 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002265 check->start = now;
2266 task_queue(t);
2267
2268 return 1;
2269}
2270
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002271/*
2272 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002273 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002274 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002275static int start_checks()
2276{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002277
2278 struct proxy *px;
2279 struct server *s;
2280 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002281 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002282
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002283 /* 1- count the checkers to run simultaneously.
2284 * We also determine the minimum interval among all of those which
2285 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2286 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002287 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002288 * too short an interval for all others.
2289 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002290 for (px = proxy; px; px = px->next) {
2291 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002292 if (s->slowstart) {
Emeric Brunc60def82017-09-27 14:59:38 +02002293 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002294 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002295 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002296 }
2297 /* We need a warmup task that will be called when the server
2298 * state switches from down to up.
2299 */
2300 s->warmup = t;
2301 t->process = server_warmup;
2302 t->context = s;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002303 /* server can be in this state only because of */
Emeric Brun52a91d32017-08-31 14:41:55 +02002304 if (s->next_state == SRV_ST_STARTING)
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002305 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 +01002306 }
2307
Willy Tarreaud8514a22013-12-11 21:10:14 +01002308 if (s->check.state & CHK_ST_CONFIGURED) {
2309 nbcheck++;
2310 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2311 (!mininter || mininter > srv_getinter(&s->check)))
2312 mininter = srv_getinter(&s->check);
2313 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002314
Willy Tarreaud8514a22013-12-11 21:10:14 +01002315 if (s->agent.state & CHK_ST_CONFIGURED) {
2316 nbcheck++;
2317 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2318 (!mininter || mininter > srv_getinter(&s->agent)))
2319 mininter = srv_getinter(&s->agent);
2320 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002321 }
2322 }
2323
Simon Horman4a741432013-02-23 15:35:38 +09002324 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002325 return 0;
2326
2327 srand((unsigned)time(NULL));
2328
2329 /*
2330 * 2- start them as far as possible from each others. For this, we will
2331 * start them after their interval set to the min interval divided by
2332 * the number of servers, weighted by the server's position in the list.
2333 */
2334 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002335 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2336 if (init_pid_list()) {
2337 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002338 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002339 }
2340 }
2341
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002342 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002343 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002344 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002345 if (s->check.type == PR_O2_EXT_CHK) {
2346 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002347 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002348 }
Simon Hormand60d6912013-11-25 10:46:36 +09002349 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002350 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002351 srvpos++;
2352 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002353
Simon Hormand60d6912013-11-25 10:46:36 +09002354 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002355 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002356 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002357 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002358 }
2359 srvpos++;
2360 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002361 }
2362 }
2363 return 0;
2364}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002365
2366/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002367 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002368 * The buffer MUST be terminated by a null byte before calling this function.
2369 * Sets server status appropriately. The caller is responsible for ensuring
2370 * that the buffer contains at least 13 characters. If <done> is zero, we may
2371 * return 0 to indicate that data is required to decide of a match.
2372 */
2373static int httpchk_expect(struct server *s, int done)
2374{
2375 static char status_msg[] = "HTTP status check returned code <000>";
2376 char status_code[] = "000";
2377 char *contentptr;
2378 int crlf;
2379 int ret;
2380
2381 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2382 case PR_O2_EXP_STS:
2383 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002384 memcpy(status_code, s->check.bi->data + 9, 3);
2385 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002386
2387 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2388 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2389 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002390 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002391
2392 /* we necessarily have the response, so there are no partial failures */
2393 if (s->proxy->options2 & PR_O2_EXP_INV)
2394 ret = !ret;
2395
Simon Horman4a741432013-02-23 15:35:38 +09002396 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002397 break;
2398
2399 case PR_O2_EXP_STR:
2400 case PR_O2_EXP_RSTR:
2401 /* very simple response parser: ignore CR and only count consecutive LFs,
2402 * stop with contentptr pointing to first char after the double CRLF or
2403 * to '\0' if crlf < 2.
2404 */
2405 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002406 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002407 if (crlf >= 2)
2408 break;
2409 if (*contentptr == '\r')
2410 continue;
2411 else if (*contentptr == '\n')
2412 crlf++;
2413 else
2414 crlf = 0;
2415 }
2416
2417 /* Check that response contains a body... */
2418 if (crlf < 2) {
2419 if (!done)
2420 return 0;
2421
Simon Horman4a741432013-02-23 15:35:38 +09002422 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002423 "HTTP content check could not find a response body");
2424 return 1;
2425 }
2426
2427 /* Check that response body is not empty... */
2428 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002429 if (!done)
2430 return 0;
2431
Simon Horman4a741432013-02-23 15:35:38 +09002432 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002433 "HTTP content check found empty response body");
2434 return 1;
2435 }
2436
2437 /* Check the response content against the supplied string
2438 * or regex... */
2439 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2440 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2441 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002442 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002443
2444 /* if we don't match, we may need to wait more */
2445 if (!ret && !done)
2446 return 0;
2447
2448 if (ret) {
2449 /* content matched */
2450 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002451 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002452 "HTTP check matched unwanted content");
2453 else
Simon Horman4a741432013-02-23 15:35:38 +09002454 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002455 "HTTP content check matched");
2456 }
2457 else {
2458 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002459 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002460 "HTTP check did not match unwanted content");
2461 else
Simon Horman4a741432013-02-23 15:35:38 +09002462 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002463 "HTTP content check did not match");
2464 }
2465 break;
2466 }
2467 return 1;
2468}
2469
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002470/*
2471 * return the id of a step in a send/expect session
2472 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002473static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002474{
2475 struct tcpcheck_rule *cur = NULL, *next = NULL;
2476 int i = 0;
2477
Willy Tarreau213c6782014-10-02 14:51:02 +02002478 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002479 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002480 return 0;
2481
Simon Hormane16c1b32015-01-30 11:22:57 +09002482 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002483
2484 /* no step => first step */
2485 if (cur == NULL)
2486 return 1;
2487
2488 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002489 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002490 if (next->list.p == &cur->list)
2491 break;
2492 ++i;
2493 }
2494
2495 return i;
2496}
2497
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002498/*
2499 * return the latest known comment before (including) the given stepid
2500 * returns NULL if no comment found
2501 */
2502static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2503{
2504 struct tcpcheck_rule *cur = NULL;
2505 char *ret = NULL;
2506 int i = 0;
2507
2508 /* not even started anything yet, return latest comment found before any action */
2509 if (!check->current_step) {
2510 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2511 if (cur->action == TCPCHK_ACT_COMMENT)
2512 ret = cur->comment;
2513 else
2514 goto return_comment;
2515 }
2516 }
2517
2518 i = 1;
2519 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2520 if (cur->comment)
2521 ret = cur->comment;
2522
2523 if (i >= stepid)
2524 goto return_comment;
2525
2526 ++i;
2527 }
2528
2529 return_comment:
2530 return ret;
2531}
2532
Willy Tarreaube74b882017-10-04 16:22:49 +02002533/* proceed with next steps for the TCP checks <check>. Note that this is called
2534 * both from the connection's wake() callback and from the check scheduling task.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002535 * It returns 0 on normal cases, or <0 if a close() has happened on an existing
2536 * connection, presenting the risk of an fd replacement.
Willy Tarreaube74b882017-10-04 16:22:49 +02002537 */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002538static int tcpcheck_main(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002539{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002540 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002541 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002542 int done = 0, ret = 0, step = 0;
Willy Tarreaube74b882017-10-04 16:22:49 +02002543 struct connection *conn = check->conn;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002544 struct server *s = check->server;
2545 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002546 struct list *head = check->tcpcheck_rules;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002547 int retcode = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002548
Willy Tarreauef953952014-10-02 14:30:14 +02002549 /* here, we know that the check is complete or that it failed */
2550 if (check->result != CHK_RES_UNKNOWN)
2551 goto out_end_tcpcheck;
2552
2553 /* We have 4 possibilities here :
2554 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
Willy Tarreau00149122017-10-04 18:05:01 +02002555 * connection attempt was made yet ; conn==NULL;current_step==NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002556 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2557 * does not exist (no rule), so a connection attempt was made
Willy Tarreau00149122017-10-04 18:05:01 +02002558 * before coming here, conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002559 * 3. we're coming back after having started with step 1, so we may
Willy Tarreau00149122017-10-04 18:05:01 +02002560 * be waiting for a connection attempt to complete. conn!=NULL.
2561 * 4. the connection + handshake are complete. conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002562 *
2563 * #2 and #3 are quite similar, we want both the connection and the
2564 * handshake to complete before going any further. Thus we must always
2565 * wait for a connection to complete unless we're before and existing
2566 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002567 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002568
2569 /* find first rule and skip comments */
2570 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2571 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2572 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2573
Willy Tarreau00149122017-10-04 18:05:01 +02002574 if ((check->current_step || &next->list == head) &&
2575 (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002576 /* we allow up to min(inter, timeout.connect) for a connection
2577 * to establish but only when timeout.check is set
2578 * as it may be to short for a full check otherwise
2579 */
2580 while (tick_is_expired(t->expire, now_ms)) {
2581 int t_con;
2582
2583 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2584 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2585
2586 if (s->proxy->timeout.check)
2587 t->expire = tick_first(t->expire, t_con);
2588 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002589 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002590 }
2591
Willy Tarreauef953952014-10-02 14:30:14 +02002592 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002593 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002594 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002595 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002596 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002597
Willy Tarreau213c6782014-10-02 14:51:02 +02002598 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002599 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002600 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002601 check->bo->p = check->bo->data;
2602 check->bo->o = 0;
2603 check->bi->p = check->bi->data;
2604 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002605 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002606 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2607 if (s->proxy->timeout.check)
2608 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2609 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002610
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002611 /* It's only the rules which will enable send/recv */
Willy Tarreau00149122017-10-04 18:05:01 +02002612 if (conn)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002613 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002614
Willy Tarreauabca5b62013-12-06 14:19:25 +01002615 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002616 /* We have to try to flush the output buffer before reading, at
2617 * the end, or if we're about to send a string that does not fit
2618 * in the remaining space. That explains why we break out of the
Willy Tarreau00149122017-10-04 18:05:01 +02002619 * loop after this control. If we have data, conn is valid.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002620 */
2621 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002622 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002623 check->current_step->action != TCPCHK_ACT_SEND ||
2624 check->current_step->string_len >= buffer_total_space(check->bo))) {
2625
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002626 __conn_xprt_want_send(conn);
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002627 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002628 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002629 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002630 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002631 goto out_end_tcpcheck;
2632 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002633 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002634 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002635 }
2636
Willy Tarreau263013d2015-05-13 11:59:14 +02002637 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002638 break;
2639
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002640 /* have 'next' point to the next rule or NULL if we're on the
2641 * last one, connect() needs this.
2642 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002643 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002644
2645 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002646 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002647 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002648
2649 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002650 if (&next->list == head)
2651 next = NULL;
2652
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002653 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2654 struct protocol *proto;
2655 struct xprt_ops *xprt;
2656
Willy Tarreau00149122017-10-04 18:05:01 +02002657 /* For a connect action we'll create a new connection.
2658 * We may also have to kill a previous one. But we don't
2659 * want to leave *without* a connection if we came here
2660 * from the connection layer, hence with a connection.
2661 * Thus we'll proceed in the following order :
2662 * 1: close but not release previous connection
2663 * 2: try to get a new connection
2664 * 3: release and replace the old one on success
2665 */
2666 if (check->conn) {
Willy Tarreau402dbc12017-10-05 17:53:13 +02002667 conn_full_close(check->conn);
Willy Tarreau00149122017-10-04 18:05:01 +02002668 retcode = -1; /* do not reuse the fd! */
2669 }
2670
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002671 /* mark the step as started */
2672 check->last_started_step = check->current_step;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002673
2674 /* prepare new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02002675 conn = conn_new();
2676 if (!conn) {
2677 step = tcpcheck_get_step_id(check);
2678 chunk_printf(&trash, "TCPCHK error allocating connection at step %d", step);
2679 comment = tcpcheck_get_step_comment(check, step);
2680 if (comment)
2681 chunk_appendf(&trash, " comment: '%s'", comment);
2682 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2683 check->current_step = NULL;
2684 return retcode;
2685 }
2686
2687 if (check->conn)
2688 conn_free(check->conn);
2689 check->conn = conn;
2690
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002691 conn_attach(conn, check, &check_conn_cb);
2692 conn->target = &s->obj_type;
2693
2694 /* no client address */
2695 clear_addr(&conn->addr.from);
2696
Simon Horman41f58762015-01-30 11:22:56 +09002697 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002698 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002699 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002700 }
2701 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002702 /* we'll connect to the addr on the server */
2703 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002704 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002705 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002706
2707 /* port */
2708 if (check->current_step->port)
2709 set_host_port(&conn->addr.to, check->current_step->port);
2710 else if (check->port)
2711 set_host_port(&conn->addr.to, check->port);
2712
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002713 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002714 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002715 }
2716 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002717 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002718 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002719 conn_prepare(conn, proto, xprt);
2720
Willy Tarreaue7dff022015-04-03 01:14:29 +02002721 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002722 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002723 ret = proto->connect(conn,
2724 1 /* I/O polling is always needed */,
2725 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002726 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2727 conn->send_proxy_ofs = 1;
2728 conn->flags |= CO_FL_SEND_PROXY;
2729 }
2730
2731 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002732 * - SF_ERR_NONE if everything's OK
2733 * - SF_ERR_SRVTO if there are no more servers
2734 * - SF_ERR_SRVCL if the connection was refused by the server
2735 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2736 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2737 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002738 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002739 * Note that we try to prevent the network stack from sending the ACK during the
2740 * connect() when a pure TCP check is used (without PROXY protocol).
2741 */
2742 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002743 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002744 /* we allow up to min(inter, timeout.connect) for a connection
2745 * to establish but only when timeout.check is set
2746 * as it may be to short for a full check otherwise
2747 */
2748 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2749
2750 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2751 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2752 t->expire = tick_first(t->expire, t_con);
2753 }
2754 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002755 case SF_ERR_SRVTO: /* ETIMEDOUT */
2756 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002757 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002758 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002759 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002760 comment = tcpcheck_get_step_comment(check, step);
2761 if (comment)
2762 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002763 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2764 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002765 case SF_ERR_PRXCOND:
2766 case SF_ERR_RESOURCE:
2767 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002768 step = tcpcheck_get_step_id(check);
2769 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002770 comment = tcpcheck_get_step_comment(check, step);
2771 if (comment)
2772 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002773 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2774 goto out_end_tcpcheck;
2775 }
2776
2777 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002778 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002779
2780 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002781 while (&check->current_step->list != head &&
2782 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002783 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002784
Willy Tarreauf2c87352015-05-13 12:08:21 +02002785 if (&check->current_step->list == head)
2786 break;
2787
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002788 /* don't do anything until the connection is established */
2789 if (!(conn->flags & CO_FL_CONNECTED)) {
2790 /* update expire time, should be done by process_chk */
2791 /* we allow up to min(inter, timeout.connect) for a connection
2792 * to establish but only when timeout.check is set
2793 * as it may be to short for a full check otherwise
2794 */
2795 while (tick_is_expired(t->expire, now_ms)) {
2796 int t_con;
2797
2798 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2799 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2800
2801 if (s->proxy->timeout.check)
2802 t->expire = tick_first(t->expire, t_con);
2803 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002804 return retcode;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002805 }
2806
2807 } /* end 'connect' */
2808 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2809 /* mark the step as started */
2810 check->last_started_step = check->current_step;
2811
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002812 /* reset the read buffer */
2813 if (*check->bi->data != '\0') {
2814 *check->bi->data = '\0';
2815 check->bi->i = 0;
2816 }
2817
Willy Tarreaubbae3f02017-08-30 09:59:52 +02002818 if (conn->flags & CO_FL_SOCK_WR_SH) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002819 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002820 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002821 goto out_end_tcpcheck;
2822 }
2823
Willy Tarreauabca5b62013-12-06 14:19:25 +01002824 if (check->current_step->string_len >= check->bo->size) {
2825 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2826 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002827 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002828 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2829 goto out_end_tcpcheck;
2830 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002831
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002832 /* do not try to send if there is no space */
2833 if (check->current_step->string_len >= buffer_total_space(check->bo))
2834 continue;
2835
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002836 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2837 *check->bo->p = '\0'; /* to make gdb output easier to read */
2838
Willy Tarreauabca5b62013-12-06 14:19:25 +01002839 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002840 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002841
2842 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002843 while (&check->current_step->list != head &&
2844 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002845 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002846
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002847 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002848 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002849 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002850 goto out_end_tcpcheck;
2851
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002852 __conn_xprt_want_recv(conn);
Willy Tarreau310987a2014-01-22 19:46:33 +01002853 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreau54e917c2017-08-30 07:35:35 +02002854 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002855 done = 1;
2856 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2857 /* Report network errors only if we got no other data. Otherwise
2858 * we'll let the upper layers decide whether the response is OK
2859 * or not. It is very common that an RST sent by the server is
2860 * reported as an error just after the last data chunk.
2861 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002862 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002863 goto out_end_tcpcheck;
2864 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002865 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002866 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002867 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002868 }
2869
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002870 /* mark the step as started */
2871 check->last_started_step = check->current_step;
2872
2873
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002874 /* Intermediate or complete response received.
2875 * Terminate string in check->bi->data buffer.
2876 */
2877 if (check->bi->i < check->bi->size) {
2878 check->bi->data[check->bi->i] = '\0';
2879 }
2880 else {
2881 check->bi->data[check->bi->i - 1] = '\0';
2882 done = 1; /* buffer full, don't wait for more data */
2883 }
2884
2885 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002886
2887 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002888 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002889 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002890 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002891
2892 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002893 step = tcpcheck_get_step_id(check);
2894 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002895 comment = tcpcheck_get_step_comment(check, step);
2896 if (comment)
2897 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002898 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2899
2900 goto out_end_tcpcheck;
2901 }
2902
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002903 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002904 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002905
Willy Tarreaua970c282013-12-06 12:47:19 +01002906 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002907 if (check->current_step->string != NULL)
2908 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2909 else if (check->current_step->expect_regex != NULL)
2910 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002911
2912 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002913 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002914
2915 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002916 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002917 if (ret) {
2918 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002919 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002920 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002921 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002922 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002923 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002924 }
2925 else {
2926 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002927 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002928 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002929 comment = tcpcheck_get_step_comment(check, step);
2930 if (comment)
2931 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002932 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2933 goto out_end_tcpcheck;
2934 }
2935 /* matched and was supposed to => OK, next step */
2936 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002937 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002938 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002939
2940 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002941 while (&check->current_step->list != head &&
2942 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002943 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002944
Willy Tarreauf2c87352015-05-13 12:08:21 +02002945 if (&check->current_step->list == head)
2946 break;
2947
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002948 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002949 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002950 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002951 }
2952 }
2953 else {
2954 /* not matched */
2955 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002956 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002957 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002958 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002959
2960 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002961 while (&check->current_step->list != head &&
2962 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002963 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002964
Willy Tarreauf2c87352015-05-13 12:08:21 +02002965 if (&check->current_step->list == head)
2966 break;
2967
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002968 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002969 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002970 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002971 }
2972 /* not matched but was supposed to => ERROR */
2973 else {
2974 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002975 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002976 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002977 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002978 }
2979 else {
2980 /* we were looking for a regex */
2981 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002982 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002983 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002984 comment = tcpcheck_get_step_comment(check, step);
2985 if (comment)
2986 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002987 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2988 goto out_end_tcpcheck;
2989 }
2990 }
2991 } /* end expect */
2992 } /* end loop over double chained step list */
2993
Willy Tarreau263013d2015-05-13 11:59:14 +02002994 /* We're waiting for some I/O to complete, we've reached the end of the
2995 * rules, or both. Do what we have to do, otherwise we're done.
2996 */
2997 if (&check->current_step->list == head && !check->bo->o) {
2998 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2999 goto out_end_tcpcheck;
3000 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003001
Willy Tarreau53c5a042015-05-13 11:38:17 +02003002 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003003 if (check->bo->o)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003004 __conn_xprt_want_send(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003005
Willy Tarreau53c5a042015-05-13 11:38:17 +02003006 if (&check->current_step->list != head &&
3007 check->current_step->action == TCPCHK_ACT_EXPECT)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003008 __conn_xprt_want_recv(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003009 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003010
3011 out_end_tcpcheck:
3012 /* collect possible new errors */
3013 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02003014 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003015
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003016 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003017 check->current_step = NULL;
3018
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003019 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003020 conn->flags |= CO_FL_ERROR;
3021
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003022 __conn_xprt_stop_both(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003023 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003024}
3025
Simon Hormanb1900d52015-01-30 11:22:54 +09003026const char *init_check(struct check *check, int type)
3027{
3028 check->type = type;
3029
3030 /* Allocate buffer for requests... */
3031 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3032 return "out of memory while allocating check buffer";
3033 }
3034 check->bi->size = global.tune.chksize;
3035
3036 /* Allocate buffer for responses... */
3037 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3038 return "out of memory while allocating check buffer";
3039 }
3040 check->bo->size = global.tune.chksize;
3041
Simon Hormanb1900d52015-01-30 11:22:54 +09003042 return NULL;
3043}
3044
Simon Hormanbfb5d332015-01-30 11:22:55 +09003045void free_check(struct check *check)
3046{
3047 free(check->bi);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003048 check->bi = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003049 free(check->bo);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003050 check->bo = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003051 free(check->conn);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003052 check->conn = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003053}
3054
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003055void email_alert_free(struct email_alert *alert)
3056{
3057 struct tcpcheck_rule *rule, *back;
3058
3059 if (!alert)
3060 return;
3061
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003062 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list) {
3063 LIST_DEL(&rule->list);
3064 free(rule->comment);
3065 free(rule->string);
3066 if (rule->expect_regex)
3067 regex_free(rule->expect_regex);
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003068 pool_free2(pool2_tcpcheck_rule, rule);
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003069 }
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003070 pool_free2(pool2_email_alert, alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003071}
3072
3073static struct task *process_email_alert(struct task *t)
3074{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003075 struct check *check = t->context;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003076 struct email_alertq *q;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003077 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003078
3079 q = container_of(check, typeof(*q), check);
3080
Christopher Faulet0108bb32017-10-20 21:34:32 +02003081 while (1) {
3082 if (!(check->state & CHK_ST_ENABLED)) {
3083 if (LIST_ISEMPTY(&q->email_alerts)) {
3084 /* All alerts processed, queue the task */
3085 t->expire = TICK_ETERNITY;
3086 task_queue(t);
3087 return t;
3088 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003089
3090 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003091 LIST_DEL(&alert->list);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003092 t->expire = now_ms;
3093 check->server = alert->srv;
3094 check->tcpcheck_rules = &alert->tcpcheck_rules;
3095 check->status = HCHK_STATUS_INI;
3096 check->state |= CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003097 }
3098
Christopher Faulet0108bb32017-10-20 21:34:32 +02003099 process_chk(t);
3100 if (check->state & CHK_ST_INPROGRESS)
3101 break;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003102
3103 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3104 email_alert_free(alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003105 check->tcpcheck_rules = NULL;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003106 check->server = NULL;
3107 check->state &= ~CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003108 }
3109 return t;
3110}
3111
Christopher Faulet0108bb32017-10-20 21:34:32 +02003112/* Initializes mailer alerts for the proxy <p> using <mls> parameters.
3113 *
3114 * The function returns 1 in success case, otherwise, it returns 0 and err is
3115 * filled.
3116 */
3117int init_email_alert(struct mailers *mls, struct proxy *p, char **err)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003118{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003119 struct mailer *mailer;
3120 struct email_alertq *queues;
3121 const char *err_str;
3122 int i = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003123
Christopher Faulet0108bb32017-10-20 21:34:32 +02003124 if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) {
3125 memprintf(err, "out of memory while allocating mailer alerts queues");
3126 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003127 }
3128
Christopher Faulet0108bb32017-10-20 21:34:32 +02003129 for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) {
3130 struct email_alertq *q = &queues[i];
3131 struct check *check = &q->check;
3132 struct task *t;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003133
3134 LIST_INIT(&q->email_alerts);
3135
Christopher Faulet0108bb32017-10-20 21:34:32 +02003136 check->inter = mls->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003137 check->rise = DEF_AGENT_RISETIME;
3138 check->fall = DEF_AGENT_FALLTIME;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003139 if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) {
3140 memprintf(err, "%s", err_str);
3141 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003142 }
3143
3144 check->xprt = mailer->xprt;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003145 check->addr = mailer->addr;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003146 if (!get_host_port(&mailer->addr))
3147 /* Default to submission port */
3148 check->port = 587;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003149 //check->server = s;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003150
Emeric Brunc60def82017-09-27 14:59:38 +02003151 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003152 memprintf(err, "out of memory while allocating mailer alerts task");
3153 goto error;
3154 }
3155
3156 check->task = t;
3157 t->process = process_email_alert;
3158 t->context = check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003159
Christopher Faulet0108bb32017-10-20 21:34:32 +02003160 /* check this in one ms */
3161 t->expire = TICK_ETERNITY;
3162 check->start = now;
3163 task_queue(t);
3164 }
3165
3166 mls->users++;
3167 free(p->email_alert.mailers.name);
3168 p->email_alert.mailers.m = mls;
3169 p->email_alert.queues = queues;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003170 return 0;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003171
3172 error:
3173 for (i = 0; i < mls->count; i++) {
3174 struct email_alertq *q = &queues[i];
3175 struct check *check = &q->check;
3176
3177 if (check->task) {
3178 task_delete(check->task);
3179 task_free(check->task);
3180 check->task = NULL;
3181 }
3182 free_check(check);
3183 }
3184 free(queues);
3185 return 1;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003186}
3187
3188
3189static int add_tcpcheck_expect_str(struct list *list, const char *str)
3190{
3191 struct tcpcheck_rule *tcpcheck;
3192
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003193 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003194 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003195 memset(tcpcheck, 0, sizeof(*tcpcheck));
3196 tcpcheck->action = TCPCHK_ACT_EXPECT;
3197 tcpcheck->string = strdup(str);
3198 tcpcheck->expect_regex = NULL;
3199 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003200 if (!tcpcheck->string) {
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003201 pool_free2(pool2_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003202 return 0;
3203 }
3204
3205 LIST_ADDQ(list, &tcpcheck->list);
3206 return 1;
3207}
3208
3209static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3210{
3211 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003212 const char *in;
3213 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003214 int i;
3215
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003216 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003217 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003218 memset(tcpcheck, 0, sizeof(*tcpcheck));
3219 tcpcheck->action = TCPCHK_ACT_SEND;
3220 tcpcheck->expect_regex = NULL;
3221 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003222 tcpcheck->string_len = 0;
3223 for (i = 0; strs[i]; i++)
3224 tcpcheck->string_len += strlen(strs[i]);
3225
3226 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3227 if (!tcpcheck->string) {
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003228 pool_free2(pool2_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003229 return 0;
3230 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003231
Willy Tarreau64345aa2016-08-10 19:29:09 +02003232 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003233 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003234 for (in = strs[i]; (*dst = *in++); dst++);
3235 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003236
3237 LIST_ADDQ(list, &tcpcheck->list);
3238 return 1;
3239}
3240
Christopher Faulet0108bb32017-10-20 21:34:32 +02003241static int enqueue_one_email_alert(struct proxy *p, struct server *s,
3242 struct email_alertq *q, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003243{
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003244 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003245 struct tcpcheck_rule *tcpcheck;
3246 struct check *check = &q->check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003247
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003248 if ((alert = pool_alloc2(pool2_email_alert)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003249 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003250 LIST_INIT(&alert->list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003251 LIST_INIT(&alert->tcpcheck_rules);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003252 alert->srv = s;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003253
3254 if ((tcpcheck = pool_alloc2(pool2_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003255 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003256 memset(tcpcheck, 0, sizeof(*tcpcheck));
3257 tcpcheck->action = TCPCHK_ACT_CONNECT;
3258 tcpcheck->comment = NULL;
3259 tcpcheck->string = NULL;
3260 tcpcheck->expect_regex = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003261 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3262
3263 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3264 goto error;
3265
3266 {
3267 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3268 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3269 goto error;
3270 }
3271
3272 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3273 goto error;
3274
3275 {
3276 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3277 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3278 goto error;
3279 }
3280
3281 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3282 goto error;
3283
3284 {
3285 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3286 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3287 goto error;
3288 }
3289
3290 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3291 goto error;
3292
3293 {
3294 const char * const strs[2] = { "DATA\r\n" };
3295 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3296 goto error;
3297 }
3298
3299 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3300 goto error;
3301
3302 {
3303 struct tm tm;
3304 char datestr[48];
3305 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003306 "From: ", p->email_alert.from, "\r\n",
3307 "To: ", p->email_alert.to, "\r\n",
3308 "Date: ", datestr, "\r\n",
3309 "Subject: [HAproxy Alert] ", msg, "\r\n",
3310 "\r\n",
3311 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003312 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003313 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003314 NULL
3315 };
3316
3317 get_localtime(date.tv_sec, &tm);
3318
3319 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3320 goto error;
3321 }
3322
3323 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3324 goto error;
3325 }
3326
3327 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3328 goto error;
3329
3330 {
3331 const char * const strs[2] = { "QUIT\r\n" };
3332 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3333 goto error;
3334 }
3335
3336 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3337 goto error;
3338
Christopher Faulet0108bb32017-10-20 21:34:32 +02003339 task_wakeup(check->task, TASK_WOKEN_MSG);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003340 LIST_ADDQ(&q->email_alerts, &alert->list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003341 return 1;
3342
3343error:
3344 email_alert_free(alert);
3345 return 0;
3346}
3347
Christopher Faulet0108bb32017-10-20 21:34:32 +02003348static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003349{
3350 int i;
3351 struct mailer *mailer;
3352
3353 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3354 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003355 if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003356 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3357 return;
3358 }
3359 }
3360
3361 return;
3362}
3363
3364/*
3365 * Send email alert if configured.
3366 */
Simon Horman64e34162015-02-06 11:11:57 +09003367void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003368{
3369 va_list argp;
3370 char buf[1024];
3371 int len;
3372 struct proxy *p = s->proxy;
3373
Christopher Faulet0108bb32017-10-20 21:34:32 +02003374 if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003375 return;
3376
3377 va_start(argp, format);
3378 len = vsnprintf(buf, sizeof(buf), format, argp);
3379 va_end(argp);
3380
Thierry FOURNIER62c8a212017-02-09 12:19:27 +01003381 if (len < 0 || len >= sizeof(buf)) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003382 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003383 return;
3384 }
3385
Christopher Faulet0108bb32017-10-20 21:34:32 +02003386 enqueue_email_alert(p, s, buf);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003387}
3388
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003389/*
3390 * Return value:
3391 * the port to be used for the health check
3392 * 0 in case no port could be found for the check
3393 */
3394int srv_check_healthcheck_port(struct check *chk)
3395{
3396 int i = 0;
3397 struct server *srv = NULL;
3398
3399 srv = chk->server;
3400
3401 /* If neither a port nor an addr was specified and no check transport
3402 * layer is forced, then the transport layer used by the checks is the
3403 * same as for the production traffic. Otherwise we use raw_sock by
3404 * default, unless one is specified.
3405 */
3406 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003407 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003408 chk->send_proxy |= (srv->pp_opts);
3409 }
3410
3411 /* by default, we use the health check port ocnfigured */
3412 if (chk->port > 0)
3413 return chk->port;
3414
3415 /* try to get the port from check_core.addr if check.port not set */
3416 i = get_host_port(&chk->addr);
3417 if (i > 0)
3418 return i;
3419
3420 /* try to get the port from server address */
3421 /* prevent MAPPORTS from working at this point, since checks could
3422 * not be performed in such case (MAPPORTS impose a relative ports
3423 * based on live traffic)
3424 */
3425 if (srv->flags & SRV_F_MAPPORTS)
3426 return 0;
Willy Tarreau04276f32017-01-06 17:41:29 +01003427
3428 i = srv->svc_port; /* by default */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003429 if (i > 0)
3430 return i;
3431
3432 return 0;
3433}
3434
Willy Tarreau865c5142016-12-21 20:04:48 +01003435__attribute__((constructor))
3436static void __check_init(void)
3437{
3438 hap_register_post_check(start_checks);
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003439
3440 pool2_email_alert = create_pool("email_alert", sizeof(struct email_alert), MEM_F_SHARED);
3441 pool2_tcpcheck_rule = create_pool("tcpcheck_rule", sizeof(struct tcpcheck_rule), MEM_F_SHARED);
Willy Tarreau865c5142016-12-21 20:04:48 +01003442}
3443
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003444
Willy Tarreaubd741542010-03-16 18:46:54 +01003445/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003446 * Local variables:
3447 * c-indent-level: 8
3448 * c-basic-offset: 8
3449 * End:
3450 */