blob: 5b6535478142610291d7fcb992d8edfae576f658 [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 Tarreau025fc712021-01-29 12:35:24 +0100837struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned short 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 Tarreau025fc712021-01-29 12:35:24 +0100852struct task *process_chk_conn(struct task *t, void *context, unsigned short 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
997 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc_margin(&check->bi, 0)) {
998 check->state &= ~CHK_ST_IN_ALLOC;
999 tasklet_wakeup(check->wait_list.tasklet);
1000 return 1;
1001 }
1002 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc_margin(&check->bo, 0)) {
1003 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)) &&
Christopher Fauletb381a502020-11-25 13:47:00 +01001019 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
1020 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) {
1065 free(check->cs->conn);
1066 check->cs->conn = NULL;
1067 cs_free(check->cs);
1068 check->cs = NULL;
1069 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001070}
1071
Christopher Faulet61cc8522020-04-20 14:54:42 +02001072/* manages a server health-check. Returns the time the task accepts to wait, or
1073 * TIME_ETERNITY for infinity.
1074 */
Willy Tarreaucee013e2020-06-05 11:40:38 +02001075struct task *process_chk(struct task *t, void *context, unsigned short state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001076{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001077 struct check *check = context;
1078
1079 if (check->type == PR_O2_EXT_CHK)
1080 return process_chk_proc(t, context, state);
1081 return process_chk_conn(t, context, state);
1082
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001083}
1084
Christopher Faulet61cc8522020-04-20 14:54:42 +02001085
1086static int start_check_task(struct check *check, int mininter,
1087 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001088{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001089 struct task *t;
1090 unsigned long thread_mask = MAX_THREADS_MASK;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001091
Christopher Faulet61cc8522020-04-20 14:54:42 +02001092 if (check->type == PR_O2_EXT_CHK)
1093 thread_mask = 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001094
Christopher Faulet61cc8522020-04-20 14:54:42 +02001095 /* task for the check */
1096 if ((t = task_new(thread_mask)) == NULL) {
1097 ha_alert("Starting [%s:%s] check: out of memory.\n",
1098 check->server->proxy->id, check->server->id);
1099 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001100 }
1101
Christopher Faulet61cc8522020-04-20 14:54:42 +02001102 check->task = t;
1103 t->process = process_chk;
1104 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001105
Christopher Faulet61cc8522020-04-20 14:54:42 +02001106 if (mininter < srv_getinter(check))
1107 mininter = srv_getinter(check);
1108
1109 if (global.max_spread_checks && mininter > global.max_spread_checks)
1110 mininter = global.max_spread_checks;
1111
1112 /* check this every ms */
1113 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1114 check->start = now;
1115 task_queue(t);
1116
1117 return 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001118}
1119
Christopher Faulet61cc8522020-04-20 14:54:42 +02001120/* updates the server's weight during a warmup stage. Once the final weight is
1121 * reached, the task automatically stops. Note that any server status change
1122 * must have updated s->last_change accordingly.
1123 */
Willy Tarreau025fc712021-01-29 12:35:24 +01001124struct task *server_warmup(struct task *t, void *context, unsigned short state)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001125{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001126 struct server *s = context;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001127
Christopher Faulet61cc8522020-04-20 14:54:42 +02001128 /* by default, plan on stopping the task */
1129 t->expire = TICK_ETERNITY;
1130 if ((s->next_admin & SRV_ADMF_MAINT) ||
1131 (s->next_state != SRV_ST_STARTING))
1132 return t;
Christopher Faulete5870d82020-04-15 11:32:03 +02001133
Christopher Faulet61cc8522020-04-20 14:54:42 +02001134 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001135
Christopher Faulet61cc8522020-04-20 14:54:42 +02001136 /* recalculate the weights and update the state */
1137 server_recalc_eweight(s, 1);
Christopher Faulet5c288742020-03-31 08:15:58 +02001138
Christopher Faulet61cc8522020-04-20 14:54:42 +02001139 /* probably that we can refill this server with a bit more connections */
1140 pendconn_grab_from_px(s);
Christopher Faulet5c288742020-03-31 08:15:58 +02001141
Christopher Faulet61cc8522020-04-20 14:54:42 +02001142 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5c288742020-03-31 08:15:58 +02001143
Christopher Faulet61cc8522020-04-20 14:54:42 +02001144 /* get back there in 1 second or 1/20th of the slowstart interval,
1145 * whichever is greater, resulting in small 5% steps.
1146 */
1147 if (s->next_state == SRV_ST_STARTING)
1148 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1149 return t;
1150}
1151
1152/*
1153 * Start health-check.
1154 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1155 */
1156static int start_checks()
1157{
1158
1159 struct proxy *px;
1160 struct server *s;
1161 struct task *t;
1162 int nbcheck=0, mininter=0, srvpos=0;
1163
1164 /* 0- init the dummy frontend used to create all checks sessions */
1165 init_new_proxy(&checks_fe);
1166 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1167 checks_fe.mode = PR_MODE_TCP;
1168 checks_fe.maxconn = 0;
1169 checks_fe.conn_retries = CONN_RETRIES;
1170 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1171 checks_fe.timeout.client = TICK_ETERNITY;
1172
1173 /* 1- count the checkers to run simultaneously.
1174 * We also determine the minimum interval among all of those which
1175 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1176 * will be used to spread their start-up date. Those which have
1177 * a shorter interval will start independently and will not dictate
1178 * too short an interval for all others.
1179 */
1180 for (px = proxies_list; px; px = px->next) {
1181 for (s = px->srv; s; s = s->next) {
1182 if (s->slowstart) {
1183 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
1184 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1185 return ERR_ALERT | ERR_FATAL;
1186 }
1187 /* We need a warmup task that will be called when the server
1188 * state switches from down to up.
1189 */
1190 s->warmup = t;
1191 t->process = server_warmup;
1192 t->context = s;
1193 /* server can be in this state only because of */
1194 if (s->next_state == SRV_ST_STARTING)
1195 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 +02001196 }
1197
Christopher Faulet61cc8522020-04-20 14:54:42 +02001198 if (s->check.state & CHK_ST_CONFIGURED) {
1199 nbcheck++;
1200 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1201 (!mininter || mininter > srv_getinter(&s->check)))
1202 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001203 }
1204
Christopher Faulet61cc8522020-04-20 14:54:42 +02001205 if (s->agent.state & CHK_ST_CONFIGURED) {
1206 nbcheck++;
1207 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1208 (!mininter || mininter > srv_getinter(&s->agent)))
1209 mininter = srv_getinter(&s->agent);
1210 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001211 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001212 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001213
Christopher Faulet61cc8522020-04-20 14:54:42 +02001214 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001215 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001216
Christopher Faulet61cc8522020-04-20 14:54:42 +02001217 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001218
William Dauchyf4300902021-02-06 20:47:50 +01001219 /* 2- start them as far as possible from each other. For this, we will
1220 * start them after their interval is set to the min interval divided
1221 * by the number of servers, weighted by the server's position in the
1222 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001223 */
1224 for (px = proxies_list; px; px = px->next) {
1225 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1226 if (init_pid_list()) {
1227 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1228 return ERR_ALERT | ERR_FATAL;
1229 }
1230 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001231
Christopher Faulet61cc8522020-04-20 14:54:42 +02001232 for (s = px->srv; s; s = s->next) {
1233 /* A task for the main check */
1234 if (s->check.state & CHK_ST_CONFIGURED) {
1235 if (s->check.type == PR_O2_EXT_CHK) {
1236 if (!prepare_external_check(&s->check))
1237 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001238 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001239 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1240 return ERR_ALERT | ERR_FATAL;
1241 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001242 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001243
Christopher Faulet61cc8522020-04-20 14:54:42 +02001244 /* A task for a auxiliary agent check */
1245 if (s->agent.state & CHK_ST_CONFIGURED) {
1246 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1247 return ERR_ALERT | ERR_FATAL;
1248 }
1249 srvpos++;
1250 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001251 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001252 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001253 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001254}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001255
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001256
Christopher Faulet61cc8522020-04-20 14:54:42 +02001257/*
1258 * Return value:
1259 * the port to be used for the health check
1260 * 0 in case no port could be found for the check
1261 */
1262static int srv_check_healthcheck_port(struct check *chk)
1263{
1264 int i = 0;
1265 struct server *srv = NULL;
1266
1267 srv = chk->server;
1268
William Dauchyf4300902021-02-06 20:47:50 +01001269 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001270 if (chk->port > 0)
1271 return chk->port;
1272
1273 /* try to get the port from check_core.addr if check.port not set */
1274 i = get_host_port(&chk->addr);
1275 if (i > 0)
1276 return i;
1277
1278 /* try to get the port from server address */
1279 /* prevent MAPPORTS from working at this point, since checks could
1280 * not be performed in such case (MAPPORTS impose a relative ports
1281 * based on live traffic)
1282 */
1283 if (srv->flags & SRV_F_MAPPORTS)
1284 return 0;
1285
1286 i = srv->svc_port; /* by default */
1287 if (i > 0)
1288 return i;
1289
1290 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001291}
1292
Christopher Faulet61cc8522020-04-20 14:54:42 +02001293/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1294 * if an error occurred.
1295 */
1296static int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001297{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001298 const char *err;
1299 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001300 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001301 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001302
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001303 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001304 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001305
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001306 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001307
Christopher Faulet61cc8522020-04-20 14:54:42 +02001308 /* If neither a port nor an addr was specified and no check transport
1309 * layer is forced, then the transport layer used by the checks is the
1310 * same as for the production traffic. Otherwise we use raw_sock by
1311 * default, unless one is specified.
1312 */
1313 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1314 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1315 srv->check.use_ssl = srv->use_ssl;
1316 srv->check.xprt = srv->xprt;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001317 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001318 else if (srv->check.use_ssl == 1)
1319 srv->check.xprt = xprt_get(XPRT_SSL);
1320 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001321 }
Christopher Faulet66163ec2020-05-20 22:36:24 +02001322 else if (srv->check.use_ssl == 1)
1323 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001324
Christopher Faulet12882cf2020-04-23 15:50:18 +02001325 /* Inherit the mux protocol from the server if not already defined for
1326 * the check
1327 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001328 if (srv->mux_proto && !srv->check.mux_proto &&
1329 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1330 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001331 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001332 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001333 /* test that check proto is valid if explicitly defined */
1334 else if (srv->check.mux_proto &&
1335 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1336 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1337 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1338 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1339 ret |= ERR_ALERT | ERR_FATAL;
1340 goto out;
1341 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001342
Christopher Faulet61cc8522020-04-20 14:54:42 +02001343 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001344
Christopher Faulet61cc8522020-04-20 14:54:42 +02001345 /* We need at least a service port, a check port or the first tcp-check
1346 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1347 */
1348 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1349 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1350 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001351
Christopher Faulet61cc8522020-04-20 14:54:42 +02001352 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1353 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1354 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1355 ret |= ERR_ALERT | ERR_ABORT;
1356 goto out;
1357 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001358
Christopher Faulet61cc8522020-04-20 14:54:42 +02001359 /* search the first action (connect / send / expect) in the list */
1360 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1361 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1362 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1363 "nor tcp_check rule 'connect' with port information.\n",
1364 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1365 ret |= ERR_ALERT | ERR_ABORT;
1366 goto out;
1367 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001368
Christopher Faulet61cc8522020-04-20 14:54:42 +02001369 /* scan the tcp-check ruleset to ensure a port has been configured */
1370 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
1371 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) {
1372 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1373 "and a tcp_check rule 'connect' with no port information.\n",
1374 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1375 ret |= ERR_ALERT | ERR_ABORT;
1376 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001377 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001378 }
1379
Christopher Faulet61cc8522020-04-20 14:54:42 +02001380 init:
1381 if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) {
1382 struct tcpcheck_ruleset *rs = NULL;
1383 struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules;
1384 //char *errmsg = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001385
Christopher Faulet61cc8522020-04-20 14:54:42 +02001386 srv->proxy->options2 &= ~PR_O2_CHK_ANY;
1387 srv->proxy->options2 |= PR_O2_TCPCHK_CHK;
Christopher Faulete5870d82020-04-15 11:32:03 +02001388
Christopher Faulet61cc8522020-04-20 14:54:42 +02001389 rs = find_tcpcheck_ruleset("*tcp-check");
1390 if (!rs) {
1391 rs = create_tcpcheck_ruleset("*tcp-check");
1392 if (rs == NULL) {
1393 ha_alert("config: %s '%s': out of memory.\n",
1394 proxy_type_str(srv->proxy), srv->proxy->id);
1395 ret |= ERR_ALERT | ERR_FATAL;
1396 goto out;
1397 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001398 }
1399
Christopher Faulet61cc8522020-04-20 14:54:42 +02001400 free_tcpcheck_vars(&rules->preset_vars);
1401 rules->list = &rs->rules;
1402 rules->flags = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001403 }
1404
Christopher Faulet61cc8522020-04-20 14:54:42 +02001405 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1406 if (err) {
1407 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1408 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1409 ret |= ERR_ALERT | ERR_ABORT;
1410 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001411 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001412 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1413 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001414
Christopher Faulet61cc8522020-04-20 14:54:42 +02001415 out:
1416 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001417}
1418
Christopher Faulet61cc8522020-04-20 14:54:42 +02001419/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1420 * if an error occurred.
1421 */
1422static int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001423{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001424 struct tcpcheck_rule *chk;
1425 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001426 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001427
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001428 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001429 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001430
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001431 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001432 * implicit one is inserted before all others.
1433 */
1434 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1435 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1436 chk = calloc(1, sizeof(*chk));
1437 if (!chk) {
1438 ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule"
1439 " to agent-check for server '%s' (out of memory).\n",
1440 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1441 ret |= ERR_ALERT | ERR_FATAL;
1442 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001443 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001444 chk->action = TCPCHK_ACT_CONNECT;
1445 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
1446 LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001447 }
1448
Christopher Faulete5870d82020-04-15 11:32:03 +02001449
Christopher Faulet61cc8522020-04-20 14:54:42 +02001450 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1451 if (err) {
1452 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1453 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1454 ret |= ERR_ALERT | ERR_ABORT;
1455 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001456 }
1457
Christopher Faulet61cc8522020-04-20 14:54:42 +02001458 if (!srv->agent.inter)
1459 srv->agent.inter = srv->check.inter;
1460
1461 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1462 global.maxsock++;
1463
1464 out:
1465 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001466}
1467
Christopher Faulet61cc8522020-04-20 14:54:42 +02001468static void deinit_srv_check(struct server *srv)
1469{
1470 if (srv->check.state & CHK_ST_CONFIGURED)
1471 free_check(&srv->check);
1472 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1473 srv->do_check = 0;
1474}
Christopher Faulete5870d82020-04-15 11:32:03 +02001475
Christopher Faulet61cc8522020-04-20 14:54:42 +02001476
1477static void deinit_srv_agent_check(struct server *srv)
1478{
1479 if (srv->agent.tcpcheck_rules) {
1480 free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars);
1481 free(srv->agent.tcpcheck_rules);
1482 srv->agent.tcpcheck_rules = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001483 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001484
Christopher Faulet61cc8522020-04-20 14:54:42 +02001485 if (srv->agent.state & CHK_ST_CONFIGURED)
1486 free_check(&srv->agent);
1487
1488 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1489 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001490}
1491
Willy Tarreaucee013e2020-06-05 11:40:38 +02001492REGISTER_POST_SERVER_CHECK(init_srv_check);
1493REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001494REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001495
Willy Tarreaucee013e2020-06-05 11:40:38 +02001496REGISTER_SERVER_DEINIT(deinit_srv_check);
1497REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001498
Christopher Faulet61cc8522020-04-20 14:54:42 +02001499
1500/**************************************************************************/
1501/************************** Check sample fetches **************************/
1502/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001503
Christopher Faulet61cc8522020-04-20 14:54:42 +02001504static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001505 { /* END */ },
1506}};
1507
1508INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1509
1510
1511/**************************************************************************/
1512/************************ Check's parsing functions ***********************/
1513/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001514/* Parse the "addr" server keyword */
1515static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1516 char **errmsg)
1517{
1518 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001519 int port1, port2, err_code = 0;
1520
1521
1522 if (!*args[*cur_arg+1]) {
1523 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1524 goto error;
1525 }
1526
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001527 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1528 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001529 if (!sk) {
1530 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1531 goto error;
1532 }
1533
William Dauchy1c921cd2021-02-03 22:30:08 +01001534 srv->check.addr = *sk;
1535 /* if agentaddr was never set, we can use addr */
1536 if (!(srv->flags & SRV_F_AGENTADDR))
1537 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001538
1539 out:
1540 return err_code;
1541
1542 error:
1543 err_code |= ERR_ALERT | ERR_FATAL;
1544 goto out;
1545}
1546
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001547/* Parse the "agent-addr" server keyword */
1548static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1549 char **errmsg)
1550{
William Dauchy1c921cd2021-02-03 22:30:08 +01001551 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001552 int err_code = 0;
1553
1554 if (!*(args[*cur_arg+1])) {
1555 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1556 goto error;
1557 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001558 memset(&sk, 0, sizeof(sk));
1559 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001560 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1561 goto error;
1562 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001563 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001564
1565 out:
1566 return err_code;
1567
1568 error:
1569 err_code |= ERR_ALERT | ERR_FATAL;
1570 goto out;
1571}
1572
1573/* Parse the "agent-check" server keyword */
1574static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1575 char **errmsg)
1576{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001577 struct tcpcheck_ruleset *rs = NULL;
1578 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1579 struct tcpcheck_rule *chk;
1580 int err_code = 0;
1581
1582 if (srv->do_agent)
1583 goto out;
1584
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001585 if (!(curpx->cap & PR_CAP_BE)) {
1586 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1587 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1588 return ERR_WARN;
1589 }
1590
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001591 if (!rules) {
1592 rules = calloc(1, sizeof(*rules));
1593 if (!rules) {
1594 memprintf(errmsg, "out of memory.");
1595 goto error;
1596 }
1597 LIST_INIT(&rules->preset_vars);
1598 srv->agent.tcpcheck_rules = rules;
1599 }
1600 rules->list = NULL;
1601 rules->flags = 0;
1602
Christopher Faulet61cc8522020-04-20 14:54:42 +02001603 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001604 if (rs)
1605 goto ruleset_found;
1606
Christopher Faulet61cc8522020-04-20 14:54:42 +02001607 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001608 if (rs == NULL) {
1609 memprintf(errmsg, "out of memory.");
1610 goto error;
1611 }
1612
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001613 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001614 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1615 if (!chk) {
1616 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1617 goto error;
1618 }
1619 chk->index = 0;
1620 LIST_ADDQ(&rs->rules, &chk->list);
1621
1622 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001623 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1624 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001625 if (!chk) {
1626 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1627 goto error;
1628 }
1629 chk->expect.custom = tcpcheck_agent_expect_reply;
1630 chk->index = 1;
1631 LIST_ADDQ(&rs->rules, &chk->list);
1632
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001633 ruleset_found:
1634 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001635 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001636 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001637 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001638
1639 out:
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001640 return 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001641
1642 error:
1643 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001644 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001645 err_code |= ERR_ALERT | ERR_FATAL;
1646 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001647}
1648
1649/* Parse the "agent-inter" server keyword */
1650static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1651 char **errmsg)
1652{
1653 const char *err = NULL;
1654 unsigned int delay;
1655 int err_code = 0;
1656
1657 if (!*(args[*cur_arg+1])) {
1658 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1659 goto error;
1660 }
1661
1662 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1663 if (err == PARSE_TIME_OVER) {
1664 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1665 args[*cur_arg+1], args[*cur_arg], srv->id);
1666 goto error;
1667 }
1668 else if (err == PARSE_TIME_UNDER) {
1669 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1670 args[*cur_arg+1], args[*cur_arg], srv->id);
1671 goto error;
1672 }
1673 else if (err) {
1674 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1675 *err, srv->id);
1676 goto error;
1677 }
1678 if (delay <= 0) {
1679 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1680 delay, args[*cur_arg], srv->id);
1681 goto error;
1682 }
1683 srv->agent.inter = delay;
1684
1685 out:
1686 return err_code;
1687
1688 error:
1689 err_code |= ERR_ALERT | ERR_FATAL;
1690 goto out;
1691}
1692
1693/* Parse the "agent-port" server keyword */
1694static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1695 char **errmsg)
1696{
1697 int err_code = 0;
1698
1699 if (!*(args[*cur_arg+1])) {
1700 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1701 goto error;
1702 }
1703
1704 global.maxsock++;
William Dauchy4858fb22021-02-03 22:30:09 +01001705 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001706
1707 out:
1708 return err_code;
1709
1710 error:
1711 err_code |= ERR_ALERT | ERR_FATAL;
1712 goto out;
1713}
1714
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001715int set_srv_agent_send(struct server *srv, const char *send)
1716{
1717 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1718 struct tcpcheck_var *var = NULL;
1719 char *str;
1720
1721 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02001722 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001723 if (str == NULL || var == NULL)
1724 goto error;
1725
1726 free_tcpcheck_vars(&rules->preset_vars);
1727
1728 var->data.type = SMP_T_STR;
1729 var->data.u.str.area = str;
1730 var->data.u.str.data = strlen(str);
1731 LIST_INIT(&var->list);
1732 LIST_ADDQ(&rules->preset_vars, &var->list);
1733
1734 return 1;
1735
1736 error:
1737 free(str);
1738 free(var);
1739 return 0;
1740}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001741
William Dauchyf4300902021-02-06 20:47:50 +01001742/* set agent addr and appropriate flag */
William Dauchy1c921cd2021-02-03 22:30:08 +01001743inline void set_srv_agent_addr(struct server *srv, struct sockaddr_storage *sk)
1744{
1745 srv->agent.addr = *sk;
1746 srv->flags |= SRV_F_AGENTADDR;
1747}
1748
William Dauchyf4300902021-02-06 20:47:50 +01001749/* set agent port and appropriate flag */
William Dauchy4858fb22021-02-03 22:30:09 +01001750inline void set_srv_agent_port(struct server *srv, int port)
1751{
1752 srv->agent.port = port;
1753 srv->flags |= SRV_F_AGENTPORT;
1754}
1755
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001756/* Parse the "agent-send" server keyword */
1757static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1758 char **errmsg)
1759{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001760 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001761 int err_code = 0;
1762
1763 if (!*(args[*cur_arg+1])) {
1764 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
1765 goto error;
1766 }
1767
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001768 if (!rules) {
1769 rules = calloc(1, sizeof(*rules));
1770 if (!rules) {
1771 memprintf(errmsg, "out of memory.");
1772 goto error;
1773 }
1774 LIST_INIT(&rules->preset_vars);
1775 srv->agent.tcpcheck_rules = rules;
1776 }
1777
1778 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001779 memprintf(errmsg, "out of memory.");
1780 goto error;
1781 }
1782
1783 out:
1784 return err_code;
1785
1786 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001787 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001788 err_code |= ERR_ALERT | ERR_FATAL;
1789 goto out;
1790}
1791
1792/* Parse the "no-agent-send" server keyword */
1793static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1794 char **errmsg)
1795{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001796 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001797 return 0;
1798}
1799
Christopher Fauletce8111e2020-04-06 15:04:11 +02001800/* Parse the "check" server keyword */
1801static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1802 char **errmsg)
1803{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001804 if (!(curpx->cap & PR_CAP_BE)) {
1805 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1806 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1807 return ERR_WARN;
1808 }
1809
Christopher Fauletce8111e2020-04-06 15:04:11 +02001810 srv->do_check = 1;
1811 return 0;
1812}
1813
1814/* Parse the "check-send-proxy" server keyword */
1815static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1816 char **errmsg)
1817{
1818 srv->check.send_proxy = 1;
1819 return 0;
1820}
1821
1822/* Parse the "check-via-socks4" server keyword */
1823static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1824 char **errmsg)
1825{
1826 srv->check.via_socks4 = 1;
1827 return 0;
1828}
1829
1830/* Parse the "no-check" server keyword */
1831static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1832 char **errmsg)
1833{
1834 deinit_srv_check(srv);
1835 return 0;
1836}
1837
1838/* Parse the "no-check-send-proxy" server keyword */
1839static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1840 char **errmsg)
1841{
1842 srv->check.send_proxy = 0;
1843 return 0;
1844}
1845
Christopher Fauletedc6ed92020-04-23 16:27:59 +02001846/* parse the "check-proto" server keyword */
1847static int srv_parse_check_proto(char **args, int *cur_arg,
1848 struct proxy *px, struct server *newsrv, char **err)
1849{
1850 int err_code = 0;
1851
1852 if (!*args[*cur_arg + 1]) {
1853 memprintf(err, "'%s' : missing value", args[*cur_arg]);
1854 goto error;
1855 }
1856 newsrv->check.mux_proto = get_mux_proto(ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1])));
1857 if (!newsrv->check.mux_proto) {
1858 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
1859 goto error;
1860 }
1861
1862 out:
1863 return err_code;
1864
1865 error:
1866 err_code |= ERR_ALERT | ERR_FATAL;
1867 goto out;
1868}
1869
1870
Christopher Fauletce8111e2020-04-06 15:04:11 +02001871/* Parse the "rise" server keyword */
1872static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1873 char **errmsg)
1874{
1875 int err_code = 0;
1876
1877 if (!*args[*cur_arg + 1]) {
1878 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
1879 goto error;
1880 }
1881
1882 srv->check.rise = atol(args[*cur_arg+1]);
1883 if (srv->check.rise <= 0) {
1884 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
1885 goto error;
1886 }
1887
1888 if (srv->check.health)
1889 srv->check.health = srv->check.rise;
1890
1891 out:
1892 return err_code;
1893
1894 error:
1895 deinit_srv_agent_check(srv);
1896 err_code |= ERR_ALERT | ERR_FATAL;
1897 goto out;
1898 return 0;
1899}
1900
1901/* Parse the "fall" server keyword */
1902static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1903 char **errmsg)
1904{
1905 int err_code = 0;
1906
1907 if (!*args[*cur_arg + 1]) {
1908 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
1909 goto error;
1910 }
1911
1912 srv->check.fall = atol(args[*cur_arg+1]);
1913 if (srv->check.fall <= 0) {
1914 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
1915 goto error;
1916 }
1917
1918 out:
1919 return err_code;
1920
1921 error:
1922 deinit_srv_agent_check(srv);
1923 err_code |= ERR_ALERT | ERR_FATAL;
1924 goto out;
1925 return 0;
1926}
1927
1928/* Parse the "inter" server keyword */
1929static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1930 char **errmsg)
1931{
1932 const char *err = NULL;
1933 unsigned int delay;
1934 int err_code = 0;
1935
1936 if (!*(args[*cur_arg+1])) {
1937 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1938 goto error;
1939 }
1940
1941 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1942 if (err == PARSE_TIME_OVER) {
1943 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1944 args[*cur_arg+1], args[*cur_arg], srv->id);
1945 goto error;
1946 }
1947 else if (err == PARSE_TIME_UNDER) {
1948 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1949 args[*cur_arg+1], args[*cur_arg], srv->id);
1950 goto error;
1951 }
1952 else if (err) {
1953 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1954 *err, srv->id);
1955 goto error;
1956 }
1957 if (delay <= 0) {
1958 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1959 delay, args[*cur_arg], srv->id);
1960 goto error;
1961 }
1962 srv->check.inter = delay;
1963
1964 out:
1965 return err_code;
1966
1967 error:
1968 err_code |= ERR_ALERT | ERR_FATAL;
1969 goto out;
1970}
1971
1972
1973/* Parse the "fastinter" server keyword */
1974static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1975 char **errmsg)
1976{
1977 const char *err = NULL;
1978 unsigned int delay;
1979 int err_code = 0;
1980
1981 if (!*(args[*cur_arg+1])) {
1982 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1983 goto error;
1984 }
1985
1986 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1987 if (err == PARSE_TIME_OVER) {
1988 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1989 args[*cur_arg+1], args[*cur_arg], srv->id);
1990 goto error;
1991 }
1992 else if (err == PARSE_TIME_UNDER) {
1993 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1994 args[*cur_arg+1], args[*cur_arg], srv->id);
1995 goto error;
1996 }
1997 else if (err) {
1998 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1999 *err, srv->id);
2000 goto error;
2001 }
2002 if (delay <= 0) {
2003 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2004 delay, args[*cur_arg], srv->id);
2005 goto error;
2006 }
2007 srv->check.fastinter = delay;
2008
2009 out:
2010 return err_code;
2011
2012 error:
2013 err_code |= ERR_ALERT | ERR_FATAL;
2014 goto out;
2015}
2016
2017
2018/* Parse the "downinter" server keyword */
2019static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2020 char **errmsg)
2021{
2022 const char *err = NULL;
2023 unsigned int delay;
2024 int err_code = 0;
2025
2026 if (!*(args[*cur_arg+1])) {
2027 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2028 goto error;
2029 }
2030
2031 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2032 if (err == PARSE_TIME_OVER) {
2033 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2034 args[*cur_arg+1], args[*cur_arg], srv->id);
2035 goto error;
2036 }
2037 else if (err == PARSE_TIME_UNDER) {
2038 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2039 args[*cur_arg+1], args[*cur_arg], srv->id);
2040 goto error;
2041 }
2042 else if (err) {
2043 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2044 *err, srv->id);
2045 goto error;
2046 }
2047 if (delay <= 0) {
2048 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2049 delay, args[*cur_arg], srv->id);
2050 goto error;
2051 }
2052 srv->check.downinter = delay;
2053
2054 out:
2055 return err_code;
2056
2057 error:
2058 err_code |= ERR_ALERT | ERR_FATAL;
2059 goto out;
2060}
2061
2062/* Parse the "port" server keyword */
2063static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2064 char **errmsg)
2065{
2066 int err_code = 0;
2067
2068 if (!*(args[*cur_arg+1])) {
2069 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2070 goto error;
2071 }
2072
2073 global.maxsock++;
2074 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002075 /* if agentport was never set, we can use port */
2076 if (!(srv->flags & SRV_F_AGENTPORT))
2077 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002078
2079 out:
2080 return err_code;
2081
2082 error:
2083 err_code |= ERR_ALERT | ERR_FATAL;
2084 goto out;
2085}
2086
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002087static struct srv_kw_list srv_kws = { "CHK", { }, {
Christopher Fauletce8111e2020-04-06 15:04:11 +02002088 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002089 { "agent-addr", srv_parse_agent_addr, 1, 1 }, /* Enable an auxiliary agent check */
2090 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable agent checks */
2091 { "agent-inter", srv_parse_agent_inter, 1, 1 }, /* Set the interval between two agent checks */
2092 { "agent-port", srv_parse_agent_port, 1, 1 }, /* Set the TCP port used for agent checks. */
2093 { "agent-send", srv_parse_agent_send, 1, 1 }, /* Set string to send to agent. */
Christopher Fauletce8111e2020-04-06 15:04:11 +02002094 { "check", srv_parse_check, 0, 1 }, /* Enable health checks */
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002095 { "check-proto", srv_parse_check_proto, 1, 1 }, /* Set the mux protocol for health checks */
Christopher Fauletce8111e2020-04-06 15:04:11 +02002096 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* Enable PROXY protocol for health checks */
2097 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* Enable socks4 proxy for health checks */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002098 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Christopher Fauletce8111e2020-04-06 15:04:11 +02002099 { "no-check", srv_parse_no_check, 0, 1 }, /* Disable health checks */
William Dauchyf4300902021-02-06 20:47:50 +01002100 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* Disable PROXY protocol for health checks */
Christopher Fauletce8111e2020-04-06 15:04:11 +02002101 { "rise", srv_parse_check_rise, 1, 1 }, /* Set rise value for health checks */
2102 { "fall", srv_parse_check_fall, 1, 1 }, /* Set fall value for health checks */
2103 { "inter", srv_parse_check_inter, 1, 1 }, /* Set inter value for health checks */
2104 { "fastinter", srv_parse_check_fastinter, 1, 1 }, /* Set fastinter value for health checks */
2105 { "downinter", srv_parse_check_downinter, 1, 1 }, /* Set downinter value for health checks */
2106 { "port", srv_parse_check_port, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002107 { NULL, NULL, 0 },
2108}};
2109
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002110INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002111
Willy Tarreaubd741542010-03-16 18:46:54 +01002112/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002113 * Local variables:
2114 * c-indent-level: 8
2115 * c-basic-offset: 8
2116 * End:
2117 */