blob: 4f45c239333c43983112112c60796cf76df89899 [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>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020039#include <haproxy/dns.h>
Christopher Fauletb381a502020-11-25 13:47:00 +010040#include <haproxy/dynbuf-t.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020041#include <haproxy/extcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/fd.h>
43#include <haproxy/global.h>
44#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020045#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020046#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020047#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/istbuf.h>
49#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020050#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/mailers.h>
52#include <haproxy/port_range.h>
53#include <haproxy/proto_tcp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020054#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020055#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020056#include <haproxy/queue.h>
57#include <haproxy/regex.h>
58#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 */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200181const char *get_check_status_info(short check_status) {
182
183 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
407 * enabled.
408 */
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
447 switch (s->onerror) {
448 case HANA_ONERR_FASTINTER:
449 /* force fastinter - nothing to do here as all modes force it */
450 break;
451
452 case HANA_ONERR_SUDDTH:
453 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900454 if (s->check.health > s->check.rise)
455 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100456
Tim Duesterhus588b3142020-05-29 14:35:51 +0200457 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100458
459 case HANA_ONERR_FAILCHK:
460 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200461 set_server_check_status(&s->check, HCHK_STATUS_HANA,
462 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200463 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100464 break;
465
466 case HANA_ONERR_MARKDWN:
467 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900468 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200469 set_server_check_status(&s->check, HCHK_STATUS_HANA,
470 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200471 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100472 break;
473
474 default:
475 /* write a warning? */
476 break;
477 }
478
479 s->consecutive_errors = 0;
Olivier Houchard7059c552019-03-08 18:49:32 +0100480 _HA_ATOMIC_ADD(&s->counters.failed_hana, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100481
Simon Horman66183002013-02-23 10:16:43 +0900482 if (s->check.fastinter) {
483 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300484 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200485 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300486 /* requeue check task with new expire */
487 task_queue(s->check.task);
488 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100489 }
Willy Tarreauef781042010-01-27 11:53:01 +0100490}
491
Christopher Faulet61cc8522020-04-20 14:54:42 +0200492/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100493 * closed, keep errno intact as it is supposed to contain the valid error code.
494 * If no error is reported, check the socket's error queue using getsockopt().
495 * Warning, this must be done only once when returning from poll, and never
496 * after an I/O error was attempted, otherwise the error queue might contain
497 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
498 * socket. Returns non-zero if an error was reported, zero if everything is
499 * clean (including a properly closed socket).
500 */
501static int retrieve_errno_from_socket(struct connection *conn)
502{
503 int skerr;
504 socklen_t lskerr = sizeof(skerr);
505
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100506 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100507 return 1;
508
Willy Tarreau3c728722014-01-23 13:50:42 +0100509 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100510 return 0;
511
Willy Tarreau585744b2017-08-24 14:31:19 +0200512 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100513 errno = skerr;
514
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100515 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100516
517 if (!errno) {
518 /* we could not retrieve an error, that does not mean there is
519 * none. Just don't change anything and only report the prior
520 * error if any.
521 */
522 if (conn->flags & CO_FL_ERROR)
523 return 1;
524 else
525 return 0;
526 }
527
528 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
529 return 1;
530}
531
Christopher Faulet61cc8522020-04-20 14:54:42 +0200532/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100533 * and adjust the server status accordingly. It may make use of <errno_bck>
534 * if non-null when the caller is absolutely certain of its validity (eg:
535 * checked just after a syscall). If the caller doesn't have a valid errno,
536 * it can pass zero, and retrieve_errno_from_socket() will be called to try
537 * to extract errno from the socket. If no error is reported, it will consider
538 * the <expired> flag. This is intended to be used when a connection error was
539 * reported in conn->flags or when a timeout was reported in <expired>. The
540 * function takes care of not updating a server status which was already set.
541 * All situations where at least one of <expired> or CO_FL_ERROR are set
542 * produce a status.
543 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200544void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100545{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200546 struct conn_stream *cs = check->cs;
547 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100548 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200549 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200550 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100551
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100552 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100553 return;
554
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100555 errno = unclean_errno(errno_bck);
556 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100557 retrieve_errno_from_socket(conn);
558
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200559 if (conn && !(conn->flags & CO_FL_ERROR) &&
560 !(cs->flags & CS_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100561 return;
562
563 /* we'll try to build a meaningful error message depending on the
564 * context of the error possibly present in conn->err_code, and the
565 * socket error possibly collected above. This is useful to know the
566 * exact step of the L6 layer (eg: SSL handshake).
567 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200568 chk = get_trash_chunk();
569
Christopher Faulet799f3a42020-04-07 12:06:14 +0200570 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200571 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200572 step = tcpcheck_get_step_id(check, NULL);
Willy Tarreau213c6782014-10-02 14:51:02 +0200573 if (!step)
574 chunk_printf(chk, " at initial connection step of tcp-check");
575 else {
576 chunk_printf(chk, " at step %d of tcp-check", step);
577 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200578 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
579 if (check->current_step->connect.port)
580 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200581 else
582 chunk_appendf(chk, " (connect)");
583 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200584 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
585 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100586
587 switch (expect->type) {
588 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200589 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100590 break;
591 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200592 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100593 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200594 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200595 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100596 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200597 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100598 chunk_appendf(chk, " (expect binary regex)");
599 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200600 case TCPCHK_EXPECT_STRING_LF:
601 chunk_appendf(chk, " (expect log-format string)");
602 break;
603 case TCPCHK_EXPECT_BINARY_LF:
604 chunk_appendf(chk, " (expect log-format binary)");
605 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200606 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200607 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200608 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200609 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200610 chunk_appendf(chk, " (expect HTTP status regex)");
611 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200612 case TCPCHK_EXPECT_HTTP_HEADER:
613 chunk_appendf(chk, " (expect HTTP header pattern)");
614 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200615 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200616 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200617 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200618 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200619 chunk_appendf(chk, " (expect HTTP body regex)");
620 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200621 case TCPCHK_EXPECT_HTTP_BODY_LF:
622 chunk_appendf(chk, " (expect log-format HTTP body)");
623 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200624 case TCPCHK_EXPECT_CUSTOM:
625 chunk_appendf(chk, " (expect custom function)");
626 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100627 case TCPCHK_EXPECT_UNDEF:
628 chunk_appendf(chk, " (undefined expect!)");
629 break;
630 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200631 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200632 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200633 chunk_appendf(chk, " (send)");
634 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200635
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200636 if (check->current_step && check->current_step->comment)
637 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200638 }
639 }
640
Willy Tarreau00149122017-10-04 18:05:01 +0200641 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100642 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200643 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
644 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100645 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200646 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
647 chk->area);
648 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100649 }
650 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100651 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200652 chunk_printf(&trash, "%s%s", strerror(errno),
653 chk->area);
654 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100655 }
656 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200657 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100658 }
659 }
660
Willy Tarreau00149122017-10-04 18:05:01 +0200661 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200662 /* NOTE: this is reported after <fall> tries */
663 chunk_printf(chk, "No port available for the TCP connection");
664 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
665 }
666
Christopher Faulet5e293762020-10-26 11:10:49 +0100667 if (!conn || !conn->ctrl) {
668 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200669 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
670 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100671 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100672 /* L4 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200673 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100674 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
675 else if (expired)
676 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200677
678 /*
679 * might be due to a server IP change.
680 * Let's trigger a DNS resolution if none are currently running.
681 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100682 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100683 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200684
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100685 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100686 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100687 /* L6 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200688 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100689 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
690 else if (expired)
691 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
692 }
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200693 else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100694 /* I/O error after connection was established and before we could diagnose */
695 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
696 }
697 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200698 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
699
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100700 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200701 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
702 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200703 tout = check->current_step->expect.tout_status;
704 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100705 }
706
707 return;
708}
709
Simon Horman98637e52014-06-20 12:30:16 +0900710
Christopher Faulet61cc8522020-04-20 14:54:42 +0200711/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200712int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900713{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200714 int sv_state;
715 int ratio;
716 char addr[46];
717 char port[6];
718 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
719 "UP %d/%d", "UP",
720 "NOLB %d/%d", "NOLB",
721 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900722
Christopher Faulet61cc8522020-04-20 14:54:42 +0200723 if (!(s->check.state & CHK_ST_ENABLED))
724 sv_state = 6;
725 else if (s->cur_state != SRV_ST_STOPPED) {
726 if (s->check.health == s->check.rise + s->check.fall - 1)
727 sv_state = 3; /* UP */
728 else
729 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900730
Christopher Faulet61cc8522020-04-20 14:54:42 +0200731 if (s->cur_state == SRV_ST_STOPPING)
732 sv_state += 2;
733 } else {
734 if (s->check.health)
735 sv_state = 1; /* going up */
736 else
737 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900738 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200739
Christopher Faulet61cc8522020-04-20 14:54:42 +0200740 chunk_appendf(buf, srv_hlt_st[sv_state],
741 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
742 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200743
Christopher Faulet61cc8522020-04-20 14:54:42 +0200744 addr_to_str(&s->addr, addr, sizeof(addr));
745 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
746 snprintf(port, sizeof(port), "%u", s->svc_port);
747 else
748 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200749
Christopher Faulet61cc8522020-04-20 14:54:42 +0200750 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
751 addr, port, s->proxy->id, s->id,
752 global.node,
753 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
754 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
755 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
756 s->nbpend);
Willy Tarreau9f6dc722019-03-01 11:15:10 +0100757
Christopher Faulet61cc8522020-04-20 14:54:42 +0200758 if ((s->cur_state == SRV_ST_STARTING) &&
759 now.tv_sec < s->last_change + s->slowstart &&
760 now.tv_sec >= s->last_change) {
761 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
762 chunk_appendf(buf, "; throttle=%d%%", ratio);
763 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +0200764
Christopher Faulet61cc8522020-04-20 14:54:42 +0200765 return b_data(buf);
766}
Christopher Fauletaaae9a02020-04-26 09:50:31 +0200767
Willy Tarreau51cd5952020-06-05 12:25:38 +0200768/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +0200769/***************** Health-checks based on connections *********************/
770/**************************************************************************/
771/* This function is used only for server health-checks. It handles connection
772 * status updates including errors. If necessary, it wakes the check task up.
773 * It returns 0 on normal cases, <0 if at least one close() has happened on the
774 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +0200775 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200776static int wake_srv_chk(struct conn_stream *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200777{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200778 struct connection *conn = cs->conn;
779 struct check *check = cs->data;
780 struct email_alertq *q = container_of(check, typeof(*q), check);
781 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200782
Willy Tarreau51cd5952020-06-05 12:25:38 +0200783 if (check->server)
784 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
785 else
786 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200787
Willy Tarreau51cd5952020-06-05 12:25:38 +0200788 /* we may have to make progress on the TCP checks */
789 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +0200790
Willy Tarreau51cd5952020-06-05 12:25:38 +0200791 cs = check->cs;
792 conn = cs->conn;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200793
Willy Tarreau51cd5952020-06-05 12:25:38 +0200794 if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) {
795 /* We may get error reports bypassing the I/O handlers, typically
796 * the case when sending a pure TCP check which fails, then the I/O
797 * handlers above are not called. This is completely handled by the
798 * main processing task so let's simply wake it up. If we get here,
799 * we expect errno to still be valid.
800 */
801 chk_report_conn_err(check, errno, 0);
802 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200803 }
804
Christopher Faulet8f100422021-01-18 15:47:03 +0100805 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Willy Tarreau51cd5952020-06-05 12:25:38 +0200806 /* Check complete or aborted. If connection not yet closed do it
807 * now and wake the check task up to be sure the result is
808 * handled ASAP. */
Willy Tarreau30bd4ef2020-12-11 11:09:29 +0100809 cs_drain_and_close(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +0200810 ret = -1;
Christopher Faulet8f100422021-01-18 15:47:03 +0100811
812 if (check->wait_list.events)
813 cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
814
Willy Tarreau51cd5952020-06-05 12:25:38 +0200815 /* We may have been scheduled to run, and the
816 * I/O handler expects to have a cs, so remove
817 * the tasklet
818 */
819 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
820 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200821 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200822
Willy Tarreau51cd5952020-06-05 12:25:38 +0200823 if (check->server)
824 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
825 else
826 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200827
Christopher Faulet61cc8522020-04-20 14:54:42 +0200828 return ret;
829}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200830
Willy Tarreau51cd5952020-06-05 12:25:38 +0200831/* This function checks if any I/O is wanted, and if so, attempts to do so */
Willy Tarreau025fc712021-01-29 12:35:24 +0100832struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned short state)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200833{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200834 struct check *check = ctx;
835 struct conn_stream *cs = check->cs;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200836
Willy Tarreau51cd5952020-06-05 12:25:38 +0200837 wake_srv_chk(cs);
838 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200839}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200840
Willy Tarreau51cd5952020-06-05 12:25:38 +0200841/* manages a server health-check that uses a connection. Returns
842 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +0200843 *
844 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +0200845 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +0200846 */
Willy Tarreau025fc712021-01-29 12:35:24 +0100847struct task *process_chk_conn(struct task *t, void *context, unsigned short state)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200848{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200849 struct check *check = context;
850 struct proxy *proxy = check->proxy;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200851 struct conn_stream *cs = check->cs;
852 struct connection *conn = cs_conn(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +0200853 int rv;
854 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +0200855
Willy Tarreau51cd5952020-06-05 12:25:38 +0200856 if (check->server)
857 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
858 if (!(check->state & CHK_ST_INPROGRESS)) {
859 /* no check currently running */
860 if (!expired) /* woke up too early */
861 goto out_unlock;
Willy Tarreauabca5b62013-12-06 14:19:25 +0100862
Willy Tarreau51cd5952020-06-05 12:25:38 +0200863 /* we don't send any health-checks when the proxy is
864 * stopped, the server should not be checked or the check
865 * is disabled.
866 */
867 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreauc3914d42020-09-24 08:39:22 +0200868 proxy->disabled)
Willy Tarreau51cd5952020-06-05 12:25:38 +0200869 goto reschedule;
Christopher Faulet404f9192020-04-09 23:13:54 +0200870
Willy Tarreau51cd5952020-06-05 12:25:38 +0200871 /* we'll initiate a new check */
872 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +0200873
Willy Tarreau51cd5952020-06-05 12:25:38 +0200874 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200875
Willy Tarreau51cd5952020-06-05 12:25:38 +0200876 task_set_affinity(t, tid_bit);
877
878 check->current_step = NULL;
879 tcpcheck_main(check);
880 goto out_unlock;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200881 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200882 else {
883 /* there was a test running.
884 * First, let's check whether there was an uncaught error,
885 * which can happen on connect timeout or error.
886 */
887 if (check->result == CHK_RES_UNKNOWN) {
Christopher Fauletb1bb0692020-11-25 16:47:30 +0100888 /* Here the connection must be defined. Otherwise the
889 * error would have already been detected
890 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200891 if ((conn->flags & CO_FL_ERROR) || cs->flags & CS_FL_ERROR || expired) {
892 chk_report_conn_err(check, 0, expired);
893 }
Christopher Faulet8f100422021-01-18 15:47:03 +0100894 else {
895 if (check->state & CHK_ST_CLOSE_CONN) {
896 cs_destroy(cs);
897 cs = NULL;
898 conn = NULL;
899 check->cs = NULL;
900 check->state &= ~CHK_ST_CLOSE_CONN;
901 tcpcheck_main(check);
902 }
903 if (check->result == CHK_RES_UNKNOWN)
904 goto out_unlock; /* timeout not reached, wait again */
905 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200906 }
Christopher Faulet404f9192020-04-09 23:13:54 +0200907
Willy Tarreau51cd5952020-06-05 12:25:38 +0200908 /* check complete or aborted */
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200909
Willy Tarreau51cd5952020-06-05 12:25:38 +0200910 check->current_step = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200911
Willy Tarreau51cd5952020-06-05 12:25:38 +0200912 if (conn && conn->xprt) {
913 /* The check was aborted and the connection was not yet closed.
914 * This can happen upon timeout, or when an external event such
915 * as a failed response coupled with "observe layer7" caused the
916 * server state to be suddenly changed.
917 */
Willy Tarreau30bd4ef2020-12-11 11:09:29 +0100918 cs_drain_and_close(cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200919 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200920
Willy Tarreau51cd5952020-06-05 12:25:38 +0200921 if (cs) {
922 if (check->wait_list.events)
923 cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
924 /* We may have been scheduled to run, and the
925 * I/O handler expects to have a cs, so remove
926 * the tasklet
927 */
928 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
929 cs_destroy(cs);
930 cs = check->cs = NULL;
931 conn = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200932 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200933
934 if (check->sess != NULL) {
935 vars_prune(&check->vars, check->sess, NULL);
936 session_free(check->sess);
937 check->sess = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200938 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200939
940 if (check->server) {
941 if (check->result == CHK_RES_FAILED) {
942 /* a failure or timeout detected */
943 check_notify_failure(check);
944 }
945 else if (check->result == CHK_RES_CONDPASS) {
946 /* check is OK but asks for stopping mode */
947 check_notify_stopping(check);
948 }
949 else if (check->result == CHK_RES_PASSED) {
950 /* a success was detected */
951 check_notify_success(check);
952 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200953 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200954 task_set_affinity(t, MAX_THREADS_MASK);
Christopher Fauletb381a502020-11-25 13:47:00 +0100955 check_release_buf(check, &check->bi);
956 check_release_buf(check, &check->bo);
957 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200958
Willy Tarreau51cd5952020-06-05 12:25:38 +0200959 if (check->server) {
960 rv = 0;
961 if (global.spread_checks > 0) {
962 rv = srv_getinter(check) * global.spread_checks / 100;
963 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +0200964 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200965 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +0200966 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200967 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200968
969 reschedule:
970 while (tick_is_expired(t->expire, now_ms))
971 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
972 out_unlock:
973 if (check->server)
974 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
975 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200976}
977
Willy Tarreau51cd5952020-06-05 12:25:38 +0200978
Christopher Faulet61cc8522020-04-20 14:54:42 +0200979/**************************************************************************/
980/************************** Init/deinit checks ****************************/
981/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +0100982/*
983 * Tries to grab a buffer and to re-enables processing on check <target>. The
984 * check flags are used to figure what buffer was requested. It returns 1 if the
985 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
986 * impossible to wake up and we prefer to be woken up later.
987 */
988int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200989{
Christopher Fauletb381a502020-11-25 13:47:00 +0100990 struct check *check = target;
991
992 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc_margin(&check->bi, 0)) {
993 check->state &= ~CHK_ST_IN_ALLOC;
994 tasklet_wakeup(check->wait_list.tasklet);
995 return 1;
996 }
997 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc_margin(&check->bo, 0)) {
998 check->state &= ~CHK_ST_OUT_ALLOC;
999 tasklet_wakeup(check->wait_list.tasklet);
1000 return 1;
1001 }
1002
1003 return 0;
1004}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001005
Christopher Fauletb381a502020-11-25 13:47:00 +01001006/*
William Dauchyf4300902021-02-06 20:47:50 +01001007 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001008 */
1009struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1010{
1011 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001012
Christopher Fauletb381a502020-11-25 13:47:00 +01001013 if (likely(!MT_LIST_ADDED(&check->buf_wait.list)) &&
1014 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
1015 check->buf_wait.target = check;
1016 check->buf_wait.wakeup_cb = check_buf_available;
1017 MT_LIST_ADDQ(&buffer_wq, &check->buf_wait.list);
1018 }
1019 return buf;
1020}
1021
1022/*
1023 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1024 * wait queue.
1025 */
1026void check_release_buf(struct check *check, struct buffer *bptr)
1027{
1028 if (bptr->size) {
1029 b_free(bptr);
1030 offer_buffers(check->buf_wait.target, tasks_run_queue);
1031 }
1032}
1033
1034const char *init_check(struct check *check, int type)
1035{
1036 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001037
Christopher Fauletb381a502020-11-25 13:47:00 +01001038 check->bi = BUF_NULL;
1039 check->bo = BUF_NULL;
1040 MT_LIST_INIT(&check->buf_wait.list);
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001041
Christopher Faulet61cc8522020-04-20 14:54:42 +02001042 check->wait_list.tasklet = tasklet_new();
1043 if (!check->wait_list.tasklet)
1044 return "out of memory while allocating check tasklet";
1045 check->wait_list.events = 0;
1046 check->wait_list.tasklet->process = event_srv_chk_io;
1047 check->wait_list.tasklet->context = check;
1048 return NULL;
1049}
1050
1051void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001052{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001053 task_destroy(check->task);
1054 if (check->wait_list.tasklet)
1055 tasklet_free(check->wait_list.tasklet);
1056
Christopher Fauletb381a502020-11-25 13:47:00 +01001057 check_release_buf(check, &check->bi);
1058 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001059 if (check->cs) {
1060 free(check->cs->conn);
1061 check->cs->conn = NULL;
1062 cs_free(check->cs);
1063 check->cs = NULL;
1064 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001065}
1066
Christopher Faulet61cc8522020-04-20 14:54:42 +02001067/* manages a server health-check. Returns the time the task accepts to wait, or
1068 * TIME_ETERNITY for infinity.
1069 */
Willy Tarreaucee013e2020-06-05 11:40:38 +02001070struct task *process_chk(struct task *t, void *context, unsigned short state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001071{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001072 struct check *check = context;
1073
1074 if (check->type == PR_O2_EXT_CHK)
1075 return process_chk_proc(t, context, state);
1076 return process_chk_conn(t, context, state);
1077
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001078}
1079
Christopher Faulet61cc8522020-04-20 14:54:42 +02001080
1081static int start_check_task(struct check *check, int mininter,
1082 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001083{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001084 struct task *t;
1085 unsigned long thread_mask = MAX_THREADS_MASK;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001086
Christopher Faulet61cc8522020-04-20 14:54:42 +02001087 if (check->type == PR_O2_EXT_CHK)
1088 thread_mask = 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001089
Christopher Faulet61cc8522020-04-20 14:54:42 +02001090 /* task for the check */
1091 if ((t = task_new(thread_mask)) == NULL) {
1092 ha_alert("Starting [%s:%s] check: out of memory.\n",
1093 check->server->proxy->id, check->server->id);
1094 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001095 }
1096
Christopher Faulet61cc8522020-04-20 14:54:42 +02001097 check->task = t;
1098 t->process = process_chk;
1099 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001100
Christopher Faulet61cc8522020-04-20 14:54:42 +02001101 if (mininter < srv_getinter(check))
1102 mininter = srv_getinter(check);
1103
1104 if (global.max_spread_checks && mininter > global.max_spread_checks)
1105 mininter = global.max_spread_checks;
1106
1107 /* check this every ms */
1108 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1109 check->start = now;
1110 task_queue(t);
1111
1112 return 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001113}
1114
Christopher Faulet61cc8522020-04-20 14:54:42 +02001115/* updates the server's weight during a warmup stage. Once the final weight is
1116 * reached, the task automatically stops. Note that any server status change
1117 * must have updated s->last_change accordingly.
1118 */
Willy Tarreau025fc712021-01-29 12:35:24 +01001119struct task *server_warmup(struct task *t, void *context, unsigned short state)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001120{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001121 struct server *s = context;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001122
Christopher Faulet61cc8522020-04-20 14:54:42 +02001123 /* by default, plan on stopping the task */
1124 t->expire = TICK_ETERNITY;
1125 if ((s->next_admin & SRV_ADMF_MAINT) ||
1126 (s->next_state != SRV_ST_STARTING))
1127 return t;
Christopher Faulete5870d82020-04-15 11:32:03 +02001128
Christopher Faulet61cc8522020-04-20 14:54:42 +02001129 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001130
Christopher Faulet61cc8522020-04-20 14:54:42 +02001131 /* recalculate the weights and update the state */
1132 server_recalc_eweight(s, 1);
Christopher Faulet5c288742020-03-31 08:15:58 +02001133
Christopher Faulet61cc8522020-04-20 14:54:42 +02001134 /* probably that we can refill this server with a bit more connections */
1135 pendconn_grab_from_px(s);
Christopher Faulet5c288742020-03-31 08:15:58 +02001136
Christopher Faulet61cc8522020-04-20 14:54:42 +02001137 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5c288742020-03-31 08:15:58 +02001138
Christopher Faulet61cc8522020-04-20 14:54:42 +02001139 /* get back there in 1 second or 1/20th of the slowstart interval,
1140 * whichever is greater, resulting in small 5% steps.
1141 */
1142 if (s->next_state == SRV_ST_STARTING)
1143 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1144 return t;
1145}
1146
1147/*
1148 * Start health-check.
1149 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1150 */
1151static int start_checks()
1152{
1153
1154 struct proxy *px;
1155 struct server *s;
1156 struct task *t;
1157 int nbcheck=0, mininter=0, srvpos=0;
1158
1159 /* 0- init the dummy frontend used to create all checks sessions */
1160 init_new_proxy(&checks_fe);
1161 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1162 checks_fe.mode = PR_MODE_TCP;
1163 checks_fe.maxconn = 0;
1164 checks_fe.conn_retries = CONN_RETRIES;
1165 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1166 checks_fe.timeout.client = TICK_ETERNITY;
1167
1168 /* 1- count the checkers to run simultaneously.
1169 * We also determine the minimum interval among all of those which
1170 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1171 * will be used to spread their start-up date. Those which have
1172 * a shorter interval will start independently and will not dictate
1173 * too short an interval for all others.
1174 */
1175 for (px = proxies_list; px; px = px->next) {
1176 for (s = px->srv; s; s = s->next) {
1177 if (s->slowstart) {
1178 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
1179 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1180 return ERR_ALERT | ERR_FATAL;
1181 }
1182 /* We need a warmup task that will be called when the server
1183 * state switches from down to up.
1184 */
1185 s->warmup = t;
1186 t->process = server_warmup;
1187 t->context = s;
1188 /* server can be in this state only because of */
1189 if (s->next_state == SRV_ST_STARTING)
1190 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 +02001191 }
1192
Christopher Faulet61cc8522020-04-20 14:54:42 +02001193 if (s->check.state & CHK_ST_CONFIGURED) {
1194 nbcheck++;
1195 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1196 (!mininter || mininter > srv_getinter(&s->check)))
1197 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001198 }
1199
Christopher Faulet61cc8522020-04-20 14:54:42 +02001200 if (s->agent.state & CHK_ST_CONFIGURED) {
1201 nbcheck++;
1202 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1203 (!mininter || mininter > srv_getinter(&s->agent)))
1204 mininter = srv_getinter(&s->agent);
1205 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001206 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001207 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001208
Christopher Faulet61cc8522020-04-20 14:54:42 +02001209 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001210 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001211
Christopher Faulet61cc8522020-04-20 14:54:42 +02001212 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001213
William Dauchyf4300902021-02-06 20:47:50 +01001214 /* 2- start them as far as possible from each other. For this, we will
1215 * start them after their interval is set to the min interval divided
1216 * by the number of servers, weighted by the server's position in the
1217 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001218 */
1219 for (px = proxies_list; px; px = px->next) {
1220 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1221 if (init_pid_list()) {
1222 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1223 return ERR_ALERT | ERR_FATAL;
1224 }
1225 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001226
Christopher Faulet61cc8522020-04-20 14:54:42 +02001227 for (s = px->srv; s; s = s->next) {
1228 /* A task for the main check */
1229 if (s->check.state & CHK_ST_CONFIGURED) {
1230 if (s->check.type == PR_O2_EXT_CHK) {
1231 if (!prepare_external_check(&s->check))
1232 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001233 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001234 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1235 return ERR_ALERT | ERR_FATAL;
1236 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001237 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001238
Christopher Faulet61cc8522020-04-20 14:54:42 +02001239 /* A task for a auxiliary agent check */
1240 if (s->agent.state & CHK_ST_CONFIGURED) {
1241 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1242 return ERR_ALERT | ERR_FATAL;
1243 }
1244 srvpos++;
1245 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001246 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001247 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001248 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001249}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001250
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001251
Christopher Faulet61cc8522020-04-20 14:54:42 +02001252/*
1253 * Return value:
1254 * the port to be used for the health check
1255 * 0 in case no port could be found for the check
1256 */
1257static int srv_check_healthcheck_port(struct check *chk)
1258{
1259 int i = 0;
1260 struct server *srv = NULL;
1261
1262 srv = chk->server;
1263
William Dauchyf4300902021-02-06 20:47:50 +01001264 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001265 if (chk->port > 0)
1266 return chk->port;
1267
1268 /* try to get the port from check_core.addr if check.port not set */
1269 i = get_host_port(&chk->addr);
1270 if (i > 0)
1271 return i;
1272
1273 /* try to get the port from server address */
1274 /* prevent MAPPORTS from working at this point, since checks could
1275 * not be performed in such case (MAPPORTS impose a relative ports
1276 * based on live traffic)
1277 */
1278 if (srv->flags & SRV_F_MAPPORTS)
1279 return 0;
1280
1281 i = srv->svc_port; /* by default */
1282 if (i > 0)
1283 return i;
1284
1285 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001286}
1287
Christopher Faulet61cc8522020-04-20 14:54:42 +02001288/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1289 * if an error occurred.
1290 */
1291static int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001292{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001293 const char *err;
1294 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001295 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001296 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001297
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001298 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001299 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001300
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001301 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001302
Christopher Faulet61cc8522020-04-20 14:54:42 +02001303 /* If neither a port nor an addr was specified and no check transport
1304 * layer is forced, then the transport layer used by the checks is the
1305 * same as for the production traffic. Otherwise we use raw_sock by
1306 * default, unless one is specified.
1307 */
1308 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1309 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1310 srv->check.use_ssl = srv->use_ssl;
1311 srv->check.xprt = srv->xprt;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001312 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001313 else if (srv->check.use_ssl == 1)
1314 srv->check.xprt = xprt_get(XPRT_SSL);
1315 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001316 }
Christopher Faulet66163ec2020-05-20 22:36:24 +02001317 else if (srv->check.use_ssl == 1)
1318 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001319
Christopher Faulet12882cf2020-04-23 15:50:18 +02001320 /* Inherit the mux protocol from the server if not already defined for
1321 * the check
1322 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001323 if (srv->mux_proto && !srv->check.mux_proto &&
1324 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1325 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001326 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001327 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001328 /* test that check proto is valid if explicitly defined */
1329 else if (srv->check.mux_proto &&
1330 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1331 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1332 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1333 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1334 ret |= ERR_ALERT | ERR_FATAL;
1335 goto out;
1336 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001337
Christopher Faulet61cc8522020-04-20 14:54:42 +02001338 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001339
Christopher Faulet61cc8522020-04-20 14:54:42 +02001340 /* We need at least a service port, a check port or the first tcp-check
1341 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1342 */
1343 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1344 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1345 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001346
Christopher Faulet61cc8522020-04-20 14:54:42 +02001347 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1348 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1349 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1350 ret |= ERR_ALERT | ERR_ABORT;
1351 goto out;
1352 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001353
Christopher Faulet61cc8522020-04-20 14:54:42 +02001354 /* search the first action (connect / send / expect) in the list */
1355 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1356 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1357 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1358 "nor tcp_check rule 'connect' with port information.\n",
1359 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1360 ret |= ERR_ALERT | ERR_ABORT;
1361 goto out;
1362 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001363
Christopher Faulet61cc8522020-04-20 14:54:42 +02001364 /* scan the tcp-check ruleset to ensure a port has been configured */
1365 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
1366 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) {
1367 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1368 "and a tcp_check rule 'connect' with no port information.\n",
1369 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1370 ret |= ERR_ALERT | ERR_ABORT;
1371 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001372 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001373 }
1374
Christopher Faulet61cc8522020-04-20 14:54:42 +02001375 init:
1376 if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) {
1377 struct tcpcheck_ruleset *rs = NULL;
1378 struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules;
1379 //char *errmsg = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001380
Christopher Faulet61cc8522020-04-20 14:54:42 +02001381 srv->proxy->options2 &= ~PR_O2_CHK_ANY;
1382 srv->proxy->options2 |= PR_O2_TCPCHK_CHK;
Christopher Faulete5870d82020-04-15 11:32:03 +02001383
Christopher Faulet61cc8522020-04-20 14:54:42 +02001384 rs = find_tcpcheck_ruleset("*tcp-check");
1385 if (!rs) {
1386 rs = create_tcpcheck_ruleset("*tcp-check");
1387 if (rs == NULL) {
1388 ha_alert("config: %s '%s': out of memory.\n",
1389 proxy_type_str(srv->proxy), srv->proxy->id);
1390 ret |= ERR_ALERT | ERR_FATAL;
1391 goto out;
1392 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001393 }
1394
Christopher Faulet61cc8522020-04-20 14:54:42 +02001395 free_tcpcheck_vars(&rules->preset_vars);
1396 rules->list = &rs->rules;
1397 rules->flags = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001398 }
1399
Christopher Faulet61cc8522020-04-20 14:54:42 +02001400 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1401 if (err) {
1402 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1403 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1404 ret |= ERR_ALERT | ERR_ABORT;
1405 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001406 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001407 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1408 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001409
Christopher Faulet61cc8522020-04-20 14:54:42 +02001410 out:
1411 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001412}
1413
Christopher Faulet61cc8522020-04-20 14:54:42 +02001414/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1415 * if an error occurred.
1416 */
1417static int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001418{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001419 struct tcpcheck_rule *chk;
1420 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001421 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001422
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001423 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001424 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001425
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001426 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001427 * implicit one is inserted before all others.
1428 */
1429 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1430 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1431 chk = calloc(1, sizeof(*chk));
1432 if (!chk) {
1433 ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule"
1434 " to agent-check for server '%s' (out of memory).\n",
1435 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1436 ret |= ERR_ALERT | ERR_FATAL;
1437 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001438 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001439 chk->action = TCPCHK_ACT_CONNECT;
1440 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
1441 LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001442 }
1443
Christopher Faulete5870d82020-04-15 11:32:03 +02001444
Christopher Faulet61cc8522020-04-20 14:54:42 +02001445 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1446 if (err) {
1447 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1448 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1449 ret |= ERR_ALERT | ERR_ABORT;
1450 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001451 }
1452
Christopher Faulet61cc8522020-04-20 14:54:42 +02001453 if (!srv->agent.inter)
1454 srv->agent.inter = srv->check.inter;
1455
1456 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1457 global.maxsock++;
1458
1459 out:
1460 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001461}
1462
Christopher Faulet61cc8522020-04-20 14:54:42 +02001463static void deinit_srv_check(struct server *srv)
1464{
1465 if (srv->check.state & CHK_ST_CONFIGURED)
1466 free_check(&srv->check);
1467 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1468 srv->do_check = 0;
1469}
Christopher Faulete5870d82020-04-15 11:32:03 +02001470
Christopher Faulet61cc8522020-04-20 14:54:42 +02001471
1472static void deinit_srv_agent_check(struct server *srv)
1473{
1474 if (srv->agent.tcpcheck_rules) {
1475 free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars);
1476 free(srv->agent.tcpcheck_rules);
1477 srv->agent.tcpcheck_rules = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001478 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001479
Christopher Faulet61cc8522020-04-20 14:54:42 +02001480 if (srv->agent.state & CHK_ST_CONFIGURED)
1481 free_check(&srv->agent);
1482
1483 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1484 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001485}
1486
Willy Tarreaucee013e2020-06-05 11:40:38 +02001487REGISTER_POST_SERVER_CHECK(init_srv_check);
1488REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001489REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001490
Willy Tarreaucee013e2020-06-05 11:40:38 +02001491REGISTER_SERVER_DEINIT(deinit_srv_check);
1492REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001493
Christopher Faulet61cc8522020-04-20 14:54:42 +02001494
1495/**************************************************************************/
1496/************************** Check sample fetches **************************/
1497/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001498
Christopher Faulet61cc8522020-04-20 14:54:42 +02001499static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001500 { /* END */ },
1501}};
1502
1503INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1504
1505
1506/**************************************************************************/
1507/************************ Check's parsing functions ***********************/
1508/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001509/* Parse the "addr" server keyword */
1510static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1511 char **errmsg)
1512{
1513 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001514 int port1, port2, err_code = 0;
1515
1516
1517 if (!*args[*cur_arg+1]) {
1518 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1519 goto error;
1520 }
1521
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001522 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1523 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001524 if (!sk) {
1525 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1526 goto error;
1527 }
1528
William Dauchy1c921cd2021-02-03 22:30:08 +01001529 srv->check.addr = *sk;
1530 /* if agentaddr was never set, we can use addr */
1531 if (!(srv->flags & SRV_F_AGENTADDR))
1532 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001533
1534 out:
1535 return err_code;
1536
1537 error:
1538 err_code |= ERR_ALERT | ERR_FATAL;
1539 goto out;
1540}
1541
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001542/* Parse the "agent-addr" server keyword */
1543static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1544 char **errmsg)
1545{
William Dauchy1c921cd2021-02-03 22:30:08 +01001546 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001547 int err_code = 0;
1548
1549 if (!*(args[*cur_arg+1])) {
1550 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1551 goto error;
1552 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001553 memset(&sk, 0, sizeof(sk));
1554 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001555 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1556 goto error;
1557 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001558 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001559
1560 out:
1561 return err_code;
1562
1563 error:
1564 err_code |= ERR_ALERT | ERR_FATAL;
1565 goto out;
1566}
1567
1568/* Parse the "agent-check" server keyword */
1569static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1570 char **errmsg)
1571{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001572 struct tcpcheck_ruleset *rs = NULL;
1573 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1574 struct tcpcheck_rule *chk;
1575 int err_code = 0;
1576
1577 if (srv->do_agent)
1578 goto out;
1579
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001580 if (!(curpx->cap & PR_CAP_BE)) {
1581 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1582 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1583 return ERR_WARN;
1584 }
1585
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001586 if (!rules) {
1587 rules = calloc(1, sizeof(*rules));
1588 if (!rules) {
1589 memprintf(errmsg, "out of memory.");
1590 goto error;
1591 }
1592 LIST_INIT(&rules->preset_vars);
1593 srv->agent.tcpcheck_rules = rules;
1594 }
1595 rules->list = NULL;
1596 rules->flags = 0;
1597
Christopher Faulet61cc8522020-04-20 14:54:42 +02001598 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001599 if (rs)
1600 goto ruleset_found;
1601
Christopher Faulet61cc8522020-04-20 14:54:42 +02001602 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001603 if (rs == NULL) {
1604 memprintf(errmsg, "out of memory.");
1605 goto error;
1606 }
1607
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001608 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001609 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1610 if (!chk) {
1611 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1612 goto error;
1613 }
1614 chk->index = 0;
1615 LIST_ADDQ(&rs->rules, &chk->list);
1616
1617 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001618 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1619 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001620 if (!chk) {
1621 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1622 goto error;
1623 }
1624 chk->expect.custom = tcpcheck_agent_expect_reply;
1625 chk->index = 1;
1626 LIST_ADDQ(&rs->rules, &chk->list);
1627
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001628 ruleset_found:
1629 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001630 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001631 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001632 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001633
1634 out:
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001635 return 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001636
1637 error:
1638 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001639 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001640 err_code |= ERR_ALERT | ERR_FATAL;
1641 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001642}
1643
1644/* Parse the "agent-inter" server keyword */
1645static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1646 char **errmsg)
1647{
1648 const char *err = NULL;
1649 unsigned int delay;
1650 int err_code = 0;
1651
1652 if (!*(args[*cur_arg+1])) {
1653 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1654 goto error;
1655 }
1656
1657 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1658 if (err == PARSE_TIME_OVER) {
1659 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1660 args[*cur_arg+1], args[*cur_arg], srv->id);
1661 goto error;
1662 }
1663 else if (err == PARSE_TIME_UNDER) {
1664 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1665 args[*cur_arg+1], args[*cur_arg], srv->id);
1666 goto error;
1667 }
1668 else if (err) {
1669 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1670 *err, srv->id);
1671 goto error;
1672 }
1673 if (delay <= 0) {
1674 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1675 delay, args[*cur_arg], srv->id);
1676 goto error;
1677 }
1678 srv->agent.inter = delay;
1679
1680 out:
1681 return err_code;
1682
1683 error:
1684 err_code |= ERR_ALERT | ERR_FATAL;
1685 goto out;
1686}
1687
1688/* Parse the "agent-port" server keyword */
1689static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1690 char **errmsg)
1691{
1692 int err_code = 0;
1693
1694 if (!*(args[*cur_arg+1])) {
1695 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1696 goto error;
1697 }
1698
1699 global.maxsock++;
William Dauchy4858fb22021-02-03 22:30:09 +01001700 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001701
1702 out:
1703 return err_code;
1704
1705 error:
1706 err_code |= ERR_ALERT | ERR_FATAL;
1707 goto out;
1708}
1709
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001710int set_srv_agent_send(struct server *srv, const char *send)
1711{
1712 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1713 struct tcpcheck_var *var = NULL;
1714 char *str;
1715
1716 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02001717 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001718 if (str == NULL || var == NULL)
1719 goto error;
1720
1721 free_tcpcheck_vars(&rules->preset_vars);
1722
1723 var->data.type = SMP_T_STR;
1724 var->data.u.str.area = str;
1725 var->data.u.str.data = strlen(str);
1726 LIST_INIT(&var->list);
1727 LIST_ADDQ(&rules->preset_vars, &var->list);
1728
1729 return 1;
1730
1731 error:
1732 free(str);
1733 free(var);
1734 return 0;
1735}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001736
William Dauchyf4300902021-02-06 20:47:50 +01001737/* set agent addr and appropriate flag */
William Dauchy1c921cd2021-02-03 22:30:08 +01001738inline void set_srv_agent_addr(struct server *srv, struct sockaddr_storage *sk)
1739{
1740 srv->agent.addr = *sk;
1741 srv->flags |= SRV_F_AGENTADDR;
1742}
1743
William Dauchyf4300902021-02-06 20:47:50 +01001744/* set agent port and appropriate flag */
William Dauchy4858fb22021-02-03 22:30:09 +01001745inline void set_srv_agent_port(struct server *srv, int port)
1746{
1747 srv->agent.port = port;
1748 srv->flags |= SRV_F_AGENTPORT;
1749}
1750
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001751/* Parse the "agent-send" server keyword */
1752static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1753 char **errmsg)
1754{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001755 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001756 int err_code = 0;
1757
1758 if (!*(args[*cur_arg+1])) {
1759 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
1760 goto error;
1761 }
1762
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001763 if (!rules) {
1764 rules = calloc(1, sizeof(*rules));
1765 if (!rules) {
1766 memprintf(errmsg, "out of memory.");
1767 goto error;
1768 }
1769 LIST_INIT(&rules->preset_vars);
1770 srv->agent.tcpcheck_rules = rules;
1771 }
1772
1773 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001774 memprintf(errmsg, "out of memory.");
1775 goto error;
1776 }
1777
1778 out:
1779 return err_code;
1780
1781 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001782 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001783 err_code |= ERR_ALERT | ERR_FATAL;
1784 goto out;
1785}
1786
1787/* Parse the "no-agent-send" server keyword */
1788static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1789 char **errmsg)
1790{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001791 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001792 return 0;
1793}
1794
Christopher Fauletce8111e2020-04-06 15:04:11 +02001795/* Parse the "check" server keyword */
1796static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1797 char **errmsg)
1798{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001799 if (!(curpx->cap & PR_CAP_BE)) {
1800 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1801 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1802 return ERR_WARN;
1803 }
1804
Christopher Fauletce8111e2020-04-06 15:04:11 +02001805 srv->do_check = 1;
1806 return 0;
1807}
1808
1809/* Parse the "check-send-proxy" server keyword */
1810static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1811 char **errmsg)
1812{
1813 srv->check.send_proxy = 1;
1814 return 0;
1815}
1816
1817/* Parse the "check-via-socks4" server keyword */
1818static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1819 char **errmsg)
1820{
1821 srv->check.via_socks4 = 1;
1822 return 0;
1823}
1824
1825/* Parse the "no-check" server keyword */
1826static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1827 char **errmsg)
1828{
1829 deinit_srv_check(srv);
1830 return 0;
1831}
1832
1833/* Parse the "no-check-send-proxy" server keyword */
1834static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1835 char **errmsg)
1836{
1837 srv->check.send_proxy = 0;
1838 return 0;
1839}
1840
Christopher Fauletedc6ed92020-04-23 16:27:59 +02001841/* parse the "check-proto" server keyword */
1842static int srv_parse_check_proto(char **args, int *cur_arg,
1843 struct proxy *px, struct server *newsrv, char **err)
1844{
1845 int err_code = 0;
1846
1847 if (!*args[*cur_arg + 1]) {
1848 memprintf(err, "'%s' : missing value", args[*cur_arg]);
1849 goto error;
1850 }
1851 newsrv->check.mux_proto = get_mux_proto(ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1])));
1852 if (!newsrv->check.mux_proto) {
1853 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
1854 goto error;
1855 }
1856
1857 out:
1858 return err_code;
1859
1860 error:
1861 err_code |= ERR_ALERT | ERR_FATAL;
1862 goto out;
1863}
1864
1865
Christopher Fauletce8111e2020-04-06 15:04:11 +02001866/* Parse the "rise" server keyword */
1867static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1868 char **errmsg)
1869{
1870 int err_code = 0;
1871
1872 if (!*args[*cur_arg + 1]) {
1873 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
1874 goto error;
1875 }
1876
1877 srv->check.rise = atol(args[*cur_arg+1]);
1878 if (srv->check.rise <= 0) {
1879 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
1880 goto error;
1881 }
1882
1883 if (srv->check.health)
1884 srv->check.health = srv->check.rise;
1885
1886 out:
1887 return err_code;
1888
1889 error:
1890 deinit_srv_agent_check(srv);
1891 err_code |= ERR_ALERT | ERR_FATAL;
1892 goto out;
1893 return 0;
1894}
1895
1896/* Parse the "fall" server keyword */
1897static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1898 char **errmsg)
1899{
1900 int err_code = 0;
1901
1902 if (!*args[*cur_arg + 1]) {
1903 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
1904 goto error;
1905 }
1906
1907 srv->check.fall = atol(args[*cur_arg+1]);
1908 if (srv->check.fall <= 0) {
1909 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
1910 goto error;
1911 }
1912
1913 out:
1914 return err_code;
1915
1916 error:
1917 deinit_srv_agent_check(srv);
1918 err_code |= ERR_ALERT | ERR_FATAL;
1919 goto out;
1920 return 0;
1921}
1922
1923/* Parse the "inter" server keyword */
1924static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1925 char **errmsg)
1926{
1927 const char *err = NULL;
1928 unsigned int delay;
1929 int err_code = 0;
1930
1931 if (!*(args[*cur_arg+1])) {
1932 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1933 goto error;
1934 }
1935
1936 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1937 if (err == PARSE_TIME_OVER) {
1938 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1939 args[*cur_arg+1], args[*cur_arg], srv->id);
1940 goto error;
1941 }
1942 else if (err == PARSE_TIME_UNDER) {
1943 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1944 args[*cur_arg+1], args[*cur_arg], srv->id);
1945 goto error;
1946 }
1947 else if (err) {
1948 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1949 *err, srv->id);
1950 goto error;
1951 }
1952 if (delay <= 0) {
1953 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1954 delay, args[*cur_arg], srv->id);
1955 goto error;
1956 }
1957 srv->check.inter = delay;
1958
1959 out:
1960 return err_code;
1961
1962 error:
1963 err_code |= ERR_ALERT | ERR_FATAL;
1964 goto out;
1965}
1966
1967
1968/* Parse the "fastinter" server keyword */
1969static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1970 char **errmsg)
1971{
1972 const char *err = NULL;
1973 unsigned int delay;
1974 int err_code = 0;
1975
1976 if (!*(args[*cur_arg+1])) {
1977 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1978 goto error;
1979 }
1980
1981 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1982 if (err == PARSE_TIME_OVER) {
1983 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1984 args[*cur_arg+1], args[*cur_arg], srv->id);
1985 goto error;
1986 }
1987 else if (err == PARSE_TIME_UNDER) {
1988 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1989 args[*cur_arg+1], args[*cur_arg], srv->id);
1990 goto error;
1991 }
1992 else if (err) {
1993 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1994 *err, srv->id);
1995 goto error;
1996 }
1997 if (delay <= 0) {
1998 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1999 delay, args[*cur_arg], srv->id);
2000 goto error;
2001 }
2002 srv->check.fastinter = delay;
2003
2004 out:
2005 return err_code;
2006
2007 error:
2008 err_code |= ERR_ALERT | ERR_FATAL;
2009 goto out;
2010}
2011
2012
2013/* Parse the "downinter" server keyword */
2014static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2015 char **errmsg)
2016{
2017 const char *err = NULL;
2018 unsigned int delay;
2019 int err_code = 0;
2020
2021 if (!*(args[*cur_arg+1])) {
2022 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2023 goto error;
2024 }
2025
2026 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2027 if (err == PARSE_TIME_OVER) {
2028 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2029 args[*cur_arg+1], args[*cur_arg], srv->id);
2030 goto error;
2031 }
2032 else if (err == PARSE_TIME_UNDER) {
2033 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2034 args[*cur_arg+1], args[*cur_arg], srv->id);
2035 goto error;
2036 }
2037 else if (err) {
2038 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2039 *err, srv->id);
2040 goto error;
2041 }
2042 if (delay <= 0) {
2043 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2044 delay, args[*cur_arg], srv->id);
2045 goto error;
2046 }
2047 srv->check.downinter = delay;
2048
2049 out:
2050 return err_code;
2051
2052 error:
2053 err_code |= ERR_ALERT | ERR_FATAL;
2054 goto out;
2055}
2056
2057/* Parse the "port" server keyword */
2058static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2059 char **errmsg)
2060{
2061 int err_code = 0;
2062
2063 if (!*(args[*cur_arg+1])) {
2064 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2065 goto error;
2066 }
2067
2068 global.maxsock++;
2069 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002070 /* if agentport was never set, we can use port */
2071 if (!(srv->flags & SRV_F_AGENTPORT))
2072 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002073
2074 out:
2075 return err_code;
2076
2077 error:
2078 err_code |= ERR_ALERT | ERR_FATAL;
2079 goto out;
2080}
2081
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002082static struct srv_kw_list srv_kws = { "CHK", { }, {
Christopher Fauletce8111e2020-04-06 15:04:11 +02002083 { "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 +02002084 { "agent-addr", srv_parse_agent_addr, 1, 1 }, /* Enable an auxiliary agent check */
2085 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable agent checks */
2086 { "agent-inter", srv_parse_agent_inter, 1, 1 }, /* Set the interval between two agent checks */
2087 { "agent-port", srv_parse_agent_port, 1, 1 }, /* Set the TCP port used for agent checks. */
2088 { "agent-send", srv_parse_agent_send, 1, 1 }, /* Set string to send to agent. */
Christopher Fauletce8111e2020-04-06 15:04:11 +02002089 { "check", srv_parse_check, 0, 1 }, /* Enable health checks */
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002090 { "check-proto", srv_parse_check_proto, 1, 1 }, /* Set the mux protocol for health checks */
Christopher Fauletce8111e2020-04-06 15:04:11 +02002091 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* Enable PROXY protocol for health checks */
2092 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* Enable socks4 proxy for health checks */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002093 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Christopher Fauletce8111e2020-04-06 15:04:11 +02002094 { "no-check", srv_parse_no_check, 0, 1 }, /* Disable health checks */
William Dauchyf4300902021-02-06 20:47:50 +01002095 { "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 +02002096 { "rise", srv_parse_check_rise, 1, 1 }, /* Set rise value for health checks */
2097 { "fall", srv_parse_check_fall, 1, 1 }, /* Set fall value for health checks */
2098 { "inter", srv_parse_check_inter, 1, 1 }, /* Set inter value for health checks */
2099 { "fastinter", srv_parse_check_fastinter, 1, 1 }, /* Set fastinter value for health checks */
2100 { "downinter", srv_parse_check_downinter, 1, 1 }, /* Set downinter value for health checks */
2101 { "port", srv_parse_check_port, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002102 { NULL, NULL, 0 },
2103}};
2104
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002105INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002106
Willy Tarreaubd741542010-03-16 18:46:54 +01002107/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002108 * Local variables:
2109 * c-indent-level: 8
2110 * c-basic-offset: 8
2111 * End:
2112 */