blob: 92d4bdcf8cdc6d07893c46e035ba3d8535ab3e71 [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
Willy Tarreau4596fe22022-05-17 19:07:51 +0200143static int wake_srv_chk(struct stconn *cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200144struct data_cb check_conn_cb = {
145 .wake = wake_srv_chk,
146 .name = "CHCK",
147};
Christopher Fauletd7e63962020-04-17 20:15:59 +0200148
Christopher Faulet5d503fc2020-03-30 20:34:34 +0200149
Gaetan Rivet05d692d2020-02-14 17:42:54 +0100150/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200151struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +0200152
Christopher Faulet147b8c92021-04-10 09:00:38 +0200153
154static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
155{
156 size_t block1, block2;
157 int line, ptr, newptr;
158
159 block1 = b_contig_data(buf, ofs);
160 block2 = 0;
161 if (block1 > len)
162 block1 = len;
163 block2 = len - block1;
164
165 ofs = b_peek_ofs(buf, ofs);
166
167 line = 0;
168 ptr = ofs;
169 while (ptr < ofs + block1) {
170 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
171 if (newptr == ptr)
172 break;
173 ptr = newptr;
174 }
175
176 line = ptr = 0;
177 while (ptr < block2) {
178 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
179 if (newptr == ptr)
180 break;
181 ptr = newptr;
182 }
183}
184
185/* trace source and events */
186static void check_trace(enum trace_level level, uint64_t mask,
187 const struct trace_source *src,
188 const struct ist where, const struct ist func,
189 const void *a1, const void *a2, const void *a3, const void *a4)
190{
191 const struct check *check = a1;
192 const struct server *srv = (check ? check->server : NULL);
193 const size_t *val = a4;
194 const char *res;
195
196 if (!check || src->verbosity < CHK_VERB_CLEAN)
197 return;
198
199 chunk_appendf(&trace_buf, " : [%c] SRV=%s",
200 ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
201 srv->id);
202
203 chunk_appendf(&trace_buf, " status=%d/%d %s",
204 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
205 (check->health >= check->rise) ? check->fall : check->rise,
206 (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN");
207
208 switch (check->result) {
209 case CHK_RES_NEUTRAL: res = "-"; break;
210 case CHK_RES_FAILED: res = "FAIL"; break;
211 case CHK_RES_PASSED: res = "PASS"; break;
212 case CHK_RES_CONDPASS: res = "COND"; break;
213 default: res = "UNK"; break;
214 }
215
216 if (src->verbosity == CHK_VERB_CLEAN)
217 return;
218
219 chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
220 get_check_status_info(check->status), check->status,
221 res, check->result);
222
223 /* Display the value to the 4th argument (level > STATE) */
224 if (src->level > TRACE_LEVEL_STATE && val)
225 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
226
227 chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
228
229 if (src->verbosity == CHK_VERB_MINIMAL)
230 return;
231
232
233 if (check->cs) {
Christopher Faulet0256da12021-12-15 09:50:17 +0100234 struct connection *conn = cs_conn(check->cs);
235
236 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200237 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags);
238 }
239
240 if (mask & CHK_EV_TCPCHK) {
241 const char *type;
242
243 switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) {
244 case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break;
245 case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break;
246 case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break;
247 case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break;
248 case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break;
249 case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break;
250 case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break;
251 case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break;
252 case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break;
253 case TCPCHK_RULES_TCP_CHK: type = "TCP"; break;
254 default: type = "???"; break;
255 }
256 if (check->current_step)
257 chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
258 else
259 chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
260 }
261
262 /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
263 if (src->level > TRACE_LEVEL_USER) {
264 const struct buffer *buf = NULL;
265
266 chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
267 (unsigned int)b_data(&check->bi), b_orig(&check->bi),
268 (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
269 chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
270 (unsigned int)b_data(&check->bo), b_orig(&check->bo),
271 (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
272
273 if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
274 buf = (b_is_null(&check->bi) ? NULL : &check->bi);
275 else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
276 buf = (b_is_null(&check->bo) ? NULL : &check->bo);
277
278 if (buf) {
279 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
280 int full = (src->verbosity == CHK_VERB_COMPLETE);
281
282 chunk_memcat(&trace_buf, "\n\t", 2);
283 htx_dump(&trace_buf, htxbuf(buf), full);
284 }
285 else {
286 int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
287
288 chunk_memcat(&trace_buf, "\n", 1);
289 if (b_data(buf) > max) {
290 check_trace_buf(buf, 0, max);
291 chunk_memcat(&trace_buf, " ...\n", 6);
292 }
293 else
294 check_trace_buf(buf, 0, b_data(buf));
295 }
296
297 }
298 }
299
300}
301
302
Christopher Faulet61cc8522020-04-20 14:54:42 +0200303/**************************************************************************/
304/************************ Handle check results ****************************/
305/**************************************************************************/
306struct check_status {
307 short result; /* one of SRV_CHK_* */
308 char *info; /* human readable short info */
309 char *desc; /* long description */
310};
311
312struct analyze_status {
313 char *desc; /* description */
314 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
315};
316
Simon Horman63a4a822012-03-19 07:24:41 +0900317static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100318 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
319 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200320 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200321
Willy Tarreau23964182014-05-20 20:56:30 +0200322 /* Below we have finished checks */
323 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100324 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100325
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100326 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200327
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100328 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
329 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
330 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200331
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100332 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
333 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
334 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200335
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100336 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
337 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200338
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200339 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200340
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100341 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
342 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
343 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900344
345 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
346 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200347 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200348};
349
Simon Horman63a4a822012-03-19 07:24:41 +0900350static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100351 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
352
353 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
354 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
355
356 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
357 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
358 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
359 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
360
361 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
362 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
363 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
364};
365
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100366/* checks if <err> is a real error for errno or one that can be ignored, and
367 * return 0 for these ones or <err> for real ones.
368 */
369static inline int unclean_errno(int err)
370{
Willy Tarreauacef5e22022-04-25 20:32:15 +0200371 if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS ||
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100372 err == EISCONN || err == EALREADY)
373 return 0;
374 return err;
375}
376
Christopher Faulet7aa32712021-02-01 13:11:50 +0100377/* Converts check_status code to result code */
378short get_check_status_result(short check_status)
379{
380 if (check_status < HCHK_STATUS_SIZE)
381 return check_statuses[check_status].result;
382 else
383 return check_statuses[HCHK_STATUS_UNKNOWN].result;
384}
385
Christopher Faulet61cc8522020-04-20 14:54:42 +0200386/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200387const char *get_check_status_description(short check_status) {
388
389 const char *desc;
390
391 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200392 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200393 else
394 desc = NULL;
395
396 if (desc && *desc)
397 return desc;
398 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200399 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200400}
401
Christopher Faulet61cc8522020-04-20 14:54:42 +0200402/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100403const char *get_check_status_info(short check_status)
404{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200405 const char *info;
406
407 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200408 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200409 else
410 info = NULL;
411
412 if (info && *info)
413 return info;
414 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200415 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200416}
417
Christopher Faulet61cc8522020-04-20 14:54:42 +0200418/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100419const char *get_analyze_status(short analyze_status) {
420
421 const char *desc;
422
423 if (analyze_status < HANA_STATUS_SIZE)
424 desc = analyze_statuses[analyze_status].desc;
425 else
426 desc = NULL;
427
428 if (desc && *desc)
429 return desc;
430 else
431 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
432}
433
Christopher Faulet61cc8522020-04-20 14:54:42 +0200434/* Sets check->status, update check->duration and fill check->result with an
435 * adequate CHK_RES_* value. The new check->health is computed based on the
436 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200437 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200438 * Shows information in logs about failed health check if server is UP or
439 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200440 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200441void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100442{
Simon Horman4a741432013-02-23 15:35:38 +0900443 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200444 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200445 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900446
Christopher Faulet147b8c92021-04-10 09:00:38 +0200447 TRACE_POINT(CHK_EV_HCHK_RUN, check);
448
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200449 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100450 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900451 check->desc[0] = '\0';
452 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200453 return;
454 }
455
Simon Horman4a741432013-02-23 15:35:38 +0900456 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200457 return;
458
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200459 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900460 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
461 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200462 } else
Simon Horman4a741432013-02-23 15:35:38 +0900463 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200464
Simon Horman4a741432013-02-23 15:35:38 +0900465 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200466 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900467 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200468
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100469 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900470 check->duration = -1;
471 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200472 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900473 check->duration = tv_ms_elapsed(&check->start, &now);
474 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200475 }
476
Willy Tarreau23964182014-05-20 20:56:30 +0200477 /* no change is expected if no state change occurred */
478 if (check->result == CHK_RES_NEUTRAL)
479 return;
480
Olivier Houchard0923fa42019-01-11 18:43:04 +0100481 /* If the check was really just sending a mail, it won't have an
482 * associated server, so we're done now.
483 */
484 if (!s)
485 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200486 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200487
Christopher Faulet147b8c92021-04-10 09:00:38 +0200488
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200489 switch (check->result) {
490 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200491 /* Failure to connect to the agent as a secondary check should not
492 * cause the server to be marked down.
493 */
494 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900495 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200496 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200497 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200498 report = 1;
499 check->health--;
500 if (check->health < check->rise)
501 check->health = 0;
502 }
503 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200504
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200505 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100506 case CHK_RES_CONDPASS:
507 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200508 report = 1;
509 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200510
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200511 if (check->health >= check->rise)
512 check->health = check->rise + check->fall - 1; /* OK now */
513 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200514
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200515 /* clear consecutive_errors if observing is enabled */
516 if (s->onerror)
517 s->consecutive_errors = 0;
518 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100519
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200520 default:
521 break;
522 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200523
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200524 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
525 (status != prev_status || report)) {
526 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200527 "%s check for %sserver %s/%s %s%s",
528 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200529 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100530 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100531 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200532 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200533
Emeric Brun5a133512017-10-19 14:42:30 +0200534 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200535
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100536 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200537 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
538 (check->health >= check->rise) ? check->fall : check->rise,
539 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200540
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200541 ha_warning("%s.\n", trash.area);
542 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
543 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200544 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200545}
546
Willy Tarreau4eec5472014-05-20 22:32:27 +0200547/* Marks the check <check>'s server down if the current check is already failed
548 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200549 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200550void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551{
Simon Horman4a741432013-02-23 15:35:38 +0900552 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900553
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200554 /* The agent secondary check should only cause a server to be marked
555 * as down if check->status is HCHK_STATUS_L7STS, which indicates
556 * that the agent returned "fail", "stopped" or "down".
557 * The implication here is that failure to connect to the agent
558 * as a secondary check should not cause the server to be marked
559 * down. */
560 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
561 return;
562
Willy Tarreau4eec5472014-05-20 22:32:27 +0200563 if (check->health > 0)
564 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100565
Christopher Faulet147b8c92021-04-10 09:00:38 +0200566 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200567 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200568 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569}
570
Willy Tarreauaf549582014-05-16 17:37:50 +0200571/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200572 * it isn't in maintenance, it is not tracking a down server and other checks
573 * comply. The rule is simple : by default, a server is up, unless any of the
574 * following conditions is true :
575 * - health check failed (check->health < rise)
576 * - agent check failed (agent->health < rise)
577 * - the server tracks a down server (track && track->state == STOPPED)
578 * Note that if the server has a slowstart, it will switch to STARTING instead
579 * of RUNNING. Also, only the health checks support the nolb mode, so the
580 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200581 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200582void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200583{
Simon Horman4a741432013-02-23 15:35:38 +0900584 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100585
Emeric Brun52a91d32017-08-31 14:41:55 +0200586 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200587 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100588
Emeric Brun52a91d32017-08-31 14:41:55 +0200589 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200590 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100591
Willy Tarreau3e048382014-05-21 10:30:54 +0200592 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
593 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100594
Willy Tarreau3e048382014-05-21 10:30:54 +0200595 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
596 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200597
Emeric Brun52a91d32017-08-31 14:41:55 +0200598 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200599 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100600
Christopher Faulet147b8c92021-04-10 09:00:38 +0200601 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Emeric Brun5a133512017-10-19 14:42:30 +0200602 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100603}
604
Willy Tarreaudb58b792014-05-21 13:57:23 +0200605/* Marks the check <check> as valid and tries to set its server into stopping mode
606 * if it was running or starting, and provided it isn't in maintenance and other
607 * checks comply. The conditions for the server to be marked in stopping mode are
608 * the same as for it to be turned up. Also, only the health checks support the
609 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200610 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200611void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200612{
Simon Horman4a741432013-02-23 15:35:38 +0900613 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100614
Emeric Brun52a91d32017-08-31 14:41:55 +0200615 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200616 return;
617
Willy Tarreaudb58b792014-05-21 13:57:23 +0200618 if (check->state & CHK_ST_AGENT)
619 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100620
Emeric Brun52a91d32017-08-31 14:41:55 +0200621 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200622 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100623
Willy Tarreaudb58b792014-05-21 13:57:23 +0200624 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
625 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100626
Willy Tarreaudb58b792014-05-21 13:57:23 +0200627 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
628 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100629
Christopher Faulet147b8c92021-04-10 09:00:38 +0200630 TRACE_STATE("health-check condionnaly succeeded, set server STOPPING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Willy Tarreaub26881a2017-12-23 11:16:49 +0100631 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100632}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100634/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100635 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100636 */
637void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100638{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100639 int failed;
640 int expire;
641
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100642 if (s->observe >= HANA_OBS_SIZE)
643 return;
644
Willy Tarreaubb956662013-01-24 00:37:39 +0100645 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100646 return;
647
648 switch (analyze_statuses[status].lr[s->observe - 1]) {
649 case 1:
650 failed = 1;
651 break;
652
653 case 2:
654 failed = 0;
655 break;
656
657 default:
658 return;
659 }
660
661 if (!failed) {
662 /* good: clear consecutive_errors */
663 s->consecutive_errors = 0;
664 return;
665 }
666
Willy Tarreau4781b152021-04-06 13:53:36 +0200667 _HA_ATOMIC_INC(&s->consecutive_errors);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100668
669 if (s->consecutive_errors < s->consecutive_errors_limit)
670 return;
671
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100672 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
673 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100674
Willy Tarreau4e9df272021-02-17 15:20:19 +0100675 if (s->check.fastinter)
676 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
677 else
678 expire = TICK_ETERNITY;
679
680 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
681
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100682 switch (s->onerror) {
683 case HANA_ONERR_FASTINTER:
684 /* force fastinter - nothing to do here as all modes force it */
685 break;
686
687 case HANA_ONERR_SUDDTH:
688 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900689 if (s->check.health > s->check.rise)
690 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100691
Tim Duesterhus588b3142020-05-29 14:35:51 +0200692 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100693
694 case HANA_ONERR_FAILCHK:
695 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 set_server_check_status(&s->check, HCHK_STATUS_HANA,
697 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200698 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100699 break;
700
701 case HANA_ONERR_MARKDWN:
702 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900703 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200704 set_server_check_status(&s->check, HCHK_STATUS_HANA,
705 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200706 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100707 break;
708
709 default:
710 /* write a warning? */
711 break;
712 }
713
Willy Tarreau4e9df272021-02-17 15:20:19 +0100714 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
715
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100716 s->consecutive_errors = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200717 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100718
Christopher Fauletea860832021-05-07 11:45:26 +0200719 if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) {
Willy Tarreau4e9df272021-02-17 15:20:19 +0100720 /* requeue check task with new expire */
721 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100722 }
Willy Tarreauef781042010-01-27 11:53:01 +0100723}
724
Christopher Faulet61cc8522020-04-20 14:54:42 +0200725/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100726 * closed, keep errno intact as it is supposed to contain the valid error code.
727 * If no error is reported, check the socket's error queue using getsockopt().
728 * Warning, this must be done only once when returning from poll, and never
729 * after an I/O error was attempted, otherwise the error queue might contain
730 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
731 * socket. Returns non-zero if an error was reported, zero if everything is
732 * clean (including a properly closed socket).
733 */
734static int retrieve_errno_from_socket(struct connection *conn)
735{
736 int skerr;
737 socklen_t lskerr = sizeof(skerr);
738
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100739 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100740 return 1;
741
Willy Tarreau3c728722014-01-23 13:50:42 +0100742 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100743 return 0;
744
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200745 BUG_ON(conn->flags & CO_FL_FDLESS);
746
Willy Tarreau585744b2017-08-24 14:31:19 +0200747 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100748 errno = skerr;
749
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100750 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100751
752 if (!errno) {
753 /* we could not retrieve an error, that does not mean there is
754 * none. Just don't change anything and only report the prior
755 * error if any.
756 */
757 if (conn->flags & CO_FL_ERROR)
758 return 1;
759 else
760 return 0;
761 }
762
763 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
764 return 1;
765}
766
Christopher Faulet61cc8522020-04-20 14:54:42 +0200767/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100768 * and adjust the server status accordingly. It may make use of <errno_bck>
769 * if non-null when the caller is absolutely certain of its validity (eg:
770 * checked just after a syscall). If the caller doesn't have a valid errno,
771 * it can pass zero, and retrieve_errno_from_socket() will be called to try
772 * to extract errno from the socket. If no error is reported, it will consider
773 * the <expired> flag. This is intended to be used when a connection error was
774 * reported in conn->flags or when a timeout was reported in <expired>. The
775 * function takes care of not updating a server status which was already set.
776 * All situations where at least one of <expired> or CO_FL_ERROR are set
777 * produce a status.
778 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200779void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100780{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200781 struct stconn *cs = check->cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200782 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100783 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200784 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200785 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100786
Christopher Faulet147b8c92021-04-10 09:00:38 +0200787 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100788 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200789 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100790
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100791 errno = unclean_errno(errno_bck);
792 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100793 retrieve_errno_from_socket(conn);
794
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200795 if (conn && !(conn->flags & CO_FL_ERROR) &&
Willy Tarreaub605c422022-05-17 17:04:55 +0200796 cs && !sc_ep_test(cs, SE_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100797 return;
798
Christopher Faulet147b8c92021-04-10 09:00:38 +0200799 TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
800
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100801 /* we'll try to build a meaningful error message depending on the
802 * context of the error possibly present in conn->err_code, and the
803 * socket error possibly collected above. This is useful to know the
804 * exact step of the L6 layer (eg: SSL handshake).
805 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200806 chk = get_trash_chunk();
807
Christopher Faulet799f3a42020-04-07 12:06:14 +0200808 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200809 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200810 step = tcpcheck_get_step_id(check, NULL);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200811 if (!step) {
812 TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200813 chunk_printf(chk, " at initial connection step of tcp-check");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200814 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200815 else {
816 chunk_printf(chk, " at step %d of tcp-check", step);
817 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200818 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
819 if (check->current_step->connect.port)
820 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200821 else
822 chunk_appendf(chk, " (connect)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200823 TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200824 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200825 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
826 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100827
828 switch (expect->type) {
829 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200830 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100831 break;
832 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200833 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100834 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200835 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200836 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100837 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200838 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100839 chunk_appendf(chk, " (expect binary regex)");
840 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200841 case TCPCHK_EXPECT_STRING_LF:
842 chunk_appendf(chk, " (expect log-format string)");
843 break;
844 case TCPCHK_EXPECT_BINARY_LF:
845 chunk_appendf(chk, " (expect log-format binary)");
846 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200847 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200848 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200849 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200850 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200851 chunk_appendf(chk, " (expect HTTP status regex)");
852 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200853 case TCPCHK_EXPECT_HTTP_HEADER:
854 chunk_appendf(chk, " (expect HTTP header pattern)");
855 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200856 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200857 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200858 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200859 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200860 chunk_appendf(chk, " (expect HTTP body regex)");
861 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200862 case TCPCHK_EXPECT_HTTP_BODY_LF:
863 chunk_appendf(chk, " (expect log-format HTTP body)");
864 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200865 case TCPCHK_EXPECT_CUSTOM:
866 chunk_appendf(chk, " (expect custom function)");
867 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100868 case TCPCHK_EXPECT_UNDEF:
869 chunk_appendf(chk, " (undefined expect!)");
870 break;
871 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200872 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200873 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200874 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200875 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200876 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200877 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200878
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200879 if (check->current_step && check->current_step->comment)
880 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200881 }
882 }
883
Willy Tarreau00149122017-10-04 18:05:01 +0200884 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100885 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200886 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
887 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100888 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200889 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
890 chk->area);
891 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100892 }
893 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100894 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200895 chunk_printf(&trash, "%s%s", strerror(errno),
896 chk->area);
897 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100898 }
899 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200900 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100901 }
902 }
903
Willy Tarreau00149122017-10-04 18:05:01 +0200904 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200905 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200906 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
907 }
908
Christopher Faulet0256da12021-12-15 09:50:17 +0100909 if (!cs || !conn || !conn->ctrl) {
Christopher Faulet5e293762020-10-26 11:10:49 +0100910 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200911 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
912 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100913 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100914 /* L4 not established (yet) */
Willy Tarreaub605c422022-05-17 17:04:55 +0200915 if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100916 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
917 else if (expired)
918 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200919
920 /*
921 * might be due to a server IP change.
922 * Let's trigger a DNS resolution if none are currently running.
923 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100924 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100925 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200926
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100927 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100928 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100929 /* L6 not established (yet) */
Willy Tarreaub605c422022-05-17 17:04:55 +0200930 if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100931 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
932 else if (expired)
933 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
934 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200935 else if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100936 /* I/O error after connection was established and before we could diagnose */
937 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
938 }
939 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200940 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
941
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100942 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200943 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
944 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200945 tout = check->current_step->expect.tout_status;
946 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100947 }
948
Christopher Faulet147b8c92021-04-10 09:00:38 +0200949 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100950 return;
951}
952
Simon Horman98637e52014-06-20 12:30:16 +0900953
Christopher Faulet61cc8522020-04-20 14:54:42 +0200954/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200955int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900956{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200957 int sv_state;
958 int ratio;
959 char addr[46];
960 char port[6];
961 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
962 "UP %d/%d", "UP",
963 "NOLB %d/%d", "NOLB",
964 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900965
Christopher Faulet61cc8522020-04-20 14:54:42 +0200966 if (!(s->check.state & CHK_ST_ENABLED))
967 sv_state = 6;
968 else if (s->cur_state != SRV_ST_STOPPED) {
969 if (s->check.health == s->check.rise + s->check.fall - 1)
970 sv_state = 3; /* UP */
971 else
972 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900973
Christopher Faulet61cc8522020-04-20 14:54:42 +0200974 if (s->cur_state == SRV_ST_STOPPING)
975 sv_state += 2;
976 } else {
977 if (s->check.health)
978 sv_state = 1; /* going up */
979 else
980 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900981 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200982
Christopher Faulet61cc8522020-04-20 14:54:42 +0200983 chunk_appendf(buf, srv_hlt_st[sv_state],
984 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
985 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200986
Christopher Faulet61cc8522020-04-20 14:54:42 +0200987 addr_to_str(&s->addr, addr, sizeof(addr));
988 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
989 snprintf(port, sizeof(port), "%u", s->svc_port);
990 else
991 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200992
Christopher Faulet61cc8522020-04-20 14:54:42 +0200993 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
994 addr, port, s->proxy->id, s->id,
995 global.node,
996 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
997 (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 +0200998 s->cur_sess, s->proxy->beconn - s->proxy->queue.length,
Willy Tarreaua0570452021-06-18 09:30:30 +0200999 s->queue.length);
Willy Tarreau9f6dc722019-03-01 11:15:10 +01001000
Christopher Faulet61cc8522020-04-20 14:54:42 +02001001 if ((s->cur_state == SRV_ST_STARTING) &&
1002 now.tv_sec < s->last_change + s->slowstart &&
1003 now.tv_sec >= s->last_change) {
1004 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
1005 chunk_appendf(buf, "; throttle=%d%%", ratio);
1006 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001007
Christopher Faulet61cc8522020-04-20 14:54:42 +02001008 return b_data(buf);
1009}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001010
Willy Tarreau51cd5952020-06-05 12:25:38 +02001011/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001012/***************** Health-checks based on connections *********************/
1013/**************************************************************************/
1014/* This function is used only for server health-checks. It handles connection
1015 * status updates including errors. If necessary, it wakes the check task up.
1016 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1017 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001018 */
Willy Tarreau4596fe22022-05-17 19:07:51 +02001019static int wake_srv_chk(struct stconn *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001020{
Christopher Faulet0256da12021-12-15 09:50:17 +01001021 struct connection *conn;
Christopher Faulet693b23b2022-02-28 09:09:05 +01001022 struct check *check = __cs_check(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001023 struct email_alertq *q = container_of(check, typeof(*q), check);
1024 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001025
Christopher Faulet147b8c92021-04-10 09:00:38 +02001026 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001027 if (check->result != CHK_RES_UNKNOWN)
1028 goto end;
1029
Willy Tarreau51cd5952020-06-05 12:25:38 +02001030 if (check->server)
1031 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1032 else
1033 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001034
Willy Tarreau51cd5952020-06-05 12:25:38 +02001035 /* we may have to make progress on the TCP checks */
1036 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001037
Willy Tarreau51cd5952020-06-05 12:25:38 +02001038 cs = check->cs;
Christopher Faulet0256da12021-12-15 09:50:17 +01001039 conn = cs_conn(cs);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001040
Willy Tarreaub605c422022-05-17 17:04:55 +02001041 if (unlikely(!conn || !cs || conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001042 /* We may get error reports bypassing the I/O handlers, typically
1043 * the case when sending a pure TCP check which fails, then the I/O
1044 * handlers above are not called. This is completely handled by the
1045 * main processing task so let's simply wake it up. If we get here,
1046 * we expect errno to still be valid.
1047 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001048 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 +02001049 chk_report_conn_err(check, errno, 0);
1050 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001051 }
1052
Christopher Faulet8f100422021-01-18 15:47:03 +01001053 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001054 /* Check complete or aborted. Wake the check task up to be sure
1055 * the result is handled ASAP. */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001056 ret = -1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001057 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001058 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001059
Willy Tarreau51cd5952020-06-05 12:25:38 +02001060 if (check->server)
1061 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1062 else
1063 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001064
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001065 end:
Christopher Faulet147b8c92021-04-10 09:00:38 +02001066 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001067 return ret;
1068}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001069
Willy Tarreau51cd5952020-06-05 12:25:38 +02001070/* This function checks if any I/O is wanted, and if so, attempts to do so */
Christopher Faulet361417f2022-05-18 14:50:30 +02001071struct task *srv_chk_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001072{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001073 struct stconn *cs = ctx;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001074
Willy Tarreau51cd5952020-06-05 12:25:38 +02001075 wake_srv_chk(cs);
1076 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001077}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001078
Willy Tarreau51cd5952020-06-05 12:25:38 +02001079/* manages a server health-check that uses a connection. Returns
1080 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001081 *
1082 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001083 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001084 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001085struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001086{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001087 struct check *check = context;
1088 struct proxy *proxy = check->proxy;
Willy Tarreau4596fe22022-05-17 19:07:51 +02001089 struct stconn *cs;
Christopher Faulet92017a32021-05-06 16:01:18 +02001090 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001091 int rv;
1092 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001093
Christopher Faulet147b8c92021-04-10 09:00:38 +02001094 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1095
Willy Tarreau51cd5952020-06-05 12:25:38 +02001096 if (check->server)
1097 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001098
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001099 if (unlikely(check->state & CHK_ST_PURGE)) {
1100 TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1101 }
1102 else if (!(check->state & (CHK_ST_INPROGRESS))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001103 /* no check currently running */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001104 if (!expired) /* woke up too early */ {
1105 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001106 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001107 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001108
Willy Tarreau51cd5952020-06-05 12:25:38 +02001109 /* we don't send any health-checks when the proxy is
1110 * stopped, the server should not be checked or the check
1111 * is disabled.
1112 */
1113 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001114 (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001115 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001116 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001117 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001118
Willy Tarreau51cd5952020-06-05 12:25:38 +02001119 /* we'll initiate a new check */
1120 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001121
Willy Tarreau51cd5952020-06-05 12:25:38 +02001122 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001123 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001124
Willy Tarreau51cd5952020-06-05 12:25:38 +02001125 task_set_affinity(t, tid_bit);
1126
1127 check->current_step = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001128
Christopher Faulet177a0e62022-04-12 17:47:07 +02001129 check->cs = cs_new_from_check(check, CS_FL_NONE);
1130 if (!check->cs) {
1131 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1132 goto end;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001133 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001134 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001135 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001136 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001137
Christopher Faulet92017a32021-05-06 16:01:18 +02001138 /* there was a test running.
1139 * First, let's check whether there was an uncaught error,
1140 * which can happen on connect timeout or error.
1141 */
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001142 if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet177a0e62022-04-12 17:47:07 +02001143 cs = check->cs;
1144 conn = (cs ? cs_conn(cs) : NULL);
1145
Christopher Faulet92017a32021-05-06 16:01:18 +02001146 /* Here the connection must be defined. Otherwise the
1147 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001148 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001149 if ((conn && ((conn->flags & CO_FL_ERROR) || sc_ep_test(cs, SE_FL_ERROR))) || expired) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001150 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1151 chk_report_conn_err(check, 0, expired);
1152 }
1153 else {
1154 if (check->state & CHK_ST_CLOSE_CONN) {
1155 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001156 check->state &= ~CHK_ST_CLOSE_CONN;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001157 conn = NULL;
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001158 if (!cs_reset_endp(check->cs)) {
1159 /* error will be handled by tcpcheck_main().
Willy Tarreaub605c422022-05-17 17:04:55 +02001160 * On success, remove all flags except SE_FL_DETACHED
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001161 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001162 sc_ep_clr(check->cs, ~SE_FL_DETACHED);
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001163 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001164 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001165 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001166 if (check->result == CHK_RES_UNKNOWN) {
1167 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1168 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001169 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001170 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001171 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001172
Christopher Faulet92017a32021-05-06 16:01:18 +02001173 /* check complete or aborted */
1174 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001175
Christopher Faulet92017a32021-05-06 16:01:18 +02001176 check->current_step = NULL;
Christopher Faulet177a0e62022-04-12 17:47:07 +02001177 cs = check->cs;
1178 conn = (cs ? cs_conn(cs) : NULL);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001179
Christopher Faulet92017a32021-05-06 16:01:18 +02001180 if (conn && conn->xprt) {
1181 /* The check was aborted and the connection was not yet closed.
1182 * This can happen upon timeout, or when an external event such
1183 * as a failed response coupled with "observe layer7" caused the
1184 * server state to be suddenly changed.
1185 */
Christopher Fauletff022a22022-04-21 08:38:54 +02001186 cs_conn_drain_and_shut(cs);
Christopher Faulet92017a32021-05-06 16:01:18 +02001187 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001188
Christopher Faulet177a0e62022-04-12 17:47:07 +02001189 if (cs) {
Christopher Fauleteb50c012022-04-21 14:22:53 +02001190 cs_destroy(cs);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001191 cs = check->cs = NULL;
1192 conn = NULL;
1193 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001194
Christopher Faulet92017a32021-05-06 16:01:18 +02001195 if (check->sess != NULL) {
1196 vars_prune(&check->vars, check->sess, NULL);
1197 session_free(check->sess);
1198 check->sess = NULL;
1199 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001200
Christopher Fauletb041b232022-03-24 10:27:02 +01001201 end:
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001202 if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001203 if (check->result == CHK_RES_FAILED) {
1204 /* a failure or timeout detected */
1205 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1206 check_notify_failure(check);
1207 }
1208 else if (check->result == CHK_RES_CONDPASS) {
1209 /* check is OK but asks for stopping mode */
1210 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1211 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001212 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001213 else if (check->result == CHK_RES_PASSED) {
1214 /* a success was detected */
1215 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1216 check_notify_success(check);
1217 }
1218 }
Christopher Faulet6d781f62022-05-18 14:24:43 +02001219
1220 if (LIST_INLIST(&check->buf_wait.list))
1221 LIST_DEL_INIT(&check->buf_wait.list);
1222
Christopher Faulet92017a32021-05-06 16:01:18 +02001223 task_set_affinity(t, MAX_THREADS_MASK);
1224 check_release_buf(check, &check->bi);
1225 check_release_buf(check, &check->bo);
1226 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001227
Christopher Faulet92017a32021-05-06 16:01:18 +02001228 if (check->server) {
1229 rv = 0;
1230 if (global.spread_checks > 0) {
1231 rv = srv_getinter(check) * global.spread_checks / 100;
1232 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001233 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001234 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001235 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001236
1237 reschedule:
1238 while (tick_is_expired(t->expire, now_ms))
1239 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1240 out_unlock:
1241 if (check->server)
1242 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001243
1244 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001245
1246 /* Free the check if set to PURGE. After this, the check instance may be
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001247 * freed via the srv_drop invocation, so it must not be accessed after
1248 * this point.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001249 */
1250 if (unlikely(check->state & CHK_ST_PURGE)) {
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001251 free_check(check);
Amaury Denoyelle9ba34ae2021-08-09 15:09:17 +02001252 if (check->server)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001253 srv_drop(check->server);
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001254
1255 t = NULL;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001256 }
1257
Willy Tarreau51cd5952020-06-05 12:25:38 +02001258 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001259}
1260
Willy Tarreau51cd5952020-06-05 12:25:38 +02001261
Christopher Faulet61cc8522020-04-20 14:54:42 +02001262/**************************************************************************/
1263/************************** Init/deinit checks ****************************/
1264/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001265/*
1266 * Tries to grab a buffer and to re-enables processing on check <target>. The
1267 * check flags are used to figure what buffer was requested. It returns 1 if the
1268 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1269 * impossible to wake up and we prefer to be woken up later.
1270 */
1271int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001272{
Christopher Fauletb381a502020-11-25 13:47:00 +01001273 struct check *check = target;
1274
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001275 BUG_ON(!check->cs);
1276
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001277 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001278 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001279 check->state &= ~CHK_ST_IN_ALLOC;
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001280 tasklet_wakeup(check->cs->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001281 return 1;
1282 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001283 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001284 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001285 check->state &= ~CHK_ST_OUT_ALLOC;
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001286 tasklet_wakeup(check->cs->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001287 return 1;
1288 }
1289
1290 return 0;
1291}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001292
Christopher Fauletb381a502020-11-25 13:47:00 +01001293/*
William Dauchyf4300902021-02-06 20:47:50 +01001294 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001295 */
1296struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1297{
1298 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001299
Willy Tarreau2b718102021-04-21 07:32:39 +02001300 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001301 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001302 check->buf_wait.target = check;
1303 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +02001304 LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001305 }
1306 return buf;
1307}
1308
1309/*
1310 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1311 * wait queue.
1312 */
1313void check_release_buf(struct check *check, struct buffer *bptr)
1314{
1315 if (bptr->size) {
1316 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001317 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001318 }
1319}
1320
1321const char *init_check(struct check *check, int type)
1322{
1323 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001324
Christopher Fauletb381a502020-11-25 13:47:00 +01001325 check->bi = BUF_NULL;
1326 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001327 LIST_INIT(&check->buf_wait.list);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001328 return NULL;
1329}
1330
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001331/* Liberates the resources allocated for a check.
1332 *
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001333 * This function must only be run by the thread owning the check.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001334 */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001335void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001336{
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001337 /* For agent-check, free the rules / vars from the server. This is not
1338 * done for health-check : the proxy is the owner of the rules / vars
1339 * in this case.
1340 */
1341 if (check->state & CHK_ST_AGENT) {
1342 free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars);
1343 ha_free(&check->tcpcheck_rules);
1344 }
1345
Christopher Faulet61cc8522020-04-20 14:54:42 +02001346 task_destroy(check->task);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001347
Christopher Fauletb381a502020-11-25 13:47:00 +01001348 check_release_buf(check, &check->bi);
1349 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001350 if (check->cs) {
Christopher Fauleteb50c012022-04-21 14:22:53 +02001351 cs_destroy(check->cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001352 check->cs = NULL;
1353 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001354}
1355
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001356/* This function must be used in order to free a started check. The check will
1357 * be scheduled for a next execution in order to properly close and free all
1358 * check elements.
1359 *
1360 * Non thread-safe.
1361 */
1362void check_purge(struct check *check)
1363{
Amaury Denoyelle25fe1032021-08-10 16:21:55 +02001364 check->state |= CHK_ST_PURGE;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001365 task_wakeup(check->task, TASK_WOKEN_OTHER);
1366}
1367
Christopher Faulet61cc8522020-04-20 14:54:42 +02001368/* manages a server health-check. Returns the time the task accepts to wait, or
1369 * TIME_ETERNITY for infinity.
1370 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001371struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001372{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001373 struct check *check = context;
1374
1375 if (check->type == PR_O2_EXT_CHK)
1376 return process_chk_proc(t, context, state);
1377 return process_chk_conn(t, context, state);
1378
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001379}
1380
Christopher Faulet61cc8522020-04-20 14:54:42 +02001381
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001382int start_check_task(struct check *check, int mininter,
Christopher Faulet61cc8522020-04-20 14:54:42 +02001383 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001384{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001385 struct task *t;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001386
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001387 /* task for the check. Process-based checks exclusively run on thread 1. */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001388 if (check->type == PR_O2_EXT_CHK)
Willy Tarreaua89c1912021-10-20 18:43:30 +02001389 t = task_new_on(0);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001390 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001391 t = task_new_anywhere();
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001392
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001393 if (!t)
1394 goto fail_alloc_task;
1395
Christopher Faulet61cc8522020-04-20 14:54:42 +02001396 check->task = t;
1397 t->process = process_chk;
1398 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001399
Christopher Faulet61cc8522020-04-20 14:54:42 +02001400 if (mininter < srv_getinter(check))
1401 mininter = srv_getinter(check);
1402
1403 if (global.max_spread_checks && mininter > global.max_spread_checks)
1404 mininter = global.max_spread_checks;
1405
1406 /* check this every ms */
1407 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1408 check->start = now;
1409 task_queue(t);
1410
1411 return 1;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001412
1413 fail_alloc_task:
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001414 ha_alert("Starting [%s:%s] check: out of memory.\n",
1415 check->server->proxy->id, check->server->id);
1416 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001417}
1418
Christopher Faulet61cc8522020-04-20 14:54:42 +02001419/*
1420 * Start health-check.
1421 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1422 */
1423static int start_checks()
1424{
1425
1426 struct proxy *px;
1427 struct server *s;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001428 int nbcheck=0, mininter=0, srvpos=0;
1429
1430 /* 0- init the dummy frontend used to create all checks sessions */
1431 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001432 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001433 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1434 checks_fe.mode = PR_MODE_TCP;
1435 checks_fe.maxconn = 0;
1436 checks_fe.conn_retries = CONN_RETRIES;
1437 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1438 checks_fe.timeout.client = TICK_ETERNITY;
1439
1440 /* 1- count the checkers to run simultaneously.
1441 * We also determine the minimum interval among all of those which
1442 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1443 * will be used to spread their start-up date. Those which have
1444 * a shorter interval will start independently and will not dictate
1445 * too short an interval for all others.
1446 */
1447 for (px = proxies_list; px; px = px->next) {
1448 for (s = px->srv; s; s = s->next) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001449 if (s->check.state & CHK_ST_CONFIGURED) {
1450 nbcheck++;
1451 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1452 (!mininter || mininter > srv_getinter(&s->check)))
1453 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001454 }
1455
Christopher Faulet61cc8522020-04-20 14:54:42 +02001456 if (s->agent.state & CHK_ST_CONFIGURED) {
1457 nbcheck++;
1458 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1459 (!mininter || mininter > srv_getinter(&s->agent)))
1460 mininter = srv_getinter(&s->agent);
1461 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001462 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001463 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001464
Christopher Faulet61cc8522020-04-20 14:54:42 +02001465 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001466 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001467
Christopher Faulet61cc8522020-04-20 14:54:42 +02001468 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001469
William Dauchyf4300902021-02-06 20:47:50 +01001470 /* 2- start them as far as possible from each other. For this, we will
1471 * start them after their interval is set to the min interval divided
1472 * by the number of servers, weighted by the server's position in the
1473 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001474 */
1475 for (px = proxies_list; px; px = px->next) {
1476 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1477 if (init_pid_list()) {
1478 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1479 return ERR_ALERT | ERR_FATAL;
1480 }
1481 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001482
Christopher Faulet61cc8522020-04-20 14:54:42 +02001483 for (s = px->srv; s; s = s->next) {
1484 /* A task for the main check */
1485 if (s->check.state & CHK_ST_CONFIGURED) {
1486 if (s->check.type == PR_O2_EXT_CHK) {
1487 if (!prepare_external_check(&s->check))
1488 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001489 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001490 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1491 return ERR_ALERT | ERR_FATAL;
1492 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001493 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001494
Christopher Faulet61cc8522020-04-20 14:54:42 +02001495 /* A task for a auxiliary agent check */
1496 if (s->agent.state & CHK_ST_CONFIGURED) {
1497 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1498 return ERR_ALERT | ERR_FATAL;
1499 }
1500 srvpos++;
1501 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001502 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001503 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001504 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001505}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001506
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001507
Christopher Faulet61cc8522020-04-20 14:54:42 +02001508/*
1509 * Return value:
1510 * the port to be used for the health check
1511 * 0 in case no port could be found for the check
1512 */
1513static int srv_check_healthcheck_port(struct check *chk)
1514{
1515 int i = 0;
1516 struct server *srv = NULL;
1517
1518 srv = chk->server;
1519
William Dauchyf4300902021-02-06 20:47:50 +01001520 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001521 if (chk->port > 0)
1522 return chk->port;
1523
1524 /* try to get the port from check_core.addr if check.port not set */
1525 i = get_host_port(&chk->addr);
1526 if (i > 0)
1527 return i;
1528
1529 /* try to get the port from server address */
1530 /* prevent MAPPORTS from working at this point, since checks could
1531 * not be performed in such case (MAPPORTS impose a relative ports
1532 * based on live traffic)
1533 */
1534 if (srv->flags & SRV_F_MAPPORTS)
1535 return 0;
1536
1537 i = srv->svc_port; /* by default */
1538 if (i > 0)
1539 return i;
1540
1541 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001542}
1543
Christopher Faulet61cc8522020-04-20 14:54:42 +02001544/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1545 * if an error occurred.
1546 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001547int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001548{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001549 const char *err;
1550 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001551 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001552 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001553
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001554 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001555 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001556
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001557 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001558
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001559 if (!(srv->flags & SRV_F_DYNAMIC)) {
1560 /* If neither a port nor an addr was specified and no check
1561 * transport layer is forced, then the transport layer used by
1562 * the checks is the same as for the production traffic.
1563 * Otherwise we use raw_sock by default, unless one is
1564 * specified.
1565 */
1566 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1567 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1568 srv->check.use_ssl = srv->use_ssl;
1569 srv->check.xprt = srv->xprt;
1570 }
1571 else if (srv->check.use_ssl == 1)
1572 srv->check.xprt = xprt_get(XPRT_SSL);
1573 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001574 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001575 else if (srv->check.use_ssl == 1)
1576 srv->check.xprt = xprt_get(XPRT_SSL);
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001577 }
1578 else {
1579 /* For dynamic servers, check-ssl and check-send-proxy must be
1580 * explicitely defined even if the check port was not
1581 * overridden.
1582 */
1583 if (srv->check.use_ssl == 1)
1584 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001585 }
1586
Christopher Faulet12882cf2020-04-23 15:50:18 +02001587 /* Inherit the mux protocol from the server if not already defined for
1588 * the check
1589 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001590 if (srv->mux_proto && !srv->check.mux_proto &&
1591 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1592 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001593 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001594 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001595 /* test that check proto is valid if explicitly defined */
1596 else if (srv->check.mux_proto &&
1597 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1598 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1599 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1600 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1601 ret |= ERR_ALERT | ERR_FATAL;
1602 goto out;
1603 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001604
Christopher Faulet61cc8522020-04-20 14:54:42 +02001605 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001606
Christopher Faulet61cc8522020-04-20 14:54:42 +02001607 /* We need at least a service port, a check port or the first tcp-check
1608 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1609 */
1610 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1611 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1612 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001613
Christopher Faulet61cc8522020-04-20 14:54:42 +02001614 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1615 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1616 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1617 ret |= ERR_ALERT | ERR_ABORT;
1618 goto out;
1619 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001620
Christopher Faulet61cc8522020-04-20 14:54:42 +02001621 /* search the first action (connect / send / expect) in the list */
1622 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1623 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1624 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1625 "nor tcp_check rule 'connect' with port information.\n",
1626 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1627 ret |= ERR_ALERT | ERR_ABORT;
1628 goto out;
1629 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001630
Christopher Faulet61cc8522020-04-20 14:54:42 +02001631 /* scan the tcp-check ruleset to ensure a port has been configured */
1632 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreauacff3092021-07-22 11:06:41 +02001633 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001634 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1635 "and a tcp_check rule 'connect' with no port information.\n",
1636 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1637 ret |= ERR_ALERT | ERR_ABORT;
1638 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001639 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001640 }
1641
Christopher Faulet61cc8522020-04-20 14:54:42 +02001642 init:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001643 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1644 if (err) {
1645 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1646 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1647 ret |= ERR_ALERT | ERR_ABORT;
1648 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001649 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001650 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001651 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001652
1653 /* Only increment maxsock for servers from the configuration. Dynamic
1654 * servers at the moment are not taken into account for the estimation
1655 * of the resources limits.
1656 */
1657 if (global.mode & MODE_STARTING)
1658 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001659
Christopher Faulet61cc8522020-04-20 14:54:42 +02001660 out:
1661 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001662}
1663
Christopher Faulet61cc8522020-04-20 14:54:42 +02001664/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1665 * if an error occurred.
1666 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001667int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001668{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001669 struct tcpcheck_rule *chk;
1670 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001671 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001672
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001673 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001674 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001675
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001676 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001677 * implicit one is inserted before all others.
1678 */
1679 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1680 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1681 chk = calloc(1, sizeof(*chk));
1682 if (!chk) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001683 ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
Christopher Faulet61cc8522020-04-20 14:54:42 +02001684 " to agent-check for server '%s' (out of memory).\n",
1685 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1686 ret |= ERR_ALERT | ERR_FATAL;
1687 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001688 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001689 chk->action = TCPCHK_ACT_CONNECT;
1690 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001691 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001692 }
1693
Christopher Faulete5870d82020-04-15 11:32:03 +02001694
Christopher Faulet61cc8522020-04-20 14:54:42 +02001695 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1696 if (err) {
1697 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1698 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1699 ret |= ERR_ALERT | ERR_ABORT;
1700 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001701 }
1702
Christopher Faulet61cc8522020-04-20 14:54:42 +02001703 if (!srv->agent.inter)
1704 srv->agent.inter = srv->check.inter;
1705
1706 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001707 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001708
1709 /* Only increment maxsock for servers from the configuration. Dynamic
1710 * servers at the moment are not taken into account for the estimation
1711 * of the resources limits.
1712 */
1713 if (global.mode & MODE_STARTING)
1714 global.maxsock++;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001715
1716 out:
1717 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001718}
1719
Christopher Faulet61cc8522020-04-20 14:54:42 +02001720static void deinit_srv_check(struct server *srv)
1721{
1722 if (srv->check.state & CHK_ST_CONFIGURED)
1723 free_check(&srv->check);
1724 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1725 srv->do_check = 0;
1726}
Christopher Faulete5870d82020-04-15 11:32:03 +02001727
Christopher Faulet61cc8522020-04-20 14:54:42 +02001728
1729static void deinit_srv_agent_check(struct server *srv)
1730{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001731 if (srv->agent.state & CHK_ST_CONFIGURED)
1732 free_check(&srv->agent);
1733
1734 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1735 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001736}
1737
Willy Tarreaucee013e2020-06-05 11:40:38 +02001738REGISTER_POST_SERVER_CHECK(init_srv_check);
1739REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001740REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001741
Willy Tarreaucee013e2020-06-05 11:40:38 +02001742REGISTER_SERVER_DEINIT(deinit_srv_check);
1743REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001744
Christopher Faulet61cc8522020-04-20 14:54:42 +02001745
1746/**************************************************************************/
1747/************************** Check sample fetches **************************/
1748/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001749
Christopher Faulet61cc8522020-04-20 14:54:42 +02001750static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001751 { /* END */ },
1752}};
1753
1754INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1755
1756
1757/**************************************************************************/
1758/************************ Check's parsing functions ***********************/
1759/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001760/* Parse the "addr" server keyword */
1761static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1762 char **errmsg)
1763{
1764 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001765 int port1, port2, err_code = 0;
1766
1767
1768 if (!*args[*cur_arg+1]) {
1769 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1770 goto error;
1771 }
1772
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001773 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1774 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001775 if (!sk) {
1776 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1777 goto error;
1778 }
1779
William Dauchy1c921cd2021-02-03 22:30:08 +01001780 srv->check.addr = *sk;
1781 /* if agentaddr was never set, we can use addr */
1782 if (!(srv->flags & SRV_F_AGENTADDR))
1783 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001784
1785 out:
1786 return err_code;
1787
1788 error:
1789 err_code |= ERR_ALERT | ERR_FATAL;
1790 goto out;
1791}
1792
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001793/* Parse the "agent-addr" server keyword */
1794static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1795 char **errmsg)
1796{
William Dauchy1c921cd2021-02-03 22:30:08 +01001797 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001798 int err_code = 0;
1799
1800 if (!*(args[*cur_arg+1])) {
1801 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1802 goto error;
1803 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001804 memset(&sk, 0, sizeof(sk));
1805 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001806 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1807 goto error;
1808 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001809 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001810
1811 out:
1812 return err_code;
1813
1814 error:
1815 err_code |= ERR_ALERT | ERR_FATAL;
1816 goto out;
1817}
1818
1819/* Parse the "agent-check" server keyword */
1820static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1821 char **errmsg)
1822{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001823 struct tcpcheck_ruleset *rs = NULL;
1824 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1825 struct tcpcheck_rule *chk;
1826 int err_code = 0;
1827
1828 if (srv->do_agent)
1829 goto out;
1830
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001831 if (!(curpx->cap & PR_CAP_BE)) {
1832 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1833 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1834 return ERR_WARN;
1835 }
1836
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001837 if (!rules) {
1838 rules = calloc(1, sizeof(*rules));
1839 if (!rules) {
1840 memprintf(errmsg, "out of memory.");
1841 goto error;
1842 }
1843 LIST_INIT(&rules->preset_vars);
1844 srv->agent.tcpcheck_rules = rules;
1845 }
1846 rules->list = NULL;
1847 rules->flags = 0;
1848
Christopher Faulet61cc8522020-04-20 14:54:42 +02001849 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001850 if (rs)
1851 goto ruleset_found;
1852
Christopher Faulet61cc8522020-04-20 14:54:42 +02001853 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001854 if (rs == NULL) {
1855 memprintf(errmsg, "out of memory.");
1856 goto error;
1857 }
1858
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001859 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001860 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1861 if (!chk) {
1862 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1863 goto error;
1864 }
1865 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001866 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001867
1868 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001869 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1870 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001871 if (!chk) {
1872 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1873 goto error;
1874 }
1875 chk->expect.custom = tcpcheck_agent_expect_reply;
1876 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001877 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001878
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001879 ruleset_found:
1880 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001881 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001882 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001883 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001884
1885 out:
Dirkjan Bussinkdfee2172021-06-18 19:57:49 +00001886 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001887
1888 error:
1889 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001890 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001891 err_code |= ERR_ALERT | ERR_FATAL;
1892 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001893}
1894
1895/* Parse the "agent-inter" server keyword */
1896static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1897 char **errmsg)
1898{
1899 const char *err = NULL;
1900 unsigned int delay;
1901 int err_code = 0;
1902
1903 if (!*(args[*cur_arg+1])) {
1904 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1905 goto error;
1906 }
1907
1908 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1909 if (err == PARSE_TIME_OVER) {
1910 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1911 args[*cur_arg+1], args[*cur_arg], srv->id);
1912 goto error;
1913 }
1914 else if (err == PARSE_TIME_UNDER) {
1915 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1916 args[*cur_arg+1], args[*cur_arg], srv->id);
1917 goto error;
1918 }
1919 else if (err) {
1920 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1921 *err, srv->id);
1922 goto error;
1923 }
1924 if (delay <= 0) {
1925 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1926 delay, args[*cur_arg], srv->id);
1927 goto error;
1928 }
1929 srv->agent.inter = delay;
1930
1931 out:
1932 return err_code;
1933
1934 error:
1935 err_code |= ERR_ALERT | ERR_FATAL;
1936 goto out;
1937}
1938
1939/* Parse the "agent-port" server keyword */
1940static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1941 char **errmsg)
1942{
1943 int err_code = 0;
1944
1945 if (!*(args[*cur_arg+1])) {
1946 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1947 goto error;
1948 }
1949
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001950 /* Only increment maxsock for servers from the configuration. Dynamic
1951 * servers at the moment are not taken into account for the estimation
1952 * of the resources limits.
1953 */
1954 if (global.mode & MODE_STARTING)
1955 global.maxsock++;
1956
William Dauchy4858fb22021-02-03 22:30:09 +01001957 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001958
1959 out:
1960 return err_code;
1961
1962 error:
1963 err_code |= ERR_ALERT | ERR_FATAL;
1964 goto out;
1965}
1966
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001967int set_srv_agent_send(struct server *srv, const char *send)
1968{
1969 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1970 struct tcpcheck_var *var = NULL;
1971 char *str;
1972
1973 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02001974 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001975 if (str == NULL || var == NULL)
1976 goto error;
1977
1978 free_tcpcheck_vars(&rules->preset_vars);
1979
1980 var->data.type = SMP_T_STR;
1981 var->data.u.str.area = str;
1982 var->data.u.str.data = strlen(str);
1983 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02001984 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001985
1986 return 1;
1987
1988 error:
1989 free(str);
1990 free(var);
1991 return 0;
1992}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001993
1994/* Parse the "agent-send" server keyword */
1995static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1996 char **errmsg)
1997{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001998 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001999 int err_code = 0;
2000
2001 if (!*(args[*cur_arg+1])) {
2002 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2003 goto error;
2004 }
2005
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002006 if (!rules) {
2007 rules = calloc(1, sizeof(*rules));
2008 if (!rules) {
2009 memprintf(errmsg, "out of memory.");
2010 goto error;
2011 }
2012 LIST_INIT(&rules->preset_vars);
2013 srv->agent.tcpcheck_rules = rules;
2014 }
2015
2016 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002017 memprintf(errmsg, "out of memory.");
2018 goto error;
2019 }
2020
2021 out:
2022 return err_code;
2023
2024 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002025 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002026 err_code |= ERR_ALERT | ERR_FATAL;
2027 goto out;
2028}
2029
2030/* Parse the "no-agent-send" server keyword */
2031static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2032 char **errmsg)
2033{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002034 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002035 return 0;
2036}
2037
Christopher Fauletce8111e2020-04-06 15:04:11 +02002038/* Parse the "check" server keyword */
2039static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2040 char **errmsg)
2041{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002042 if (!(curpx->cap & PR_CAP_BE)) {
2043 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2044 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2045 return ERR_WARN;
2046 }
2047
Christopher Fauletce8111e2020-04-06 15:04:11 +02002048 srv->do_check = 1;
2049 return 0;
2050}
2051
2052/* Parse the "check-send-proxy" server keyword */
2053static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2054 char **errmsg)
2055{
2056 srv->check.send_proxy = 1;
2057 return 0;
2058}
2059
2060/* Parse the "check-via-socks4" server keyword */
2061static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2062 char **errmsg)
2063{
2064 srv->check.via_socks4 = 1;
2065 return 0;
2066}
2067
2068/* Parse the "no-check" server keyword */
2069static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2070 char **errmsg)
2071{
2072 deinit_srv_check(srv);
2073 return 0;
2074}
2075
2076/* Parse the "no-check-send-proxy" server keyword */
2077static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2078 char **errmsg)
2079{
2080 srv->check.send_proxy = 0;
2081 return 0;
2082}
2083
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002084/* parse the "check-proto" server keyword */
2085static int srv_parse_check_proto(char **args, int *cur_arg,
2086 struct proxy *px, struct server *newsrv, char **err)
2087{
2088 int err_code = 0;
2089
2090 if (!*args[*cur_arg + 1]) {
2091 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2092 goto error;
2093 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002094 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002095 if (!newsrv->check.mux_proto) {
2096 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2097 goto error;
2098 }
2099
2100 out:
2101 return err_code;
2102
2103 error:
2104 err_code |= ERR_ALERT | ERR_FATAL;
2105 goto out;
2106}
2107
2108
Christopher Fauletce8111e2020-04-06 15:04:11 +02002109/* Parse the "rise" server keyword */
2110static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2111 char **errmsg)
2112{
2113 int err_code = 0;
2114
2115 if (!*args[*cur_arg + 1]) {
2116 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2117 goto error;
2118 }
2119
2120 srv->check.rise = atol(args[*cur_arg+1]);
2121 if (srv->check.rise <= 0) {
2122 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2123 goto error;
2124 }
2125
2126 if (srv->check.health)
2127 srv->check.health = srv->check.rise;
2128
2129 out:
2130 return err_code;
2131
2132 error:
2133 deinit_srv_agent_check(srv);
2134 err_code |= ERR_ALERT | ERR_FATAL;
2135 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002136}
2137
2138/* Parse the "fall" server keyword */
2139static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2140 char **errmsg)
2141{
2142 int err_code = 0;
2143
2144 if (!*args[*cur_arg + 1]) {
2145 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2146 goto error;
2147 }
2148
2149 srv->check.fall = atol(args[*cur_arg+1]);
2150 if (srv->check.fall <= 0) {
2151 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2152 goto error;
2153 }
2154
2155 out:
2156 return err_code;
2157
2158 error:
2159 deinit_srv_agent_check(srv);
2160 err_code |= ERR_ALERT | ERR_FATAL;
2161 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002162}
2163
2164/* Parse the "inter" server keyword */
2165static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2166 char **errmsg)
2167{
2168 const char *err = NULL;
2169 unsigned int delay;
2170 int err_code = 0;
2171
2172 if (!*(args[*cur_arg+1])) {
2173 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2174 goto error;
2175 }
2176
2177 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2178 if (err == PARSE_TIME_OVER) {
2179 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2180 args[*cur_arg+1], args[*cur_arg], srv->id);
2181 goto error;
2182 }
2183 else if (err == PARSE_TIME_UNDER) {
2184 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2185 args[*cur_arg+1], args[*cur_arg], srv->id);
2186 goto error;
2187 }
2188 else if (err) {
2189 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2190 *err, srv->id);
2191 goto error;
2192 }
2193 if (delay <= 0) {
2194 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2195 delay, args[*cur_arg], srv->id);
2196 goto error;
2197 }
2198 srv->check.inter = delay;
2199
2200 out:
2201 return err_code;
2202
2203 error:
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206}
2207
2208
2209/* Parse the "fastinter" server keyword */
2210static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2211 char **errmsg)
2212{
2213 const char *err = NULL;
2214 unsigned int delay;
2215 int err_code = 0;
2216
2217 if (!*(args[*cur_arg+1])) {
2218 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2219 goto error;
2220 }
2221
2222 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2223 if (err == PARSE_TIME_OVER) {
2224 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2225 args[*cur_arg+1], args[*cur_arg], srv->id);
2226 goto error;
2227 }
2228 else if (err == PARSE_TIME_UNDER) {
2229 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2230 args[*cur_arg+1], args[*cur_arg], srv->id);
2231 goto error;
2232 }
2233 else if (err) {
2234 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2235 *err, srv->id);
2236 goto error;
2237 }
2238 if (delay <= 0) {
2239 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2240 delay, args[*cur_arg], srv->id);
2241 goto error;
2242 }
2243 srv->check.fastinter = delay;
2244
2245 out:
2246 return err_code;
2247
2248 error:
2249 err_code |= ERR_ALERT | ERR_FATAL;
2250 goto out;
2251}
2252
2253
2254/* Parse the "downinter" server keyword */
2255static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2256 char **errmsg)
2257{
2258 const char *err = NULL;
2259 unsigned int delay;
2260 int err_code = 0;
2261
2262 if (!*(args[*cur_arg+1])) {
2263 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2264 goto error;
2265 }
2266
2267 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2268 if (err == PARSE_TIME_OVER) {
2269 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2270 args[*cur_arg+1], args[*cur_arg], srv->id);
2271 goto error;
2272 }
2273 else if (err == PARSE_TIME_UNDER) {
2274 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2275 args[*cur_arg+1], args[*cur_arg], srv->id);
2276 goto error;
2277 }
2278 else if (err) {
2279 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2280 *err, srv->id);
2281 goto error;
2282 }
2283 if (delay <= 0) {
2284 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2285 delay, args[*cur_arg], srv->id);
2286 goto error;
2287 }
2288 srv->check.downinter = delay;
2289
2290 out:
2291 return err_code;
2292
2293 error:
2294 err_code |= ERR_ALERT | ERR_FATAL;
2295 goto out;
2296}
2297
2298/* Parse the "port" server keyword */
2299static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2300 char **errmsg)
2301{
2302 int err_code = 0;
2303
2304 if (!*(args[*cur_arg+1])) {
2305 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2306 goto error;
2307 }
2308
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002309 /* Only increment maxsock for servers from the configuration. Dynamic
2310 * servers at the moment are not taken into account for the estimation
2311 * of the resources limits.
2312 */
2313 if (global.mode & MODE_STARTING)
2314 global.maxsock++;
2315
Christopher Fauletce8111e2020-04-06 15:04:11 +02002316 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002317 /* if agentport was never set, we can use port */
2318 if (!(srv->flags & SRV_F_AGENTPORT))
2319 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002320
2321 out:
2322 return err_code;
2323
2324 error:
2325 err_code |= ERR_ALERT | ERR_FATAL;
2326 goto out;
2327}
2328
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002329static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002330 { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */
2331 { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02002332 { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002333 { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */
2334 { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */
2335 { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02002336 { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002337 { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */
2338 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */
2339 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002340 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2341 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2342 { "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 +02002343 { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */
2344 { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */
2345 { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */
2346 { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */
2347 { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */
2348 { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002349 { NULL, NULL, 0 },
2350}};
2351
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002352INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002353
Willy Tarreaubd741542010-03-16 18:46:54 +01002354/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002355 * Local variables:
2356 * c-indent-level: 8
2357 * c-basic-offset: 8
2358 * End:
2359 */