blob: 123a24e89b70480430ddbaa70e058e180e0cc927 [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
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +0200431/* append check info to buffer msg */
432void check_append_info(struct buffer *msg, struct check *check)
433{
434 if (!check)
435 return;
436 chunk_appendf(msg, ", reason: %s", get_check_status_description(check->status));
437
438 if (check->status >= HCHK_STATUS_L57DATA)
439 chunk_appendf(msg, ", code: %d", check->code);
440
441 if (check->desc[0]) {
442 struct buffer src;
443
444 chunk_appendf(msg, ", info: \"");
445
446 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
447 chunk_asciiencode(msg, &src, '"');
448
449 chunk_appendf(msg, "\"");
450 }
451
452 if (check->duration >= 0)
453 chunk_appendf(msg, ", check duration: %ldms", check->duration);
454}
455
Christopher Faulet61cc8522020-04-20 14:54:42 +0200456/* Sets check->status, update check->duration and fill check->result with an
457 * adequate CHK_RES_* value. The new check->health is computed based on the
458 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200459 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200460 * Shows information in logs about failed health check if server is UP or
461 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200462 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200463void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100464{
Simon Horman4a741432013-02-23 15:35:38 +0900465 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200466 short prev_status = check->status;
Aurelien DARRAGONdcbc2d22023-03-30 10:19:08 +0200467 int report = (status != prev_status) ? 1 : 0;
Simon Horman4a741432013-02-23 15:35:38 +0900468
Christopher Faulet147b8c92021-04-10 09:00:38 +0200469 TRACE_POINT(CHK_EV_HCHK_RUN, check);
470
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200471 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100472 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900473 check->desc[0] = '\0';
Willy Tarreau69530f52023-04-28 09:16:15 +0200474 check->start = now_ns;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200475 return;
476 }
477
Simon Horman4a741432013-02-23 15:35:38 +0900478 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200479 return;
480
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200481 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900482 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
483 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200484 } else
Simon Horman4a741432013-02-23 15:35:38 +0900485 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200486
Simon Horman4a741432013-02-23 15:35:38 +0900487 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200488 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900489 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200490
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100491 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900492 check->duration = -1;
Willy Tarreaue8e47122023-04-28 14:39:50 +0200493 else if (check->start) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200494 /* set_server_check_status() may be called more than once */
Willy Tarreau69530f52023-04-28 09:16:15 +0200495 check->duration = ns_to_ms(now_ns - check->start);
Willy Tarreaue8e47122023-04-28 14:39:50 +0200496 check->start = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200497 }
498
Willy Tarreau23964182014-05-20 20:56:30 +0200499 /* no change is expected if no state change occurred */
500 if (check->result == CHK_RES_NEUTRAL)
501 return;
502
Olivier Houchard0923fa42019-01-11 18:43:04 +0100503 /* If the check was really just sending a mail, it won't have an
504 * associated server, so we're done now.
505 */
506 if (!s)
507 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200508
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200509 switch (check->result) {
510 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200511 /* Failure to connect to the agent as a secondary check should not
512 * cause the server to be marked down.
513 */
514 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900515 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200516 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200517 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200518 report = 1;
519 check->health--;
520 if (check->health < check->rise)
521 check->health = 0;
522 }
523 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200524
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200525 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100526 case CHK_RES_CONDPASS:
527 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200528 report = 1;
529 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200530
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200531 if (check->health >= check->rise)
532 check->health = check->rise + check->fall - 1; /* OK now */
533 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200534
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200535 /* clear consecutive_errors if observing is enabled */
536 if (s->onerror)
Aurelien DARRAGON81b7c952022-12-07 14:27:42 +0100537 HA_ATOMIC_STORE(&s->consecutive_errors, 0);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200538 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100539
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200540 default:
541 break;
542 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200543
Aurelien DARRAGONdcbc2d22023-03-30 10:19:08 +0200544 if (report)
545 srv_event_hdl_publish_check(s, check);
546
547 if (s->proxy->options2 & PR_O2_LOGHCHKS && report) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200548 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200549 "%s check for %sserver %s/%s %s%s",
550 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200551 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100552 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100553 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200554 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200555
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +0200556 check_append_info(&trash, check);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200557
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100558 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200559 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
560 (check->health >= check->rise) ? check->fall : check->rise,
561 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200562
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200563 ha_warning("%s.\n", trash.area);
564 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
565 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200566 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200567}
568
Aurelien DARRAGON1746b562023-04-04 10:17:40 +0200569static inline enum srv_op_st_chg_cause check_notify_cause(struct check *check)
570{
571 struct server *s = check->server;
572
573 /* We only report a cause for the check if we did not do so previously */
574 if (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))
575 return (check->state & CHK_ST_AGENT) ? SRV_OP_STCHGC_AGENT : SRV_OP_STCHGC_HEALTH;
576 return SRV_OP_STCHGC_NONE;
577}
578
Willy Tarreau4eec5472014-05-20 22:32:27 +0200579/* Marks the check <check>'s server down if the current check is already failed
580 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200582void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583{
Simon Horman4a741432013-02-23 15:35:38 +0900584 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900585
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200586 /* The agent secondary check should only cause a server to be marked
587 * as down if check->status is HCHK_STATUS_L7STS, which indicates
588 * that the agent returned "fail", "stopped" or "down".
589 * The implication here is that failure to connect to the agent
590 * as a secondary check should not cause the server to be marked
591 * down. */
592 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
593 return;
594
Willy Tarreau4eec5472014-05-20 22:32:27 +0200595 if (check->health > 0)
596 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100597
Christopher Faulet147b8c92021-04-10 09:00:38 +0200598 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +0200599 srv_set_stopped(s, check_notify_cause(check));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600}
601
Willy Tarreauaf549582014-05-16 17:37:50 +0200602/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200603 * it isn't in maintenance, it is not tracking a down server and other checks
604 * comply. The rule is simple : by default, a server is up, unless any of the
605 * following conditions is true :
606 * - health check failed (check->health < rise)
607 * - agent check failed (agent->health < rise)
608 * - the server tracks a down server (track && track->state == STOPPED)
609 * Note that if the server has a slowstart, it will switch to STARTING instead
610 * of RUNNING. Also, only the health checks support the nolb mode, so the
611 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200612 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200613void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200614{
Simon Horman4a741432013-02-23 15:35:38 +0900615 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100616
Emeric Brun52a91d32017-08-31 14:41:55 +0200617 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200618 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100619
Emeric Brun52a91d32017-08-31 14:41:55 +0200620 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200621 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100622
Willy Tarreau3e048382014-05-21 10:30:54 +0200623 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
624 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100625
Willy Tarreau3e048382014-05-21 10:30:54 +0200626 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
627 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200628
Emeric Brun52a91d32017-08-31 14:41:55 +0200629 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200630 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100631
Christopher Faulet147b8c92021-04-10 09:00:38 +0200632 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +0200633 srv_set_running(s, check_notify_cause(check));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100634}
635
Willy Tarreaudb58b792014-05-21 13:57:23 +0200636/* Marks the check <check> as valid and tries to set its server into stopping mode
637 * if it was running or starting, and provided it isn't in maintenance and other
638 * checks comply. The conditions for the server to be marked in stopping mode are
639 * the same as for it to be turned up. Also, only the health checks support the
640 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200641 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200642void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200643{
Simon Horman4a741432013-02-23 15:35:38 +0900644 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100645
Emeric Brun52a91d32017-08-31 14:41:55 +0200646 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200647 return;
648
Willy Tarreaudb58b792014-05-21 13:57:23 +0200649 if (check->state & CHK_ST_AGENT)
650 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100651
Emeric Brun52a91d32017-08-31 14:41:55 +0200652 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200653 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100654
Willy Tarreaudb58b792014-05-21 13:57:23 +0200655 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
656 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100657
Willy Tarreaudb58b792014-05-21 13:57:23 +0200658 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
659 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100660
Christopher Faulet147b8c92021-04-10 09:00:38 +0200661 TRACE_STATE("health-check condionnaly succeeded, set server STOPPING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +0200662 srv_set_stopping(s, check_notify_cause(check));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100663}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100665/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100666 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100667 */
668void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100669{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100670 int failed;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100671
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100672 if (s->observe >= HANA_OBS_SIZE)
673 return;
674
Willy Tarreaubb956662013-01-24 00:37:39 +0100675 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100676 return;
677
678 switch (analyze_statuses[status].lr[s->observe - 1]) {
679 case 1:
680 failed = 1;
681 break;
682
683 case 2:
684 failed = 0;
685 break;
686
687 default:
688 return;
689 }
690
691 if (!failed) {
692 /* good: clear consecutive_errors */
Aurelien DARRAGON81b7c952022-12-07 14:27:42 +0100693 HA_ATOMIC_STORE(&s->consecutive_errors, 0);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100694 return;
695 }
696
Aurelien DARRAGON81b7c952022-12-07 14:27:42 +0100697 if (HA_ATOMIC_ADD_FETCH(&s->consecutive_errors, 1) < s->consecutive_errors_limit)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100698 return;
699
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100700 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
Aurelien DARRAGON81b7c952022-12-07 14:27:42 +0100701 HA_ATOMIC_LOAD(&s->consecutive_errors), get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100702
Willy Tarreau4e9df272021-02-17 15:20:19 +0100703 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
704
Aurelien DARRAGON7d541a92022-12-07 12:17:24 +0100705 /* force fastinter for upcoming check
706 * (does nothing if fastinter is not enabled)
707 */
708 s->check.state |= CHK_ST_FASTINTER;
709
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100710 switch (s->onerror) {
711 case HANA_ONERR_FASTINTER:
712 /* force fastinter - nothing to do here as all modes force it */
713 break;
714
715 case HANA_ONERR_SUDDTH:
716 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900717 if (s->check.health > s->check.rise)
718 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100719
Willy Tarreau36a73432022-11-14 07:31:36 +0100720 __fallthrough;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100721
722 case HANA_ONERR_FAILCHK:
723 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200724 set_server_check_status(&s->check, HCHK_STATUS_HANA,
725 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200726 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100727 break;
728
729 case HANA_ONERR_MARKDWN:
730 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900731 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 set_server_check_status(&s->check, HCHK_STATUS_HANA,
733 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200734 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100735 break;
736
737 default:
738 /* write a warning? */
739 break;
740 }
741
Willy Tarreau4e9df272021-02-17 15:20:19 +0100742 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
743
Aurelien DARRAGON81b7c952022-12-07 14:27:42 +0100744 HA_ATOMIC_STORE(&s->consecutive_errors, 0);
Willy Tarreau4781b152021-04-06 13:53:36 +0200745 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100746
Willy Tarreaua56798e2022-12-06 11:38:18 +0100747 if (s->check.fastinter) {
748 /* timer might need to be advanced, it might also already be
749 * running in another thread. Let's just wake the task up, it
750 * will automatically adjust its timer.
751 */
752 task_wakeup(s->check.task, TASK_WOKEN_MSG);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100753 }
Willy Tarreauef781042010-01-27 11:53:01 +0100754}
755
Christopher Faulet61cc8522020-04-20 14:54:42 +0200756/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100757 * closed, keep errno intact as it is supposed to contain the valid error code.
758 * If no error is reported, check the socket's error queue using getsockopt().
759 * Warning, this must be done only once when returning from poll, and never
760 * after an I/O error was attempted, otherwise the error queue might contain
761 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
762 * socket. Returns non-zero if an error was reported, zero if everything is
763 * clean (including a properly closed socket).
764 */
765static int retrieve_errno_from_socket(struct connection *conn)
766{
767 int skerr;
768 socklen_t lskerr = sizeof(skerr);
769
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100770 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100771 return 1;
772
Willy Tarreau3c728722014-01-23 13:50:42 +0100773 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100774 return 0;
775
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200776 BUG_ON(conn->flags & CO_FL_FDLESS);
777
Willy Tarreau585744b2017-08-24 14:31:19 +0200778 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100779 errno = skerr;
780
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100781 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100782
783 if (!errno) {
784 /* we could not retrieve an error, that does not mean there is
785 * none. Just don't change anything and only report the prior
786 * error if any.
787 */
788 if (conn->flags & CO_FL_ERROR)
789 return 1;
790 else
791 return 0;
792 }
793
794 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
795 return 1;
796}
797
Christopher Faulet61cc8522020-04-20 14:54:42 +0200798/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100799 * and adjust the server status accordingly. It may make use of <errno_bck>
800 * if non-null when the caller is absolutely certain of its validity (eg:
801 * checked just after a syscall). If the caller doesn't have a valid errno,
802 * it can pass zero, and retrieve_errno_from_socket() will be called to try
803 * to extract errno from the socket. If no error is reported, it will consider
804 * the <expired> flag. This is intended to be used when a connection error was
805 * reported in conn->flags or when a timeout was reported in <expired>. The
806 * function takes care of not updating a server status which was already set.
807 * All situations where at least one of <expired> or CO_FL_ERROR are set
808 * produce a status.
809 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200810void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100811{
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200812 struct stconn *sc = check->sc;
813 struct connection *conn = sc_conn(sc);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100814 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200815 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200816 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100817
Christopher Faulet147b8c92021-04-10 09:00:38 +0200818 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100819 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200820 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100821
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100822 errno = unclean_errno(errno_bck);
823 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100824 retrieve_errno_from_socket(conn);
825
Christopher Faulete4b40192022-06-13 07:59:46 +0200826 if (conn && !(conn->flags & CO_FL_ERROR) && !sc_ep_test(sc, SE_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100827 return;
828
Christopher Faulet147b8c92021-04-10 09:00:38 +0200829 TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
830
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100831 /* we'll try to build a meaningful error message depending on the
832 * context of the error possibly present in conn->err_code, and the
833 * socket error possibly collected above. This is useful to know the
834 * exact step of the L6 layer (eg: SSL handshake).
835 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200836 chk = get_trash_chunk();
837
Christopher Faulet799f3a42020-04-07 12:06:14 +0200838 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200839 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200840 step = tcpcheck_get_step_id(check, NULL);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200841 if (!step) {
842 TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200843 chunk_printf(chk, " at initial connection step of tcp-check");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200844 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200845 else {
846 chunk_printf(chk, " at step %d of tcp-check", step);
847 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200848 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
849 if (check->current_step->connect.port)
850 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200851 else
852 chunk_appendf(chk, " (connect)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200853 TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200854 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200855 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
856 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100857
858 switch (expect->type) {
859 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200860 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100861 break;
862 case TCPCHK_EXPECT_BINARY:
Christopher Fauleta8b082d2023-10-30 09:00:37 +0100863 chunk_appendf(chk, " (expect binary '");
864 dump_binary(chk, istptr(expect->data), (int)istlen(expect->data));
865 chunk_appendf(chk, "')");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100866 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200867 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200868 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100869 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200870 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100871 chunk_appendf(chk, " (expect binary regex)");
872 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200873 case TCPCHK_EXPECT_STRING_LF:
874 chunk_appendf(chk, " (expect log-format string)");
875 break;
876 case TCPCHK_EXPECT_BINARY_LF:
877 chunk_appendf(chk, " (expect log-format binary)");
878 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200879 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200880 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200881 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200882 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200883 chunk_appendf(chk, " (expect HTTP status regex)");
884 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200885 case TCPCHK_EXPECT_HTTP_HEADER:
886 chunk_appendf(chk, " (expect HTTP header pattern)");
887 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200888 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200889 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200890 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200891 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200892 chunk_appendf(chk, " (expect HTTP body regex)");
893 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200894 case TCPCHK_EXPECT_HTTP_BODY_LF:
895 chunk_appendf(chk, " (expect log-format HTTP body)");
896 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200897 case TCPCHK_EXPECT_CUSTOM:
898 chunk_appendf(chk, " (expect custom function)");
899 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100900 case TCPCHK_EXPECT_UNDEF:
901 chunk_appendf(chk, " (undefined expect!)");
902 break;
903 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200904 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200905 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200906 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200907 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200908 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200909 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200910
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200911 if (check->current_step && check->current_step->comment)
912 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200913 }
914 }
915
Willy Tarreau00149122017-10-04 18:05:01 +0200916 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100917 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200918 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
919 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100920 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200921 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
922 chk->area);
923 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100924 }
925 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100926 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200927 chunk_printf(&trash, "%s%s", strerror(errno),
928 chk->area);
929 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100930 }
931 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200932 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100933 }
934 }
935
Willy Tarreau00149122017-10-04 18:05:01 +0200936 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200937 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200938 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
939 }
940
Christopher Faulete4b40192022-06-13 07:59:46 +0200941 if (!conn || !conn->ctrl) {
Christopher Faulet5e293762020-10-26 11:10:49 +0100942 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200943 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
944 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100945 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100946 /* L4 not established (yet) */
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200947 if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100948 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
949 else if (expired)
950 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200951
952 /*
953 * might be due to a server IP change.
954 * Let's trigger a DNS resolution if none are currently running.
955 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100956 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100957 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200958
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100959 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100960 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100961 /* L6 not established (yet) */
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200962 if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100963 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
964 else if (expired)
965 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
966 }
Willy Tarreaubde14ad2022-05-27 10:04:04 +0200967 else if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100968 /* I/O error after connection was established and before we could diagnose */
969 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
970 }
971 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200972 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
973
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100974 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200975 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
976 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200977 tout = check->current_step->expect.tout_status;
978 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100979 }
980
Christopher Faulet147b8c92021-04-10 09:00:38 +0200981 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100982 return;
983}
984
Simon Horman98637e52014-06-20 12:30:16 +0900985
Christopher Faulet61cc8522020-04-20 14:54:42 +0200986/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200987int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900988{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200989 int sv_state;
990 int ratio;
991 char addr[46];
992 char port[6];
993 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
994 "UP %d/%d", "UP",
995 "NOLB %d/%d", "NOLB",
996 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900997
Christopher Faulet61cc8522020-04-20 14:54:42 +0200998 if (!(s->check.state & CHK_ST_ENABLED))
999 sv_state = 6;
1000 else if (s->cur_state != SRV_ST_STOPPED) {
1001 if (s->check.health == s->check.rise + s->check.fall - 1)
1002 sv_state = 3; /* UP */
1003 else
1004 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +09001005
Christopher Faulet61cc8522020-04-20 14:54:42 +02001006 if (s->cur_state == SRV_ST_STOPPING)
1007 sv_state += 2;
1008 } else {
1009 if (s->check.health)
1010 sv_state = 1; /* going up */
1011 else
1012 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +09001013 }
Willy Tarreaub7b24782016-06-21 15:32:29 +02001014
Christopher Faulet61cc8522020-04-20 14:54:42 +02001015 chunk_appendf(buf, srv_hlt_st[sv_state],
1016 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
1017 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +02001018
Christopher Faulet61cc8522020-04-20 14:54:42 +02001019 addr_to_str(&s->addr, addr, sizeof(addr));
1020 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1021 snprintf(port, sizeof(port), "%u", s->svc_port);
1022 else
1023 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001024
Christopher Faulet61cc8522020-04-20 14:54:42 +02001025 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
1026 addr, port, s->proxy->id, s->id,
1027 global.node,
1028 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
1029 (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 +02001030 s->cur_sess, s->proxy->beconn - s->proxy->queue.length,
Willy Tarreaua0570452021-06-18 09:30:30 +02001031 s->queue.length);
Willy Tarreau9f6dc722019-03-01 11:15:10 +01001032
Christopher Faulet61cc8522020-04-20 14:54:42 +02001033 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreau69530f52023-04-28 09:16:15 +02001034 ns_to_sec(now_ns) < s->last_change + s->slowstart &&
1035 ns_to_sec(now_ns) >= s->last_change) {
1036 ratio = MAX(1, 100 * (ns_to_sec(now_ns) - s->last_change) / s->slowstart);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001037 chunk_appendf(buf, "; throttle=%d%%", ratio);
1038 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001039
Christopher Faulet61cc8522020-04-20 14:54:42 +02001040 return b_data(buf);
1041}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001042
Willy Tarreau51cd5952020-06-05 12:25:38 +02001043/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001044/***************** Health-checks based on connections *********************/
1045/**************************************************************************/
1046/* This function is used only for server health-checks. It handles connection
1047 * status updates including errors. If necessary, it wakes the check task up.
1048 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1049 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001050 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001051int wake_srv_chk(struct stconn *sc)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001052{
Christopher Faulet0256da12021-12-15 09:50:17 +01001053 struct connection *conn;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001054 struct check *check = __sc_check(sc);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001055 struct email_alertq *q = container_of(check, typeof(*q), check);
1056 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001057
Christopher Faulet147b8c92021-04-10 09:00:38 +02001058 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001059 if (check->result != CHK_RES_UNKNOWN)
1060 goto end;
1061
Willy Tarreau51cd5952020-06-05 12:25:38 +02001062 if (check->server)
1063 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1064 else
1065 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001066
Willy Tarreau51cd5952020-06-05 12:25:38 +02001067 /* we may have to make progress on the TCP checks */
1068 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001069
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001070 sc = check->sc;
1071 conn = sc_conn(sc);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001072
Christopher Faulete4b40192022-06-13 07:59:46 +02001073 if (unlikely(!conn || conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001074 /* We may get error reports bypassing the I/O handlers, typically
1075 * the case when sending a pure TCP check which fails, then the I/O
1076 * handlers above are not called. This is completely handled by the
1077 * main processing task so let's simply wake it up. If we get here,
1078 * we expect errno to still be valid.
1079 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001080 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 +02001081 chk_report_conn_err(check, errno, 0);
1082 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001083 }
1084
Christopher Faulet8f100422021-01-18 15:47:03 +01001085 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001086 /* Check complete or aborted. Wake the check task up to be sure
1087 * the result is handled ASAP. */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001088 ret = -1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001089 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001090 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001091
Willy Tarreau51cd5952020-06-05 12:25:38 +02001092 if (check->server)
1093 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1094 else
1095 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001096
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001097 end:
Christopher Faulet147b8c92021-04-10 09:00:38 +02001098 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001099 return ret;
1100}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001101
Willy Tarreau51cd5952020-06-05 12:25:38 +02001102/* This function checks if any I/O is wanted, and if so, attempts to do so */
Christopher Faulet361417f2022-05-18 14:50:30 +02001103struct task *srv_chk_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001104{
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001105 struct stconn *sc = ctx;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001106
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001107 wake_srv_chk(sc);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001108 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001109}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001110
Willy Tarreau51cd5952020-06-05 12:25:38 +02001111/* manages a server health-check that uses a connection. Returns
1112 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001113 *
1114 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001115 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001116 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001117struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001118{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001119 struct check *check = context;
1120 struct proxy *proxy = check->proxy;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001121 struct stconn *sc;
Christopher Faulet92017a32021-05-06 16:01:18 +02001122 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001123 int rv;
1124 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001125
Christopher Faulet147b8c92021-04-10 09:00:38 +02001126 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1127
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001128 if (check->state & CHK_ST_SLEEPING) {
1129 /* This check just restarted. It's still time to verify if
1130 * we're on an overloaded thread or if a more suitable one is
1131 * available. This helps spread the load over the available
1132 * threads, without migrating too often. For this we'll check
1133 * our load, and pick a random thread, check if it has less
1134 * than half of the current thread's load, and if so we'll
1135 * bounce the task there. It's possible because it's not yet
1136 * tied to the current thread. The other thread will not bounce
1137 * the task again because we're removing CHK_ST_SLEEPING.
1138 */
1139 uint my_load = HA_ATOMIC_LOAD(&th_ctx->rq_total);
1140
1141 check->state &= ~CHK_ST_SLEEPING;
1142
1143 if (my_load >= 2) {
1144 uint new_tid = statistical_prng_range(global.nbthread);
1145 uint new_load = HA_ATOMIC_LOAD(&ha_thread_ctx[new_tid].rq_total);
1146
1147 if (new_load <= my_load / 2) {
1148 /* Found one. Let's migrate the task over there. We have to
1149 * remove it from the WQ first and kill its expire time
1150 * otherwise the scheduler will reinsert it and trigger a
1151 * BUG_ON() as we're not allowed to call task_queue() for a
1152 * foreign thread. The recipient will restore the expiration.
1153 */
1154 task_unlink_wq(t);
1155 t->expire = TICK_ETERNITY;
1156 task_set_thread(t, new_tid);
1157 task_wakeup(t, TASK_WOKEN_MSG);
1158 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
1159 return t;
1160 }
1161 }
1162 }
1163
Willy Tarreau51cd5952020-06-05 12:25:38 +02001164 if (check->server)
1165 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001166
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001167 if (!(check->state & (CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC))) {
1168 /* This task might have bounced from another overloaded thread, it
1169 * needs an expiration timer that was supposed to be now, but that
1170 * was erased during the bounce.
1171 */
Willy Tarreauacbd22f2023-09-01 07:41:46 +02001172 if (!tick_isset(t->expire)) {
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001173 t->expire = now_ms;
Willy Tarreauacbd22f2023-09-01 07:41:46 +02001174 expired = 0;
1175 }
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001176 }
1177
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001178 if (unlikely(check->state & CHK_ST_PURGE)) {
1179 TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1180 }
1181 else if (!(check->state & (CHK_ST_INPROGRESS))) {
Willy Tarreau9e080bf2022-12-06 18:20:56 +01001182 /* no check currently running, but we might have been woken up
1183 * before the timer's expiration to update it according to a
1184 * new state (e.g. fastinter), in which case we'll reprogram
1185 * the new timer.
1186 */
Willy Tarreauacbd22f2023-09-01 07:41:46 +02001187 if (!tick_is_expired(t->expire, now_ms)) { /* woke up too early */
Willy Tarreau9e080bf2022-12-06 18:20:56 +01001188 if (check->server) {
1189 int new_exp = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check)));
1190
1191 if (tick_is_expired(new_exp, t->expire)) {
1192 TRACE_STATE("health-check was advanced", CHK_EV_TASK_WAKE, check);
1193 goto update_timer;
1194 }
1195 }
1196
Christopher Faulet147b8c92021-04-10 09:00:38 +02001197 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001198 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001199 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001200
Willy Tarreau51cd5952020-06-05 12:25:38 +02001201 /* we don't send any health-checks when the proxy is
1202 * stopped, the server should not be checked or the check
1203 * is disabled.
1204 */
1205 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001206 (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001207 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001208 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001209 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001210
Willy Tarreau51cd5952020-06-05 12:25:38 +02001211 /* we'll initiate a new check */
1212 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001213
Willy Tarreau51cd5952020-06-05 12:25:38 +02001214 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001215 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001216
Willy Tarreaueed39112022-06-15 17:20:16 +02001217 task_set_thread(t, tid);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001218
1219 check->current_step = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001220
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001221 check->sc = sc_new_from_check(check, SC_FL_NONE);
1222 if (!check->sc) {
Christopher Faulet177a0e62022-04-12 17:47:07 +02001223 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1224 goto end;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001225 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001226 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001227 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001228 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001229
Christopher Faulet92017a32021-05-06 16:01:18 +02001230 /* there was a test running.
1231 * First, let's check whether there was an uncaught error,
1232 * which can happen on connect timeout or error.
1233 */
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001234 if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001235 sc = check->sc;
Christopher Faulete4b40192022-06-13 07:59:46 +02001236 conn = sc_conn(sc);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001237
Christopher Faulet92017a32021-05-06 16:01:18 +02001238 /* Here the connection must be defined. Otherwise the
1239 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001240 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001241 if ((conn && ((conn->flags & CO_FL_ERROR) || sc_ep_test(sc, SE_FL_ERROR))) || expired) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001242 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1243 chk_report_conn_err(check, 0, expired);
1244 }
1245 else {
1246 if (check->state & CHK_ST_CLOSE_CONN) {
1247 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001248 check->state &= ~CHK_ST_CLOSE_CONN;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001249 if (!sc_reset_endp(check->sc)) {
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001250 /* error will be handled by tcpcheck_main().
Willy Tarreaub605c422022-05-17 17:04:55 +02001251 * On success, remove all flags except SE_FL_DETACHED
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001252 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001253 sc_ep_clr(check->sc, ~SE_FL_DETACHED);
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001254 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001255 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001256 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001257 if (check->result == CHK_RES_UNKNOWN) {
1258 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1259 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001260 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001261 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001262 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001263
Christopher Faulet92017a32021-05-06 16:01:18 +02001264 /* check complete or aborted */
1265 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001266
Christopher Faulete4b40192022-06-13 07:59:46 +02001267 /* check->sc may be NULL when the healthcheck is purged */
Christopher Faulet92017a32021-05-06 16:01:18 +02001268 check->current_step = NULL;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001269 sc = check->sc;
1270 conn = (sc ? sc_conn(sc) : NULL);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001271
Christopher Faulet92017a32021-05-06 16:01:18 +02001272 if (conn && conn->xprt) {
1273 /* The check was aborted and the connection was not yet closed.
1274 * This can happen upon timeout, or when an external event such
1275 * as a failed response coupled with "observe layer7" caused the
1276 * server state to be suddenly changed.
1277 */
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001278 sc_conn_drain_and_shut(sc);
Christopher Faulet92017a32021-05-06 16:01:18 +02001279 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001280
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001281 if (sc) {
1282 sc_destroy(sc);
Christopher Fauletcb760302023-05-17 09:25:32 +02001283 check->sc = NULL;
Christopher Faulet177a0e62022-04-12 17:47:07 +02001284 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001285
Christopher Faulet92017a32021-05-06 16:01:18 +02001286 if (check->sess != NULL) {
1287 vars_prune(&check->vars, check->sess, NULL);
1288 session_free(check->sess);
1289 check->sess = NULL;
1290 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001291
Christopher Fauletb041b232022-03-24 10:27:02 +01001292 end:
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001293 if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001294 if (check->result == CHK_RES_FAILED) {
1295 /* a failure or timeout detected */
1296 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1297 check_notify_failure(check);
1298 }
1299 else if (check->result == CHK_RES_CONDPASS) {
1300 /* check is OK but asks for stopping mode */
1301 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1302 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001303 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001304 else if (check->result == CHK_RES_PASSED) {
1305 /* a success was detected */
1306 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1307 check_notify_success(check);
1308 }
1309 }
Christopher Faulet6d781f62022-05-18 14:24:43 +02001310
1311 if (LIST_INLIST(&check->buf_wait.list))
1312 LIST_DEL_INIT(&check->buf_wait.list);
1313
Christopher Faulet92017a32021-05-06 16:01:18 +02001314 check_release_buf(check, &check->bi);
1315 check_release_buf(check, &check->bo);
1316 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Willy Tarreaud114f4a2022-10-12 20:58:18 +02001317 check->state |= CHK_ST_SLEEPING;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001318
Willy Tarreau9e080bf2022-12-06 18:20:56 +01001319 update_timer:
Christopher Faulet92017a32021-05-06 16:01:18 +02001320 if (check->server) {
1321 rv = 0;
1322 if (global.spread_checks > 0) {
1323 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreaua840b4a2022-10-12 21:48:17 +02001324 rv -= (int) (2 * rv * (statistical_prng() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001325 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001326 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Aurelien DARRAGON7d541a92022-12-07 12:17:24 +01001327 /* reset fastinter flag (if set) so that srv_getinter()
1328 * only returns fastinter if server health is degraded
1329 */
1330 check->state &= ~CHK_ST_FASTINTER;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001331 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001332
1333 reschedule:
Christopher Faulet8bca3cc2023-05-16 18:07:51 +02001334 if (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1335 t->expire = TICK_ETERNITY;
1336 else {
1337 while (tick_is_expired(t->expire, now_ms))
1338 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1339 }
1340
Willy Tarreau51cd5952020-06-05 12:25:38 +02001341 out_unlock:
1342 if (check->server)
1343 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001344
1345 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001346
1347 /* Free the check if set to PURGE. After this, the check instance may be
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001348 * freed via the srv_drop invocation, so it must not be accessed after
1349 * this point.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001350 */
1351 if (unlikely(check->state & CHK_ST_PURGE)) {
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001352 free_check(check);
Amaury Denoyelle9ba34ae2021-08-09 15:09:17 +02001353 if (check->server)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001354 srv_drop(check->server);
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001355
1356 t = NULL;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001357 }
1358
Willy Tarreau51cd5952020-06-05 12:25:38 +02001359 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001360}
1361
Willy Tarreau51cd5952020-06-05 12:25:38 +02001362
Christopher Faulet61cc8522020-04-20 14:54:42 +02001363/**************************************************************************/
1364/************************** Init/deinit checks ****************************/
1365/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001366/*
1367 * Tries to grab a buffer and to re-enables processing on check <target>. The
1368 * check flags are used to figure what buffer was requested. It returns 1 if the
1369 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1370 * impossible to wake up and we prefer to be woken up later.
1371 */
1372int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001373{
Christopher Fauletb381a502020-11-25 13:47:00 +01001374 struct check *check = target;
1375
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001376 BUG_ON(!check->sc);
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001377
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001378 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001379 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001380 check->state &= ~CHK_ST_IN_ALLOC;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001381 tasklet_wakeup(check->sc->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001382 return 1;
1383 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001384 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001385 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001386 check->state &= ~CHK_ST_OUT_ALLOC;
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001387 tasklet_wakeup(check->sc->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001388 return 1;
1389 }
1390
1391 return 0;
1392}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001393
Christopher Fauletb381a502020-11-25 13:47:00 +01001394/*
William Dauchyf4300902021-02-06 20:47:50 +01001395 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001396 */
1397struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1398{
1399 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001400
Willy Tarreau2b718102021-04-21 07:32:39 +02001401 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001402 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001403 check->buf_wait.target = check;
1404 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +02001405 LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001406 }
1407 return buf;
1408}
1409
1410/*
1411 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1412 * wait queue.
1413 */
1414void check_release_buf(struct check *check, struct buffer *bptr)
1415{
1416 if (bptr->size) {
1417 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001418 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001419 }
1420}
1421
1422const char *init_check(struct check *check, int type)
1423{
1424 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001425
Christopher Fauletb381a502020-11-25 13:47:00 +01001426 check->bi = BUF_NULL;
1427 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001428 LIST_INIT(&check->buf_wait.list);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001429 return NULL;
1430}
1431
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001432/* Liberates the resources allocated for a check.
1433 *
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001434 * This function must only be run by the thread owning the check.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001435 */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001436void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001437{
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001438 /* For agent-check, free the rules / vars from the server. This is not
1439 * done for health-check : the proxy is the owner of the rules / vars
1440 * in this case.
1441 */
1442 if (check->state & CHK_ST_AGENT) {
1443 free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars);
1444 ha_free(&check->tcpcheck_rules);
1445 }
1446
Christopher Faulet61cc8522020-04-20 14:54:42 +02001447 task_destroy(check->task);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001448
Christopher Fauletb381a502020-11-25 13:47:00 +01001449 check_release_buf(check, &check->bi);
1450 check_release_buf(check, &check->bo);
Willy Tarreaubde14ad2022-05-27 10:04:04 +02001451 if (check->sc) {
1452 sc_destroy(check->sc);
1453 check->sc = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001454 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001455}
1456
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001457/* This function must be used in order to free a started check. The check will
1458 * be scheduled for a next execution in order to properly close and free all
1459 * check elements.
1460 *
1461 * Non thread-safe.
1462 */
1463void check_purge(struct check *check)
1464{
Amaury Denoyelle25fe1032021-08-10 16:21:55 +02001465 check->state |= CHK_ST_PURGE;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001466 task_wakeup(check->task, TASK_WOKEN_OTHER);
1467}
1468
Christopher Faulet61cc8522020-04-20 14:54:42 +02001469/* manages a server health-check. Returns the time the task accepts to wait, or
1470 * TIME_ETERNITY for infinity.
1471 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001472struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001473{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001474 struct check *check = context;
1475
1476 if (check->type == PR_O2_EXT_CHK)
1477 return process_chk_proc(t, context, state);
1478 return process_chk_conn(t, context, state);
1479
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001480}
1481
Christopher Faulet61cc8522020-04-20 14:54:42 +02001482
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001483int start_check_task(struct check *check, int mininter,
Christopher Faulet61cc8522020-04-20 14:54:42 +02001484 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001485{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001486 struct task *t;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001487
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001488 /* task for the check. Process-based checks exclusively run on thread 1. */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001489 if (check->type == PR_O2_EXT_CHK)
Willy Tarreaua89c1912021-10-20 18:43:30 +02001490 t = task_new_on(0);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001491 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001492 t = task_new_anywhere();
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001493
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001494 if (!t)
1495 goto fail_alloc_task;
1496
Christopher Faulet61cc8522020-04-20 14:54:42 +02001497 check->task = t;
1498 t->process = process_chk;
1499 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001500
Christopher Faulet61cc8522020-04-20 14:54:42 +02001501 if (mininter < srv_getinter(check))
1502 mininter = srv_getinter(check);
1503
Willy Tarreaub93758c2023-05-17 07:39:57 +02001504 if (global.spread_checks > 0) {
1505 int rnd;
1506
1507 rnd = srv_getinter(check) * global.spread_checks / 100;
1508 rnd -= (int) (2 * rnd * (ha_random32() / 4294967295.0));
1509 mininter += rnd;
1510 }
1511
Christopher Faulet61cc8522020-04-20 14:54:42 +02001512 if (global.max_spread_checks && mininter > global.max_spread_checks)
1513 mininter = global.max_spread_checks;
1514
1515 /* check this every ms */
Willy Tarreauc7b93082023-05-16 19:19:36 +02001516 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Willy Tarreau69530f52023-04-28 09:16:15 +02001517 check->start = now_ns;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001518 task_queue(t);
1519
1520 return 1;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001521
1522 fail_alloc_task:
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001523 ha_alert("Starting [%s:%s] check: out of memory.\n",
1524 check->server->proxy->id, check->server->id);
1525 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001526}
1527
Christopher Faulet61cc8522020-04-20 14:54:42 +02001528/*
1529 * Start health-check.
1530 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1531 */
1532static int start_checks()
1533{
1534
1535 struct proxy *px;
1536 struct server *s;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001537 int nbcheck=0, mininter=0, srvpos=0;
1538
1539 /* 0- init the dummy frontend used to create all checks sessions */
1540 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001541 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001542 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1543 checks_fe.mode = PR_MODE_TCP;
1544 checks_fe.maxconn = 0;
1545 checks_fe.conn_retries = CONN_RETRIES;
1546 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1547 checks_fe.timeout.client = TICK_ETERNITY;
1548
1549 /* 1- count the checkers to run simultaneously.
1550 * We also determine the minimum interval among all of those which
1551 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1552 * will be used to spread their start-up date. Those which have
1553 * a shorter interval will start independently and will not dictate
1554 * too short an interval for all others.
1555 */
1556 for (px = proxies_list; px; px = px->next) {
1557 for (s = px->srv; s; s = s->next) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001558 if (s->check.state & CHK_ST_CONFIGURED) {
1559 nbcheck++;
1560 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1561 (!mininter || mininter > srv_getinter(&s->check)))
1562 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001563 }
1564
Christopher Faulet61cc8522020-04-20 14:54:42 +02001565 if (s->agent.state & CHK_ST_CONFIGURED) {
1566 nbcheck++;
1567 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1568 (!mininter || mininter > srv_getinter(&s->agent)))
1569 mininter = srv_getinter(&s->agent);
1570 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001571 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001572 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001573
Christopher Faulet61cc8522020-04-20 14:54:42 +02001574 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001575 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001576
Christopher Faulet61cc8522020-04-20 14:54:42 +02001577 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001578
William Dauchyf4300902021-02-06 20:47:50 +01001579 /* 2- start them as far as possible from each other. For this, we will
1580 * start them after their interval is set to the min interval divided
1581 * by the number of servers, weighted by the server's position in the
1582 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001583 */
1584 for (px = proxies_list; px; px = px->next) {
1585 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1586 if (init_pid_list()) {
1587 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1588 return ERR_ALERT | ERR_FATAL;
1589 }
1590 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001591
Christopher Faulet61cc8522020-04-20 14:54:42 +02001592 for (s = px->srv; s; s = s->next) {
1593 /* A task for the main check */
1594 if (s->check.state & CHK_ST_CONFIGURED) {
1595 if (s->check.type == PR_O2_EXT_CHK) {
1596 if (!prepare_external_check(&s->check))
1597 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001598 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001599 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1600 return ERR_ALERT | ERR_FATAL;
1601 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001602 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001603
Christopher Faulet61cc8522020-04-20 14:54:42 +02001604 /* A task for a auxiliary agent check */
1605 if (s->agent.state & CHK_ST_CONFIGURED) {
1606 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1607 return ERR_ALERT | ERR_FATAL;
1608 }
1609 srvpos++;
1610 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001611 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001612 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001613 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001614}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001615
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001616
Christopher Faulet61cc8522020-04-20 14:54:42 +02001617/*
1618 * Return value:
1619 * the port to be used for the health check
1620 * 0 in case no port could be found for the check
1621 */
1622static int srv_check_healthcheck_port(struct check *chk)
1623{
1624 int i = 0;
1625 struct server *srv = NULL;
1626
1627 srv = chk->server;
1628
William Dauchyf4300902021-02-06 20:47:50 +01001629 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001630 if (chk->port > 0)
1631 return chk->port;
1632
1633 /* try to get the port from check_core.addr if check.port not set */
1634 i = get_host_port(&chk->addr);
1635 if (i > 0)
1636 return i;
1637
1638 /* try to get the port from server address */
1639 /* prevent MAPPORTS from working at this point, since checks could
1640 * not be performed in such case (MAPPORTS impose a relative ports
1641 * based on live traffic)
1642 */
1643 if (srv->flags & SRV_F_MAPPORTS)
1644 return 0;
1645
1646 i = srv->svc_port; /* by default */
1647 if (i > 0)
1648 return i;
1649
1650 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001651}
1652
Christopher Faulet61cc8522020-04-20 14:54:42 +02001653/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1654 * if an error occurred.
1655 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001656int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001657{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001658 const char *err;
1659 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001660 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001661 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001662
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001663 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001664 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001665
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001666 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001667
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001668 if (!(srv->flags & SRV_F_DYNAMIC)) {
1669 /* If neither a port nor an addr was specified and no check
1670 * transport layer is forced, then the transport layer used by
1671 * the checks is the same as for the production traffic.
1672 * Otherwise we use raw_sock by default, unless one is
1673 * specified.
1674 */
1675 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1676 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1677 srv->check.use_ssl = srv->use_ssl;
1678 srv->check.xprt = srv->xprt;
1679 }
1680 else if (srv->check.use_ssl == 1)
1681 srv->check.xprt = xprt_get(XPRT_SSL);
1682 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001683 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001684 else if (srv->check.use_ssl == 1)
1685 srv->check.xprt = xprt_get(XPRT_SSL);
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001686 }
1687 else {
1688 /* For dynamic servers, check-ssl and check-send-proxy must be
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001689 * explicitly defined even if the check port was not
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001690 * overridden.
1691 */
1692 if (srv->check.use_ssl == 1)
1693 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001694 }
1695
Christopher Faulet12882cf2020-04-23 15:50:18 +02001696 /* Inherit the mux protocol from the server if not already defined for
1697 * the check
1698 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001699 if (srv->mux_proto && !srv->check.mux_proto &&
1700 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1701 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001702 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001703 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001704 /* test that check proto is valid if explicitly defined */
1705 else if (srv->check.mux_proto &&
1706 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1707 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1708 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1709 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1710 ret |= ERR_ALERT | ERR_FATAL;
1711 goto out;
1712 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001713
Christopher Faulet61cc8522020-04-20 14:54:42 +02001714 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001715
Christopher Faulet61cc8522020-04-20 14:54:42 +02001716 /* We need at least a service port, a check port or the first tcp-check
1717 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1718 */
1719 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1720 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1721 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001722
Christopher Faulet61cc8522020-04-20 14:54:42 +02001723 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1724 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1725 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1726 ret |= ERR_ALERT | ERR_ABORT;
1727 goto out;
1728 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001729
Christopher Faulet61cc8522020-04-20 14:54:42 +02001730 /* search the first action (connect / send / expect) in the list */
1731 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1732 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1733 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1734 "nor tcp_check rule 'connect' with port information.\n",
1735 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1736 ret |= ERR_ALERT | ERR_ABORT;
1737 goto out;
1738 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001739
Christopher Faulet61cc8522020-04-20 14:54:42 +02001740 /* scan the tcp-check ruleset to ensure a port has been configured */
1741 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreauacff3092021-07-22 11:06:41 +02001742 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001743 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1744 "and a tcp_check rule 'connect' with no port information.\n",
1745 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1746 ret |= ERR_ALERT | ERR_ABORT;
1747 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001748 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001749 }
1750
Christopher Faulet61cc8522020-04-20 14:54:42 +02001751 init:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001752 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1753 if (err) {
1754 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1755 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1756 ret |= ERR_ALERT | ERR_ABORT;
1757 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001758 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001759 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001760 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001761
1762 /* Only increment maxsock for servers from the configuration. Dynamic
1763 * servers at the moment are not taken into account for the estimation
1764 * of the resources limits.
1765 */
1766 if (global.mode & MODE_STARTING)
1767 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001768
Christopher Faulet61cc8522020-04-20 14:54:42 +02001769 out:
1770 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001771}
1772
Christopher Faulet61cc8522020-04-20 14:54:42 +02001773/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1774 * if an error occurred.
1775 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001776int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001777{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001778 struct tcpcheck_rule *chk;
1779 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001780 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001781
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001782 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001783 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001784
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001785 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001786 * implicit one is inserted before all others.
1787 */
1788 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1789 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1790 chk = calloc(1, sizeof(*chk));
1791 if (!chk) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001792 ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
Christopher Faulet61cc8522020-04-20 14:54:42 +02001793 " to agent-check for server '%s' (out of memory).\n",
1794 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1795 ret |= ERR_ALERT | ERR_FATAL;
1796 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001797 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001798 chk->action = TCPCHK_ACT_CONNECT;
1799 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001800 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001801 }
1802
Christopher Faulet871dd822022-08-24 11:38:03 +02001803 /* <chk> is always defined here and it is a CONNECT action. If there is
1804 * a preset variable, it means there is an agent string defined and data
1805 * will be sent after the connect.
1806 */
1807 if (!LIST_ISEMPTY(&srv->agent.tcpcheck_rules->preset_vars))
1808 chk->connect.options |= TCPCHK_OPT_HAS_DATA;
1809
Christopher Faulete5870d82020-04-15 11:32:03 +02001810
Christopher Faulet61cc8522020-04-20 14:54:42 +02001811 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1812 if (err) {
1813 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1814 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1815 ret |= ERR_ALERT | ERR_ABORT;
1816 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001817 }
1818
Christopher Faulet61cc8522020-04-20 14:54:42 +02001819 if (!srv->agent.inter)
1820 srv->agent.inter = srv->check.inter;
1821
1822 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001823 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001824
1825 /* Only increment maxsock for servers from the configuration. Dynamic
1826 * servers at the moment are not taken into account for the estimation
1827 * of the resources limits.
1828 */
1829 if (global.mode & MODE_STARTING)
1830 global.maxsock++;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001831
1832 out:
1833 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001834}
1835
Christopher Faulet61cc8522020-04-20 14:54:42 +02001836static void deinit_srv_check(struct server *srv)
1837{
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01001838 if (srv->check.state & CHK_ST_CONFIGURED) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001839 free_check(&srv->check);
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01001840 /* it is safe to drop now since the main server reference is still held by the proxy */
1841 srv_drop(srv);
1842 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001843 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1844 srv->do_check = 0;
1845}
Christopher Faulete5870d82020-04-15 11:32:03 +02001846
Christopher Faulet61cc8522020-04-20 14:54:42 +02001847
1848static void deinit_srv_agent_check(struct server *srv)
1849{
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01001850 if (srv->agent.state & CHK_ST_CONFIGURED) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001851 free_check(&srv->agent);
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01001852 /* it is safe to drop now since the main server reference is still held by the proxy */
1853 srv_drop(srv);
1854 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001855
1856 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1857 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001858}
1859
Willy Tarreaucee013e2020-06-05 11:40:38 +02001860REGISTER_POST_SERVER_CHECK(init_srv_check);
1861REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001862REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001863
Willy Tarreaucee013e2020-06-05 11:40:38 +02001864REGISTER_SERVER_DEINIT(deinit_srv_check);
1865REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001866
Christopher Faulet61cc8522020-04-20 14:54:42 +02001867
1868/**************************************************************************/
1869/************************** Check sample fetches **************************/
1870/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001871
Christopher Faulet61cc8522020-04-20 14:54:42 +02001872static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001873 { /* END */ },
1874}};
1875
1876INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1877
1878
1879/**************************************************************************/
1880/************************ Check's parsing functions ***********************/
1881/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001882/* Parse the "addr" server keyword */
1883static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1884 char **errmsg)
1885{
1886 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001887 int port1, port2, err_code = 0;
1888
1889
1890 if (!*args[*cur_arg+1]) {
1891 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1892 goto error;
1893 }
1894
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001895 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1896 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001897 if (!sk) {
1898 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1899 goto error;
1900 }
1901
William Dauchy1c921cd2021-02-03 22:30:08 +01001902 srv->check.addr = *sk;
1903 /* if agentaddr was never set, we can use addr */
1904 if (!(srv->flags & SRV_F_AGENTADDR))
1905 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001906
1907 out:
1908 return err_code;
1909
1910 error:
1911 err_code |= ERR_ALERT | ERR_FATAL;
1912 goto out;
1913}
1914
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001915/* Parse the "agent-addr" server keyword */
1916static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1917 char **errmsg)
1918{
William Dauchy1c921cd2021-02-03 22:30:08 +01001919 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001920 int err_code = 0;
1921
1922 if (!*(args[*cur_arg+1])) {
1923 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1924 goto error;
1925 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001926 memset(&sk, 0, sizeof(sk));
1927 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001928 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1929 goto error;
1930 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001931 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001932
1933 out:
1934 return err_code;
1935
1936 error:
1937 err_code |= ERR_ALERT | ERR_FATAL;
1938 goto out;
1939}
1940
1941/* Parse the "agent-check" server keyword */
1942static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1943 char **errmsg)
1944{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001945 struct tcpcheck_ruleset *rs = NULL;
1946 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1947 struct tcpcheck_rule *chk;
1948 int err_code = 0;
1949
1950 if (srv->do_agent)
1951 goto out;
1952
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001953 if (!(curpx->cap & PR_CAP_BE)) {
1954 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1955 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1956 return ERR_WARN;
1957 }
1958
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001959 if (!rules) {
1960 rules = calloc(1, sizeof(*rules));
1961 if (!rules) {
1962 memprintf(errmsg, "out of memory.");
1963 goto error;
1964 }
1965 LIST_INIT(&rules->preset_vars);
1966 srv->agent.tcpcheck_rules = rules;
1967 }
1968 rules->list = NULL;
1969 rules->flags = 0;
1970
Christopher Faulet61cc8522020-04-20 14:54:42 +02001971 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001972 if (rs)
1973 goto ruleset_found;
1974
Christopher Faulet61cc8522020-04-20 14:54:42 +02001975 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001976 if (rs == NULL) {
1977 memprintf(errmsg, "out of memory.");
1978 goto error;
1979 }
1980
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001981 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001982 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1983 if (!chk) {
1984 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1985 goto error;
1986 }
1987 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001988 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001989
1990 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001991 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1992 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001993 if (!chk) {
1994 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1995 goto error;
1996 }
1997 chk->expect.custom = tcpcheck_agent_expect_reply;
1998 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001999 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002000
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002001 ruleset_found:
2002 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01002003 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02002004 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002005 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002006
2007 out:
Dirkjan Bussinkdfee2172021-06-18 19:57:49 +00002008 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002009
2010 error:
2011 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02002012 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002013 err_code |= ERR_ALERT | ERR_FATAL;
2014 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002015}
2016
2017/* Parse the "agent-inter" server keyword */
2018static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2019 char **errmsg)
2020{
2021 const char *err = NULL;
2022 unsigned int delay;
2023 int err_code = 0;
2024
2025 if (!*(args[*cur_arg+1])) {
2026 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2027 goto error;
2028 }
2029
2030 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2031 if (err == PARSE_TIME_OVER) {
2032 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2033 args[*cur_arg+1], args[*cur_arg], srv->id);
2034 goto error;
2035 }
2036 else if (err == PARSE_TIME_UNDER) {
2037 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2038 args[*cur_arg+1], args[*cur_arg], srv->id);
2039 goto error;
2040 }
2041 else if (err) {
2042 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2043 *err, srv->id);
2044 goto error;
2045 }
2046 if (delay <= 0) {
2047 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2048 delay, args[*cur_arg], srv->id);
2049 goto error;
2050 }
2051 srv->agent.inter = delay;
2052
2053 out:
2054 return err_code;
2055
2056 error:
2057 err_code |= ERR_ALERT | ERR_FATAL;
2058 goto out;
2059}
2060
2061/* Parse the "agent-port" server keyword */
2062static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2063 char **errmsg)
2064{
2065 int err_code = 0;
2066
2067 if (!*(args[*cur_arg+1])) {
2068 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2069 goto error;
2070 }
2071
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002072 /* Only increment maxsock for servers from the configuration. Dynamic
2073 * servers at the moment are not taken into account for the estimation
2074 * of the resources limits.
2075 */
2076 if (global.mode & MODE_STARTING)
2077 global.maxsock++;
2078
William Dauchy4858fb22021-02-03 22:30:09 +01002079 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002080
2081 out:
2082 return err_code;
2083
2084 error:
2085 err_code |= ERR_ALERT | ERR_FATAL;
2086 goto out;
2087}
2088
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002089int set_srv_agent_send(struct server *srv, const char *send)
2090{
2091 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
2092 struct tcpcheck_var *var = NULL;
2093 char *str;
2094
2095 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02002096 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002097 if (str == NULL || var == NULL)
2098 goto error;
2099
2100 free_tcpcheck_vars(&rules->preset_vars);
2101
2102 var->data.type = SMP_T_STR;
2103 var->data.u.str.area = str;
2104 var->data.u.str.data = strlen(str);
2105 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02002106 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002107
2108 return 1;
2109
2110 error:
2111 free(str);
2112 free(var);
2113 return 0;
2114}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002115
2116/* Parse the "agent-send" server keyword */
2117static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2118 char **errmsg)
2119{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002120 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002121 int err_code = 0;
2122
2123 if (!*(args[*cur_arg+1])) {
2124 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2125 goto error;
2126 }
2127
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002128 if (!rules) {
2129 rules = calloc(1, sizeof(*rules));
2130 if (!rules) {
2131 memprintf(errmsg, "out of memory.");
2132 goto error;
2133 }
2134 LIST_INIT(&rules->preset_vars);
2135 srv->agent.tcpcheck_rules = rules;
2136 }
2137
2138 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002139 memprintf(errmsg, "out of memory.");
2140 goto error;
2141 }
2142
2143 out:
2144 return err_code;
2145
2146 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002147 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002148 err_code |= ERR_ALERT | ERR_FATAL;
2149 goto out;
2150}
2151
2152/* Parse the "no-agent-send" server keyword */
2153static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2154 char **errmsg)
2155{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002156 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002157 return 0;
2158}
2159
Christopher Fauletce8111e2020-04-06 15:04:11 +02002160/* Parse the "check" server keyword */
2161static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2162 char **errmsg)
2163{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002164 if (!(curpx->cap & PR_CAP_BE)) {
2165 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2166 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2167 return ERR_WARN;
2168 }
2169
Christopher Fauletce8111e2020-04-06 15:04:11 +02002170 srv->do_check = 1;
2171 return 0;
2172}
2173
2174/* Parse the "check-send-proxy" server keyword */
2175static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2176 char **errmsg)
2177{
2178 srv->check.send_proxy = 1;
2179 return 0;
2180}
2181
2182/* Parse the "check-via-socks4" server keyword */
2183static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2184 char **errmsg)
2185{
2186 srv->check.via_socks4 = 1;
2187 return 0;
2188}
2189
2190/* Parse the "no-check" server keyword */
2191static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2192 char **errmsg)
2193{
2194 deinit_srv_check(srv);
2195 return 0;
2196}
2197
2198/* Parse the "no-check-send-proxy" server keyword */
2199static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2200 char **errmsg)
2201{
2202 srv->check.send_proxy = 0;
2203 return 0;
2204}
2205
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002206/* parse the "check-proto" server keyword */
2207static int srv_parse_check_proto(char **args, int *cur_arg,
2208 struct proxy *px, struct server *newsrv, char **err)
2209{
2210 int err_code = 0;
2211
2212 if (!*args[*cur_arg + 1]) {
2213 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2214 goto error;
2215 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002216 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002217 if (!newsrv->check.mux_proto) {
2218 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2219 goto error;
2220 }
2221
2222 out:
2223 return err_code;
2224
2225 error:
2226 err_code |= ERR_ALERT | ERR_FATAL;
2227 goto out;
2228}
2229
2230
Christopher Fauletce8111e2020-04-06 15:04:11 +02002231/* Parse the "rise" server keyword */
2232static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2233 char **errmsg)
2234{
2235 int err_code = 0;
2236
2237 if (!*args[*cur_arg + 1]) {
2238 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2239 goto error;
2240 }
2241
2242 srv->check.rise = atol(args[*cur_arg+1]);
2243 if (srv->check.rise <= 0) {
2244 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2245 goto error;
2246 }
2247
2248 if (srv->check.health)
2249 srv->check.health = srv->check.rise;
2250
2251 out:
2252 return err_code;
2253
2254 error:
2255 deinit_srv_agent_check(srv);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002258}
2259
2260/* Parse the "fall" server keyword */
2261static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2262 char **errmsg)
2263{
2264 int err_code = 0;
2265
2266 if (!*args[*cur_arg + 1]) {
2267 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2268 goto error;
2269 }
2270
2271 srv->check.fall = atol(args[*cur_arg+1]);
2272 if (srv->check.fall <= 0) {
2273 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2274 goto error;
2275 }
2276
2277 out:
2278 return err_code;
2279
2280 error:
2281 deinit_srv_agent_check(srv);
2282 err_code |= ERR_ALERT | ERR_FATAL;
2283 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002284}
2285
2286/* Parse the "inter" server keyword */
2287static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2288 char **errmsg)
2289{
2290 const char *err = NULL;
2291 unsigned int delay;
2292 int err_code = 0;
2293
2294 if (!*(args[*cur_arg+1])) {
2295 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2296 goto error;
2297 }
2298
2299 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2300 if (err == PARSE_TIME_OVER) {
2301 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2302 args[*cur_arg+1], args[*cur_arg], srv->id);
2303 goto error;
2304 }
2305 else if (err == PARSE_TIME_UNDER) {
2306 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2307 args[*cur_arg+1], args[*cur_arg], srv->id);
2308 goto error;
2309 }
2310 else if (err) {
2311 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2312 *err, srv->id);
2313 goto error;
2314 }
2315 if (delay <= 0) {
2316 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2317 delay, args[*cur_arg], srv->id);
2318 goto error;
2319 }
2320 srv->check.inter = delay;
2321
2322 out:
2323 return err_code;
2324
2325 error:
2326 err_code |= ERR_ALERT | ERR_FATAL;
2327 goto out;
2328}
2329
2330
2331/* Parse the "fastinter" server keyword */
2332static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2333 char **errmsg)
2334{
2335 const char *err = NULL;
2336 unsigned int delay;
2337 int err_code = 0;
2338
2339 if (!*(args[*cur_arg+1])) {
2340 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2341 goto error;
2342 }
2343
2344 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2345 if (err == PARSE_TIME_OVER) {
2346 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2347 args[*cur_arg+1], args[*cur_arg], srv->id);
2348 goto error;
2349 }
2350 else if (err == PARSE_TIME_UNDER) {
2351 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2352 args[*cur_arg+1], args[*cur_arg], srv->id);
2353 goto error;
2354 }
2355 else if (err) {
2356 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2357 *err, srv->id);
2358 goto error;
2359 }
2360 if (delay <= 0) {
2361 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2362 delay, args[*cur_arg], srv->id);
2363 goto error;
2364 }
2365 srv->check.fastinter = delay;
2366
2367 out:
2368 return err_code;
2369
2370 error:
2371 err_code |= ERR_ALERT | ERR_FATAL;
2372 goto out;
2373}
2374
2375
2376/* Parse the "downinter" server keyword */
2377static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2378 char **errmsg)
2379{
2380 const char *err = NULL;
2381 unsigned int delay;
2382 int err_code = 0;
2383
2384 if (!*(args[*cur_arg+1])) {
2385 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2386 goto error;
2387 }
2388
2389 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2390 if (err == PARSE_TIME_OVER) {
2391 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2392 args[*cur_arg+1], args[*cur_arg], srv->id);
2393 goto error;
2394 }
2395 else if (err == PARSE_TIME_UNDER) {
2396 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2397 args[*cur_arg+1], args[*cur_arg], srv->id);
2398 goto error;
2399 }
2400 else if (err) {
2401 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2402 *err, srv->id);
2403 goto error;
2404 }
2405 if (delay <= 0) {
2406 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2407 delay, args[*cur_arg], srv->id);
2408 goto error;
2409 }
2410 srv->check.downinter = delay;
2411
2412 out:
2413 return err_code;
2414
2415 error:
2416 err_code |= ERR_ALERT | ERR_FATAL;
2417 goto out;
2418}
2419
2420/* Parse the "port" server keyword */
2421static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2422 char **errmsg)
2423{
2424 int err_code = 0;
2425
2426 if (!*(args[*cur_arg+1])) {
2427 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2428 goto error;
2429 }
2430
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002431 /* Only increment maxsock for servers from the configuration. Dynamic
2432 * servers at the moment are not taken into account for the estimation
2433 * of the resources limits.
2434 */
2435 if (global.mode & MODE_STARTING)
2436 global.maxsock++;
2437
Christopher Fauletce8111e2020-04-06 15:04:11 +02002438 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002439 /* if agentport was never set, we can use port */
2440 if (!(srv->flags & SRV_F_AGENTPORT))
2441 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002442
2443 out:
2444 return err_code;
2445
2446 error:
2447 err_code |= ERR_ALERT | ERR_FATAL;
2448 goto out;
2449}
2450
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002451static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002452 { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */
2453 { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02002454 { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002455 { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */
2456 { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */
2457 { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02002458 { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002459 { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */
2460 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */
2461 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002462 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2463 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2464 { "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 +02002465 { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */
2466 { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */
2467 { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */
2468 { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */
2469 { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */
2470 { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002471 { NULL, NULL, 0 },
2472}};
2473
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002474INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002475
Willy Tarreaubd741542010-03-16 18:46:54 +01002476/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002477 * Local variables:
2478 * c-indent-level: 8
2479 * c-basic-offset: 8
2480 * End:
2481 */