blob: b036bba26025ec1d9e999851e213cb4aa40de556 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090018#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020020#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020022#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023#include <unistd.h>
Willy Tarreau9f6dc722019-03-01 11:15:10 +010024#include <sys/resource.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090027#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020029#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <arpa/inet.h>
31
Willy Tarreau122eba92020-06-04 10:15:32 +020032#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020033#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020035#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020036#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020037#include <haproxy/chunk.h>
Willy Tarreau7c18b542020-06-11 09:23:02 +020038#include <haproxy/dgram.h>
Christopher Fauletb381a502020-11-25 13:47:00 +010039#include <haproxy/dynbuf-t.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020040#include <haproxy/extcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/fd.h>
42#include <haproxy/global.h>
43#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020044#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/istbuf.h>
48#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020049#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020050#include <haproxy/mailers.h>
51#include <haproxy/port_range.h>
52#include <haproxy/proto_tcp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020053#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/queue.h>
56#include <haproxy/regex.h>
Emeric Brunc9437992021-02-12 19:42:55 +010057#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020059#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020060#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020061#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020062#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020063#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020064#include <haproxy/tcpcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/thread.h>
66#include <haproxy/time.h>
67#include <haproxy/tools.h>
Christopher Faulet147b8c92021-04-10 09:00:38 +020068#include <haproxy/trace.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020069#include <haproxy/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Christopher Faulet147b8c92021-04-10 09:00:38 +020071/* trace source and events */
72static void check_trace(enum trace_level level, uint64_t mask,
73 const struct trace_source *src,
74 const struct ist where, const struct ist func,
75 const void *a1, const void *a2, const void *a3, const void *a4);
76
77/* The event representation is split like this :
78 * check - check
79 *
80 * CHECK_EV_* macros are defined in <haproxy/check.h>
81 */
82static const struct trace_event check_trace_events[] = {
83 { .mask = CHK_EV_TASK_WAKE, .name = "task_wake", .desc = "Check task woken up" },
84 { .mask = CHK_EV_HCHK_START, .name = "hchck_start", .desc = "Health-check started" },
85 { .mask = CHK_EV_HCHK_WAKE, .name = "hchck_wake", .desc = "Health-check woken up" },
86 { .mask = CHK_EV_HCHK_RUN, .name = "hchck_run", .desc = "Health-check running" },
87 { .mask = CHK_EV_HCHK_END, .name = "hchck_end", .desc = "Health-check terminated" },
88 { .mask = CHK_EV_HCHK_SUCC, .name = "hchck_succ", .desc = "Health-check success" },
89 { .mask = CHK_EV_HCHK_ERR, .name = "hchck_err", .desc = "Health-check failure" },
90
91 { .mask = CHK_EV_TCPCHK_EVAL, .name = "tcp_check_eval", .desc = "tcp-check rules evaluation" },
92 { .mask = CHK_EV_TCPCHK_ERR, .name = "tcp_check_err", .desc = "tcp-check evaluation error" },
93 { .mask = CHK_EV_TCPCHK_CONN, .name = "tcp_check_conn", .desc = "tcp-check connection rule" },
94 { .mask = CHK_EV_TCPCHK_SND, .name = "tcp_check_send", .desc = "tcp-check send rule" },
95 { .mask = CHK_EV_TCPCHK_EXP, .name = "tcp_check_expect", .desc = "tcp-check expect rule" },
96 { .mask = CHK_EV_TCPCHK_ACT, .name = "tcp_check_action", .desc = "tcp-check action rule" },
97
98 { .mask = CHK_EV_RX_DATA, .name = "rx_data", .desc = "receipt of data" },
99 { .mask = CHK_EV_RX_BLK, .name = "rx_blk", .desc = "receipt blocked" },
100 { .mask = CHK_EV_RX_ERR, .name = "rx_err", .desc = "receipt error" },
101
102 { .mask = CHK_EV_TX_DATA, .name = "tx_data", .desc = "transmission of data" },
103 { .mask = CHK_EV_TX_BLK, .name = "tx_blk", .desc = "transmission blocked" },
104 { .mask = CHK_EV_TX_ERR, .name = "tx_err", .desc = "transmission error" },
105
106 {}
107};
108
109static const struct name_desc check_trace_lockon_args[4] = {
110 /* arg1 */ { /* already used by the check */ },
111 /* arg2 */ { },
112 /* arg3 */ { },
113 /* arg4 */ { }
114};
115
116static const struct name_desc check_trace_decoding[] = {
117#define CHK_VERB_CLEAN 1
118 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
119#define CHK_VERB_MINIMAL 2
120 { .name="minimal", .desc="report info on stream and stream-interfaces" },
121#define CHK_VERB_SIMPLE 3
122 { .name="simple", .desc="add info on request and response channels" },
123#define CHK_VERB_ADVANCED 4
124 { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" },
125#define CHK_VERB_COMPLETE 5
126 { .name="complete", .desc="add info on channel's buffer" },
127 { /* end */ }
128};
129
130struct trace_source trace_check = {
131 .name = IST("check"),
132 .desc = "Health-check",
133 .arg_def = TRC_ARG1_CHK, // TRACE()'s first argument is always a stream
134 .default_cb = check_trace,
135 .known_events = check_trace_events,
136 .lockon_args = check_trace_lockon_args,
137 .decoding = check_trace_decoding,
138 .report_events = ~0, // report everything by default
139};
140
141#define TRACE_SOURCE &trace_check
142INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
143
Olivier Houchard9130a962017-10-17 17:33:43 +0200144
Christopher Faulet61cc8522020-04-20 14:54:42 +0200145static int wake_srv_chk(struct conn_stream *cs);
146struct data_cb check_conn_cb = {
147 .wake = wake_srv_chk,
148 .name = "CHCK",
149};
Christopher Fauletd7e63962020-04-17 20:15:59 +0200150
Christopher Faulet5d503fc2020-03-30 20:34:34 +0200151
Gaetan Rivet05d692d2020-02-14 17:42:54 +0100152/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200153struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +0200154
Christopher Faulet147b8c92021-04-10 09:00:38 +0200155
156static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
157{
158 size_t block1, block2;
159 int line, ptr, newptr;
160
161 block1 = b_contig_data(buf, ofs);
162 block2 = 0;
163 if (block1 > len)
164 block1 = len;
165 block2 = len - block1;
166
167 ofs = b_peek_ofs(buf, ofs);
168
169 line = 0;
170 ptr = ofs;
171 while (ptr < ofs + block1) {
172 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
173 if (newptr == ptr)
174 break;
175 ptr = newptr;
176 }
177
178 line = ptr = 0;
179 while (ptr < block2) {
180 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
181 if (newptr == ptr)
182 break;
183 ptr = newptr;
184 }
185}
186
187/* trace source and events */
188static void check_trace(enum trace_level level, uint64_t mask,
189 const struct trace_source *src,
190 const struct ist where, const struct ist func,
191 const void *a1, const void *a2, const void *a3, const void *a4)
192{
193 const struct check *check = a1;
194 const struct server *srv = (check ? check->server : NULL);
195 const size_t *val = a4;
196 const char *res;
197
198 if (!check || src->verbosity < CHK_VERB_CLEAN)
199 return;
200
Christopher Faulet5f76c902022-06-08 09:12:58 +0200201 if (srv) {
202 chunk_appendf(&trace_buf, " : [%c] SRV=%s",
203 ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
204 srv->id);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200205
Christopher Faulet5f76c902022-06-08 09:12:58 +0200206 chunk_appendf(&trace_buf, " status=%d/%d %s",
207 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
208 (check->health >= check->rise) ? check->fall : check->rise,
209 (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN");
210 }
211 else
212 chunk_appendf(&trace_buf, " : [EMAIL]");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200213
214 switch (check->result) {
215 case CHK_RES_NEUTRAL: res = "-"; break;
216 case CHK_RES_FAILED: res = "FAIL"; break;
217 case CHK_RES_PASSED: res = "PASS"; break;
218 case CHK_RES_CONDPASS: res = "COND"; break;
219 default: res = "UNK"; break;
220 }
221
222 if (src->verbosity == CHK_VERB_CLEAN)
223 return;
224
225 chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
226 get_check_status_info(check->status), check->status,
227 res, check->result);
228
229 /* Display the value to the 4th argument (level > STATE) */
230 if (src->level > TRACE_LEVEL_STATE && val)
231 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
232
233 chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
234
235 if (src->verbosity == CHK_VERB_MINIMAL)
236 return;
237
238
239 if (check->cs) {
240 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", check->cs->conn, check->cs->conn->flags);
241 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags);
242 }
243
244 if (mask & CHK_EV_TCPCHK) {
245 const char *type;
246
247 switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) {
248 case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break;
249 case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break;
250 case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break;
251 case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break;
252 case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break;
253 case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break;
254 case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break;
255 case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break;
256 case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break;
257 case TCPCHK_RULES_TCP_CHK: type = "TCP"; break;
258 default: type = "???"; break;
259 }
260 if (check->current_step)
261 chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
262 else
263 chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
264 }
265
266 /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
267 if (src->level > TRACE_LEVEL_USER) {
268 const struct buffer *buf = NULL;
269
270 chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
271 (unsigned int)b_data(&check->bi), b_orig(&check->bi),
272 (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
273 chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
274 (unsigned int)b_data(&check->bo), b_orig(&check->bo),
275 (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
276
277 if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
278 buf = (b_is_null(&check->bi) ? NULL : &check->bi);
279 else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
280 buf = (b_is_null(&check->bo) ? NULL : &check->bo);
281
282 if (buf) {
283 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
284 int full = (src->verbosity == CHK_VERB_COMPLETE);
285
286 chunk_memcat(&trace_buf, "\n\t", 2);
287 htx_dump(&trace_buf, htxbuf(buf), full);
288 }
289 else {
290 int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
291
292 chunk_memcat(&trace_buf, "\n", 1);
293 if (b_data(buf) > max) {
294 check_trace_buf(buf, 0, max);
295 chunk_memcat(&trace_buf, " ...\n", 6);
296 }
297 else
298 check_trace_buf(buf, 0, b_data(buf));
299 }
300
301 }
302 }
303
304}
305
306
Christopher Faulet61cc8522020-04-20 14:54:42 +0200307/**************************************************************************/
308/************************ Handle check results ****************************/
309/**************************************************************************/
310struct check_status {
311 short result; /* one of SRV_CHK_* */
312 char *info; /* human readable short info */
313 char *desc; /* long description */
314};
315
316struct analyze_status {
317 char *desc; /* description */
318 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
319};
320
Simon Horman63a4a822012-03-19 07:24:41 +0900321static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100322 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
323 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200324 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200325
Willy Tarreau23964182014-05-20 20:56:30 +0200326 /* Below we have finished checks */
327 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100328 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100329
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100330 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200331
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100332 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
333 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
334 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200335
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100336 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
337 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
338 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200339
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100340 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
341 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200342
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200343 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200344
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100345 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
346 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
347 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900348
349 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
350 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200351 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200352};
353
Simon Horman63a4a822012-03-19 07:24:41 +0900354static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100355 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
356
357 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
358 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
359
360 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
361 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
362 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
363 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
364
365 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
366 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
367 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
368};
369
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100370/* checks if <err> is a real error for errno or one that can be ignored, and
371 * return 0 for these ones or <err> for real ones.
372 */
373static inline int unclean_errno(int err)
374{
375 if (err == EAGAIN || err == EINPROGRESS ||
376 err == EISCONN || err == EALREADY)
377 return 0;
378 return err;
379}
380
Christopher Faulet7aa32712021-02-01 13:11:50 +0100381/* Converts check_status code to result code */
382short get_check_status_result(short check_status)
383{
384 if (check_status < HCHK_STATUS_SIZE)
385 return check_statuses[check_status].result;
386 else
387 return check_statuses[HCHK_STATUS_UNKNOWN].result;
388}
389
Christopher Faulet61cc8522020-04-20 14:54:42 +0200390/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200391const char *get_check_status_description(short check_status) {
392
393 const char *desc;
394
395 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200396 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200397 else
398 desc = NULL;
399
400 if (desc && *desc)
401 return desc;
402 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200403 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200404}
405
Christopher Faulet61cc8522020-04-20 14:54:42 +0200406/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100407const char *get_check_status_info(short check_status)
408{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200409 const char *info;
410
411 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200412 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200413 else
414 info = NULL;
415
416 if (info && *info)
417 return info;
418 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200419 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200420}
421
Christopher Faulet61cc8522020-04-20 14:54:42 +0200422/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100423const char *get_analyze_status(short analyze_status) {
424
425 const char *desc;
426
427 if (analyze_status < HANA_STATUS_SIZE)
428 desc = analyze_statuses[analyze_status].desc;
429 else
430 desc = NULL;
431
432 if (desc && *desc)
433 return desc;
434 else
435 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
436}
437
Christopher Faulet61cc8522020-04-20 14:54:42 +0200438/* Sets check->status, update check->duration and fill check->result with an
439 * adequate CHK_RES_* value. The new check->health is computed based on the
440 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200441 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200442 * Shows information in logs about failed health check if server is UP or
443 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200444 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200445void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100446{
Simon Horman4a741432013-02-23 15:35:38 +0900447 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200448 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200449 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900450
Christopher Faulet147b8c92021-04-10 09:00:38 +0200451 TRACE_POINT(CHK_EV_HCHK_RUN, check);
452
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200453 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100454 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900455 check->desc[0] = '\0';
456 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200457 return;
458 }
459
Simon Horman4a741432013-02-23 15:35:38 +0900460 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200461 return;
462
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200463 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900464 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
465 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200466 } else
Simon Horman4a741432013-02-23 15:35:38 +0900467 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200468
Simon Horman4a741432013-02-23 15:35:38 +0900469 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200470 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900471 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200472
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100473 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900474 check->duration = -1;
475 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200476 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900477 check->duration = tv_ms_elapsed(&check->start, &now);
478 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200479 }
480
Willy Tarreau23964182014-05-20 20:56:30 +0200481 /* no change is expected if no state change occurred */
482 if (check->result == CHK_RES_NEUTRAL)
483 return;
484
Olivier Houchard0923fa42019-01-11 18:43:04 +0100485 /* If the check was really just sending a mail, it won't have an
486 * associated server, so we're done now.
487 */
488 if (!s)
489 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200490 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200491
Christopher Faulet147b8c92021-04-10 09:00:38 +0200492
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200493 switch (check->result) {
494 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200495 /* Failure to connect to the agent as a secondary check should not
496 * cause the server to be marked down.
497 */
498 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900499 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200500 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200501 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200502 report = 1;
503 check->health--;
504 if (check->health < check->rise)
505 check->health = 0;
506 }
507 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200508
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200509 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100510 case CHK_RES_CONDPASS:
511 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200512 report = 1;
513 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200514
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200515 if (check->health >= check->rise)
516 check->health = check->rise + check->fall - 1; /* OK now */
517 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200518
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200519 /* clear consecutive_errors if observing is enabled */
520 if (s->onerror)
521 s->consecutive_errors = 0;
522 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100523
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200524 default:
525 break;
526 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200527
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200528 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
529 (status != prev_status || report)) {
530 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200531 "%s check for %sserver %s/%s %s%s",
532 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200533 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100534 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100535 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200536 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200537
Emeric Brun5a133512017-10-19 14:42:30 +0200538 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200539
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100540 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200541 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
542 (check->health >= check->rise) ? check->fall : check->rise,
543 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200544
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 ha_warning("%s.\n", trash.area);
546 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
547 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200548 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200549}
550
Willy Tarreau4eec5472014-05-20 22:32:27 +0200551/* Marks the check <check>'s server down if the current check is already failed
552 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200554void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555{
Simon Horman4a741432013-02-23 15:35:38 +0900556 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900557
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200558 /* The agent secondary check should only cause a server to be marked
559 * as down if check->status is HCHK_STATUS_L7STS, which indicates
560 * that the agent returned "fail", "stopped" or "down".
561 * The implication here is that failure to connect to the agent
562 * as a secondary check should not cause the server to be marked
563 * down. */
564 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
565 return;
566
Willy Tarreau4eec5472014-05-20 22:32:27 +0200567 if (check->health > 0)
568 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100569
Christopher Faulet147b8c92021-04-10 09:00:38 +0200570 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200571 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200572 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573}
574
Willy Tarreauaf549582014-05-16 17:37:50 +0200575/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200576 * it isn't in maintenance, it is not tracking a down server and other checks
577 * comply. The rule is simple : by default, a server is up, unless any of the
578 * following conditions is true :
579 * - health check failed (check->health < rise)
580 * - agent check failed (agent->health < rise)
581 * - the server tracks a down server (track && track->state == STOPPED)
582 * Note that if the server has a slowstart, it will switch to STARTING instead
583 * of RUNNING. Also, only the health checks support the nolb mode, so the
584 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200585 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200586void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200587{
Simon Horman4a741432013-02-23 15:35:38 +0900588 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100589
Emeric Brun52a91d32017-08-31 14:41:55 +0200590 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200591 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100592
Emeric Brun52a91d32017-08-31 14:41:55 +0200593 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200594 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100595
Willy Tarreau3e048382014-05-21 10:30:54 +0200596 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
597 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100598
Willy Tarreau3e048382014-05-21 10:30:54 +0200599 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
600 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200601
Emeric Brun52a91d32017-08-31 14:41:55 +0200602 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200603 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100604
Christopher Faulet147b8c92021-04-10 09:00:38 +0200605 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Emeric Brun5a133512017-10-19 14:42:30 +0200606 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100607}
608
Willy Tarreaudb58b792014-05-21 13:57:23 +0200609/* Marks the check <check> as valid and tries to set its server into stopping mode
610 * if it was running or starting, and provided it isn't in maintenance and other
611 * checks comply. The conditions for the server to be marked in stopping mode are
612 * the same as for it to be turned up. Also, only the health checks support the
613 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200614 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200615void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200616{
Simon Horman4a741432013-02-23 15:35:38 +0900617 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100618
Emeric Brun52a91d32017-08-31 14:41:55 +0200619 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200620 return;
621
Willy Tarreaudb58b792014-05-21 13:57:23 +0200622 if (check->state & CHK_ST_AGENT)
623 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100624
Emeric Brun52a91d32017-08-31 14:41:55 +0200625 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200626 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100627
Willy Tarreaudb58b792014-05-21 13:57:23 +0200628 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
629 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100630
Willy Tarreaudb58b792014-05-21 13:57:23 +0200631 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
632 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100633
Christopher Faulet147b8c92021-04-10 09:00:38 +0200634 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 +0100635 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100636}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100638/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100639 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100640 */
641void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100642{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100643 int failed;
644 int expire;
645
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100646 if (s->observe >= HANA_OBS_SIZE)
647 return;
648
Willy Tarreaubb956662013-01-24 00:37:39 +0100649 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100650 return;
651
652 switch (analyze_statuses[status].lr[s->observe - 1]) {
653 case 1:
654 failed = 1;
655 break;
656
657 case 2:
658 failed = 0;
659 break;
660
661 default:
662 return;
663 }
664
665 if (!failed) {
666 /* good: clear consecutive_errors */
667 s->consecutive_errors = 0;
668 return;
669 }
670
Willy Tarreau4781b152021-04-06 13:53:36 +0200671 _HA_ATOMIC_INC(&s->consecutive_errors);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100672
673 if (s->consecutive_errors < s->consecutive_errors_limit)
674 return;
675
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100676 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
677 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100678
Willy Tarreau4e9df272021-02-17 15:20:19 +0100679 if (s->check.fastinter)
680 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
681 else
682 expire = TICK_ETERNITY;
683
684 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
685
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100686 switch (s->onerror) {
687 case HANA_ONERR_FASTINTER:
688 /* force fastinter - nothing to do here as all modes force it */
689 break;
690
691 case HANA_ONERR_SUDDTH:
692 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900693 if (s->check.health > s->check.rise)
694 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100695
Tim Duesterhus588b3142020-05-29 14:35:51 +0200696 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100697
698 case HANA_ONERR_FAILCHK:
699 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200700 set_server_check_status(&s->check, HCHK_STATUS_HANA,
701 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200702 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100703 break;
704
705 case HANA_ONERR_MARKDWN:
706 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900707 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 set_server_check_status(&s->check, HCHK_STATUS_HANA,
709 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200710 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100711 break;
712
713 default:
714 /* write a warning? */
715 break;
716 }
717
Willy Tarreau4e9df272021-02-17 15:20:19 +0100718 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
719
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100720 s->consecutive_errors = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200721 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100722
Christopher Fauletea860832021-05-07 11:45:26 +0200723 if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) {
Willy Tarreau4e9df272021-02-17 15:20:19 +0100724 /* requeue check task with new expire */
725 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100726 }
Willy Tarreauef781042010-01-27 11:53:01 +0100727}
728
Christopher Faulet61cc8522020-04-20 14:54:42 +0200729/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100730 * closed, keep errno intact as it is supposed to contain the valid error code.
731 * If no error is reported, check the socket's error queue using getsockopt().
732 * Warning, this must be done only once when returning from poll, and never
733 * after an I/O error was attempted, otherwise the error queue might contain
734 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
735 * socket. Returns non-zero if an error was reported, zero if everything is
736 * clean (including a properly closed socket).
737 */
738static int retrieve_errno_from_socket(struct connection *conn)
739{
740 int skerr;
741 socklen_t lskerr = sizeof(skerr);
742
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100743 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100744 return 1;
745
Willy Tarreau3c728722014-01-23 13:50:42 +0100746 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100747 return 0;
748
Willy Tarreau585744b2017-08-24 14:31:19 +0200749 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100750 errno = skerr;
751
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100752 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100753
754 if (!errno) {
755 /* we could not retrieve an error, that does not mean there is
756 * none. Just don't change anything and only report the prior
757 * error if any.
758 */
759 if (conn->flags & CO_FL_ERROR)
760 return 1;
761 else
762 return 0;
763 }
764
765 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
766 return 1;
767}
768
Christopher Faulet61cc8522020-04-20 14:54:42 +0200769/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100770 * and adjust the server status accordingly. It may make use of <errno_bck>
771 * if non-null when the caller is absolutely certain of its validity (eg:
772 * checked just after a syscall). If the caller doesn't have a valid errno,
773 * it can pass zero, and retrieve_errno_from_socket() will be called to try
774 * to extract errno from the socket. If no error is reported, it will consider
775 * the <expired> flag. This is intended to be used when a connection error was
776 * reported in conn->flags or when a timeout was reported in <expired>. The
777 * function takes care of not updating a server status which was already set.
778 * All situations where at least one of <expired> or CO_FL_ERROR are set
779 * produce a status.
780 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200781void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100782{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200783 struct conn_stream *cs = check->cs;
784 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100785 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200786 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200787 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100788
Christopher Faulet147b8c92021-04-10 09:00:38 +0200789 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100790 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200791 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100792
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100793 errno = unclean_errno(errno_bck);
794 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100795 retrieve_errno_from_socket(conn);
796
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200797 if (conn && !(conn->flags & CO_FL_ERROR) &&
798 !(cs->flags & CS_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100799 return;
800
Christopher Faulet147b8c92021-04-10 09:00:38 +0200801 TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
802
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100803 /* we'll try to build a meaningful error message depending on the
804 * context of the error possibly present in conn->err_code, and the
805 * socket error possibly collected above. This is useful to know the
806 * exact step of the L6 layer (eg: SSL handshake).
807 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200808 chk = get_trash_chunk();
809
Christopher Faulet799f3a42020-04-07 12:06:14 +0200810 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200811 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200812 step = tcpcheck_get_step_id(check, NULL);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200813 if (!step) {
814 TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200815 chunk_printf(chk, " at initial connection step of tcp-check");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200816 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200817 else {
818 chunk_printf(chk, " at step %d of tcp-check", step);
819 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200820 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
821 if (check->current_step->connect.port)
822 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200823 else
824 chunk_appendf(chk, " (connect)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200825 TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200826 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200827 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
828 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100829
830 switch (expect->type) {
831 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200832 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100833 break;
834 case TCPCHK_EXPECT_BINARY:
Christopher Faulet326e2972023-10-30 09:00:37 +0100835 chunk_appendf(chk, " (expect binary '");
836 dump_binary(chk, istptr(expect->data), (int)istlen(expect->data));
837 chunk_appendf(chk, "')");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100838 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200839 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200840 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100841 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200842 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100843 chunk_appendf(chk, " (expect binary regex)");
844 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200845 case TCPCHK_EXPECT_STRING_LF:
846 chunk_appendf(chk, " (expect log-format string)");
847 break;
848 case TCPCHK_EXPECT_BINARY_LF:
849 chunk_appendf(chk, " (expect log-format binary)");
850 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200851 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200852 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200853 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200854 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200855 chunk_appendf(chk, " (expect HTTP status regex)");
856 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200857 case TCPCHK_EXPECT_HTTP_HEADER:
858 chunk_appendf(chk, " (expect HTTP header pattern)");
859 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200860 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200861 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200862 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200863 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200864 chunk_appendf(chk, " (expect HTTP body regex)");
865 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200866 case TCPCHK_EXPECT_HTTP_BODY_LF:
867 chunk_appendf(chk, " (expect log-format HTTP body)");
868 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200869 case TCPCHK_EXPECT_CUSTOM:
870 chunk_appendf(chk, " (expect custom function)");
871 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100872 case TCPCHK_EXPECT_UNDEF:
873 chunk_appendf(chk, " (undefined expect!)");
874 break;
875 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200876 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200877 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200878 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200879 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200880 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200881 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200882
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200883 if (check->current_step && check->current_step->comment)
884 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200885 }
886 }
887
Willy Tarreau00149122017-10-04 18:05:01 +0200888 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100889 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200890 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
891 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100892 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200893 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
894 chk->area);
895 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100896 }
897 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100898 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200899 chunk_printf(&trash, "%s%s", strerror(errno),
900 chk->area);
901 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100902 }
903 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200904 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100905 }
906 }
907
Willy Tarreau00149122017-10-04 18:05:01 +0200908 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200909 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200910 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
911 }
912
Christopher Faulet5e293762020-10-26 11:10:49 +0100913 if (!conn || !conn->ctrl) {
914 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200915 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
916 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100917 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100918 /* L4 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200919 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100920 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
921 else if (expired)
922 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200923
924 /*
925 * might be due to a server IP change.
926 * Let's trigger a DNS resolution if none are currently running.
927 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100928 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100929 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200930
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100931 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100932 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100933 /* L6 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200934 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100935 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
936 else if (expired)
937 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
938 }
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200939 else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100940 /* I/O error after connection was established and before we could diagnose */
941 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
942 }
943 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200944 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
945
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100946 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200947 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
948 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200949 tout = check->current_step->expect.tout_status;
950 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100951 }
952
Christopher Faulet147b8c92021-04-10 09:00:38 +0200953 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100954 return;
955}
956
Simon Horman98637e52014-06-20 12:30:16 +0900957
Christopher Faulet61cc8522020-04-20 14:54:42 +0200958/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200959int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900960{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200961 int sv_state;
962 int ratio;
963 char addr[46];
964 char port[6];
965 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
966 "UP %d/%d", "UP",
967 "NOLB %d/%d", "NOLB",
968 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900969
Christopher Faulet61cc8522020-04-20 14:54:42 +0200970 if (!(s->check.state & CHK_ST_ENABLED))
971 sv_state = 6;
972 else if (s->cur_state != SRV_ST_STOPPED) {
973 if (s->check.health == s->check.rise + s->check.fall - 1)
974 sv_state = 3; /* UP */
975 else
976 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900977
Christopher Faulet61cc8522020-04-20 14:54:42 +0200978 if (s->cur_state == SRV_ST_STOPPING)
979 sv_state += 2;
980 } else {
981 if (s->check.health)
982 sv_state = 1; /* going up */
983 else
984 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900985 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200986
Christopher Faulet61cc8522020-04-20 14:54:42 +0200987 chunk_appendf(buf, srv_hlt_st[sv_state],
988 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
989 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200990
Christopher Faulet61cc8522020-04-20 14:54:42 +0200991 addr_to_str(&s->addr, addr, sizeof(addr));
992 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
993 snprintf(port, sizeof(port), "%u", s->svc_port);
994 else
995 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200996
Christopher Faulet61cc8522020-04-20 14:54:42 +0200997 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
998 addr, port, s->proxy->id, s->id,
999 global.node,
1000 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
1001 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
1002 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
1003 s->nbpend);
Willy Tarreau9f6dc722019-03-01 11:15:10 +01001004
Christopher Faulet61cc8522020-04-20 14:54:42 +02001005 if ((s->cur_state == SRV_ST_STARTING) &&
1006 now.tv_sec < s->last_change + s->slowstart &&
1007 now.tv_sec >= s->last_change) {
1008 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
1009 chunk_appendf(buf, "; throttle=%d%%", ratio);
1010 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001011
Christopher Faulet61cc8522020-04-20 14:54:42 +02001012 return b_data(buf);
1013}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001014
Willy Tarreau51cd5952020-06-05 12:25:38 +02001015/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001016/***************** Health-checks based on connections *********************/
1017/**************************************************************************/
1018/* This function is used only for server health-checks. It handles connection
1019 * status updates including errors. If necessary, it wakes the check task up.
1020 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1021 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001022 */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001023static int wake_srv_chk(struct conn_stream *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001024{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001025 struct connection *conn = cs->conn;
1026 struct check *check = cs->data;
1027 struct email_alertq *q = container_of(check, typeof(*q), check);
1028 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001029
Christopher Faulet147b8c92021-04-10 09:00:38 +02001030 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001031 if (check->server)
1032 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1033 else
1034 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001035
Willy Tarreau51cd5952020-06-05 12:25:38 +02001036 /* we may have to make progress on the TCP checks */
1037 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001038
Willy Tarreau51cd5952020-06-05 12:25:38 +02001039 cs = check->cs;
1040 conn = cs->conn;
Christopher Fauletaaab0832020-05-05 15:54:22 +02001041
Willy Tarreau51cd5952020-06-05 12:25:38 +02001042 if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) {
1043 /* We may get error reports bypassing the I/O handlers, typically
1044 * the case when sending a pure TCP check which fails, then the I/O
1045 * handlers above are not called. This is completely handled by the
1046 * main processing task so let's simply wake it up. If we get here,
1047 * we expect errno to still be valid.
1048 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001049 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 +02001050 chk_report_conn_err(check, errno, 0);
1051 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001052 }
1053
Christopher Faulet8f100422021-01-18 15:47:03 +01001054 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001055 /* Check complete or aborted. If connection not yet closed do it
1056 * now and wake the check task up to be sure the result is
1057 * handled ASAP. */
Willy Tarreau30bd4ef2020-12-11 11:09:29 +01001058 cs_drain_and_close(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001059 ret = -1;
Christopher Faulet8f100422021-01-18 15:47:03 +01001060
1061 if (check->wait_list.events)
1062 cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
1063
Willy Tarreau51cd5952020-06-05 12:25:38 +02001064 /* We may have been scheduled to run, and the
1065 * I/O handler expects to have a cs, so remove
1066 * the tasklet
1067 */
1068 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
1069 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001070 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001071
Willy Tarreau51cd5952020-06-05 12:25:38 +02001072 if (check->server)
1073 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1074 else
1075 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001076
Christopher Faulet147b8c92021-04-10 09:00:38 +02001077 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001078 return ret;
1079}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001080
Willy Tarreau51cd5952020-06-05 12:25:38 +02001081/* This function checks if any I/O is wanted, and if so, attempts to do so */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001082struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001083{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001084 struct check *check = ctx;
1085 struct conn_stream *cs = check->cs;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001086
Willy Tarreau51cd5952020-06-05 12:25:38 +02001087 wake_srv_chk(cs);
1088 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001089}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001090
Willy Tarreau51cd5952020-06-05 12:25:38 +02001091/* manages a server health-check that uses a connection. Returns
1092 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001093 *
1094 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001095 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001096 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001097struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001098{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001099 struct check *check = context;
1100 struct proxy *proxy = check->proxy;
Christopher Faulet92017a32021-05-06 16:01:18 +02001101 struct conn_stream *cs;
1102 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001103 int rv;
1104 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001105
Christopher Faulet147b8c92021-04-10 09:00:38 +02001106 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1107
Willy Tarreau51cd5952020-06-05 12:25:38 +02001108 if (check->server)
1109 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001110
Willy Tarreau51cd5952020-06-05 12:25:38 +02001111 if (!(check->state & CHK_ST_INPROGRESS)) {
1112 /* no check currently running */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001113 if (!expired) /* woke up too early */ {
1114 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001115 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001116 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001117
Willy Tarreau51cd5952020-06-05 12:25:38 +02001118 /* we don't send any health-checks when the proxy is
1119 * stopped, the server should not be checked or the check
1120 * is disabled.
1121 */
1122 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Faulet147b8c92021-04-10 09:00:38 +02001123 proxy->disabled) {
1124 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001125 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001126 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001127
Willy Tarreau51cd5952020-06-05 12:25:38 +02001128 /* we'll initiate a new check */
1129 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001130
Willy Tarreau51cd5952020-06-05 12:25:38 +02001131 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001132 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001133
Willy Tarreau51cd5952020-06-05 12:25:38 +02001134 task_set_affinity(t, tid_bit);
1135
1136 check->current_step = NULL;
1137 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001138 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001139 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001140
1141 cs = check->cs;
1142 conn = cs_conn(cs);
1143
1144 /* there was a test running.
1145 * First, let's check whether there was an uncaught error,
1146 * which can happen on connect timeout or error.
1147 */
1148 if (check->result == CHK_RES_UNKNOWN) {
1149 /* Here the connection must be defined. Otherwise the
1150 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001151 */
Christopher Faulet92017a32021-05-06 16:01:18 +02001152 if ((conn && ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR))) || expired) {
1153 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1154 chk_report_conn_err(check, 0, expired);
1155 }
1156 else {
1157 if (check->state & CHK_ST_CLOSE_CONN) {
1158 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1159 cs_destroy(cs);
1160 cs = NULL;
1161 conn = NULL;
1162 check->cs = NULL;
1163 check->state &= ~CHK_ST_CLOSE_CONN;
1164 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001165 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001166 if (check->result == CHK_RES_UNKNOWN) {
1167 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1168 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001169 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001170 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001171 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001172
Christopher Faulet92017a32021-05-06 16:01:18 +02001173 /* check complete or aborted */
1174 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001175
Christopher Faulet92017a32021-05-06 16:01:18 +02001176 check->current_step = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001177
Christopher Faulet92017a32021-05-06 16:01:18 +02001178 if (conn && conn->xprt) {
1179 /* The check was aborted and the connection was not yet closed.
1180 * This can happen upon timeout, or when an external event such
1181 * as a failed response coupled with "observe layer7" caused the
1182 * server state to be suddenly changed.
1183 */
1184 cs_drain_and_close(cs);
1185 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001186
Christopher Faulet92017a32021-05-06 16:01:18 +02001187 if (cs) {
1188 if (check->wait_list.events)
1189 cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
1190 /* We may have been scheduled to run, and the
1191 * I/O handler expects to have a cs, so remove
1192 * the tasklet
1193 */
1194 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
1195 cs_destroy(cs);
1196 cs = check->cs = NULL;
1197 conn = NULL;
1198 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001199
Christopher Faulet92017a32021-05-06 16:01:18 +02001200 if (check->sess != NULL) {
1201 vars_prune(&check->vars, check->sess, NULL);
1202 session_free(check->sess);
1203 check->sess = NULL;
1204 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001205
Christopher Faulet92017a32021-05-06 16:01:18 +02001206 if (check->server) {
1207 if (check->result == CHK_RES_FAILED) {
1208 /* a failure or timeout detected */
1209 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1210 check_notify_failure(check);
1211 }
1212 else if (check->result == CHK_RES_CONDPASS) {
1213 /* check is OK but asks for stopping mode */
1214 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1215 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001216 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001217 else if (check->result == CHK_RES_PASSED) {
1218 /* a success was detected */
1219 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1220 check_notify_success(check);
1221 }
1222 }
Christopher Fauletecc34262022-05-18 14:24:43 +02001223
1224 if (LIST_INLIST(&check->buf_wait.list))
1225 LIST_DEL_INIT(&check->buf_wait.list);
1226
Christopher Faulet92017a32021-05-06 16:01:18 +02001227 task_set_affinity(t, MAX_THREADS_MASK);
1228 check_release_buf(check, &check->bi);
1229 check_release_buf(check, &check->bo);
1230 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001231
Christopher Faulet92017a32021-05-06 16:01:18 +02001232 if (check->server) {
1233 rv = 0;
1234 if (global.spread_checks > 0) {
1235 rv = srv_getinter(check) * global.spread_checks / 100;
1236 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001237 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001238 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001239 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001240
1241 reschedule:
1242 while (tick_is_expired(t->expire, now_ms))
1243 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1244 out_unlock:
1245 if (check->server)
1246 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001247
1248 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001249 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001250}
1251
Willy Tarreau51cd5952020-06-05 12:25:38 +02001252
Christopher Faulet61cc8522020-04-20 14:54:42 +02001253/**************************************************************************/
1254/************************** Init/deinit checks ****************************/
1255/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001256/*
1257 * Tries to grab a buffer and to re-enables processing on check <target>. The
1258 * check flags are used to figure what buffer was requested. It returns 1 if the
1259 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1260 * impossible to wake up and we prefer to be woken up later.
1261 */
1262int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001263{
Christopher Fauletb381a502020-11-25 13:47:00 +01001264 struct check *check = target;
1265
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001266 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001267 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001268 check->state &= ~CHK_ST_IN_ALLOC;
1269 tasklet_wakeup(check->wait_list.tasklet);
1270 return 1;
1271 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001272 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001273 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001274 check->state &= ~CHK_ST_OUT_ALLOC;
1275 tasklet_wakeup(check->wait_list.tasklet);
1276 return 1;
1277 }
1278
1279 return 0;
1280}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001281
Christopher Fauletb381a502020-11-25 13:47:00 +01001282/*
William Dauchyf4300902021-02-06 20:47:50 +01001283 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001284 */
1285struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1286{
1287 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001288
Willy Tarreau2b718102021-04-21 07:32:39 +02001289 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001290 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001291 check->buf_wait.target = check;
1292 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreau2b718102021-04-21 07:32:39 +02001293 LIST_APPEND(&ti->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001294 }
1295 return buf;
1296}
1297
1298/*
1299 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1300 * wait queue.
1301 */
1302void check_release_buf(struct check *check, struct buffer *bptr)
1303{
1304 if (bptr->size) {
1305 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001306 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001307 }
1308}
1309
1310const char *init_check(struct check *check, int type)
1311{
1312 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001313
Christopher Fauletb381a502020-11-25 13:47:00 +01001314 check->bi = BUF_NULL;
1315 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001316 LIST_INIT(&check->buf_wait.list);
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001317
Christopher Faulet61cc8522020-04-20 14:54:42 +02001318 check->wait_list.tasklet = tasklet_new();
1319 if (!check->wait_list.tasklet)
1320 return "out of memory while allocating check tasklet";
1321 check->wait_list.events = 0;
1322 check->wait_list.tasklet->process = event_srv_chk_io;
1323 check->wait_list.tasklet->context = check;
1324 return NULL;
1325}
1326
1327void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001328{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001329 task_destroy(check->task);
1330 if (check->wait_list.tasklet)
1331 tasklet_free(check->wait_list.tasklet);
1332
Christopher Fauletb381a502020-11-25 13:47:00 +01001333 check_release_buf(check, &check->bi);
1334 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001335 if (check->cs) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001336 ha_free(&check->cs->conn);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001337 cs_free(check->cs);
1338 check->cs = NULL;
1339 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001340}
1341
Christopher Faulet61cc8522020-04-20 14:54:42 +02001342/* manages a server health-check. Returns the time the task accepts to wait, or
1343 * TIME_ETERNITY for infinity.
1344 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001345struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001346{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001347 struct check *check = context;
1348
1349 if (check->type == PR_O2_EXT_CHK)
1350 return process_chk_proc(t, context, state);
1351 return process_chk_conn(t, context, state);
1352
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001353}
1354
Christopher Faulet61cc8522020-04-20 14:54:42 +02001355
1356static int start_check_task(struct check *check, int mininter,
1357 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001358{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001359 struct task *t;
1360 unsigned long thread_mask = MAX_THREADS_MASK;
Willy Tarreauc4dc79b2023-05-17 09:01:22 +02001361 ulong boottime = tv_ms_remain(&start_date, &ready_date);
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001362
Christopher Faulet61cc8522020-04-20 14:54:42 +02001363 if (check->type == PR_O2_EXT_CHK)
1364 thread_mask = 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001365
Christopher Faulet61cc8522020-04-20 14:54:42 +02001366 /* task for the check */
1367 if ((t = task_new(thread_mask)) == NULL) {
1368 ha_alert("Starting [%s:%s] check: out of memory.\n",
1369 check->server->proxy->id, check->server->id);
1370 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001371 }
1372
Christopher Faulet61cc8522020-04-20 14:54:42 +02001373 check->task = t;
1374 t->process = process_chk;
1375 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001376
Christopher Faulet61cc8522020-04-20 14:54:42 +02001377 if (mininter < srv_getinter(check))
1378 mininter = srv_getinter(check);
1379
Willy Tarreau18e018a2023-05-17 07:39:57 +02001380 if (global.spread_checks > 0) {
1381 int rnd;
1382
1383 rnd = srv_getinter(check) * global.spread_checks / 100;
1384 rnd -= (int) (2 * rnd * (ha_random32() / 4294967295.0));
1385 mininter += rnd;
1386 }
1387
Christopher Faulet61cc8522020-04-20 14:54:42 +02001388 if (global.max_spread_checks && mininter > global.max_spread_checks)
1389 mininter = global.max_spread_checks;
1390
1391 /* check this every ms */
Willy Tarreauc4dc79b2023-05-17 09:01:22 +02001392 t->expire = tick_add(now_ms, MS_TO_TICKS(boottime + mininter * srvpos / nbcheck));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001393 check->start = now;
1394 task_queue(t);
1395
1396 return 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001397}
1398
Christopher Faulet61cc8522020-04-20 14:54:42 +02001399/* updates the server's weight during a warmup stage. Once the final weight is
1400 * reached, the task automatically stops. Note that any server status change
1401 * must have updated s->last_change accordingly.
1402 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001403struct task *server_warmup(struct task *t, void *context, unsigned int state)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001404{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001405 struct server *s = context;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001406
Christopher Faulet61cc8522020-04-20 14:54:42 +02001407 /* by default, plan on stopping the task */
1408 t->expire = TICK_ETERNITY;
1409 if ((s->next_admin & SRV_ADMF_MAINT) ||
1410 (s->next_state != SRV_ST_STARTING))
1411 return t;
Christopher Faulete5870d82020-04-15 11:32:03 +02001412
Christopher Faulet61cc8522020-04-20 14:54:42 +02001413 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001414
Christopher Faulet61cc8522020-04-20 14:54:42 +02001415 /* recalculate the weights and update the state */
1416 server_recalc_eweight(s, 1);
Christopher Faulet5c288742020-03-31 08:15:58 +02001417
Christopher Faulet61cc8522020-04-20 14:54:42 +02001418 /* probably that we can refill this server with a bit more connections */
1419 pendconn_grab_from_px(s);
Christopher Faulet5c288742020-03-31 08:15:58 +02001420
Christopher Faulet61cc8522020-04-20 14:54:42 +02001421 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5c288742020-03-31 08:15:58 +02001422
Christopher Faulet61cc8522020-04-20 14:54:42 +02001423 /* get back there in 1 second or 1/20th of the slowstart interval,
1424 * whichever is greater, resulting in small 5% steps.
1425 */
1426 if (s->next_state == SRV_ST_STARTING)
1427 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1428 return t;
1429}
1430
1431/*
1432 * Start health-check.
1433 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1434 */
1435static int start_checks()
1436{
1437
1438 struct proxy *px;
1439 struct server *s;
1440 struct task *t;
1441 int nbcheck=0, mininter=0, srvpos=0;
1442
1443 /* 0- init the dummy frontend used to create all checks sessions */
1444 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001445 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001446 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1447 checks_fe.mode = PR_MODE_TCP;
1448 checks_fe.maxconn = 0;
1449 checks_fe.conn_retries = CONN_RETRIES;
1450 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1451 checks_fe.timeout.client = TICK_ETERNITY;
1452
1453 /* 1- count the checkers to run simultaneously.
1454 * We also determine the minimum interval among all of those which
1455 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1456 * will be used to spread their start-up date. Those which have
1457 * a shorter interval will start independently and will not dictate
1458 * too short an interval for all others.
1459 */
1460 for (px = proxies_list; px; px = px->next) {
1461 for (s = px->srv; s; s = s->next) {
1462 if (s->slowstart) {
1463 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
1464 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1465 return ERR_ALERT | ERR_FATAL;
1466 }
1467 /* We need a warmup task that will be called when the server
1468 * state switches from down to up.
1469 */
1470 s->warmup = t;
1471 t->process = server_warmup;
1472 t->context = s;
1473 /* server can be in this state only because of */
1474 if (s->next_state == SRV_ST_STARTING)
1475 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20)));
Christopher Faulet5c288742020-03-31 08:15:58 +02001476 }
1477
Christopher Faulet61cc8522020-04-20 14:54:42 +02001478 if (s->check.state & CHK_ST_CONFIGURED) {
1479 nbcheck++;
1480 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1481 (!mininter || mininter > srv_getinter(&s->check)))
1482 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001483 }
1484
Christopher Faulet61cc8522020-04-20 14:54:42 +02001485 if (s->agent.state & CHK_ST_CONFIGURED) {
1486 nbcheck++;
1487 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1488 (!mininter || mininter > srv_getinter(&s->agent)))
1489 mininter = srv_getinter(&s->agent);
1490 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001491 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001492 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001493
Christopher Faulet61cc8522020-04-20 14:54:42 +02001494 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001495 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001496
Christopher Faulet61cc8522020-04-20 14:54:42 +02001497 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001498
William Dauchyf4300902021-02-06 20:47:50 +01001499 /* 2- start them as far as possible from each other. For this, we will
1500 * start them after their interval is set to the min interval divided
1501 * by the number of servers, weighted by the server's position in the
1502 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001503 */
1504 for (px = proxies_list; px; px = px->next) {
1505 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1506 if (init_pid_list()) {
1507 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1508 return ERR_ALERT | ERR_FATAL;
1509 }
1510 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001511
Christopher Faulet61cc8522020-04-20 14:54:42 +02001512 for (s = px->srv; s; s = s->next) {
1513 /* A task for the main check */
1514 if (s->check.state & CHK_ST_CONFIGURED) {
1515 if (s->check.type == PR_O2_EXT_CHK) {
1516 if (!prepare_external_check(&s->check))
1517 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001518 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001519 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1520 return ERR_ALERT | ERR_FATAL;
1521 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001522 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001523
Christopher Faulet61cc8522020-04-20 14:54:42 +02001524 /* A task for a auxiliary agent check */
1525 if (s->agent.state & CHK_ST_CONFIGURED) {
1526 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1527 return ERR_ALERT | ERR_FATAL;
1528 }
1529 srvpos++;
1530 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001531 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001532 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001533 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001534}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001535
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001536
Christopher Faulet61cc8522020-04-20 14:54:42 +02001537/*
1538 * Return value:
1539 * the port to be used for the health check
1540 * 0 in case no port could be found for the check
1541 */
1542static int srv_check_healthcheck_port(struct check *chk)
1543{
1544 int i = 0;
1545 struct server *srv = NULL;
1546
1547 srv = chk->server;
1548
William Dauchyf4300902021-02-06 20:47:50 +01001549 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001550 if (chk->port > 0)
1551 return chk->port;
1552
1553 /* try to get the port from check_core.addr if check.port not set */
1554 i = get_host_port(&chk->addr);
1555 if (i > 0)
1556 return i;
1557
1558 /* try to get the port from server address */
1559 /* prevent MAPPORTS from working at this point, since checks could
1560 * not be performed in such case (MAPPORTS impose a relative ports
1561 * based on live traffic)
1562 */
1563 if (srv->flags & SRV_F_MAPPORTS)
1564 return 0;
1565
1566 i = srv->svc_port; /* by default */
1567 if (i > 0)
1568 return i;
1569
1570 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001571}
1572
Christopher Faulet61cc8522020-04-20 14:54:42 +02001573/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1574 * if an error occurred.
1575 */
1576static int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001577{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001578 const char *err;
1579 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001580 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001581 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001582
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001583 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001584 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001585
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001586 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001587
Christopher Faulet61cc8522020-04-20 14:54:42 +02001588 /* If neither a port nor an addr was specified and no check transport
1589 * layer is forced, then the transport layer used by the checks is the
1590 * same as for the production traffic. Otherwise we use raw_sock by
1591 * default, unless one is specified.
1592 */
1593 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1594 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1595 srv->check.use_ssl = srv->use_ssl;
1596 srv->check.xprt = srv->xprt;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001597 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001598 else if (srv->check.use_ssl == 1)
1599 srv->check.xprt = xprt_get(XPRT_SSL);
1600 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001601 }
Christopher Faulet66163ec2020-05-20 22:36:24 +02001602 else if (srv->check.use_ssl == 1)
1603 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001604
Christopher Faulet12882cf2020-04-23 15:50:18 +02001605 /* Inherit the mux protocol from the server if not already defined for
1606 * the check
1607 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001608 if (srv->mux_proto && !srv->check.mux_proto &&
1609 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1610 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001611 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001612 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001613 /* test that check proto is valid if explicitly defined */
1614 else if (srv->check.mux_proto &&
1615 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1616 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1617 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1618 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1619 ret |= ERR_ALERT | ERR_FATAL;
1620 goto out;
1621 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001622
Christopher Faulet61cc8522020-04-20 14:54:42 +02001623 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001624
Christopher Faulet61cc8522020-04-20 14:54:42 +02001625 /* We need at least a service port, a check port or the first tcp-check
1626 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1627 */
1628 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1629 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1630 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001631
Christopher Faulet61cc8522020-04-20 14:54:42 +02001632 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1633 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1634 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1635 ret |= ERR_ALERT | ERR_ABORT;
1636 goto out;
1637 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001638
Christopher Faulet61cc8522020-04-20 14:54:42 +02001639 /* search the first action (connect / send / expect) in the list */
1640 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1641 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1642 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1643 "nor tcp_check rule 'connect' with port information.\n",
1644 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1645 ret |= ERR_ALERT | ERR_ABORT;
1646 goto out;
1647 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001648
Christopher Faulet61cc8522020-04-20 14:54:42 +02001649 /* scan the tcp-check ruleset to ensure a port has been configured */
1650 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreau565f3332021-07-22 11:06:41 +02001651 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001652 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1653 "and a tcp_check rule 'connect' with no port information.\n",
1654 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1655 ret |= ERR_ALERT | ERR_ABORT;
1656 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001657 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001658 }
1659
Christopher Faulet61cc8522020-04-20 14:54:42 +02001660 init:
1661 if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) {
1662 struct tcpcheck_ruleset *rs = NULL;
1663 struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules;
1664 //char *errmsg = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001665
Christopher Faulet61cc8522020-04-20 14:54:42 +02001666 srv->proxy->options2 &= ~PR_O2_CHK_ANY;
1667 srv->proxy->options2 |= PR_O2_TCPCHK_CHK;
Christopher Faulete5870d82020-04-15 11:32:03 +02001668
Christopher Faulet61cc8522020-04-20 14:54:42 +02001669 rs = find_tcpcheck_ruleset("*tcp-check");
1670 if (!rs) {
1671 rs = create_tcpcheck_ruleset("*tcp-check");
1672 if (rs == NULL) {
1673 ha_alert("config: %s '%s': out of memory.\n",
1674 proxy_type_str(srv->proxy), srv->proxy->id);
1675 ret |= ERR_ALERT | ERR_FATAL;
1676 goto out;
1677 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001678 }
1679
Christopher Faulet61cc8522020-04-20 14:54:42 +02001680 free_tcpcheck_vars(&rules->preset_vars);
1681 rules->list = &rs->rules;
1682 rules->flags = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001683 }
1684
Christopher Faulet61cc8522020-04-20 14:54:42 +02001685 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1686 if (err) {
1687 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1688 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1689 ret |= ERR_ALERT | ERR_ABORT;
1690 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001691 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001692 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1693 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001694
Christopher Faulet61cc8522020-04-20 14:54:42 +02001695 out:
1696 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001697}
1698
Christopher Faulet61cc8522020-04-20 14:54:42 +02001699/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1700 * if an error occurred.
1701 */
1702static int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001703{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001704 struct tcpcheck_rule *chk;
1705 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001706 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001707
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001708 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001709 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001710
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001711 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001712 * implicit one is inserted before all others.
1713 */
1714 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1715 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1716 chk = calloc(1, sizeof(*chk));
1717 if (!chk) {
1718 ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule"
1719 " to agent-check for server '%s' (out of memory).\n",
1720 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1721 ret |= ERR_ALERT | ERR_FATAL;
1722 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001723 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001724 chk->action = TCPCHK_ACT_CONNECT;
1725 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001726 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001727 }
1728
Christopher Faulet8af4ab82022-08-24 11:38:03 +02001729 /* <chk> is always defined here and it is a CONNECT action. If there is
1730 * a preset variable, it means there is an agent string defined and data
1731 * will be sent after the connect.
1732 */
1733 if (!LIST_ISEMPTY(&srv->agent.tcpcheck_rules->preset_vars))
1734 chk->connect.options |= TCPCHK_OPT_HAS_DATA;
1735
Christopher Faulete5870d82020-04-15 11:32:03 +02001736
Christopher Faulet61cc8522020-04-20 14:54:42 +02001737 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1738 if (err) {
1739 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1740 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1741 ret |= ERR_ALERT | ERR_ABORT;
1742 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001743 }
1744
Christopher Faulet61cc8522020-04-20 14:54:42 +02001745 if (!srv->agent.inter)
1746 srv->agent.inter = srv->check.inter;
1747
1748 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1749 global.maxsock++;
1750
1751 out:
1752 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001753}
1754
Christopher Faulet61cc8522020-04-20 14:54:42 +02001755static void deinit_srv_check(struct server *srv)
1756{
1757 if (srv->check.state & CHK_ST_CONFIGURED)
1758 free_check(&srv->check);
1759 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1760 srv->do_check = 0;
1761}
Christopher Faulete5870d82020-04-15 11:32:03 +02001762
Christopher Faulet61cc8522020-04-20 14:54:42 +02001763
1764static void deinit_srv_agent_check(struct server *srv)
1765{
1766 if (srv->agent.tcpcheck_rules) {
1767 free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001768 ha_free(&srv->agent.tcpcheck_rules);
Christopher Faulete5870d82020-04-15 11:32:03 +02001769 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001770
Christopher Faulet61cc8522020-04-20 14:54:42 +02001771 if (srv->agent.state & CHK_ST_CONFIGURED)
1772 free_check(&srv->agent);
1773
1774 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1775 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001776}
1777
Willy Tarreaucee013e2020-06-05 11:40:38 +02001778REGISTER_POST_SERVER_CHECK(init_srv_check);
1779REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001780REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001781
Willy Tarreaucee013e2020-06-05 11:40:38 +02001782REGISTER_SERVER_DEINIT(deinit_srv_check);
1783REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001784
Christopher Faulet61cc8522020-04-20 14:54:42 +02001785
1786/**************************************************************************/
1787/************************** Check sample fetches **************************/
1788/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001789
Christopher Faulet61cc8522020-04-20 14:54:42 +02001790static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001791 { /* END */ },
1792}};
1793
1794INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1795
1796
1797/**************************************************************************/
1798/************************ Check's parsing functions ***********************/
1799/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001800/* Parse the "addr" server keyword */
1801static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1802 char **errmsg)
1803{
1804 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001805 int port1, port2, err_code = 0;
1806
1807
1808 if (!*args[*cur_arg+1]) {
1809 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1810 goto error;
1811 }
1812
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001813 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1814 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001815 if (!sk) {
1816 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1817 goto error;
1818 }
1819
William Dauchy1c921cd2021-02-03 22:30:08 +01001820 srv->check.addr = *sk;
1821 /* if agentaddr was never set, we can use addr */
1822 if (!(srv->flags & SRV_F_AGENTADDR))
1823 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001824
1825 out:
1826 return err_code;
1827
1828 error:
1829 err_code |= ERR_ALERT | ERR_FATAL;
1830 goto out;
1831}
1832
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001833/* Parse the "agent-addr" server keyword */
1834static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1835 char **errmsg)
1836{
William Dauchy1c921cd2021-02-03 22:30:08 +01001837 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001838 int err_code = 0;
1839
1840 if (!*(args[*cur_arg+1])) {
1841 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1842 goto error;
1843 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001844 memset(&sk, 0, sizeof(sk));
1845 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001846 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1847 goto error;
1848 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001849 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001850
1851 out:
1852 return err_code;
1853
1854 error:
1855 err_code |= ERR_ALERT | ERR_FATAL;
1856 goto out;
1857}
1858
1859/* Parse the "agent-check" server keyword */
1860static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1861 char **errmsg)
1862{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001863 struct tcpcheck_ruleset *rs = NULL;
1864 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1865 struct tcpcheck_rule *chk;
1866 int err_code = 0;
1867
1868 if (srv->do_agent)
1869 goto out;
1870
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001871 if (!(curpx->cap & PR_CAP_BE)) {
1872 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1873 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1874 return ERR_WARN;
1875 }
1876
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001877 if (!rules) {
1878 rules = calloc(1, sizeof(*rules));
1879 if (!rules) {
1880 memprintf(errmsg, "out of memory.");
1881 goto error;
1882 }
1883 LIST_INIT(&rules->preset_vars);
1884 srv->agent.tcpcheck_rules = rules;
1885 }
1886 rules->list = NULL;
1887 rules->flags = 0;
1888
Christopher Faulet61cc8522020-04-20 14:54:42 +02001889 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001890 if (rs)
1891 goto ruleset_found;
1892
Christopher Faulet61cc8522020-04-20 14:54:42 +02001893 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001894 if (rs == NULL) {
1895 memprintf(errmsg, "out of memory.");
1896 goto error;
1897 }
1898
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001899 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001900 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1901 if (!chk) {
1902 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1903 goto error;
1904 }
1905 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001906 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001907
1908 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001909 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1910 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001911 if (!chk) {
1912 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1913 goto error;
1914 }
1915 chk->expect.custom = tcpcheck_agent_expect_reply;
1916 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001917 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001918
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001919 ruleset_found:
1920 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001921 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001922 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001923 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001924
1925 out:
Dirkjan Bussink1065e7e2021-06-18 19:57:49 +00001926 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001927
1928 error:
1929 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001930 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001931 err_code |= ERR_ALERT | ERR_FATAL;
1932 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001933}
1934
1935/* Parse the "agent-inter" server keyword */
1936static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1937 char **errmsg)
1938{
1939 const char *err = NULL;
1940 unsigned int delay;
1941 int err_code = 0;
1942
1943 if (!*(args[*cur_arg+1])) {
1944 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1945 goto error;
1946 }
1947
1948 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1949 if (err == PARSE_TIME_OVER) {
1950 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1951 args[*cur_arg+1], args[*cur_arg], srv->id);
1952 goto error;
1953 }
1954 else if (err == PARSE_TIME_UNDER) {
1955 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1956 args[*cur_arg+1], args[*cur_arg], srv->id);
1957 goto error;
1958 }
1959 else if (err) {
1960 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1961 *err, srv->id);
1962 goto error;
1963 }
1964 if (delay <= 0) {
1965 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1966 delay, args[*cur_arg], srv->id);
1967 goto error;
1968 }
1969 srv->agent.inter = delay;
1970
1971 out:
1972 return err_code;
1973
1974 error:
1975 err_code |= ERR_ALERT | ERR_FATAL;
1976 goto out;
1977}
1978
1979/* Parse the "agent-port" server keyword */
1980static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1981 char **errmsg)
1982{
1983 int err_code = 0;
1984
1985 if (!*(args[*cur_arg+1])) {
1986 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1987 goto error;
1988 }
1989
1990 global.maxsock++;
William Dauchy4858fb22021-02-03 22:30:09 +01001991 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001992
1993 out:
1994 return err_code;
1995
1996 error:
1997 err_code |= ERR_ALERT | ERR_FATAL;
1998 goto out;
1999}
2000
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002001int set_srv_agent_send(struct server *srv, const char *send)
2002{
2003 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
2004 struct tcpcheck_var *var = NULL;
2005 char *str;
2006
2007 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02002008 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002009 if (str == NULL || var == NULL)
2010 goto error;
2011
2012 free_tcpcheck_vars(&rules->preset_vars);
2013
2014 var->data.type = SMP_T_STR;
2015 var->data.u.str.area = str;
2016 var->data.u.str.data = strlen(str);
2017 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02002018 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002019
2020 return 1;
2021
2022 error:
2023 free(str);
2024 free(var);
2025 return 0;
2026}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002027
William Dauchyf4300902021-02-06 20:47:50 +01002028/* set agent addr and appropriate flag */
William Dauchy1c921cd2021-02-03 22:30:08 +01002029inline void set_srv_agent_addr(struct server *srv, struct sockaddr_storage *sk)
2030{
2031 srv->agent.addr = *sk;
2032 srv->flags |= SRV_F_AGENTADDR;
2033}
2034
William Dauchyf4300902021-02-06 20:47:50 +01002035/* set agent port and appropriate flag */
William Dauchy4858fb22021-02-03 22:30:09 +01002036inline void set_srv_agent_port(struct server *srv, int port)
2037{
2038 srv->agent.port = port;
2039 srv->flags |= SRV_F_AGENTPORT;
2040}
2041
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002042/* Parse the "agent-send" server keyword */
2043static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2044 char **errmsg)
2045{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002046 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002047 int err_code = 0;
2048
2049 if (!*(args[*cur_arg+1])) {
2050 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2051 goto error;
2052 }
2053
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002054 if (!rules) {
2055 rules = calloc(1, sizeof(*rules));
2056 if (!rules) {
2057 memprintf(errmsg, "out of memory.");
2058 goto error;
2059 }
2060 LIST_INIT(&rules->preset_vars);
2061 srv->agent.tcpcheck_rules = rules;
2062 }
2063
2064 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002065 memprintf(errmsg, "out of memory.");
2066 goto error;
2067 }
2068
2069 out:
2070 return err_code;
2071
2072 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002073 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002074 err_code |= ERR_ALERT | ERR_FATAL;
2075 goto out;
2076}
2077
2078/* Parse the "no-agent-send" server keyword */
2079static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2080 char **errmsg)
2081{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002082 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002083 return 0;
2084}
2085
Christopher Fauletce8111e2020-04-06 15:04:11 +02002086/* Parse the "check" server keyword */
2087static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2088 char **errmsg)
2089{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002090 if (!(curpx->cap & PR_CAP_BE)) {
2091 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2092 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2093 return ERR_WARN;
2094 }
2095
Christopher Fauletce8111e2020-04-06 15:04:11 +02002096 srv->do_check = 1;
2097 return 0;
2098}
2099
2100/* Parse the "check-send-proxy" server keyword */
2101static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2102 char **errmsg)
2103{
2104 srv->check.send_proxy = 1;
2105 return 0;
2106}
2107
2108/* Parse the "check-via-socks4" server keyword */
2109static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2110 char **errmsg)
2111{
2112 srv->check.via_socks4 = 1;
2113 return 0;
2114}
2115
2116/* Parse the "no-check" server keyword */
2117static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2118 char **errmsg)
2119{
2120 deinit_srv_check(srv);
2121 return 0;
2122}
2123
2124/* Parse the "no-check-send-proxy" server keyword */
2125static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2126 char **errmsg)
2127{
2128 srv->check.send_proxy = 0;
2129 return 0;
2130}
2131
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002132/* parse the "check-proto" server keyword */
2133static int srv_parse_check_proto(char **args, int *cur_arg,
2134 struct proxy *px, struct server *newsrv, char **err)
2135{
2136 int err_code = 0;
2137
2138 if (!*args[*cur_arg + 1]) {
2139 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2140 goto error;
2141 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002142 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002143 if (!newsrv->check.mux_proto) {
2144 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2145 goto error;
2146 }
2147
2148 out:
2149 return err_code;
2150
2151 error:
2152 err_code |= ERR_ALERT | ERR_FATAL;
2153 goto out;
2154}
2155
2156
Christopher Fauletce8111e2020-04-06 15:04:11 +02002157/* Parse the "rise" server keyword */
2158static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2159 char **errmsg)
2160{
2161 int err_code = 0;
2162
2163 if (!*args[*cur_arg + 1]) {
2164 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2165 goto error;
2166 }
2167
2168 srv->check.rise = atol(args[*cur_arg+1]);
2169 if (srv->check.rise <= 0) {
2170 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2171 goto error;
2172 }
2173
2174 if (srv->check.health)
2175 srv->check.health = srv->check.rise;
2176
2177 out:
2178 return err_code;
2179
2180 error:
2181 deinit_srv_agent_check(srv);
2182 err_code |= ERR_ALERT | ERR_FATAL;
2183 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002184}
2185
2186/* Parse the "fall" server keyword */
2187static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2188 char **errmsg)
2189{
2190 int err_code = 0;
2191
2192 if (!*args[*cur_arg + 1]) {
2193 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2194 goto error;
2195 }
2196
2197 srv->check.fall = atol(args[*cur_arg+1]);
2198 if (srv->check.fall <= 0) {
2199 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2200 goto error;
2201 }
2202
2203 out:
2204 return err_code;
2205
2206 error:
2207 deinit_srv_agent_check(srv);
2208 err_code |= ERR_ALERT | ERR_FATAL;
2209 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002210}
2211
2212/* Parse the "inter" server keyword */
2213static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2214 char **errmsg)
2215{
2216 const char *err = NULL;
2217 unsigned int delay;
2218 int err_code = 0;
2219
2220 if (!*(args[*cur_arg+1])) {
2221 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2222 goto error;
2223 }
2224
2225 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2226 if (err == PARSE_TIME_OVER) {
2227 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2228 args[*cur_arg+1], args[*cur_arg], srv->id);
2229 goto error;
2230 }
2231 else if (err == PARSE_TIME_UNDER) {
2232 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2233 args[*cur_arg+1], args[*cur_arg], srv->id);
2234 goto error;
2235 }
2236 else if (err) {
2237 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2238 *err, srv->id);
2239 goto error;
2240 }
2241 if (delay <= 0) {
2242 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2243 delay, args[*cur_arg], srv->id);
2244 goto error;
2245 }
2246 srv->check.inter = delay;
2247
2248 out:
2249 return err_code;
2250
2251 error:
2252 err_code |= ERR_ALERT | ERR_FATAL;
2253 goto out;
2254}
2255
2256
2257/* Parse the "fastinter" server keyword */
2258static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2259 char **errmsg)
2260{
2261 const char *err = NULL;
2262 unsigned int delay;
2263 int err_code = 0;
2264
2265 if (!*(args[*cur_arg+1])) {
2266 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2267 goto error;
2268 }
2269
2270 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2271 if (err == PARSE_TIME_OVER) {
2272 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2273 args[*cur_arg+1], args[*cur_arg], srv->id);
2274 goto error;
2275 }
2276 else if (err == PARSE_TIME_UNDER) {
2277 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2278 args[*cur_arg+1], args[*cur_arg], srv->id);
2279 goto error;
2280 }
2281 else if (err) {
2282 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2283 *err, srv->id);
2284 goto error;
2285 }
2286 if (delay <= 0) {
2287 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2288 delay, args[*cur_arg], srv->id);
2289 goto error;
2290 }
2291 srv->check.fastinter = delay;
2292
2293 out:
2294 return err_code;
2295
2296 error:
2297 err_code |= ERR_ALERT | ERR_FATAL;
2298 goto out;
2299}
2300
2301
2302/* Parse the "downinter" server keyword */
2303static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2304 char **errmsg)
2305{
2306 const char *err = NULL;
2307 unsigned int delay;
2308 int err_code = 0;
2309
2310 if (!*(args[*cur_arg+1])) {
2311 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2312 goto error;
2313 }
2314
2315 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2316 if (err == PARSE_TIME_OVER) {
2317 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2318 args[*cur_arg+1], args[*cur_arg], srv->id);
2319 goto error;
2320 }
2321 else if (err == PARSE_TIME_UNDER) {
2322 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2323 args[*cur_arg+1], args[*cur_arg], srv->id);
2324 goto error;
2325 }
2326 else if (err) {
2327 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2328 *err, srv->id);
2329 goto error;
2330 }
2331 if (delay <= 0) {
2332 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2333 delay, args[*cur_arg], srv->id);
2334 goto error;
2335 }
2336 srv->check.downinter = delay;
2337
2338 out:
2339 return err_code;
2340
2341 error:
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344}
2345
2346/* Parse the "port" server keyword */
2347static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2348 char **errmsg)
2349{
2350 int err_code = 0;
2351
2352 if (!*(args[*cur_arg+1])) {
2353 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2354 goto error;
2355 }
2356
2357 global.maxsock++;
2358 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002359 /* if agentport was never set, we can use port */
2360 if (!(srv->flags & SRV_F_AGENTPORT))
2361 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002362
2363 out:
2364 return err_code;
2365
2366 error:
2367 err_code |= ERR_ALERT | ERR_FATAL;
2368 goto out;
2369}
2370
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002371static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002372 { "addr", srv_parse_addr, 1, 1, 0 }, /* IP address to send health to or to probe from agent-check */
2373 { "agent-addr", srv_parse_agent_addr, 1, 1, 0 }, /* Enable an auxiliary agent check */
2374 { "agent-check", srv_parse_agent_check, 0, 1, 0 }, /* Enable agent checks */
2375 { "agent-inter", srv_parse_agent_inter, 1, 1, 0 }, /* Set the interval between two agent checks */
2376 { "agent-port", srv_parse_agent_port, 1, 1, 0 }, /* Set the TCP port used for agent checks. */
2377 { "agent-send", srv_parse_agent_send, 1, 1, 0 }, /* Set string to send to agent. */
2378 { "check", srv_parse_check, 0, 1, 0 }, /* Enable health checks */
2379 { "check-proto", srv_parse_check_proto, 1, 1, 0 }, /* Set the mux protocol for health checks */
2380 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 0 }, /* Enable PROXY protocol for health checks */
2381 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 0 }, /* Enable socks4 proxy for health checks */
2382 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2383 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2384 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1, 0 }, /* Disable PROXY protocol for health checks */
2385 { "rise", srv_parse_check_rise, 1, 1, 0 }, /* Set rise value for health checks */
2386 { "fall", srv_parse_check_fall, 1, 1, 0 }, /* Set fall value for health checks */
2387 { "inter", srv_parse_check_inter, 1, 1, 0 }, /* Set inter value for health checks */
2388 { "fastinter", srv_parse_check_fastinter, 1, 1, 0 }, /* Set fastinter value for health checks */
2389 { "downinter", srv_parse_check_downinter, 1, 1, 0 }, /* Set downinter value for health checks */
2390 { "port", srv_parse_check_port, 1, 1, 0 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002391 { NULL, NULL, 0 },
2392}};
2393
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002394INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002395
Willy Tarreaubd741542010-03-16 18:46:54 +01002396/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002397 * Local variables:
2398 * c-indent-level: 8
2399 * c-basic-offset: 8
2400 * End:
2401 */