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