blob: 96276c140f8d6317573abf190a4715b8bd18efaa [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>
Willy Tarreaua1718922020-06-04 16:25:31 +020068#include <haproxy/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Olivier Houchard9130a962017-10-17 17:33:43 +020070
Christopher Faulet61cc8522020-04-20 14:54:42 +020071static int wake_srv_chk(struct conn_stream *cs);
72struct data_cb check_conn_cb = {
73 .wake = wake_srv_chk,
74 .name = "CHCK",
75};
Christopher Fauletd7e63962020-04-17 20:15:59 +020076
Christopher Faulet5d503fc2020-03-30 20:34:34 +020077
Gaetan Rivet05d692d2020-02-14 17:42:54 +010078/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +020079struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +020080
Christopher Faulet61cc8522020-04-20 14:54:42 +020081/**************************************************************************/
82/************************ Handle check results ****************************/
83/**************************************************************************/
84struct check_status {
85 short result; /* one of SRV_CHK_* */
86 char *info; /* human readable short info */
87 char *desc; /* long description */
88};
89
90struct analyze_status {
91 char *desc; /* description */
92 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
93};
94
Simon Horman63a4a822012-03-19 07:24:41 +090095static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010096 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
97 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020098 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099
Willy Tarreau23964182014-05-20 20:56:30 +0200100 /* Below we have finished checks */
101 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100102 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100103
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100104 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200105
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100106 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
107 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
108 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200109
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100110 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
111 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
112 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200113
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100114 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
115 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200116
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200117 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200118
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100119 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
120 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
121 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900122
123 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
124 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200125 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200126};
127
Simon Horman63a4a822012-03-19 07:24:41 +0900128static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100129 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
130
131 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
132 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
133
134 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
135 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
136 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
137 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
138
139 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
140 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
141 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
142};
143
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100144/* checks if <err> is a real error for errno or one that can be ignored, and
145 * return 0 for these ones or <err> for real ones.
146 */
147static inline int unclean_errno(int err)
148{
149 if (err == EAGAIN || err == EINPROGRESS ||
150 err == EISCONN || err == EALREADY)
151 return 0;
152 return err;
153}
154
Christopher Faulet7aa32712021-02-01 13:11:50 +0100155/* Converts check_status code to result code */
156short get_check_status_result(short check_status)
157{
158 if (check_status < HCHK_STATUS_SIZE)
159 return check_statuses[check_status].result;
160 else
161 return check_statuses[HCHK_STATUS_UNKNOWN].result;
162}
163
Christopher Faulet61cc8522020-04-20 14:54:42 +0200164/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200165const char *get_check_status_description(short check_status) {
166
167 const char *desc;
168
169 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200170 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200171 else
172 desc = NULL;
173
174 if (desc && *desc)
175 return desc;
176 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200177 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200178}
179
Christopher Faulet61cc8522020-04-20 14:54:42 +0200180/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100181const char *get_check_status_info(short check_status)
182{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200183 const char *info;
184
185 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200186 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200187 else
188 info = NULL;
189
190 if (info && *info)
191 return info;
192 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200193 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200194}
195
Christopher Faulet61cc8522020-04-20 14:54:42 +0200196/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100197const char *get_analyze_status(short analyze_status) {
198
199 const char *desc;
200
201 if (analyze_status < HANA_STATUS_SIZE)
202 desc = analyze_statuses[analyze_status].desc;
203 else
204 desc = NULL;
205
206 if (desc && *desc)
207 return desc;
208 else
209 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
210}
211
Christopher Faulet61cc8522020-04-20 14:54:42 +0200212/* Sets check->status, update check->duration and fill check->result with an
213 * adequate CHK_RES_* value. The new check->health is computed based on the
214 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200215 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200216 * Shows information in logs about failed health check if server is UP or
217 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200218 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200219void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100220{
Simon Horman4a741432013-02-23 15:35:38 +0900221 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200222 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200223 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900224
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200225 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100226 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900227 check->desc[0] = '\0';
228 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200229 return;
230 }
231
Simon Horman4a741432013-02-23 15:35:38 +0900232 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200233 return;
234
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200235 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900236 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
237 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200238 } else
Simon Horman4a741432013-02-23 15:35:38 +0900239 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200240
Simon Horman4a741432013-02-23 15:35:38 +0900241 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200242 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900243 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200244
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100245 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900246 check->duration = -1;
247 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200248 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900249 check->duration = tv_ms_elapsed(&check->start, &now);
250 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200251 }
252
Willy Tarreau23964182014-05-20 20:56:30 +0200253 /* no change is expected if no state change occurred */
254 if (check->result == CHK_RES_NEUTRAL)
255 return;
256
Olivier Houchard0923fa42019-01-11 18:43:04 +0100257 /* If the check was really just sending a mail, it won't have an
258 * associated server, so we're done now.
259 */
260 if (!s)
261 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200262 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200263
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200264 switch (check->result) {
265 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200266 /* Failure to connect to the agent as a secondary check should not
267 * cause the server to be marked down.
268 */
269 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900270 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200271 (check->health > 0)) {
Olivier Houchard7059c552019-03-08 18:49:32 +0100272 _HA_ATOMIC_ADD(&s->counters.failed_checks, 1);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200273 report = 1;
274 check->health--;
275 if (check->health < check->rise)
276 check->health = 0;
277 }
278 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200279
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200280 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100281 case CHK_RES_CONDPASS:
282 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200283 report = 1;
284 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200286 if (check->health >= check->rise)
287 check->health = check->rise + check->fall - 1; /* OK now */
288 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200289
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200290 /* clear consecutive_errors if observing is enabled */
291 if (s->onerror)
292 s->consecutive_errors = 0;
293 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100294
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200295 default:
296 break;
297 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200298
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200299 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
300 (status != prev_status || report)) {
301 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200302 "%s check for %sserver %s/%s %s%s",
303 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200304 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100305 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100306 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200307 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200308
Emeric Brun5a133512017-10-19 14:42:30 +0200309 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200310
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100311 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200312 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
313 (check->health >= check->rise) ? check->fall : check->rise,
314 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200315
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200316 ha_warning("%s.\n", trash.area);
317 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
318 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200319 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200320}
321
Willy Tarreau4eec5472014-05-20 22:32:27 +0200322/* Marks the check <check>'s server down if the current check is already failed
323 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200325void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326{
Simon Horman4a741432013-02-23 15:35:38 +0900327 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900328
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200329 /* The agent secondary check should only cause a server to be marked
330 * as down if check->status is HCHK_STATUS_L7STS, which indicates
331 * that the agent returned "fail", "stopped" or "down".
332 * The implication here is that failure to connect to the agent
333 * as a secondary check should not cause the server to be marked
334 * down. */
335 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
336 return;
337
Willy Tarreau4eec5472014-05-20 22:32:27 +0200338 if (check->health > 0)
339 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100340
Willy Tarreau4eec5472014-05-20 22:32:27 +0200341 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200342 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343}
344
Willy Tarreauaf549582014-05-16 17:37:50 +0200345/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200346 * it isn't in maintenance, it is not tracking a down server and other checks
347 * comply. The rule is simple : by default, a server is up, unless any of the
348 * following conditions is true :
349 * - health check failed (check->health < rise)
350 * - agent check failed (agent->health < rise)
351 * - the server tracks a down server (track && track->state == STOPPED)
352 * Note that if the server has a slowstart, it will switch to STARTING instead
353 * of RUNNING. Also, only the health checks support the nolb mode, so the
354 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200355 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200356void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200357{
Simon Horman4a741432013-02-23 15:35:38 +0900358 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100359
Emeric Brun52a91d32017-08-31 14:41:55 +0200360 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200361 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100362
Emeric Brun52a91d32017-08-31 14:41:55 +0200363 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200364 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100365
Willy Tarreau3e048382014-05-21 10:30:54 +0200366 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
367 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100368
Willy Tarreau3e048382014-05-21 10:30:54 +0200369 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
370 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200371
Emeric Brun52a91d32017-08-31 14:41:55 +0200372 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200373 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100374
Emeric Brun5a133512017-10-19 14:42:30 +0200375 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100376}
377
Willy Tarreaudb58b792014-05-21 13:57:23 +0200378/* Marks the check <check> as valid and tries to set its server into stopping mode
379 * if it was running or starting, and provided it isn't in maintenance and other
380 * checks comply. The conditions for the server to be marked in stopping mode are
381 * the same as for it to be turned up. Also, only the health checks support the
382 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200383 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200384void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200385{
Simon Horman4a741432013-02-23 15:35:38 +0900386 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100387
Emeric Brun52a91d32017-08-31 14:41:55 +0200388 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200389 return;
390
Willy Tarreaudb58b792014-05-21 13:57:23 +0200391 if (check->state & CHK_ST_AGENT)
392 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100393
Emeric Brun52a91d32017-08-31 14:41:55 +0200394 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200395 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100396
Willy Tarreaudb58b792014-05-21 13:57:23 +0200397 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
398 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100399
Willy Tarreaudb58b792014-05-21 13:57:23 +0200400 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
401 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100402
Willy Tarreaub26881a2017-12-23 11:16:49 +0100403 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100404}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100406/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100407 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100408 */
409void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100410{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100411 int failed;
412 int expire;
413
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100414 if (s->observe >= HANA_OBS_SIZE)
415 return;
416
Willy Tarreaubb956662013-01-24 00:37:39 +0100417 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100418 return;
419
420 switch (analyze_statuses[status].lr[s->observe - 1]) {
421 case 1:
422 failed = 1;
423 break;
424
425 case 2:
426 failed = 0;
427 break;
428
429 default:
430 return;
431 }
432
433 if (!failed) {
434 /* good: clear consecutive_errors */
435 s->consecutive_errors = 0;
436 return;
437 }
438
Olivier Houchard7059c552019-03-08 18:49:32 +0100439 _HA_ATOMIC_ADD(&s->consecutive_errors, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100440
441 if (s->consecutive_errors < s->consecutive_errors_limit)
442 return;
443
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100444 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
445 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100446
Willy Tarreau4e9df272021-02-17 15:20:19 +0100447 if (s->check.fastinter)
448 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
449 else
450 expire = TICK_ETERNITY;
451
452 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
453
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100454 switch (s->onerror) {
455 case HANA_ONERR_FASTINTER:
456 /* force fastinter - nothing to do here as all modes force it */
457 break;
458
459 case HANA_ONERR_SUDDTH:
460 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900461 if (s->check.health > s->check.rise)
462 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100463
Tim Duesterhus588b3142020-05-29 14:35:51 +0200464 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100465
466 case HANA_ONERR_FAILCHK:
467 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200468 set_server_check_status(&s->check, HCHK_STATUS_HANA,
469 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200470 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100471 break;
472
473 case HANA_ONERR_MARKDWN:
474 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900475 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200476 set_server_check_status(&s->check, HCHK_STATUS_HANA,
477 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200478 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100479 break;
480
481 default:
482 /* write a warning? */
483 break;
484 }
485
Willy Tarreau4e9df272021-02-17 15:20:19 +0100486 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
487
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100488 s->consecutive_errors = 0;
Olivier Houchard7059c552019-03-08 18:49:32 +0100489 _HA_ATOMIC_ADD(&s->counters.failed_hana, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100490
Willy Tarreau4e9df272021-02-17 15:20:19 +0100491 if (tick_is_lt(expire, s->check.task->expire)) {
492 /* requeue check task with new expire */
493 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100494 }
Willy Tarreauef781042010-01-27 11:53:01 +0100495}
496
Christopher Faulet61cc8522020-04-20 14:54:42 +0200497/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100498 * closed, keep errno intact as it is supposed to contain the valid error code.
499 * If no error is reported, check the socket's error queue using getsockopt().
500 * Warning, this must be done only once when returning from poll, and never
501 * after an I/O error was attempted, otherwise the error queue might contain
502 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
503 * socket. Returns non-zero if an error was reported, zero if everything is
504 * clean (including a properly closed socket).
505 */
506static int retrieve_errno_from_socket(struct connection *conn)
507{
508 int skerr;
509 socklen_t lskerr = sizeof(skerr);
510
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100511 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100512 return 1;
513
Willy Tarreau3c728722014-01-23 13:50:42 +0100514 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100515 return 0;
516
Willy Tarreau585744b2017-08-24 14:31:19 +0200517 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100518 errno = skerr;
519
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100520 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100521
522 if (!errno) {
523 /* we could not retrieve an error, that does not mean there is
524 * none. Just don't change anything and only report the prior
525 * error if any.
526 */
527 if (conn->flags & CO_FL_ERROR)
528 return 1;
529 else
530 return 0;
531 }
532
533 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
534 return 1;
535}
536
Christopher Faulet61cc8522020-04-20 14:54:42 +0200537/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100538 * and adjust the server status accordingly. It may make use of <errno_bck>
539 * if non-null when the caller is absolutely certain of its validity (eg:
540 * checked just after a syscall). If the caller doesn't have a valid errno,
541 * it can pass zero, and retrieve_errno_from_socket() will be called to try
542 * to extract errno from the socket. If no error is reported, it will consider
543 * the <expired> flag. This is intended to be used when a connection error was
544 * reported in conn->flags or when a timeout was reported in <expired>. The
545 * function takes care of not updating a server status which was already set.
546 * All situations where at least one of <expired> or CO_FL_ERROR are set
547 * produce a status.
548 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200549void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100550{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200551 struct conn_stream *cs = check->cs;
552 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100553 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200554 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200555 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100556
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100557 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100558 return;
559
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100560 errno = unclean_errno(errno_bck);
561 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100562 retrieve_errno_from_socket(conn);
563
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200564 if (conn && !(conn->flags & CO_FL_ERROR) &&
565 !(cs->flags & CS_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100566 return;
567
568 /* we'll try to build a meaningful error message depending on the
569 * context of the error possibly present in conn->err_code, and the
570 * socket error possibly collected above. This is useful to know the
571 * exact step of the L6 layer (eg: SSL handshake).
572 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200573 chk = get_trash_chunk();
574
Christopher Faulet799f3a42020-04-07 12:06:14 +0200575 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200576 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200577 step = tcpcheck_get_step_id(check, NULL);
Willy Tarreau213c6782014-10-02 14:51:02 +0200578 if (!step)
579 chunk_printf(chk, " at initial connection step of tcp-check");
580 else {
581 chunk_printf(chk, " at step %d of tcp-check", step);
582 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200583 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
584 if (check->current_step->connect.port)
585 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200586 else
587 chunk_appendf(chk, " (connect)");
588 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200589 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
590 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100591
592 switch (expect->type) {
593 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200594 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100595 break;
596 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200597 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100598 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200599 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200600 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100601 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200602 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100603 chunk_appendf(chk, " (expect binary regex)");
604 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200605 case TCPCHK_EXPECT_STRING_LF:
606 chunk_appendf(chk, " (expect log-format string)");
607 break;
608 case TCPCHK_EXPECT_BINARY_LF:
609 chunk_appendf(chk, " (expect log-format binary)");
610 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200611 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200612 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200613 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200614 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200615 chunk_appendf(chk, " (expect HTTP status regex)");
616 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200617 case TCPCHK_EXPECT_HTTP_HEADER:
618 chunk_appendf(chk, " (expect HTTP header pattern)");
619 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200620 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200621 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200622 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200623 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200624 chunk_appendf(chk, " (expect HTTP body regex)");
625 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200626 case TCPCHK_EXPECT_HTTP_BODY_LF:
627 chunk_appendf(chk, " (expect log-format HTTP body)");
628 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200629 case TCPCHK_EXPECT_CUSTOM:
630 chunk_appendf(chk, " (expect custom function)");
631 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100632 case TCPCHK_EXPECT_UNDEF:
633 chunk_appendf(chk, " (undefined expect!)");
634 break;
635 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200636 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200637 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200638 chunk_appendf(chk, " (send)");
639 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200640
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200641 if (check->current_step && check->current_step->comment)
642 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200643 }
644 }
645
Willy Tarreau00149122017-10-04 18:05:01 +0200646 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100647 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200648 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
649 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100650 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200651 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
652 chk->area);
653 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100654 }
655 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100656 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200657 chunk_printf(&trash, "%s%s", strerror(errno),
658 chk->area);
659 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100660 }
661 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100663 }
664 }
665
Willy Tarreau00149122017-10-04 18:05:01 +0200666 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200667 /* NOTE: this is reported after <fall> tries */
668 chunk_printf(chk, "No port available for the TCP connection");
669 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
670 }
671
Christopher Faulet5e293762020-10-26 11:10:49 +0100672 if (!conn || !conn->ctrl) {
673 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200674 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
675 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100676 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100677 /* L4 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200678 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100679 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
680 else if (expired)
681 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200682
683 /*
684 * might be due to a server IP change.
685 * Let's trigger a DNS resolution if none are currently running.
686 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100687 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100688 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200689
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100690 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100691 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100692 /* L6 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200693 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100694 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
695 else if (expired)
696 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
697 }
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200698 else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100699 /* I/O error after connection was established and before we could diagnose */
700 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
701 }
702 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200703 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
704
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100705 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200706 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
707 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200708 tout = check->current_step->expect.tout_status;
709 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100710 }
711
712 return;
713}
714
Simon Horman98637e52014-06-20 12:30:16 +0900715
Christopher Faulet61cc8522020-04-20 14:54:42 +0200716/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200717int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900718{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200719 int sv_state;
720 int ratio;
721 char addr[46];
722 char port[6];
723 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
724 "UP %d/%d", "UP",
725 "NOLB %d/%d", "NOLB",
726 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900727
Christopher Faulet61cc8522020-04-20 14:54:42 +0200728 if (!(s->check.state & CHK_ST_ENABLED))
729 sv_state = 6;
730 else if (s->cur_state != SRV_ST_STOPPED) {
731 if (s->check.health == s->check.rise + s->check.fall - 1)
732 sv_state = 3; /* UP */
733 else
734 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900735
Christopher Faulet61cc8522020-04-20 14:54:42 +0200736 if (s->cur_state == SRV_ST_STOPPING)
737 sv_state += 2;
738 } else {
739 if (s->check.health)
740 sv_state = 1; /* going up */
741 else
742 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900743 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200744
Christopher Faulet61cc8522020-04-20 14:54:42 +0200745 chunk_appendf(buf, srv_hlt_st[sv_state],
746 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
747 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200748
Christopher Faulet61cc8522020-04-20 14:54:42 +0200749 addr_to_str(&s->addr, addr, sizeof(addr));
750 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
751 snprintf(port, sizeof(port), "%u", s->svc_port);
752 else
753 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200754
Christopher Faulet61cc8522020-04-20 14:54:42 +0200755 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
756 addr, port, s->proxy->id, s->id,
757 global.node,
758 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
759 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
760 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
761 s->nbpend);
Willy Tarreau9f6dc722019-03-01 11:15:10 +0100762
Christopher Faulet61cc8522020-04-20 14:54:42 +0200763 if ((s->cur_state == SRV_ST_STARTING) &&
764 now.tv_sec < s->last_change + s->slowstart &&
765 now.tv_sec >= s->last_change) {
766 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
767 chunk_appendf(buf, "; throttle=%d%%", ratio);
768 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +0200769
Christopher Faulet61cc8522020-04-20 14:54:42 +0200770 return b_data(buf);
771}
Christopher Fauletaaae9a02020-04-26 09:50:31 +0200772
Willy Tarreau51cd5952020-06-05 12:25:38 +0200773/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +0200774/***************** Health-checks based on connections *********************/
775/**************************************************************************/
776/* This function is used only for server health-checks. It handles connection
777 * status updates including errors. If necessary, it wakes the check task up.
778 * It returns 0 on normal cases, <0 if at least one close() has happened on the
779 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +0200780 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200781static int wake_srv_chk(struct conn_stream *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200782{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200783 struct connection *conn = cs->conn;
784 struct check *check = cs->data;
785 struct email_alertq *q = container_of(check, typeof(*q), check);
786 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200787
Willy Tarreau51cd5952020-06-05 12:25:38 +0200788 if (check->server)
789 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
790 else
791 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200792
Willy Tarreau51cd5952020-06-05 12:25:38 +0200793 /* we may have to make progress on the TCP checks */
794 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +0200795
Willy Tarreau51cd5952020-06-05 12:25:38 +0200796 cs = check->cs;
797 conn = cs->conn;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200798
Willy Tarreau51cd5952020-06-05 12:25:38 +0200799 if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) {
800 /* We may get error reports bypassing the I/O handlers, typically
801 * the case when sending a pure TCP check which fails, then the I/O
802 * handlers above are not called. This is completely handled by the
803 * main processing task so let's simply wake it up. If we get here,
804 * we expect errno to still be valid.
805 */
806 chk_report_conn_err(check, errno, 0);
807 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200808 }
809
Christopher Faulet8f100422021-01-18 15:47:03 +0100810 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Willy Tarreau51cd5952020-06-05 12:25:38 +0200811 /* Check complete or aborted. If connection not yet closed do it
812 * now and wake the check task up to be sure the result is
813 * handled ASAP. */
Willy Tarreau30bd4ef2020-12-11 11:09:29 +0100814 cs_drain_and_close(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +0200815 ret = -1;
Christopher Faulet8f100422021-01-18 15:47:03 +0100816
817 if (check->wait_list.events)
818 cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
819
Willy Tarreau51cd5952020-06-05 12:25:38 +0200820 /* We may have been scheduled to run, and the
821 * I/O handler expects to have a cs, so remove
822 * the tasklet
823 */
824 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
825 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200826 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200827
Willy Tarreau51cd5952020-06-05 12:25:38 +0200828 if (check->server)
829 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
830 else
831 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200832
Christopher Faulet61cc8522020-04-20 14:54:42 +0200833 return ret;
834}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200835
Willy Tarreau51cd5952020-06-05 12:25:38 +0200836/* This function checks if any I/O is wanted, and if so, attempts to do so */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100837struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200838{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200839 struct check *check = ctx;
840 struct conn_stream *cs = check->cs;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200841
Willy Tarreau51cd5952020-06-05 12:25:38 +0200842 wake_srv_chk(cs);
843 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200844}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200845
Willy Tarreau51cd5952020-06-05 12:25:38 +0200846/* manages a server health-check that uses a connection. Returns
847 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +0200848 *
849 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +0200850 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +0200851 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100852struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200853{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200854 struct check *check = context;
855 struct proxy *proxy = check->proxy;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200856 struct conn_stream *cs = check->cs;
857 struct connection *conn = cs_conn(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +0200858 int rv;
859 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +0200860
Willy Tarreau51cd5952020-06-05 12:25:38 +0200861 if (check->server)
862 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
863 if (!(check->state & CHK_ST_INPROGRESS)) {
864 /* no check currently running */
865 if (!expired) /* woke up too early */
866 goto out_unlock;
Willy Tarreauabca5b62013-12-06 14:19:25 +0100867
Willy Tarreau51cd5952020-06-05 12:25:38 +0200868 /* we don't send any health-checks when the proxy is
869 * stopped, the server should not be checked or the check
870 * is disabled.
871 */
872 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreauc3914d42020-09-24 08:39:22 +0200873 proxy->disabled)
Willy Tarreau51cd5952020-06-05 12:25:38 +0200874 goto reschedule;
Christopher Faulet404f9192020-04-09 23:13:54 +0200875
Willy Tarreau51cd5952020-06-05 12:25:38 +0200876 /* we'll initiate a new check */
877 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +0200878
Willy Tarreau51cd5952020-06-05 12:25:38 +0200879 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200880
Willy Tarreau51cd5952020-06-05 12:25:38 +0200881 task_set_affinity(t, tid_bit);
882
883 check->current_step = NULL;
884 tcpcheck_main(check);
885 goto out_unlock;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200886 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200887 else {
888 /* there was a test running.
889 * First, let's check whether there was an uncaught error,
890 * which can happen on connect timeout or error.
891 */
892 if (check->result == CHK_RES_UNKNOWN) {
Christopher Fauletb1bb0692020-11-25 16:47:30 +0100893 /* Here the connection must be defined. Otherwise the
894 * error would have already been detected
895 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200896 if ((conn->flags & CO_FL_ERROR) || cs->flags & CS_FL_ERROR || expired) {
897 chk_report_conn_err(check, 0, expired);
898 }
Christopher Faulet8f100422021-01-18 15:47:03 +0100899 else {
900 if (check->state & CHK_ST_CLOSE_CONN) {
901 cs_destroy(cs);
902 cs = NULL;
903 conn = NULL;
904 check->cs = NULL;
905 check->state &= ~CHK_ST_CLOSE_CONN;
906 tcpcheck_main(check);
907 }
908 if (check->result == CHK_RES_UNKNOWN)
909 goto out_unlock; /* timeout not reached, wait again */
910 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200911 }
Christopher Faulet404f9192020-04-09 23:13:54 +0200912
Willy Tarreau51cd5952020-06-05 12:25:38 +0200913 /* check complete or aborted */
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200914
Willy Tarreau51cd5952020-06-05 12:25:38 +0200915 check->current_step = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200916
Willy Tarreau51cd5952020-06-05 12:25:38 +0200917 if (conn && conn->xprt) {
918 /* The check was aborted and the connection was not yet closed.
919 * This can happen upon timeout, or when an external event such
920 * as a failed response coupled with "observe layer7" caused the
921 * server state to be suddenly changed.
922 */
Willy Tarreau30bd4ef2020-12-11 11:09:29 +0100923 cs_drain_and_close(cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200924 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200925
Willy Tarreau51cd5952020-06-05 12:25:38 +0200926 if (cs) {
927 if (check->wait_list.events)
928 cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
929 /* We may have been scheduled to run, and the
930 * I/O handler expects to have a cs, so remove
931 * the tasklet
932 */
933 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
934 cs_destroy(cs);
935 cs = check->cs = NULL;
936 conn = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200937 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200938
939 if (check->sess != NULL) {
940 vars_prune(&check->vars, check->sess, NULL);
941 session_free(check->sess);
942 check->sess = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200943 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200944
945 if (check->server) {
946 if (check->result == CHK_RES_FAILED) {
947 /* a failure or timeout detected */
948 check_notify_failure(check);
949 }
950 else if (check->result == CHK_RES_CONDPASS) {
951 /* check is OK but asks for stopping mode */
952 check_notify_stopping(check);
953 }
954 else if (check->result == CHK_RES_PASSED) {
955 /* a success was detected */
956 check_notify_success(check);
957 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200958 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200959 task_set_affinity(t, MAX_THREADS_MASK);
Christopher Fauletb381a502020-11-25 13:47:00 +0100960 check_release_buf(check, &check->bi);
961 check_release_buf(check, &check->bo);
962 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200963
Willy Tarreau51cd5952020-06-05 12:25:38 +0200964 if (check->server) {
965 rv = 0;
966 if (global.spread_checks > 0) {
967 rv = srv_getinter(check) * global.spread_checks / 100;
968 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +0200969 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200970 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +0200971 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200972 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200973
974 reschedule:
975 while (tick_is_expired(t->expire, now_ms))
976 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
977 out_unlock:
978 if (check->server)
979 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
980 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200981}
982
Willy Tarreau51cd5952020-06-05 12:25:38 +0200983
Christopher Faulet61cc8522020-04-20 14:54:42 +0200984/**************************************************************************/
985/************************** Init/deinit checks ****************************/
986/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +0100987/*
988 * Tries to grab a buffer and to re-enables processing on check <target>. The
989 * check flags are used to figure what buffer was requested. It returns 1 if the
990 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
991 * impossible to wake up and we prefer to be woken up later.
992 */
993int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200994{
Christopher Fauletb381a502020-11-25 13:47:00 +0100995 struct check *check = target;
996
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100997 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Fauletb381a502020-11-25 13:47:00 +0100998 check->state &= ~CHK_ST_IN_ALLOC;
999 tasklet_wakeup(check->wait_list.tasklet);
1000 return 1;
1001 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001002 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001003 check->state &= ~CHK_ST_OUT_ALLOC;
1004 tasklet_wakeup(check->wait_list.tasklet);
1005 return 1;
1006 }
1007
1008 return 0;
1009}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001010
Christopher Fauletb381a502020-11-25 13:47:00 +01001011/*
William Dauchyf4300902021-02-06 20:47:50 +01001012 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001013 */
1014struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1015{
1016 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001017
Willy Tarreau90f366b2021-02-20 11:49:49 +01001018 if (likely(!LIST_ADDED(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001019 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001020 check->buf_wait.target = check;
1021 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001022 LIST_ADDQ(&ti->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001023 }
1024 return buf;
1025}
1026
1027/*
1028 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1029 * wait queue.
1030 */
1031void check_release_buf(struct check *check, struct buffer *bptr)
1032{
1033 if (bptr->size) {
1034 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001035 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001036 }
1037}
1038
1039const char *init_check(struct check *check, int type)
1040{
1041 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001042
Christopher Fauletb381a502020-11-25 13:47:00 +01001043 check->bi = BUF_NULL;
1044 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001045 LIST_INIT(&check->buf_wait.list);
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001046
Christopher Faulet61cc8522020-04-20 14:54:42 +02001047 check->wait_list.tasklet = tasklet_new();
1048 if (!check->wait_list.tasklet)
1049 return "out of memory while allocating check tasklet";
1050 check->wait_list.events = 0;
1051 check->wait_list.tasklet->process = event_srv_chk_io;
1052 check->wait_list.tasklet->context = check;
1053 return NULL;
1054}
1055
1056void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001057{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001058 task_destroy(check->task);
1059 if (check->wait_list.tasklet)
1060 tasklet_free(check->wait_list.tasklet);
1061
Christopher Fauletb381a502020-11-25 13:47:00 +01001062 check_release_buf(check, &check->bi);
1063 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001064 if (check->cs) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001065 ha_free(&check->cs->conn);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001066 cs_free(check->cs);
1067 check->cs = NULL;
1068 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001069}
1070
Christopher Faulet61cc8522020-04-20 14:54:42 +02001071/* manages a server health-check. Returns the time the task accepts to wait, or
1072 * TIME_ETERNITY for infinity.
1073 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001074struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001075{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001076 struct check *check = context;
1077
1078 if (check->type == PR_O2_EXT_CHK)
1079 return process_chk_proc(t, context, state);
1080 return process_chk_conn(t, context, state);
1081
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001082}
1083
Christopher Faulet61cc8522020-04-20 14:54:42 +02001084
1085static int start_check_task(struct check *check, int mininter,
1086 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001087{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001088 struct task *t;
1089 unsigned long thread_mask = MAX_THREADS_MASK;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001090
Christopher Faulet61cc8522020-04-20 14:54:42 +02001091 if (check->type == PR_O2_EXT_CHK)
1092 thread_mask = 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001093
Christopher Faulet61cc8522020-04-20 14:54:42 +02001094 /* task for the check */
1095 if ((t = task_new(thread_mask)) == NULL) {
1096 ha_alert("Starting [%s:%s] check: out of memory.\n",
1097 check->server->proxy->id, check->server->id);
1098 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001099 }
1100
Christopher Faulet61cc8522020-04-20 14:54:42 +02001101 check->task = t;
1102 t->process = process_chk;
1103 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001104
Christopher Faulet61cc8522020-04-20 14:54:42 +02001105 if (mininter < srv_getinter(check))
1106 mininter = srv_getinter(check);
1107
1108 if (global.max_spread_checks && mininter > global.max_spread_checks)
1109 mininter = global.max_spread_checks;
1110
1111 /* check this every ms */
1112 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1113 check->start = now;
1114 task_queue(t);
1115
1116 return 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001117}
1118
Christopher Faulet61cc8522020-04-20 14:54:42 +02001119/* updates the server's weight during a warmup stage. Once the final weight is
1120 * reached, the task automatically stops. Note that any server status change
1121 * must have updated s->last_change accordingly.
1122 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001123struct task *server_warmup(struct task *t, void *context, unsigned int state)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001124{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001125 struct server *s = context;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001126
Christopher Faulet61cc8522020-04-20 14:54:42 +02001127 /* by default, plan on stopping the task */
1128 t->expire = TICK_ETERNITY;
1129 if ((s->next_admin & SRV_ADMF_MAINT) ||
1130 (s->next_state != SRV_ST_STARTING))
1131 return t;
Christopher Faulete5870d82020-04-15 11:32:03 +02001132
Christopher Faulet61cc8522020-04-20 14:54:42 +02001133 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001134
Christopher Faulet61cc8522020-04-20 14:54:42 +02001135 /* recalculate the weights and update the state */
1136 server_recalc_eweight(s, 1);
Christopher Faulet5c288742020-03-31 08:15:58 +02001137
Christopher Faulet61cc8522020-04-20 14:54:42 +02001138 /* probably that we can refill this server with a bit more connections */
1139 pendconn_grab_from_px(s);
Christopher Faulet5c288742020-03-31 08:15:58 +02001140
Christopher Faulet61cc8522020-04-20 14:54:42 +02001141 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5c288742020-03-31 08:15:58 +02001142
Christopher Faulet61cc8522020-04-20 14:54:42 +02001143 /* get back there in 1 second or 1/20th of the slowstart interval,
1144 * whichever is greater, resulting in small 5% steps.
1145 */
1146 if (s->next_state == SRV_ST_STARTING)
1147 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1148 return t;
1149}
1150
1151/*
1152 * Start health-check.
1153 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1154 */
1155static int start_checks()
1156{
1157
1158 struct proxy *px;
1159 struct server *s;
1160 struct task *t;
1161 int nbcheck=0, mininter=0, srvpos=0;
1162
1163 /* 0- init the dummy frontend used to create all checks sessions */
1164 init_new_proxy(&checks_fe);
1165 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1166 checks_fe.mode = PR_MODE_TCP;
1167 checks_fe.maxconn = 0;
1168 checks_fe.conn_retries = CONN_RETRIES;
1169 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1170 checks_fe.timeout.client = TICK_ETERNITY;
1171
1172 /* 1- count the checkers to run simultaneously.
1173 * We also determine the minimum interval among all of those which
1174 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1175 * will be used to spread their start-up date. Those which have
1176 * a shorter interval will start independently and will not dictate
1177 * too short an interval for all others.
1178 */
1179 for (px = proxies_list; px; px = px->next) {
1180 for (s = px->srv; s; s = s->next) {
1181 if (s->slowstart) {
1182 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
1183 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1184 return ERR_ALERT | ERR_FATAL;
1185 }
1186 /* We need a warmup task that will be called when the server
1187 * state switches from down to up.
1188 */
1189 s->warmup = t;
1190 t->process = server_warmup;
1191 t->context = s;
1192 /* server can be in this state only because of */
1193 if (s->next_state == SRV_ST_STARTING)
1194 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20)));
Christopher Faulet5c288742020-03-31 08:15:58 +02001195 }
1196
Christopher Faulet61cc8522020-04-20 14:54:42 +02001197 if (s->check.state & CHK_ST_CONFIGURED) {
1198 nbcheck++;
1199 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1200 (!mininter || mininter > srv_getinter(&s->check)))
1201 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001202 }
1203
Christopher Faulet61cc8522020-04-20 14:54:42 +02001204 if (s->agent.state & CHK_ST_CONFIGURED) {
1205 nbcheck++;
1206 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1207 (!mininter || mininter > srv_getinter(&s->agent)))
1208 mininter = srv_getinter(&s->agent);
1209 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001210 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001211 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001212
Christopher Faulet61cc8522020-04-20 14:54:42 +02001213 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001214 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001215
Christopher Faulet61cc8522020-04-20 14:54:42 +02001216 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001217
William Dauchyf4300902021-02-06 20:47:50 +01001218 /* 2- start them as far as possible from each other. For this, we will
1219 * start them after their interval is set to the min interval divided
1220 * by the number of servers, weighted by the server's position in the
1221 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001222 */
1223 for (px = proxies_list; px; px = px->next) {
1224 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1225 if (init_pid_list()) {
1226 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1227 return ERR_ALERT | ERR_FATAL;
1228 }
1229 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001230
Christopher Faulet61cc8522020-04-20 14:54:42 +02001231 for (s = px->srv; s; s = s->next) {
1232 /* A task for the main check */
1233 if (s->check.state & CHK_ST_CONFIGURED) {
1234 if (s->check.type == PR_O2_EXT_CHK) {
1235 if (!prepare_external_check(&s->check))
1236 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001237 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001238 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1239 return ERR_ALERT | ERR_FATAL;
1240 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001241 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001242
Christopher Faulet61cc8522020-04-20 14:54:42 +02001243 /* A task for a auxiliary agent check */
1244 if (s->agent.state & CHK_ST_CONFIGURED) {
1245 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1246 return ERR_ALERT | ERR_FATAL;
1247 }
1248 srvpos++;
1249 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001250 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001251 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001252 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001253}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001254
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001255
Christopher Faulet61cc8522020-04-20 14:54:42 +02001256/*
1257 * Return value:
1258 * the port to be used for the health check
1259 * 0 in case no port could be found for the check
1260 */
1261static int srv_check_healthcheck_port(struct check *chk)
1262{
1263 int i = 0;
1264 struct server *srv = NULL;
1265
1266 srv = chk->server;
1267
William Dauchyf4300902021-02-06 20:47:50 +01001268 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001269 if (chk->port > 0)
1270 return chk->port;
1271
1272 /* try to get the port from check_core.addr if check.port not set */
1273 i = get_host_port(&chk->addr);
1274 if (i > 0)
1275 return i;
1276
1277 /* try to get the port from server address */
1278 /* prevent MAPPORTS from working at this point, since checks could
1279 * not be performed in such case (MAPPORTS impose a relative ports
1280 * based on live traffic)
1281 */
1282 if (srv->flags & SRV_F_MAPPORTS)
1283 return 0;
1284
1285 i = srv->svc_port; /* by default */
1286 if (i > 0)
1287 return i;
1288
1289 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001290}
1291
Christopher Faulet61cc8522020-04-20 14:54:42 +02001292/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1293 * if an error occurred.
1294 */
1295static int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001296{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001297 const char *err;
1298 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001299 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001300 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001301
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001302 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001303 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001304
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001305 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001306
Christopher Faulet61cc8522020-04-20 14:54:42 +02001307 /* If neither a port nor an addr was specified and no check transport
1308 * layer is forced, then the transport layer used by the checks is the
1309 * same as for the production traffic. Otherwise we use raw_sock by
1310 * default, unless one is specified.
1311 */
1312 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1313 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1314 srv->check.use_ssl = srv->use_ssl;
1315 srv->check.xprt = srv->xprt;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001316 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001317 else if (srv->check.use_ssl == 1)
1318 srv->check.xprt = xprt_get(XPRT_SSL);
1319 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001320 }
Christopher Faulet66163ec2020-05-20 22:36:24 +02001321 else if (srv->check.use_ssl == 1)
1322 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001323
Christopher Faulet12882cf2020-04-23 15:50:18 +02001324 /* Inherit the mux protocol from the server if not already defined for
1325 * the check
1326 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001327 if (srv->mux_proto && !srv->check.mux_proto &&
1328 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1329 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001330 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001331 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001332 /* test that check proto is valid if explicitly defined */
1333 else if (srv->check.mux_proto &&
1334 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1335 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1336 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1337 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1338 ret |= ERR_ALERT | ERR_FATAL;
1339 goto out;
1340 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001341
Christopher Faulet61cc8522020-04-20 14:54:42 +02001342 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001343
Christopher Faulet61cc8522020-04-20 14:54:42 +02001344 /* We need at least a service port, a check port or the first tcp-check
1345 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1346 */
1347 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1348 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1349 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001350
Christopher Faulet61cc8522020-04-20 14:54:42 +02001351 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1352 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1353 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1354 ret |= ERR_ALERT | ERR_ABORT;
1355 goto out;
1356 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001357
Christopher Faulet61cc8522020-04-20 14:54:42 +02001358 /* search the first action (connect / send / expect) in the list */
1359 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1360 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1361 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1362 "nor tcp_check rule 'connect' with port information.\n",
1363 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1364 ret |= ERR_ALERT | ERR_ABORT;
1365 goto out;
1366 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001367
Christopher Faulet61cc8522020-04-20 14:54:42 +02001368 /* scan the tcp-check ruleset to ensure a port has been configured */
1369 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
1370 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) {
1371 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1372 "and a tcp_check rule 'connect' with no port information.\n",
1373 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1374 ret |= ERR_ALERT | ERR_ABORT;
1375 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001376 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001377 }
1378
Christopher Faulet61cc8522020-04-20 14:54:42 +02001379 init:
1380 if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) {
1381 struct tcpcheck_ruleset *rs = NULL;
1382 struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules;
1383 //char *errmsg = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001384
Christopher Faulet61cc8522020-04-20 14:54:42 +02001385 srv->proxy->options2 &= ~PR_O2_CHK_ANY;
1386 srv->proxy->options2 |= PR_O2_TCPCHK_CHK;
Christopher Faulete5870d82020-04-15 11:32:03 +02001387
Christopher Faulet61cc8522020-04-20 14:54:42 +02001388 rs = find_tcpcheck_ruleset("*tcp-check");
1389 if (!rs) {
1390 rs = create_tcpcheck_ruleset("*tcp-check");
1391 if (rs == NULL) {
1392 ha_alert("config: %s '%s': out of memory.\n",
1393 proxy_type_str(srv->proxy), srv->proxy->id);
1394 ret |= ERR_ALERT | ERR_FATAL;
1395 goto out;
1396 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001397 }
1398
Christopher Faulet61cc8522020-04-20 14:54:42 +02001399 free_tcpcheck_vars(&rules->preset_vars);
1400 rules->list = &rs->rules;
1401 rules->flags = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001402 }
1403
Christopher Faulet61cc8522020-04-20 14:54:42 +02001404 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1405 if (err) {
1406 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1407 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1408 ret |= ERR_ALERT | ERR_ABORT;
1409 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001410 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001411 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1412 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001413
Christopher Faulet61cc8522020-04-20 14:54:42 +02001414 out:
1415 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001416}
1417
Christopher Faulet61cc8522020-04-20 14:54:42 +02001418/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1419 * if an error occurred.
1420 */
1421static int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001422{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001423 struct tcpcheck_rule *chk;
1424 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001425 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001426
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001427 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001428 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001429
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001430 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001431 * implicit one is inserted before all others.
1432 */
1433 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1434 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1435 chk = calloc(1, sizeof(*chk));
1436 if (!chk) {
1437 ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule"
1438 " to agent-check for server '%s' (out of memory).\n",
1439 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1440 ret |= ERR_ALERT | ERR_FATAL;
1441 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001442 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001443 chk->action = TCPCHK_ACT_CONNECT;
1444 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
1445 LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001446 }
1447
Christopher Faulete5870d82020-04-15 11:32:03 +02001448
Christopher Faulet61cc8522020-04-20 14:54:42 +02001449 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1450 if (err) {
1451 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1452 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1453 ret |= ERR_ALERT | ERR_ABORT;
1454 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001455 }
1456
Christopher Faulet61cc8522020-04-20 14:54:42 +02001457 if (!srv->agent.inter)
1458 srv->agent.inter = srv->check.inter;
1459
1460 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1461 global.maxsock++;
1462
1463 out:
1464 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001465}
1466
Christopher Faulet61cc8522020-04-20 14:54:42 +02001467static void deinit_srv_check(struct server *srv)
1468{
1469 if (srv->check.state & CHK_ST_CONFIGURED)
1470 free_check(&srv->check);
1471 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1472 srv->do_check = 0;
1473}
Christopher Faulete5870d82020-04-15 11:32:03 +02001474
Christopher Faulet61cc8522020-04-20 14:54:42 +02001475
1476static void deinit_srv_agent_check(struct server *srv)
1477{
1478 if (srv->agent.tcpcheck_rules) {
1479 free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001480 ha_free(&srv->agent.tcpcheck_rules);
Christopher Faulete5870d82020-04-15 11:32:03 +02001481 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001482
Christopher Faulet61cc8522020-04-20 14:54:42 +02001483 if (srv->agent.state & CHK_ST_CONFIGURED)
1484 free_check(&srv->agent);
1485
1486 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1487 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001488}
1489
Willy Tarreaucee013e2020-06-05 11:40:38 +02001490REGISTER_POST_SERVER_CHECK(init_srv_check);
1491REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001492REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001493
Willy Tarreaucee013e2020-06-05 11:40:38 +02001494REGISTER_SERVER_DEINIT(deinit_srv_check);
1495REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001496
Christopher Faulet61cc8522020-04-20 14:54:42 +02001497
1498/**************************************************************************/
1499/************************** Check sample fetches **************************/
1500/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001501
Christopher Faulet61cc8522020-04-20 14:54:42 +02001502static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001503 { /* END */ },
1504}};
1505
1506INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1507
1508
1509/**************************************************************************/
1510/************************ Check's parsing functions ***********************/
1511/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001512/* Parse the "addr" server keyword */
1513static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1514 char **errmsg)
1515{
1516 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001517 int port1, port2, err_code = 0;
1518
1519
1520 if (!*args[*cur_arg+1]) {
1521 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1522 goto error;
1523 }
1524
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001525 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1526 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001527 if (!sk) {
1528 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1529 goto error;
1530 }
1531
William Dauchy1c921cd2021-02-03 22:30:08 +01001532 srv->check.addr = *sk;
1533 /* if agentaddr was never set, we can use addr */
1534 if (!(srv->flags & SRV_F_AGENTADDR))
1535 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001536
1537 out:
1538 return err_code;
1539
1540 error:
1541 err_code |= ERR_ALERT | ERR_FATAL;
1542 goto out;
1543}
1544
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001545/* Parse the "agent-addr" server keyword */
1546static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1547 char **errmsg)
1548{
William Dauchy1c921cd2021-02-03 22:30:08 +01001549 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001550 int err_code = 0;
1551
1552 if (!*(args[*cur_arg+1])) {
1553 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1554 goto error;
1555 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001556 memset(&sk, 0, sizeof(sk));
1557 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001558 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1559 goto error;
1560 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001561 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001562
1563 out:
1564 return err_code;
1565
1566 error:
1567 err_code |= ERR_ALERT | ERR_FATAL;
1568 goto out;
1569}
1570
1571/* Parse the "agent-check" server keyword */
1572static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1573 char **errmsg)
1574{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001575 struct tcpcheck_ruleset *rs = NULL;
1576 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1577 struct tcpcheck_rule *chk;
1578 int err_code = 0;
1579
1580 if (srv->do_agent)
1581 goto out;
1582
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001583 if (!(curpx->cap & PR_CAP_BE)) {
1584 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1585 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1586 return ERR_WARN;
1587 }
1588
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001589 if (!rules) {
1590 rules = calloc(1, sizeof(*rules));
1591 if (!rules) {
1592 memprintf(errmsg, "out of memory.");
1593 goto error;
1594 }
1595 LIST_INIT(&rules->preset_vars);
1596 srv->agent.tcpcheck_rules = rules;
1597 }
1598 rules->list = NULL;
1599 rules->flags = 0;
1600
Christopher Faulet61cc8522020-04-20 14:54:42 +02001601 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001602 if (rs)
1603 goto ruleset_found;
1604
Christopher Faulet61cc8522020-04-20 14:54:42 +02001605 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001606 if (rs == NULL) {
1607 memprintf(errmsg, "out of memory.");
1608 goto error;
1609 }
1610
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001611 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001612 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1613 if (!chk) {
1614 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1615 goto error;
1616 }
1617 chk->index = 0;
1618 LIST_ADDQ(&rs->rules, &chk->list);
1619
1620 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001621 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1622 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001623 if (!chk) {
1624 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1625 goto error;
1626 }
1627 chk->expect.custom = tcpcheck_agent_expect_reply;
1628 chk->index = 1;
1629 LIST_ADDQ(&rs->rules, &chk->list);
1630
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001631 ruleset_found:
1632 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001633 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001634 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001635 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001636
1637 out:
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001638 return 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001639
1640 error:
1641 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001642 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001643 err_code |= ERR_ALERT | ERR_FATAL;
1644 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001645}
1646
1647/* Parse the "agent-inter" server keyword */
1648static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1649 char **errmsg)
1650{
1651 const char *err = NULL;
1652 unsigned int delay;
1653 int err_code = 0;
1654
1655 if (!*(args[*cur_arg+1])) {
1656 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1657 goto error;
1658 }
1659
1660 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1661 if (err == PARSE_TIME_OVER) {
1662 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1663 args[*cur_arg+1], args[*cur_arg], srv->id);
1664 goto error;
1665 }
1666 else if (err == PARSE_TIME_UNDER) {
1667 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1668 args[*cur_arg+1], args[*cur_arg], srv->id);
1669 goto error;
1670 }
1671 else if (err) {
1672 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1673 *err, srv->id);
1674 goto error;
1675 }
1676 if (delay <= 0) {
1677 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1678 delay, args[*cur_arg], srv->id);
1679 goto error;
1680 }
1681 srv->agent.inter = delay;
1682
1683 out:
1684 return err_code;
1685
1686 error:
1687 err_code |= ERR_ALERT | ERR_FATAL;
1688 goto out;
1689}
1690
1691/* Parse the "agent-port" server keyword */
1692static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1693 char **errmsg)
1694{
1695 int err_code = 0;
1696
1697 if (!*(args[*cur_arg+1])) {
1698 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1699 goto error;
1700 }
1701
1702 global.maxsock++;
William Dauchy4858fb22021-02-03 22:30:09 +01001703 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001704
1705 out:
1706 return err_code;
1707
1708 error:
1709 err_code |= ERR_ALERT | ERR_FATAL;
1710 goto out;
1711}
1712
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001713int set_srv_agent_send(struct server *srv, const char *send)
1714{
1715 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1716 struct tcpcheck_var *var = NULL;
1717 char *str;
1718
1719 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02001720 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001721 if (str == NULL || var == NULL)
1722 goto error;
1723
1724 free_tcpcheck_vars(&rules->preset_vars);
1725
1726 var->data.type = SMP_T_STR;
1727 var->data.u.str.area = str;
1728 var->data.u.str.data = strlen(str);
1729 LIST_INIT(&var->list);
1730 LIST_ADDQ(&rules->preset_vars, &var->list);
1731
1732 return 1;
1733
1734 error:
1735 free(str);
1736 free(var);
1737 return 0;
1738}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001739
William Dauchyf4300902021-02-06 20:47:50 +01001740/* set agent addr and appropriate flag */
William Dauchy1c921cd2021-02-03 22:30:08 +01001741inline void set_srv_agent_addr(struct server *srv, struct sockaddr_storage *sk)
1742{
1743 srv->agent.addr = *sk;
1744 srv->flags |= SRV_F_AGENTADDR;
1745}
1746
William Dauchyf4300902021-02-06 20:47:50 +01001747/* set agent port and appropriate flag */
William Dauchy4858fb22021-02-03 22:30:09 +01001748inline void set_srv_agent_port(struct server *srv, int port)
1749{
1750 srv->agent.port = port;
1751 srv->flags |= SRV_F_AGENTPORT;
1752}
1753
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001754/* Parse the "agent-send" server keyword */
1755static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1756 char **errmsg)
1757{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001758 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001759 int err_code = 0;
1760
1761 if (!*(args[*cur_arg+1])) {
1762 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
1763 goto error;
1764 }
1765
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001766 if (!rules) {
1767 rules = calloc(1, sizeof(*rules));
1768 if (!rules) {
1769 memprintf(errmsg, "out of memory.");
1770 goto error;
1771 }
1772 LIST_INIT(&rules->preset_vars);
1773 srv->agent.tcpcheck_rules = rules;
1774 }
1775
1776 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001777 memprintf(errmsg, "out of memory.");
1778 goto error;
1779 }
1780
1781 out:
1782 return err_code;
1783
1784 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001785 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001786 err_code |= ERR_ALERT | ERR_FATAL;
1787 goto out;
1788}
1789
1790/* Parse the "no-agent-send" server keyword */
1791static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1792 char **errmsg)
1793{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001794 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001795 return 0;
1796}
1797
Christopher Fauletce8111e2020-04-06 15:04:11 +02001798/* Parse the "check" server keyword */
1799static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1800 char **errmsg)
1801{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001802 if (!(curpx->cap & PR_CAP_BE)) {
1803 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1804 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1805 return ERR_WARN;
1806 }
1807
Christopher Fauletce8111e2020-04-06 15:04:11 +02001808 srv->do_check = 1;
1809 return 0;
1810}
1811
1812/* Parse the "check-send-proxy" server keyword */
1813static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1814 char **errmsg)
1815{
1816 srv->check.send_proxy = 1;
1817 return 0;
1818}
1819
1820/* Parse the "check-via-socks4" server keyword */
1821static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1822 char **errmsg)
1823{
1824 srv->check.via_socks4 = 1;
1825 return 0;
1826}
1827
1828/* Parse the "no-check" server keyword */
1829static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1830 char **errmsg)
1831{
1832 deinit_srv_check(srv);
1833 return 0;
1834}
1835
1836/* Parse the "no-check-send-proxy" server keyword */
1837static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1838 char **errmsg)
1839{
1840 srv->check.send_proxy = 0;
1841 return 0;
1842}
1843
Christopher Fauletedc6ed92020-04-23 16:27:59 +02001844/* parse the "check-proto" server keyword */
1845static int srv_parse_check_proto(char **args, int *cur_arg,
1846 struct proxy *px, struct server *newsrv, char **err)
1847{
1848 int err_code = 0;
1849
1850 if (!*args[*cur_arg + 1]) {
1851 memprintf(err, "'%s' : missing value", args[*cur_arg]);
1852 goto error;
1853 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001854 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02001855 if (!newsrv->check.mux_proto) {
1856 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
1857 goto error;
1858 }
1859
1860 out:
1861 return err_code;
1862
1863 error:
1864 err_code |= ERR_ALERT | ERR_FATAL;
1865 goto out;
1866}
1867
1868
Christopher Fauletce8111e2020-04-06 15:04:11 +02001869/* Parse the "rise" server keyword */
1870static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1871 char **errmsg)
1872{
1873 int err_code = 0;
1874
1875 if (!*args[*cur_arg + 1]) {
1876 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
1877 goto error;
1878 }
1879
1880 srv->check.rise = atol(args[*cur_arg+1]);
1881 if (srv->check.rise <= 0) {
1882 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
1883 goto error;
1884 }
1885
1886 if (srv->check.health)
1887 srv->check.health = srv->check.rise;
1888
1889 out:
1890 return err_code;
1891
1892 error:
1893 deinit_srv_agent_check(srv);
1894 err_code |= ERR_ALERT | ERR_FATAL;
1895 goto out;
1896 return 0;
1897}
1898
1899/* Parse the "fall" server keyword */
1900static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1901 char **errmsg)
1902{
1903 int err_code = 0;
1904
1905 if (!*args[*cur_arg + 1]) {
1906 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
1907 goto error;
1908 }
1909
1910 srv->check.fall = atol(args[*cur_arg+1]);
1911 if (srv->check.fall <= 0) {
1912 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
1913 goto error;
1914 }
1915
1916 out:
1917 return err_code;
1918
1919 error:
1920 deinit_srv_agent_check(srv);
1921 err_code |= ERR_ALERT | ERR_FATAL;
1922 goto out;
1923 return 0;
1924}
1925
1926/* Parse the "inter" server keyword */
1927static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1928 char **errmsg)
1929{
1930 const char *err = NULL;
1931 unsigned int delay;
1932 int err_code = 0;
1933
1934 if (!*(args[*cur_arg+1])) {
1935 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1936 goto error;
1937 }
1938
1939 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1940 if (err == PARSE_TIME_OVER) {
1941 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1942 args[*cur_arg+1], args[*cur_arg], srv->id);
1943 goto error;
1944 }
1945 else if (err == PARSE_TIME_UNDER) {
1946 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1947 args[*cur_arg+1], args[*cur_arg], srv->id);
1948 goto error;
1949 }
1950 else if (err) {
1951 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1952 *err, srv->id);
1953 goto error;
1954 }
1955 if (delay <= 0) {
1956 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1957 delay, args[*cur_arg], srv->id);
1958 goto error;
1959 }
1960 srv->check.inter = delay;
1961
1962 out:
1963 return err_code;
1964
1965 error:
1966 err_code |= ERR_ALERT | ERR_FATAL;
1967 goto out;
1968}
1969
1970
1971/* Parse the "fastinter" server keyword */
1972static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1973 char **errmsg)
1974{
1975 const char *err = NULL;
1976 unsigned int delay;
1977 int err_code = 0;
1978
1979 if (!*(args[*cur_arg+1])) {
1980 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1981 goto error;
1982 }
1983
1984 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1985 if (err == PARSE_TIME_OVER) {
1986 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1987 args[*cur_arg+1], args[*cur_arg], srv->id);
1988 goto error;
1989 }
1990 else if (err == PARSE_TIME_UNDER) {
1991 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1992 args[*cur_arg+1], args[*cur_arg], srv->id);
1993 goto error;
1994 }
1995 else if (err) {
1996 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1997 *err, srv->id);
1998 goto error;
1999 }
2000 if (delay <= 0) {
2001 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2002 delay, args[*cur_arg], srv->id);
2003 goto error;
2004 }
2005 srv->check.fastinter = delay;
2006
2007 out:
2008 return err_code;
2009
2010 error:
2011 err_code |= ERR_ALERT | ERR_FATAL;
2012 goto out;
2013}
2014
2015
2016/* Parse the "downinter" server keyword */
2017static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2018 char **errmsg)
2019{
2020 const char *err = NULL;
2021 unsigned int delay;
2022 int err_code = 0;
2023
2024 if (!*(args[*cur_arg+1])) {
2025 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2026 goto error;
2027 }
2028
2029 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2030 if (err == PARSE_TIME_OVER) {
2031 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2032 args[*cur_arg+1], args[*cur_arg], srv->id);
2033 goto error;
2034 }
2035 else if (err == PARSE_TIME_UNDER) {
2036 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2037 args[*cur_arg+1], args[*cur_arg], srv->id);
2038 goto error;
2039 }
2040 else if (err) {
2041 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2042 *err, srv->id);
2043 goto error;
2044 }
2045 if (delay <= 0) {
2046 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2047 delay, args[*cur_arg], srv->id);
2048 goto error;
2049 }
2050 srv->check.downinter = delay;
2051
2052 out:
2053 return err_code;
2054
2055 error:
2056 err_code |= ERR_ALERT | ERR_FATAL;
2057 goto out;
2058}
2059
2060/* Parse the "port" server keyword */
2061static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2062 char **errmsg)
2063{
2064 int err_code = 0;
2065
2066 if (!*(args[*cur_arg+1])) {
2067 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2068 goto error;
2069 }
2070
2071 global.maxsock++;
2072 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002073 /* if agentport was never set, we can use port */
2074 if (!(srv->flags & SRV_F_AGENTPORT))
2075 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002076
2077 out:
2078 return err_code;
2079
2080 error:
2081 err_code |= ERR_ALERT | ERR_FATAL;
2082 goto out;
2083}
2084
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002085static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002086 { "addr", srv_parse_addr, 1, 1, 0 }, /* IP address to send health to or to probe from agent-check */
2087 { "agent-addr", srv_parse_agent_addr, 1, 1, 0 }, /* Enable an auxiliary agent check */
2088 { "agent-check", srv_parse_agent_check, 0, 1, 0 }, /* Enable agent checks */
2089 { "agent-inter", srv_parse_agent_inter, 1, 1, 0 }, /* Set the interval between two agent checks */
2090 { "agent-port", srv_parse_agent_port, 1, 1, 0 }, /* Set the TCP port used for agent checks. */
2091 { "agent-send", srv_parse_agent_send, 1, 1, 0 }, /* Set string to send to agent. */
2092 { "check", srv_parse_check, 0, 1, 0 }, /* Enable health checks */
2093 { "check-proto", srv_parse_check_proto, 1, 1, 0 }, /* Set the mux protocol for health checks */
2094 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 0 }, /* Enable PROXY protocol for health checks */
2095 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 0 }, /* Enable socks4 proxy for health checks */
2096 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2097 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2098 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1, 0 }, /* Disable PROXY protocol for health checks */
2099 { "rise", srv_parse_check_rise, 1, 1, 0 }, /* Set rise value for health checks */
2100 { "fall", srv_parse_check_fall, 1, 1, 0 }, /* Set fall value for health checks */
2101 { "inter", srv_parse_check_inter, 1, 1, 0 }, /* Set inter value for health checks */
2102 { "fastinter", srv_parse_check_fastinter, 1, 1, 0 }, /* Set fastinter value for health checks */
2103 { "downinter", srv_parse_check_downinter, 1, 1, 0 }, /* Set downinter value for health checks */
2104 { "port", srv_parse_check_port, 1, 1, 0 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002105 { NULL, NULL, 0 },
2106}};
2107
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002108INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002109
Willy Tarreaubd741542010-03-16 18:46:54 +01002110/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002111 * Local variables:
2112 * c-indent-level: 8
2113 * c-basic-offset: 8
2114 * End:
2115 */