blob: f0793fe49d26498baab615422abec94d568d07fa [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
201 chunk_appendf(&trace_buf, " : [%c] SRV=%s",
202 ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
203 srv->id);
204
205 chunk_appendf(&trace_buf, " status=%d/%d %s",
206 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
207 (check->health >= check->rise) ? check->fall : check->rise,
208 (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN");
209
210 switch (check->result) {
211 case CHK_RES_NEUTRAL: res = "-"; break;
212 case CHK_RES_FAILED: res = "FAIL"; break;
213 case CHK_RES_PASSED: res = "PASS"; break;
214 case CHK_RES_CONDPASS: res = "COND"; break;
215 default: res = "UNK"; break;
216 }
217
218 if (src->verbosity == CHK_VERB_CLEAN)
219 return;
220
221 chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
222 get_check_status_info(check->status), check->status,
223 res, check->result);
224
225 /* Display the value to the 4th argument (level > STATE) */
226 if (src->level > TRACE_LEVEL_STATE && val)
227 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
228
229 chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
230
231 if (src->verbosity == CHK_VERB_MINIMAL)
232 return;
233
234
235 if (check->cs) {
Christopher Faulet0256da12021-12-15 09:50:17 +0100236 struct connection *conn = cs_conn(check->cs);
237
238 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200239 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags);
240 }
241
242 if (mask & CHK_EV_TCPCHK) {
243 const char *type;
244
245 switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) {
246 case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break;
247 case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break;
248 case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break;
249 case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break;
250 case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break;
251 case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break;
252 case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break;
253 case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break;
254 case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break;
255 case TCPCHK_RULES_TCP_CHK: type = "TCP"; break;
256 default: type = "???"; break;
257 }
258 if (check->current_step)
259 chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
260 else
261 chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
262 }
263
264 /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
265 if (src->level > TRACE_LEVEL_USER) {
266 const struct buffer *buf = NULL;
267
268 chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
269 (unsigned int)b_data(&check->bi), b_orig(&check->bi),
270 (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
271 chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
272 (unsigned int)b_data(&check->bo), b_orig(&check->bo),
273 (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
274
275 if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
276 buf = (b_is_null(&check->bi) ? NULL : &check->bi);
277 else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
278 buf = (b_is_null(&check->bo) ? NULL : &check->bo);
279
280 if (buf) {
281 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
282 int full = (src->verbosity == CHK_VERB_COMPLETE);
283
284 chunk_memcat(&trace_buf, "\n\t", 2);
285 htx_dump(&trace_buf, htxbuf(buf), full);
286 }
287 else {
288 int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
289
290 chunk_memcat(&trace_buf, "\n", 1);
291 if (b_data(buf) > max) {
292 check_trace_buf(buf, 0, max);
293 chunk_memcat(&trace_buf, " ...\n", 6);
294 }
295 else
296 check_trace_buf(buf, 0, b_data(buf));
297 }
298
299 }
300 }
301
302}
303
304
Christopher Faulet61cc8522020-04-20 14:54:42 +0200305/**************************************************************************/
306/************************ Handle check results ****************************/
307/**************************************************************************/
308struct check_status {
309 short result; /* one of SRV_CHK_* */
310 char *info; /* human readable short info */
311 char *desc; /* long description */
312};
313
314struct analyze_status {
315 char *desc; /* description */
316 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
317};
318
Simon Horman63a4a822012-03-19 07:24:41 +0900319static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100320 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
321 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200322 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200323
Willy Tarreau23964182014-05-20 20:56:30 +0200324 /* Below we have finished checks */
325 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100326 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100327
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100328 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200329
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100330 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
331 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
332 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200333
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100334 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
335 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
336 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200337
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100338 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
339 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200340
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200341 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200342
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100343 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
344 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
345 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900346
347 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
348 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200349 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200350};
351
Simon Horman63a4a822012-03-19 07:24:41 +0900352static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100353 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
354
355 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
356 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
357
358 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
359 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
360 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
361 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
362
363 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
364 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
365 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
366};
367
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100368/* checks if <err> is a real error for errno or one that can be ignored, and
369 * return 0 for these ones or <err> for real ones.
370 */
371static inline int unclean_errno(int err)
372{
373 if (err == EAGAIN || err == EINPROGRESS ||
374 err == EISCONN || err == EALREADY)
375 return 0;
376 return err;
377}
378
Christopher Faulet7aa32712021-02-01 13:11:50 +0100379/* Converts check_status code to result code */
380short get_check_status_result(short check_status)
381{
382 if (check_status < HCHK_STATUS_SIZE)
383 return check_statuses[check_status].result;
384 else
385 return check_statuses[HCHK_STATUS_UNKNOWN].result;
386}
387
Christopher Faulet61cc8522020-04-20 14:54:42 +0200388/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200389const char *get_check_status_description(short check_status) {
390
391 const char *desc;
392
393 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200394 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200395 else
396 desc = NULL;
397
398 if (desc && *desc)
399 return desc;
400 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200401 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200402}
403
Christopher Faulet61cc8522020-04-20 14:54:42 +0200404/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100405const char *get_check_status_info(short check_status)
406{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200407 const char *info;
408
409 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200410 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200411 else
412 info = NULL;
413
414 if (info && *info)
415 return info;
416 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200417 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200418}
419
Christopher Faulet61cc8522020-04-20 14:54:42 +0200420/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100421const char *get_analyze_status(short analyze_status) {
422
423 const char *desc;
424
425 if (analyze_status < HANA_STATUS_SIZE)
426 desc = analyze_statuses[analyze_status].desc;
427 else
428 desc = NULL;
429
430 if (desc && *desc)
431 return desc;
432 else
433 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
434}
435
Christopher Faulet61cc8522020-04-20 14:54:42 +0200436/* Sets check->status, update check->duration and fill check->result with an
437 * adequate CHK_RES_* value. The new check->health is computed based on the
438 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200439 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200440 * Shows information in logs about failed health check if server is UP or
441 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200442 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200443void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100444{
Simon Horman4a741432013-02-23 15:35:38 +0900445 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200446 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200447 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900448
Christopher Faulet147b8c92021-04-10 09:00:38 +0200449 TRACE_POINT(CHK_EV_HCHK_RUN, check);
450
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200451 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100452 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900453 check->desc[0] = '\0';
454 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200455 return;
456 }
457
Simon Horman4a741432013-02-23 15:35:38 +0900458 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200459 return;
460
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200461 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900462 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
463 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200464 } else
Simon Horman4a741432013-02-23 15:35:38 +0900465 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200466
Simon Horman4a741432013-02-23 15:35:38 +0900467 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200468 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900469 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200470
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100471 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900472 check->duration = -1;
473 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200474 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900475 check->duration = tv_ms_elapsed(&check->start, &now);
476 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200477 }
478
Willy Tarreau23964182014-05-20 20:56:30 +0200479 /* no change is expected if no state change occurred */
480 if (check->result == CHK_RES_NEUTRAL)
481 return;
482
Olivier Houchard0923fa42019-01-11 18:43:04 +0100483 /* If the check was really just sending a mail, it won't have an
484 * associated server, so we're done now.
485 */
486 if (!s)
487 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200488 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200489
Christopher Faulet147b8c92021-04-10 09:00:38 +0200490
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200491 switch (check->result) {
492 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200493 /* Failure to connect to the agent as a secondary check should not
494 * cause the server to be marked down.
495 */
496 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900497 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200498 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200499 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200500 report = 1;
501 check->health--;
502 if (check->health < check->rise)
503 check->health = 0;
504 }
505 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200506
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200507 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100508 case CHK_RES_CONDPASS:
509 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200510 report = 1;
511 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200512
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200513 if (check->health >= check->rise)
514 check->health = check->rise + check->fall - 1; /* OK now */
515 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200516
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200517 /* clear consecutive_errors if observing is enabled */
518 if (s->onerror)
519 s->consecutive_errors = 0;
520 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100521
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200522 default:
523 break;
524 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200525
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200526 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
527 (status != prev_status || report)) {
528 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200529 "%s check for %sserver %s/%s %s%s",
530 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200531 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100532 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100533 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200534 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200535
Emeric Brun5a133512017-10-19 14:42:30 +0200536 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200537
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100538 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200539 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
540 (check->health >= check->rise) ? check->fall : check->rise,
541 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200542
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200543 ha_warning("%s.\n", trash.area);
544 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
545 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200546 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200547}
548
Willy Tarreau4eec5472014-05-20 22:32:27 +0200549/* Marks the check <check>'s server down if the current check is already failed
550 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200552void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553{
Simon Horman4a741432013-02-23 15:35:38 +0900554 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900555
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200556 /* The agent secondary check should only cause a server to be marked
557 * as down if check->status is HCHK_STATUS_L7STS, which indicates
558 * that the agent returned "fail", "stopped" or "down".
559 * The implication here is that failure to connect to the agent
560 * as a secondary check should not cause the server to be marked
561 * down. */
562 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
563 return;
564
Willy Tarreau4eec5472014-05-20 22:32:27 +0200565 if (check->health > 0)
566 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100567
Christopher Faulet147b8c92021-04-10 09:00:38 +0200568 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200569 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200570 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571}
572
Willy Tarreauaf549582014-05-16 17:37:50 +0200573/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200574 * it isn't in maintenance, it is not tracking a down server and other checks
575 * comply. The rule is simple : by default, a server is up, unless any of the
576 * following conditions is true :
577 * - health check failed (check->health < rise)
578 * - agent check failed (agent->health < rise)
579 * - the server tracks a down server (track && track->state == STOPPED)
580 * Note that if the server has a slowstart, it will switch to STARTING instead
581 * of RUNNING. Also, only the health checks support the nolb mode, so the
582 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200583 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200584void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200585{
Simon Horman4a741432013-02-23 15:35:38 +0900586 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100587
Emeric Brun52a91d32017-08-31 14:41:55 +0200588 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200589 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100590
Emeric Brun52a91d32017-08-31 14:41:55 +0200591 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200592 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100593
Willy Tarreau3e048382014-05-21 10:30:54 +0200594 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
595 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100596
Willy Tarreau3e048382014-05-21 10:30:54 +0200597 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
598 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200599
Emeric Brun52a91d32017-08-31 14:41:55 +0200600 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200601 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100602
Christopher Faulet147b8c92021-04-10 09:00:38 +0200603 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Emeric Brun5a133512017-10-19 14:42:30 +0200604 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100605}
606
Willy Tarreaudb58b792014-05-21 13:57:23 +0200607/* Marks the check <check> as valid and tries to set its server into stopping mode
608 * if it was running or starting, and provided it isn't in maintenance and other
609 * checks comply. The conditions for the server to be marked in stopping mode are
610 * the same as for it to be turned up. Also, only the health checks support the
611 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200612 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200613void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200614{
Simon Horman4a741432013-02-23 15:35:38 +0900615 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100616
Emeric Brun52a91d32017-08-31 14:41:55 +0200617 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200618 return;
619
Willy Tarreaudb58b792014-05-21 13:57:23 +0200620 if (check->state & CHK_ST_AGENT)
621 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100622
Emeric Brun52a91d32017-08-31 14:41:55 +0200623 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200624 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100625
Willy Tarreaudb58b792014-05-21 13:57:23 +0200626 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
627 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100628
Willy Tarreaudb58b792014-05-21 13:57:23 +0200629 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
630 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100631
Christopher Faulet147b8c92021-04-10 09:00:38 +0200632 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 +0100633 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100634}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100636/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100637 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100638 */
639void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100640{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100641 int failed;
642 int expire;
643
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100644 if (s->observe >= HANA_OBS_SIZE)
645 return;
646
Willy Tarreaubb956662013-01-24 00:37:39 +0100647 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100648 return;
649
650 switch (analyze_statuses[status].lr[s->observe - 1]) {
651 case 1:
652 failed = 1;
653 break;
654
655 case 2:
656 failed = 0;
657 break;
658
659 default:
660 return;
661 }
662
663 if (!failed) {
664 /* good: clear consecutive_errors */
665 s->consecutive_errors = 0;
666 return;
667 }
668
Willy Tarreau4781b152021-04-06 13:53:36 +0200669 _HA_ATOMIC_INC(&s->consecutive_errors);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100670
671 if (s->consecutive_errors < s->consecutive_errors_limit)
672 return;
673
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100674 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
675 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100676
Willy Tarreau4e9df272021-02-17 15:20:19 +0100677 if (s->check.fastinter)
678 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
679 else
680 expire = TICK_ETERNITY;
681
682 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
683
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100684 switch (s->onerror) {
685 case HANA_ONERR_FASTINTER:
686 /* force fastinter - nothing to do here as all modes force it */
687 break;
688
689 case HANA_ONERR_SUDDTH:
690 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900691 if (s->check.health > s->check.rise)
692 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100693
Tim Duesterhus588b3142020-05-29 14:35:51 +0200694 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100695
696 case HANA_ONERR_FAILCHK:
697 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200698 set_server_check_status(&s->check, HCHK_STATUS_HANA,
699 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200700 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100701 break;
702
703 case HANA_ONERR_MARKDWN:
704 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900705 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200706 set_server_check_status(&s->check, HCHK_STATUS_HANA,
707 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200708 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100709 break;
710
711 default:
712 /* write a warning? */
713 break;
714 }
715
Willy Tarreau4e9df272021-02-17 15:20:19 +0100716 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
717
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100718 s->consecutive_errors = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200719 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100720
Christopher Fauletea860832021-05-07 11:45:26 +0200721 if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) {
Willy Tarreau4e9df272021-02-17 15:20:19 +0100722 /* requeue check task with new expire */
723 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100724 }
Willy Tarreauef781042010-01-27 11:53:01 +0100725}
726
Christopher Faulet61cc8522020-04-20 14:54:42 +0200727/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100728 * closed, keep errno intact as it is supposed to contain the valid error code.
729 * If no error is reported, check the socket's error queue using getsockopt().
730 * Warning, this must be done only once when returning from poll, and never
731 * after an I/O error was attempted, otherwise the error queue might contain
732 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
733 * socket. Returns non-zero if an error was reported, zero if everything is
734 * clean (including a properly closed socket).
735 */
736static int retrieve_errno_from_socket(struct connection *conn)
737{
738 int skerr;
739 socklen_t lskerr = sizeof(skerr);
740
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100741 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100742 return 1;
743
Willy Tarreau3c728722014-01-23 13:50:42 +0100744 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100745 return 0;
746
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200747 BUG_ON(conn->flags & CO_FL_FDLESS);
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) &&
Christopher Fauletb041b232022-03-24 10:27:02 +0100798 cs && !(cs->endp->flags & CS_EP_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 Fauletb61caf42020-04-21 10:57:42 +0200835 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100836 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200837 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200838 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100839 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200840 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100841 chunk_appendf(chk, " (expect binary regex)");
842 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200843 case TCPCHK_EXPECT_STRING_LF:
844 chunk_appendf(chk, " (expect log-format string)");
845 break;
846 case TCPCHK_EXPECT_BINARY_LF:
847 chunk_appendf(chk, " (expect log-format binary)");
848 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200849 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200850 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200851 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200852 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200853 chunk_appendf(chk, " (expect HTTP status regex)");
854 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200855 case TCPCHK_EXPECT_HTTP_HEADER:
856 chunk_appendf(chk, " (expect HTTP header pattern)");
857 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200858 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200859 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200860 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200861 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200862 chunk_appendf(chk, " (expect HTTP body regex)");
863 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200864 case TCPCHK_EXPECT_HTTP_BODY_LF:
865 chunk_appendf(chk, " (expect log-format HTTP body)");
866 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200867 case TCPCHK_EXPECT_CUSTOM:
868 chunk_appendf(chk, " (expect custom function)");
869 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100870 case TCPCHK_EXPECT_UNDEF:
871 chunk_appendf(chk, " (undefined expect!)");
872 break;
873 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200874 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200875 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200876 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200877 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200878 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200879 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200880
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200881 if (check->current_step && check->current_step->comment)
882 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200883 }
884 }
885
Willy Tarreau00149122017-10-04 18:05:01 +0200886 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100887 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200888 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
889 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100890 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200891 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
892 chk->area);
893 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100894 }
895 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100896 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200897 chunk_printf(&trash, "%s%s", strerror(errno),
898 chk->area);
899 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100900 }
901 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200902 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100903 }
904 }
905
Willy Tarreau00149122017-10-04 18:05:01 +0200906 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200907 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200908 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
909 }
910
Christopher Faulet0256da12021-12-15 09:50:17 +0100911 if (!cs || !conn || !conn->ctrl) {
Christopher Faulet5e293762020-10-26 11:10:49 +0100912 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200913 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
914 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100915 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100916 /* L4 not established (yet) */
Christopher Fauletb041b232022-03-24 10:27:02 +0100917 if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100918 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
919 else if (expired)
920 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200921
922 /*
923 * might be due to a server IP change.
924 * Let's trigger a DNS resolution if none are currently running.
925 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100926 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100927 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200928
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100929 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100930 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100931 /* L6 not established (yet) */
Christopher Fauletb041b232022-03-24 10:27:02 +0100932 if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100933 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
934 else if (expired)
935 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
936 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100937 else if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100938 /* I/O error after connection was established and before we could diagnose */
939 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
940 }
941 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200942 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
943
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100944 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200945 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
946 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200947 tout = check->current_step->expect.tout_status;
948 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100949 }
950
Christopher Faulet147b8c92021-04-10 09:00:38 +0200951 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100952 return;
953}
954
Simon Horman98637e52014-06-20 12:30:16 +0900955
Christopher Faulet61cc8522020-04-20 14:54:42 +0200956/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200957int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900958{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200959 int sv_state;
960 int ratio;
961 char addr[46];
962 char port[6];
963 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
964 "UP %d/%d", "UP",
965 "NOLB %d/%d", "NOLB",
966 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900967
Christopher Faulet61cc8522020-04-20 14:54:42 +0200968 if (!(s->check.state & CHK_ST_ENABLED))
969 sv_state = 6;
970 else if (s->cur_state != SRV_ST_STOPPED) {
971 if (s->check.health == s->check.rise + s->check.fall - 1)
972 sv_state = 3; /* UP */
973 else
974 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900975
Christopher Faulet61cc8522020-04-20 14:54:42 +0200976 if (s->cur_state == SRV_ST_STOPPING)
977 sv_state += 2;
978 } else {
979 if (s->check.health)
980 sv_state = 1; /* going up */
981 else
982 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900983 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200984
Christopher Faulet61cc8522020-04-20 14:54:42 +0200985 chunk_appendf(buf, srv_hlt_st[sv_state],
986 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
987 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200988
Christopher Faulet61cc8522020-04-20 14:54:42 +0200989 addr_to_str(&s->addr, addr, sizeof(addr));
990 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
991 snprintf(port, sizeof(port), "%u", s->svc_port);
992 else
993 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200994
Christopher Faulet61cc8522020-04-20 14:54:42 +0200995 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
996 addr, port, s->proxy->id, s->id,
997 global.node,
998 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
999 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02001000 s->cur_sess, s->proxy->beconn - s->proxy->queue.length,
Willy Tarreaua0570452021-06-18 09:30:30 +02001001 s->queue.length);
Willy Tarreau9f6dc722019-03-01 11:15:10 +01001002
Christopher Faulet61cc8522020-04-20 14:54:42 +02001003 if ((s->cur_state == SRV_ST_STARTING) &&
1004 now.tv_sec < s->last_change + s->slowstart &&
1005 now.tv_sec >= s->last_change) {
1006 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
1007 chunk_appendf(buf, "; throttle=%d%%", ratio);
1008 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001009
Christopher Faulet61cc8522020-04-20 14:54:42 +02001010 return b_data(buf);
1011}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001012
Willy Tarreau51cd5952020-06-05 12:25:38 +02001013/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001014/***************** Health-checks based on connections *********************/
1015/**************************************************************************/
1016/* This function is used only for server health-checks. It handles connection
1017 * status updates including errors. If necessary, it wakes the check task up.
1018 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1019 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001020 */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001021static int wake_srv_chk(struct conn_stream *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001022{
Christopher Faulet0256da12021-12-15 09:50:17 +01001023 struct connection *conn;
Christopher Faulet693b23b2022-02-28 09:09:05 +01001024 struct check *check = __cs_check(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001025 struct email_alertq *q = container_of(check, typeof(*q), check);
1026 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001027
Christopher Faulet147b8c92021-04-10 09:00:38 +02001028 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001029 if (check->server)
1030 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1031 else
1032 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001033
Willy Tarreau51cd5952020-06-05 12:25:38 +02001034 /* we may have to make progress on the TCP checks */
1035 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001036
Willy Tarreau51cd5952020-06-05 12:25:38 +02001037 cs = check->cs;
Christopher Faulet0256da12021-12-15 09:50:17 +01001038 conn = cs_conn(cs);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001039
Christopher Fauletb041b232022-03-24 10:27:02 +01001040 if (unlikely(!conn || !cs || conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR)) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001041 /* We may get error reports bypassing the I/O handlers, typically
1042 * the case when sending a pure TCP check which fails, then the I/O
1043 * handlers above are not called. This is completely handled by the
1044 * main processing task so let's simply wake it up. If we get here,
1045 * we expect errno to still be valid.
1046 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001047 TRACE_ERROR("report connection error", CHK_EV_HCHK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001048 chk_report_conn_err(check, errno, 0);
1049 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001050 }
1051
Christopher Faulet8f100422021-01-18 15:47:03 +01001052 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001053 /* Check complete or aborted. If connection not yet closed do it
1054 * now and wake the check task up to be sure the result is
1055 * handled ASAP. */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001056 ret = -1;
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001057 if (conn) {
1058 cs_conn_drain_and_close(cs);
1059 if (check->wait_list.events)
1060 conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
1061 }
Christopher Faulet8f100422021-01-18 15:47:03 +01001062
Willy Tarreau51cd5952020-06-05 12:25:38 +02001063 /* We may have been scheduled to run, and the
1064 * I/O handler expects to have a cs, so remove
1065 * the tasklet
1066 */
1067 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
1068 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001069 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001070
Willy Tarreau51cd5952020-06-05 12:25:38 +02001071 if (check->server)
1072 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1073 else
1074 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001075
Christopher Faulet147b8c92021-04-10 09:00:38 +02001076 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001077 return ret;
1078}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001079
Willy Tarreau51cd5952020-06-05 12:25:38 +02001080/* This function checks if any I/O is wanted, and if so, attempts to do so */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001081struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001082{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001083 struct check *check = ctx;
1084 struct conn_stream *cs = check->cs;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001085
Willy Tarreau51cd5952020-06-05 12:25:38 +02001086 wake_srv_chk(cs);
1087 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001088}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001089
Willy Tarreau51cd5952020-06-05 12:25:38 +02001090/* manages a server health-check that uses a connection. Returns
1091 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001092 *
1093 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001094 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001095 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001096struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001097{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001098 struct check *check = context;
1099 struct proxy *proxy = check->proxy;
Christopher Faulet92017a32021-05-06 16:01:18 +02001100 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001101 int rv;
1102 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001103
Christopher Faulet147b8c92021-04-10 09:00:38 +02001104 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1105
Willy Tarreau51cd5952020-06-05 12:25:38 +02001106 if (check->server)
1107 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001108
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001109 if (unlikely(check->state & CHK_ST_PURGE)) {
1110 TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1111 }
1112 else if (!(check->state & (CHK_ST_INPROGRESS))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001113 /* no check currently running */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001114 if (!expired) /* woke up too early */ {
1115 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001116 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001117 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001118
Willy Tarreau51cd5952020-06-05 12:25:38 +02001119 /* we don't send any health-checks when the proxy is
1120 * stopped, the server should not be checked or the check
1121 * is disabled.
1122 */
1123 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001124 (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001125 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001126 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001127 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001128
Willy Tarreau51cd5952020-06-05 12:25:38 +02001129 /* we'll initiate a new check */
1130 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001131
Willy Tarreau51cd5952020-06-05 12:25:38 +02001132 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001133 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001134
Willy Tarreau51cd5952020-06-05 12:25:38 +02001135 task_set_affinity(t, tid_bit);
1136
1137 check->current_step = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001138
Christopher Fauletb041b232022-03-24 10:27:02 +01001139 if (!check->cs->endp) {
1140 /* CS endpoint may be NULL if a previous reset
1141 * failed. Try to allocate a new one and report a
1142 * SOCKERR if it fails.
1143 */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001144 check->cs->endp = cs_endpoint_new();
Christopher Fauletb041b232022-03-24 10:27:02 +01001145 if (!check->cs->endp) {
1146 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1147 goto end;
1148 }
1149 check->cs->endp->flags |= CS_EP_DETACHED;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001150 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001151 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001152 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001153 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001154
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001155 conn = cs_conn(check->cs);
Christopher Faulet92017a32021-05-06 16:01:18 +02001156
1157 /* there was a test running.
1158 * First, let's check whether there was an uncaught error,
1159 * which can happen on connect timeout or error.
1160 */
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001161 if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001162 /* Here the connection must be defined. Otherwise the
1163 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001164 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001165 if ((conn && ((conn->flags & CO_FL_ERROR) || (check->cs->endp->flags & CS_EP_ERROR))) || expired) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001166 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1167 chk_report_conn_err(check, 0, expired);
1168 }
1169 else {
1170 if (check->state & CHK_ST_CLOSE_CONN) {
1171 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001172 check->state &= ~CHK_ST_CLOSE_CONN;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001173 conn = NULL;
Christopher Fauletb041b232022-03-24 10:27:02 +01001174 cs_reset_endp(check->cs); /* error will be handled by tcpcheck_main() */
Christopher Faulet92017a32021-05-06 16:01:18 +02001175 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001176 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001177 if (check->result == CHK_RES_UNKNOWN) {
1178 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1179 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001180 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001181 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001182 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001183
Christopher Faulet92017a32021-05-06 16:01:18 +02001184 /* check complete or aborted */
1185 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001186
Christopher Faulet92017a32021-05-06 16:01:18 +02001187 check->current_step = NULL;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001188 conn = cs_conn(check->cs);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001189
Christopher Faulet92017a32021-05-06 16:01:18 +02001190 if (conn && conn->xprt) {
1191 /* The check was aborted and the connection was not yet closed.
1192 * This can happen upon timeout, or when an external event such
1193 * as a failed response coupled with "observe layer7" caused the
1194 * server state to be suddenly changed.
1195 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001196 cs_conn_drain_and_close(check->cs);
Christopher Faulet92017a32021-05-06 16:01:18 +02001197 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001198
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001199 /* TODO: must be handled by cs_detach_endp */
1200 if (conn && check->wait_list.events)
1201 conn->mux->unsubscribe(check->cs, check->wait_list.events, &check->wait_list);
1202 /* We may have been scheduled to run, and the
1203 * I/O handler expects to have a cs, so remove
1204 * the tasklet
1205 */
1206 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001207
Christopher Fauletb041b232022-03-24 10:27:02 +01001208 /* Force detach on error. the endpoint will be recreated on the next start */
1209 if (cs_reset_endp(check->cs) < 0)
1210 cs_detach_endp(check->cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001211
Christopher Faulet92017a32021-05-06 16:01:18 +02001212 if (check->sess != NULL) {
1213 vars_prune(&check->vars, check->sess, NULL);
1214 session_free(check->sess);
1215 check->sess = NULL;
1216 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001217
Christopher Fauletb041b232022-03-24 10:27:02 +01001218 end:
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001219 if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001220 if (check->result == CHK_RES_FAILED) {
1221 /* a failure or timeout detected */
1222 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1223 check_notify_failure(check);
1224 }
1225 else if (check->result == CHK_RES_CONDPASS) {
1226 /* check is OK but asks for stopping mode */
1227 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1228 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001229 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001230 else if (check->result == CHK_RES_PASSED) {
1231 /* a success was detected */
1232 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1233 check_notify_success(check);
1234 }
1235 }
1236 task_set_affinity(t, MAX_THREADS_MASK);
1237 check_release_buf(check, &check->bi);
1238 check_release_buf(check, &check->bo);
1239 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001240
Christopher Faulet92017a32021-05-06 16:01:18 +02001241 if (check->server) {
1242 rv = 0;
1243 if (global.spread_checks > 0) {
1244 rv = srv_getinter(check) * global.spread_checks / 100;
1245 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001246 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001247 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001248 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001249
1250 reschedule:
1251 while (tick_is_expired(t->expire, now_ms))
1252 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1253 out_unlock:
1254 if (check->server)
1255 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001256
1257 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001258
1259 /* Free the check if set to PURGE. After this, the check instance may be
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001260 * freed via the srv_drop invocation, so it must not be accessed after
1261 * this point.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001262 */
1263 if (unlikely(check->state & CHK_ST_PURGE)) {
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001264 free_check(check);
Amaury Denoyelle9ba34ae2021-08-09 15:09:17 +02001265 if (check->server)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001266 srv_drop(check->server);
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001267
1268 t = NULL;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001269 }
1270
Willy Tarreau51cd5952020-06-05 12:25:38 +02001271 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001272}
1273
Willy Tarreau51cd5952020-06-05 12:25:38 +02001274
Christopher Faulet61cc8522020-04-20 14:54:42 +02001275/**************************************************************************/
1276/************************** Init/deinit checks ****************************/
1277/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001278/*
1279 * Tries to grab a buffer and to re-enables processing on check <target>. The
1280 * check flags are used to figure what buffer was requested. It returns 1 if the
1281 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1282 * impossible to wake up and we prefer to be woken up later.
1283 */
1284int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001285{
Christopher Fauletb381a502020-11-25 13:47:00 +01001286 struct check *check = target;
1287
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001288 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001289 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001290 check->state &= ~CHK_ST_IN_ALLOC;
1291 tasklet_wakeup(check->wait_list.tasklet);
1292 return 1;
1293 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001294 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001295 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001296 check->state &= ~CHK_ST_OUT_ALLOC;
1297 tasklet_wakeup(check->wait_list.tasklet);
1298 return 1;
1299 }
1300
1301 return 0;
1302}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001303
Christopher Fauletb381a502020-11-25 13:47:00 +01001304/*
William Dauchyf4300902021-02-06 20:47:50 +01001305 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001306 */
1307struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1308{
1309 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001310
Willy Tarreau2b718102021-04-21 07:32:39 +02001311 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001312 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001313 check->buf_wait.target = check;
1314 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +02001315 LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001316 }
1317 return buf;
1318}
1319
1320/*
1321 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1322 * wait queue.
1323 */
1324void check_release_buf(struct check *check, struct buffer *bptr)
1325{
1326 if (bptr->size) {
1327 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001328 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001329 }
1330}
1331
1332const char *init_check(struct check *check, int type)
1333{
1334 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001335
Christopher Fauletb381a502020-11-25 13:47:00 +01001336 check->bi = BUF_NULL;
1337 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001338 LIST_INIT(&check->buf_wait.list);
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001339
Christopher Faulet61cc8522020-04-20 14:54:42 +02001340 check->wait_list.tasklet = tasklet_new();
1341 if (!check->wait_list.tasklet)
1342 return "out of memory while allocating check tasklet";
1343 check->wait_list.events = 0;
1344 check->wait_list.tasklet->process = event_srv_chk_io;
1345 check->wait_list.tasklet->context = check;
1346 return NULL;
1347}
1348
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001349/* Liberates the resources allocated for a check.
1350 *
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001351 * This function must only be run by the thread owning the check.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001352 */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001353void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001354{
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001355 /* For agent-check, free the rules / vars from the server. This is not
1356 * done for health-check : the proxy is the owner of the rules / vars
1357 * in this case.
1358 */
1359 if (check->state & CHK_ST_AGENT) {
1360 free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars);
1361 ha_free(&check->tcpcheck_rules);
1362 }
1363
Christopher Faulet61cc8522020-04-20 14:54:42 +02001364 task_destroy(check->task);
1365 if (check->wait_list.tasklet)
1366 tasklet_free(check->wait_list.tasklet);
1367
Christopher Fauletb381a502020-11-25 13:47:00 +01001368 check_release_buf(check, &check->bi);
1369 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001370 if (check->cs) {
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001371 cs_detach_endp(check->cs);
1372 cs_detach_app(check->cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001373 check->cs = NULL;
1374 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001375}
1376
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001377/* This function must be used in order to free a started check. The check will
1378 * be scheduled for a next execution in order to properly close and free all
1379 * check elements.
1380 *
1381 * Non thread-safe.
1382 */
1383void check_purge(struct check *check)
1384{
Amaury Denoyelle25fe1032021-08-10 16:21:55 +02001385 check->state |= CHK_ST_PURGE;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001386 task_wakeup(check->task, TASK_WOKEN_OTHER);
1387}
1388
Christopher Faulet61cc8522020-04-20 14:54:42 +02001389/* manages a server health-check. Returns the time the task accepts to wait, or
1390 * TIME_ETERNITY for infinity.
1391 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001392struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001393{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001394 struct check *check = context;
1395
1396 if (check->type == PR_O2_EXT_CHK)
1397 return process_chk_proc(t, context, state);
1398 return process_chk_conn(t, context, state);
1399
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001400}
1401
Christopher Faulet61cc8522020-04-20 14:54:42 +02001402
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001403int start_check_task(struct check *check, int mininter,
Christopher Faulet61cc8522020-04-20 14:54:42 +02001404 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001405{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001406 struct task *t;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001407
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001408 /* task for the check. Process-based checks exclusively run on thread 1. */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001409 if (check->type == PR_O2_EXT_CHK)
Willy Tarreaua89c1912021-10-20 18:43:30 +02001410 t = task_new_on(0);
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001411 else {
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001412 check->cs = cs_new_from_check(check, CS_FL_NONE);
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001413 if (!check->cs)
1414 goto fail_alloc_cs;
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001415 t = task_new_anywhere();
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001416 }
1417
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001418 if (!t)
1419 goto fail_alloc_task;
1420
Christopher Faulet61cc8522020-04-20 14:54:42 +02001421 check->task = t;
1422 t->process = process_chk;
1423 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001424
Christopher Faulet61cc8522020-04-20 14:54:42 +02001425 if (mininter < srv_getinter(check))
1426 mininter = srv_getinter(check);
1427
1428 if (global.max_spread_checks && mininter > global.max_spread_checks)
1429 mininter = global.max_spread_checks;
1430
1431 /* check this every ms */
1432 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1433 check->start = now;
1434 task_queue(t);
1435
1436 return 1;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001437
1438 fail_alloc_task:
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001439 cs_free(check->cs);
1440 fail_alloc_cs:
1441 ha_alert("Starting [%s:%s] check: out of memory.\n",
1442 check->server->proxy->id, check->server->id);
1443 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001444}
1445
Christopher Faulet61cc8522020-04-20 14:54:42 +02001446/*
1447 * Start health-check.
1448 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1449 */
1450static int start_checks()
1451{
1452
1453 struct proxy *px;
1454 struct server *s;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001455 int nbcheck=0, mininter=0, srvpos=0;
1456
1457 /* 0- init the dummy frontend used to create all checks sessions */
1458 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001459 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001460 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1461 checks_fe.mode = PR_MODE_TCP;
1462 checks_fe.maxconn = 0;
1463 checks_fe.conn_retries = CONN_RETRIES;
1464 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1465 checks_fe.timeout.client = TICK_ETERNITY;
1466
1467 /* 1- count the checkers to run simultaneously.
1468 * We also determine the minimum interval among all of those which
1469 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1470 * will be used to spread their start-up date. Those which have
1471 * a shorter interval will start independently and will not dictate
1472 * too short an interval for all others.
1473 */
1474 for (px = proxies_list; px; px = px->next) {
1475 for (s = px->srv; s; s = s->next) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001476 if (s->check.state & CHK_ST_CONFIGURED) {
1477 nbcheck++;
1478 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1479 (!mininter || mininter > srv_getinter(&s->check)))
1480 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001481 }
1482
Christopher Faulet61cc8522020-04-20 14:54:42 +02001483 if (s->agent.state & CHK_ST_CONFIGURED) {
1484 nbcheck++;
1485 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1486 (!mininter || mininter > srv_getinter(&s->agent)))
1487 mininter = srv_getinter(&s->agent);
1488 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001489 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001490 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001491
Christopher Faulet61cc8522020-04-20 14:54:42 +02001492 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001493 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001494
Christopher Faulet61cc8522020-04-20 14:54:42 +02001495 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001496
William Dauchyf4300902021-02-06 20:47:50 +01001497 /* 2- start them as far as possible from each other. For this, we will
1498 * start them after their interval is set to the min interval divided
1499 * by the number of servers, weighted by the server's position in the
1500 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001501 */
1502 for (px = proxies_list; px; px = px->next) {
1503 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1504 if (init_pid_list()) {
1505 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1506 return ERR_ALERT | ERR_FATAL;
1507 }
1508 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001509
Christopher Faulet61cc8522020-04-20 14:54:42 +02001510 for (s = px->srv; s; s = s->next) {
1511 /* A task for the main check */
1512 if (s->check.state & CHK_ST_CONFIGURED) {
1513 if (s->check.type == PR_O2_EXT_CHK) {
1514 if (!prepare_external_check(&s->check))
1515 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001516 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001517 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1518 return ERR_ALERT | ERR_FATAL;
1519 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001520 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001521
Christopher Faulet61cc8522020-04-20 14:54:42 +02001522 /* A task for a auxiliary agent check */
1523 if (s->agent.state & CHK_ST_CONFIGURED) {
1524 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1525 return ERR_ALERT | ERR_FATAL;
1526 }
1527 srvpos++;
1528 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001529 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001530 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001531 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001532}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001533
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001534
Christopher Faulet61cc8522020-04-20 14:54:42 +02001535/*
1536 * Return value:
1537 * the port to be used for the health check
1538 * 0 in case no port could be found for the check
1539 */
1540static int srv_check_healthcheck_port(struct check *chk)
1541{
1542 int i = 0;
1543 struct server *srv = NULL;
1544
1545 srv = chk->server;
1546
William Dauchyf4300902021-02-06 20:47:50 +01001547 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001548 if (chk->port > 0)
1549 return chk->port;
1550
1551 /* try to get the port from check_core.addr if check.port not set */
1552 i = get_host_port(&chk->addr);
1553 if (i > 0)
1554 return i;
1555
1556 /* try to get the port from server address */
1557 /* prevent MAPPORTS from working at this point, since checks could
1558 * not be performed in such case (MAPPORTS impose a relative ports
1559 * based on live traffic)
1560 */
1561 if (srv->flags & SRV_F_MAPPORTS)
1562 return 0;
1563
1564 i = srv->svc_port; /* by default */
1565 if (i > 0)
1566 return i;
1567
1568 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001569}
1570
Christopher Faulet61cc8522020-04-20 14:54:42 +02001571/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1572 * if an error occurred.
1573 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001574int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001575{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001576 const char *err;
1577 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001578 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001579 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001580
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001581 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001582 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001583
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001584 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001585
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001586 if (!(srv->flags & SRV_F_DYNAMIC)) {
1587 /* If neither a port nor an addr was specified and no check
1588 * transport layer is forced, then the transport layer used by
1589 * the checks is the same as for the production traffic.
1590 * Otherwise we use raw_sock by default, unless one is
1591 * 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;
1597 }
1598 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 Faulet61cc8522020-04-20 14:54:42 +02001602 else if (srv->check.use_ssl == 1)
1603 srv->check.xprt = xprt_get(XPRT_SSL);
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001604 }
1605 else {
1606 /* For dynamic servers, check-ssl and check-send-proxy must be
1607 * explicitely defined even if the check port was not
1608 * overridden.
1609 */
1610 if (srv->check.use_ssl == 1)
1611 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001612 }
1613
Christopher Faulet12882cf2020-04-23 15:50:18 +02001614 /* Inherit the mux protocol from the server if not already defined for
1615 * the check
1616 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001617 if (srv->mux_proto && !srv->check.mux_proto &&
1618 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1619 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001620 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001621 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001622 /* test that check proto is valid if explicitly defined */
1623 else if (srv->check.mux_proto &&
1624 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1625 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1626 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1627 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1628 ret |= ERR_ALERT | ERR_FATAL;
1629 goto out;
1630 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001631
Christopher Faulet61cc8522020-04-20 14:54:42 +02001632 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001633
Christopher Faulet61cc8522020-04-20 14:54:42 +02001634 /* We need at least a service port, a check port or the first tcp-check
1635 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1636 */
1637 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1638 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1639 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001640
Christopher Faulet61cc8522020-04-20 14:54:42 +02001641 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1642 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1643 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1644 ret |= ERR_ALERT | ERR_ABORT;
1645 goto out;
1646 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001647
Christopher Faulet61cc8522020-04-20 14:54:42 +02001648 /* search the first action (connect / send / expect) in the list */
1649 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1650 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1651 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1652 "nor tcp_check rule 'connect' with port information.\n",
1653 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1654 ret |= ERR_ALERT | ERR_ABORT;
1655 goto out;
1656 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001657
Christopher Faulet61cc8522020-04-20 14:54:42 +02001658 /* scan the tcp-check ruleset to ensure a port has been configured */
1659 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreauacff3092021-07-22 11:06:41 +02001660 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001661 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1662 "and a tcp_check rule 'connect' with no port information.\n",
1663 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1664 ret |= ERR_ALERT | ERR_ABORT;
1665 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001666 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001667 }
1668
Christopher Faulet61cc8522020-04-20 14:54:42 +02001669 init:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001670 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1671 if (err) {
1672 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1673 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1674 ret |= ERR_ALERT | ERR_ABORT;
1675 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001676 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001677 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001678 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001679
1680 /* Only increment maxsock for servers from the configuration. Dynamic
1681 * servers at the moment are not taken into account for the estimation
1682 * of the resources limits.
1683 */
1684 if (global.mode & MODE_STARTING)
1685 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001686
Christopher Faulet61cc8522020-04-20 14:54:42 +02001687 out:
1688 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001689}
1690
Christopher Faulet61cc8522020-04-20 14:54:42 +02001691/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1692 * if an error occurred.
1693 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001694int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001695{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001696 struct tcpcheck_rule *chk;
1697 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001698 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001699
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001700 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001701 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001702
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001703 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001704 * implicit one is inserted before all others.
1705 */
1706 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1707 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1708 chk = calloc(1, sizeof(*chk));
1709 if (!chk) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001710 ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
Christopher Faulet61cc8522020-04-20 14:54:42 +02001711 " to agent-check for server '%s' (out of memory).\n",
1712 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1713 ret |= ERR_ALERT | ERR_FATAL;
1714 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001715 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001716 chk->action = TCPCHK_ACT_CONNECT;
1717 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001718 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001719 }
1720
Christopher Faulete5870d82020-04-15 11:32:03 +02001721
Christopher Faulet61cc8522020-04-20 14:54:42 +02001722 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1723 if (err) {
1724 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1725 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1726 ret |= ERR_ALERT | ERR_ABORT;
1727 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001728 }
1729
Christopher Faulet61cc8522020-04-20 14:54:42 +02001730 if (!srv->agent.inter)
1731 srv->agent.inter = srv->check.inter;
1732
1733 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001734 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001735
1736 /* Only increment maxsock for servers from the configuration. Dynamic
1737 * servers at the moment are not taken into account for the estimation
1738 * of the resources limits.
1739 */
1740 if (global.mode & MODE_STARTING)
1741 global.maxsock++;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001742
1743 out:
1744 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001745}
1746
Christopher Faulet61cc8522020-04-20 14:54:42 +02001747static void deinit_srv_check(struct server *srv)
1748{
1749 if (srv->check.state & CHK_ST_CONFIGURED)
1750 free_check(&srv->check);
1751 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1752 srv->do_check = 0;
1753}
Christopher Faulete5870d82020-04-15 11:32:03 +02001754
Christopher Faulet61cc8522020-04-20 14:54:42 +02001755
1756static void deinit_srv_agent_check(struct server *srv)
1757{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001758 if (srv->agent.state & CHK_ST_CONFIGURED)
1759 free_check(&srv->agent);
1760
1761 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1762 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001763}
1764
Willy Tarreaucee013e2020-06-05 11:40:38 +02001765REGISTER_POST_SERVER_CHECK(init_srv_check);
1766REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001767REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001768
Willy Tarreaucee013e2020-06-05 11:40:38 +02001769REGISTER_SERVER_DEINIT(deinit_srv_check);
1770REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001771
Christopher Faulet61cc8522020-04-20 14:54:42 +02001772
1773/**************************************************************************/
1774/************************** Check sample fetches **************************/
1775/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001776
Christopher Faulet61cc8522020-04-20 14:54:42 +02001777static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001778 { /* END */ },
1779}};
1780
1781INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1782
1783
1784/**************************************************************************/
1785/************************ Check's parsing functions ***********************/
1786/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001787/* Parse the "addr" server keyword */
1788static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1789 char **errmsg)
1790{
1791 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001792 int port1, port2, err_code = 0;
1793
1794
1795 if (!*args[*cur_arg+1]) {
1796 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1797 goto error;
1798 }
1799
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001800 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1801 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001802 if (!sk) {
1803 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1804 goto error;
1805 }
1806
William Dauchy1c921cd2021-02-03 22:30:08 +01001807 srv->check.addr = *sk;
1808 /* if agentaddr was never set, we can use addr */
1809 if (!(srv->flags & SRV_F_AGENTADDR))
1810 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001811
1812 out:
1813 return err_code;
1814
1815 error:
1816 err_code |= ERR_ALERT | ERR_FATAL;
1817 goto out;
1818}
1819
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001820/* Parse the "agent-addr" server keyword */
1821static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1822 char **errmsg)
1823{
William Dauchy1c921cd2021-02-03 22:30:08 +01001824 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001825 int err_code = 0;
1826
1827 if (!*(args[*cur_arg+1])) {
1828 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1829 goto error;
1830 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001831 memset(&sk, 0, sizeof(sk));
1832 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001833 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1834 goto error;
1835 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001836 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001837
1838 out:
1839 return err_code;
1840
1841 error:
1842 err_code |= ERR_ALERT | ERR_FATAL;
1843 goto out;
1844}
1845
1846/* Parse the "agent-check" server keyword */
1847static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1848 char **errmsg)
1849{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001850 struct tcpcheck_ruleset *rs = NULL;
1851 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1852 struct tcpcheck_rule *chk;
1853 int err_code = 0;
1854
1855 if (srv->do_agent)
1856 goto out;
1857
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001858 if (!(curpx->cap & PR_CAP_BE)) {
1859 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1860 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1861 return ERR_WARN;
1862 }
1863
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001864 if (!rules) {
1865 rules = calloc(1, sizeof(*rules));
1866 if (!rules) {
1867 memprintf(errmsg, "out of memory.");
1868 goto error;
1869 }
1870 LIST_INIT(&rules->preset_vars);
1871 srv->agent.tcpcheck_rules = rules;
1872 }
1873 rules->list = NULL;
1874 rules->flags = 0;
1875
Christopher Faulet61cc8522020-04-20 14:54:42 +02001876 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001877 if (rs)
1878 goto ruleset_found;
1879
Christopher Faulet61cc8522020-04-20 14:54:42 +02001880 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001881 if (rs == NULL) {
1882 memprintf(errmsg, "out of memory.");
1883 goto error;
1884 }
1885
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001886 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001887 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1888 if (!chk) {
1889 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1890 goto error;
1891 }
1892 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001893 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001894
1895 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001896 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1897 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001898 if (!chk) {
1899 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1900 goto error;
1901 }
1902 chk->expect.custom = tcpcheck_agent_expect_reply;
1903 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001904 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001905
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001906 ruleset_found:
1907 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001908 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001909 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001910 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001911
1912 out:
Dirkjan Bussinkdfee2172021-06-18 19:57:49 +00001913 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001914
1915 error:
1916 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001917 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001918 err_code |= ERR_ALERT | ERR_FATAL;
1919 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001920}
1921
1922/* Parse the "agent-inter" server keyword */
1923static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1924 char **errmsg)
1925{
1926 const char *err = NULL;
1927 unsigned int delay;
1928 int err_code = 0;
1929
1930 if (!*(args[*cur_arg+1])) {
1931 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1932 goto error;
1933 }
1934
1935 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1936 if (err == PARSE_TIME_OVER) {
1937 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1938 args[*cur_arg+1], args[*cur_arg], srv->id);
1939 goto error;
1940 }
1941 else if (err == PARSE_TIME_UNDER) {
1942 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1943 args[*cur_arg+1], args[*cur_arg], srv->id);
1944 goto error;
1945 }
1946 else if (err) {
1947 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1948 *err, srv->id);
1949 goto error;
1950 }
1951 if (delay <= 0) {
1952 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1953 delay, args[*cur_arg], srv->id);
1954 goto error;
1955 }
1956 srv->agent.inter = delay;
1957
1958 out:
1959 return err_code;
1960
1961 error:
1962 err_code |= ERR_ALERT | ERR_FATAL;
1963 goto out;
1964}
1965
1966/* Parse the "agent-port" server keyword */
1967static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1968 char **errmsg)
1969{
1970 int err_code = 0;
1971
1972 if (!*(args[*cur_arg+1])) {
1973 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1974 goto error;
1975 }
1976
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001977 /* Only increment maxsock for servers from the configuration. Dynamic
1978 * servers at the moment are not taken into account for the estimation
1979 * of the resources limits.
1980 */
1981 if (global.mode & MODE_STARTING)
1982 global.maxsock++;
1983
William Dauchy4858fb22021-02-03 22:30:09 +01001984 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001985
1986 out:
1987 return err_code;
1988
1989 error:
1990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992}
1993
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001994int set_srv_agent_send(struct server *srv, const char *send)
1995{
1996 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1997 struct tcpcheck_var *var = NULL;
1998 char *str;
1999
2000 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02002001 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002002 if (str == NULL || var == NULL)
2003 goto error;
2004
2005 free_tcpcheck_vars(&rules->preset_vars);
2006
2007 var->data.type = SMP_T_STR;
2008 var->data.u.str.area = str;
2009 var->data.u.str.data = strlen(str);
2010 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02002011 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002012
2013 return 1;
2014
2015 error:
2016 free(str);
2017 free(var);
2018 return 0;
2019}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002020
2021/* Parse the "agent-send" server keyword */
2022static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2023 char **errmsg)
2024{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002025 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002026 int err_code = 0;
2027
2028 if (!*(args[*cur_arg+1])) {
2029 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2030 goto error;
2031 }
2032
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002033 if (!rules) {
2034 rules = calloc(1, sizeof(*rules));
2035 if (!rules) {
2036 memprintf(errmsg, "out of memory.");
2037 goto error;
2038 }
2039 LIST_INIT(&rules->preset_vars);
2040 srv->agent.tcpcheck_rules = rules;
2041 }
2042
2043 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002044 memprintf(errmsg, "out of memory.");
2045 goto error;
2046 }
2047
2048 out:
2049 return err_code;
2050
2051 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002052 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002053 err_code |= ERR_ALERT | ERR_FATAL;
2054 goto out;
2055}
2056
2057/* Parse the "no-agent-send" server keyword */
2058static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2059 char **errmsg)
2060{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002061 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002062 return 0;
2063}
2064
Christopher Fauletce8111e2020-04-06 15:04:11 +02002065/* Parse the "check" server keyword */
2066static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2067 char **errmsg)
2068{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002069 if (!(curpx->cap & PR_CAP_BE)) {
2070 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2071 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2072 return ERR_WARN;
2073 }
2074
Christopher Fauletce8111e2020-04-06 15:04:11 +02002075 srv->do_check = 1;
2076 return 0;
2077}
2078
2079/* Parse the "check-send-proxy" server keyword */
2080static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2081 char **errmsg)
2082{
2083 srv->check.send_proxy = 1;
2084 return 0;
2085}
2086
2087/* Parse the "check-via-socks4" server keyword */
2088static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2089 char **errmsg)
2090{
2091 srv->check.via_socks4 = 1;
2092 return 0;
2093}
2094
2095/* Parse the "no-check" server keyword */
2096static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2097 char **errmsg)
2098{
2099 deinit_srv_check(srv);
2100 return 0;
2101}
2102
2103/* Parse the "no-check-send-proxy" server keyword */
2104static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2105 char **errmsg)
2106{
2107 srv->check.send_proxy = 0;
2108 return 0;
2109}
2110
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002111/* parse the "check-proto" server keyword */
2112static int srv_parse_check_proto(char **args, int *cur_arg,
2113 struct proxy *px, struct server *newsrv, char **err)
2114{
2115 int err_code = 0;
2116
2117 if (!*args[*cur_arg + 1]) {
2118 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2119 goto error;
2120 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002121 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002122 if (!newsrv->check.mux_proto) {
2123 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2124 goto error;
2125 }
2126
2127 out:
2128 return err_code;
2129
2130 error:
2131 err_code |= ERR_ALERT | ERR_FATAL;
2132 goto out;
2133}
2134
2135
Christopher Fauletce8111e2020-04-06 15:04:11 +02002136/* Parse the "rise" server keyword */
2137static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2138 char **errmsg)
2139{
2140 int err_code = 0;
2141
2142 if (!*args[*cur_arg + 1]) {
2143 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2144 goto error;
2145 }
2146
2147 srv->check.rise = atol(args[*cur_arg+1]);
2148 if (srv->check.rise <= 0) {
2149 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2150 goto error;
2151 }
2152
2153 if (srv->check.health)
2154 srv->check.health = srv->check.rise;
2155
2156 out:
2157 return err_code;
2158
2159 error:
2160 deinit_srv_agent_check(srv);
2161 err_code |= ERR_ALERT | ERR_FATAL;
2162 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002163}
2164
2165/* Parse the "fall" server keyword */
2166static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2167 char **errmsg)
2168{
2169 int err_code = 0;
2170
2171 if (!*args[*cur_arg + 1]) {
2172 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2173 goto error;
2174 }
2175
2176 srv->check.fall = atol(args[*cur_arg+1]);
2177 if (srv->check.fall <= 0) {
2178 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2179 goto error;
2180 }
2181
2182 out:
2183 return err_code;
2184
2185 error:
2186 deinit_srv_agent_check(srv);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002189}
2190
2191/* Parse the "inter" server keyword */
2192static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2193 char **errmsg)
2194{
2195 const char *err = NULL;
2196 unsigned int delay;
2197 int err_code = 0;
2198
2199 if (!*(args[*cur_arg+1])) {
2200 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2201 goto error;
2202 }
2203
2204 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2205 if (err == PARSE_TIME_OVER) {
2206 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2207 args[*cur_arg+1], args[*cur_arg], srv->id);
2208 goto error;
2209 }
2210 else if (err == PARSE_TIME_UNDER) {
2211 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2212 args[*cur_arg+1], args[*cur_arg], srv->id);
2213 goto error;
2214 }
2215 else if (err) {
2216 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2217 *err, srv->id);
2218 goto error;
2219 }
2220 if (delay <= 0) {
2221 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2222 delay, args[*cur_arg], srv->id);
2223 goto error;
2224 }
2225 srv->check.inter = delay;
2226
2227 out:
2228 return err_code;
2229
2230 error:
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 goto out;
2233}
2234
2235
2236/* Parse the "fastinter" server keyword */
2237static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2238 char **errmsg)
2239{
2240 const char *err = NULL;
2241 unsigned int delay;
2242 int err_code = 0;
2243
2244 if (!*(args[*cur_arg+1])) {
2245 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2246 goto error;
2247 }
2248
2249 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2250 if (err == PARSE_TIME_OVER) {
2251 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2252 args[*cur_arg+1], args[*cur_arg], srv->id);
2253 goto error;
2254 }
2255 else if (err == PARSE_TIME_UNDER) {
2256 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2257 args[*cur_arg+1], args[*cur_arg], srv->id);
2258 goto error;
2259 }
2260 else if (err) {
2261 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2262 *err, srv->id);
2263 goto error;
2264 }
2265 if (delay <= 0) {
2266 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2267 delay, args[*cur_arg], srv->id);
2268 goto error;
2269 }
2270 srv->check.fastinter = delay;
2271
2272 out:
2273 return err_code;
2274
2275 error:
2276 err_code |= ERR_ALERT | ERR_FATAL;
2277 goto out;
2278}
2279
2280
2281/* Parse the "downinter" server keyword */
2282static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2283 char **errmsg)
2284{
2285 const char *err = NULL;
2286 unsigned int delay;
2287 int err_code = 0;
2288
2289 if (!*(args[*cur_arg+1])) {
2290 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2291 goto error;
2292 }
2293
2294 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2295 if (err == PARSE_TIME_OVER) {
2296 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2297 args[*cur_arg+1], args[*cur_arg], srv->id);
2298 goto error;
2299 }
2300 else if (err == PARSE_TIME_UNDER) {
2301 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2302 args[*cur_arg+1], args[*cur_arg], srv->id);
2303 goto error;
2304 }
2305 else if (err) {
2306 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2307 *err, srv->id);
2308 goto error;
2309 }
2310 if (delay <= 0) {
2311 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2312 delay, args[*cur_arg], srv->id);
2313 goto error;
2314 }
2315 srv->check.downinter = delay;
2316
2317 out:
2318 return err_code;
2319
2320 error:
2321 err_code |= ERR_ALERT | ERR_FATAL;
2322 goto out;
2323}
2324
2325/* Parse the "port" server keyword */
2326static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2327 char **errmsg)
2328{
2329 int err_code = 0;
2330
2331 if (!*(args[*cur_arg+1])) {
2332 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2333 goto error;
2334 }
2335
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002336 /* Only increment maxsock for servers from the configuration. Dynamic
2337 * servers at the moment are not taken into account for the estimation
2338 * of the resources limits.
2339 */
2340 if (global.mode & MODE_STARTING)
2341 global.maxsock++;
2342
Christopher Fauletce8111e2020-04-06 15:04:11 +02002343 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002344 /* if agentport was never set, we can use port */
2345 if (!(srv->flags & SRV_F_AGENTPORT))
2346 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002347
2348 out:
2349 return err_code;
2350
2351 error:
2352 err_code |= ERR_ALERT | ERR_FATAL;
2353 goto out;
2354}
2355
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002356static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002357 { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */
2358 { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02002359 { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002360 { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */
2361 { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */
2362 { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02002363 { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002364 { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */
2365 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */
2366 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002367 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2368 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2369 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1, 0 }, /* Disable PROXY protocol for health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002370 { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */
2371 { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */
2372 { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */
2373 { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */
2374 { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */
2375 { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002376 { NULL, NULL, 0 },
2377}};
2378
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002379INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002380
Willy Tarreaubd741542010-03-16 18:46:54 +01002381/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002382 * Local variables:
2383 * c-indent-level: 8
2384 * c-basic-offset: 8
2385 * End:
2386 */