Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Health-checks functions. |
| 3 | * |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 5 | * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 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 Tarreau | b881608 | 2008-01-18 12:18:15 +0100 | [diff] [blame] | 14 | #include <assert.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 15 | #include <ctype.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <errno.h> |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 17 | #include <stdarg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 21 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 23 | #include <sys/resource.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 25 | #include <sys/types.h> |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 26 | #include <sys/wait.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 28 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <arpa/inet.h> |
| 30 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 31 | #include <haproxy/action.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 32 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 33 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 34 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 35 | #include <haproxy/check.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 36 | #include <haproxy/chunk.h> |
Willy Tarreau | 7c18b54 | 2020-06-11 09:23:02 +0200 | [diff] [blame] | 37 | #include <haproxy/dgram.h> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 38 | #include <haproxy/dynbuf.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 39 | #include <haproxy/extcheck.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <haproxy/fd.h> |
| 41 | #include <haproxy/global.h> |
| 42 | #include <haproxy/h1.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 43 | #include <haproxy/http.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 44 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 45 | #include <haproxy/htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/istbuf.h> |
| 47 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 48 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 49 | #include <haproxy/mailers.h> |
| 50 | #include <haproxy/port_range.h> |
| 51 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 52 | #include <haproxy/protocol.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 53 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 54 | #include <haproxy/queue.h> |
| 55 | #include <haproxy/regex.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 56 | #include <haproxy/resolvers.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 57 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 58 | #include <haproxy/server.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 59 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 60 | #include <haproxy/stats-t.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 61 | #include <haproxy/task.h> |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 62 | #include <haproxy/tcpcheck.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 63 | #include <haproxy/thread.h> |
| 64 | #include <haproxy/time.h> |
| 65 | #include <haproxy/tools.h> |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 66 | #include <haproxy/trace.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 67 | #include <haproxy/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 69 | /* trace source and events */ |
| 70 | static void check_trace(enum trace_level level, uint64_t mask, |
| 71 | const struct trace_source *src, |
| 72 | const struct ist where, const struct ist func, |
| 73 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 74 | |
| 75 | /* The event representation is split like this : |
| 76 | * check - check |
| 77 | * |
| 78 | * CHECK_EV_* macros are defined in <haproxy/check.h> |
| 79 | */ |
| 80 | static const struct trace_event check_trace_events[] = { |
| 81 | { .mask = CHK_EV_TASK_WAKE, .name = "task_wake", .desc = "Check task woken up" }, |
| 82 | { .mask = CHK_EV_HCHK_START, .name = "hchck_start", .desc = "Health-check started" }, |
| 83 | { .mask = CHK_EV_HCHK_WAKE, .name = "hchck_wake", .desc = "Health-check woken up" }, |
| 84 | { .mask = CHK_EV_HCHK_RUN, .name = "hchck_run", .desc = "Health-check running" }, |
| 85 | { .mask = CHK_EV_HCHK_END, .name = "hchck_end", .desc = "Health-check terminated" }, |
| 86 | { .mask = CHK_EV_HCHK_SUCC, .name = "hchck_succ", .desc = "Health-check success" }, |
| 87 | { .mask = CHK_EV_HCHK_ERR, .name = "hchck_err", .desc = "Health-check failure" }, |
| 88 | |
| 89 | { .mask = CHK_EV_TCPCHK_EVAL, .name = "tcp_check_eval", .desc = "tcp-check rules evaluation" }, |
| 90 | { .mask = CHK_EV_TCPCHK_ERR, .name = "tcp_check_err", .desc = "tcp-check evaluation error" }, |
| 91 | { .mask = CHK_EV_TCPCHK_CONN, .name = "tcp_check_conn", .desc = "tcp-check connection rule" }, |
| 92 | { .mask = CHK_EV_TCPCHK_SND, .name = "tcp_check_send", .desc = "tcp-check send rule" }, |
| 93 | { .mask = CHK_EV_TCPCHK_EXP, .name = "tcp_check_expect", .desc = "tcp-check expect rule" }, |
| 94 | { .mask = CHK_EV_TCPCHK_ACT, .name = "tcp_check_action", .desc = "tcp-check action rule" }, |
| 95 | |
| 96 | { .mask = CHK_EV_RX_DATA, .name = "rx_data", .desc = "receipt of data" }, |
| 97 | { .mask = CHK_EV_RX_BLK, .name = "rx_blk", .desc = "receipt blocked" }, |
| 98 | { .mask = CHK_EV_RX_ERR, .name = "rx_err", .desc = "receipt error" }, |
| 99 | |
| 100 | { .mask = CHK_EV_TX_DATA, .name = "tx_data", .desc = "transmission of data" }, |
| 101 | { .mask = CHK_EV_TX_BLK, .name = "tx_blk", .desc = "transmission blocked" }, |
| 102 | { .mask = CHK_EV_TX_ERR, .name = "tx_err", .desc = "transmission error" }, |
| 103 | |
| 104 | {} |
| 105 | }; |
| 106 | |
| 107 | static const struct name_desc check_trace_lockon_args[4] = { |
| 108 | /* arg1 */ { /* already used by the check */ }, |
| 109 | /* arg2 */ { }, |
| 110 | /* arg3 */ { }, |
| 111 | /* arg4 */ { } |
| 112 | }; |
| 113 | |
| 114 | static const struct name_desc check_trace_decoding[] = { |
| 115 | #define CHK_VERB_CLEAN 1 |
| 116 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 117 | #define CHK_VERB_MINIMAL 2 |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 118 | { .name="minimal", .desc="report info on stream and conn-streams" }, |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 119 | #define CHK_VERB_SIMPLE 3 |
| 120 | { .name="simple", .desc="add info on request and response channels" }, |
| 121 | #define CHK_VERB_ADVANCED 4 |
| 122 | { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" }, |
| 123 | #define CHK_VERB_COMPLETE 5 |
| 124 | { .name="complete", .desc="add info on channel's buffer" }, |
| 125 | { /* end */ } |
| 126 | }; |
| 127 | |
| 128 | struct trace_source trace_check = { |
| 129 | .name = IST("check"), |
| 130 | .desc = "Health-check", |
| 131 | .arg_def = TRC_ARG1_CHK, // TRACE()'s first argument is always a stream |
| 132 | .default_cb = check_trace, |
| 133 | .known_events = check_trace_events, |
| 134 | .lockon_args = check_trace_lockon_args, |
| 135 | .decoding = check_trace_decoding, |
| 136 | .report_events = ~0, // report everything by default |
| 137 | }; |
| 138 | |
| 139 | #define TRACE_SOURCE &trace_check |
| 140 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 141 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 142 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 143 | static int wake_srv_chk(struct conn_stream *cs); |
| 144 | struct data_cb check_conn_cb = { |
| 145 | .wake = wake_srv_chk, |
| 146 | .name = "CHCK", |
| 147 | }; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 148 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 149 | |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 150 | /* Dummy frontend used to create all checks sessions. */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 151 | struct proxy checks_fe; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 152 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 153 | |
| 154 | static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len) |
| 155 | { |
| 156 | size_t block1, block2; |
| 157 | int line, ptr, newptr; |
| 158 | |
| 159 | block1 = b_contig_data(buf, ofs); |
| 160 | block2 = 0; |
| 161 | if (block1 > len) |
| 162 | block1 = len; |
| 163 | block2 = len - block1; |
| 164 | |
| 165 | ofs = b_peek_ofs(buf, ofs); |
| 166 | |
| 167 | line = 0; |
| 168 | ptr = ofs; |
| 169 | while (ptr < ofs + block1) { |
| 170 | newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr); |
| 171 | if (newptr == ptr) |
| 172 | break; |
| 173 | ptr = newptr; |
| 174 | } |
| 175 | |
| 176 | line = ptr = 0; |
| 177 | while (ptr < block2) { |
| 178 | newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr); |
| 179 | if (newptr == ptr) |
| 180 | break; |
| 181 | ptr = newptr; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* trace source and events */ |
| 186 | static void check_trace(enum trace_level level, uint64_t mask, |
| 187 | const struct trace_source *src, |
| 188 | const struct ist where, const struct ist func, |
| 189 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 190 | { |
| 191 | const struct check *check = a1; |
| 192 | const struct server *srv = (check ? check->server : NULL); |
| 193 | const size_t *val = a4; |
| 194 | const char *res; |
| 195 | |
| 196 | if (!check || src->verbosity < CHK_VERB_CLEAN) |
| 197 | return; |
| 198 | |
| 199 | chunk_appendf(&trace_buf, " : [%c] SRV=%s", |
| 200 | ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')), |
| 201 | srv->id); |
| 202 | |
| 203 | chunk_appendf(&trace_buf, " status=%d/%d %s", |
| 204 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 205 | (check->health >= check->rise) ? check->fall : check->rise, |
| 206 | (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN"); |
| 207 | |
| 208 | switch (check->result) { |
| 209 | case CHK_RES_NEUTRAL: res = "-"; break; |
| 210 | case CHK_RES_FAILED: res = "FAIL"; break; |
| 211 | case CHK_RES_PASSED: res = "PASS"; break; |
| 212 | case CHK_RES_CONDPASS: res = "COND"; break; |
| 213 | default: res = "UNK"; break; |
| 214 | } |
| 215 | |
| 216 | if (src->verbosity == CHK_VERB_CLEAN) |
| 217 | return; |
| 218 | |
| 219 | chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)", |
| 220 | get_check_status_info(check->status), check->status, |
| 221 | res, check->result); |
| 222 | |
| 223 | /* Display the value to the 4th argument (level > STATE) */ |
| 224 | if (src->level > TRACE_LEVEL_STATE && val) |
| 225 | chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val); |
| 226 | |
| 227 | chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state); |
| 228 | |
| 229 | if (src->verbosity == CHK_VERB_MINIMAL) |
| 230 | return; |
| 231 | |
| 232 | |
| 233 | if (check->cs) { |
Christopher Faulet | 0256da1 | 2021-12-15 09:50:17 +0100 | [diff] [blame] | 234 | struct connection *conn = cs_conn(check->cs); |
| 235 | |
| 236 | chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 237 | chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags); |
| 238 | } |
| 239 | |
| 240 | if (mask & CHK_EV_TCPCHK) { |
| 241 | const char *type; |
| 242 | |
| 243 | switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) { |
| 244 | case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break; |
| 245 | case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break; |
| 246 | case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break; |
| 247 | case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break; |
| 248 | case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break; |
| 249 | case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break; |
| 250 | case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break; |
| 251 | case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break; |
| 252 | case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break; |
| 253 | case TCPCHK_RULES_TCP_CHK: type = "TCP"; break; |
| 254 | default: type = "???"; break; |
| 255 | } |
| 256 | if (check->current_step) |
| 257 | chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL)); |
| 258 | else |
| 259 | chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type); |
| 260 | } |
| 261 | |
| 262 | /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */ |
| 263 | if (src->level > TRACE_LEVEL_USER) { |
| 264 | const struct buffer *buf = NULL; |
| 265 | |
| 266 | chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u", |
| 267 | (unsigned int)b_data(&check->bi), b_orig(&check->bi), |
| 268 | (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi)); |
| 269 | chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u", |
| 270 | (unsigned int)b_data(&check->bo), b_orig(&check->bo), |
| 271 | (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo)); |
| 272 | |
| 273 | if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX))) |
| 274 | buf = (b_is_null(&check->bi) ? NULL : &check->bi); |
| 275 | else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX))) |
| 276 | buf = (b_is_null(&check->bo) ? NULL : &check->bo); |
| 277 | |
| 278 | if (buf) { |
| 279 | if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 280 | int full = (src->verbosity == CHK_VERB_COMPLETE); |
| 281 | |
| 282 | chunk_memcat(&trace_buf, "\n\t", 2); |
| 283 | htx_dump(&trace_buf, htxbuf(buf), full); |
| 284 | } |
| 285 | else { |
| 286 | int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256); |
| 287 | |
| 288 | chunk_memcat(&trace_buf, "\n", 1); |
| 289 | if (b_data(buf) > max) { |
| 290 | check_trace_buf(buf, 0, max); |
| 291 | chunk_memcat(&trace_buf, " ...\n", 6); |
| 292 | } |
| 293 | else |
| 294 | check_trace_buf(buf, 0, b_data(buf)); |
| 295 | } |
| 296 | |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | } |
| 301 | |
| 302 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 303 | /**************************************************************************/ |
| 304 | /************************ Handle check results ****************************/ |
| 305 | /**************************************************************************/ |
| 306 | struct check_status { |
| 307 | short result; /* one of SRV_CHK_* */ |
| 308 | char *info; /* human readable short info */ |
| 309 | char *desc; /* long description */ |
| 310 | }; |
| 311 | |
| 312 | struct analyze_status { |
| 313 | char *desc; /* description */ |
| 314 | unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */ |
| 315 | }; |
| 316 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 317 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 318 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 319 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 320 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 321 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 322 | /* Below we have finished checks */ |
| 323 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 324 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 325 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 326 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 327 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 328 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 329 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 330 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 331 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 332 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 333 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 334 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 335 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 336 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 337 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 338 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 339 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 340 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 341 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 342 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 343 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 344 | |
| 345 | [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" }, |
| 346 | [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" }, |
Cyril Bonté | 77010d8 | 2014-08-07 01:55:37 +0200 | [diff] [blame] | 347 | [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 348 | }; |
| 349 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 350 | static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 351 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 352 | |
| 353 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 354 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 355 | |
| 356 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 357 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 358 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 359 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 360 | |
| 361 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 362 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 363 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 364 | }; |
| 365 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 366 | /* checks if <err> is a real error for errno or one that can be ignored, and |
| 367 | * return 0 for these ones or <err> for real ones. |
| 368 | */ |
| 369 | static inline int unclean_errno(int err) |
| 370 | { |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 371 | if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS || |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 372 | err == EISCONN || err == EALREADY) |
| 373 | return 0; |
| 374 | return err; |
| 375 | } |
| 376 | |
Christopher Faulet | 7aa3271 | 2021-02-01 13:11:50 +0100 | [diff] [blame] | 377 | /* Converts check_status code to result code */ |
| 378 | short get_check_status_result(short check_status) |
| 379 | { |
| 380 | if (check_status < HCHK_STATUS_SIZE) |
| 381 | return check_statuses[check_status].result; |
| 382 | else |
| 383 | return check_statuses[HCHK_STATUS_UNKNOWN].result; |
| 384 | } |
| 385 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 386 | /* Converts check_status code to description */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 387 | const char *get_check_status_description(short check_status) { |
| 388 | |
| 389 | const char *desc; |
| 390 | |
| 391 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 392 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 393 | else |
| 394 | desc = NULL; |
| 395 | |
| 396 | if (desc && *desc) |
| 397 | return desc; |
| 398 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 399 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 402 | /* Converts check_status code to short info */ |
William Dauchy | b26122b | 2021-02-14 22:26:23 +0100 | [diff] [blame] | 403 | const char *get_check_status_info(short check_status) |
| 404 | { |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 405 | const char *info; |
| 406 | |
| 407 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 408 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 409 | else |
| 410 | info = NULL; |
| 411 | |
| 412 | if (info && *info) |
| 413 | return info; |
| 414 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 415 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 418 | /* Convert analyze_status to description */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 419 | const char *get_analyze_status(short analyze_status) { |
| 420 | |
| 421 | const char *desc; |
| 422 | |
| 423 | if (analyze_status < HANA_STATUS_SIZE) |
| 424 | desc = analyze_statuses[analyze_status].desc; |
| 425 | else |
| 426 | desc = NULL; |
| 427 | |
| 428 | if (desc && *desc) |
| 429 | return desc; |
| 430 | else |
| 431 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 432 | } |
| 433 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 434 | /* Sets check->status, update check->duration and fill check->result with an |
| 435 | * adequate CHK_RES_* value. The new check->health is computed based on the |
| 436 | * result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 437 | * |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 438 | * Shows information in logs about failed health check if server is UP or |
| 439 | * succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 440 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 441 | void set_server_check_status(struct check *check, short status, const char *desc) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 442 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 443 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 444 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 445 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 446 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 447 | TRACE_POINT(CHK_EV_HCHK_RUN, check); |
| 448 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 449 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 450 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 451 | check->desc[0] = '\0'; |
| 452 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 456 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 457 | return; |
| 458 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 459 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 460 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 461 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 462 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 463 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 464 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 465 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 466 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 467 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 468 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 469 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 470 | check->duration = -1; |
| 471 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 472 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 473 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 474 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 477 | /* no change is expected if no state change occurred */ |
| 478 | if (check->result == CHK_RES_NEUTRAL) |
| 479 | return; |
| 480 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 481 | /* If the check was really just sending a mail, it won't have an |
| 482 | * associated server, so we're done now. |
| 483 | */ |
| 484 | if (!s) |
| 485 | return; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 486 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 487 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 488 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 489 | switch (check->result) { |
| 490 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 491 | /* Failure to connect to the agent as a secondary check should not |
| 492 | * cause the server to be marked down. |
| 493 | */ |
| 494 | if ((!(check->state & CHK_ST_AGENT) || |
Simon Horman | eaabd52 | 2015-02-26 11:26:17 +0900 | [diff] [blame] | 495 | (check->status >= HCHK_STATUS_L57DATA)) && |
Christopher Faulet | b119a79 | 2018-05-02 12:12:45 +0200 | [diff] [blame] | 496 | (check->health > 0)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 497 | _HA_ATOMIC_INC(&s->counters.failed_checks); |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 498 | report = 1; |
| 499 | check->health--; |
| 500 | if (check->health < check->rise) |
| 501 | check->health = 0; |
| 502 | } |
| 503 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 505 | case CHK_RES_PASSED: |
Christopher Faulet | 1e527cb | 2020-11-20 18:13:02 +0100 | [diff] [blame] | 506 | case CHK_RES_CONDPASS: |
| 507 | if (check->health < check->rise + check->fall - 1) { |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 508 | report = 1; |
| 509 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 510 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 511 | if (check->health >= check->rise) |
| 512 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 513 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 515 | /* clear consecutive_errors if observing is enabled */ |
| 516 | if (s->onerror) |
| 517 | s->consecutive_errors = 0; |
| 518 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 519 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 520 | default: |
| 521 | break; |
| 522 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 523 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 524 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 525 | (status != prev_status || report)) { |
| 526 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 527 | "%s check for %sserver %s/%s %s%s", |
| 528 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 529 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 530 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 531 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 532 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 533 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 534 | srv_append_status(&trash, s, check, -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 535 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 536 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 537 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 538 | (check->health >= check->rise) ? check->fall : check->rise, |
| 539 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 540 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 541 | ha_warning("%s.\n", trash.area); |
| 542 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
| 543 | send_email_alert(s, LOG_INFO, "%s", trash.area); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 544 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 545 | } |
| 546 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 547 | /* Marks the check <check>'s server down if the current check is already failed |
| 548 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 549 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 550 | void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 551 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 552 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 553 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 554 | /* The agent secondary check should only cause a server to be marked |
| 555 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 556 | * that the agent returned "fail", "stopped" or "down". |
| 557 | * The implication here is that failure to connect to the agent |
| 558 | * as a secondary check should not cause the server to be marked |
| 559 | * down. */ |
| 560 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 561 | return; |
| 562 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 563 | if (check->health > 0) |
| 564 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 565 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 566 | TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 567 | /* We only report a reason for the check if we did not do so previously */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 568 | srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 571 | /* Marks the check <check> as valid and tries to set its server up, provided |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 572 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 573 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 574 | * following conditions is true : |
| 575 | * - health check failed (check->health < rise) |
| 576 | * - agent check failed (agent->health < rise) |
| 577 | * - the server tracks a down server (track && track->state == STOPPED) |
| 578 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 579 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 580 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 581 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 582 | void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 583 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 584 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 585 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 586 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 587 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 588 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 589 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 590 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 591 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 592 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 593 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 594 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 595 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 596 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 597 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 598 | if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 599 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 600 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 601 | TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 602 | srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 605 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 606 | * if it was running or starting, and provided it isn't in maintenance and other |
| 607 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 608 | * the same as for it to be turned up. Also, only the health checks support the |
| 609 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 610 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 611 | void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 612 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 613 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 614 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 615 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 616 | return; |
| 617 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 618 | if (check->state & CHK_ST_AGENT) |
| 619 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 620 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 621 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 622 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 623 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 624 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 625 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 626 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 627 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 628 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 629 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 630 | TRACE_STATE("health-check condionnaly succeeded, set server STOPPING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
Willy Tarreau | b26881a | 2017-12-23 11:16:49 +0100 | [diff] [blame] | 631 | srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 632 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 633 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 634 | /* note: use health_adjust() only, which first checks that the observe mode is |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 635 | * enabled. This will take the server lock if needed. |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 636 | */ |
| 637 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 638 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 639 | int failed; |
| 640 | int expire; |
| 641 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 642 | if (s->observe >= HANA_OBS_SIZE) |
| 643 | return; |
| 644 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 645 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 646 | return; |
| 647 | |
| 648 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 649 | case 1: |
| 650 | failed = 1; |
| 651 | break; |
| 652 | |
| 653 | case 2: |
| 654 | failed = 0; |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | if (!failed) { |
| 662 | /* good: clear consecutive_errors */ |
| 663 | s->consecutive_errors = 0; |
| 664 | return; |
| 665 | } |
| 666 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 667 | _HA_ATOMIC_INC(&s->consecutive_errors); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 668 | |
| 669 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 670 | return; |
| 671 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 672 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 673 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 674 | |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 675 | if (s->check.fastinter) |
| 676 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
| 677 | else |
| 678 | expire = TICK_ETERNITY; |
| 679 | |
| 680 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 681 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 682 | switch (s->onerror) { |
| 683 | case HANA_ONERR_FASTINTER: |
| 684 | /* force fastinter - nothing to do here as all modes force it */ |
| 685 | break; |
| 686 | |
| 687 | case HANA_ONERR_SUDDTH: |
| 688 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 689 | if (s->check.health > s->check.rise) |
| 690 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 691 | |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 692 | /* fall through */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 693 | |
| 694 | case HANA_ONERR_FAILCHK: |
| 695 | /* simulate a failed health check */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 696 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 697 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 698 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 699 | break; |
| 700 | |
| 701 | case HANA_ONERR_MARKDWN: |
| 702 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 703 | s->check.health = s->check.rise; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 704 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 705 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 706 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 707 | break; |
| 708 | |
| 709 | default: |
| 710 | /* write a warning? */ |
| 711 | break; |
| 712 | } |
| 713 | |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 714 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 715 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 716 | s->consecutive_errors = 0; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 717 | _HA_ATOMIC_INC(&s->counters.failed_hana); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 718 | |
Christopher Faulet | ea86083 | 2021-05-07 11:45:26 +0200 | [diff] [blame] | 719 | if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) { |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 720 | /* requeue check task with new expire */ |
| 721 | task_schedule(s->check.task, expire); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 722 | } |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 725 | /* Checks the connection. If an error has already been reported or the socket is |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 726 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 727 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 728 | * Warning, this must be done only once when returning from poll, and never |
| 729 | * after an I/O error was attempted, otherwise the error queue might contain |
| 730 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 731 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 732 | * clean (including a properly closed socket). |
| 733 | */ |
| 734 | static int retrieve_errno_from_socket(struct connection *conn) |
| 735 | { |
| 736 | int skerr; |
| 737 | socklen_t lskerr = sizeof(skerr); |
| 738 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 739 | if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 740 | return 1; |
| 741 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 742 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 743 | return 0; |
| 744 | |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 745 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 746 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 747 | if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 748 | errno = skerr; |
| 749 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 750 | errno = unclean_errno(errno); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 751 | |
| 752 | if (!errno) { |
| 753 | /* we could not retrieve an error, that does not mean there is |
| 754 | * none. Just don't change anything and only report the prior |
| 755 | * error if any. |
| 756 | */ |
| 757 | if (conn->flags & CO_FL_ERROR) |
| 758 | return 1; |
| 759 | else |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 764 | return 1; |
| 765 | } |
| 766 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 767 | /* Tries to collect as much information as possible on the connection status, |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 768 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 769 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 770 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 771 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 772 | * to extract errno from the socket. If no error is reported, it will consider |
| 773 | * the <expired> flag. This is intended to be used when a connection error was |
| 774 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 775 | * function takes care of not updating a server status which was already set. |
| 776 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 777 | * produce a status. |
| 778 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 779 | void chk_report_conn_err(struct check *check, int errno_bck, int expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 780 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 781 | struct conn_stream *cs = check->cs; |
| 782 | struct connection *conn = cs_conn(cs); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 783 | const char *err_msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 784 | struct buffer *chk; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 785 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 786 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 787 | if (check->result != CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 788 | return; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 789 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 790 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 791 | errno = unclean_errno(errno_bck); |
| 792 | if (conn && errno) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 793 | retrieve_errno_from_socket(conn); |
| 794 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 795 | if (conn && !(conn->flags & CO_FL_ERROR) && |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 796 | cs && !(cs->endp->flags & CS_EP_ERROR) && !expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 797 | return; |
| 798 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 799 | TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired}); |
| 800 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 801 | /* we'll try to build a meaningful error message depending on the |
| 802 | * context of the error possibly present in conn->err_code, and the |
| 803 | * socket error possibly collected above. This is useful to know the |
| 804 | * exact step of the L6 layer (eg: SSL handshake). |
| 805 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 806 | chk = get_trash_chunk(); |
| 807 | |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 808 | if (check->type == PR_O2_TCPCHK_CHK && |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 809 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 810 | step = tcpcheck_get_step_id(check, NULL); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 811 | if (!step) { |
| 812 | TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 813 | chunk_printf(chk, " at initial connection step of tcp-check"); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 814 | } |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 815 | else { |
| 816 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 817 | /* we were looking for a string */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 818 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 819 | if (check->current_step->connect.port) |
| 820 | chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 821 | else |
| 822 | chunk_appendf(chk, " (connect)"); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 823 | TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 824 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 825 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) { |
| 826 | struct tcpcheck_expect *expect = &check->current_step->expect; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 827 | |
| 828 | switch (expect->type) { |
| 829 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 830 | chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 831 | break; |
| 832 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 833 | chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 834 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 835 | case TCPCHK_EXPECT_STRING_REGEX: |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 836 | chunk_appendf(chk, " (expect regex)"); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 837 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 838 | case TCPCHK_EXPECT_BINARY_REGEX: |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 839 | chunk_appendf(chk, " (expect binary regex)"); |
| 840 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 841 | case TCPCHK_EXPECT_STRING_LF: |
| 842 | chunk_appendf(chk, " (expect log-format string)"); |
| 843 | break; |
| 844 | case TCPCHK_EXPECT_BINARY_LF: |
| 845 | chunk_appendf(chk, " (expect log-format binary)"); |
| 846 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 847 | case TCPCHK_EXPECT_HTTP_STATUS: |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 848 | chunk_appendf(chk, " (expect HTTP status codes)"); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 849 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 850 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 851 | chunk_appendf(chk, " (expect HTTP status regex)"); |
| 852 | break; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 853 | case TCPCHK_EXPECT_HTTP_HEADER: |
| 854 | chunk_appendf(chk, " (expect HTTP header pattern)"); |
| 855 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 856 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 857 | chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 858 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 859 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 860 | chunk_appendf(chk, " (expect HTTP body regex)"); |
| 861 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 862 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 863 | chunk_appendf(chk, " (expect log-format HTTP body)"); |
| 864 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 865 | case TCPCHK_EXPECT_CUSTOM: |
| 866 | chunk_appendf(chk, " (expect custom function)"); |
| 867 | break; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 868 | case TCPCHK_EXPECT_UNDEF: |
| 869 | chunk_appendf(chk, " (undefined expect!)"); |
| 870 | break; |
| 871 | } |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 872 | TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 873 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 874 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 875 | chunk_appendf(chk, " (send)"); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 876 | TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 877 | } |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 878 | |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 879 | if (check->current_step && check->current_step->comment) |
| 880 | chunk_appendf(chk, " comment: '%s'", check->current_step->comment); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 884 | if (conn && conn->err_code) { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 885 | if (unclean_errno(errno)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 886 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), |
| 887 | chk->area); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 888 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 889 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), |
| 890 | chk->area); |
| 891 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 892 | } |
| 893 | else { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 894 | if (unclean_errno(errno)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 895 | chunk_printf(&trash, "%s%s", strerror(errno), |
| 896 | chk->area); |
| 897 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 898 | } |
| 899 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 900 | err_msg = chk->area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 904 | if (check->state & CHK_ST_PORT_MISS) { |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 905 | /* NOTE: this is reported after <fall> tries */ |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 906 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 907 | } |
| 908 | |
Christopher Faulet | 0256da1 | 2021-12-15 09:50:17 +0100 | [diff] [blame] | 909 | if (!cs || !conn || !conn->ctrl) { |
Christopher Faulet | 5e29376 | 2020-10-26 11:10:49 +0100 | [diff] [blame] | 910 | /* error before any connection attempt (connection allocation error or no control layer) */ |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 911 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 912 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 913 | else if (conn->flags & CO_FL_WAIT_L4_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 914 | /* L4 not established (yet) */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 915 | if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 916 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 917 | else if (expired) |
| 918 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 919 | |
| 920 | /* |
| 921 | * might be due to a server IP change. |
| 922 | * Let's trigger a DNS resolution if none are currently running. |
| 923 | */ |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 924 | if (check->server) |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 925 | resolv_trigger_resolution(check->server->resolv_requester); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 926 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 927 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 928 | else if (conn->flags & CO_FL_WAIT_L6_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 929 | /* L6 not established (yet) */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 930 | if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 931 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 932 | else if (expired) |
| 933 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 934 | } |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 935 | else if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 936 | /* I/O error after connection was established and before we could diagnose */ |
| 937 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 938 | } |
| 939 | else if (expired) { |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 940 | enum healthcheck_status tout = HCHK_STATUS_L7TOUT; |
| 941 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 942 | /* connection established but expired check */ |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 943 | if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT && |
| 944 | check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN) |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 945 | tout = check->current_step->expect.tout_status; |
| 946 | set_server_check_status(check, tout, err_msg); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 949 | TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 950 | return; |
| 951 | } |
| 952 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 953 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 954 | /* Builds the server state header used by HTTP health-checks */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 955 | int httpchk_build_status_header(struct server *s, struct buffer *buf) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 956 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 957 | int sv_state; |
| 958 | int ratio; |
| 959 | char addr[46]; |
| 960 | char port[6]; |
| 961 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 962 | "UP %d/%d", "UP", |
| 963 | "NOLB %d/%d", "NOLB", |
| 964 | "no check" }; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 965 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 966 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 967 | sv_state = 6; |
| 968 | else if (s->cur_state != SRV_ST_STOPPED) { |
| 969 | if (s->check.health == s->check.rise + s->check.fall - 1) |
| 970 | sv_state = 3; /* UP */ |
| 971 | else |
| 972 | sv_state = 2; /* going down */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 973 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 974 | if (s->cur_state == SRV_ST_STOPPING) |
| 975 | sv_state += 2; |
| 976 | } else { |
| 977 | if (s->check.health) |
| 978 | sv_state = 1; /* going up */ |
| 979 | else |
| 980 | sv_state = 0; /* DOWN */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 981 | } |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 982 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 983 | chunk_appendf(buf, srv_hlt_st[sv_state], |
| 984 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 985 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 986 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 987 | addr_to_str(&s->addr, addr, sizeof(addr)); |
| 988 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 989 | snprintf(port, sizeof(port), "%u", s->svc_port); |
| 990 | else |
| 991 | *port = 0; |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 992 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 993 | chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 994 | addr, port, s->proxy->id, s->id, |
| 995 | global.node, |
| 996 | (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 997 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
Willy Tarreau | 7f3c1df | 2021-06-18 09:22:21 +0200 | [diff] [blame] | 998 | s->cur_sess, s->proxy->beconn - s->proxy->queue.length, |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 999 | s->queue.length); |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 1000 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1001 | if ((s->cur_state == SRV_ST_STARTING) && |
| 1002 | now.tv_sec < s->last_change + s->slowstart && |
| 1003 | now.tv_sec >= s->last_change) { |
| 1004 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
| 1005 | chunk_appendf(buf, "; throttle=%d%%", ratio); |
| 1006 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1007 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1008 | return b_data(buf); |
| 1009 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1010 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1011 | /**************************************************************************/ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1012 | /***************** Health-checks based on connections *********************/ |
| 1013 | /**************************************************************************/ |
| 1014 | /* This function is used only for server health-checks. It handles connection |
| 1015 | * status updates including errors. If necessary, it wakes the check task up. |
| 1016 | * It returns 0 on normal cases, <0 if at least one close() has happened on the |
| 1017 | * connection (eg: reconnect). It relies on tcpcheck_main(). |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1018 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1019 | static int wake_srv_chk(struct conn_stream *cs) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1020 | { |
Christopher Faulet | 0256da1 | 2021-12-15 09:50:17 +0100 | [diff] [blame] | 1021 | struct connection *conn; |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1022 | struct check *check = __cs_check(cs); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1023 | struct email_alertq *q = container_of(check, typeof(*q), check); |
| 1024 | int ret = 0; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1025 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1026 | TRACE_ENTER(CHK_EV_HCHK_WAKE, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1027 | if (check->server) |
| 1028 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 1029 | else |
| 1030 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1031 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1032 | /* we may have to make progress on the TCP checks */ |
| 1033 | ret = tcpcheck_main(check); |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 1034 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1035 | cs = check->cs; |
Christopher Faulet | 0256da1 | 2021-12-15 09:50:17 +0100 | [diff] [blame] | 1036 | conn = cs_conn(cs); |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 1037 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1038 | if (unlikely(!conn || !cs || conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR)) { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1039 | /* We may get error reports bypassing the I/O handlers, typically |
| 1040 | * the case when sending a pure TCP check which fails, then the I/O |
| 1041 | * handlers above are not called. This is completely handled by the |
| 1042 | * main processing task so let's simply wake it up. If we get here, |
| 1043 | * we expect errno to still be valid. |
| 1044 | */ |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1045 | TRACE_ERROR("report connection error", CHK_EV_HCHK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1046 | chk_report_conn_err(check, errno, 0); |
| 1047 | task_wakeup(check->task, TASK_WOKEN_IO); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Christopher Faulet | 8f10042 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 1050 | if (check->result != CHK_RES_UNKNOWN || ret == -1) { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1051 | /* Check complete or aborted. If connection not yet closed do it |
| 1052 | * now and wake the check task up to be sure the result is |
| 1053 | * handled ASAP. */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1054 | ret = -1; |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1055 | if (conn) { |
Christopher Faulet | ff022a2 | 2022-04-21 08:38:54 +0200 | [diff] [blame] | 1056 | cs_conn_drain_and_shut(cs); |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1057 | if (check->wait_list.events) |
| 1058 | conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list); |
| 1059 | } |
Christopher Faulet | 8f10042 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 1060 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1061 | /* We may have been scheduled to run, and the |
| 1062 | * I/O handler expects to have a cs, so remove |
| 1063 | * the tasklet |
| 1064 | */ |
| 1065 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 1066 | task_wakeup(check->task, TASK_WOKEN_IO); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1067 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1068 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1069 | if (check->server) |
| 1070 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 1071 | else |
| 1072 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1073 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1074 | TRACE_LEAVE(CHK_EV_HCHK_WAKE, check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1075 | return ret; |
| 1076 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1077 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1078 | /* This function checks if any I/O is wanted, and if so, attempts to do so */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1079 | struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned int state) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1080 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1081 | struct check *check = ctx; |
| 1082 | struct conn_stream *cs = check->cs; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1083 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1084 | wake_srv_chk(cs); |
| 1085 | return NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1086 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1087 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1088 | /* manages a server health-check that uses a connection. Returns |
| 1089 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1090 | * |
| 1091 | * Please do NOT place any return statement in this function and only leave |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1092 | * via the out_unlock label. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1093 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1094 | struct task *process_chk_conn(struct task *t, void *context, unsigned int state) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1095 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1096 | struct check *check = context; |
| 1097 | struct proxy *proxy = check->proxy; |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1098 | struct conn_stream *cs; |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1099 | struct connection *conn; |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1100 | int rv; |
| 1101 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 1102 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1103 | TRACE_ENTER(CHK_EV_TASK_WAKE, check); |
| 1104 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1105 | if (check->server) |
| 1106 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1107 | |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1108 | if (unlikely(check->state & CHK_ST_PURGE)) { |
| 1109 | TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check); |
| 1110 | } |
| 1111 | else if (!(check->state & (CHK_ST_INPROGRESS))) { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1112 | /* no check currently running */ |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1113 | if (!expired) /* woke up too early */ { |
| 1114 | TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1115 | goto out_unlock; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1116 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1117 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1118 | /* we don't send any health-checks when the proxy is |
| 1119 | * stopped, the server should not be checked or the check |
| 1120 | * is disabled. |
| 1121 | */ |
| 1122 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 1123 | (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) { |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1124 | TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1125 | goto reschedule; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1126 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1127 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1128 | /* we'll initiate a new check */ |
| 1129 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1130 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1131 | check->state |= CHK_ST_INPROGRESS; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1132 | TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1133 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1134 | task_set_affinity(t, tid_bit); |
| 1135 | |
| 1136 | check->current_step = NULL; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1137 | |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1138 | check->cs = cs_new_from_check(check, CS_FL_NONE); |
| 1139 | if (!check->cs) { |
| 1140 | set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL); |
| 1141 | goto end; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1142 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1143 | tcpcheck_main(check); |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1144 | expired = 0; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1145 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1146 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1147 | /* there was a test running. |
| 1148 | * First, let's check whether there was an uncaught error, |
| 1149 | * which can happen on connect timeout or error. |
| 1150 | */ |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1151 | if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) { |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1152 | cs = check->cs; |
| 1153 | conn = (cs ? cs_conn(cs) : NULL); |
| 1154 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1155 | /* Here the connection must be defined. Otherwise the |
| 1156 | * error would have already been detected |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1157 | */ |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1158 | if ((conn && ((conn->flags & CO_FL_ERROR) || (cs->endp->flags & CS_EP_ERROR))) || expired) { |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1159 | TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
| 1160 | chk_report_conn_err(check, 0, expired); |
| 1161 | } |
| 1162 | else { |
| 1163 | if (check->state & CHK_ST_CLOSE_CONN) { |
| 1164 | TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check); |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1165 | check->state &= ~CHK_ST_CLOSE_CONN; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1166 | conn = NULL; |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1167 | cs_reset_endp(check->cs); /* error will be handled by tcpcheck_main() */ |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1168 | tcpcheck_main(check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1169 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1170 | if (check->result == CHK_RES_UNKNOWN) { |
| 1171 | TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check); |
| 1172 | goto out_unlock; /* timeout not reached, wait again */ |
Christopher Faulet | 8f10042 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 1173 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1174 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1175 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1176 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1177 | /* check complete or aborted */ |
| 1178 | TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check); |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1179 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1180 | check->current_step = NULL; |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1181 | cs = check->cs; |
| 1182 | conn = (cs ? cs_conn(cs) : NULL); |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1183 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1184 | if (conn && conn->xprt) { |
| 1185 | /* The check was aborted and the connection was not yet closed. |
| 1186 | * This can happen upon timeout, or when an external event such |
| 1187 | * as a failed response coupled with "observe layer7" caused the |
| 1188 | * server state to be suddenly changed. |
| 1189 | */ |
Christopher Faulet | ff022a2 | 2022-04-21 08:38:54 +0200 | [diff] [blame] | 1190 | cs_conn_drain_and_shut(cs); |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1191 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1192 | |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1193 | if (cs) { |
| 1194 | if (conn && check->wait_list.events) |
| 1195 | conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list); |
| 1196 | /* We may have been scheduled to run, and the |
| 1197 | * I/O handler expects to have a cs, so remove |
| 1198 | * the tasklet |
| 1199 | */ |
| 1200 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 1201 | cs_destroy(cs); |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1202 | cs = check->cs = NULL; |
| 1203 | conn = NULL; |
| 1204 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1205 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1206 | if (check->sess != NULL) { |
| 1207 | vars_prune(&check->vars, check->sess, NULL); |
| 1208 | session_free(check->sess); |
| 1209 | check->sess = NULL; |
| 1210 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1211 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1212 | end: |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1213 | if (check->server && likely(!(check->state & CHK_ST_PURGE))) { |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1214 | if (check->result == CHK_RES_FAILED) { |
| 1215 | /* a failure or timeout detected */ |
| 1216 | TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
| 1217 | check_notify_failure(check); |
| 1218 | } |
| 1219 | else if (check->result == CHK_RES_CONDPASS) { |
| 1220 | /* check is OK but asks for stopping mode */ |
| 1221 | TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
| 1222 | check_notify_stopping(check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1223 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1224 | else if (check->result == CHK_RES_PASSED) { |
| 1225 | /* a success was detected */ |
| 1226 | TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
| 1227 | check_notify_success(check); |
| 1228 | } |
| 1229 | } |
| 1230 | task_set_affinity(t, MAX_THREADS_MASK); |
| 1231 | check_release_buf(check, &check->bi); |
| 1232 | check_release_buf(check, &check->bo); |
| 1233 | check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC); |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1234 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1235 | if (check->server) { |
| 1236 | rv = 0; |
| 1237 | if (global.spread_checks > 0) { |
| 1238 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 1239 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1240 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1241 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1242 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1243 | |
| 1244 | reschedule: |
| 1245 | while (tick_is_expired(t->expire, now_ms)) |
| 1246 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1247 | out_unlock: |
| 1248 | if (check->server) |
| 1249 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1250 | |
| 1251 | TRACE_LEAVE(CHK_EV_TASK_WAKE, check); |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1252 | |
| 1253 | /* Free the check if set to PURGE. After this, the check instance may be |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 1254 | * freed via the srv_drop invocation, so it must not be accessed after |
| 1255 | * this point. |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1256 | */ |
| 1257 | if (unlikely(check->state & CHK_ST_PURGE)) { |
Amaury Denoyelle | 26cb834 | 2021-08-10 16:23:49 +0200 | [diff] [blame] | 1258 | free_check(check); |
Amaury Denoyelle | 9ba34ae | 2021-08-09 15:09:17 +0200 | [diff] [blame] | 1259 | if (check->server) |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 1260 | srv_drop(check->server); |
Amaury Denoyelle | 26cb834 | 2021-08-10 16:23:49 +0200 | [diff] [blame] | 1261 | |
| 1262 | t = NULL; |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1265 | return t; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1268 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1269 | /**************************************************************************/ |
| 1270 | /************************** Init/deinit checks ****************************/ |
| 1271 | /**************************************************************************/ |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1272 | /* |
| 1273 | * Tries to grab a buffer and to re-enables processing on check <target>. The |
| 1274 | * check flags are used to figure what buffer was requested. It returns 1 if the |
| 1275 | * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's |
| 1276 | * impossible to wake up and we prefer to be woken up later. |
| 1277 | */ |
| 1278 | int check_buf_available(void *target) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1279 | { |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1280 | struct check *check = target; |
| 1281 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 1282 | if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) { |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1283 | TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1284 | check->state &= ~CHK_ST_IN_ALLOC; |
| 1285 | tasklet_wakeup(check->wait_list.tasklet); |
| 1286 | return 1; |
| 1287 | } |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 1288 | if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) { |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1289 | TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1290 | check->state &= ~CHK_ST_OUT_ALLOC; |
| 1291 | tasklet_wakeup(check->wait_list.tasklet); |
| 1292 | return 1; |
| 1293 | } |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1297 | |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1298 | /* |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 1299 | * Allocate a buffer. If it fails, it adds the check in buffer wait queue. |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1300 | */ |
| 1301 | struct buffer *check_get_buf(struct check *check, struct buffer *bptr) |
| 1302 | { |
| 1303 | struct buffer *buf = NULL; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1304 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1305 | if (likely(!LIST_INLIST(&check->buf_wait.list)) && |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 1306 | unlikely((buf = b_alloc(bptr)) == NULL)) { |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1307 | check->buf_wait.target = check; |
| 1308 | check->buf_wait.wakeup_cb = check_buf_available; |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 1309 | LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1310 | } |
| 1311 | return buf; |
| 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 1316 | * wait queue. |
| 1317 | */ |
| 1318 | void check_release_buf(struct check *check, struct buffer *bptr) |
| 1319 | { |
| 1320 | if (bptr->size) { |
| 1321 | b_free(bptr); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 1322 | offer_buffers(check->buf_wait.target, 1); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | const char *init_check(struct check *check, int type) |
| 1327 | { |
| 1328 | check->type = type; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1329 | |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1330 | check->bi = BUF_NULL; |
| 1331 | check->bo = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 1332 | LIST_INIT(&check->buf_wait.list); |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1333 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1334 | check->wait_list.tasklet = tasklet_new(); |
| 1335 | if (!check->wait_list.tasklet) |
| 1336 | return "out of memory while allocating check tasklet"; |
| 1337 | check->wait_list.events = 0; |
| 1338 | check->wait_list.tasklet->process = event_srv_chk_io; |
| 1339 | check->wait_list.tasklet->context = check; |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1343 | /* Liberates the resources allocated for a check. |
| 1344 | * |
Amaury Denoyelle | 6d7fc44 | 2021-08-10 16:22:51 +0200 | [diff] [blame] | 1345 | * This function must only be run by the thread owning the check. |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1346 | */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1347 | void free_check(struct check *check) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1348 | { |
Amaury Denoyelle | 6d7fc44 | 2021-08-10 16:22:51 +0200 | [diff] [blame] | 1349 | /* For agent-check, free the rules / vars from the server. This is not |
| 1350 | * done for health-check : the proxy is the owner of the rules / vars |
| 1351 | * in this case. |
| 1352 | */ |
| 1353 | if (check->state & CHK_ST_AGENT) { |
| 1354 | free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars); |
| 1355 | ha_free(&check->tcpcheck_rules); |
| 1356 | } |
| 1357 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1358 | task_destroy(check->task); |
| 1359 | if (check->wait_list.tasklet) |
| 1360 | tasklet_free(check->wait_list.tasklet); |
| 1361 | |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1362 | check_release_buf(check, &check->bi); |
| 1363 | check_release_buf(check, &check->bo); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1364 | if (check->cs) { |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 1365 | cs_destroy(check->cs); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1366 | check->cs = NULL; |
| 1367 | } |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1370 | /* This function must be used in order to free a started check. The check will |
| 1371 | * be scheduled for a next execution in order to properly close and free all |
| 1372 | * check elements. |
| 1373 | * |
| 1374 | * Non thread-safe. |
| 1375 | */ |
| 1376 | void check_purge(struct check *check) |
| 1377 | { |
Amaury Denoyelle | 25fe103 | 2021-08-10 16:21:55 +0200 | [diff] [blame] | 1378 | check->state |= CHK_ST_PURGE; |
Amaury Denoyelle | b33a0ab | 2021-07-29 15:51:45 +0200 | [diff] [blame] | 1379 | task_wakeup(check->task, TASK_WOKEN_OTHER); |
| 1380 | } |
| 1381 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1382 | /* manages a server health-check. Returns the time the task accepts to wait, or |
| 1383 | * TIME_ETERNITY for infinity. |
| 1384 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1385 | struct task *process_chk(struct task *t, void *context, unsigned int state) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1386 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1387 | struct check *check = context; |
| 1388 | |
| 1389 | if (check->type == PR_O2_EXT_CHK) |
| 1390 | return process_chk_proc(t, context, state); |
| 1391 | return process_chk_conn(t, context, state); |
| 1392 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1393 | } |
| 1394 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1395 | |
Amaury Denoyelle | 3c2ab1a | 2021-07-22 16:04:40 +0200 | [diff] [blame] | 1396 | int start_check_task(struct check *check, int mininter, |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1397 | int nbcheck, int srvpos) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1398 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1399 | struct task *t; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1400 | |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 1401 | /* task for the check. Process-based checks exclusively run on thread 1. */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1402 | if (check->type == PR_O2_EXT_CHK) |
Willy Tarreau | a89c191 | 2021-10-20 18:43:30 +0200 | [diff] [blame] | 1403 | t = task_new_on(0); |
Christopher Faulet | 177a0e6 | 2022-04-12 17:47:07 +0200 | [diff] [blame] | 1404 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 1405 | t = task_new_anywhere(); |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1406 | |
Christopher Faulet | 54e85cb | 2022-01-06 08:46:56 +0100 | [diff] [blame] | 1407 | if (!t) |
| 1408 | goto fail_alloc_task; |
| 1409 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1410 | check->task = t; |
| 1411 | t->process = process_chk; |
| 1412 | t->context = check; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1413 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1414 | if (mininter < srv_getinter(check)) |
| 1415 | mininter = srv_getinter(check); |
| 1416 | |
| 1417 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 1418 | mininter = global.max_spread_checks; |
| 1419 | |
| 1420 | /* check this every ms */ |
| 1421 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
| 1422 | check->start = now; |
| 1423 | task_queue(t); |
| 1424 | |
| 1425 | return 1; |
Christopher Faulet | 54e85cb | 2022-01-06 08:46:56 +0100 | [diff] [blame] | 1426 | |
| 1427 | fail_alloc_task: |
Christopher Faulet | 54e85cb | 2022-01-06 08:46:56 +0100 | [diff] [blame] | 1428 | ha_alert("Starting [%s:%s] check: out of memory.\n", |
| 1429 | check->server->proxy->id, check->server->id); |
| 1430 | return 0; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1433 | /* |
| 1434 | * Start health-check. |
| 1435 | * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case. |
| 1436 | */ |
| 1437 | static int start_checks() |
| 1438 | { |
| 1439 | |
| 1440 | struct proxy *px; |
| 1441 | struct server *s; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1442 | int nbcheck=0, mininter=0, srvpos=0; |
| 1443 | |
| 1444 | /* 0- init the dummy frontend used to create all checks sessions */ |
| 1445 | init_new_proxy(&checks_fe); |
Christopher Faulet | 0f1fc23 | 2021-04-16 10:49:07 +0200 | [diff] [blame] | 1446 | checks_fe.id = strdup("CHECKS-FE"); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1447 | checks_fe.cap = PR_CAP_FE | PR_CAP_BE; |
| 1448 | checks_fe.mode = PR_MODE_TCP; |
| 1449 | checks_fe.maxconn = 0; |
| 1450 | checks_fe.conn_retries = CONN_RETRIES; |
| 1451 | checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
| 1452 | checks_fe.timeout.client = TICK_ETERNITY; |
| 1453 | |
| 1454 | /* 1- count the checkers to run simultaneously. |
| 1455 | * We also determine the minimum interval among all of those which |
| 1456 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1457 | * will be used to spread their start-up date. Those which have |
| 1458 | * a shorter interval will start independently and will not dictate |
| 1459 | * too short an interval for all others. |
| 1460 | */ |
| 1461 | for (px = proxies_list; px; px = px->next) { |
| 1462 | for (s = px->srv; s; s = s->next) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1463 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1464 | nbcheck++; |
| 1465 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 1466 | (!mininter || mininter > srv_getinter(&s->check))) |
| 1467 | mininter = srv_getinter(&s->check); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1470 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1471 | nbcheck++; |
| 1472 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 1473 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 1474 | mininter = srv_getinter(&s->agent); |
| 1475 | } |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1476 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1477 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1478 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1479 | if (!nbcheck) |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1480 | return ERR_NONE; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1481 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1482 | srand((unsigned)time(NULL)); |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1483 | |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 1484 | /* 2- start them as far as possible from each other. For this, we will |
| 1485 | * start them after their interval is set to the min interval divided |
| 1486 | * by the number of servers, weighted by the server's position in the |
| 1487 | * list. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1488 | */ |
| 1489 | for (px = proxies_list; px; px = px->next) { |
| 1490 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 1491 | if (init_pid_list()) { |
| 1492 | ha_alert("Starting [%s] check: out of memory.\n", px->id); |
| 1493 | return ERR_ALERT | ERR_FATAL; |
| 1494 | } |
| 1495 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1496 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1497 | for (s = px->srv; s; s = s->next) { |
| 1498 | /* A task for the main check */ |
| 1499 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1500 | if (s->check.type == PR_O2_EXT_CHK) { |
| 1501 | if (!prepare_external_check(&s->check)) |
| 1502 | return ERR_ALERT | ERR_FATAL; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1503 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1504 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 1505 | return ERR_ALERT | ERR_FATAL; |
| 1506 | srvpos++; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 1507 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1508 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1509 | /* A task for a auxiliary agent check */ |
| 1510 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1511 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 1512 | return ERR_ALERT | ERR_FATAL; |
| 1513 | } |
| 1514 | srvpos++; |
| 1515 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1516 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1517 | } |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1518 | return ERR_NONE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1519 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1520 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1521 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1522 | /* |
| 1523 | * Return value: |
| 1524 | * the port to be used for the health check |
| 1525 | * 0 in case no port could be found for the check |
| 1526 | */ |
| 1527 | static int srv_check_healthcheck_port(struct check *chk) |
| 1528 | { |
| 1529 | int i = 0; |
| 1530 | struct server *srv = NULL; |
| 1531 | |
| 1532 | srv = chk->server; |
| 1533 | |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 1534 | /* by default, we use the health check port configured */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1535 | if (chk->port > 0) |
| 1536 | return chk->port; |
| 1537 | |
| 1538 | /* try to get the port from check_core.addr if check.port not set */ |
| 1539 | i = get_host_port(&chk->addr); |
| 1540 | if (i > 0) |
| 1541 | return i; |
| 1542 | |
| 1543 | /* try to get the port from server address */ |
| 1544 | /* prevent MAPPORTS from working at this point, since checks could |
| 1545 | * not be performed in such case (MAPPORTS impose a relative ports |
| 1546 | * based on live traffic) |
| 1547 | */ |
| 1548 | if (srv->flags & SRV_F_MAPPORTS) |
| 1549 | return 0; |
| 1550 | |
| 1551 | i = srv->svc_port; /* by default */ |
| 1552 | if (i > 0) |
| 1553 | return i; |
| 1554 | |
| 1555 | return 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1556 | } |
| 1557 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1558 | /* Initializes an health-check attached to the server <srv>. Non-zero is returned |
| 1559 | * if an error occurred. |
| 1560 | */ |
Amaury Denoyelle | 3c2ab1a | 2021-07-22 16:04:40 +0200 | [diff] [blame] | 1561 | int init_srv_check(struct server *srv) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1562 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1563 | const char *err; |
| 1564 | struct tcpcheck_rule *r; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1565 | int ret = ERR_NONE; |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1566 | int check_type; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1567 | |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1568 | if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1569 | goto out; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1570 | |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1571 | check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1572 | |
Amaury Denoyelle | 7d098be | 2022-03-09 14:20:10 +0100 | [diff] [blame] | 1573 | if (!(srv->flags & SRV_F_DYNAMIC)) { |
| 1574 | /* If neither a port nor an addr was specified and no check |
| 1575 | * transport layer is forced, then the transport layer used by |
| 1576 | * the checks is the same as for the production traffic. |
| 1577 | * Otherwise we use raw_sock by default, unless one is |
| 1578 | * specified. |
| 1579 | */ |
| 1580 | if (!srv->check.port && !is_addr(&srv->check.addr)) { |
| 1581 | if (!srv->check.use_ssl && srv->use_ssl != -1) { |
| 1582 | srv->check.use_ssl = srv->use_ssl; |
| 1583 | srv->check.xprt = srv->xprt; |
| 1584 | } |
| 1585 | else if (srv->check.use_ssl == 1) |
| 1586 | srv->check.xprt = xprt_get(XPRT_SSL); |
| 1587 | srv->check.send_proxy |= (srv->pp_opts); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1588 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1589 | else if (srv->check.use_ssl == 1) |
| 1590 | srv->check.xprt = xprt_get(XPRT_SSL); |
Amaury Denoyelle | 7d098be | 2022-03-09 14:20:10 +0100 | [diff] [blame] | 1591 | } |
| 1592 | else { |
| 1593 | /* For dynamic servers, check-ssl and check-send-proxy must be |
| 1594 | * explicitely defined even if the check port was not |
| 1595 | * overridden. |
| 1596 | */ |
| 1597 | if (srv->check.use_ssl == 1) |
| 1598 | srv->check.xprt = xprt_get(XPRT_SSL); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1599 | } |
| 1600 | |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1601 | /* Inherit the mux protocol from the server if not already defined for |
| 1602 | * the check |
| 1603 | */ |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1604 | if (srv->mux_proto && !srv->check.mux_proto && |
| 1605 | ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) || |
| 1606 | (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) { |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1607 | srv->check.mux_proto = srv->mux_proto; |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1608 | } |
Amaury Denoyelle | 7c14890 | 2020-11-13 12:34:57 +0100 | [diff] [blame] | 1609 | /* test that check proto is valid if explicitly defined */ |
| 1610 | else if (srv->check.mux_proto && |
| 1611 | ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) || |
| 1612 | (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) { |
| 1613 | ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n", |
| 1614 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1615 | ret |= ERR_ALERT | ERR_FATAL; |
| 1616 | goto out; |
| 1617 | } |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1618 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1619 | /* validate <srv> server health-check settings */ |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1620 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1621 | /* We need at least a service port, a check port or the first tcp-check |
| 1622 | * rule must be a 'connect' one when checking an IPv4/IPv6 server. |
| 1623 | */ |
| 1624 | if ((srv_check_healthcheck_port(&srv->check) != 0) || |
| 1625 | (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr)))) |
| 1626 | goto init; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1627 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1628 | if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) { |
| 1629 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n", |
| 1630 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1631 | ret |= ERR_ALERT | ERR_ABORT; |
| 1632 | goto out; |
| 1633 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1634 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1635 | /* search the first action (connect / send / expect) in the list */ |
| 1636 | r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules); |
| 1637 | if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) { |
| 1638 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port " |
| 1639 | "nor tcp_check rule 'connect' with port information.\n", |
| 1640 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1641 | ret |= ERR_ALERT | ERR_ABORT; |
| 1642 | goto out; |
| 1643 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1644 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1645 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 1646 | list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) { |
Willy Tarreau | acff309 | 2021-07-22 11:06:41 +0200 | [diff] [blame] | 1647 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1648 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port, " |
| 1649 | "and a tcp_check rule 'connect' with no port information.\n", |
| 1650 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1651 | ret |= ERR_ALERT | ERR_ABORT; |
| 1652 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1653 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1656 | init: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1657 | err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY); |
| 1658 | if (err) { |
| 1659 | ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n", |
| 1660 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 1661 | ret |= ERR_ALERT | ERR_ABORT; |
| 1662 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1663 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1664 | srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 1665 | srv_take(srv); |
Amaury Denoyelle | 403dce8 | 2021-07-29 15:39:43 +0200 | [diff] [blame] | 1666 | |
| 1667 | /* Only increment maxsock for servers from the configuration. Dynamic |
| 1668 | * servers at the moment are not taken into account for the estimation |
| 1669 | * of the resources limits. |
| 1670 | */ |
| 1671 | if (global.mode & MODE_STARTING) |
| 1672 | global.maxsock++; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1673 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1674 | out: |
| 1675 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1676 | } |
| 1677 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1678 | /* Initializes an agent-check attached to the server <srv>. Non-zero is returned |
| 1679 | * if an error occurred. |
| 1680 | */ |
Amaury Denoyelle | 3c2ab1a | 2021-07-22 16:04:40 +0200 | [diff] [blame] | 1681 | int init_srv_agent_check(struct server *srv) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1682 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1683 | struct tcpcheck_rule *chk; |
| 1684 | const char *err; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1685 | int ret = ERR_NONE; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1686 | |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1687 | if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1688 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1689 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1690 | /* If there is no connect rule preceding all send / expect rules, an |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1691 | * implicit one is inserted before all others. |
| 1692 | */ |
| 1693 | chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules); |
| 1694 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 1695 | chk = calloc(1, sizeof(*chk)); |
| 1696 | if (!chk) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 1697 | ha_alert("%s '%s': unable to add implicit tcp-check connect rule" |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1698 | " to agent-check for server '%s' (out of memory).\n", |
| 1699 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1700 | ret |= ERR_ALERT | ERR_FATAL; |
| 1701 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1702 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1703 | chk->action = TCPCHK_ACT_CONNECT; |
| 1704 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1705 | LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1708 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1709 | err = init_check(&srv->agent, PR_O2_TCPCHK_CHK); |
| 1710 | if (err) { |
| 1711 | ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n", |
| 1712 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 1713 | ret |= ERR_ALERT | ERR_ABORT; |
| 1714 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1717 | if (!srv->agent.inter) |
| 1718 | srv->agent.inter = srv->check.inter; |
| 1719 | |
| 1720 | srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 1721 | srv_take(srv); |
Amaury Denoyelle | 403dce8 | 2021-07-29 15:39:43 +0200 | [diff] [blame] | 1722 | |
| 1723 | /* Only increment maxsock for servers from the configuration. Dynamic |
| 1724 | * servers at the moment are not taken into account for the estimation |
| 1725 | * of the resources limits. |
| 1726 | */ |
| 1727 | if (global.mode & MODE_STARTING) |
| 1728 | global.maxsock++; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1729 | |
| 1730 | out: |
| 1731 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1734 | static void deinit_srv_check(struct server *srv) |
| 1735 | { |
| 1736 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 1737 | free_check(&srv->check); |
| 1738 | srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED; |
| 1739 | srv->do_check = 0; |
| 1740 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1741 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1742 | |
| 1743 | static void deinit_srv_agent_check(struct server *srv) |
| 1744 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1745 | if (srv->agent.state & CHK_ST_CONFIGURED) |
| 1746 | free_check(&srv->agent); |
| 1747 | |
| 1748 | srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT; |
| 1749 | srv->do_agent = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1752 | REGISTER_POST_SERVER_CHECK(init_srv_check); |
| 1753 | REGISTER_POST_SERVER_CHECK(init_srv_agent_check); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1754 | REGISTER_POST_CHECK(start_checks); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1755 | |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1756 | REGISTER_SERVER_DEINIT(deinit_srv_check); |
| 1757 | REGISTER_SERVER_DEINIT(deinit_srv_agent_check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1758 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1759 | |
| 1760 | /**************************************************************************/ |
| 1761 | /************************** Check sample fetches **************************/ |
| 1762 | /**************************************************************************/ |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1763 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1764 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1765 | { /* END */ }, |
| 1766 | }}; |
| 1767 | |
| 1768 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 1769 | |
| 1770 | |
| 1771 | /**************************************************************************/ |
| 1772 | /************************ Check's parsing functions ***********************/ |
| 1773 | /**************************************************************************/ |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1774 | /* Parse the "addr" server keyword */ |
| 1775 | static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1776 | char **errmsg) |
| 1777 | { |
| 1778 | struct sockaddr_storage *sk; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1779 | int port1, port2, err_code = 0; |
| 1780 | |
| 1781 | |
| 1782 | if (!*args[*cur_arg+1]) { |
| 1783 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]); |
| 1784 | goto error; |
| 1785 | } |
| 1786 | |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1787 | sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL, |
| 1788 | PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT); |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1789 | if (!sk) { |
| 1790 | memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg); |
| 1791 | goto error; |
| 1792 | } |
| 1793 | |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1794 | srv->check.addr = *sk; |
| 1795 | /* if agentaddr was never set, we can use addr */ |
| 1796 | if (!(srv->flags & SRV_F_AGENTADDR)) |
| 1797 | srv->agent.addr = *sk; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1798 | |
| 1799 | out: |
| 1800 | return err_code; |
| 1801 | |
| 1802 | error: |
| 1803 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1804 | goto out; |
| 1805 | } |
| 1806 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1807 | /* Parse the "agent-addr" server keyword */ |
| 1808 | static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1809 | char **errmsg) |
| 1810 | { |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1811 | struct sockaddr_storage sk; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1812 | int err_code = 0; |
| 1813 | |
| 1814 | if (!*(args[*cur_arg+1])) { |
| 1815 | memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]); |
| 1816 | goto error; |
| 1817 | } |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1818 | memset(&sk, 0, sizeof(sk)); |
| 1819 | if (str2ip(args[*cur_arg + 1], &sk) == NULL) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1820 | memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]); |
| 1821 | goto error; |
| 1822 | } |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1823 | set_srv_agent_addr(srv, &sk); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1824 | |
| 1825 | out: |
| 1826 | return err_code; |
| 1827 | |
| 1828 | error: |
| 1829 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1830 | goto out; |
| 1831 | } |
| 1832 | |
| 1833 | /* Parse the "agent-check" server keyword */ |
| 1834 | static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1835 | char **errmsg) |
| 1836 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1837 | struct tcpcheck_ruleset *rs = NULL; |
| 1838 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 1839 | struct tcpcheck_rule *chk; |
| 1840 | int err_code = 0; |
| 1841 | |
| 1842 | if (srv->do_agent) |
| 1843 | goto out; |
| 1844 | |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1845 | if (!(curpx->cap & PR_CAP_BE)) { |
| 1846 | memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability", |
| 1847 | args[*cur_arg], proxy_type_str(curpx), curpx->id); |
| 1848 | return ERR_WARN; |
| 1849 | } |
| 1850 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1851 | if (!rules) { |
| 1852 | rules = calloc(1, sizeof(*rules)); |
| 1853 | if (!rules) { |
| 1854 | memprintf(errmsg, "out of memory."); |
| 1855 | goto error; |
| 1856 | } |
| 1857 | LIST_INIT(&rules->preset_vars); |
| 1858 | srv->agent.tcpcheck_rules = rules; |
| 1859 | } |
| 1860 | rules->list = NULL; |
| 1861 | rules->flags = 0; |
| 1862 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1863 | rs = find_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1864 | if (rs) |
| 1865 | goto ruleset_found; |
| 1866 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1867 | rs = create_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1868 | if (rs == NULL) { |
| 1869 | memprintf(errmsg, "out of memory."); |
| 1870 | goto error; |
| 1871 | } |
| 1872 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 1873 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""}, |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1874 | 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg); |
| 1875 | if (!chk) { |
| 1876 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 1877 | goto error; |
| 1878 | } |
| 1879 | chk->index = 0; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1880 | LIST_APPEND(&rs->rules, &chk->list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1881 | |
| 1882 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1883 | 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK, |
| 1884 | srv->conf.file, srv->conf.line, errmsg); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1885 | if (!chk) { |
| 1886 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 1887 | goto error; |
| 1888 | } |
| 1889 | chk->expect.custom = tcpcheck_agent_expect_reply; |
| 1890 | chk->index = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1891 | LIST_APPEND(&rs->rules, &chk->list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1892 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1893 | ruleset_found: |
| 1894 | rules->list = &rs->rules; |
Christopher Faulet | 1faf18a | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 1895 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1896 | rules->flags |= TCPCHK_RULES_AGENT_CHK; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1897 | srv->do_agent = 1; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1898 | |
| 1899 | out: |
Dirkjan Bussink | dfee217 | 2021-06-18 19:57:49 +0000 | [diff] [blame] | 1900 | return err_code; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1901 | |
| 1902 | error: |
| 1903 | deinit_srv_agent_check(srv); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1904 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1905 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1906 | goto out; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | /* Parse the "agent-inter" server keyword */ |
| 1910 | static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1911 | char **errmsg) |
| 1912 | { |
| 1913 | const char *err = NULL; |
| 1914 | unsigned int delay; |
| 1915 | int err_code = 0; |
| 1916 | |
| 1917 | if (!*(args[*cur_arg+1])) { |
| 1918 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 1919 | goto error; |
| 1920 | } |
| 1921 | |
| 1922 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 1923 | if (err == PARSE_TIME_OVER) { |
| 1924 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 1925 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 1926 | goto error; |
| 1927 | } |
| 1928 | else if (err == PARSE_TIME_UNDER) { |
| 1929 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 1930 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 1931 | goto error; |
| 1932 | } |
| 1933 | else if (err) { |
| 1934 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 1935 | *err, srv->id); |
| 1936 | goto error; |
| 1937 | } |
| 1938 | if (delay <= 0) { |
| 1939 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 1940 | delay, args[*cur_arg], srv->id); |
| 1941 | goto error; |
| 1942 | } |
| 1943 | srv->agent.inter = delay; |
| 1944 | |
| 1945 | out: |
| 1946 | return err_code; |
| 1947 | |
| 1948 | error: |
| 1949 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1950 | goto out; |
| 1951 | } |
| 1952 | |
| 1953 | /* Parse the "agent-port" server keyword */ |
| 1954 | static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1955 | char **errmsg) |
| 1956 | { |
| 1957 | int err_code = 0; |
| 1958 | |
| 1959 | if (!*(args[*cur_arg+1])) { |
| 1960 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 1961 | goto error; |
| 1962 | } |
| 1963 | |
Amaury Denoyelle | 403dce8 | 2021-07-29 15:39:43 +0200 | [diff] [blame] | 1964 | /* Only increment maxsock for servers from the configuration. Dynamic |
| 1965 | * servers at the moment are not taken into account for the estimation |
| 1966 | * of the resources limits. |
| 1967 | */ |
| 1968 | if (global.mode & MODE_STARTING) |
| 1969 | global.maxsock++; |
| 1970 | |
William Dauchy | 4858fb2 | 2021-02-03 22:30:09 +0100 | [diff] [blame] | 1971 | set_srv_agent_port(srv, atol(args[*cur_arg + 1])); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1972 | |
| 1973 | out: |
| 1974 | return err_code; |
| 1975 | |
| 1976 | error: |
| 1977 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1978 | goto out; |
| 1979 | } |
| 1980 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1981 | int set_srv_agent_send(struct server *srv, const char *send) |
| 1982 | { |
| 1983 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 1984 | struct tcpcheck_var *var = NULL; |
| 1985 | char *str; |
| 1986 | |
| 1987 | str = strdup(send); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1988 | var = create_tcpcheck_var(ist("check.agent_string")); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1989 | if (str == NULL || var == NULL) |
| 1990 | goto error; |
| 1991 | |
| 1992 | free_tcpcheck_vars(&rules->preset_vars); |
| 1993 | |
| 1994 | var->data.type = SMP_T_STR; |
| 1995 | var->data.u.str.area = str; |
| 1996 | var->data.u.str.data = strlen(str); |
| 1997 | LIST_INIT(&var->list); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1998 | LIST_APPEND(&rules->preset_vars, &var->list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1999 | |
| 2000 | return 1; |
| 2001 | |
| 2002 | error: |
| 2003 | free(str); |
| 2004 | free(var); |
| 2005 | return 0; |
| 2006 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2007 | |
| 2008 | /* Parse the "agent-send" server keyword */ |
| 2009 | static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2010 | char **errmsg) |
| 2011 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2012 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2013 | int err_code = 0; |
| 2014 | |
| 2015 | if (!*(args[*cur_arg+1])) { |
| 2016 | memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]); |
| 2017 | goto error; |
| 2018 | } |
| 2019 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2020 | if (!rules) { |
| 2021 | rules = calloc(1, sizeof(*rules)); |
| 2022 | if (!rules) { |
| 2023 | memprintf(errmsg, "out of memory."); |
| 2024 | goto error; |
| 2025 | } |
| 2026 | LIST_INIT(&rules->preset_vars); |
| 2027 | srv->agent.tcpcheck_rules = rules; |
| 2028 | } |
| 2029 | |
| 2030 | if (!set_srv_agent_send(srv, args[*cur_arg+1])) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2031 | memprintf(errmsg, "out of memory."); |
| 2032 | goto error; |
| 2033 | } |
| 2034 | |
| 2035 | out: |
| 2036 | return err_code; |
| 2037 | |
| 2038 | error: |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2039 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2040 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2041 | goto out; |
| 2042 | } |
| 2043 | |
| 2044 | /* Parse the "no-agent-send" server keyword */ |
| 2045 | static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2046 | char **errmsg) |
| 2047 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2048 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2049 | return 0; |
| 2050 | } |
| 2051 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2052 | /* Parse the "check" server keyword */ |
| 2053 | static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2054 | char **errmsg) |
| 2055 | { |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 2056 | if (!(curpx->cap & PR_CAP_BE)) { |
| 2057 | memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability", |
| 2058 | args[*cur_arg], proxy_type_str(curpx), curpx->id); |
| 2059 | return ERR_WARN; |
| 2060 | } |
| 2061 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2062 | srv->do_check = 1; |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
| 2066 | /* Parse the "check-send-proxy" server keyword */ |
| 2067 | static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2068 | char **errmsg) |
| 2069 | { |
| 2070 | srv->check.send_proxy = 1; |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
| 2074 | /* Parse the "check-via-socks4" server keyword */ |
| 2075 | static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2076 | char **errmsg) |
| 2077 | { |
| 2078 | srv->check.via_socks4 = 1; |
| 2079 | return 0; |
| 2080 | } |
| 2081 | |
| 2082 | /* Parse the "no-check" server keyword */ |
| 2083 | static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2084 | char **errmsg) |
| 2085 | { |
| 2086 | deinit_srv_check(srv); |
| 2087 | return 0; |
| 2088 | } |
| 2089 | |
| 2090 | /* Parse the "no-check-send-proxy" server keyword */ |
| 2091 | static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2092 | char **errmsg) |
| 2093 | { |
| 2094 | srv->check.send_proxy = 0; |
| 2095 | return 0; |
| 2096 | } |
| 2097 | |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 2098 | /* parse the "check-proto" server keyword */ |
| 2099 | static int srv_parse_check_proto(char **args, int *cur_arg, |
| 2100 | struct proxy *px, struct server *newsrv, char **err) |
| 2101 | { |
| 2102 | int err_code = 0; |
| 2103 | |
| 2104 | if (!*args[*cur_arg + 1]) { |
| 2105 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 2106 | goto error; |
| 2107 | } |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2108 | newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1])); |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 2109 | if (!newsrv->check.mux_proto) { |
| 2110 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 2111 | goto error; |
| 2112 | } |
| 2113 | |
| 2114 | out: |
| 2115 | return err_code; |
| 2116 | |
| 2117 | error: |
| 2118 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2119 | goto out; |
| 2120 | } |
| 2121 | |
| 2122 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2123 | /* Parse the "rise" server keyword */ |
| 2124 | static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2125 | char **errmsg) |
| 2126 | { |
| 2127 | int err_code = 0; |
| 2128 | |
| 2129 | if (!*args[*cur_arg + 1]) { |
| 2130 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 2131 | goto error; |
| 2132 | } |
| 2133 | |
| 2134 | srv->check.rise = atol(args[*cur_arg+1]); |
| 2135 | if (srv->check.rise <= 0) { |
| 2136 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 2137 | goto error; |
| 2138 | } |
| 2139 | |
| 2140 | if (srv->check.health) |
| 2141 | srv->check.health = srv->check.rise; |
| 2142 | |
| 2143 | out: |
| 2144 | return err_code; |
| 2145 | |
| 2146 | error: |
| 2147 | deinit_srv_agent_check(srv); |
| 2148 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2149 | goto out; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* Parse the "fall" server keyword */ |
| 2153 | static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2154 | char **errmsg) |
| 2155 | { |
| 2156 | int err_code = 0; |
| 2157 | |
| 2158 | if (!*args[*cur_arg + 1]) { |
| 2159 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 2160 | goto error; |
| 2161 | } |
| 2162 | |
| 2163 | srv->check.fall = atol(args[*cur_arg+1]); |
| 2164 | if (srv->check.fall <= 0) { |
| 2165 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 2166 | goto error; |
| 2167 | } |
| 2168 | |
| 2169 | out: |
| 2170 | return err_code; |
| 2171 | |
| 2172 | error: |
| 2173 | deinit_srv_agent_check(srv); |
| 2174 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2175 | goto out; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | /* Parse the "inter" server keyword */ |
| 2179 | static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2180 | char **errmsg) |
| 2181 | { |
| 2182 | const char *err = NULL; |
| 2183 | unsigned int delay; |
| 2184 | int err_code = 0; |
| 2185 | |
| 2186 | if (!*(args[*cur_arg+1])) { |
| 2187 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 2188 | goto error; |
| 2189 | } |
| 2190 | |
| 2191 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 2192 | if (err == PARSE_TIME_OVER) { |
| 2193 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 2194 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2195 | goto error; |
| 2196 | } |
| 2197 | else if (err == PARSE_TIME_UNDER) { |
| 2198 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 2199 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2200 | goto error; |
| 2201 | } |
| 2202 | else if (err) { |
| 2203 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 2204 | *err, srv->id); |
| 2205 | goto error; |
| 2206 | } |
| 2207 | if (delay <= 0) { |
| 2208 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 2209 | delay, args[*cur_arg], srv->id); |
| 2210 | goto error; |
| 2211 | } |
| 2212 | srv->check.inter = delay; |
| 2213 | |
| 2214 | out: |
| 2215 | return err_code; |
| 2216 | |
| 2217 | error: |
| 2218 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2219 | goto out; |
| 2220 | } |
| 2221 | |
| 2222 | |
| 2223 | /* Parse the "fastinter" server keyword */ |
| 2224 | static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2225 | char **errmsg) |
| 2226 | { |
| 2227 | const char *err = NULL; |
| 2228 | unsigned int delay; |
| 2229 | int err_code = 0; |
| 2230 | |
| 2231 | if (!*(args[*cur_arg+1])) { |
| 2232 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 2233 | goto error; |
| 2234 | } |
| 2235 | |
| 2236 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 2237 | if (err == PARSE_TIME_OVER) { |
| 2238 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 2239 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2240 | goto error; |
| 2241 | } |
| 2242 | else if (err == PARSE_TIME_UNDER) { |
| 2243 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 2244 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2245 | goto error; |
| 2246 | } |
| 2247 | else if (err) { |
| 2248 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 2249 | *err, srv->id); |
| 2250 | goto error; |
| 2251 | } |
| 2252 | if (delay <= 0) { |
| 2253 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 2254 | delay, args[*cur_arg], srv->id); |
| 2255 | goto error; |
| 2256 | } |
| 2257 | srv->check.fastinter = delay; |
| 2258 | |
| 2259 | out: |
| 2260 | return err_code; |
| 2261 | |
| 2262 | error: |
| 2263 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2264 | goto out; |
| 2265 | } |
| 2266 | |
| 2267 | |
| 2268 | /* Parse the "downinter" server keyword */ |
| 2269 | static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2270 | char **errmsg) |
| 2271 | { |
| 2272 | const char *err = NULL; |
| 2273 | unsigned int delay; |
| 2274 | int err_code = 0; |
| 2275 | |
| 2276 | if (!*(args[*cur_arg+1])) { |
| 2277 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 2278 | goto error; |
| 2279 | } |
| 2280 | |
| 2281 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 2282 | if (err == PARSE_TIME_OVER) { |
| 2283 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 2284 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2285 | goto error; |
| 2286 | } |
| 2287 | else if (err == PARSE_TIME_UNDER) { |
| 2288 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 2289 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2290 | goto error; |
| 2291 | } |
| 2292 | else if (err) { |
| 2293 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 2294 | *err, srv->id); |
| 2295 | goto error; |
| 2296 | } |
| 2297 | if (delay <= 0) { |
| 2298 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 2299 | delay, args[*cur_arg], srv->id); |
| 2300 | goto error; |
| 2301 | } |
| 2302 | srv->check.downinter = delay; |
| 2303 | |
| 2304 | out: |
| 2305 | return err_code; |
| 2306 | |
| 2307 | error: |
| 2308 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2309 | goto out; |
| 2310 | } |
| 2311 | |
| 2312 | /* Parse the "port" server keyword */ |
| 2313 | static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2314 | char **errmsg) |
| 2315 | { |
| 2316 | int err_code = 0; |
| 2317 | |
| 2318 | if (!*(args[*cur_arg+1])) { |
| 2319 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 2320 | goto error; |
| 2321 | } |
| 2322 | |
Amaury Denoyelle | 403dce8 | 2021-07-29 15:39:43 +0200 | [diff] [blame] | 2323 | /* Only increment maxsock for servers from the configuration. Dynamic |
| 2324 | * servers at the moment are not taken into account for the estimation |
| 2325 | * of the resources limits. |
| 2326 | */ |
| 2327 | if (global.mode & MODE_STARTING) |
| 2328 | global.maxsock++; |
| 2329 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2330 | srv->check.port = atol(args[*cur_arg+1]); |
William Dauchy | 4858fb2 | 2021-02-03 22:30:09 +0100 | [diff] [blame] | 2331 | /* if agentport was never set, we can use port */ |
| 2332 | if (!(srv->flags & SRV_F_AGENTPORT)) |
| 2333 | srv->agent.port = srv->check.port; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2334 | |
| 2335 | out: |
| 2336 | return err_code; |
| 2337 | |
| 2338 | error: |
| 2339 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2340 | goto out; |
| 2341 | } |
| 2342 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2343 | static struct srv_kw_list srv_kws = { "CHK", { }, { |
Amaury Denoyelle | 9ecee0f | 2021-07-23 16:34:58 +0200 | [diff] [blame] | 2344 | { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */ |
| 2345 | { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */ |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 2346 | { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */ |
Amaury Denoyelle | 9ecee0f | 2021-07-23 16:34:58 +0200 | [diff] [blame] | 2347 | { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */ |
| 2348 | { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */ |
| 2349 | { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */ |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 2350 | { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */ |
Amaury Denoyelle | 9ecee0f | 2021-07-23 16:34:58 +0200 | [diff] [blame] | 2351 | { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */ |
| 2352 | { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */ |
| 2353 | { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2354 | { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */ |
| 2355 | { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */ |
| 2356 | { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1, 0 }, /* Disable PROXY protocol for health checks */ |
Amaury Denoyelle | 9ecee0f | 2021-07-23 16:34:58 +0200 | [diff] [blame] | 2357 | { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */ |
| 2358 | { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */ |
| 2359 | { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */ |
| 2360 | { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */ |
| 2361 | { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */ |
| 2362 | { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */ |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2363 | { NULL, NULL, 0 }, |
| 2364 | }}; |
| 2365 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2366 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 2367 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2368 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2369 | * Local variables: |
| 2370 | * c-indent-level: 8 |
| 2371 | * c-basic-offset: 8 |
| 2372 | * End: |
| 2373 | */ |