blob: 722292c0e9a5a55b78f6ba9b4bd3c240b2e11def [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>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090017#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
Willy Tarreau9f6dc722019-03-01 11:15:10 +010023#include <sys/resource.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040025#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090026#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020028#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <arpa/inet.h>
30
Willy Tarreau122eba92020-06-04 10:15:32 +020031#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020032#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020034#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020035#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020036#include <haproxy/chunk.h>
Willy Tarreau7c18b542020-06-11 09:23:02 +020037#include <haproxy/dgram.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020038#include <haproxy/dynbuf.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020039#include <haproxy/extcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/fd.h>
41#include <haproxy/global.h>
42#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/istbuf.h>
47#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020048#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/mailers.h>
50#include <haproxy/port_range.h>
51#include <haproxy/proto_tcp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020054#include <haproxy/queue.h>
55#include <haproxy/regex.h>
Emeric Brunc9437992021-02-12 19:42:55 +010056#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020057#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020058#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020059#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020060#include <haproxy/stats-t.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020061#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020062#include <haproxy/tcpcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/thread.h>
64#include <haproxy/time.h>
65#include <haproxy/tools.h>
Christopher Faulet147b8c92021-04-10 09:00:38 +020066#include <haproxy/trace.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020067#include <haproxy/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Christopher Faulet147b8c92021-04-10 09:00:38 +020069/* trace source and events */
70static void check_trace(enum trace_level level, uint64_t mask,
71 const struct trace_source *src,
72 const struct ist where, const struct ist func,
73 const void *a1, const void *a2, const void *a3, const void *a4);
74
75/* The event representation is split like this :
76 * check - check
77 *
78 * CHECK_EV_* macros are defined in <haproxy/check.h>
79 */
80static const struct trace_event check_trace_events[] = {
81 { .mask = CHK_EV_TASK_WAKE, .name = "task_wake", .desc = "Check task woken up" },
82 { .mask = CHK_EV_HCHK_START, .name = "hchck_start", .desc = "Health-check started" },
83 { .mask = CHK_EV_HCHK_WAKE, .name = "hchck_wake", .desc = "Health-check woken up" },
84 { .mask = CHK_EV_HCHK_RUN, .name = "hchck_run", .desc = "Health-check running" },
85 { .mask = CHK_EV_HCHK_END, .name = "hchck_end", .desc = "Health-check terminated" },
86 { .mask = CHK_EV_HCHK_SUCC, .name = "hchck_succ", .desc = "Health-check success" },
87 { .mask = CHK_EV_HCHK_ERR, .name = "hchck_err", .desc = "Health-check failure" },
88
89 { .mask = CHK_EV_TCPCHK_EVAL, .name = "tcp_check_eval", .desc = "tcp-check rules evaluation" },
90 { .mask = CHK_EV_TCPCHK_ERR, .name = "tcp_check_err", .desc = "tcp-check evaluation error" },
91 { .mask = CHK_EV_TCPCHK_CONN, .name = "tcp_check_conn", .desc = "tcp-check connection rule" },
92 { .mask = CHK_EV_TCPCHK_SND, .name = "tcp_check_send", .desc = "tcp-check send rule" },
93 { .mask = CHK_EV_TCPCHK_EXP, .name = "tcp_check_expect", .desc = "tcp-check expect rule" },
94 { .mask = CHK_EV_TCPCHK_ACT, .name = "tcp_check_action", .desc = "tcp-check action rule" },
95
96 { .mask = CHK_EV_RX_DATA, .name = "rx_data", .desc = "receipt of data" },
97 { .mask = CHK_EV_RX_BLK, .name = "rx_blk", .desc = "receipt blocked" },
98 { .mask = CHK_EV_RX_ERR, .name = "rx_err", .desc = "receipt error" },
99
100 { .mask = CHK_EV_TX_DATA, .name = "tx_data", .desc = "transmission of data" },
101 { .mask = CHK_EV_TX_BLK, .name = "tx_blk", .desc = "transmission blocked" },
102 { .mask = CHK_EV_TX_ERR, .name = "tx_err", .desc = "transmission error" },
103
104 {}
105};
106
107static const struct name_desc check_trace_lockon_args[4] = {
108 /* arg1 */ { /* already used by the check */ },
109 /* arg2 */ { },
110 /* arg3 */ { },
111 /* arg4 */ { }
112};
113
114static const struct name_desc check_trace_decoding[] = {
115#define CHK_VERB_CLEAN 1
116 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
117#define CHK_VERB_MINIMAL 2
Willy Tarreau4596fe22022-05-17 19:07:51 +0200118 { .name="minimal", .desc="report info on streams and connectors" },
Christopher Faulet147b8c92021-04-10 09:00:38 +0200119#define CHK_VERB_SIMPLE 3
120 { .name="simple", .desc="add info on request and response channels" },
121#define CHK_VERB_ADVANCED 4
122 { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" },
123#define CHK_VERB_COMPLETE 5
124 { .name="complete", .desc="add info on channel's buffer" },
125 { /* end */ }
126};
127
128struct trace_source trace_check = {
129 .name = IST("check"),
130 .desc = "Health-check",
131 .arg_def = TRC_ARG1_CHK, // TRACE()'s first argument is always a stream
132 .default_cb = check_trace,
133 .known_events = check_trace_events,
134 .lockon_args = check_trace_lockon_args,
135 .decoding = check_trace_decoding,
136 .report_events = ~0, // report everything by default
137};
138
139#define TRACE_SOURCE &trace_check
140INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
141
Olivier Houchard9130a962017-10-17 17:33:43 +0200142
Gaetan Rivet05d692d2020-02-14 17:42:54 +0100143/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200144struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +0200145
Christopher Faulet147b8c92021-04-10 09:00:38 +0200146
147static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
148{
149 size_t block1, block2;
150 int line, ptr, newptr;
151
152 block1 = b_contig_data(buf, ofs);
153 block2 = 0;
154 if (block1 > len)
155 block1 = len;
156 block2 = len - block1;
157
158 ofs = b_peek_ofs(buf, ofs);
159
160 line = 0;
161 ptr = ofs;
162 while (ptr < ofs + block1) {
163 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
164 if (newptr == ptr)
165 break;
166 ptr = newptr;
167 }
168
169 line = ptr = 0;
170 while (ptr < block2) {
171 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
172 if (newptr == ptr)
173 break;
174 ptr = newptr;
175 }
176}
177
178/* trace source and events */
179static void check_trace(enum trace_level level, uint64_t mask,
180 const struct trace_source *src,
181 const struct ist where, const struct ist func,
182 const void *a1, const void *a2, const void *a3, const void *a4)
183{
184 const struct check *check = a1;
185 const struct server *srv = (check ? check->server : NULL);
186 const size_t *val = a4;
187 const char *res;
188
189 if (!check || src->verbosity < CHK_VERB_CLEAN)
190 return;
191
Christopher Faulet4f1825c2022-06-08 09:12:58 +0200192 if (srv) {
193 chunk_appendf(&trace_buf, " : [%c] SRV=%s",
194 ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
195 srv->id);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200196
Christopher Faulet4f1825c2022-06-08 09:12:58 +0200197 chunk_appendf(&trace_buf, " status=%d/%d %s",
198 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
199 (check->health >= check->rise) ? check->fall : check->rise,
200 (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN");
201 }
202 else
203 chunk_appendf(&trace_buf, " : [EMAIL]");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200204
205 switch (check->result) {
206 case CHK_RES_NEUTRAL: res = "-"; break;
207 case CHK_RES_FAILED: res = "FAIL"; break;
208 case CHK_RES_PASSED: res = "PASS"; break;
209 case CHK_RES_CONDPASS: res = "COND"; break;
210 default: res = "UNK"; break;
211 }
212
213 if (src->verbosity == CHK_VERB_CLEAN)
214 return;
215
216 chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
217 get_check_status_info(check->status), check->status,
218 res, check->result);
219
220 /* Display the value to the 4th argument (level > STATE) */
221 if (src->level > TRACE_LEVEL_STATE && val)
222 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
223
224 chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
225
226 if (src->verbosity == CHK_VERB_MINIMAL)
227 return;
228
229
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200230 if (check->sc) {
231 struct connection *conn = sc_conn(check->sc);
Christopher Faulet0256da12021-12-15 09:50:17 +0100232
233 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0);
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200234 chunk_appendf(&trace_buf, " sc=%p(0x%08x)", check->sc, check->sc->flags);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200235 }
236
237 if (mask & CHK_EV_TCPCHK) {
238 const char *type;
239
240 switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) {
241 case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break;
242 case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break;
243 case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break;
244 case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break;
245 case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break;
246 case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break;
247 case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break;
248 case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break;
249 case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break;
250 case TCPCHK_RULES_TCP_CHK: type = "TCP"; break;
251 default: type = "???"; break;
252 }
253 if (check->current_step)
254 chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
255 else
256 chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
257 }
258
259 /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
260 if (src->level > TRACE_LEVEL_USER) {
261 const struct buffer *buf = NULL;
262
263 chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
264 (unsigned int)b_data(&check->bi), b_orig(&check->bi),
265 (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
266 chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
267 (unsigned int)b_data(&check->bo), b_orig(&check->bo),
268 (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
269
270 if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
271 buf = (b_is_null(&check->bi) ? NULL : &check->bi);
272 else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
273 buf = (b_is_null(&check->bo) ? NULL : &check->bo);
274
275 if (buf) {
276 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
277 int full = (src->verbosity == CHK_VERB_COMPLETE);
278
279 chunk_memcat(&trace_buf, "\n\t", 2);
280 htx_dump(&trace_buf, htxbuf(buf), full);
281 }
282 else {
283 int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
284
285 chunk_memcat(&trace_buf, "\n", 1);
286 if (b_data(buf) > max) {
287 check_trace_buf(buf, 0, max);
288 chunk_memcat(&trace_buf, " ...\n", 6);
289 }
290 else
291 check_trace_buf(buf, 0, b_data(buf));
292 }
293
294 }
295 }
296
297}
298
299
Christopher Faulet61cc8522020-04-20 14:54:42 +0200300/**************************************************************************/
301/************************ Handle check results ****************************/
302/**************************************************************************/
303struct check_status {
304 short result; /* one of SRV_CHK_* */
305 char *info; /* human readable short info */
306 char *desc; /* long description */
307};
308
309struct analyze_status {
310 char *desc; /* description */
311 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
312};
313
Simon Horman63a4a822012-03-19 07:24:41 +0900314static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100315 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
316 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200317 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200318
Willy Tarreau23964182014-05-20 20:56:30 +0200319 /* Below we have finished checks */
320 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100321 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100322
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100323 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200324
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100325 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
326 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
327 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200328
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100329 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
330 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
331 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200332
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100333 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
334 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200335
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200336 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200337
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100338 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
339 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
340 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900341
342 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
343 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200344 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200345};
346
Simon Horman63a4a822012-03-19 07:24:41 +0900347static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100348 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
349
350 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
351 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
352
353 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
354 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
355 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
356 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
357
358 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
359 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
360 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
361};
362
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100363/* checks if <err> is a real error for errno or one that can be ignored, and
364 * return 0 for these ones or <err> for real ones.
365 */
366static inline int unclean_errno(int err)
367{
Willy Tarreauacef5e22022-04-25 20:32:15 +0200368 if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS ||
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100369 err == EISCONN || err == EALREADY)
370 return 0;
371 return err;
372}
373
Christopher Faulet7aa32712021-02-01 13:11:50 +0100374/* Converts check_status code to result code */
375short get_check_status_result(short check_status)
376{
377 if (check_status < HCHK_STATUS_SIZE)
378 return check_statuses[check_status].result;
379 else
380 return check_statuses[HCHK_STATUS_UNKNOWN].result;
381}
382
Christopher Faulet61cc8522020-04-20 14:54:42 +0200383/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200384const char *get_check_status_description(short check_status) {
385
386 const char *desc;
387
388 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200389 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200390 else
391 desc = NULL;
392
393 if (desc && *desc)
394 return desc;
395 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200396 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200397}
398
Christopher Faulet61cc8522020-04-20 14:54:42 +0200399/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100400const char *get_check_status_info(short check_status)
401{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200402 const char *info;
403
404 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200405 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200406 else
407 info = NULL;
408
409 if (info && *info)
410 return info;
411 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200412 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200413}
414
Christopher Faulet61cc8522020-04-20 14:54:42 +0200415/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100416const char *get_analyze_status(short analyze_status) {
417
418 const char *desc;
419
420 if (analyze_status < HANA_STATUS_SIZE)
421 desc = analyze_statuses[analyze_status].desc;
422 else
423 desc = NULL;
424
425 if (desc && *desc)
426 return desc;
427 else
428 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
429}
430
Christopher Faulet61cc8522020-04-20 14:54:42 +0200431/* Sets check->status, update check->duration and fill check->result with an
432 * adequate CHK_RES_* value. The new check->health is computed based on the
433 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200434 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200435 * Shows information in logs about failed health check if server is UP or
436 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200437 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200438void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100439{
Simon Horman4a741432013-02-23 15:35:38 +0900440 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200441 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200442 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900443
Christopher Faulet147b8c92021-04-10 09:00:38 +0200444 TRACE_POINT(CHK_EV_HCHK_RUN, check);
445
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200446 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100447 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900448 check->desc[0] = '\0';
449 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200450 return;
451 }
452
Simon Horman4a741432013-02-23 15:35:38 +0900453 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200454 return;
455
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200456 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900457 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
458 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200459 } else
Simon Horman4a741432013-02-23 15:35:38 +0900460 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200461
Simon Horman4a741432013-02-23 15:35:38 +0900462 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200463 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900464 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200465
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100466 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900467 check->duration = -1;
468 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200469 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900470 check->duration = tv_ms_elapsed(&check->start, &now);
471 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200472 }
473
Willy Tarreau23964182014-05-20 20:56:30 +0200474 /* no change is expected if no state change occurred */
475 if (check->result == CHK_RES_NEUTRAL)
476 return;
477
Olivier Houchard0923fa42019-01-11 18:43:04 +0100478 /* If the check was really just sending a mail, it won't have an
479 * associated server, so we're done now.
480 */
481 if (!s)
482 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200483 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200484
Christopher Faulet147b8c92021-04-10 09:00:38 +0200485
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200486 switch (check->result) {
487 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200488 /* Failure to connect to the agent as a secondary check should not
489 * cause the server to be marked down.
490 */
491 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900492 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200493 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200494 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200495 report = 1;
496 check->health--;
497 if (check->health < check->rise)
498 check->health = 0;
499 }
500 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200501
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200502 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100503 case CHK_RES_CONDPASS:
504 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200505 report = 1;
506 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200507
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200508 if (check->health >= check->rise)
509 check->health = check->rise + check->fall - 1; /* OK now */
510 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200511
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200512 /* clear consecutive_errors if observing is enabled */
513 if (s->onerror)
514 s->consecutive_errors = 0;
515 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100516
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200517 default:
518 break;
519 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200520
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200521 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
522 (status != prev_status || report)) {
523 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200524 "%s check for %sserver %s/%s %s%s",
525 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200526 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100527 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100528 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200529 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200530
Emeric Brun5a133512017-10-19 14:42:30 +0200531 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200532
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100533 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200534 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
535 (check->health >= check->rise) ? check->fall : check->rise,
536 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200537
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 ha_warning("%s.\n", trash.area);
539 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
540 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200541 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200542}
543
Willy Tarreau4eec5472014-05-20 22:32:27 +0200544/* Marks the check <check>'s server down if the current check is already failed
545 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200547void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548{
Simon Horman4a741432013-02-23 15:35:38 +0900549 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900550
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200551 /* The agent secondary check should only cause a server to be marked
552 * as down if check->status is HCHK_STATUS_L7STS, which indicates
553 * that the agent returned "fail", "stopped" or "down".
554 * The implication here is that failure to connect to the agent
555 * as a secondary check should not cause the server to be marked
556 * down. */
557 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
558 return;
559
Willy Tarreau4eec5472014-05-20 22:32:27 +0200560 if (check->health > 0)
561 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100562
Christopher Faulet147b8c92021-04-10 09:00:38 +0200563 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200564 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200565 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566}
567
Willy Tarreauaf549582014-05-16 17:37:50 +0200568/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200569 * it isn't in maintenance, it is not tracking a down server and other checks
570 * comply. The rule is simple : by default, a server is up, unless any of the
571 * following conditions is true :
572 * - health check failed (check->health < rise)
573 * - agent check failed (agent->health < rise)
574 * - the server tracks a down server (track && track->state == STOPPED)
575 * Note that if the server has a slowstart, it will switch to STARTING instead
576 * of RUNNING. Also, only the health checks support the nolb mode, so the
577 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200578 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200579void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200580{
Simon Horman4a741432013-02-23 15:35:38 +0900581 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100582
Emeric Brun52a91d32017-08-31 14:41:55 +0200583 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200584 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100585
Emeric Brun52a91d32017-08-31 14:41:55 +0200586 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200587 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100588
Willy Tarreau3e048382014-05-21 10:30:54 +0200589 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
590 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100591
Willy Tarreau3e048382014-05-21 10:30:54 +0200592 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
593 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200594
Emeric Brun52a91d32017-08-31 14:41:55 +0200595 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200596 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100597
Christopher Faulet147b8c92021-04-10 09:00:38 +0200598 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Emeric Brun5a133512017-10-19 14:42:30 +0200599 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100600}
601
Willy Tarreaudb58b792014-05-21 13:57:23 +0200602/* Marks the check <check> as valid and tries to set its server into stopping mode
603 * if it was running or starting, and provided it isn't in maintenance and other
604 * checks comply. The conditions for the server to be marked in stopping mode are
605 * the same as for it to be turned up. Also, only the health checks support the
606 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200607 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200608void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200609{
Simon Horman4a741432013-02-23 15:35:38 +0900610 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100611
Emeric Brun52a91d32017-08-31 14:41:55 +0200612 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200613 return;
614
Willy Tarreaudb58b792014-05-21 13:57:23 +0200615 if (check->state & CHK_ST_AGENT)
616 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100617
Emeric Brun52a91d32017-08-31 14:41:55 +0200618 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200619 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100620
Willy Tarreaudb58b792014-05-21 13:57:23 +0200621 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
622 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100623
Willy Tarreaudb58b792014-05-21 13:57:23 +0200624 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
625 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100626
Christopher Faulet147b8c92021-04-10 09:00:38 +0200627 TRACE_STATE("health-check condionnaly succeeded, set server STOPPING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Willy Tarreaub26881a2017-12-23 11:16:49 +0100628 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100629}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200630
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100631/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100632 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100633 */
634void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100635{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100636 int failed;
637 int expire;
638
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100639 if (s->observe >= HANA_OBS_SIZE)
640 return;
641
Willy Tarreaubb956662013-01-24 00:37:39 +0100642 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100643 return;
644
645 switch (analyze_statuses[status].lr[s->observe - 1]) {
646 case 1:
647 failed = 1;
648 break;
649
650 case 2:
651 failed = 0;
652 break;
653
654 default:
655 return;
656 }
657
658 if (!failed) {
659 /* good: clear consecutive_errors */
660 s->consecutive_errors = 0;
661 return;
662 }
663
Willy Tarreau4781b152021-04-06 13:53:36 +0200664 _HA_ATOMIC_INC(&s->consecutive_errors);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100665
666 if (s->consecutive_errors < s->consecutive_errors_limit)
667 return;
668
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100669 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
670 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100671
Willy Tarreau4e9df272021-02-17 15:20:19 +0100672 if (s->check.fastinter)
673 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
674 else
675 expire = TICK_ETERNITY;
676
677 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
678
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100679 switch (s->onerror) {
680 case HANA_ONERR_FASTINTER:
681 /* force fastinter - nothing to do here as all modes force it */
682 break;
683
684 case HANA_ONERR_SUDDTH:
685 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900686 if (s->check.health > s->check.rise)
687 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100688
Tim Duesterhus588b3142020-05-29 14:35:51 +0200689 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100690
691 case HANA_ONERR_FAILCHK:
692 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 set_server_check_status(&s->check, HCHK_STATUS_HANA,
694 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200695 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100696 break;
697
698 case HANA_ONERR_MARKDWN:
699 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900700 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200701 set_server_check_status(&s->check, HCHK_STATUS_HANA,
702 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200703 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100704 break;
705
706 default:
707 /* write a warning? */
708 break;
709 }
710
Willy Tarreau4e9df272021-02-17 15:20:19 +0100711 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
712
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100713 s->consecutive_errors = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200714 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100715
Christopher Fauletea860832021-05-07 11:45:26 +0200716 if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) {
Willy Tarreau4e9df272021-02-17 15:20:19 +0100717 /* requeue check task with new expire */
718 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100719 }
Willy Tarreauef781042010-01-27 11:53:01 +0100720}
721
Christopher Faulet61cc8522020-04-20 14:54:42 +0200722/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100723 * closed, keep errno intact as it is supposed to contain the valid error code.
724 * If no error is reported, check the socket's error queue using getsockopt().
725 * Warning, this must be done only once when returning from poll, and never
726 * after an I/O error was attempted, otherwise the error queue might contain
727 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
728 * socket. Returns non-zero if an error was reported, zero if everything is
729 * clean (including a properly closed socket).
730 */
731static int retrieve_errno_from_socket(struct connection *conn)
732{
733 int skerr;
734 socklen_t lskerr = sizeof(skerr);
735
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100736 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100737 return 1;
738
Willy Tarreau3c728722014-01-23 13:50:42 +0100739 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100740 return 0;
741
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200742 BUG_ON(conn->flags & CO_FL_FDLESS);
743
Willy Tarreau585744b2017-08-24 14:31:19 +0200744 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100745 errno = skerr;
746
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100747 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100748
749 if (!errno) {
750 /* we could not retrieve an error, that does not mean there is
751 * none. Just don't change anything and only report the prior
752 * error if any.
753 */
754 if (conn->flags & CO_FL_ERROR)
755 return 1;
756 else
757 return 0;
758 }
759
760 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
761 return 1;
762}
763
Christopher Faulet61cc8522020-04-20 14:54:42 +0200764/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100765 * and adjust the server status accordingly. It may make use of <errno_bck>
766 * if non-null when the caller is absolutely certain of its validity (eg:
767 * checked just after a syscall). If the caller doesn't have a valid errno,
768 * it can pass zero, and retrieve_errno_from_socket() will be called to try
769 * to extract errno from the socket. If no error is reported, it will consider
770 * the <expired> flag. This is intended to be used when a connection error was
771 * reported in conn->flags or when a timeout was reported in <expired>. The
772 * function takes care of not updating a server status which was already set.
773 * All situations where at least one of <expired> or CO_FL_ERROR are set
774 * produce a status.
775 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200776void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100777{
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200778 struct stconn *sc = check->sc;
779 struct connection *conn = sc_conn(sc);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100780 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200781 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200782 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100783
Christopher Faulet147b8c92021-04-10 09:00:38 +0200784 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100785 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200786 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100787
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100788 errno = unclean_errno(errno_bck);
789 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100790 retrieve_errno_from_socket(conn);
791
Christopher Faulete4b40192022-06-13 07:59:46 +0200792 if (conn && !(conn->flags & CO_FL_ERROR) && !sc_ep_test(sc, SE_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100793 return;
794
Christopher Faulet147b8c92021-04-10 09:00:38 +0200795 TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
796
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100797 /* we'll try to build a meaningful error message depending on the
798 * context of the error possibly present in conn->err_code, and the
799 * socket error possibly collected above. This is useful to know the
800 * exact step of the L6 layer (eg: SSL handshake).
801 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200802 chk = get_trash_chunk();
803
Christopher Faulet799f3a42020-04-07 12:06:14 +0200804 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200805 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200806 step = tcpcheck_get_step_id(check, NULL);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200807 if (!step) {
808 TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200809 chunk_printf(chk, " at initial connection step of tcp-check");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200810 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200811 else {
812 chunk_printf(chk, " at step %d of tcp-check", step);
813 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200814 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
815 if (check->current_step->connect.port)
816 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200817 else
818 chunk_appendf(chk, " (connect)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200819 TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200820 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200821 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
822 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100823
824 switch (expect->type) {
825 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200826 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100827 break;
828 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200829 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100830 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200831 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200832 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100833 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200834 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100835 chunk_appendf(chk, " (expect binary regex)");
836 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200837 case TCPCHK_EXPECT_STRING_LF:
838 chunk_appendf(chk, " (expect log-format string)");
839 break;
840 case TCPCHK_EXPECT_BINARY_LF:
841 chunk_appendf(chk, " (expect log-format binary)");
842 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200843 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200844 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200845 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200846 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200847 chunk_appendf(chk, " (expect HTTP status regex)");
848 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200849 case TCPCHK_EXPECT_HTTP_HEADER:
850 chunk_appendf(chk, " (expect HTTP header pattern)");
851 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200852 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200853 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200854 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200855 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200856 chunk_appendf(chk, " (expect HTTP body regex)");
857 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200858 case TCPCHK_EXPECT_HTTP_BODY_LF:
859 chunk_appendf(chk, " (expect log-format HTTP body)");
860 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200861 case TCPCHK_EXPECT_CUSTOM:
862 chunk_appendf(chk, " (expect custom function)");
863 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100864 case TCPCHK_EXPECT_UNDEF:
865 chunk_appendf(chk, " (undefined expect!)");
866 break;
867 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200868 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200869 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200870 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200871 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200872 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200873 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200874
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200875 if (check->current_step && check->current_step->comment)
876 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200877 }
878 }
879
Willy Tarreau00149122017-10-04 18:05:01 +0200880 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100881 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200882 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
883 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100884 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200885 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
886 chk->area);
887 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100888 }
889 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100890 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200891 chunk_printf(&trash, "%s%s", strerror(errno),
892 chk->area);
893 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100894 }
895 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200896 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100897 }
898 }
899
Willy Tarreau00149122017-10-04 18:05:01 +0200900 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200901 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200902 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
903 }
904
Christopher Faulete4b40192022-06-13 07:59:46 +0200905 if (!conn || !conn->ctrl) {
Christopher Faulet5e293762020-10-26 11:10:49 +0100906 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200907 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
908 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100909 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100910 /* L4 not established (yet) */
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200911 if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100912 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
913 else if (expired)
914 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200915
916 /*
917 * might be due to a server IP change.
918 * Let's trigger a DNS resolution if none are currently running.
919 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100920 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100921 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200922
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100923 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100924 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100925 /* L6 not established (yet) */
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200926 if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100927 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
928 else if (expired)
929 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
930 }
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200931 else if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100932 /* I/O error after connection was established and before we could diagnose */
933 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
934 }
935 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200936 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
937
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100938 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200939 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
940 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200941 tout = check->current_step->expect.tout_status;
942 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100943 }
944
Christopher Faulet147b8c92021-04-10 09:00:38 +0200945 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100946 return;
947}
948
Simon Horman98637e52014-06-20 12:30:16 +0900949
Christopher Faulet61cc8522020-04-20 14:54:42 +0200950/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200951int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900952{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200953 int sv_state;
954 int ratio;
955 char addr[46];
956 char port[6];
957 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
958 "UP %d/%d", "UP",
959 "NOLB %d/%d", "NOLB",
960 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900961
Christopher Faulet61cc8522020-04-20 14:54:42 +0200962 if (!(s->check.state & CHK_ST_ENABLED))
963 sv_state = 6;
964 else if (s->cur_state != SRV_ST_STOPPED) {
965 if (s->check.health == s->check.rise + s->check.fall - 1)
966 sv_state = 3; /* UP */
967 else
968 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900969
Christopher Faulet61cc8522020-04-20 14:54:42 +0200970 if (s->cur_state == SRV_ST_STOPPING)
971 sv_state += 2;
972 } else {
973 if (s->check.health)
974 sv_state = 1; /* going up */
975 else
976 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900977 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200978
Christopher Faulet61cc8522020-04-20 14:54:42 +0200979 chunk_appendf(buf, srv_hlt_st[sv_state],
980 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
981 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200982
Christopher Faulet61cc8522020-04-20 14:54:42 +0200983 addr_to_str(&s->addr, addr, sizeof(addr));
984 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
985 snprintf(port, sizeof(port), "%u", s->svc_port);
986 else
987 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200988
Christopher Faulet61cc8522020-04-20 14:54:42 +0200989 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
990 addr, port, s->proxy->id, s->id,
991 global.node,
992 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
993 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreau7f3c1df2021-06-18 09:22:21 +0200994 s->cur_sess, s->proxy->beconn - s->proxy->queue.length,
Willy Tarreaua0570452021-06-18 09:30:30 +0200995 s->queue.length);
Willy Tarreau9f6dc722019-03-01 11:15:10 +0100996
Christopher Faulet61cc8522020-04-20 14:54:42 +0200997 if ((s->cur_state == SRV_ST_STARTING) &&
998 now.tv_sec < s->last_change + s->slowstart &&
999 now.tv_sec >= s->last_change) {
1000 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
1001 chunk_appendf(buf, "; throttle=%d%%", ratio);
1002 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001003
Christopher Faulet61cc8522020-04-20 14:54:42 +02001004 return b_data(buf);
1005}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001006
Willy Tarreau51cd5952020-06-05 12:25:38 +02001007/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001008/***************** Health-checks based on connections *********************/
1009/**************************************************************************/
1010/* This function is used only for server health-checks. It handles connection
1011 * status updates including errors. If necessary, it wakes the check task up.
1012 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1013 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001014 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001015int wake_srv_chk(struct stconn *sc)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001016{
Christopher Faulet0256da12021-12-15 09:50:17 +01001017 struct connection *conn;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001018 struct check *check = __sc_check(sc);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001019 struct email_alertq *q = container_of(check, typeof(*q), check);
1020 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001021
Christopher Faulet147b8c92021-04-10 09:00:38 +02001022 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001023 if (check->result != CHK_RES_UNKNOWN)
1024 goto end;
1025
Willy Tarreau51cd5952020-06-05 12:25:38 +02001026 if (check->server)
1027 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1028 else
1029 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001030
Willy Tarreau51cd5952020-06-05 12:25:38 +02001031 /* we may have to make progress on the TCP checks */
1032 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001033
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001034 sc = check->sc;
1035 conn = sc_conn(sc);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001036
Christopher Faulete4b40192022-06-13 07:59:46 +02001037 if (unlikely(!conn || conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001038 /* We may get error reports bypassing the I/O handlers, typically
1039 * the case when sending a pure TCP check which fails, then the I/O
1040 * handlers above are not called. This is completely handled by the
1041 * main processing task so let's simply wake it up. If we get here,
1042 * we expect errno to still be valid.
1043 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001044 TRACE_ERROR("report connection error", CHK_EV_HCHK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001045 chk_report_conn_err(check, errno, 0);
1046 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001047 }
1048
Christopher Faulet8f100422021-01-18 15:47:03 +01001049 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001050 /* Check complete or aborted. Wake the check task up to be sure
1051 * the result is handled ASAP. */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001052 ret = -1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001053 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001054 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001055
Willy Tarreau51cd5952020-06-05 12:25:38 +02001056 if (check->server)
1057 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1058 else
1059 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001060
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001061 end:
Christopher Faulet147b8c92021-04-10 09:00:38 +02001062 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001063 return ret;
1064}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001065
Willy Tarreau51cd5952020-06-05 12:25:38 +02001066/* This function checks if any I/O is wanted, and if so, attempts to do so */
Christopher Faulet361417f2022-05-18 14:50:30 +02001067struct task *srv_chk_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001068{
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001069 struct stconn *sc = ctx;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001070
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001071 wake_srv_chk(sc);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001072 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001073}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001074
Willy Tarreau51cd5952020-06-05 12:25:38 +02001075/* manages a server health-check that uses a connection. Returns
1076 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001077 *
1078 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001079 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001080 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001081struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001082{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001083 struct check *check = context;
1084 struct proxy *proxy = check->proxy;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001085 struct stconn *sc;
Christopher Faulet92017a32021-05-06 16:01:18 +02001086 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001087 int rv;
1088 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001089
Christopher Faulet147b8c92021-04-10 09:00:38 +02001090 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1091
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001092 if (check->state & CHK_ST_SLEEPING) {
1093 /* This check just restarted. It's still time to verify if
1094 * we're on an overloaded thread or if a more suitable one is
1095 * available. This helps spread the load over the available
1096 * threads, without migrating too often. For this we'll check
1097 * our load, and pick a random thread, check if it has less
1098 * than half of the current thread's load, and if so we'll
1099 * bounce the task there. It's possible because it's not yet
1100 * tied to the current thread. The other thread will not bounce
1101 * the task again because we're removing CHK_ST_SLEEPING.
1102 */
1103 uint my_load = HA_ATOMIC_LOAD(&th_ctx->rq_total);
1104
1105 check->state &= ~CHK_ST_SLEEPING;
1106
1107 if (my_load >= 2) {
1108 uint new_tid = statistical_prng_range(global.nbthread);
1109 uint new_load = HA_ATOMIC_LOAD(&ha_thread_ctx[new_tid].rq_total);
1110
1111 if (new_load <= my_load / 2) {
1112 /* Found one. Let's migrate the task over there. We have to
1113 * remove it from the WQ first and kill its expire time
1114 * otherwise the scheduler will reinsert it and trigger a
1115 * BUG_ON() as we're not allowed to call task_queue() for a
1116 * foreign thread. The recipient will restore the expiration.
1117 */
1118 task_unlink_wq(t);
1119 t->expire = TICK_ETERNITY;
1120 task_set_thread(t, new_tid);
1121 task_wakeup(t, TASK_WOKEN_MSG);
1122 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
1123 return t;
1124 }
1125 }
1126 }
1127
Willy Tarreau51cd5952020-06-05 12:25:38 +02001128 if (check->server)
1129 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001130
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001131 if (!(check->state & (CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC))) {
1132 /* This task might have bounced from another overloaded thread, it
1133 * needs an expiration timer that was supposed to be now, but that
1134 * was erased during the bounce.
1135 */
1136 if (!tick_isset(t->expire))
1137 t->expire = now_ms;
1138 }
1139
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001140 if (unlikely(check->state & CHK_ST_PURGE)) {
1141 TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1142 }
1143 else if (!(check->state & (CHK_ST_INPROGRESS))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001144 /* no check currently running */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001145 if (!expired) /* woke up too early */ {
1146 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001147 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001148 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001149
Willy Tarreau51cd5952020-06-05 12:25:38 +02001150 /* we don't send any health-checks when the proxy is
1151 * stopped, the server should not be checked or the check
1152 * is disabled.
1153 */
1154 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001155 (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001156 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001157 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001158 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001159
Willy Tarreau51cd5952020-06-05 12:25:38 +02001160 /* we'll initiate a new check */
1161 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001162
Willy Tarreau51cd5952020-06-05 12:25:38 +02001163 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001164 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001165
Willy Tarreaueed39112022-06-15 17:20:16 +02001166 task_set_thread(t, tid);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001167
1168 check->current_step = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001169
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001170 check->sc = sc_new_from_check(check, SC_FL_NONE);
1171 if (!check->sc) {
Christopher Faulet177a0e62022-04-12 17:47:07 +02001172 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1173 goto end;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001174 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001175 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001176 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001177 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001178
Christopher Faulet92017a32021-05-06 16:01:18 +02001179 /* there was a test running.
1180 * First, let's check whether there was an uncaught error,
1181 * which can happen on connect timeout or error.
1182 */
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001183 if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001184 sc = check->sc;
Christopher Faulete4b40192022-06-13 07:59:46 +02001185 conn = sc_conn(sc);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001186
Christopher Faulet92017a32021-05-06 16:01:18 +02001187 /* Here the connection must be defined. Otherwise the
1188 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001189 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001190 if ((conn && ((conn->flags & CO_FL_ERROR) || sc_ep_test(sc, SE_FL_ERROR))) || expired) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001191 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1192 chk_report_conn_err(check, 0, expired);
1193 }
1194 else {
1195 if (check->state & CHK_ST_CLOSE_CONN) {
1196 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001197 check->state &= ~CHK_ST_CLOSE_CONN;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001198 conn = NULL;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001199 if (!sc_reset_endp(check->sc)) {
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001200 /* error will be handled by tcpcheck_main().
Willy Tarreaub605c422022-05-17 17:04:55 +02001201 * On success, remove all flags except SE_FL_DETACHED
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001202 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001203 sc_ep_clr(check->sc, ~SE_FL_DETACHED);
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001204 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001205 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001206 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001207 if (check->result == CHK_RES_UNKNOWN) {
1208 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1209 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001210 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001211 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001212 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001213
Christopher Faulet92017a32021-05-06 16:01:18 +02001214 /* check complete or aborted */
1215 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001216
Christopher Faulete4b40192022-06-13 07:59:46 +02001217 /* check->sc may be NULL when the healthcheck is purged */
Christopher Faulet92017a32021-05-06 16:01:18 +02001218 check->current_step = NULL;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001219 sc = check->sc;
1220 conn = (sc ? sc_conn(sc) : NULL);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001221
Christopher Faulet92017a32021-05-06 16:01:18 +02001222 if (conn && conn->xprt) {
1223 /* The check was aborted and the connection was not yet closed.
1224 * This can happen upon timeout, or when an external event such
1225 * as a failed response coupled with "observe layer7" caused the
1226 * server state to be suddenly changed.
1227 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001228 sc_conn_drain_and_shut(sc);
Christopher Faulet92017a32021-05-06 16:01:18 +02001229 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001230
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001231 if (sc) {
1232 sc_destroy(sc);
1233 sc = check->sc = NULL;
Christopher Faulet177a0e62022-04-12 17:47:07 +02001234 conn = NULL;
1235 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001236
Christopher Faulet92017a32021-05-06 16:01:18 +02001237 if (check->sess != NULL) {
1238 vars_prune(&check->vars, check->sess, NULL);
1239 session_free(check->sess);
1240 check->sess = NULL;
1241 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001242
Christopher Fauletb041b232022-03-24 10:27:02 +01001243 end:
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001244 if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001245 if (check->result == CHK_RES_FAILED) {
1246 /* a failure or timeout detected */
1247 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1248 check_notify_failure(check);
1249 }
1250 else if (check->result == CHK_RES_CONDPASS) {
1251 /* check is OK but asks for stopping mode */
1252 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1253 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001254 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001255 else if (check->result == CHK_RES_PASSED) {
1256 /* a success was detected */
1257 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1258 check_notify_success(check);
1259 }
1260 }
Christopher Faulet6d781f62022-05-18 14:24:43 +02001261
1262 if (LIST_INLIST(&check->buf_wait.list))
1263 LIST_DEL_INIT(&check->buf_wait.list);
1264
Christopher Faulet92017a32021-05-06 16:01:18 +02001265 check_release_buf(check, &check->bi);
1266 check_release_buf(check, &check->bo);
1267 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001268 check->state |= CHK_ST_SLEEPING;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001269
Christopher Faulet92017a32021-05-06 16:01:18 +02001270 if (check->server) {
1271 rv = 0;
1272 if (global.spread_checks > 0) {
1273 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreaua840b4a2022-10-12 21:48:17 +02001274 rv -= (int) (2 * rv * (statistical_prng() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001275 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001276 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001277 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001278
1279 reschedule:
1280 while (tick_is_expired(t->expire, now_ms))
1281 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1282 out_unlock:
1283 if (check->server)
1284 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001285
1286 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001287
1288 /* Free the check if set to PURGE. After this, the check instance may be
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001289 * freed via the srv_drop invocation, so it must not be accessed after
1290 * this point.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001291 */
1292 if (unlikely(check->state & CHK_ST_PURGE)) {
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001293 free_check(check);
Amaury Denoyelle9ba34ae2021-08-09 15:09:17 +02001294 if (check->server)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001295 srv_drop(check->server);
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001296
1297 t = NULL;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001298 }
1299
Willy Tarreau51cd5952020-06-05 12:25:38 +02001300 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001301}
1302
Willy Tarreau51cd5952020-06-05 12:25:38 +02001303
Christopher Faulet61cc8522020-04-20 14:54:42 +02001304/**************************************************************************/
1305/************************** Init/deinit checks ****************************/
1306/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001307/*
1308 * Tries to grab a buffer and to re-enables processing on check <target>. The
1309 * check flags are used to figure what buffer was requested. It returns 1 if the
1310 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1311 * impossible to wake up and we prefer to be woken up later.
1312 */
1313int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001314{
Christopher Fauletb381a502020-11-25 13:47:00 +01001315 struct check *check = target;
1316
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001317 BUG_ON(!check->sc);
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001318
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001319 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001320 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001321 check->state &= ~CHK_ST_IN_ALLOC;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001322 tasklet_wakeup(check->sc->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001323 return 1;
1324 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001325 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001326 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001327 check->state &= ~CHK_ST_OUT_ALLOC;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001328 tasklet_wakeup(check->sc->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001329 return 1;
1330 }
1331
1332 return 0;
1333}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001334
Christopher Fauletb381a502020-11-25 13:47:00 +01001335/*
William Dauchyf4300902021-02-06 20:47:50 +01001336 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001337 */
1338struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1339{
1340 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001341
Willy Tarreau2b718102021-04-21 07:32:39 +02001342 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001343 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001344 check->buf_wait.target = check;
1345 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +02001346 LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001347 }
1348 return buf;
1349}
1350
1351/*
1352 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1353 * wait queue.
1354 */
1355void check_release_buf(struct check *check, struct buffer *bptr)
1356{
1357 if (bptr->size) {
1358 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001359 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001360 }
1361}
1362
1363const char *init_check(struct check *check, int type)
1364{
1365 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001366
Christopher Fauletb381a502020-11-25 13:47:00 +01001367 check->bi = BUF_NULL;
1368 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001369 LIST_INIT(&check->buf_wait.list);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001370 return NULL;
1371}
1372
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001373/* Liberates the resources allocated for a check.
1374 *
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001375 * This function must only be run by the thread owning the check.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001376 */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001377void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001378{
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001379 /* For agent-check, free the rules / vars from the server. This is not
1380 * done for health-check : the proxy is the owner of the rules / vars
1381 * in this case.
1382 */
1383 if (check->state & CHK_ST_AGENT) {
1384 free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars);
1385 ha_free(&check->tcpcheck_rules);
1386 }
1387
Christopher Faulet61cc8522020-04-20 14:54:42 +02001388 task_destroy(check->task);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001389
Christopher Fauletb381a502020-11-25 13:47:00 +01001390 check_release_buf(check, &check->bi);
1391 check_release_buf(check, &check->bo);
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001392 if (check->sc) {
1393 sc_destroy(check->sc);
1394 check->sc = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001395 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001396}
1397
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001398/* This function must be used in order to free a started check. The check will
1399 * be scheduled for a next execution in order to properly close and free all
1400 * check elements.
1401 *
1402 * Non thread-safe.
1403 */
1404void check_purge(struct check *check)
1405{
Amaury Denoyelle25fe1032021-08-10 16:21:55 +02001406 check->state |= CHK_ST_PURGE;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001407 task_wakeup(check->task, TASK_WOKEN_OTHER);
1408}
1409
Christopher Faulet61cc8522020-04-20 14:54:42 +02001410/* manages a server health-check. Returns the time the task accepts to wait, or
1411 * TIME_ETERNITY for infinity.
1412 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001413struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001414{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001415 struct check *check = context;
1416
1417 if (check->type == PR_O2_EXT_CHK)
1418 return process_chk_proc(t, context, state);
1419 return process_chk_conn(t, context, state);
1420
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001421}
1422
Christopher Faulet61cc8522020-04-20 14:54:42 +02001423
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001424int start_check_task(struct check *check, int mininter,
Christopher Faulet61cc8522020-04-20 14:54:42 +02001425 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001426{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001427 struct task *t;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001428
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001429 /* task for the check. Process-based checks exclusively run on thread 1. */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001430 if (check->type == PR_O2_EXT_CHK)
Willy Tarreaua89c1912021-10-20 18:43:30 +02001431 t = task_new_on(0);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001432 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001433 t = task_new_anywhere();
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001434
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001435 if (!t)
1436 goto fail_alloc_task;
1437
Christopher Faulet61cc8522020-04-20 14:54:42 +02001438 check->task = t;
1439 t->process = process_chk;
1440 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001441
Christopher Faulet61cc8522020-04-20 14:54:42 +02001442 if (mininter < srv_getinter(check))
1443 mininter = srv_getinter(check);
1444
1445 if (global.max_spread_checks && mininter > global.max_spread_checks)
1446 mininter = global.max_spread_checks;
1447
1448 /* check this every ms */
1449 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1450 check->start = now;
1451 task_queue(t);
1452
1453 return 1;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001454
1455 fail_alloc_task:
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001456 ha_alert("Starting [%s:%s] check: out of memory.\n",
1457 check->server->proxy->id, check->server->id);
1458 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001459}
1460
Christopher Faulet61cc8522020-04-20 14:54:42 +02001461/*
1462 * Start health-check.
1463 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1464 */
1465static int start_checks()
1466{
1467
1468 struct proxy *px;
1469 struct server *s;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001470 int nbcheck=0, mininter=0, srvpos=0;
1471
1472 /* 0- init the dummy frontend used to create all checks sessions */
1473 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001474 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001475 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1476 checks_fe.mode = PR_MODE_TCP;
1477 checks_fe.maxconn = 0;
1478 checks_fe.conn_retries = CONN_RETRIES;
1479 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1480 checks_fe.timeout.client = TICK_ETERNITY;
1481
1482 /* 1- count the checkers to run simultaneously.
1483 * We also determine the minimum interval among all of those which
1484 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1485 * will be used to spread their start-up date. Those which have
1486 * a shorter interval will start independently and will not dictate
1487 * too short an interval for all others.
1488 */
1489 for (px = proxies_list; px; px = px->next) {
1490 for (s = px->srv; s; s = s->next) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001491 if (s->check.state & CHK_ST_CONFIGURED) {
1492 nbcheck++;
1493 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1494 (!mininter || mininter > srv_getinter(&s->check)))
1495 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001496 }
1497
Christopher Faulet61cc8522020-04-20 14:54:42 +02001498 if (s->agent.state & CHK_ST_CONFIGURED) {
1499 nbcheck++;
1500 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1501 (!mininter || mininter > srv_getinter(&s->agent)))
1502 mininter = srv_getinter(&s->agent);
1503 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001504 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001505 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001506
Christopher Faulet61cc8522020-04-20 14:54:42 +02001507 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001508 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001509
Christopher Faulet61cc8522020-04-20 14:54:42 +02001510 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001511
William Dauchyf4300902021-02-06 20:47:50 +01001512 /* 2- start them as far as possible from each other. For this, we will
1513 * start them after their interval is set to the min interval divided
1514 * by the number of servers, weighted by the server's position in the
1515 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001516 */
1517 for (px = proxies_list; px; px = px->next) {
1518 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1519 if (init_pid_list()) {
1520 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1521 return ERR_ALERT | ERR_FATAL;
1522 }
1523 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001524
Christopher Faulet61cc8522020-04-20 14:54:42 +02001525 for (s = px->srv; s; s = s->next) {
1526 /* A task for the main check */
1527 if (s->check.state & CHK_ST_CONFIGURED) {
1528 if (s->check.type == PR_O2_EXT_CHK) {
1529 if (!prepare_external_check(&s->check))
1530 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001531 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001532 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1533 return ERR_ALERT | ERR_FATAL;
1534 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001535 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001536
Christopher Faulet61cc8522020-04-20 14:54:42 +02001537 /* A task for a auxiliary agent check */
1538 if (s->agent.state & CHK_ST_CONFIGURED) {
1539 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1540 return ERR_ALERT | ERR_FATAL;
1541 }
1542 srvpos++;
1543 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001544 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001545 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001546 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001547}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001548
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001549
Christopher Faulet61cc8522020-04-20 14:54:42 +02001550/*
1551 * Return value:
1552 * the port to be used for the health check
1553 * 0 in case no port could be found for the check
1554 */
1555static int srv_check_healthcheck_port(struct check *chk)
1556{
1557 int i = 0;
1558 struct server *srv = NULL;
1559
1560 srv = chk->server;
1561
William Dauchyf4300902021-02-06 20:47:50 +01001562 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001563 if (chk->port > 0)
1564 return chk->port;
1565
1566 /* try to get the port from check_core.addr if check.port not set */
1567 i = get_host_port(&chk->addr);
1568 if (i > 0)
1569 return i;
1570
1571 /* try to get the port from server address */
1572 /* prevent MAPPORTS from working at this point, since checks could
1573 * not be performed in such case (MAPPORTS impose a relative ports
1574 * based on live traffic)
1575 */
1576 if (srv->flags & SRV_F_MAPPORTS)
1577 return 0;
1578
1579 i = srv->svc_port; /* by default */
1580 if (i > 0)
1581 return i;
1582
1583 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001584}
1585
Christopher Faulet61cc8522020-04-20 14:54:42 +02001586/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1587 * if an error occurred.
1588 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001589int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001590{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001591 const char *err;
1592 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001593 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001594 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001595
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001596 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001597 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001598
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001599 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001600
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001601 if (!(srv->flags & SRV_F_DYNAMIC)) {
1602 /* If neither a port nor an addr was specified and no check
1603 * transport layer is forced, then the transport layer used by
1604 * the checks is the same as for the production traffic.
1605 * Otherwise we use raw_sock by default, unless one is
1606 * specified.
1607 */
1608 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1609 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1610 srv->check.use_ssl = srv->use_ssl;
1611 srv->check.xprt = srv->xprt;
1612 }
1613 else if (srv->check.use_ssl == 1)
1614 srv->check.xprt = xprt_get(XPRT_SSL);
1615 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001616 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001617 else if (srv->check.use_ssl == 1)
1618 srv->check.xprt = xprt_get(XPRT_SSL);
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001619 }
1620 else {
1621 /* For dynamic servers, check-ssl and check-send-proxy must be
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001622 * explicitly defined even if the check port was not
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001623 * overridden.
1624 */
1625 if (srv->check.use_ssl == 1)
1626 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001627 }
1628
Christopher Faulet12882cf2020-04-23 15:50:18 +02001629 /* Inherit the mux protocol from the server if not already defined for
1630 * the check
1631 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001632 if (srv->mux_proto && !srv->check.mux_proto &&
1633 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1634 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001635 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001636 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001637 /* test that check proto is valid if explicitly defined */
1638 else if (srv->check.mux_proto &&
1639 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1640 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1641 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1642 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1643 ret |= ERR_ALERT | ERR_FATAL;
1644 goto out;
1645 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001646
Christopher Faulet61cc8522020-04-20 14:54:42 +02001647 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001648
Christopher Faulet61cc8522020-04-20 14:54:42 +02001649 /* We need at least a service port, a check port or the first tcp-check
1650 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1651 */
1652 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1653 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1654 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001655
Christopher Faulet61cc8522020-04-20 14:54:42 +02001656 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1657 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1658 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1659 ret |= ERR_ALERT | ERR_ABORT;
1660 goto out;
1661 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001662
Christopher Faulet61cc8522020-04-20 14:54:42 +02001663 /* search the first action (connect / send / expect) in the list */
1664 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1665 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1666 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1667 "nor tcp_check rule 'connect' with port information.\n",
1668 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1669 ret |= ERR_ALERT | ERR_ABORT;
1670 goto out;
1671 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001672
Christopher Faulet61cc8522020-04-20 14:54:42 +02001673 /* scan the tcp-check ruleset to ensure a port has been configured */
1674 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreauacff3092021-07-22 11:06:41 +02001675 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001676 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1677 "and a tcp_check rule 'connect' with no port information.\n",
1678 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1679 ret |= ERR_ALERT | ERR_ABORT;
1680 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001681 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001682 }
1683
Christopher Faulet61cc8522020-04-20 14:54:42 +02001684 init:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001685 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1686 if (err) {
1687 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1688 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1689 ret |= ERR_ALERT | ERR_ABORT;
1690 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001691 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001692 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001693 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001694
1695 /* Only increment maxsock for servers from the configuration. Dynamic
1696 * servers at the moment are not taken into account for the estimation
1697 * of the resources limits.
1698 */
1699 if (global.mode & MODE_STARTING)
1700 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001701
Christopher Faulet61cc8522020-04-20 14:54:42 +02001702 out:
1703 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001704}
1705
Christopher Faulet61cc8522020-04-20 14:54:42 +02001706/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1707 * if an error occurred.
1708 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001709int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001710{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001711 struct tcpcheck_rule *chk;
1712 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001713 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001714
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001715 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001716 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001717
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001718 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001719 * implicit one is inserted before all others.
1720 */
1721 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1722 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1723 chk = calloc(1, sizeof(*chk));
1724 if (!chk) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001725 ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
Christopher Faulet61cc8522020-04-20 14:54:42 +02001726 " to agent-check for server '%s' (out of memory).\n",
1727 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1728 ret |= ERR_ALERT | ERR_FATAL;
1729 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001730 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001731 chk->action = TCPCHK_ACT_CONNECT;
1732 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001733 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001734 }
1735
Christopher Faulet871dd822022-08-24 11:38:03 +02001736 /* <chk> is always defined here and it is a CONNECT action. If there is
1737 * a preset variable, it means there is an agent string defined and data
1738 * will be sent after the connect.
1739 */
1740 if (!LIST_ISEMPTY(&srv->agent.tcpcheck_rules->preset_vars))
1741 chk->connect.options |= TCPCHK_OPT_HAS_DATA;
1742
Christopher Faulete5870d82020-04-15 11:32:03 +02001743
Christopher Faulet61cc8522020-04-20 14:54:42 +02001744 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1745 if (err) {
1746 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1747 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1748 ret |= ERR_ALERT | ERR_ABORT;
1749 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001750 }
1751
Christopher Faulet61cc8522020-04-20 14:54:42 +02001752 if (!srv->agent.inter)
1753 srv->agent.inter = srv->check.inter;
1754
1755 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001756 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001757
1758 /* Only increment maxsock for servers from the configuration. Dynamic
1759 * servers at the moment are not taken into account for the estimation
1760 * of the resources limits.
1761 */
1762 if (global.mode & MODE_STARTING)
1763 global.maxsock++;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001764
1765 out:
1766 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001767}
1768
Christopher Faulet61cc8522020-04-20 14:54:42 +02001769static void deinit_srv_check(struct server *srv)
1770{
1771 if (srv->check.state & CHK_ST_CONFIGURED)
1772 free_check(&srv->check);
1773 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1774 srv->do_check = 0;
1775}
Christopher Faulete5870d82020-04-15 11:32:03 +02001776
Christopher Faulet61cc8522020-04-20 14:54:42 +02001777
1778static void deinit_srv_agent_check(struct server *srv)
1779{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001780 if (srv->agent.state & CHK_ST_CONFIGURED)
1781 free_check(&srv->agent);
1782
1783 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1784 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001785}
1786
Willy Tarreaucee013e2020-06-05 11:40:38 +02001787REGISTER_POST_SERVER_CHECK(init_srv_check);
1788REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001789REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001790
Willy Tarreaucee013e2020-06-05 11:40:38 +02001791REGISTER_SERVER_DEINIT(deinit_srv_check);
1792REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001793
Christopher Faulet61cc8522020-04-20 14:54:42 +02001794
1795/**************************************************************************/
1796/************************** Check sample fetches **************************/
1797/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001798
Christopher Faulet61cc8522020-04-20 14:54:42 +02001799static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001800 { /* END */ },
1801}};
1802
1803INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1804
1805
1806/**************************************************************************/
1807/************************ Check's parsing functions ***********************/
1808/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001809/* Parse the "addr" server keyword */
1810static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1811 char **errmsg)
1812{
1813 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001814 int port1, port2, err_code = 0;
1815
1816
1817 if (!*args[*cur_arg+1]) {
1818 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1819 goto error;
1820 }
1821
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001822 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1823 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001824 if (!sk) {
1825 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1826 goto error;
1827 }
1828
William Dauchy1c921cd2021-02-03 22:30:08 +01001829 srv->check.addr = *sk;
1830 /* if agentaddr was never set, we can use addr */
1831 if (!(srv->flags & SRV_F_AGENTADDR))
1832 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001833
1834 out:
1835 return err_code;
1836
1837 error:
1838 err_code |= ERR_ALERT | ERR_FATAL;
1839 goto out;
1840}
1841
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001842/* Parse the "agent-addr" server keyword */
1843static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1844 char **errmsg)
1845{
William Dauchy1c921cd2021-02-03 22:30:08 +01001846 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001847 int err_code = 0;
1848
1849 if (!*(args[*cur_arg+1])) {
1850 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1851 goto error;
1852 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001853 memset(&sk, 0, sizeof(sk));
1854 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001855 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1856 goto error;
1857 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001858 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001859
1860 out:
1861 return err_code;
1862
1863 error:
1864 err_code |= ERR_ALERT | ERR_FATAL;
1865 goto out;
1866}
1867
1868/* Parse the "agent-check" server keyword */
1869static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1870 char **errmsg)
1871{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001872 struct tcpcheck_ruleset *rs = NULL;
1873 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1874 struct tcpcheck_rule *chk;
1875 int err_code = 0;
1876
1877 if (srv->do_agent)
1878 goto out;
1879
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001880 if (!(curpx->cap & PR_CAP_BE)) {
1881 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1882 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1883 return ERR_WARN;
1884 }
1885
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001886 if (!rules) {
1887 rules = calloc(1, sizeof(*rules));
1888 if (!rules) {
1889 memprintf(errmsg, "out of memory.");
1890 goto error;
1891 }
1892 LIST_INIT(&rules->preset_vars);
1893 srv->agent.tcpcheck_rules = rules;
1894 }
1895 rules->list = NULL;
1896 rules->flags = 0;
1897
Christopher Faulet61cc8522020-04-20 14:54:42 +02001898 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001899 if (rs)
1900 goto ruleset_found;
1901
Christopher Faulet61cc8522020-04-20 14:54:42 +02001902 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001903 if (rs == NULL) {
1904 memprintf(errmsg, "out of memory.");
1905 goto error;
1906 }
1907
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001908 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001909 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1910 if (!chk) {
1911 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1912 goto error;
1913 }
1914 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001915 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001916
1917 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001918 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1919 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001920 if (!chk) {
1921 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1922 goto error;
1923 }
1924 chk->expect.custom = tcpcheck_agent_expect_reply;
1925 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001926 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001927
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001928 ruleset_found:
1929 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001930 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001931 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001932 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001933
1934 out:
Dirkjan Bussinkdfee2172021-06-18 19:57:49 +00001935 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001936
1937 error:
1938 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001939 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001940 err_code |= ERR_ALERT | ERR_FATAL;
1941 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001942}
1943
1944/* Parse the "agent-inter" server keyword */
1945static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1946 char **errmsg)
1947{
1948 const char *err = NULL;
1949 unsigned int delay;
1950 int err_code = 0;
1951
1952 if (!*(args[*cur_arg+1])) {
1953 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1954 goto error;
1955 }
1956
1957 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1958 if (err == PARSE_TIME_OVER) {
1959 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1960 args[*cur_arg+1], args[*cur_arg], srv->id);
1961 goto error;
1962 }
1963 else if (err == PARSE_TIME_UNDER) {
1964 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1965 args[*cur_arg+1], args[*cur_arg], srv->id);
1966 goto error;
1967 }
1968 else if (err) {
1969 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1970 *err, srv->id);
1971 goto error;
1972 }
1973 if (delay <= 0) {
1974 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1975 delay, args[*cur_arg], srv->id);
1976 goto error;
1977 }
1978 srv->agent.inter = delay;
1979
1980 out:
1981 return err_code;
1982
1983 error:
1984 err_code |= ERR_ALERT | ERR_FATAL;
1985 goto out;
1986}
1987
1988/* Parse the "agent-port" server keyword */
1989static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1990 char **errmsg)
1991{
1992 int err_code = 0;
1993
1994 if (!*(args[*cur_arg+1])) {
1995 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1996 goto error;
1997 }
1998
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001999 /* Only increment maxsock for servers from the configuration. Dynamic
2000 * servers at the moment are not taken into account for the estimation
2001 * of the resources limits.
2002 */
2003 if (global.mode & MODE_STARTING)
2004 global.maxsock++;
2005
William Dauchy4858fb22021-02-03 22:30:09 +01002006 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002007
2008 out:
2009 return err_code;
2010
2011 error:
2012 err_code |= ERR_ALERT | ERR_FATAL;
2013 goto out;
2014}
2015
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002016int set_srv_agent_send(struct server *srv, const char *send)
2017{
2018 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
2019 struct tcpcheck_var *var = NULL;
2020 char *str;
2021
2022 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02002023 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002024 if (str == NULL || var == NULL)
2025 goto error;
2026
2027 free_tcpcheck_vars(&rules->preset_vars);
2028
2029 var->data.type = SMP_T_STR;
2030 var->data.u.str.area = str;
2031 var->data.u.str.data = strlen(str);
2032 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02002033 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002034
2035 return 1;
2036
2037 error:
2038 free(str);
2039 free(var);
2040 return 0;
2041}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002042
2043/* Parse the "agent-send" server keyword */
2044static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2045 char **errmsg)
2046{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002047 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002048 int err_code = 0;
2049
2050 if (!*(args[*cur_arg+1])) {
2051 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2052 goto error;
2053 }
2054
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002055 if (!rules) {
2056 rules = calloc(1, sizeof(*rules));
2057 if (!rules) {
2058 memprintf(errmsg, "out of memory.");
2059 goto error;
2060 }
2061 LIST_INIT(&rules->preset_vars);
2062 srv->agent.tcpcheck_rules = rules;
2063 }
2064
2065 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002066 memprintf(errmsg, "out of memory.");
2067 goto error;
2068 }
2069
2070 out:
2071 return err_code;
2072
2073 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002074 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002075 err_code |= ERR_ALERT | ERR_FATAL;
2076 goto out;
2077}
2078
2079/* Parse the "no-agent-send" server keyword */
2080static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2081 char **errmsg)
2082{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002083 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002084 return 0;
2085}
2086
Christopher Fauletce8111e2020-04-06 15:04:11 +02002087/* Parse the "check" server keyword */
2088static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2089 char **errmsg)
2090{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002091 if (!(curpx->cap & PR_CAP_BE)) {
2092 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2093 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2094 return ERR_WARN;
2095 }
2096
Christopher Fauletce8111e2020-04-06 15:04:11 +02002097 srv->do_check = 1;
2098 return 0;
2099}
2100
2101/* Parse the "check-send-proxy" server keyword */
2102static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2103 char **errmsg)
2104{
2105 srv->check.send_proxy = 1;
2106 return 0;
2107}
2108
2109/* Parse the "check-via-socks4" server keyword */
2110static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2111 char **errmsg)
2112{
2113 srv->check.via_socks4 = 1;
2114 return 0;
2115}
2116
2117/* Parse the "no-check" server keyword */
2118static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2119 char **errmsg)
2120{
2121 deinit_srv_check(srv);
2122 return 0;
2123}
2124
2125/* Parse the "no-check-send-proxy" server keyword */
2126static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2127 char **errmsg)
2128{
2129 srv->check.send_proxy = 0;
2130 return 0;
2131}
2132
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002133/* parse the "check-proto" server keyword */
2134static int srv_parse_check_proto(char **args, int *cur_arg,
2135 struct proxy *px, struct server *newsrv, char **err)
2136{
2137 int err_code = 0;
2138
2139 if (!*args[*cur_arg + 1]) {
2140 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2141 goto error;
2142 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002143 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002144 if (!newsrv->check.mux_proto) {
2145 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2146 goto error;
2147 }
2148
2149 out:
2150 return err_code;
2151
2152 error:
2153 err_code |= ERR_ALERT | ERR_FATAL;
2154 goto out;
2155}
2156
2157
Christopher Fauletce8111e2020-04-06 15:04:11 +02002158/* Parse the "rise" server keyword */
2159static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2160 char **errmsg)
2161{
2162 int err_code = 0;
2163
2164 if (!*args[*cur_arg + 1]) {
2165 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2166 goto error;
2167 }
2168
2169 srv->check.rise = atol(args[*cur_arg+1]);
2170 if (srv->check.rise <= 0) {
2171 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2172 goto error;
2173 }
2174
2175 if (srv->check.health)
2176 srv->check.health = srv->check.rise;
2177
2178 out:
2179 return err_code;
2180
2181 error:
2182 deinit_srv_agent_check(srv);
2183 err_code |= ERR_ALERT | ERR_FATAL;
2184 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002185}
2186
2187/* Parse the "fall" server keyword */
2188static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2189 char **errmsg)
2190{
2191 int err_code = 0;
2192
2193 if (!*args[*cur_arg + 1]) {
2194 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2195 goto error;
2196 }
2197
2198 srv->check.fall = atol(args[*cur_arg+1]);
2199 if (srv->check.fall <= 0) {
2200 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2201 goto error;
2202 }
2203
2204 out:
2205 return err_code;
2206
2207 error:
2208 deinit_srv_agent_check(srv);
2209 err_code |= ERR_ALERT | ERR_FATAL;
2210 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002211}
2212
2213/* Parse the "inter" server keyword */
2214static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2215 char **errmsg)
2216{
2217 const char *err = NULL;
2218 unsigned int delay;
2219 int err_code = 0;
2220
2221 if (!*(args[*cur_arg+1])) {
2222 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2223 goto error;
2224 }
2225
2226 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2227 if (err == PARSE_TIME_OVER) {
2228 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2229 args[*cur_arg+1], args[*cur_arg], srv->id);
2230 goto error;
2231 }
2232 else if (err == PARSE_TIME_UNDER) {
2233 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2234 args[*cur_arg+1], args[*cur_arg], srv->id);
2235 goto error;
2236 }
2237 else if (err) {
2238 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2239 *err, srv->id);
2240 goto error;
2241 }
2242 if (delay <= 0) {
2243 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2244 delay, args[*cur_arg], srv->id);
2245 goto error;
2246 }
2247 srv->check.inter = delay;
2248
2249 out:
2250 return err_code;
2251
2252 error:
2253 err_code |= ERR_ALERT | ERR_FATAL;
2254 goto out;
2255}
2256
2257
2258/* Parse the "fastinter" server keyword */
2259static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2260 char **errmsg)
2261{
2262 const char *err = NULL;
2263 unsigned int delay;
2264 int err_code = 0;
2265
2266 if (!*(args[*cur_arg+1])) {
2267 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2268 goto error;
2269 }
2270
2271 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2272 if (err == PARSE_TIME_OVER) {
2273 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2274 args[*cur_arg+1], args[*cur_arg], srv->id);
2275 goto error;
2276 }
2277 else if (err == PARSE_TIME_UNDER) {
2278 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2279 args[*cur_arg+1], args[*cur_arg], srv->id);
2280 goto error;
2281 }
2282 else if (err) {
2283 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2284 *err, srv->id);
2285 goto error;
2286 }
2287 if (delay <= 0) {
2288 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2289 delay, args[*cur_arg], srv->id);
2290 goto error;
2291 }
2292 srv->check.fastinter = delay;
2293
2294 out:
2295 return err_code;
2296
2297 error:
2298 err_code |= ERR_ALERT | ERR_FATAL;
2299 goto out;
2300}
2301
2302
2303/* Parse the "downinter" server keyword */
2304static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2305 char **errmsg)
2306{
2307 const char *err = NULL;
2308 unsigned int delay;
2309 int err_code = 0;
2310
2311 if (!*(args[*cur_arg+1])) {
2312 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2313 goto error;
2314 }
2315
2316 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2317 if (err == PARSE_TIME_OVER) {
2318 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2319 args[*cur_arg+1], args[*cur_arg], srv->id);
2320 goto error;
2321 }
2322 else if (err == PARSE_TIME_UNDER) {
2323 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2324 args[*cur_arg+1], args[*cur_arg], srv->id);
2325 goto error;
2326 }
2327 else if (err) {
2328 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2329 *err, srv->id);
2330 goto error;
2331 }
2332 if (delay <= 0) {
2333 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2334 delay, args[*cur_arg], srv->id);
2335 goto error;
2336 }
2337 srv->check.downinter = delay;
2338
2339 out:
2340 return err_code;
2341
2342 error:
2343 err_code |= ERR_ALERT | ERR_FATAL;
2344 goto out;
2345}
2346
2347/* Parse the "port" server keyword */
2348static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2349 char **errmsg)
2350{
2351 int err_code = 0;
2352
2353 if (!*(args[*cur_arg+1])) {
2354 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2355 goto error;
2356 }
2357
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002358 /* Only increment maxsock for servers from the configuration. Dynamic
2359 * servers at the moment are not taken into account for the estimation
2360 * of the resources limits.
2361 */
2362 if (global.mode & MODE_STARTING)
2363 global.maxsock++;
2364
Christopher Fauletce8111e2020-04-06 15:04:11 +02002365 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002366 /* if agentport was never set, we can use port */
2367 if (!(srv->flags & SRV_F_AGENTPORT))
2368 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002369
2370 out:
2371 return err_code;
2372
2373 error:
2374 err_code |= ERR_ALERT | ERR_FATAL;
2375 goto out;
2376}
2377
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002378static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002379 { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */
2380 { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02002381 { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002382 { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */
2383 { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */
2384 { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02002385 { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002386 { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */
2387 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */
2388 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002389 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2390 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2391 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1, 0 }, /* Disable PROXY protocol for health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002392 { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */
2393 { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */
2394 { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */
2395 { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */
2396 { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */
2397 { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002398 { NULL, NULL, 0 },
2399}};
2400
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002401INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002402
Willy Tarreaubd741542010-03-16 18:46:54 +01002403/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002404 * Local variables:
2405 * c-indent-level: 8
2406 * c-basic-offset: 8
2407 * End:
2408 */