blob: 315bc873f64c5612fee46d1c77ee3f94cd7bbb62 [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>
Willy Tarreaubcc67332020-06-05 15:31:31 +020040#include <haproxy/extcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/fd.h>
42#include <haproxy/global.h>
43#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020044#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/istbuf.h>
48#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020049#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020050#include <haproxy/mailers.h>
51#include <haproxy/port_range.h>
52#include <haproxy/proto_tcp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020053#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/queue.h>
56#include <haproxy/regex.h>
57#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020058#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020059#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020060#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020061#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020063#include <haproxy/tcpcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020064#include <haproxy/thread.h>
65#include <haproxy/time.h>
66#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020067#include <haproxy/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Olivier Houchard9130a962017-10-17 17:33:43 +020069
Christopher Faulet61cc8522020-04-20 14:54:42 +020070static int wake_srv_chk(struct conn_stream *cs);
71struct data_cb check_conn_cb = {
72 .wake = wake_srv_chk,
73 .name = "CHCK",
74};
Christopher Fauletd7e63962020-04-17 20:15:59 +020075
Christopher Faulet5d503fc2020-03-30 20:34:34 +020076
Gaetan Rivet05d692d2020-02-14 17:42:54 +010077/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +020078struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +020079
Christopher Faulet61cc8522020-04-20 14:54:42 +020080/**************************************************************************/
81/************************ Handle check results ****************************/
82/**************************************************************************/
83struct check_status {
84 short result; /* one of SRV_CHK_* */
85 char *info; /* human readable short info */
86 char *desc; /* long description */
87};
88
89struct analyze_status {
90 char *desc; /* description */
91 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
92};
93
Simon Horman63a4a822012-03-19 07:24:41 +090094static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010095 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
96 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020097 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020098
Willy Tarreau23964182014-05-20 20:56:30 +020099 /* Below we have finished checks */
100 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100101 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100102
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100103 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200104
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100105 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
106 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
107 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200108
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100109 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
110 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
111 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200112
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100113 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
114 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200115
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200116 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200117
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100118 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
119 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
120 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900121
122 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
123 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200124 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200125};
126
Simon Horman63a4a822012-03-19 07:24:41 +0900127static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100128 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
129
130 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
131 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
132
133 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
134 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
135 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
136 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
137
138 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
139 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
140 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
141};
142
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100143/* checks if <err> is a real error for errno or one that can be ignored, and
144 * return 0 for these ones or <err> for real ones.
145 */
146static inline int unclean_errno(int err)
147{
148 if (err == EAGAIN || err == EINPROGRESS ||
149 err == EISCONN || err == EALREADY)
150 return 0;
151 return err;
152}
153
Christopher Faulet61cc8522020-04-20 14:54:42 +0200154/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200155const char *get_check_status_description(short check_status) {
156
157 const char *desc;
158
159 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200160 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200161 else
162 desc = NULL;
163
164 if (desc && *desc)
165 return desc;
166 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200167 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200168}
169
Christopher Faulet61cc8522020-04-20 14:54:42 +0200170/* Converts check_status code to short info */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200171const char *get_check_status_info(short check_status) {
172
173 const char *info;
174
175 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200176 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200177 else
178 info = NULL;
179
180 if (info && *info)
181 return info;
182 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200183 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200184}
185
Christopher Faulet61cc8522020-04-20 14:54:42 +0200186/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100187const char *get_analyze_status(short analyze_status) {
188
189 const char *desc;
190
191 if (analyze_status < HANA_STATUS_SIZE)
192 desc = analyze_statuses[analyze_status].desc;
193 else
194 desc = NULL;
195
196 if (desc && *desc)
197 return desc;
198 else
199 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
200}
201
Christopher Faulet61cc8522020-04-20 14:54:42 +0200202/* Sets check->status, update check->duration and fill check->result with an
203 * adequate CHK_RES_* value. The new check->health is computed based on the
204 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200205 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200206 * Shows information in logs about failed health check if server is UP or
207 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200208 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200209void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100210{
Simon Horman4a741432013-02-23 15:35:38 +0900211 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200212 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200213 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900214
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200215 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100216 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900217 check->desc[0] = '\0';
218 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200219 return;
220 }
221
Simon Horman4a741432013-02-23 15:35:38 +0900222 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200223 return;
224
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200225 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900226 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
227 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200228 } else
Simon Horman4a741432013-02-23 15:35:38 +0900229 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200230
Simon Horman4a741432013-02-23 15:35:38 +0900231 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200232 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900233 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200234
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100235 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900236 check->duration = -1;
237 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200238 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900239 check->duration = tv_ms_elapsed(&check->start, &now);
240 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200241 }
242
Willy Tarreau23964182014-05-20 20:56:30 +0200243 /* no change is expected if no state change occurred */
244 if (check->result == CHK_RES_NEUTRAL)
245 return;
246
Olivier Houchard0923fa42019-01-11 18:43:04 +0100247 /* If the check was really just sending a mail, it won't have an
248 * associated server, so we're done now.
249 */
250 if (!s)
251 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200252 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200253
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200254 switch (check->result) {
255 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200256 /* Failure to connect to the agent as a secondary check should not
257 * cause the server to be marked down.
258 */
259 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900260 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200261 (check->health > 0)) {
Olivier Houchard7059c552019-03-08 18:49:32 +0100262 _HA_ATOMIC_ADD(&s->counters.failed_checks, 1);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200263 report = 1;
264 check->health--;
265 if (check->health < check->rise)
266 check->health = 0;
267 }
268 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200269
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200270 case CHK_RES_PASSED:
271 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
272 if ((check->health < check->rise + check->fall - 1) &&
273 (check->result == CHK_RES_PASSED || check->health > 0)) {
274 report = 1;
275 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200276
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200277 if (check->health >= check->rise)
278 check->health = check->rise + check->fall - 1; /* OK now */
279 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200280
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200281 /* clear consecutive_errors if observing is enabled */
282 if (s->onerror)
283 s->consecutive_errors = 0;
284 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100285
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200286 default:
287 break;
288 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200289
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200290 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
291 (status != prev_status || report)) {
292 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200293 "%s check for %sserver %s/%s %s%s",
294 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200295 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100296 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100297 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200298 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200299
Emeric Brun5a133512017-10-19 14:42:30 +0200300 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200301
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100302 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200303 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
304 (check->health >= check->rise) ? check->fall : check->rise,
305 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200306
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200307 ha_warning("%s.\n", trash.area);
308 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
309 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200310 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200311}
312
Willy Tarreau4eec5472014-05-20 22:32:27 +0200313/* Marks the check <check>'s server down if the current check is already failed
314 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200316void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317{
Simon Horman4a741432013-02-23 15:35:38 +0900318 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900319
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200320 /* The agent secondary check should only cause a server to be marked
321 * as down if check->status is HCHK_STATUS_L7STS, which indicates
322 * that the agent returned "fail", "stopped" or "down".
323 * The implication here is that failure to connect to the agent
324 * as a secondary check should not cause the server to be marked
325 * down. */
326 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
327 return;
328
Willy Tarreau4eec5472014-05-20 22:32:27 +0200329 if (check->health > 0)
330 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100331
Willy Tarreau4eec5472014-05-20 22:32:27 +0200332 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200333 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334}
335
Willy Tarreauaf549582014-05-16 17:37:50 +0200336/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200337 * it isn't in maintenance, it is not tracking a down server and other checks
338 * comply. The rule is simple : by default, a server is up, unless any of the
339 * following conditions is true :
340 * - health check failed (check->health < rise)
341 * - agent check failed (agent->health < rise)
342 * - the server tracks a down server (track && track->state == STOPPED)
343 * Note that if the server has a slowstart, it will switch to STARTING instead
344 * of RUNNING. Also, only the health checks support the nolb mode, so the
345 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200346 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200347void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200348{
Simon Horman4a741432013-02-23 15:35:38 +0900349 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100350
Emeric Brun52a91d32017-08-31 14:41:55 +0200351 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200352 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100353
Emeric Brun52a91d32017-08-31 14:41:55 +0200354 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200355 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100356
Willy Tarreau3e048382014-05-21 10:30:54 +0200357 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
358 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100359
Willy Tarreau3e048382014-05-21 10:30:54 +0200360 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
361 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200362
Emeric Brun52a91d32017-08-31 14:41:55 +0200363 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200364 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100365
Emeric Brun5a133512017-10-19 14:42:30 +0200366 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100367}
368
Willy Tarreaudb58b792014-05-21 13:57:23 +0200369/* Marks the check <check> as valid and tries to set its server into stopping mode
370 * if it was running or starting, and provided it isn't in maintenance and other
371 * checks comply. The conditions for the server to be marked in stopping mode are
372 * the same as for it to be turned up. Also, only the health checks support the
373 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200374 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200375void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200376{
Simon Horman4a741432013-02-23 15:35:38 +0900377 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100378
Emeric Brun52a91d32017-08-31 14:41:55 +0200379 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200380 return;
381
Willy Tarreaudb58b792014-05-21 13:57:23 +0200382 if (check->state & CHK_ST_AGENT)
383 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100384
Emeric Brun52a91d32017-08-31 14:41:55 +0200385 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200386 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100387
Willy Tarreaudb58b792014-05-21 13:57:23 +0200388 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
389 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100390
Willy Tarreaudb58b792014-05-21 13:57:23 +0200391 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
392 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100393
Willy Tarreaub26881a2017-12-23 11:16:49 +0100394 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100395}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100397/* note: use health_adjust() only, which first checks that the observe mode is
398 * enabled.
399 */
400void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100401{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100402 int failed;
403 int expire;
404
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100405 if (s->observe >= HANA_OBS_SIZE)
406 return;
407
Willy Tarreaubb956662013-01-24 00:37:39 +0100408 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100409 return;
410
411 switch (analyze_statuses[status].lr[s->observe - 1]) {
412 case 1:
413 failed = 1;
414 break;
415
416 case 2:
417 failed = 0;
418 break;
419
420 default:
421 return;
422 }
423
424 if (!failed) {
425 /* good: clear consecutive_errors */
426 s->consecutive_errors = 0;
427 return;
428 }
429
Olivier Houchard7059c552019-03-08 18:49:32 +0100430 _HA_ATOMIC_ADD(&s->consecutive_errors, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100431
432 if (s->consecutive_errors < s->consecutive_errors_limit)
433 return;
434
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100435 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
436 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100437
438 switch (s->onerror) {
439 case HANA_ONERR_FASTINTER:
440 /* force fastinter - nothing to do here as all modes force it */
441 break;
442
443 case HANA_ONERR_SUDDTH:
444 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900445 if (s->check.health > s->check.rise)
446 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100447
Tim Duesterhus588b3142020-05-29 14:35:51 +0200448 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100449
450 case HANA_ONERR_FAILCHK:
451 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 set_server_check_status(&s->check, HCHK_STATUS_HANA,
453 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200454 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100455 break;
456
457 case HANA_ONERR_MARKDWN:
458 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900459 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200460 set_server_check_status(&s->check, HCHK_STATUS_HANA,
461 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200462 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100463 break;
464
465 default:
466 /* write a warning? */
467 break;
468 }
469
470 s->consecutive_errors = 0;
Olivier Houchard7059c552019-03-08 18:49:32 +0100471 _HA_ATOMIC_ADD(&s->counters.failed_hana, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100472
Simon Horman66183002013-02-23 10:16:43 +0900473 if (s->check.fastinter) {
474 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300475 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200476 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300477 /* requeue check task with new expire */
478 task_queue(s->check.task);
479 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100480 }
Willy Tarreauef781042010-01-27 11:53:01 +0100481}
482
Christopher Faulet61cc8522020-04-20 14:54:42 +0200483/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100484 * closed, keep errno intact as it is supposed to contain the valid error code.
485 * If no error is reported, check the socket's error queue using getsockopt().
486 * Warning, this must be done only once when returning from poll, and never
487 * after an I/O error was attempted, otherwise the error queue might contain
488 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
489 * socket. Returns non-zero if an error was reported, zero if everything is
490 * clean (including a properly closed socket).
491 */
492static int retrieve_errno_from_socket(struct connection *conn)
493{
494 int skerr;
495 socklen_t lskerr = sizeof(skerr);
496
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100497 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100498 return 1;
499
Willy Tarreau3c728722014-01-23 13:50:42 +0100500 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100501 return 0;
502
Willy Tarreau585744b2017-08-24 14:31:19 +0200503 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100504 errno = skerr;
505
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100506 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100507
508 if (!errno) {
509 /* we could not retrieve an error, that does not mean there is
510 * none. Just don't change anything and only report the prior
511 * error if any.
512 */
513 if (conn->flags & CO_FL_ERROR)
514 return 1;
515 else
516 return 0;
517 }
518
519 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
520 return 1;
521}
522
Christopher Faulet61cc8522020-04-20 14:54:42 +0200523/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100524 * and adjust the server status accordingly. It may make use of <errno_bck>
525 * if non-null when the caller is absolutely certain of its validity (eg:
526 * checked just after a syscall). If the caller doesn't have a valid errno,
527 * it can pass zero, and retrieve_errno_from_socket() will be called to try
528 * to extract errno from the socket. If no error is reported, it will consider
529 * the <expired> flag. This is intended to be used when a connection error was
530 * reported in conn->flags or when a timeout was reported in <expired>. The
531 * function takes care of not updating a server status which was already set.
532 * All situations where at least one of <expired> or CO_FL_ERROR are set
533 * produce a status.
534 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200535void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100536{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200537 struct conn_stream *cs = check->cs;
538 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100539 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200540 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200541 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100542
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100543 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100544 return;
545
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100546 errno = unclean_errno(errno_bck);
547 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100548 retrieve_errno_from_socket(conn);
549
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200550 if (conn && !(conn->flags & CO_FL_ERROR) &&
551 !(cs->flags & CS_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100552 return;
553
554 /* we'll try to build a meaningful error message depending on the
555 * context of the error possibly present in conn->err_code, and the
556 * socket error possibly collected above. This is useful to know the
557 * exact step of the L6 layer (eg: SSL handshake).
558 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200559 chk = get_trash_chunk();
560
Christopher Faulet799f3a42020-04-07 12:06:14 +0200561 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200562 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200563 step = tcpcheck_get_step_id(check, NULL);
Willy Tarreau213c6782014-10-02 14:51:02 +0200564 if (!step)
565 chunk_printf(chk, " at initial connection step of tcp-check");
566 else {
567 chunk_printf(chk, " at step %d of tcp-check", step);
568 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200569 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
570 if (check->current_step->connect.port)
571 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200572 else
573 chunk_appendf(chk, " (connect)");
574 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200575 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
576 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100577
578 switch (expect->type) {
579 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200580 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100581 break;
582 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200583 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100584 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200585 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200586 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100587 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200588 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100589 chunk_appendf(chk, " (expect binary regex)");
590 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200591 case TCPCHK_EXPECT_STRING_LF:
592 chunk_appendf(chk, " (expect log-format string)");
593 break;
594 case TCPCHK_EXPECT_BINARY_LF:
595 chunk_appendf(chk, " (expect log-format binary)");
596 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200597 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200598 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200599 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200600 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200601 chunk_appendf(chk, " (expect HTTP status regex)");
602 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200603 case TCPCHK_EXPECT_HTTP_HEADER:
604 chunk_appendf(chk, " (expect HTTP header pattern)");
605 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200606 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200607 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200608 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200609 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200610 chunk_appendf(chk, " (expect HTTP body regex)");
611 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200612 case TCPCHK_EXPECT_HTTP_BODY_LF:
613 chunk_appendf(chk, " (expect log-format HTTP body)");
614 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200615 case TCPCHK_EXPECT_CUSTOM:
616 chunk_appendf(chk, " (expect custom function)");
617 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100618 case TCPCHK_EXPECT_UNDEF:
619 chunk_appendf(chk, " (undefined expect!)");
620 break;
621 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200622 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200623 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200624 chunk_appendf(chk, " (send)");
625 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200626
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200627 if (check->current_step && check->current_step->comment)
628 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200629 }
630 }
631
Willy Tarreau00149122017-10-04 18:05:01 +0200632 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100633 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200634 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
635 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100636 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200637 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
638 chk->area);
639 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100640 }
641 else {
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", strerror(errno),
644 chk->area);
645 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100646 }
647 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200648 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100649 }
650 }
651
Willy Tarreau00149122017-10-04 18:05:01 +0200652 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200653 /* NOTE: this is reported after <fall> tries */
654 chunk_printf(chk, "No port available for the TCP connection");
655 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
656 }
657
Christopher Faulet5e293762020-10-26 11:10:49 +0100658 if (!conn || !conn->ctrl) {
659 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200660 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
661 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100662 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100663 /* L4 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200664 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100665 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
666 else if (expired)
667 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200668
669 /*
670 * might be due to a server IP change.
671 * Let's trigger a DNS resolution if none are currently running.
672 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100673 if (check->server)
674 dns_trigger_resolution(check->server->dns_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200675
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100676 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100677 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100678 /* L6 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200679 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100680 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
681 else if (expired)
682 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
683 }
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200684 else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100685 /* I/O error after connection was established and before we could diagnose */
686 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
687 }
688 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200689 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
690
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100691 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200692 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
693 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200694 tout = check->current_step->expect.tout_status;
695 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100696 }
697
698 return;
699}
700
Simon Horman98637e52014-06-20 12:30:16 +0900701
Christopher Faulet61cc8522020-04-20 14:54:42 +0200702/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200703int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900704{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200705 int sv_state;
706 int ratio;
707 char addr[46];
708 char port[6];
709 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
710 "UP %d/%d", "UP",
711 "NOLB %d/%d", "NOLB",
712 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900713
Christopher Faulet61cc8522020-04-20 14:54:42 +0200714 if (!(s->check.state & CHK_ST_ENABLED))
715 sv_state = 6;
716 else if (s->cur_state != SRV_ST_STOPPED) {
717 if (s->check.health == s->check.rise + s->check.fall - 1)
718 sv_state = 3; /* UP */
719 else
720 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900721
Christopher Faulet61cc8522020-04-20 14:54:42 +0200722 if (s->cur_state == SRV_ST_STOPPING)
723 sv_state += 2;
724 } else {
725 if (s->check.health)
726 sv_state = 1; /* going up */
727 else
728 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900729 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200730
Christopher Faulet61cc8522020-04-20 14:54:42 +0200731 chunk_appendf(buf, srv_hlt_st[sv_state],
732 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
733 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200734
Christopher Faulet61cc8522020-04-20 14:54:42 +0200735 addr_to_str(&s->addr, addr, sizeof(addr));
736 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
737 snprintf(port, sizeof(port), "%u", s->svc_port);
738 else
739 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200740
Christopher Faulet61cc8522020-04-20 14:54:42 +0200741 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
742 addr, port, s->proxy->id, s->id,
743 global.node,
744 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
745 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
746 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
747 s->nbpend);
Willy Tarreau9f6dc722019-03-01 11:15:10 +0100748
Christopher Faulet61cc8522020-04-20 14:54:42 +0200749 if ((s->cur_state == SRV_ST_STARTING) &&
750 now.tv_sec < s->last_change + s->slowstart &&
751 now.tv_sec >= s->last_change) {
752 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
753 chunk_appendf(buf, "; throttle=%d%%", ratio);
754 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +0200755
Christopher Faulet61cc8522020-04-20 14:54:42 +0200756 return b_data(buf);
757}
Christopher Fauletaaae9a02020-04-26 09:50:31 +0200758
Willy Tarreau51cd5952020-06-05 12:25:38 +0200759/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +0200760/***************** Health-checks based on connections *********************/
761/**************************************************************************/
762/* This function is used only for server health-checks. It handles connection
763 * status updates including errors. If necessary, it wakes the check task up.
764 * It returns 0 on normal cases, <0 if at least one close() has happened on the
765 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +0200766 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200767static int wake_srv_chk(struct conn_stream *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200768{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200769 struct connection *conn = cs->conn;
770 struct check *check = cs->data;
771 struct email_alertq *q = container_of(check, typeof(*q), check);
772 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200773
Willy Tarreau51cd5952020-06-05 12:25:38 +0200774 if (check->server)
775 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
776 else
777 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200778
Willy Tarreau51cd5952020-06-05 12:25:38 +0200779 /* we may have to make progress on the TCP checks */
780 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +0200781
Willy Tarreau51cd5952020-06-05 12:25:38 +0200782 cs = check->cs;
783 conn = cs->conn;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200784
Willy Tarreau51cd5952020-06-05 12:25:38 +0200785 if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) {
786 /* We may get error reports bypassing the I/O handlers, typically
787 * the case when sending a pure TCP check which fails, then the I/O
788 * handlers above are not called. This is completely handled by the
789 * main processing task so let's simply wake it up. If we get here,
790 * we expect errno to still be valid.
791 */
792 chk_report_conn_err(check, errno, 0);
793 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200794 }
795
Willy Tarreau51cd5952020-06-05 12:25:38 +0200796 if (check->result != CHK_RES_UNKNOWN) {
797 /* Check complete or aborted. If connection not yet closed do it
798 * now and wake the check task up to be sure the result is
799 * handled ASAP. */
800 conn_sock_drain(conn);
801 cs_close(cs);
802 ret = -1;
803 /* We may have been scheduled to run, and the
804 * I/O handler expects to have a cs, so remove
805 * the tasklet
806 */
807 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
808 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200809 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200810
Willy Tarreau51cd5952020-06-05 12:25:38 +0200811 if (check->server)
812 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
813 else
814 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200815
Willy Tarreau51cd5952020-06-05 12:25:38 +0200816 /* if a connection got replaced, we must absolutely prevent the connection
817 * handler from touching its fd, and perform the FD polling updates ourselves
818 */
819 if (ret < 0)
820 conn_cond_update_polling(conn);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200821
Christopher Faulet61cc8522020-04-20 14:54:42 +0200822 return ret;
823}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200824
Willy Tarreau51cd5952020-06-05 12:25:38 +0200825/* This function checks if any I/O is wanted, and if so, attempts to do so */
826static struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned short state)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200827{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200828 struct check *check = ctx;
829 struct conn_stream *cs = check->cs;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200830
Willy Tarreau51cd5952020-06-05 12:25:38 +0200831 wake_srv_chk(cs);
832 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200833}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +0200834
Willy Tarreau51cd5952020-06-05 12:25:38 +0200835/* manages a server health-check that uses a connection. Returns
836 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +0200837 *
838 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +0200839 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +0200840 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200841static struct task *process_chk_conn(struct task *t, void *context, unsigned short state)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200842{
Willy Tarreau51cd5952020-06-05 12:25:38 +0200843 struct check *check = context;
844 struct proxy *proxy = check->proxy;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200845 struct conn_stream *cs = check->cs;
846 struct connection *conn = cs_conn(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +0200847 int rv;
848 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +0200849
Willy Tarreau51cd5952020-06-05 12:25:38 +0200850 if (check->server)
851 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
852 if (!(check->state & CHK_ST_INPROGRESS)) {
853 /* no check currently running */
854 if (!expired) /* woke up too early */
855 goto out_unlock;
Willy Tarreauabca5b62013-12-06 14:19:25 +0100856
Willy Tarreau51cd5952020-06-05 12:25:38 +0200857 /* we don't send any health-checks when the proxy is
858 * stopped, the server should not be checked or the check
859 * is disabled.
860 */
861 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreauc3914d42020-09-24 08:39:22 +0200862 proxy->disabled)
Willy Tarreau51cd5952020-06-05 12:25:38 +0200863 goto reschedule;
Christopher Faulet404f9192020-04-09 23:13:54 +0200864
Willy Tarreau51cd5952020-06-05 12:25:38 +0200865 /* we'll initiate a new check */
866 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +0200867
Willy Tarreau51cd5952020-06-05 12:25:38 +0200868 check->state |= CHK_ST_INPROGRESS;
869 b_reset(&check->bi);
870 b_reset(&check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200871
Willy Tarreau51cd5952020-06-05 12:25:38 +0200872 task_set_affinity(t, tid_bit);
873
874 check->current_step = NULL;
875 tcpcheck_main(check);
876 goto out_unlock;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200877 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200878 else {
879 /* there was a test running.
880 * First, let's check whether there was an uncaught error,
881 * which can happen on connect timeout or error.
882 */
883 if (check->result == CHK_RES_UNKNOWN) {
884 if ((conn->flags & CO_FL_ERROR) || cs->flags & CS_FL_ERROR || expired) {
885 chk_report_conn_err(check, 0, expired);
886 }
887 else
888 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet61cc8522020-04-20 14:54:42 +0200889 }
Christopher Faulet404f9192020-04-09 23:13:54 +0200890
Willy Tarreau51cd5952020-06-05 12:25:38 +0200891 /* check complete or aborted */
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200892
Willy Tarreau51cd5952020-06-05 12:25:38 +0200893 check->current_step = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200894
Willy Tarreau51cd5952020-06-05 12:25:38 +0200895 if (conn && conn->xprt) {
896 /* The check was aborted and the connection was not yet closed.
897 * This can happen upon timeout, or when an external event such
898 * as a failed response coupled with "observe layer7" caused the
899 * server state to be suddenly changed.
900 */
901 conn_sock_drain(conn);
902 cs_close(cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +0200903 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200904
Willy Tarreau51cd5952020-06-05 12:25:38 +0200905 if (cs) {
906 if (check->wait_list.events)
907 cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
908 /* We may have been scheduled to run, and the
909 * I/O handler expects to have a cs, so remove
910 * the tasklet
911 */
912 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
913 cs_destroy(cs);
914 cs = check->cs = NULL;
915 conn = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200916 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200917
918 if (check->sess != NULL) {
919 vars_prune(&check->vars, check->sess, NULL);
920 session_free(check->sess);
921 check->sess = NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +0200922 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200923
924 if (check->server) {
925 if (check->result == CHK_RES_FAILED) {
926 /* a failure or timeout detected */
927 check_notify_failure(check);
928 }
929 else if (check->result == CHK_RES_CONDPASS) {
930 /* check is OK but asks for stopping mode */
931 check_notify_stopping(check);
932 }
933 else if (check->result == CHK_RES_PASSED) {
934 /* a success was detected */
935 check_notify_success(check);
936 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200937 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200938 task_set_affinity(t, MAX_THREADS_MASK);
939 check->state &= ~CHK_ST_INPROGRESS;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200940
Willy Tarreau51cd5952020-06-05 12:25:38 +0200941 if (check->server) {
942 rv = 0;
943 if (global.spread_checks > 0) {
944 rv = srv_getinter(check) * global.spread_checks / 100;
945 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +0200946 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200947 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +0200948 }
Christopher Faulet61cc8522020-04-20 14:54:42 +0200949 }
Willy Tarreau51cd5952020-06-05 12:25:38 +0200950
951 reschedule:
952 while (tick_is_expired(t->expire, now_ms))
953 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
954 out_unlock:
955 if (check->server)
956 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
957 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200958}
959
Willy Tarreau51cd5952020-06-05 12:25:38 +0200960
Christopher Faulet61cc8522020-04-20 14:54:42 +0200961/**************************************************************************/
962/************************** Init/deinit checks ****************************/
963/**************************************************************************/
Willy Tarreaucee013e2020-06-05 11:40:38 +0200964const char *init_check(struct check *check, int type)
Christopher Faulet61cc8522020-04-20 14:54:42 +0200965{
966 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200967
Christopher Faulet61cc8522020-04-20 14:54:42 +0200968 b_reset(&check->bi); check->bi.size = global.tune.chksize;
969 b_reset(&check->bo); check->bo.size = global.tune.chksize;
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200970
Tim Duesterhuse52b6e52020-09-12 20:26:43 +0200971 check->bi.area = calloc(check->bi.size, sizeof(*check->bi.area));
972 check->bo.area = calloc(check->bo.size, sizeof(*check->bo.area));
Christopher Fauletba3c68f2020-04-01 16:27:05 +0200973
Christopher Faulet61cc8522020-04-20 14:54:42 +0200974 if (!check->bi.area || !check->bo.area)
975 return "out of memory while allocating check buffer";
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100976
Christopher Faulet61cc8522020-04-20 14:54:42 +0200977 check->wait_list.tasklet = tasklet_new();
978 if (!check->wait_list.tasklet)
979 return "out of memory while allocating check tasklet";
980 check->wait_list.events = 0;
981 check->wait_list.tasklet->process = event_srv_chk_io;
982 check->wait_list.tasklet->context = check;
983 return NULL;
984}
985
986void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100987{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200988 task_destroy(check->task);
989 if (check->wait_list.tasklet)
990 tasklet_free(check->wait_list.tasklet);
991
992 free(check->bi.area);
993 free(check->bo.area);
994 if (check->cs) {
995 free(check->cs->conn);
996 check->cs->conn = NULL;
997 cs_free(check->cs);
998 check->cs = NULL;
999 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001000}
1001
Christopher Faulet61cc8522020-04-20 14:54:42 +02001002/* manages a server health-check. Returns the time the task accepts to wait, or
1003 * TIME_ETERNITY for infinity.
1004 */
Willy Tarreaucee013e2020-06-05 11:40:38 +02001005struct task *process_chk(struct task *t, void *context, unsigned short state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001006{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001007 struct check *check = context;
1008
1009 if (check->type == PR_O2_EXT_CHK)
1010 return process_chk_proc(t, context, state);
1011 return process_chk_conn(t, context, state);
1012
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001013}
1014
Christopher Faulet61cc8522020-04-20 14:54:42 +02001015
1016static int start_check_task(struct check *check, int mininter,
1017 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001018{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001019 struct task *t;
1020 unsigned long thread_mask = MAX_THREADS_MASK;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001021
Christopher Faulet61cc8522020-04-20 14:54:42 +02001022 if (check->type == PR_O2_EXT_CHK)
1023 thread_mask = 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001024
Christopher Faulet61cc8522020-04-20 14:54:42 +02001025 /* task for the check */
1026 if ((t = task_new(thread_mask)) == NULL) {
1027 ha_alert("Starting [%s:%s] check: out of memory.\n",
1028 check->server->proxy->id, check->server->id);
1029 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001030 }
1031
Christopher Faulet61cc8522020-04-20 14:54:42 +02001032 check->task = t;
1033 t->process = process_chk;
1034 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001035
Christopher Faulet61cc8522020-04-20 14:54:42 +02001036 if (mininter < srv_getinter(check))
1037 mininter = srv_getinter(check);
1038
1039 if (global.max_spread_checks && mininter > global.max_spread_checks)
1040 mininter = global.max_spread_checks;
1041
1042 /* check this every ms */
1043 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1044 check->start = now;
1045 task_queue(t);
1046
1047 return 1;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001048}
1049
Christopher Faulet61cc8522020-04-20 14:54:42 +02001050/* updates the server's weight during a warmup stage. Once the final weight is
1051 * reached, the task automatically stops. Note that any server status change
1052 * must have updated s->last_change accordingly.
1053 */
1054static struct task *server_warmup(struct task *t, void *context, unsigned short state)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001055{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001056 struct server *s = context;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001057
Christopher Faulet61cc8522020-04-20 14:54:42 +02001058 /* by default, plan on stopping the task */
1059 t->expire = TICK_ETERNITY;
1060 if ((s->next_admin & SRV_ADMF_MAINT) ||
1061 (s->next_state != SRV_ST_STARTING))
1062 return t;
Christopher Faulete5870d82020-04-15 11:32:03 +02001063
Christopher Faulet61cc8522020-04-20 14:54:42 +02001064 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001065
Christopher Faulet61cc8522020-04-20 14:54:42 +02001066 /* recalculate the weights and update the state */
1067 server_recalc_eweight(s, 1);
Christopher Faulet5c288742020-03-31 08:15:58 +02001068
Christopher Faulet61cc8522020-04-20 14:54:42 +02001069 /* probably that we can refill this server with a bit more connections */
1070 pendconn_grab_from_px(s);
Christopher Faulet5c288742020-03-31 08:15:58 +02001071
Christopher Faulet61cc8522020-04-20 14:54:42 +02001072 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5c288742020-03-31 08:15:58 +02001073
Christopher Faulet61cc8522020-04-20 14:54:42 +02001074 /* get back there in 1 second or 1/20th of the slowstart interval,
1075 * whichever is greater, resulting in small 5% steps.
1076 */
1077 if (s->next_state == SRV_ST_STARTING)
1078 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1079 return t;
1080}
1081
1082/*
1083 * Start health-check.
1084 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1085 */
1086static int start_checks()
1087{
1088
1089 struct proxy *px;
1090 struct server *s;
1091 struct task *t;
1092 int nbcheck=0, mininter=0, srvpos=0;
1093
1094 /* 0- init the dummy frontend used to create all checks sessions */
1095 init_new_proxy(&checks_fe);
1096 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1097 checks_fe.mode = PR_MODE_TCP;
1098 checks_fe.maxconn = 0;
1099 checks_fe.conn_retries = CONN_RETRIES;
1100 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1101 checks_fe.timeout.client = TICK_ETERNITY;
1102
1103 /* 1- count the checkers to run simultaneously.
1104 * We also determine the minimum interval among all of those which
1105 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1106 * will be used to spread their start-up date. Those which have
1107 * a shorter interval will start independently and will not dictate
1108 * too short an interval for all others.
1109 */
1110 for (px = proxies_list; px; px = px->next) {
1111 for (s = px->srv; s; s = s->next) {
1112 if (s->slowstart) {
1113 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
1114 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1115 return ERR_ALERT | ERR_FATAL;
1116 }
1117 /* We need a warmup task that will be called when the server
1118 * state switches from down to up.
1119 */
1120 s->warmup = t;
1121 t->process = server_warmup;
1122 t->context = s;
1123 /* server can be in this state only because of */
1124 if (s->next_state == SRV_ST_STARTING)
1125 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 +02001126 }
1127
Christopher Faulet61cc8522020-04-20 14:54:42 +02001128 if (s->check.state & CHK_ST_CONFIGURED) {
1129 nbcheck++;
1130 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1131 (!mininter || mininter > srv_getinter(&s->check)))
1132 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001133 }
1134
Christopher Faulet61cc8522020-04-20 14:54:42 +02001135 if (s->agent.state & CHK_ST_CONFIGURED) {
1136 nbcheck++;
1137 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1138 (!mininter || mininter > srv_getinter(&s->agent)))
1139 mininter = srv_getinter(&s->agent);
1140 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001141 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001142 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001143
Christopher Faulet61cc8522020-04-20 14:54:42 +02001144 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001145 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001146
Christopher Faulet61cc8522020-04-20 14:54:42 +02001147 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001148
Christopher Faulet61cc8522020-04-20 14:54:42 +02001149 /*
1150 * 2- start them as far as possible from each others. For this, we will
1151 * start them after their interval set to the min interval divided by
1152 * the number of servers, weighted by the server's position in the list.
1153 */
1154 for (px = proxies_list; px; px = px->next) {
1155 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1156 if (init_pid_list()) {
1157 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1158 return ERR_ALERT | ERR_FATAL;
1159 }
1160 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001161
Christopher Faulet61cc8522020-04-20 14:54:42 +02001162 for (s = px->srv; s; s = s->next) {
1163 /* A task for the main check */
1164 if (s->check.state & CHK_ST_CONFIGURED) {
1165 if (s->check.type == PR_O2_EXT_CHK) {
1166 if (!prepare_external_check(&s->check))
1167 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001168 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001169 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1170 return ERR_ALERT | ERR_FATAL;
1171 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001172 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001173
Christopher Faulet61cc8522020-04-20 14:54:42 +02001174 /* A task for a auxiliary agent check */
1175 if (s->agent.state & CHK_ST_CONFIGURED) {
1176 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1177 return ERR_ALERT | ERR_FATAL;
1178 }
1179 srvpos++;
1180 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001181 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001182 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001183 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001184}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001185
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001186
Christopher Faulet61cc8522020-04-20 14:54:42 +02001187/*
1188 * Return value:
1189 * the port to be used for the health check
1190 * 0 in case no port could be found for the check
1191 */
1192static int srv_check_healthcheck_port(struct check *chk)
1193{
1194 int i = 0;
1195 struct server *srv = NULL;
1196
1197 srv = chk->server;
1198
1199 /* by default, we use the health check port ocnfigured */
1200 if (chk->port > 0)
1201 return chk->port;
1202
1203 /* try to get the port from check_core.addr if check.port not set */
1204 i = get_host_port(&chk->addr);
1205 if (i > 0)
1206 return i;
1207
1208 /* try to get the port from server address */
1209 /* prevent MAPPORTS from working at this point, since checks could
1210 * not be performed in such case (MAPPORTS impose a relative ports
1211 * based on live traffic)
1212 */
1213 if (srv->flags & SRV_F_MAPPORTS)
1214 return 0;
1215
1216 i = srv->svc_port; /* by default */
1217 if (i > 0)
1218 return i;
1219
1220 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001221}
1222
Christopher Faulet61cc8522020-04-20 14:54:42 +02001223/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1224 * if an error occurred.
1225 */
1226static int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001227{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001228 const char *err;
1229 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001230 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001231 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001232
Christopher Faulet61cc8522020-04-20 14:54:42 +02001233 if (!srv->do_check)
1234 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001235
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001236 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001237
Christopher Faulet61cc8522020-04-20 14:54:42 +02001238 /* If neither a port nor an addr was specified and no check transport
1239 * layer is forced, then the transport layer used by the checks is the
1240 * same as for the production traffic. Otherwise we use raw_sock by
1241 * default, unless one is specified.
1242 */
1243 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1244 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1245 srv->check.use_ssl = srv->use_ssl;
1246 srv->check.xprt = srv->xprt;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001247 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001248 else if (srv->check.use_ssl == 1)
1249 srv->check.xprt = xprt_get(XPRT_SSL);
1250 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001251 }
Christopher Faulet66163ec2020-05-20 22:36:24 +02001252 else if (srv->check.use_ssl == 1)
1253 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001254
Christopher Faulet12882cf2020-04-23 15:50:18 +02001255 /* Inherit the mux protocol from the server if not already defined for
1256 * the check
1257 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001258 if (srv->mux_proto && !srv->check.mux_proto &&
1259 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1260 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001261 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001262 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001263 /* test that check proto is valid if explicitly defined */
1264 else if (srv->check.mux_proto &&
1265 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1266 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1267 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1268 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1269 ret |= ERR_ALERT | ERR_FATAL;
1270 goto out;
1271 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001272
Christopher Faulet61cc8522020-04-20 14:54:42 +02001273 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001274
Christopher Faulet61cc8522020-04-20 14:54:42 +02001275 /* We need at least a service port, a check port or the first tcp-check
1276 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1277 */
1278 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1279 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1280 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001281
Christopher Faulet61cc8522020-04-20 14:54:42 +02001282 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1283 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1284 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1285 ret |= ERR_ALERT | ERR_ABORT;
1286 goto out;
1287 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001288
Christopher Faulet61cc8522020-04-20 14:54:42 +02001289 /* search the first action (connect / send / expect) in the list */
1290 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1291 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1292 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1293 "nor tcp_check rule 'connect' with port information.\n",
1294 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1295 ret |= ERR_ALERT | ERR_ABORT;
1296 goto out;
1297 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001298
Christopher Faulet61cc8522020-04-20 14:54:42 +02001299 /* scan the tcp-check ruleset to ensure a port has been configured */
1300 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
1301 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) {
1302 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1303 "and a tcp_check rule 'connect' with no port information.\n",
1304 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1305 ret |= ERR_ALERT | ERR_ABORT;
1306 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001307 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001308 }
1309
Christopher Faulet61cc8522020-04-20 14:54:42 +02001310 init:
1311 if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) {
1312 struct tcpcheck_ruleset *rs = NULL;
1313 struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules;
1314 //char *errmsg = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001315
Christopher Faulet61cc8522020-04-20 14:54:42 +02001316 srv->proxy->options2 &= ~PR_O2_CHK_ANY;
1317 srv->proxy->options2 |= PR_O2_TCPCHK_CHK;
Christopher Faulete5870d82020-04-15 11:32:03 +02001318
Christopher Faulet61cc8522020-04-20 14:54:42 +02001319 rs = find_tcpcheck_ruleset("*tcp-check");
1320 if (!rs) {
1321 rs = create_tcpcheck_ruleset("*tcp-check");
1322 if (rs == NULL) {
1323 ha_alert("config: %s '%s': out of memory.\n",
1324 proxy_type_str(srv->proxy), srv->proxy->id);
1325 ret |= ERR_ALERT | ERR_FATAL;
1326 goto out;
1327 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001328 }
1329
Christopher Faulet61cc8522020-04-20 14:54:42 +02001330 free_tcpcheck_vars(&rules->preset_vars);
1331 rules->list = &rs->rules;
1332 rules->flags = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001333 }
1334
Christopher Faulet61cc8522020-04-20 14:54:42 +02001335 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1336 if (err) {
1337 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1338 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1339 ret |= ERR_ALERT | ERR_ABORT;
1340 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001341 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001342 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1343 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001344
Christopher Faulet61cc8522020-04-20 14:54:42 +02001345 out:
1346 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001347}
1348
Christopher Faulet61cc8522020-04-20 14:54:42 +02001349/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1350 * if an error occurred.
1351 */
1352static int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001353{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001354 struct tcpcheck_rule *chk;
1355 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001356 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001357
Christopher Faulet61cc8522020-04-20 14:54:42 +02001358 if (!srv->do_agent)
1359 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001360
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001361 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001362 * implicit one is inserted before all others.
1363 */
1364 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1365 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1366 chk = calloc(1, sizeof(*chk));
1367 if (!chk) {
1368 ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule"
1369 " to agent-check for server '%s' (out of memory).\n",
1370 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1371 ret |= ERR_ALERT | ERR_FATAL;
1372 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001373 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001374 chk->action = TCPCHK_ACT_CONNECT;
1375 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
1376 LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001377 }
1378
Christopher Faulete5870d82020-04-15 11:32:03 +02001379
Christopher Faulet61cc8522020-04-20 14:54:42 +02001380 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1381 if (err) {
1382 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1383 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1384 ret |= ERR_ALERT | ERR_ABORT;
1385 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001386 }
1387
Christopher Faulet61cc8522020-04-20 14:54:42 +02001388 if (!srv->agent.inter)
1389 srv->agent.inter = srv->check.inter;
1390
1391 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1392 global.maxsock++;
1393
1394 out:
1395 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001396}
1397
Christopher Faulet61cc8522020-04-20 14:54:42 +02001398static void deinit_srv_check(struct server *srv)
1399{
1400 if (srv->check.state & CHK_ST_CONFIGURED)
1401 free_check(&srv->check);
1402 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1403 srv->do_check = 0;
1404}
Christopher Faulete5870d82020-04-15 11:32:03 +02001405
Christopher Faulet61cc8522020-04-20 14:54:42 +02001406
1407static void deinit_srv_agent_check(struct server *srv)
1408{
1409 if (srv->agent.tcpcheck_rules) {
1410 free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars);
1411 free(srv->agent.tcpcheck_rules);
1412 srv->agent.tcpcheck_rules = NULL;
Christopher Faulete5870d82020-04-15 11:32:03 +02001413 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001414
Christopher Faulet61cc8522020-04-20 14:54:42 +02001415 if (srv->agent.state & CHK_ST_CONFIGURED)
1416 free_check(&srv->agent);
1417
1418 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1419 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001420}
1421
Willy Tarreaucee013e2020-06-05 11:40:38 +02001422REGISTER_POST_SERVER_CHECK(init_srv_check);
1423REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001424REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001425
Willy Tarreaucee013e2020-06-05 11:40:38 +02001426REGISTER_SERVER_DEINIT(deinit_srv_check);
1427REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001428
Christopher Faulet61cc8522020-04-20 14:54:42 +02001429
1430/**************************************************************************/
1431/************************** Check sample fetches **************************/
1432/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001433
Christopher Faulet61cc8522020-04-20 14:54:42 +02001434static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001435 { /* END */ },
1436}};
1437
1438INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1439
1440
1441/**************************************************************************/
1442/************************ Check's parsing functions ***********************/
1443/**************************************************************************/
Christopher Faulet51b129f2020-04-09 15:54:18 +02001444/* Parses the "http-check" proxy keyword */
1445static int proxy_parse_httpcheck(char **args, int section, struct proxy *curpx,
1446 struct proxy *defpx, const char *file, int line,
1447 char **errmsg)
1448{
Christopher Faulete5870d82020-04-15 11:32:03 +02001449 struct tcpcheck_ruleset *rs = NULL;
1450 struct tcpcheck_rule *chk = NULL;
1451 int index, cur_arg, ret = 0;
Christopher Faulet51b129f2020-04-09 15:54:18 +02001452
1453 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL))
1454 ret = 1;
1455
1456 cur_arg = 1;
1457 if (strcmp(args[cur_arg], "disable-on-404") == 0) {
1458 /* enable a graceful server shutdown on an HTTP 404 response */
1459 curpx->options |= PR_O_DISABLE404;
1460 if (too_many_args(1, args, errmsg, NULL))
1461 goto error;
Christopher Faulete5870d82020-04-15 11:32:03 +02001462 goto out;
Christopher Faulet51b129f2020-04-09 15:54:18 +02001463 }
1464 else if (strcmp(args[cur_arg], "send-state") == 0) {
1465 /* enable emission of the apparent state of a server in HTTP checks */
1466 curpx->options2 |= PR_O2_CHK_SNDST;
1467 if (too_many_args(1, args, errmsg, NULL))
1468 goto error;
Christopher Faulete5870d82020-04-15 11:32:03 +02001469 goto out;
Christopher Faulet51b129f2020-04-09 15:54:18 +02001470 }
Christopher Faulet51b129f2020-04-09 15:54:18 +02001471
Christopher Faulete5870d82020-04-15 11:32:03 +02001472 /* Deduce the ruleset name from the proxy info */
1473 chunk_printf(&trash, "*http-check-%s_%s-%d",
1474 ((curpx == defpx) ? "defaults" : curpx->id),
1475 curpx->conf.file, curpx->conf.line);
Christopher Faulet51b129f2020-04-09 15:54:18 +02001476
Christopher Faulet61cc8522020-04-20 14:54:42 +02001477 rs = find_tcpcheck_ruleset(b_orig(&trash));
Christopher Faulete5870d82020-04-15 11:32:03 +02001478 if (rs == NULL) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001479 rs = create_tcpcheck_ruleset(b_orig(&trash));
Christopher Faulete5870d82020-04-15 11:32:03 +02001480 if (rs == NULL) {
1481 memprintf(errmsg, "out of memory.\n");
1482 goto error;
Christopher Faulet51b129f2020-04-09 15:54:18 +02001483 }
1484 }
Christopher Faulet51b129f2020-04-09 15:54:18 +02001485
Christopher Faulete5870d82020-04-15 11:32:03 +02001486 index = 0;
1487 if (!LIST_ISEMPTY(&rs->rules)) {
1488 chk = LIST_PREV(&rs->rules, typeof(chk), list);
1489 if (chk->action != TCPCHK_ACT_SEND || !(chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT))
1490 index = chk->index + 1;
1491 }
Christopher Faulet51b129f2020-04-09 15:54:18 +02001492
Christopher Faulete5870d82020-04-15 11:32:03 +02001493 if (strcmp(args[cur_arg], "connect") == 0)
1494 chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg);
1495 else if (strcmp(args[cur_arg], "send") == 0)
1496 chk = parse_tcpcheck_send_http(args, cur_arg, curpx, &rs->rules, file, line, errmsg);
1497 else if (strcmp(args[cur_arg], "expect") == 0)
1498 chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, TCPCHK_RULES_HTTP_CHK,
1499 file, line, errmsg);
1500 else if (strcmp(args[cur_arg], "comment") == 0)
1501 chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg);
1502 else {
1503 struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]);
Christopher Faulet51b129f2020-04-09 15:54:18 +02001504
Christopher Faulete5870d82020-04-15 11:32:03 +02001505 if (!kw) {
1506 action_kw_tcp_check_build_list(&trash);
1507 memprintf(errmsg, "'%s' only supports 'disable-on-404', 'send-state', 'comment', 'connect',"
1508 " 'send', 'expect'%s%s. but got '%s'",
1509 args[0], (*trash.area ? ", " : ""), trash.area, args[1]);
1510 goto error;
Christopher Faulet51b129f2020-04-09 15:54:18 +02001511 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001512 chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg);
1513 }
Christopher Faulet51b129f2020-04-09 15:54:18 +02001514
Christopher Faulete5870d82020-04-15 11:32:03 +02001515 if (!chk) {
1516 memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg);
1517 goto error;
1518 }
1519 ret = (*errmsg != NULL); /* Handle warning */
1520
1521 chk->index = index;
1522 if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK &&
1523 (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
1524 /* Use this ruleset if the proxy already has http-check enabled */
1525 curpx->tcpcheck_rules.list = &rs->rules;
1526 curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_HTTP_RS;
1527 if (!tcpcheck_add_http_rule(chk, &curpx->tcpcheck_rules, errmsg)) {
1528 memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg);
1529 curpx->tcpcheck_rules.list = NULL;
Christopher Faulet51b129f2020-04-09 15:54:18 +02001530 goto error;
1531 }
1532 }
1533 else {
Christopher Faulete5870d82020-04-15 11:32:03 +02001534 /* mark this ruleset as unused for now */
1535 curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_HTTP_RS;
1536 LIST_ADDQ(&rs->rules, &chk->list);
Christopher Faulet51b129f2020-04-09 15:54:18 +02001537 }
1538
Christopher Faulete5870d82020-04-15 11:32:03 +02001539 out:
Christopher Faulet51b129f2020-04-09 15:54:18 +02001540 return ret;
1541
1542 error:
Christopher Faulete5870d82020-04-15 11:32:03 +02001543 free_tcpcheck(chk, 0);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001544 free_tcpcheck_ruleset(rs);
Christopher Faulet51b129f2020-04-09 15:54:18 +02001545 return -1;
1546}
1547
Christopher Faulet430e4802020-04-09 15:28:16 +02001548/* Parses the "option tcp-check" proxy keyword */
1549int proxy_parse_tcp_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
1550 const char *file, int line)
1551{
Christopher Faulet404f9192020-04-09 23:13:54 +02001552 struct tcpcheck_ruleset *rs = NULL;
Christopher Faulet430e4802020-04-09 15:28:16 +02001553 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
1554 int err_code = 0;
1555
1556 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
1557 err_code |= ERR_WARN;
1558
1559 if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code))
1560 goto out;
1561
Christopher Faulet404f9192020-04-09 23:13:54 +02001562 curpx->options2 &= ~PR_O2_CHK_ANY;
1563 curpx->options2 |= PR_O2_TCPCHK_CHK;
1564
Christopher Fauletd7e63962020-04-17 20:15:59 +02001565 if ((rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Faulet404f9192020-04-09 23:13:54 +02001566 /* If a tcp-check rulesset is already set, do nothing */
1567 if (rules->list)
1568 goto out;
1569
1570 /* If a tcp-check ruleset is waiting to be used for the current proxy,
1571 * get it.
1572 */
1573 if (rules->flags & TCPCHK_RULES_UNUSED_TCP_RS)
1574 goto curpx_ruleset;
1575
1576 /* Otherwise, try to get the tcp-check ruleset of the default proxy */
1577 chunk_printf(&trash, "*tcp-check-defaults_%s-%d", defpx->conf.file, defpx->conf.line);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001578 rs = find_tcpcheck_ruleset(b_orig(&trash));
Christopher Faulet404f9192020-04-09 23:13:54 +02001579 if (rs)
1580 goto ruleset_found;
Christopher Faulet430e4802020-04-09 15:28:16 +02001581 }
1582
Christopher Faulet404f9192020-04-09 23:13:54 +02001583 curpx_ruleset:
1584 /* Deduce the ruleset name from the proxy info */
1585 chunk_printf(&trash, "*tcp-check-%s_%s-%d",
1586 ((curpx == defpx) ? "defaults" : curpx->id),
1587 curpx->conf.file, curpx->conf.line);
1588
Christopher Faulet61cc8522020-04-20 14:54:42 +02001589 rs = find_tcpcheck_ruleset(b_orig(&trash));
Christopher Faulet404f9192020-04-09 23:13:54 +02001590 if (rs == NULL) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001591 rs = create_tcpcheck_ruleset(b_orig(&trash));
Christopher Faulet404f9192020-04-09 23:13:54 +02001592 if (rs == NULL) {
Christopher Faulet430e4802020-04-09 15:28:16 +02001593 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
1594 goto error;
1595 }
Christopher Faulet430e4802020-04-09 15:28:16 +02001596 }
1597
Christopher Faulet404f9192020-04-09 23:13:54 +02001598 ruleset_found:
1599 free_tcpcheck_vars(&rules->preset_vars);
Christopher Faulet404f9192020-04-09 23:13:54 +02001600 rules->list = &rs->rules;
Christopher Fauletd7e63962020-04-17 20:15:59 +02001601 rules->flags |= TCPCHK_RULES_TCP_CHK;
Christopher Fauletc751b452020-11-13 08:55:57 +01001602 rules->flags &= ~TCPCHK_RULES_UNUSED_TCP_RS;
Christopher Faulet430e4802020-04-09 15:28:16 +02001603
1604 out:
1605 return err_code;
1606
1607 error:
1608 err_code |= ERR_ALERT | ERR_FATAL;
1609 goto out;
1610}
Christopher Faulet33f05df2020-04-01 11:08:50 +02001611
1612/* Parses the "option redis-check" proxy keyword */
1613int proxy_parse_redis_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
1614 const char *file, int line)
1615{
1616 static char *redis_req = "*1\r\n$4\r\nPING\r\n";
1617 static char *redis_res = "+PONG\r\n";
1618
1619 struct tcpcheck_ruleset *rs = NULL;
1620 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
1621 struct tcpcheck_rule *chk;
1622 char *errmsg = NULL;
1623 int err_code = 0;
1624
1625 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
1626 err_code |= ERR_WARN;
1627
1628 if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code))
1629 goto out;
Christopher Faulet33f05df2020-04-01 11:08:50 +02001630
1631 curpx->options2 &= ~PR_O2_CHK_ANY;
1632 curpx->options2 |= PR_O2_TCPCHK_CHK;
1633
1634 free_tcpcheck_vars(&rules->preset_vars);
1635 rules->list = NULL;
1636 rules->flags = 0;
1637
Christopher Faulet61cc8522020-04-20 14:54:42 +02001638 rs = find_tcpcheck_ruleset("*redis-check");
Christopher Faulet33f05df2020-04-01 11:08:50 +02001639 if (rs)
1640 goto ruleset_found;
1641
Christopher Faulet61cc8522020-04-20 14:54:42 +02001642 rs = create_tcpcheck_ruleset("*redis-check");
Christopher Faulet33f05df2020-04-01 11:08:50 +02001643 if (rs == NULL) {
1644 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
1645 goto error;
1646 }
1647
1648 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", redis_req, ""},
1649 1, curpx, &rs->rules, file, line, &errmsg);
1650 if (!chk) {
1651 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1652 goto error;
1653 }
1654 chk->index = 0;
1655 LIST_ADDQ(&rs->rules, &chk->list);
1656
1657 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "string", redis_res,
1658 "error-status", "L7STS",
Christopher Faulete596d182020-05-05 17:46:34 +02001659 "on-error", "%[res.payload(0,0),cut_crlf]",
Christopher Faulet33f05df2020-04-01 11:08:50 +02001660 "on-success", "Redis server is ok",
1661 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001662 1, curpx, &rs->rules, TCPCHK_RULES_REDIS_CHK, file, line, &errmsg);
Christopher Faulet33f05df2020-04-01 11:08:50 +02001663 if (!chk) {
1664 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1665 goto error;
1666 }
1667 chk->index = 1;
1668 LIST_ADDQ(&rs->rules, &chk->list);
1669
Christopher Faulet33f05df2020-04-01 11:08:50 +02001670 ruleset_found:
1671 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02001672 rules->flags |= TCPCHK_RULES_REDIS_CHK;
Christopher Faulet33f05df2020-04-01 11:08:50 +02001673
1674 out:
1675 free(errmsg);
1676 return err_code;
1677
1678 error:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001679 free_tcpcheck_ruleset(rs);
Christopher Faulet33f05df2020-04-01 11:08:50 +02001680 err_code |= ERR_ALERT | ERR_FATAL;
1681 goto out;
1682}
1683
Christopher Faulet811f78c2020-04-01 11:10:27 +02001684
1685/* Parses the "option ssl-hello-chk" proxy keyword */
1686int proxy_parse_ssl_hello_chk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
1687 const char *file, int line)
1688{
1689 /* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
1690 * ssl-hello-chk option to ensure that the remote server speaks SSL.
1691 *
1692 * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
1693 */
1694 static char sslv3_client_hello[] = {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001695 "16" /* ContentType : 0x16 = Handshake */
Christopher Faulet811f78c2020-04-01 11:10:27 +02001696 "0300" /* ProtocolVersion : 0x0300 = SSLv3 */
1697 "0079" /* ContentLength : 0x79 bytes after this one */
1698 "01" /* HanshakeType : 0x01 = CLIENT HELLO */
1699 "000075" /* HandshakeLength : 0x75 bytes after this one */
1700 "0300" /* Hello Version : 0x0300 = v3 */
1701 "%[date(),htonl,hex]" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
1702 "%[str(HAPROXYSSLCHK\nHAPROXYSSLCHK\n),hex]" /* Random : must be exactly 28 bytes */
1703 "00" /* Session ID length : empty (no session ID) */
1704 "004E" /* Cipher Suite Length : 78 bytes after this one */
1705 "0001" "0002" "0003" "0004" /* 39 most common ciphers : */
1706 "0005" "0006" "0007" "0008" /* 0x01...0x1B, 0x2F...0x3A */
1707 "0009" "000A" "000B" "000C" /* This covers RSA/DH, */
1708 "000D" "000E" "000F" "0010" /* various bit lengths, */
1709 "0011" "0012" "0013" "0014" /* SHA1/MD5, DES/3DES/AES... */
1710 "0015" "0016" "0017" "0018"
1711 "0019" "001A" "001B" "002F"
1712 "0030" "0031" "0032" "0033"
1713 "0034" "0035" "0036" "0037"
1714 "0038" "0039" "003A"
1715 "01" /* Compression Length : 0x01 = 1 byte for types */
1716 "00" /* Compression Type : 0x00 = NULL compression */
1717 };
1718
1719 struct tcpcheck_ruleset *rs = NULL;
1720 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
1721 struct tcpcheck_rule *chk;
1722 char *errmsg = NULL;
1723 int err_code = 0;
1724
1725 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
1726 err_code |= ERR_WARN;
1727
1728 if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code))
1729 goto out;
1730
Christopher Faulet811f78c2020-04-01 11:10:27 +02001731 curpx->options2 &= ~PR_O2_CHK_ANY;
1732 curpx->options2 |= PR_O2_TCPCHK_CHK;
1733
1734 free_tcpcheck_vars(&rules->preset_vars);
1735 rules->list = NULL;
1736 rules->flags = 0;
1737
Christopher Faulet61cc8522020-04-20 14:54:42 +02001738 rs = find_tcpcheck_ruleset("*ssl-hello-check");
Christopher Faulet811f78c2020-04-01 11:10:27 +02001739 if (rs)
1740 goto ruleset_found;
1741
Christopher Faulet61cc8522020-04-20 14:54:42 +02001742 rs = create_tcpcheck_ruleset("*ssl-hello-check");
Christopher Faulet811f78c2020-04-01 11:10:27 +02001743 if (rs == NULL) {
1744 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
1745 goto error;
1746 }
1747
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001748 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", sslv3_client_hello, ""},
Christopher Faulet811f78c2020-04-01 11:10:27 +02001749 1, curpx, &rs->rules, file, line, &errmsg);
1750 if (!chk) {
1751 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1752 goto error;
1753 }
1754 chk->index = 0;
1755 LIST_ADDQ(&rs->rules, &chk->list);
1756
1757 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^1[56]",
Christopher Fauletec07e382020-04-07 14:56:26 +02001758 "min-recv", "5", "ok-status", "L6OK",
Christopher Faulet811f78c2020-04-01 11:10:27 +02001759 "error-status", "L6RSP", "tout-status", "L6TOUT",
1760 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001761 1, curpx, &rs->rules, TCPCHK_RULES_SSL3_CHK, file, line, &errmsg);
Christopher Faulet811f78c2020-04-01 11:10:27 +02001762 if (!chk) {
1763 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1764 goto error;
1765 }
1766 chk->index = 1;
1767 LIST_ADDQ(&rs->rules, &chk->list);
1768
Christopher Faulet811f78c2020-04-01 11:10:27 +02001769 ruleset_found:
1770 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02001771 rules->flags |= TCPCHK_RULES_SSL3_CHK;
Christopher Faulet811f78c2020-04-01 11:10:27 +02001772
1773 out:
1774 free(errmsg);
1775 return err_code;
1776
1777 error:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001778 free_tcpcheck_ruleset(rs);
Christopher Faulet811f78c2020-04-01 11:10:27 +02001779 err_code |= ERR_ALERT | ERR_FATAL;
1780 goto out;
1781}
1782
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001783/* Parses the "option smtpchk" proxy keyword */
1784int proxy_parse_smtpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
1785 const char *file, int line)
1786{
1787 static char *smtp_req = "%[var(check.smtp_cmd)]\r\n";
1788
1789 struct tcpcheck_ruleset *rs = NULL;
1790 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
1791 struct tcpcheck_rule *chk;
1792 struct tcpcheck_var *var = NULL;
1793 char *cmd = NULL, *errmsg = NULL;
1794 int err_code = 0;
1795
1796 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
1797 err_code |= ERR_WARN;
1798
1799 if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code))
1800 goto out;
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001801
1802 curpx->options2 &= ~PR_O2_CHK_ANY;
1803 curpx->options2 |= PR_O2_TCPCHK_CHK;
1804
1805 free_tcpcheck_vars(&rules->preset_vars);
1806 rules->list = NULL;
1807 rules->flags = 0;
1808
1809 cur_arg += 2;
1810 if (*args[cur_arg] && *args[cur_arg+1] &&
1811 (strcmp(args[cur_arg], "EHLO") == 0 || strcmp(args[cur_arg], "HELO") == 0)) {
Tim Duesterhus2867b402020-06-12 15:58:48 +02001812 /* <EHLO|HELO> + space (1) + <host> + null byte (1) */
1813 cmd = calloc(strlen(args[cur_arg]) + 1 + strlen(args[cur_arg+1]) + 1, sizeof(*cmd));
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001814 if (cmd)
1815 sprintf(cmd, "%s %s", args[cur_arg], args[cur_arg+1]);
1816 }
1817 else {
1818 /* this just hits the default for now, but you could potentially expand it to allow for other stuff
1819 though, it's unlikely you'd want to send anything other than an EHLO or HELO */
1820 cmd = strdup("HELO localhost");
1821 }
1822
Christopher Fauletb61caf42020-04-21 10:57:42 +02001823 var = create_tcpcheck_var(ist("check.smtp_cmd"));
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001824 if (cmd == NULL || var == NULL) {
1825 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
1826 goto error;
1827 }
1828 var->data.type = SMP_T_STR;
1829 var->data.u.str.area = cmd;
1830 var->data.u.str.data = strlen(cmd);
1831 LIST_INIT(&var->list);
1832 LIST_ADDQ(&rules->preset_vars, &var->list);
1833 cmd = NULL;
1834 var = NULL;
1835
Christopher Faulet61cc8522020-04-20 14:54:42 +02001836 rs = find_tcpcheck_ruleset("*smtp-check");
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001837 if (rs)
1838 goto ruleset_found;
1839
Christopher Faulet61cc8522020-04-20 14:54:42 +02001840 rs = create_tcpcheck_ruleset("*smtp-check");
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001841 if (rs == NULL) {
1842 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
1843 goto error;
1844 }
1845
1846 chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""},
1847 1, curpx, &rs->rules, file, line, &errmsg);
1848 if (!chk) {
1849 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1850 goto error;
1851 }
1852 chk->index = 0;
1853 LIST_ADDQ(&rs->rules, &chk->list);
1854
1855 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^[0-9]{3}[ \r]",
1856 "min-recv", "4",
1857 "error-status", "L7RSP",
Christopher Faulete596d182020-05-05 17:46:34 +02001858 "on-error", "%[res.payload(0,0),cut_crlf]",
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001859 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001860 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg);
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001861 if (!chk) {
1862 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1863 goto error;
1864 }
1865 chk->index = 1;
1866 LIST_ADDQ(&rs->rules, &chk->list);
1867
1868 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[ \r]",
1869 "min-recv", "4",
1870 "error-status", "L7STS",
Christopher Faulete596d182020-05-05 17:46:34 +02001871 "on-error", "%[res.payload(4,0),ltrim(' '),cut_crlf]",
1872 "status-code", "res.payload(0,3)",
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001873 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001874 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg);
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001875 if (!chk) {
1876 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1877 goto error;
1878 }
1879 chk->index = 2;
1880 LIST_ADDQ(&rs->rules, &chk->list);
1881
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001882 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", smtp_req, ""},
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001883 1, curpx, &rs->rules, file, line, &errmsg);
1884 if (!chk) {
1885 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1886 goto error;
1887 }
1888 chk->index = 3;
1889 LIST_ADDQ(&rs->rules, &chk->list);
1890
1891 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[- \r]",
1892 "min-recv", "4",
1893 "error-status", "L7STS",
Christopher Faulete596d182020-05-05 17:46:34 +02001894 "on-error", "%[res.payload(4,0),ltrim(' '),cut_crlf]",
1895 "on-success", "%[res.payload(4,0),ltrim(' '),cut_crlf]",
1896 "status-code", "res.payload(0,3)",
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001897 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001898 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg);
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001899 if (!chk) {
1900 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
1901 goto error;
1902 }
1903 chk->index = 4;
1904 LIST_ADDQ(&rs->rules, &chk->list);
1905
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001906 ruleset_found:
1907 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02001908 rules->flags |= TCPCHK_RULES_SMTP_CHK;
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001909
1910 out:
1911 free(errmsg);
1912 return err_code;
1913
1914 error:
1915 free(cmd);
1916 free(var);
1917 free_tcpcheck_vars(&rules->preset_vars);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001918 free_tcpcheck_ruleset(rs);
Christopher Fauletfbcc77c2020-04-01 20:54:05 +02001919 err_code |= ERR_ALERT | ERR_FATAL;
1920 goto out;
1921}
Christopher Faulet811f78c2020-04-01 11:10:27 +02001922
Christopher Fauletce355072020-04-02 11:44:39 +02001923/* Parses the "option pgsql-check" proxy keyword */
1924int proxy_parse_pgsql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
1925 const char *file, int line)
1926{
1927 static char pgsql_req[] = {
1928 "%[var(check.plen),htonl,hex]" /* The packet length*/
1929 "00030000" /* the version 3.0 */
1930 "7573657200" /* "user" key */
1931 "%[var(check.username),hex]00" /* the username */
1932 "00"
1933 };
1934
1935 struct tcpcheck_ruleset *rs = NULL;
1936 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
1937 struct tcpcheck_rule *chk;
1938 struct tcpcheck_var *var = NULL;
1939 char *user = NULL, *errmsg = NULL;
1940 size_t packetlen = 0;
1941 int err_code = 0;
1942
1943 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
1944 err_code |= ERR_WARN;
1945
1946 if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code))
1947 goto out;
1948
Christopher Fauletce355072020-04-02 11:44:39 +02001949 curpx->options2 &= ~PR_O2_CHK_ANY;
1950 curpx->options2 |= PR_O2_TCPCHK_CHK;
1951
1952 free_tcpcheck_vars(&rules->preset_vars);
1953 rules->list = NULL;
1954 rules->flags = 0;
1955
1956 cur_arg += 2;
1957 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1958 ha_alert("parsing [%s:%d] : '%s %s' expects 'user <username>' as argument.\n",
1959 file, line, args[0], args[1]);
1960 goto error;
1961 }
1962 if (strcmp(args[cur_arg], "user") == 0) {
1963 packetlen = 15 + strlen(args[cur_arg+1]);
1964 user = strdup(args[cur_arg+1]);
1965
Christopher Fauletb61caf42020-04-21 10:57:42 +02001966 var = create_tcpcheck_var(ist("check.username"));
Christopher Fauletce355072020-04-02 11:44:39 +02001967 if (user == NULL || var == NULL) {
1968 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
1969 goto error;
1970 }
1971 var->data.type = SMP_T_STR;
1972 var->data.u.str.area = user;
1973 var->data.u.str.data = strlen(user);
1974 LIST_INIT(&var->list);
1975 LIST_ADDQ(&rules->preset_vars, &var->list);
1976 user = NULL;
1977 var = NULL;
1978
Christopher Fauletb61caf42020-04-21 10:57:42 +02001979 var = create_tcpcheck_var(ist("check.plen"));
Christopher Fauletce355072020-04-02 11:44:39 +02001980 if (var == NULL) {
1981 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
1982 goto error;
1983 }
1984 var->data.type = SMP_T_SINT;
1985 var->data.u.sint = packetlen;
1986 LIST_INIT(&var->list);
1987 LIST_ADDQ(&rules->preset_vars, &var->list);
1988 var = NULL;
1989 }
1990 else {
1991 ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user'.\n",
1992 file, line, args[0], args[1]);
1993 goto error;
1994 }
1995
Christopher Faulet61cc8522020-04-20 14:54:42 +02001996 rs = find_tcpcheck_ruleset("*pgsql-check");
Christopher Fauletce355072020-04-02 11:44:39 +02001997 if (rs)
1998 goto ruleset_found;
1999
Christopher Faulet61cc8522020-04-20 14:54:42 +02002000 rs = create_tcpcheck_ruleset("*pgsql-check");
Christopher Fauletce355072020-04-02 11:44:39 +02002001 if (rs == NULL) {
2002 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2003 goto error;
2004 }
2005
2006 chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""},
2007 1, curpx, &rs->rules, file, line, &errmsg);
2008 if (!chk) {
2009 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2010 goto error;
2011 }
2012 chk->index = 0;
2013 LIST_ADDQ(&rs->rules, &chk->list);
2014
Christopher Fauletb50b3e62020-05-05 18:43:43 +02002015 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", pgsql_req, ""},
Christopher Fauletce355072020-04-02 11:44:39 +02002016 1, curpx, &rs->rules, file, line, &errmsg);
2017 if (!chk) {
2018 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2019 goto error;
2020 }
2021 chk->index = 1;
2022 LIST_ADDQ(&rs->rules, &chk->list);
2023
2024 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "!rstring", "^E",
2025 "min-recv", "5",
2026 "error-status", "L7RSP",
Christopher Faulete596d182020-05-05 17:46:34 +02002027 "on-error", "%[res.payload(6,0)]",
Christopher Fauletce355072020-04-02 11:44:39 +02002028 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002029 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg);
Christopher Fauletce355072020-04-02 11:44:39 +02002030 if (!chk) {
2031 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2032 goto error;
2033 }
2034 chk->index = 2;
2035 LIST_ADDQ(&rs->rules, &chk->list);
2036
Christopher Fauletb841c742020-04-27 18:29:49 +02002037 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^52000000(08|0A|0C)000000(00|02|03|04|05|06)",
Christopher Fauletce355072020-04-02 11:44:39 +02002038 "min-recv", "9",
2039 "error-status", "L7STS",
2040 "on-success", "PostgreSQL server is ok",
2041 "on-error", "PostgreSQL unknown error",
2042 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002043 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg);
Christopher Fauletce355072020-04-02 11:44:39 +02002044 if (!chk) {
2045 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2046 goto error;
2047 }
2048 chk->index = 3;
2049 LIST_ADDQ(&rs->rules, &chk->list);
2050
Christopher Fauletce355072020-04-02 11:44:39 +02002051 ruleset_found:
2052 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02002053 rules->flags |= TCPCHK_RULES_PGSQL_CHK;
Christopher Fauletce355072020-04-02 11:44:39 +02002054
2055 out:
2056 free(errmsg);
2057 return err_code;
2058
2059 error:
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002060 free(user);
2061 free(var);
2062 free_tcpcheck_vars(&rules->preset_vars);
Christopher Faulet61cc8522020-04-20 14:54:42 +02002063 free_tcpcheck_ruleset(rs);
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002064 err_code |= ERR_ALERT | ERR_FATAL;
2065 goto out;
2066}
2067
2068
2069/* Parses the "option mysql-check" proxy keyword */
2070int proxy_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
2071 const char *file, int line)
2072{
2073 /* This is an example of a MySQL >=4.0 client Authentication packet kindly provided by Cyril Bonte.
2074 * const char mysql40_client_auth_pkt[] = {
2075 * "\x0e\x00\x00" // packet length
2076 * "\x01" // packet number
2077 * "\x00\x00" // client capabilities
2078 * "\x00\x00\x01" // max packet
2079 * "haproxy\x00" // username (null terminated string)
2080 * "\x00" // filler (always 0x00)
2081 * "\x01\x00\x00" // packet length
2082 * "\x00" // packet number
2083 * "\x01" // COM_QUIT command
2084 * };
2085 */
2086 static char mysql40_rsname[] = "*mysql40-check";
2087 static char mysql40_req[] = {
2088 "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */
2089 "0080" /* client capabilities */
2090 "000001" /* max packet */
2091 "%[var(check.username),hex]00" /* the username */
2092 "00" /* filler (always 0x00) */
2093 "010000" /* packet length*/
2094 "00" /* sequence ID */
2095 "01" /* COM_QUIT command */
2096 };
2097
2098 /* This is an example of a MySQL >=4.1 client Authentication packet provided by Nenad Merdanovic.
2099 * const char mysql41_client_auth_pkt[] = {
2100 * "\x0e\x00\x00\" // packet length
2101 * "\x01" // packet number
2102 * "\x00\x00\x00\x00" // client capabilities
2103 * "\x00\x00\x00\x01" // max packet
2104 * "\x21" // character set (UTF-8)
2105 * char[23] // All zeroes
2106 * "haproxy\x00" // username (null terminated string)
2107 * "\x00" // filler (always 0x00)
2108 * "\x01\x00\x00" // packet length
2109 * "\x00" // packet number
2110 * "\x01" // COM_QUIT command
2111 * };
2112 */
2113 static char mysql41_rsname[] = "*mysql41-check";
2114 static char mysql41_req[] = {
2115 "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */
2116 "00820000" /* client capabilities */
2117 "00800001" /* max packet */
2118 "21" /* character set (UTF-8) */
2119 "000000000000000000000000" /* 23 bytes, al zeroes */
2120 "0000000000000000000000"
2121 "%[var(check.username),hex]00" /* the username */
2122 "00" /* filler (always 0x00) */
2123 "010000" /* packet length*/
2124 "00" /* sequence ID */
2125 "01" /* COM_QUIT command */
2126 };
2127
2128 struct tcpcheck_ruleset *rs = NULL;
2129 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
2130 struct tcpcheck_rule *chk;
2131 struct tcpcheck_var *var = NULL;
2132 char *mysql_rsname = "*mysql-check";
2133 char *mysql_req = NULL, *hdr = NULL, *user = NULL, *errmsg = NULL;
2134 int index = 0, err_code = 0;
2135
2136 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
2137 err_code |= ERR_WARN;
2138
2139 if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code))
2140 goto out;
2141
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002142 curpx->options2 &= ~PR_O2_CHK_ANY;
2143 curpx->options2 |= PR_O2_TCPCHK_CHK;
2144
2145 free_tcpcheck_vars(&rules->preset_vars);
2146 rules->list = NULL;
2147 rules->flags = 0;
2148
2149 cur_arg += 2;
2150 if (*args[cur_arg]) {
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002151 int packetlen, userlen;
2152
2153 if (strcmp(args[cur_arg], "user") != 0) {
2154 ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user' (got '%s').\n",
2155 file, line, args[0], args[1], args[cur_arg]);
2156 goto error;
2157 }
2158
2159 if (*(args[cur_arg+1]) == 0) {
2160 ha_alert("parsing [%s:%d] : '%s %s %s' expects <username> as argument.\n",
2161 file, line, args[0], args[1], args[cur_arg]);
2162 goto error;
2163 }
2164
2165 hdr = calloc(4, sizeof(*hdr));
2166 user = strdup(args[cur_arg+1]);
2167 userlen = strlen(args[cur_arg+1]);
2168
2169 if (hdr == NULL || user == NULL) {
2170 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2171 goto error;
2172 }
2173
Christopher Faulet62f79fe2020-05-18 18:13:03 +02002174 if (!*args[cur_arg+2] || strcmp(args[cur_arg+2], "post-41") == 0) {
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002175 packetlen = userlen + 7 + 27;
2176 mysql_req = mysql41_req;
2177 mysql_rsname = mysql41_rsname;
2178 }
Christopher Faulet62f79fe2020-05-18 18:13:03 +02002179 else if (strcmp(args[cur_arg+2], "pre-41") == 0) {
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002180 packetlen = userlen + 7;
2181 mysql_req = mysql40_req;
2182 mysql_rsname = mysql40_rsname;
2183 }
Christopher Faulet62f79fe2020-05-18 18:13:03 +02002184 else {
2185 ha_alert("parsing [%s:%d] : keyword '%s' only supports 'post-41' and 'pre-41' (got '%s').\n",
2186 file, line, args[cur_arg], args[cur_arg+2]);
2187 goto error;
2188 }
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002189
2190 hdr[0] = (unsigned char)(packetlen & 0xff);
2191 hdr[1] = (unsigned char)((packetlen >> 8) & 0xff);
2192 hdr[2] = (unsigned char)((packetlen >> 16) & 0xff);
2193 hdr[3] = 1;
2194
Christopher Fauletb61caf42020-04-21 10:57:42 +02002195 var = create_tcpcheck_var(ist("check.header"));
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002196 if (var == NULL) {
2197 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2198 goto error;
2199 }
2200 var->data.type = SMP_T_STR;
2201 var->data.u.str.area = hdr;
2202 var->data.u.str.data = 4;
2203 LIST_INIT(&var->list);
2204 LIST_ADDQ(&rules->preset_vars, &var->list);
2205 hdr = NULL;
2206 var = NULL;
2207
Christopher Fauletb61caf42020-04-21 10:57:42 +02002208 var = create_tcpcheck_var(ist("check.username"));
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002209 if (var == NULL) {
2210 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2211 goto error;
2212 }
2213 var->data.type = SMP_T_STR;
2214 var->data.u.str.area = user;
2215 var->data.u.str.data = strlen(user);
2216 LIST_INIT(&var->list);
2217 LIST_ADDQ(&rules->preset_vars, &var->list);
2218 user = NULL;
2219 var = NULL;
2220 }
2221
Christopher Faulet61cc8522020-04-20 14:54:42 +02002222 rs = find_tcpcheck_ruleset(mysql_rsname);
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002223 if (rs)
2224 goto ruleset_found;
2225
Christopher Faulet61cc8522020-04-20 14:54:42 +02002226 rs = create_tcpcheck_ruleset(mysql_rsname);
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002227 if (rs == NULL) {
2228 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2229 goto error;
2230 }
2231
2232 chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""},
2233 1, curpx, &rs->rules, file, line, &errmsg);
2234 if (!chk) {
2235 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2236 goto error;
2237 }
2238 chk->index = index++;
2239 LIST_ADDQ(&rs->rules, &chk->list);
2240
2241 if (mysql_req) {
Christopher Fauletb50b3e62020-05-05 18:43:43 +02002242 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", mysql_req, ""},
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002243 1, curpx, &rs->rules, file, line, &errmsg);
2244 if (!chk) {
2245 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2246 goto error;
2247 }
2248 chk->index = index++;
2249 LIST_ADDQ(&rs->rules, &chk->list);
2250 }
2251
2252 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002253 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg);
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002254 if (!chk) {
2255 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2256 goto error;
2257 }
2258 chk->expect.custom = tcpcheck_mysql_expect_iniths;
2259 chk->index = index++;
2260 LIST_ADDQ(&rs->rules, &chk->list);
2261
2262 if (mysql_req) {
2263 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002264 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg);
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002265 if (!chk) {
2266 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2267 goto error;
2268 }
2269 chk->expect.custom = tcpcheck_mysql_expect_ok;
2270 chk->index = index++;
2271 LIST_ADDQ(&rs->rules, &chk->list);
2272 }
2273
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002274 ruleset_found:
2275 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02002276 rules->flags |= TCPCHK_RULES_MYSQL_CHK;
Christopher Fauletf2b3be52020-04-02 18:07:37 +02002277
2278 out:
2279 free(errmsg);
2280 return err_code;
2281
2282 error:
2283 free(hdr);
Christopher Fauletce355072020-04-02 11:44:39 +02002284 free(user);
2285 free(var);
2286 free_tcpcheck_vars(&rules->preset_vars);
Christopher Faulet61cc8522020-04-20 14:54:42 +02002287 free_tcpcheck_ruleset(rs);
Christopher Fauletce355072020-04-02 11:44:39 +02002288 err_code |= ERR_ALERT | ERR_FATAL;
2289 goto out;
2290}
2291
Christopher Faulet1997eca2020-04-03 23:13:50 +02002292int proxy_parse_ldap_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
2293 const char *file, int line)
2294{
2295 static char *ldap_req = "300C020101600702010304008000";
2296
2297 struct tcpcheck_ruleset *rs = NULL;
2298 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
2299 struct tcpcheck_rule *chk;
2300 char *errmsg = NULL;
2301 int err_code = 0;
2302
2303 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
2304 err_code |= ERR_WARN;
2305
2306 if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code))
2307 goto out;
Christopher Faulet1997eca2020-04-03 23:13:50 +02002308
2309 curpx->options2 &= ~PR_O2_CHK_ANY;
2310 curpx->options2 |= PR_O2_TCPCHK_CHK;
2311
2312 free_tcpcheck_vars(&rules->preset_vars);
2313 rules->list = NULL;
2314 rules->flags = 0;
2315
Christopher Faulet61cc8522020-04-20 14:54:42 +02002316 rs = find_tcpcheck_ruleset("*ldap-check");
Christopher Faulet1997eca2020-04-03 23:13:50 +02002317 if (rs)
2318 goto ruleset_found;
2319
Christopher Faulet61cc8522020-04-20 14:54:42 +02002320 rs = create_tcpcheck_ruleset("*ldap-check");
Christopher Faulet1997eca2020-04-03 23:13:50 +02002321 if (rs == NULL) {
2322 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2323 goto error;
2324 }
2325
2326 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", ldap_req, ""},
2327 1, curpx, &rs->rules, file, line, &errmsg);
2328 if (!chk) {
2329 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2330 goto error;
2331 }
2332 chk->index = 0;
2333 LIST_ADDQ(&rs->rules, &chk->list);
2334
2335 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^30",
2336 "min-recv", "14",
2337 "on-error", "Not LDAPv3 protocol",
2338 ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002339 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg);
Christopher Faulet1997eca2020-04-03 23:13:50 +02002340 if (!chk) {
2341 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2342 goto error;
2343 }
2344 chk->index = 1;
2345 LIST_ADDQ(&rs->rules, &chk->list);
2346
2347 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002348 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg);
Christopher Faulet1997eca2020-04-03 23:13:50 +02002349 if (!chk) {
2350 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2351 goto error;
2352 }
2353 chk->expect.custom = tcpcheck_ldap_expect_bindrsp;
2354 chk->index = 2;
2355 LIST_ADDQ(&rs->rules, &chk->list);
2356
Christopher Faulet1997eca2020-04-03 23:13:50 +02002357 ruleset_found:
2358 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02002359 rules->flags |= TCPCHK_RULES_LDAP_CHK;
Christopher Faulet1997eca2020-04-03 23:13:50 +02002360
2361 out:
Christopher Faulet267b01b2020-04-04 10:27:09 +02002362 free(errmsg);
2363 return err_code;
2364
2365 error:
Christopher Faulet61cc8522020-04-20 14:54:42 +02002366 free_tcpcheck_ruleset(rs);
Christopher Faulet267b01b2020-04-04 10:27:09 +02002367 err_code |= ERR_ALERT | ERR_FATAL;
2368 goto out;
2369}
2370
2371int proxy_parse_spop_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
2372 const char *file, int line)
2373{
2374 struct tcpcheck_ruleset *rs = NULL;
2375 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
2376 struct tcpcheck_rule *chk;
2377 char *spop_req = NULL;
2378 char *errmsg = NULL;
2379 int spop_len = 0, err_code = 0;
2380
2381 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
2382 err_code |= ERR_WARN;
2383
2384 if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code))
2385 goto out;
2386
Christopher Faulet267b01b2020-04-04 10:27:09 +02002387 curpx->options2 &= ~PR_O2_CHK_ANY;
2388 curpx->options2 |= PR_O2_TCPCHK_CHK;
2389
2390 free_tcpcheck_vars(&rules->preset_vars);
2391 rules->list = NULL;
2392 rules->flags = 0;
2393
2394
Christopher Faulet61cc8522020-04-20 14:54:42 +02002395 rs = find_tcpcheck_ruleset("*spop-check");
Christopher Faulet267b01b2020-04-04 10:27:09 +02002396 if (rs)
2397 goto ruleset_found;
2398
Christopher Faulet61cc8522020-04-20 14:54:42 +02002399 rs = create_tcpcheck_ruleset("*spop-check");
Christopher Faulet267b01b2020-04-04 10:27:09 +02002400 if (rs == NULL) {
2401 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2402 goto error;
2403 }
2404
2405 if (spoe_prepare_healthcheck_request(&spop_req, &spop_len) == -1) {
2406 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2407 goto error;
2408 }
2409 chunk_reset(&trash);
2410 dump_binary(&trash, spop_req, spop_len);
2411 trash.area[trash.data] = '\0';
2412
2413 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", b_head(&trash), ""},
2414 1, curpx, &rs->rules, file, line, &errmsg);
2415 if (!chk) {
2416 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2417 goto error;
2418 }
2419 chk->index = 0;
2420 LIST_ADDQ(&rs->rules, &chk->list);
2421
2422 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", "min-recv", "4", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002423 1, curpx, &rs->rules, TCPCHK_RULES_SPOP_CHK, file, line, &errmsg);
Christopher Faulet267b01b2020-04-04 10:27:09 +02002424 if (!chk) {
2425 ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
2426 goto error;
2427 }
2428 chk->expect.custom = tcpcheck_spop_expect_agenthello;
2429 chk->index = 1;
2430 LIST_ADDQ(&rs->rules, &chk->list);
2431
Christopher Faulet267b01b2020-04-04 10:27:09 +02002432 ruleset_found:
2433 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02002434 rules->flags |= TCPCHK_RULES_SPOP_CHK;
Christopher Faulet267b01b2020-04-04 10:27:09 +02002435
2436 out:
2437 free(spop_req);
Christopher Faulet1997eca2020-04-03 23:13:50 +02002438 free(errmsg);
2439 return err_code;
2440
2441 error:
Christopher Faulet61cc8522020-04-20 14:54:42 +02002442 free_tcpcheck_ruleset(rs);
Christopher Faulet1997eca2020-04-03 23:13:50 +02002443 err_code |= ERR_ALERT | ERR_FATAL;
2444 goto out;
2445}
Christopher Fauletce355072020-04-02 11:44:39 +02002446
Christopher Faulete5870d82020-04-15 11:32:03 +02002447
2448struct tcpcheck_rule *proxy_parse_httpchk_req(char **args, int cur_arg, struct proxy *px, char **errmsg)
2449{
2450 struct tcpcheck_rule *chk = NULL;
2451 struct tcpcheck_http_hdr *hdr = NULL;
2452 char *meth = NULL, *uri = NULL, *vsn = NULL;
2453 char *hdrs, *body;
2454
2455 hdrs = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n") : NULL);
2456 body = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n\r\n") : NULL);
2457 if (hdrs == body)
2458 hdrs = NULL;
2459 if (hdrs) {
2460 *hdrs = '\0';
2461 hdrs +=2;
2462 }
2463 if (body) {
2464 *body = '\0';
2465 body += 4;
2466 }
2467 if (hdrs || body) {
2468 memprintf(errmsg, "hiding headers or body at the end of the version string is deprecated."
2469 " Please, consider to use 'http-check send' directive instead.");
2470 }
2471
2472 chk = calloc(1, sizeof(*chk));
2473 if (!chk) {
2474 memprintf(errmsg, "out of memory");
2475 goto error;
2476 }
2477 chk->action = TCPCHK_ACT_SEND;
2478 chk->send.type = TCPCHK_SEND_HTTP;
2479 chk->send.http.flags |= TCPCHK_SND_HTTP_FROM_OPT;
2480 chk->send.http.meth.meth = HTTP_METH_OPTIONS;
2481 LIST_INIT(&chk->send.http.hdrs);
2482
2483 /* Copy the method, uri and version */
2484 if (*args[cur_arg]) {
2485 if (!*args[cur_arg+1])
2486 uri = args[cur_arg];
2487 else
2488 meth = args[cur_arg];
2489 }
2490 if (*args[cur_arg+1])
2491 uri = args[cur_arg+1];
2492 if (*args[cur_arg+2])
2493 vsn = args[cur_arg+2];
2494
2495 if (meth) {
2496 chk->send.http.meth.meth = find_http_meth(meth, strlen(meth));
2497 chk->send.http.meth.str.area = strdup(meth);
2498 chk->send.http.meth.str.data = strlen(meth);
2499 if (!chk->send.http.meth.str.area) {
2500 memprintf(errmsg, "out of memory");
2501 goto error;
2502 }
2503 }
2504 if (uri) {
2505 chk->send.http.uri = ist2(strdup(uri), strlen(uri));
Christopher Fauletb61caf42020-04-21 10:57:42 +02002506 if (!isttest(chk->send.http.uri)) {
Christopher Faulete5870d82020-04-15 11:32:03 +02002507 memprintf(errmsg, "out of memory");
2508 goto error;
2509 }
2510 }
2511 if (vsn) {
2512 chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn));
Christopher Fauletb61caf42020-04-21 10:57:42 +02002513 if (!isttest(chk->send.http.vsn)) {
Christopher Faulete5870d82020-04-15 11:32:03 +02002514 memprintf(errmsg, "out of memory");
2515 goto error;
2516 }
2517 }
2518
2519 /* Copy the header */
2520 if (hdrs) {
2521 struct http_hdr tmp_hdrs[global.tune.max_http_hdr];
2522 struct h1m h1m;
2523 int i, ret;
2524
2525 /* Build and parse the request */
2526 chunk_printf(&trash, "%s\r\n\r\n", hdrs);
2527
2528 h1m.flags = H1_MF_HDRS_ONLY;
2529 ret = h1_headers_to_hdr_list(b_orig(&trash), b_tail(&trash),
2530 tmp_hdrs, sizeof(tmp_hdrs)/sizeof(tmp_hdrs[0]),
2531 &h1m, NULL);
2532 if (ret <= 0) {
2533 memprintf(errmsg, "unable to parse the request '%s'.", b_orig(&trash));
2534 goto error;
2535 }
2536
Christopher Fauletb61caf42020-04-21 10:57:42 +02002537 for (i = 0; istlen(tmp_hdrs[i].n); i++) {
Christopher Faulete5870d82020-04-15 11:32:03 +02002538 hdr = calloc(1, sizeof(*hdr));
2539 if (!hdr) {
2540 memprintf(errmsg, "out of memory");
2541 goto error;
2542 }
2543 LIST_INIT(&hdr->value);
Christopher Fauletb61caf42020-04-21 10:57:42 +02002544 hdr->name = istdup(tmp_hdrs[i].n);
Christopher Faulete5870d82020-04-15 11:32:03 +02002545 if (!hdr->name.ptr) {
2546 memprintf(errmsg, "out of memory");
2547 goto error;
2548 }
2549
Christopher Fauletb61caf42020-04-21 10:57:42 +02002550 ist0(tmp_hdrs[i].v);
2551 if (!parse_logformat_string(istptr(tmp_hdrs[i].v), px, &hdr->value, 0, SMP_VAL_BE_CHK_RUL, errmsg))
Christopher Faulete5870d82020-04-15 11:32:03 +02002552 goto error;
2553 LIST_ADDQ(&chk->send.http.hdrs, &hdr->list);
2554 }
2555 }
2556
2557 /* Copy the body */
2558 if (body) {
2559 chk->send.http.body = ist2(strdup(body), strlen(body));
Christopher Fauletb61caf42020-04-21 10:57:42 +02002560 if (!isttest(chk->send.http.body)) {
Christopher Faulete5870d82020-04-15 11:32:03 +02002561 memprintf(errmsg, "out of memory");
2562 goto error;
2563 }
2564 }
2565
2566 return chk;
2567
2568 error:
2569 free_tcpcheck_http_hdr(hdr);
2570 free_tcpcheck(chk, 0);
2571 return NULL;
2572}
2573
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002574int proxy_parse_httpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx,
2575 const char *file, int line)
2576{
Christopher Faulete5870d82020-04-15 11:32:03 +02002577 struct tcpcheck_ruleset *rs = NULL;
2578 struct tcpcheck_rules *rules = &curpx->tcpcheck_rules;
2579 struct tcpcheck_rule *chk;
2580 char *errmsg = NULL;
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002581 int err_code = 0;
2582
2583 if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL))
2584 err_code |= ERR_WARN;
2585
2586 if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code))
2587 goto out;
2588
Christopher Faulete5870d82020-04-15 11:32:03 +02002589 chk = proxy_parse_httpchk_req(args, cur_arg+2, curpx, &errmsg);
2590 if (!chk) {
2591 ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg);
2592 goto error;
2593 }
2594 if (errmsg) {
2595 ha_warning("parsing [%s:%d]: '%s %s' : %s\n", file, line, args[0], args[1], errmsg);
2596 err_code |= ERR_WARN;
2597 free(errmsg);
2598 errmsg = NULL;
2599 }
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002600
Christopher Faulete5870d82020-04-15 11:32:03 +02002601 no_request:
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002602 curpx->options2 &= ~PR_O2_CHK_ANY;
Christopher Faulete5870d82020-04-15 11:32:03 +02002603 curpx->options2 |= PR_O2_TCPCHK_CHK;
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002604
Christopher Faulete5870d82020-04-15 11:32:03 +02002605 free_tcpcheck_vars(&rules->preset_vars);
2606 rules->list = NULL;
2607 rules->flags |= TCPCHK_SND_HTTP_FROM_OPT;
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002608
Christopher Faulete5870d82020-04-15 11:32:03 +02002609 /* Deduce the ruleset name from the proxy info */
2610 chunk_printf(&trash, "*http-check-%s_%s-%d",
2611 ((curpx == defpx) ? "defaults" : curpx->id),
2612 curpx->conf.file, curpx->conf.line);
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002613
Christopher Faulet61cc8522020-04-20 14:54:42 +02002614 rs = find_tcpcheck_ruleset(b_orig(&trash));
Christopher Faulete5870d82020-04-15 11:32:03 +02002615 if (rs == NULL) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02002616 rs = create_tcpcheck_ruleset(b_orig(&trash));
Christopher Faulete5870d82020-04-15 11:32:03 +02002617 if (rs == NULL) {
2618 ha_alert("parsing [%s:%d] : out of memory.\n", file, line);
2619 goto error;
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002620 }
Christopher Faulete5870d82020-04-15 11:32:03 +02002621 }
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002622
Christopher Faulete5870d82020-04-15 11:32:03 +02002623 rules->list = &rs->rules;
2624 rules->flags |= TCPCHK_RULES_HTTP_CHK;
Christopher Fauletc751b452020-11-13 08:55:57 +01002625 rules->flags &= ~TCPCHK_RULES_UNUSED_HTTP_RS;
Christopher Faulete5870d82020-04-15 11:32:03 +02002626 if (!tcpcheck_add_http_rule(chk, rules, &errmsg)) {
2627 ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg);
2628 rules->list = NULL;
2629 goto error;
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002630 }
Christopher Faulete5870d82020-04-15 11:32:03 +02002631
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002632 out:
Christopher Faulete5870d82020-04-15 11:32:03 +02002633 free(errmsg);
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002634 return err_code;
2635
2636 error:
Christopher Faulet61cc8522020-04-20 14:54:42 +02002637 free_tcpcheck_ruleset(rs);
Christopher Faulete5870d82020-04-15 11:32:03 +02002638 free_tcpcheck(chk, 0);
Christopher Faulet6c2a7432020-04-09 14:48:48 +02002639 err_code |= ERR_ALERT | ERR_FATAL;
2640 goto out;
2641}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002642
Christopher Fauletce8111e2020-04-06 15:04:11 +02002643/* Parse the "addr" server keyword */
2644static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2645 char **errmsg)
2646{
2647 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002648 int port1, port2, err_code = 0;
2649
2650
2651 if (!*args[*cur_arg+1]) {
2652 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
2653 goto error;
2654 }
2655
Willy Tarreau65ec4e32020-09-16 19:17:08 +02002656 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
2657 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02002658 if (!sk) {
2659 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
2660 goto error;
2661 }
2662
Christopher Fauletce8111e2020-04-06 15:04:11 +02002663 srv->check.addr = srv->agent.addr = *sk;
2664 srv->flags |= SRV_F_CHECKADDR;
2665 srv->flags |= SRV_F_AGENTADDR;
2666
2667 out:
2668 return err_code;
2669
2670 error:
2671 err_code |= ERR_ALERT | ERR_FATAL;
2672 goto out;
2673}
2674
2675
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002676/* Parse the "agent-addr" server keyword */
2677static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2678 char **errmsg)
2679{
2680 int err_code = 0;
2681
2682 if (!*(args[*cur_arg+1])) {
2683 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
2684 goto error;
2685 }
2686 if(str2ip(args[*cur_arg+1], &srv->agent.addr) == NULL) {
2687 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
2688 goto error;
2689 }
2690
2691 out:
2692 return err_code;
2693
2694 error:
2695 err_code |= ERR_ALERT | ERR_FATAL;
2696 goto out;
2697}
2698
2699/* Parse the "agent-check" server keyword */
2700static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2701 char **errmsg)
2702{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002703 struct tcpcheck_ruleset *rs = NULL;
2704 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
2705 struct tcpcheck_rule *chk;
2706 int err_code = 0;
2707
2708 if (srv->do_agent)
2709 goto out;
2710
2711 if (!rules) {
2712 rules = calloc(1, sizeof(*rules));
2713 if (!rules) {
2714 memprintf(errmsg, "out of memory.");
2715 goto error;
2716 }
2717 LIST_INIT(&rules->preset_vars);
2718 srv->agent.tcpcheck_rules = rules;
2719 }
2720 rules->list = NULL;
2721 rules->flags = 0;
2722
Christopher Faulet61cc8522020-04-20 14:54:42 +02002723 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002724 if (rs)
2725 goto ruleset_found;
2726
Christopher Faulet61cc8522020-04-20 14:54:42 +02002727 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002728 if (rs == NULL) {
2729 memprintf(errmsg, "out of memory.");
2730 goto error;
2731 }
2732
Christopher Fauletb50b3e62020-05-05 18:43:43 +02002733 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002734 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
2735 if (!chk) {
2736 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
2737 goto error;
2738 }
2739 chk->index = 0;
2740 LIST_ADDQ(&rs->rules, &chk->list);
2741
2742 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02002743 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
2744 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002745 if (!chk) {
2746 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
2747 goto error;
2748 }
2749 chk->expect.custom = tcpcheck_agent_expect_reply;
2750 chk->index = 1;
2751 LIST_ADDQ(&rs->rules, &chk->list);
2752
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002753 ruleset_found:
2754 rules->list = &rs->rules;
Christopher Faulet404f9192020-04-09 23:13:54 +02002755 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002756 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002757
2758 out:
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002759 return 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002760
2761 error:
2762 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02002763 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002764 err_code |= ERR_ALERT | ERR_FATAL;
2765 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002766}
2767
2768/* Parse the "agent-inter" server keyword */
2769static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2770 char **errmsg)
2771{
2772 const char *err = NULL;
2773 unsigned int delay;
2774 int err_code = 0;
2775
2776 if (!*(args[*cur_arg+1])) {
2777 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2778 goto error;
2779 }
2780
2781 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2782 if (err == PARSE_TIME_OVER) {
2783 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2784 args[*cur_arg+1], args[*cur_arg], srv->id);
2785 goto error;
2786 }
2787 else if (err == PARSE_TIME_UNDER) {
2788 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2789 args[*cur_arg+1], args[*cur_arg], srv->id);
2790 goto error;
2791 }
2792 else if (err) {
2793 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2794 *err, srv->id);
2795 goto error;
2796 }
2797 if (delay <= 0) {
2798 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2799 delay, args[*cur_arg], srv->id);
2800 goto error;
2801 }
2802 srv->agent.inter = delay;
2803
2804 out:
2805 return err_code;
2806
2807 error:
2808 err_code |= ERR_ALERT | ERR_FATAL;
2809 goto out;
2810}
2811
2812/* Parse the "agent-port" server keyword */
2813static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2814 char **errmsg)
2815{
2816 int err_code = 0;
2817
2818 if (!*(args[*cur_arg+1])) {
2819 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2820 goto error;
2821 }
2822
2823 global.maxsock++;
2824 srv->agent.port = atol(args[*cur_arg+1]);
2825
2826 out:
2827 return err_code;
2828
2829 error:
2830 err_code |= ERR_ALERT | ERR_FATAL;
2831 goto out;
2832}
2833
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002834int set_srv_agent_send(struct server *srv, const char *send)
2835{
2836 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
2837 struct tcpcheck_var *var = NULL;
2838 char *str;
2839
2840 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02002841 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002842 if (str == NULL || var == NULL)
2843 goto error;
2844
2845 free_tcpcheck_vars(&rules->preset_vars);
2846
2847 var->data.type = SMP_T_STR;
2848 var->data.u.str.area = str;
2849 var->data.u.str.data = strlen(str);
2850 LIST_INIT(&var->list);
2851 LIST_ADDQ(&rules->preset_vars, &var->list);
2852
2853 return 1;
2854
2855 error:
2856 free(str);
2857 free(var);
2858 return 0;
2859}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002860
2861/* Parse the "agent-send" server keyword */
2862static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2863 char **errmsg)
2864{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002865 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002866 int err_code = 0;
2867
2868 if (!*(args[*cur_arg+1])) {
2869 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2870 goto error;
2871 }
2872
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002873 if (!rules) {
2874 rules = calloc(1, sizeof(*rules));
2875 if (!rules) {
2876 memprintf(errmsg, "out of memory.");
2877 goto error;
2878 }
2879 LIST_INIT(&rules->preset_vars);
2880 srv->agent.tcpcheck_rules = rules;
2881 }
2882
2883 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002884 memprintf(errmsg, "out of memory.");
2885 goto error;
2886 }
2887
2888 out:
2889 return err_code;
2890
2891 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002892 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002893 err_code |= ERR_ALERT | ERR_FATAL;
2894 goto out;
2895}
2896
2897/* Parse the "no-agent-send" server keyword */
2898static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2899 char **errmsg)
2900{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002901 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002902 return 0;
2903}
2904
Christopher Fauletce8111e2020-04-06 15:04:11 +02002905/* Parse the "check" server keyword */
2906static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2907 char **errmsg)
2908{
2909 srv->do_check = 1;
2910 return 0;
2911}
2912
2913/* Parse the "check-send-proxy" server keyword */
2914static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2915 char **errmsg)
2916{
2917 srv->check.send_proxy = 1;
2918 return 0;
2919}
2920
2921/* Parse the "check-via-socks4" server keyword */
2922static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2923 char **errmsg)
2924{
2925 srv->check.via_socks4 = 1;
2926 return 0;
2927}
2928
2929/* Parse the "no-check" server keyword */
2930static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2931 char **errmsg)
2932{
2933 deinit_srv_check(srv);
2934 return 0;
2935}
2936
2937/* Parse the "no-check-send-proxy" server keyword */
2938static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2939 char **errmsg)
2940{
2941 srv->check.send_proxy = 0;
2942 return 0;
2943}
2944
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002945/* parse the "check-proto" server keyword */
2946static int srv_parse_check_proto(char **args, int *cur_arg,
2947 struct proxy *px, struct server *newsrv, char **err)
2948{
2949 int err_code = 0;
2950
2951 if (!*args[*cur_arg + 1]) {
2952 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2953 goto error;
2954 }
2955 newsrv->check.mux_proto = get_mux_proto(ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1])));
2956 if (!newsrv->check.mux_proto) {
2957 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2958 goto error;
2959 }
2960
2961 out:
2962 return err_code;
2963
2964 error:
2965 err_code |= ERR_ALERT | ERR_FATAL;
2966 goto out;
2967}
2968
2969
Christopher Fauletce8111e2020-04-06 15:04:11 +02002970/* Parse the "rise" server keyword */
2971static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2972 char **errmsg)
2973{
2974 int err_code = 0;
2975
2976 if (!*args[*cur_arg + 1]) {
2977 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2978 goto error;
2979 }
2980
2981 srv->check.rise = atol(args[*cur_arg+1]);
2982 if (srv->check.rise <= 0) {
2983 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2984 goto error;
2985 }
2986
2987 if (srv->check.health)
2988 srv->check.health = srv->check.rise;
2989
2990 out:
2991 return err_code;
2992
2993 error:
2994 deinit_srv_agent_check(srv);
2995 err_code |= ERR_ALERT | ERR_FATAL;
2996 goto out;
2997 return 0;
2998}
2999
3000/* Parse the "fall" server keyword */
3001static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
3002 char **errmsg)
3003{
3004 int err_code = 0;
3005
3006 if (!*args[*cur_arg + 1]) {
3007 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
3008 goto error;
3009 }
3010
3011 srv->check.fall = atol(args[*cur_arg+1]);
3012 if (srv->check.fall <= 0) {
3013 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
3014 goto error;
3015 }
3016
3017 out:
3018 return err_code;
3019
3020 error:
3021 deinit_srv_agent_check(srv);
3022 err_code |= ERR_ALERT | ERR_FATAL;
3023 goto out;
3024 return 0;
3025}
3026
3027/* Parse the "inter" server keyword */
3028static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
3029 char **errmsg)
3030{
3031 const char *err = NULL;
3032 unsigned int delay;
3033 int err_code = 0;
3034
3035 if (!*(args[*cur_arg+1])) {
3036 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
3037 goto error;
3038 }
3039
3040 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
3041 if (err == PARSE_TIME_OVER) {
3042 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
3043 args[*cur_arg+1], args[*cur_arg], srv->id);
3044 goto error;
3045 }
3046 else if (err == PARSE_TIME_UNDER) {
3047 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
3048 args[*cur_arg+1], args[*cur_arg], srv->id);
3049 goto error;
3050 }
3051 else if (err) {
3052 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
3053 *err, srv->id);
3054 goto error;
3055 }
3056 if (delay <= 0) {
3057 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
3058 delay, args[*cur_arg], srv->id);
3059 goto error;
3060 }
3061 srv->check.inter = delay;
3062
3063 out:
3064 return err_code;
3065
3066 error:
3067 err_code |= ERR_ALERT | ERR_FATAL;
3068 goto out;
3069}
3070
3071
3072/* Parse the "fastinter" server keyword */
3073static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
3074 char **errmsg)
3075{
3076 const char *err = NULL;
3077 unsigned int delay;
3078 int err_code = 0;
3079
3080 if (!*(args[*cur_arg+1])) {
3081 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
3082 goto error;
3083 }
3084
3085 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
3086 if (err == PARSE_TIME_OVER) {
3087 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
3088 args[*cur_arg+1], args[*cur_arg], srv->id);
3089 goto error;
3090 }
3091 else if (err == PARSE_TIME_UNDER) {
3092 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
3093 args[*cur_arg+1], args[*cur_arg], srv->id);
3094 goto error;
3095 }
3096 else if (err) {
3097 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
3098 *err, srv->id);
3099 goto error;
3100 }
3101 if (delay <= 0) {
3102 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
3103 delay, args[*cur_arg], srv->id);
3104 goto error;
3105 }
3106 srv->check.fastinter = delay;
3107
3108 out:
3109 return err_code;
3110
3111 error:
3112 err_code |= ERR_ALERT | ERR_FATAL;
3113 goto out;
3114}
3115
3116
3117/* Parse the "downinter" server keyword */
3118static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
3119 char **errmsg)
3120{
3121 const char *err = NULL;
3122 unsigned int delay;
3123 int err_code = 0;
3124
3125 if (!*(args[*cur_arg+1])) {
3126 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
3127 goto error;
3128 }
3129
3130 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
3131 if (err == PARSE_TIME_OVER) {
3132 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
3133 args[*cur_arg+1], args[*cur_arg], srv->id);
3134 goto error;
3135 }
3136 else if (err == PARSE_TIME_UNDER) {
3137 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
3138 args[*cur_arg+1], args[*cur_arg], srv->id);
3139 goto error;
3140 }
3141 else if (err) {
3142 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
3143 *err, srv->id);
3144 goto error;
3145 }
3146 if (delay <= 0) {
3147 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
3148 delay, args[*cur_arg], srv->id);
3149 goto error;
3150 }
3151 srv->check.downinter = delay;
3152
3153 out:
3154 return err_code;
3155
3156 error:
3157 err_code |= ERR_ALERT | ERR_FATAL;
3158 goto out;
3159}
3160
3161/* Parse the "port" server keyword */
3162static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
3163 char **errmsg)
3164{
3165 int err_code = 0;
3166
3167 if (!*(args[*cur_arg+1])) {
3168 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
3169 goto error;
3170 }
3171
3172 global.maxsock++;
3173 srv->check.port = atol(args[*cur_arg+1]);
3174 srv->flags |= SRV_F_CHECKPORT;
3175
3176 out:
3177 return err_code;
3178
3179 error:
3180 err_code |= ERR_ALERT | ERR_FATAL;
3181 goto out;
3182}
3183
Christopher Fauletfd6c2292020-03-25 18:20:15 +01003184static struct cfg_kw_list cfg_kws = {ILH, {
Christopher Faulete9111b62020-04-09 18:12:08 +02003185 { CFG_LISTEN, "http-check", proxy_parse_httpcheck },
Christopher Fauletfd6c2292020-03-25 18:20:15 +01003186 { 0, NULL, NULL },
3187}};
3188
Christopher Fauletcbba66c2020-04-06 14:26:30 +02003189static struct srv_kw_list srv_kws = { "CHK", { }, {
Christopher Fauletce8111e2020-04-06 15:04:11 +02003190 { "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 +02003191 { "agent-addr", srv_parse_agent_addr, 1, 1 }, /* Enable an auxiliary agent check */
3192 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable agent checks */
3193 { "agent-inter", srv_parse_agent_inter, 1, 1 }, /* Set the interval between two agent checks */
3194 { "agent-port", srv_parse_agent_port, 1, 1 }, /* Set the TCP port used for agent checks. */
3195 { "agent-send", srv_parse_agent_send, 1, 1 }, /* Set string to send to agent. */
Christopher Fauletce8111e2020-04-06 15:04:11 +02003196 { "check", srv_parse_check, 0, 1 }, /* Enable health checks */
Christopher Fauletedc6ed92020-04-23 16:27:59 +02003197 { "check-proto", srv_parse_check_proto, 1, 1 }, /* Set the mux protocol for health checks */
Christopher Fauletce8111e2020-04-06 15:04:11 +02003198 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* Enable PROXY protocol for health checks */
3199 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* Enable socks4 proxy for health checks */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02003200 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Christopher Fauletce8111e2020-04-06 15:04:11 +02003201 { "no-check", srv_parse_no_check, 0, 1 }, /* Disable health checks */
3202 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* Disable PROXY protol for health checks */
3203 { "rise", srv_parse_check_rise, 1, 1 }, /* Set rise value for health checks */
3204 { "fall", srv_parse_check_fall, 1, 1 }, /* Set fall value for health checks */
3205 { "inter", srv_parse_check_inter, 1, 1 }, /* Set inter value for health checks */
3206 { "fastinter", srv_parse_check_fastinter, 1, 1 }, /* Set fastinter value for health checks */
3207 { "downinter", srv_parse_check_downinter, 1, 1 }, /* Set downinter value for health checks */
3208 { "port", srv_parse_check_port, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02003209 { NULL, NULL, 0 },
3210}};
3211
Christopher Fauletfd6c2292020-03-25 18:20:15 +01003212INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02003213INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01003214
Willy Tarreaubd741542010-03-16 18:46:54 +01003215/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003216 * Local variables:
3217 * c-indent-level: 8
3218 * c-basic-offset: 8
3219 * End:
3220 */