blob: e93e50bc5f94ddb3fbeb182df71d2ef9aba7e4ac [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
Christopher Faulet61cc8522020-04-20 14:54:42 +0200143struct data_cb check_conn_cb = {
144 .wake = wake_srv_chk,
145 .name = "CHCK",
146};
Christopher Fauletd7e63962020-04-17 20:15:59 +0200147
Christopher Faulet5d503fc2020-03-30 20:34:34 +0200148
Gaetan Rivet05d692d2020-02-14 17:42:54 +0100149/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200150struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +0200151
Christopher Faulet147b8c92021-04-10 09:00:38 +0200152
153static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
154{
155 size_t block1, block2;
156 int line, ptr, newptr;
157
158 block1 = b_contig_data(buf, ofs);
159 block2 = 0;
160 if (block1 > len)
161 block1 = len;
162 block2 = len - block1;
163
164 ofs = b_peek_ofs(buf, ofs);
165
166 line = 0;
167 ptr = ofs;
168 while (ptr < ofs + block1) {
169 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
170 if (newptr == ptr)
171 break;
172 ptr = newptr;
173 }
174
175 line = ptr = 0;
176 while (ptr < block2) {
177 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
178 if (newptr == ptr)
179 break;
180 ptr = newptr;
181 }
182}
183
184/* trace source and events */
185static void check_trace(enum trace_level level, uint64_t mask,
186 const struct trace_source *src,
187 const struct ist where, const struct ist func,
188 const void *a1, const void *a2, const void *a3, const void *a4)
189{
190 const struct check *check = a1;
191 const struct server *srv = (check ? check->server : NULL);
192 const size_t *val = a4;
193 const char *res;
194
195 if (!check || src->verbosity < CHK_VERB_CLEAN)
196 return;
197
198 chunk_appendf(&trace_buf, " : [%c] SRV=%s",
199 ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
200 srv->id);
201
202 chunk_appendf(&trace_buf, " status=%d/%d %s",
203 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
204 (check->health >= check->rise) ? check->fall : check->rise,
205 (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN");
206
207 switch (check->result) {
208 case CHK_RES_NEUTRAL: res = "-"; break;
209 case CHK_RES_FAILED: res = "FAIL"; break;
210 case CHK_RES_PASSED: res = "PASS"; break;
211 case CHK_RES_CONDPASS: res = "COND"; break;
212 default: res = "UNK"; break;
213 }
214
215 if (src->verbosity == CHK_VERB_CLEAN)
216 return;
217
218 chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
219 get_check_status_info(check->status), check->status,
220 res, check->result);
221
222 /* Display the value to the 4th argument (level > STATE) */
223 if (src->level > TRACE_LEVEL_STATE && val)
224 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
225
226 chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
227
228 if (src->verbosity == CHK_VERB_MINIMAL)
229 return;
230
231
232 if (check->cs) {
Christopher Faulet0256da12021-12-15 09:50:17 +0100233 struct connection *conn = cs_conn(check->cs);
234
235 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200236 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags);
237 }
238
239 if (mask & CHK_EV_TCPCHK) {
240 const char *type;
241
242 switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) {
243 case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break;
244 case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break;
245 case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break;
246 case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break;
247 case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break;
248 case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break;
249 case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break;
250 case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break;
251 case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break;
252 case TCPCHK_RULES_TCP_CHK: type = "TCP"; break;
253 default: type = "???"; break;
254 }
255 if (check->current_step)
256 chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
257 else
258 chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
259 }
260
261 /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
262 if (src->level > TRACE_LEVEL_USER) {
263 const struct buffer *buf = NULL;
264
265 chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
266 (unsigned int)b_data(&check->bi), b_orig(&check->bi),
267 (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
268 chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
269 (unsigned int)b_data(&check->bo), b_orig(&check->bo),
270 (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
271
272 if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
273 buf = (b_is_null(&check->bi) ? NULL : &check->bi);
274 else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
275 buf = (b_is_null(&check->bo) ? NULL : &check->bo);
276
277 if (buf) {
278 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
279 int full = (src->verbosity == CHK_VERB_COMPLETE);
280
281 chunk_memcat(&trace_buf, "\n\t", 2);
282 htx_dump(&trace_buf, htxbuf(buf), full);
283 }
284 else {
285 int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
286
287 chunk_memcat(&trace_buf, "\n", 1);
288 if (b_data(buf) > max) {
289 check_trace_buf(buf, 0, max);
290 chunk_memcat(&trace_buf, " ...\n", 6);
291 }
292 else
293 check_trace_buf(buf, 0, b_data(buf));
294 }
295
296 }
297 }
298
299}
300
301
Christopher Faulet61cc8522020-04-20 14:54:42 +0200302/**************************************************************************/
303/************************ Handle check results ****************************/
304/**************************************************************************/
305struct check_status {
306 short result; /* one of SRV_CHK_* */
307 char *info; /* human readable short info */
308 char *desc; /* long description */
309};
310
311struct analyze_status {
312 char *desc; /* description */
313 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
314};
315
Simon Horman63a4a822012-03-19 07:24:41 +0900316static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100317 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
318 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200319 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200320
Willy Tarreau23964182014-05-20 20:56:30 +0200321 /* Below we have finished checks */
322 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100323 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100324
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100325 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200326
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100327 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
328 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
329 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200330
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100331 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
332 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
333 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200334
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100335 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
336 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200337
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200338 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200339
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100340 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
341 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
342 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900343
344 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
345 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200346 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200347};
348
Simon Horman63a4a822012-03-19 07:24:41 +0900349static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100350 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
351
352 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
353 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
354
355 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
356 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
357 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
358 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
359
360 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
361 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
362 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
363};
364
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100365/* checks if <err> is a real error for errno or one that can be ignored, and
366 * return 0 for these ones or <err> for real ones.
367 */
368static inline int unclean_errno(int err)
369{
Willy Tarreauacef5e22022-04-25 20:32:15 +0200370 if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS ||
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100371 err == EISCONN || err == EALREADY)
372 return 0;
373 return err;
374}
375
Christopher Faulet7aa32712021-02-01 13:11:50 +0100376/* Converts check_status code to result code */
377short get_check_status_result(short check_status)
378{
379 if (check_status < HCHK_STATUS_SIZE)
380 return check_statuses[check_status].result;
381 else
382 return check_statuses[HCHK_STATUS_UNKNOWN].result;
383}
384
Christopher Faulet61cc8522020-04-20 14:54:42 +0200385/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200386const char *get_check_status_description(short check_status) {
387
388 const char *desc;
389
390 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200391 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200392 else
393 desc = NULL;
394
395 if (desc && *desc)
396 return desc;
397 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200398 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200399}
400
Christopher Faulet61cc8522020-04-20 14:54:42 +0200401/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100402const char *get_check_status_info(short check_status)
403{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200404 const char *info;
405
406 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200407 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200408 else
409 info = NULL;
410
411 if (info && *info)
412 return info;
413 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200414 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200415}
416
Christopher Faulet61cc8522020-04-20 14:54:42 +0200417/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100418const char *get_analyze_status(short analyze_status) {
419
420 const char *desc;
421
422 if (analyze_status < HANA_STATUS_SIZE)
423 desc = analyze_statuses[analyze_status].desc;
424 else
425 desc = NULL;
426
427 if (desc && *desc)
428 return desc;
429 else
430 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
431}
432
Christopher Faulet61cc8522020-04-20 14:54:42 +0200433/* Sets check->status, update check->duration and fill check->result with an
434 * adequate CHK_RES_* value. The new check->health is computed based on the
435 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200436 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200437 * Shows information in logs about failed health check if server is UP or
438 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200439 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200440void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100441{
Simon Horman4a741432013-02-23 15:35:38 +0900442 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200443 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200444 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900445
Christopher Faulet147b8c92021-04-10 09:00:38 +0200446 TRACE_POINT(CHK_EV_HCHK_RUN, check);
447
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200448 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100449 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900450 check->desc[0] = '\0';
451 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200452 return;
453 }
454
Simon Horman4a741432013-02-23 15:35:38 +0900455 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200456 return;
457
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200458 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900459 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
460 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200461 } else
Simon Horman4a741432013-02-23 15:35:38 +0900462 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200463
Simon Horman4a741432013-02-23 15:35:38 +0900464 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200465 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900466 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200467
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100468 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900469 check->duration = -1;
470 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200471 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900472 check->duration = tv_ms_elapsed(&check->start, &now);
473 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200474 }
475
Willy Tarreau23964182014-05-20 20:56:30 +0200476 /* no change is expected if no state change occurred */
477 if (check->result == CHK_RES_NEUTRAL)
478 return;
479
Olivier Houchard0923fa42019-01-11 18:43:04 +0100480 /* If the check was really just sending a mail, it won't have an
481 * associated server, so we're done now.
482 */
483 if (!s)
484 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200485 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200486
Christopher Faulet147b8c92021-04-10 09:00:38 +0200487
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200488 switch (check->result) {
489 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200490 /* Failure to connect to the agent as a secondary check should not
491 * cause the server to be marked down.
492 */
493 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900494 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200495 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200496 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200497 report = 1;
498 check->health--;
499 if (check->health < check->rise)
500 check->health = 0;
501 }
502 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200503
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200504 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100505 case CHK_RES_CONDPASS:
506 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200507 report = 1;
508 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200509
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200510 if (check->health >= check->rise)
511 check->health = check->rise + check->fall - 1; /* OK now */
512 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200513
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200514 /* clear consecutive_errors if observing is enabled */
515 if (s->onerror)
516 s->consecutive_errors = 0;
517 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100518
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200519 default:
520 break;
521 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200522
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200523 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
524 (status != prev_status || report)) {
525 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200526 "%s check for %sserver %s/%s %s%s",
527 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200528 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100529 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100530 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200531 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200532
Emeric Brun5a133512017-10-19 14:42:30 +0200533 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200534
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100535 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200536 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
537 (check->health >= check->rise) ? check->fall : check->rise,
538 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200539
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200540 ha_warning("%s.\n", trash.area);
541 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
542 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200543 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200544}
545
Willy Tarreau4eec5472014-05-20 22:32:27 +0200546/* Marks the check <check>'s server down if the current check is already failed
547 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200549void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550{
Simon Horman4a741432013-02-23 15:35:38 +0900551 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900552
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200553 /* The agent secondary check should only cause a server to be marked
554 * as down if check->status is HCHK_STATUS_L7STS, which indicates
555 * that the agent returned "fail", "stopped" or "down".
556 * The implication here is that failure to connect to the agent
557 * as a secondary check should not cause the server to be marked
558 * down. */
559 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
560 return;
561
Willy Tarreau4eec5472014-05-20 22:32:27 +0200562 if (check->health > 0)
563 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100564
Christopher Faulet147b8c92021-04-10 09:00:38 +0200565 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200566 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200567 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568}
569
Willy Tarreauaf549582014-05-16 17:37:50 +0200570/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200571 * it isn't in maintenance, it is not tracking a down server and other checks
572 * comply. The rule is simple : by default, a server is up, unless any of the
573 * following conditions is true :
574 * - health check failed (check->health < rise)
575 * - agent check failed (agent->health < rise)
576 * - the server tracks a down server (track && track->state == STOPPED)
577 * Note that if the server has a slowstart, it will switch to STARTING instead
578 * of RUNNING. Also, only the health checks support the nolb mode, so the
579 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200580 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200581void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200582{
Simon Horman4a741432013-02-23 15:35:38 +0900583 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100584
Emeric Brun52a91d32017-08-31 14:41:55 +0200585 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200586 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100587
Emeric Brun52a91d32017-08-31 14:41:55 +0200588 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200589 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100590
Willy Tarreau3e048382014-05-21 10:30:54 +0200591 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
592 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100593
Willy Tarreau3e048382014-05-21 10:30:54 +0200594 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
595 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200596
Emeric Brun52a91d32017-08-31 14:41:55 +0200597 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200598 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100599
Christopher Faulet147b8c92021-04-10 09:00:38 +0200600 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Emeric Brun5a133512017-10-19 14:42:30 +0200601 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100602}
603
Willy Tarreaudb58b792014-05-21 13:57:23 +0200604/* Marks the check <check> as valid and tries to set its server into stopping mode
605 * if it was running or starting, and provided it isn't in maintenance and other
606 * checks comply. The conditions for the server to be marked in stopping mode are
607 * the same as for it to be turned up. Also, only the health checks support the
608 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200609 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200610void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200611{
Simon Horman4a741432013-02-23 15:35:38 +0900612 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100613
Emeric Brun52a91d32017-08-31 14:41:55 +0200614 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200615 return;
616
Willy Tarreaudb58b792014-05-21 13:57:23 +0200617 if (check->state & CHK_ST_AGENT)
618 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100619
Emeric Brun52a91d32017-08-31 14:41:55 +0200620 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200621 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100622
Willy Tarreaudb58b792014-05-21 13:57:23 +0200623 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
624 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100625
Willy Tarreaudb58b792014-05-21 13:57:23 +0200626 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
627 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100628
Christopher Faulet147b8c92021-04-10 09:00:38 +0200629 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 +0100630 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100631}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100633/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100634 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100635 */
636void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100637{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100638 int failed;
639 int expire;
640
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100641 if (s->observe >= HANA_OBS_SIZE)
642 return;
643
Willy Tarreaubb956662013-01-24 00:37:39 +0100644 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100645 return;
646
647 switch (analyze_statuses[status].lr[s->observe - 1]) {
648 case 1:
649 failed = 1;
650 break;
651
652 case 2:
653 failed = 0;
654 break;
655
656 default:
657 return;
658 }
659
660 if (!failed) {
661 /* good: clear consecutive_errors */
662 s->consecutive_errors = 0;
663 return;
664 }
665
Willy Tarreau4781b152021-04-06 13:53:36 +0200666 _HA_ATOMIC_INC(&s->consecutive_errors);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100667
668 if (s->consecutive_errors < s->consecutive_errors_limit)
669 return;
670
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100671 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
672 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100673
Willy Tarreau4e9df272021-02-17 15:20:19 +0100674 if (s->check.fastinter)
675 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
676 else
677 expire = TICK_ETERNITY;
678
679 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
680
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100681 switch (s->onerror) {
682 case HANA_ONERR_FASTINTER:
683 /* force fastinter - nothing to do here as all modes force it */
684 break;
685
686 case HANA_ONERR_SUDDTH:
687 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900688 if (s->check.health > s->check.rise)
689 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100690
Tim Duesterhus588b3142020-05-29 14:35:51 +0200691 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100692
693 case HANA_ONERR_FAILCHK:
694 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200695 set_server_check_status(&s->check, HCHK_STATUS_HANA,
696 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200697 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100698 break;
699
700 case HANA_ONERR_MARKDWN:
701 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900702 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 set_server_check_status(&s->check, HCHK_STATUS_HANA,
704 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200705 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100706 break;
707
708 default:
709 /* write a warning? */
710 break;
711 }
712
Willy Tarreau4e9df272021-02-17 15:20:19 +0100713 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
714
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100715 s->consecutive_errors = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200716 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100717
Christopher Fauletea860832021-05-07 11:45:26 +0200718 if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) {
Willy Tarreau4e9df272021-02-17 15:20:19 +0100719 /* requeue check task with new expire */
720 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100721 }
Willy Tarreauef781042010-01-27 11:53:01 +0100722}
723
Christopher Faulet61cc8522020-04-20 14:54:42 +0200724/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100725 * closed, keep errno intact as it is supposed to contain the valid error code.
726 * If no error is reported, check the socket's error queue using getsockopt().
727 * Warning, this must be done only once when returning from poll, and never
728 * after an I/O error was attempted, otherwise the error queue might contain
729 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
730 * socket. Returns non-zero if an error was reported, zero if everything is
731 * clean (including a properly closed socket).
732 */
733static int retrieve_errno_from_socket(struct connection *conn)
734{
735 int skerr;
736 socklen_t lskerr = sizeof(skerr);
737
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100738 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100739 return 1;
740
Willy Tarreau3c728722014-01-23 13:50:42 +0100741 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100742 return 0;
743
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200744 BUG_ON(conn->flags & CO_FL_FDLESS);
745
Willy Tarreau585744b2017-08-24 14:31:19 +0200746 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100747 errno = skerr;
748
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100749 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100750
751 if (!errno) {
752 /* we could not retrieve an error, that does not mean there is
753 * none. Just don't change anything and only report the prior
754 * error if any.
755 */
756 if (conn->flags & CO_FL_ERROR)
757 return 1;
758 else
759 return 0;
760 }
761
762 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
763 return 1;
764}
765
Christopher Faulet61cc8522020-04-20 14:54:42 +0200766/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100767 * and adjust the server status accordingly. It may make use of <errno_bck>
768 * if non-null when the caller is absolutely certain of its validity (eg:
769 * checked just after a syscall). If the caller doesn't have a valid errno,
770 * it can pass zero, and retrieve_errno_from_socket() will be called to try
771 * to extract errno from the socket. If no error is reported, it will consider
772 * the <expired> flag. This is intended to be used when a connection error was
773 * reported in conn->flags or when a timeout was reported in <expired>. The
774 * function takes care of not updating a server status which was already set.
775 * All situations where at least one of <expired> or CO_FL_ERROR are set
776 * produce a status.
777 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200778void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100779{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200780 struct stconn *cs = check->cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200781 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100782 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200783 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200784 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100785
Christopher Faulet147b8c92021-04-10 09:00:38 +0200786 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100787 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200788 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100789
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100790 errno = unclean_errno(errno_bck);
791 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100792 retrieve_errno_from_socket(conn);
793
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200794 if (conn && !(conn->flags & CO_FL_ERROR) &&
Willy Tarreaub605c422022-05-17 17:04:55 +0200795 cs && !sc_ep_test(cs, SE_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100796 return;
797
Christopher Faulet147b8c92021-04-10 09:00:38 +0200798 TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
799
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100800 /* we'll try to build a meaningful error message depending on the
801 * context of the error possibly present in conn->err_code, and the
802 * socket error possibly collected above. This is useful to know the
803 * exact step of the L6 layer (eg: SSL handshake).
804 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200805 chk = get_trash_chunk();
806
Christopher Faulet799f3a42020-04-07 12:06:14 +0200807 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200808 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200809 step = tcpcheck_get_step_id(check, NULL);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200810 if (!step) {
811 TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200812 chunk_printf(chk, " at initial connection step of tcp-check");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200813 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200814 else {
815 chunk_printf(chk, " at step %d of tcp-check", step);
816 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200817 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
818 if (check->current_step->connect.port)
819 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200820 else
821 chunk_appendf(chk, " (connect)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200822 TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200823 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200824 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
825 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100826
827 switch (expect->type) {
828 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200829 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100830 break;
831 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200832 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100833 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200834 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200835 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100836 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200837 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100838 chunk_appendf(chk, " (expect binary regex)");
839 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200840 case TCPCHK_EXPECT_STRING_LF:
841 chunk_appendf(chk, " (expect log-format string)");
842 break;
843 case TCPCHK_EXPECT_BINARY_LF:
844 chunk_appendf(chk, " (expect log-format binary)");
845 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200846 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200847 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200848 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200849 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200850 chunk_appendf(chk, " (expect HTTP status regex)");
851 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200852 case TCPCHK_EXPECT_HTTP_HEADER:
853 chunk_appendf(chk, " (expect HTTP header pattern)");
854 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200855 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200856 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200857 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200858 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200859 chunk_appendf(chk, " (expect HTTP body regex)");
860 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200861 case TCPCHK_EXPECT_HTTP_BODY_LF:
862 chunk_appendf(chk, " (expect log-format HTTP body)");
863 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200864 case TCPCHK_EXPECT_CUSTOM:
865 chunk_appendf(chk, " (expect custom function)");
866 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100867 case TCPCHK_EXPECT_UNDEF:
868 chunk_appendf(chk, " (undefined expect!)");
869 break;
870 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200871 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200872 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200873 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200874 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200875 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200876 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200877
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200878 if (check->current_step && check->current_step->comment)
879 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200880 }
881 }
882
Willy Tarreau00149122017-10-04 18:05:01 +0200883 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100884 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200885 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
886 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100887 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200888 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
889 chk->area);
890 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100891 }
892 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100893 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200894 chunk_printf(&trash, "%s%s", strerror(errno),
895 chk->area);
896 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100897 }
898 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200899 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100900 }
901 }
902
Willy Tarreau00149122017-10-04 18:05:01 +0200903 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200904 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200905 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
906 }
907
Christopher Faulet0256da12021-12-15 09:50:17 +0100908 if (!cs || !conn || !conn->ctrl) {
Christopher Faulet5e293762020-10-26 11:10:49 +0100909 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200910 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
911 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100912 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100913 /* L4 not established (yet) */
Willy Tarreaub605c422022-05-17 17:04:55 +0200914 if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100915 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
916 else if (expired)
917 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200918
919 /*
920 * might be due to a server IP change.
921 * Let's trigger a DNS resolution if none are currently running.
922 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100923 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100924 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200925
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100926 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100927 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100928 /* L6 not established (yet) */
Willy Tarreaub605c422022-05-17 17:04:55 +0200929 if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100930 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
931 else if (expired)
932 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
933 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200934 else if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100935 /* I/O error after connection was established and before we could diagnose */
936 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
937 }
938 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200939 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
940
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100941 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200942 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
943 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200944 tout = check->current_step->expect.tout_status;
945 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100946 }
947
Christopher Faulet147b8c92021-04-10 09:00:38 +0200948 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100949 return;
950}
951
Simon Horman98637e52014-06-20 12:30:16 +0900952
Christopher Faulet61cc8522020-04-20 14:54:42 +0200953/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200954int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900955{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200956 int sv_state;
957 int ratio;
958 char addr[46];
959 char port[6];
960 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
961 "UP %d/%d", "UP",
962 "NOLB %d/%d", "NOLB",
963 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900964
Christopher Faulet61cc8522020-04-20 14:54:42 +0200965 if (!(s->check.state & CHK_ST_ENABLED))
966 sv_state = 6;
967 else if (s->cur_state != SRV_ST_STOPPED) {
968 if (s->check.health == s->check.rise + s->check.fall - 1)
969 sv_state = 3; /* UP */
970 else
971 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900972
Christopher Faulet61cc8522020-04-20 14:54:42 +0200973 if (s->cur_state == SRV_ST_STOPPING)
974 sv_state += 2;
975 } else {
976 if (s->check.health)
977 sv_state = 1; /* going up */
978 else
979 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900980 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200981
Christopher Faulet61cc8522020-04-20 14:54:42 +0200982 chunk_appendf(buf, srv_hlt_st[sv_state],
983 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
984 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200985
Christopher Faulet61cc8522020-04-20 14:54:42 +0200986 addr_to_str(&s->addr, addr, sizeof(addr));
987 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
988 snprintf(port, sizeof(port), "%u", s->svc_port);
989 else
990 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200991
Christopher Faulet61cc8522020-04-20 14:54:42 +0200992 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
993 addr, port, s->proxy->id, s->id,
994 global.node,
995 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
996 (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 +0200997 s->cur_sess, s->proxy->beconn - s->proxy->queue.length,
Willy Tarreaua0570452021-06-18 09:30:30 +0200998 s->queue.length);
Willy Tarreau9f6dc722019-03-01 11:15:10 +0100999
Christopher Faulet61cc8522020-04-20 14:54:42 +02001000 if ((s->cur_state == SRV_ST_STARTING) &&
1001 now.tv_sec < s->last_change + s->slowstart &&
1002 now.tv_sec >= s->last_change) {
1003 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
1004 chunk_appendf(buf, "; throttle=%d%%", ratio);
1005 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001006
Christopher Faulet61cc8522020-04-20 14:54:42 +02001007 return b_data(buf);
1008}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001009
Willy Tarreau51cd5952020-06-05 12:25:38 +02001010/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001011/***************** Health-checks based on connections *********************/
1012/**************************************************************************/
1013/* This function is used only for server health-checks. It handles connection
1014 * status updates including errors. If necessary, it wakes the check task up.
1015 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1016 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001017 */
Willy Tarreauf3ae34b2022-05-18 09:11:52 +02001018int wake_srv_chk(struct stconn *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001019{
Christopher Faulet0256da12021-12-15 09:50:17 +01001020 struct connection *conn;
Christopher Faulet693b23b2022-02-28 09:09:05 +01001021 struct check *check = __cs_check(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001022 struct email_alertq *q = container_of(check, typeof(*q), check);
1023 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001024
Christopher Faulet147b8c92021-04-10 09:00:38 +02001025 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001026 if (check->result != CHK_RES_UNKNOWN)
1027 goto end;
1028
Willy Tarreau51cd5952020-06-05 12:25:38 +02001029 if (check->server)
1030 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1031 else
1032 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001033
Willy Tarreau51cd5952020-06-05 12:25:38 +02001034 /* we may have to make progress on the TCP checks */
1035 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001036
Willy Tarreau51cd5952020-06-05 12:25:38 +02001037 cs = check->cs;
Christopher Faulet0256da12021-12-15 09:50:17 +01001038 conn = cs_conn(cs);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001039
Willy Tarreaub605c422022-05-17 17:04:55 +02001040 if (unlikely(!conn || !cs || conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001041 /* We may get error reports bypassing the I/O handlers, typically
1042 * the case when sending a pure TCP check which fails, then the I/O
1043 * handlers above are not called. This is completely handled by the
1044 * main processing task so let's simply wake it up. If we get here,
1045 * we expect errno to still be valid.
1046 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001047 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 +02001048 chk_report_conn_err(check, errno, 0);
1049 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001050 }
1051
Christopher Faulet8f100422021-01-18 15:47:03 +01001052 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001053 /* Check complete or aborted. Wake the check task up to be sure
1054 * the result is handled ASAP. */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001055 ret = -1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001056 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001057 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001058
Willy Tarreau51cd5952020-06-05 12:25:38 +02001059 if (check->server)
1060 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1061 else
1062 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001063
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001064 end:
Christopher Faulet147b8c92021-04-10 09:00:38 +02001065 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001066 return ret;
1067}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001068
Willy Tarreau51cd5952020-06-05 12:25:38 +02001069/* This function checks if any I/O is wanted, and if so, attempts to do so */
Christopher Faulet361417f2022-05-18 14:50:30 +02001070struct task *srv_chk_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001071{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001072 struct stconn *cs = ctx;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001073
Willy Tarreau51cd5952020-06-05 12:25:38 +02001074 wake_srv_chk(cs);
1075 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001076}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001077
Willy Tarreau51cd5952020-06-05 12:25:38 +02001078/* manages a server health-check that uses a connection. Returns
1079 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001080 *
1081 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001082 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001083 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001084struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001085{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001086 struct check *check = context;
1087 struct proxy *proxy = check->proxy;
Willy Tarreau4596fe22022-05-17 19:07:51 +02001088 struct stconn *cs;
Christopher Faulet92017a32021-05-06 16:01:18 +02001089 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001090 int rv;
1091 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001092
Christopher Faulet147b8c92021-04-10 09:00:38 +02001093 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1094
Willy Tarreau51cd5952020-06-05 12:25:38 +02001095 if (check->server)
1096 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001097
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001098 if (unlikely(check->state & CHK_ST_PURGE)) {
1099 TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1100 }
1101 else if (!(check->state & (CHK_ST_INPROGRESS))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001102 /* no check currently running */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001103 if (!expired) /* woke up too early */ {
1104 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001105 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001106 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001107
Willy Tarreau51cd5952020-06-05 12:25:38 +02001108 /* we don't send any health-checks when the proxy is
1109 * stopped, the server should not be checked or the check
1110 * is disabled.
1111 */
1112 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001113 (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001114 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001115 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001116 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001117
Willy Tarreau51cd5952020-06-05 12:25:38 +02001118 /* we'll initiate a new check */
1119 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001120
Willy Tarreau51cd5952020-06-05 12:25:38 +02001121 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001122 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001123
Willy Tarreau51cd5952020-06-05 12:25:38 +02001124 task_set_affinity(t, tid_bit);
1125
1126 check->current_step = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001127
Willy Tarreaucb041662022-05-17 19:44:42 +02001128 check->cs = cs_new_from_check(check, SC_FL_NONE);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001129 if (!check->cs) {
1130 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1131 goto end;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001132 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001133 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001134 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001135 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001136
Christopher Faulet92017a32021-05-06 16:01:18 +02001137 /* there was a test running.
1138 * First, let's check whether there was an uncaught error,
1139 * which can happen on connect timeout or error.
1140 */
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001141 if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet177a0e62022-04-12 17:47:07 +02001142 cs = check->cs;
1143 conn = (cs ? cs_conn(cs) : NULL);
1144
Christopher Faulet92017a32021-05-06 16:01:18 +02001145 /* Here the connection must be defined. Otherwise the
1146 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001147 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001148 if ((conn && ((conn->flags & CO_FL_ERROR) || sc_ep_test(cs, SE_FL_ERROR))) || expired) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001149 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1150 chk_report_conn_err(check, 0, expired);
1151 }
1152 else {
1153 if (check->state & CHK_ST_CLOSE_CONN) {
1154 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001155 check->state &= ~CHK_ST_CLOSE_CONN;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001156 conn = NULL;
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001157 if (!cs_reset_endp(check->cs)) {
1158 /* error will be handled by tcpcheck_main().
Willy Tarreaub605c422022-05-17 17:04:55 +02001159 * On success, remove all flags except SE_FL_DETACHED
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001160 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001161 sc_ep_clr(check->cs, ~SE_FL_DETACHED);
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001162 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001163 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001164 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001165 if (check->result == CHK_RES_UNKNOWN) {
1166 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1167 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001168 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001169 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001170 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001171
Christopher Faulet92017a32021-05-06 16:01:18 +02001172 /* check complete or aborted */
1173 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001174
Christopher Faulet92017a32021-05-06 16:01:18 +02001175 check->current_step = NULL;
Christopher Faulet177a0e62022-04-12 17:47:07 +02001176 cs = check->cs;
1177 conn = (cs ? cs_conn(cs) : NULL);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001178
Christopher Faulet92017a32021-05-06 16:01:18 +02001179 if (conn && conn->xprt) {
1180 /* The check was aborted and the connection was not yet closed.
1181 * This can happen upon timeout, or when an external event such
1182 * as a failed response coupled with "observe layer7" caused the
1183 * server state to be suddenly changed.
1184 */
Christopher Fauletff022a22022-04-21 08:38:54 +02001185 cs_conn_drain_and_shut(cs);
Christopher Faulet92017a32021-05-06 16:01:18 +02001186 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001187
Christopher Faulet177a0e62022-04-12 17:47:07 +02001188 if (cs) {
Christopher Fauleteb50c012022-04-21 14:22:53 +02001189 cs_destroy(cs);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001190 cs = check->cs = NULL;
1191 conn = NULL;
1192 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001193
Christopher Faulet92017a32021-05-06 16:01:18 +02001194 if (check->sess != NULL) {
1195 vars_prune(&check->vars, check->sess, NULL);
1196 session_free(check->sess);
1197 check->sess = NULL;
1198 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001199
Christopher Fauletb041b232022-03-24 10:27:02 +01001200 end:
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001201 if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001202 if (check->result == CHK_RES_FAILED) {
1203 /* a failure or timeout detected */
1204 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1205 check_notify_failure(check);
1206 }
1207 else if (check->result == CHK_RES_CONDPASS) {
1208 /* check is OK but asks for stopping mode */
1209 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1210 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001211 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001212 else if (check->result == CHK_RES_PASSED) {
1213 /* a success was detected */
1214 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1215 check_notify_success(check);
1216 }
1217 }
Christopher Faulet6d781f62022-05-18 14:24:43 +02001218
1219 if (LIST_INLIST(&check->buf_wait.list))
1220 LIST_DEL_INIT(&check->buf_wait.list);
1221
Christopher Faulet92017a32021-05-06 16:01:18 +02001222 task_set_affinity(t, MAX_THREADS_MASK);
1223 check_release_buf(check, &check->bi);
1224 check_release_buf(check, &check->bo);
1225 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001226
Christopher Faulet92017a32021-05-06 16:01:18 +02001227 if (check->server) {
1228 rv = 0;
1229 if (global.spread_checks > 0) {
1230 rv = srv_getinter(check) * global.spread_checks / 100;
1231 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001232 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001233 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001234 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001235
1236 reschedule:
1237 while (tick_is_expired(t->expire, now_ms))
1238 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1239 out_unlock:
1240 if (check->server)
1241 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001242
1243 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001244
1245 /* Free the check if set to PURGE. After this, the check instance may be
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001246 * freed via the srv_drop invocation, so it must not be accessed after
1247 * this point.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001248 */
1249 if (unlikely(check->state & CHK_ST_PURGE)) {
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001250 free_check(check);
Amaury Denoyelle9ba34ae2021-08-09 15:09:17 +02001251 if (check->server)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001252 srv_drop(check->server);
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001253
1254 t = NULL;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001255 }
1256
Willy Tarreau51cd5952020-06-05 12:25:38 +02001257 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001258}
1259
Willy Tarreau51cd5952020-06-05 12:25:38 +02001260
Christopher Faulet61cc8522020-04-20 14:54:42 +02001261/**************************************************************************/
1262/************************** Init/deinit checks ****************************/
1263/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001264/*
1265 * Tries to grab a buffer and to re-enables processing on check <target>. The
1266 * check flags are used to figure what buffer was requested. It returns 1 if the
1267 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1268 * impossible to wake up and we prefer to be woken up later.
1269 */
1270int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001271{
Christopher Fauletb381a502020-11-25 13:47:00 +01001272 struct check *check = target;
1273
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001274 BUG_ON(!check->cs);
1275
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001276 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001277 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001278 check->state &= ~CHK_ST_IN_ALLOC;
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001279 tasklet_wakeup(check->cs->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001280 return 1;
1281 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001282 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001283 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001284 check->state &= ~CHK_ST_OUT_ALLOC;
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001285 tasklet_wakeup(check->cs->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001286 return 1;
1287 }
1288
1289 return 0;
1290}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001291
Christopher Fauletb381a502020-11-25 13:47:00 +01001292/*
William Dauchyf4300902021-02-06 20:47:50 +01001293 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001294 */
1295struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1296{
1297 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001298
Willy Tarreau2b718102021-04-21 07:32:39 +02001299 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001300 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001301 check->buf_wait.target = check;
1302 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +02001303 LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001304 }
1305 return buf;
1306}
1307
1308/*
1309 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1310 * wait queue.
1311 */
1312void check_release_buf(struct check *check, struct buffer *bptr)
1313{
1314 if (bptr->size) {
1315 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001316 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001317 }
1318}
1319
1320const char *init_check(struct check *check, int type)
1321{
1322 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001323
Christopher Fauletb381a502020-11-25 13:47:00 +01001324 check->bi = BUF_NULL;
1325 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001326 LIST_INIT(&check->buf_wait.list);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001327 return NULL;
1328}
1329
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001330/* Liberates the resources allocated for a check.
1331 *
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001332 * This function must only be run by the thread owning the check.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001333 */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001334void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001335{
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001336 /* For agent-check, free the rules / vars from the server. This is not
1337 * done for health-check : the proxy is the owner of the rules / vars
1338 * in this case.
1339 */
1340 if (check->state & CHK_ST_AGENT) {
1341 free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars);
1342 ha_free(&check->tcpcheck_rules);
1343 }
1344
Christopher Faulet61cc8522020-04-20 14:54:42 +02001345 task_destroy(check->task);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001346
Christopher Fauletb381a502020-11-25 13:47:00 +01001347 check_release_buf(check, &check->bi);
1348 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001349 if (check->cs) {
Christopher Fauleteb50c012022-04-21 14:22:53 +02001350 cs_destroy(check->cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001351 check->cs = NULL;
1352 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001353}
1354
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001355/* This function must be used in order to free a started check. The check will
1356 * be scheduled for a next execution in order to properly close and free all
1357 * check elements.
1358 *
1359 * Non thread-safe.
1360 */
1361void check_purge(struct check *check)
1362{
Amaury Denoyelle25fe1032021-08-10 16:21:55 +02001363 check->state |= CHK_ST_PURGE;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001364 task_wakeup(check->task, TASK_WOKEN_OTHER);
1365}
1366
Christopher Faulet61cc8522020-04-20 14:54:42 +02001367/* manages a server health-check. Returns the time the task accepts to wait, or
1368 * TIME_ETERNITY for infinity.
1369 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001370struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001371{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001372 struct check *check = context;
1373
1374 if (check->type == PR_O2_EXT_CHK)
1375 return process_chk_proc(t, context, state);
1376 return process_chk_conn(t, context, state);
1377
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001378}
1379
Christopher Faulet61cc8522020-04-20 14:54:42 +02001380
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001381int start_check_task(struct check *check, int mininter,
Christopher Faulet61cc8522020-04-20 14:54:42 +02001382 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001383{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001384 struct task *t;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001385
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001386 /* task for the check. Process-based checks exclusively run on thread 1. */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001387 if (check->type == PR_O2_EXT_CHK)
Willy Tarreaua89c1912021-10-20 18:43:30 +02001388 t = task_new_on(0);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001389 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001390 t = task_new_anywhere();
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001391
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001392 if (!t)
1393 goto fail_alloc_task;
1394
Christopher Faulet61cc8522020-04-20 14:54:42 +02001395 check->task = t;
1396 t->process = process_chk;
1397 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001398
Christopher Faulet61cc8522020-04-20 14:54:42 +02001399 if (mininter < srv_getinter(check))
1400 mininter = srv_getinter(check);
1401
1402 if (global.max_spread_checks && mininter > global.max_spread_checks)
1403 mininter = global.max_spread_checks;
1404
1405 /* check this every ms */
1406 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1407 check->start = now;
1408 task_queue(t);
1409
1410 return 1;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001411
1412 fail_alloc_task:
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001413 ha_alert("Starting [%s:%s] check: out of memory.\n",
1414 check->server->proxy->id, check->server->id);
1415 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001416}
1417
Christopher Faulet61cc8522020-04-20 14:54:42 +02001418/*
1419 * Start health-check.
1420 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1421 */
1422static int start_checks()
1423{
1424
1425 struct proxy *px;
1426 struct server *s;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001427 int nbcheck=0, mininter=0, srvpos=0;
1428
1429 /* 0- init the dummy frontend used to create all checks sessions */
1430 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001431 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001432 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1433 checks_fe.mode = PR_MODE_TCP;
1434 checks_fe.maxconn = 0;
1435 checks_fe.conn_retries = CONN_RETRIES;
1436 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1437 checks_fe.timeout.client = TICK_ETERNITY;
1438
1439 /* 1- count the checkers to run simultaneously.
1440 * We also determine the minimum interval among all of those which
1441 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1442 * will be used to spread their start-up date. Those which have
1443 * a shorter interval will start independently and will not dictate
1444 * too short an interval for all others.
1445 */
1446 for (px = proxies_list; px; px = px->next) {
1447 for (s = px->srv; s; s = s->next) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001448 if (s->check.state & CHK_ST_CONFIGURED) {
1449 nbcheck++;
1450 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1451 (!mininter || mininter > srv_getinter(&s->check)))
1452 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001453 }
1454
Christopher Faulet61cc8522020-04-20 14:54:42 +02001455 if (s->agent.state & CHK_ST_CONFIGURED) {
1456 nbcheck++;
1457 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1458 (!mininter || mininter > srv_getinter(&s->agent)))
1459 mininter = srv_getinter(&s->agent);
1460 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001461 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001462 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001463
Christopher Faulet61cc8522020-04-20 14:54:42 +02001464 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001465 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001466
Christopher Faulet61cc8522020-04-20 14:54:42 +02001467 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001468
William Dauchyf4300902021-02-06 20:47:50 +01001469 /* 2- start them as far as possible from each other. For this, we will
1470 * start them after their interval is set to the min interval divided
1471 * by the number of servers, weighted by the server's position in the
1472 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001473 */
1474 for (px = proxies_list; px; px = px->next) {
1475 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1476 if (init_pid_list()) {
1477 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1478 return ERR_ALERT | ERR_FATAL;
1479 }
1480 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001481
Christopher Faulet61cc8522020-04-20 14:54:42 +02001482 for (s = px->srv; s; s = s->next) {
1483 /* A task for the main check */
1484 if (s->check.state & CHK_ST_CONFIGURED) {
1485 if (s->check.type == PR_O2_EXT_CHK) {
1486 if (!prepare_external_check(&s->check))
1487 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001488 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001489 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1490 return ERR_ALERT | ERR_FATAL;
1491 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001492 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001493
Christopher Faulet61cc8522020-04-20 14:54:42 +02001494 /* A task for a auxiliary agent check */
1495 if (s->agent.state & CHK_ST_CONFIGURED) {
1496 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1497 return ERR_ALERT | ERR_FATAL;
1498 }
1499 srvpos++;
1500 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001501 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001502 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001503 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001504}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001505
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001506
Christopher Faulet61cc8522020-04-20 14:54:42 +02001507/*
1508 * Return value:
1509 * the port to be used for the health check
1510 * 0 in case no port could be found for the check
1511 */
1512static int srv_check_healthcheck_port(struct check *chk)
1513{
1514 int i = 0;
1515 struct server *srv = NULL;
1516
1517 srv = chk->server;
1518
William Dauchyf4300902021-02-06 20:47:50 +01001519 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001520 if (chk->port > 0)
1521 return chk->port;
1522
1523 /* try to get the port from check_core.addr if check.port not set */
1524 i = get_host_port(&chk->addr);
1525 if (i > 0)
1526 return i;
1527
1528 /* try to get the port from server address */
1529 /* prevent MAPPORTS from working at this point, since checks could
1530 * not be performed in such case (MAPPORTS impose a relative ports
1531 * based on live traffic)
1532 */
1533 if (srv->flags & SRV_F_MAPPORTS)
1534 return 0;
1535
1536 i = srv->svc_port; /* by default */
1537 if (i > 0)
1538 return i;
1539
1540 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001541}
1542
Christopher Faulet61cc8522020-04-20 14:54:42 +02001543/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1544 * if an error occurred.
1545 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001546int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001547{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001548 const char *err;
1549 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001550 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001551 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001552
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001553 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001554 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001555
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001556 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001557
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001558 if (!(srv->flags & SRV_F_DYNAMIC)) {
1559 /* If neither a port nor an addr was specified and no check
1560 * transport layer is forced, then the transport layer used by
1561 * the checks is the same as for the production traffic.
1562 * Otherwise we use raw_sock by default, unless one is
1563 * specified.
1564 */
1565 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1566 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1567 srv->check.use_ssl = srv->use_ssl;
1568 srv->check.xprt = srv->xprt;
1569 }
1570 else if (srv->check.use_ssl == 1)
1571 srv->check.xprt = xprt_get(XPRT_SSL);
1572 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001573 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001574 else if (srv->check.use_ssl == 1)
1575 srv->check.xprt = xprt_get(XPRT_SSL);
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001576 }
1577 else {
1578 /* For dynamic servers, check-ssl and check-send-proxy must be
1579 * explicitely defined even if the check port was not
1580 * overridden.
1581 */
1582 if (srv->check.use_ssl == 1)
1583 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001584 }
1585
Christopher Faulet12882cf2020-04-23 15:50:18 +02001586 /* Inherit the mux protocol from the server if not already defined for
1587 * the check
1588 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001589 if (srv->mux_proto && !srv->check.mux_proto &&
1590 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1591 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001592 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001593 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001594 /* test that check proto is valid if explicitly defined */
1595 else if (srv->check.mux_proto &&
1596 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1597 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1598 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1599 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1600 ret |= ERR_ALERT | ERR_FATAL;
1601 goto out;
1602 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001603
Christopher Faulet61cc8522020-04-20 14:54:42 +02001604 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001605
Christopher Faulet61cc8522020-04-20 14:54:42 +02001606 /* We need at least a service port, a check port or the first tcp-check
1607 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1608 */
1609 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1610 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1611 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001612
Christopher Faulet61cc8522020-04-20 14:54:42 +02001613 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1614 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1615 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1616 ret |= ERR_ALERT | ERR_ABORT;
1617 goto out;
1618 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001619
Christopher Faulet61cc8522020-04-20 14:54:42 +02001620 /* search the first action (connect / send / expect) in the list */
1621 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1622 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1623 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1624 "nor tcp_check rule 'connect' with port information.\n",
1625 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1626 ret |= ERR_ALERT | ERR_ABORT;
1627 goto out;
1628 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001629
Christopher Faulet61cc8522020-04-20 14:54:42 +02001630 /* scan the tcp-check ruleset to ensure a port has been configured */
1631 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreauacff3092021-07-22 11:06:41 +02001632 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001633 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1634 "and a tcp_check rule 'connect' with no port information.\n",
1635 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1636 ret |= ERR_ALERT | ERR_ABORT;
1637 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001638 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001639 }
1640
Christopher Faulet61cc8522020-04-20 14:54:42 +02001641 init:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001642 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1643 if (err) {
1644 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1645 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1646 ret |= ERR_ALERT | ERR_ABORT;
1647 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001648 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001649 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001650 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001651
1652 /* Only increment maxsock for servers from the configuration. Dynamic
1653 * servers at the moment are not taken into account for the estimation
1654 * of the resources limits.
1655 */
1656 if (global.mode & MODE_STARTING)
1657 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001658
Christopher Faulet61cc8522020-04-20 14:54:42 +02001659 out:
1660 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001661}
1662
Christopher Faulet61cc8522020-04-20 14:54:42 +02001663/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1664 * if an error occurred.
1665 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001666int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001667{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001668 struct tcpcheck_rule *chk;
1669 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001670 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001671
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001672 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001673 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001674
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001675 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001676 * implicit one is inserted before all others.
1677 */
1678 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1679 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1680 chk = calloc(1, sizeof(*chk));
1681 if (!chk) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001682 ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
Christopher Faulet61cc8522020-04-20 14:54:42 +02001683 " to agent-check for server '%s' (out of memory).\n",
1684 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1685 ret |= ERR_ALERT | ERR_FATAL;
1686 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001687 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001688 chk->action = TCPCHK_ACT_CONNECT;
1689 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001690 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001691 }
1692
Christopher Faulete5870d82020-04-15 11:32:03 +02001693
Christopher Faulet61cc8522020-04-20 14:54:42 +02001694 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1695 if (err) {
1696 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1697 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1698 ret |= ERR_ALERT | ERR_ABORT;
1699 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001700 }
1701
Christopher Faulet61cc8522020-04-20 14:54:42 +02001702 if (!srv->agent.inter)
1703 srv->agent.inter = srv->check.inter;
1704
1705 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001706 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001707
1708 /* Only increment maxsock for servers from the configuration. Dynamic
1709 * servers at the moment are not taken into account for the estimation
1710 * of the resources limits.
1711 */
1712 if (global.mode & MODE_STARTING)
1713 global.maxsock++;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001714
1715 out:
1716 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001717}
1718
Christopher Faulet61cc8522020-04-20 14:54:42 +02001719static void deinit_srv_check(struct server *srv)
1720{
1721 if (srv->check.state & CHK_ST_CONFIGURED)
1722 free_check(&srv->check);
1723 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1724 srv->do_check = 0;
1725}
Christopher Faulete5870d82020-04-15 11:32:03 +02001726
Christopher Faulet61cc8522020-04-20 14:54:42 +02001727
1728static void deinit_srv_agent_check(struct server *srv)
1729{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001730 if (srv->agent.state & CHK_ST_CONFIGURED)
1731 free_check(&srv->agent);
1732
1733 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1734 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001735}
1736
Willy Tarreaucee013e2020-06-05 11:40:38 +02001737REGISTER_POST_SERVER_CHECK(init_srv_check);
1738REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001739REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001740
Willy Tarreaucee013e2020-06-05 11:40:38 +02001741REGISTER_SERVER_DEINIT(deinit_srv_check);
1742REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001743
Christopher Faulet61cc8522020-04-20 14:54:42 +02001744
1745/**************************************************************************/
1746/************************** Check sample fetches **************************/
1747/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001748
Christopher Faulet61cc8522020-04-20 14:54:42 +02001749static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001750 { /* END */ },
1751}};
1752
1753INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1754
1755
1756/**************************************************************************/
1757/************************ Check's parsing functions ***********************/
1758/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001759/* Parse the "addr" server keyword */
1760static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1761 char **errmsg)
1762{
1763 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001764 int port1, port2, err_code = 0;
1765
1766
1767 if (!*args[*cur_arg+1]) {
1768 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1769 goto error;
1770 }
1771
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001772 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1773 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001774 if (!sk) {
1775 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1776 goto error;
1777 }
1778
William Dauchy1c921cd2021-02-03 22:30:08 +01001779 srv->check.addr = *sk;
1780 /* if agentaddr was never set, we can use addr */
1781 if (!(srv->flags & SRV_F_AGENTADDR))
1782 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001783
1784 out:
1785 return err_code;
1786
1787 error:
1788 err_code |= ERR_ALERT | ERR_FATAL;
1789 goto out;
1790}
1791
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001792/* Parse the "agent-addr" server keyword */
1793static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1794 char **errmsg)
1795{
William Dauchy1c921cd2021-02-03 22:30:08 +01001796 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001797 int err_code = 0;
1798
1799 if (!*(args[*cur_arg+1])) {
1800 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1801 goto error;
1802 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001803 memset(&sk, 0, sizeof(sk));
1804 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001805 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1806 goto error;
1807 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001808 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001809
1810 out:
1811 return err_code;
1812
1813 error:
1814 err_code |= ERR_ALERT | ERR_FATAL;
1815 goto out;
1816}
1817
1818/* Parse the "agent-check" server keyword */
1819static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1820 char **errmsg)
1821{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001822 struct tcpcheck_ruleset *rs = NULL;
1823 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1824 struct tcpcheck_rule *chk;
1825 int err_code = 0;
1826
1827 if (srv->do_agent)
1828 goto out;
1829
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001830 if (!(curpx->cap & PR_CAP_BE)) {
1831 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1832 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1833 return ERR_WARN;
1834 }
1835
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001836 if (!rules) {
1837 rules = calloc(1, sizeof(*rules));
1838 if (!rules) {
1839 memprintf(errmsg, "out of memory.");
1840 goto error;
1841 }
1842 LIST_INIT(&rules->preset_vars);
1843 srv->agent.tcpcheck_rules = rules;
1844 }
1845 rules->list = NULL;
1846 rules->flags = 0;
1847
Christopher Faulet61cc8522020-04-20 14:54:42 +02001848 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001849 if (rs)
1850 goto ruleset_found;
1851
Christopher Faulet61cc8522020-04-20 14:54:42 +02001852 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001853 if (rs == NULL) {
1854 memprintf(errmsg, "out of memory.");
1855 goto error;
1856 }
1857
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001858 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001859 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1860 if (!chk) {
1861 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1862 goto error;
1863 }
1864 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001865 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001866
1867 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001868 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1869 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001870 if (!chk) {
1871 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1872 goto error;
1873 }
1874 chk->expect.custom = tcpcheck_agent_expect_reply;
1875 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001876 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001877
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001878 ruleset_found:
1879 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001880 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001881 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001882 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001883
1884 out:
Dirkjan Bussinkdfee2172021-06-18 19:57:49 +00001885 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001886
1887 error:
1888 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001889 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001890 err_code |= ERR_ALERT | ERR_FATAL;
1891 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001892}
1893
1894/* Parse the "agent-inter" server keyword */
1895static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1896 char **errmsg)
1897{
1898 const char *err = NULL;
1899 unsigned int delay;
1900 int err_code = 0;
1901
1902 if (!*(args[*cur_arg+1])) {
1903 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1904 goto error;
1905 }
1906
1907 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1908 if (err == PARSE_TIME_OVER) {
1909 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1910 args[*cur_arg+1], args[*cur_arg], srv->id);
1911 goto error;
1912 }
1913 else if (err == PARSE_TIME_UNDER) {
1914 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1915 args[*cur_arg+1], args[*cur_arg], srv->id);
1916 goto error;
1917 }
1918 else if (err) {
1919 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1920 *err, srv->id);
1921 goto error;
1922 }
1923 if (delay <= 0) {
1924 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1925 delay, args[*cur_arg], srv->id);
1926 goto error;
1927 }
1928 srv->agent.inter = delay;
1929
1930 out:
1931 return err_code;
1932
1933 error:
1934 err_code |= ERR_ALERT | ERR_FATAL;
1935 goto out;
1936}
1937
1938/* Parse the "agent-port" server keyword */
1939static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1940 char **errmsg)
1941{
1942 int err_code = 0;
1943
1944 if (!*(args[*cur_arg+1])) {
1945 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1946 goto error;
1947 }
1948
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001949 /* Only increment maxsock for servers from the configuration. Dynamic
1950 * servers at the moment are not taken into account for the estimation
1951 * of the resources limits.
1952 */
1953 if (global.mode & MODE_STARTING)
1954 global.maxsock++;
1955
William Dauchy4858fb22021-02-03 22:30:09 +01001956 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001957
1958 out:
1959 return err_code;
1960
1961 error:
1962 err_code |= ERR_ALERT | ERR_FATAL;
1963 goto out;
1964}
1965
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001966int set_srv_agent_send(struct server *srv, const char *send)
1967{
1968 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1969 struct tcpcheck_var *var = NULL;
1970 char *str;
1971
1972 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02001973 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001974 if (str == NULL || var == NULL)
1975 goto error;
1976
1977 free_tcpcheck_vars(&rules->preset_vars);
1978
1979 var->data.type = SMP_T_STR;
1980 var->data.u.str.area = str;
1981 var->data.u.str.data = strlen(str);
1982 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02001983 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001984
1985 return 1;
1986
1987 error:
1988 free(str);
1989 free(var);
1990 return 0;
1991}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001992
1993/* Parse the "agent-send" server keyword */
1994static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1995 char **errmsg)
1996{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001997 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001998 int err_code = 0;
1999
2000 if (!*(args[*cur_arg+1])) {
2001 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2002 goto error;
2003 }
2004
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002005 if (!rules) {
2006 rules = calloc(1, sizeof(*rules));
2007 if (!rules) {
2008 memprintf(errmsg, "out of memory.");
2009 goto error;
2010 }
2011 LIST_INIT(&rules->preset_vars);
2012 srv->agent.tcpcheck_rules = rules;
2013 }
2014
2015 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002016 memprintf(errmsg, "out of memory.");
2017 goto error;
2018 }
2019
2020 out:
2021 return err_code;
2022
2023 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002024 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002025 err_code |= ERR_ALERT | ERR_FATAL;
2026 goto out;
2027}
2028
2029/* Parse the "no-agent-send" server keyword */
2030static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2031 char **errmsg)
2032{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002033 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002034 return 0;
2035}
2036
Christopher Fauletce8111e2020-04-06 15:04:11 +02002037/* Parse the "check" server keyword */
2038static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2039 char **errmsg)
2040{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002041 if (!(curpx->cap & PR_CAP_BE)) {
2042 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2043 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2044 return ERR_WARN;
2045 }
2046
Christopher Fauletce8111e2020-04-06 15:04:11 +02002047 srv->do_check = 1;
2048 return 0;
2049}
2050
2051/* Parse the "check-send-proxy" server keyword */
2052static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2053 char **errmsg)
2054{
2055 srv->check.send_proxy = 1;
2056 return 0;
2057}
2058
2059/* Parse the "check-via-socks4" server keyword */
2060static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2061 char **errmsg)
2062{
2063 srv->check.via_socks4 = 1;
2064 return 0;
2065}
2066
2067/* Parse the "no-check" server keyword */
2068static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2069 char **errmsg)
2070{
2071 deinit_srv_check(srv);
2072 return 0;
2073}
2074
2075/* Parse the "no-check-send-proxy" server keyword */
2076static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2077 char **errmsg)
2078{
2079 srv->check.send_proxy = 0;
2080 return 0;
2081}
2082
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002083/* parse the "check-proto" server keyword */
2084static int srv_parse_check_proto(char **args, int *cur_arg,
2085 struct proxy *px, struct server *newsrv, char **err)
2086{
2087 int err_code = 0;
2088
2089 if (!*args[*cur_arg + 1]) {
2090 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2091 goto error;
2092 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002093 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002094 if (!newsrv->check.mux_proto) {
2095 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2096 goto error;
2097 }
2098
2099 out:
2100 return err_code;
2101
2102 error:
2103 err_code |= ERR_ALERT | ERR_FATAL;
2104 goto out;
2105}
2106
2107
Christopher Fauletce8111e2020-04-06 15:04:11 +02002108/* Parse the "rise" server keyword */
2109static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2110 char **errmsg)
2111{
2112 int err_code = 0;
2113
2114 if (!*args[*cur_arg + 1]) {
2115 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2116 goto error;
2117 }
2118
2119 srv->check.rise = atol(args[*cur_arg+1]);
2120 if (srv->check.rise <= 0) {
2121 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2122 goto error;
2123 }
2124
2125 if (srv->check.health)
2126 srv->check.health = srv->check.rise;
2127
2128 out:
2129 return err_code;
2130
2131 error:
2132 deinit_srv_agent_check(srv);
2133 err_code |= ERR_ALERT | ERR_FATAL;
2134 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002135}
2136
2137/* Parse the "fall" server keyword */
2138static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2139 char **errmsg)
2140{
2141 int err_code = 0;
2142
2143 if (!*args[*cur_arg + 1]) {
2144 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2145 goto error;
2146 }
2147
2148 srv->check.fall = atol(args[*cur_arg+1]);
2149 if (srv->check.fall <= 0) {
2150 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2151 goto error;
2152 }
2153
2154 out:
2155 return err_code;
2156
2157 error:
2158 deinit_srv_agent_check(srv);
2159 err_code |= ERR_ALERT | ERR_FATAL;
2160 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002161}
2162
2163/* Parse the "inter" server keyword */
2164static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2165 char **errmsg)
2166{
2167 const char *err = NULL;
2168 unsigned int delay;
2169 int err_code = 0;
2170
2171 if (!*(args[*cur_arg+1])) {
2172 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2173 goto error;
2174 }
2175
2176 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2177 if (err == PARSE_TIME_OVER) {
2178 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2179 args[*cur_arg+1], args[*cur_arg], srv->id);
2180 goto error;
2181 }
2182 else if (err == PARSE_TIME_UNDER) {
2183 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2184 args[*cur_arg+1], args[*cur_arg], srv->id);
2185 goto error;
2186 }
2187 else if (err) {
2188 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2189 *err, srv->id);
2190 goto error;
2191 }
2192 if (delay <= 0) {
2193 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2194 delay, args[*cur_arg], srv->id);
2195 goto error;
2196 }
2197 srv->check.inter = delay;
2198
2199 out:
2200 return err_code;
2201
2202 error:
2203 err_code |= ERR_ALERT | ERR_FATAL;
2204 goto out;
2205}
2206
2207
2208/* Parse the "fastinter" server keyword */
2209static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2210 char **errmsg)
2211{
2212 const char *err = NULL;
2213 unsigned int delay;
2214 int err_code = 0;
2215
2216 if (!*(args[*cur_arg+1])) {
2217 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2218 goto error;
2219 }
2220
2221 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2222 if (err == PARSE_TIME_OVER) {
2223 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2224 args[*cur_arg+1], args[*cur_arg], srv->id);
2225 goto error;
2226 }
2227 else if (err == PARSE_TIME_UNDER) {
2228 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2229 args[*cur_arg+1], args[*cur_arg], srv->id);
2230 goto error;
2231 }
2232 else if (err) {
2233 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2234 *err, srv->id);
2235 goto error;
2236 }
2237 if (delay <= 0) {
2238 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2239 delay, args[*cur_arg], srv->id);
2240 goto error;
2241 }
2242 srv->check.fastinter = delay;
2243
2244 out:
2245 return err_code;
2246
2247 error:
2248 err_code |= ERR_ALERT | ERR_FATAL;
2249 goto out;
2250}
2251
2252
2253/* Parse the "downinter" server keyword */
2254static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2255 char **errmsg)
2256{
2257 const char *err = NULL;
2258 unsigned int delay;
2259 int err_code = 0;
2260
2261 if (!*(args[*cur_arg+1])) {
2262 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2263 goto error;
2264 }
2265
2266 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2267 if (err == PARSE_TIME_OVER) {
2268 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2269 args[*cur_arg+1], args[*cur_arg], srv->id);
2270 goto error;
2271 }
2272 else if (err == PARSE_TIME_UNDER) {
2273 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2274 args[*cur_arg+1], args[*cur_arg], srv->id);
2275 goto error;
2276 }
2277 else if (err) {
2278 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2279 *err, srv->id);
2280 goto error;
2281 }
2282 if (delay <= 0) {
2283 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2284 delay, args[*cur_arg], srv->id);
2285 goto error;
2286 }
2287 srv->check.downinter = delay;
2288
2289 out:
2290 return err_code;
2291
2292 error:
2293 err_code |= ERR_ALERT | ERR_FATAL;
2294 goto out;
2295}
2296
2297/* Parse the "port" server keyword */
2298static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2299 char **errmsg)
2300{
2301 int err_code = 0;
2302
2303 if (!*(args[*cur_arg+1])) {
2304 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2305 goto error;
2306 }
2307
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002308 /* Only increment maxsock for servers from the configuration. Dynamic
2309 * servers at the moment are not taken into account for the estimation
2310 * of the resources limits.
2311 */
2312 if (global.mode & MODE_STARTING)
2313 global.maxsock++;
2314
Christopher Fauletce8111e2020-04-06 15:04:11 +02002315 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002316 /* if agentport was never set, we can use port */
2317 if (!(srv->flags & SRV_F_AGENTPORT))
2318 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002319
2320 out:
2321 return err_code;
2322
2323 error:
2324 err_code |= ERR_ALERT | ERR_FATAL;
2325 goto out;
2326}
2327
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002328static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002329 { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */
2330 { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02002331 { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002332 { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */
2333 { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */
2334 { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02002335 { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002336 { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */
2337 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */
2338 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002339 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2340 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2341 { "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 +02002342 { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */
2343 { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */
2344 { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */
2345 { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */
2346 { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */
2347 { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002348 { NULL, NULL, 0 },
2349}};
2350
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002351INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002352
Willy Tarreaubd741542010-03-16 18:46:54 +01002353/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002354 * Local variables:
2355 * c-indent-level: 8
2356 * c-basic-offset: 8
2357 * End:
2358 */