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