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> |
Willy Tarreau | eb92deb | 2020-06-04 10:53:16 +0200 | [diff] [blame] | 39 | #include <haproxy/dns.h> |
Christopher Faulet | b51e037 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 40 | #include <haproxy/dynbuf-t.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 41 | #include <haproxy/extcheck.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 42 | #include <haproxy/fd.h> |
| 43 | #include <haproxy/global.h> |
| 44 | #include <haproxy/h1.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 45 | #include <haproxy/http.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 46 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 47 | #include <haproxy/htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 48 | #include <haproxy/istbuf.h> |
| 49 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 50 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 51 | #include <haproxy/mailers.h> |
| 52 | #include <haproxy/port_range.h> |
| 53 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 54 | #include <haproxy/protocol.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 55 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 56 | #include <haproxy/queue.h> |
| 57 | #include <haproxy/regex.h> |
| 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 | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 155 | /* Converts check_status code to description */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 156 | const char *get_check_status_description(short check_status) { |
| 157 | |
| 158 | const char *desc; |
| 159 | |
| 160 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 161 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 162 | else |
| 163 | desc = NULL; |
| 164 | |
| 165 | if (desc && *desc) |
| 166 | return desc; |
| 167 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 168 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 171 | /* Converts check_status code to short info */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 172 | const char *get_check_status_info(short check_status) { |
| 173 | |
| 174 | const char *info; |
| 175 | |
| 176 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 177 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 178 | else |
| 179 | info = NULL; |
| 180 | |
| 181 | if (info && *info) |
| 182 | return info; |
| 183 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 184 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 187 | /* Convert analyze_status to description */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 188 | const char *get_analyze_status(short analyze_status) { |
| 189 | |
| 190 | const char *desc; |
| 191 | |
| 192 | if (analyze_status < HANA_STATUS_SIZE) |
| 193 | desc = analyze_statuses[analyze_status].desc; |
| 194 | else |
| 195 | desc = NULL; |
| 196 | |
| 197 | if (desc && *desc) |
| 198 | return desc; |
| 199 | else |
| 200 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 201 | } |
| 202 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 203 | /* Sets check->status, update check->duration and fill check->result with an |
| 204 | * adequate CHK_RES_* value. The new check->health is computed based on the |
| 205 | * result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 206 | * |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 207 | * Shows information in logs about failed health check if server is UP or |
| 208 | * succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 209 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 210 | 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] | 211 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 212 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 213 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 214 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 215 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 216 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 217 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 218 | check->desc[0] = '\0'; |
| 219 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 223 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 224 | return; |
| 225 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 226 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 227 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 228 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 229 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 230 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 231 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 232 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 233 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 234 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 235 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 236 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 237 | check->duration = -1; |
| 238 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 239 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 240 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 241 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 244 | /* no change is expected if no state change occurred */ |
| 245 | if (check->result == CHK_RES_NEUTRAL) |
| 246 | return; |
| 247 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 248 | /* If the check was really just sending a mail, it won't have an |
| 249 | * associated server, so we're done now. |
| 250 | */ |
| 251 | if (!s) |
| 252 | return; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 253 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 254 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 255 | switch (check->result) { |
| 256 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 257 | /* Failure to connect to the agent as a secondary check should not |
| 258 | * cause the server to be marked down. |
| 259 | */ |
| 260 | if ((!(check->state & CHK_ST_AGENT) || |
Simon Horman | eaabd52 | 2015-02-26 11:26:17 +0900 | [diff] [blame] | 261 | (check->status >= HCHK_STATUS_L57DATA)) && |
Christopher Faulet | b119a79 | 2018-05-02 12:12:45 +0200 | [diff] [blame] | 262 | (check->health > 0)) { |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 263 | _HA_ATOMIC_ADD(&s->counters.failed_checks, 1); |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 264 | report = 1; |
| 265 | check->health--; |
| 266 | if (check->health < check->rise) |
| 267 | check->health = 0; |
| 268 | } |
| 269 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 270 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 271 | case CHK_RES_PASSED: |
| 272 | case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */ |
| 273 | if ((check->health < check->rise + check->fall - 1) && |
| 274 | (check->result == CHK_RES_PASSED || check->health > 0)) { |
| 275 | report = 1; |
| 276 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 277 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 278 | if (check->health >= check->rise) |
| 279 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 280 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 281 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 282 | /* clear consecutive_errors if observing is enabled */ |
| 283 | if (s->onerror) |
| 284 | s->consecutive_errors = 0; |
| 285 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 286 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 287 | default: |
| 288 | break; |
| 289 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 291 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 292 | (status != prev_status || report)) { |
| 293 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 294 | "%s check for %sserver %s/%s %s%s", |
| 295 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 296 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 297 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 298 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 299 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 300 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 301 | srv_append_status(&trash, s, check, -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 302 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 303 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 304 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 305 | (check->health >= check->rise) ? check->fall : check->rise, |
| 306 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 307 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 308 | ha_warning("%s.\n", trash.area); |
| 309 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
| 310 | send_email_alert(s, LOG_INFO, "%s", trash.area); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 311 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 314 | /* Marks the check <check>'s server down if the current check is already failed |
| 315 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 316 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 317 | void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 318 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 319 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 320 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 321 | /* The agent secondary check should only cause a server to be marked |
| 322 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 323 | * that the agent returned "fail", "stopped" or "down". |
| 324 | * The implication here is that failure to connect to the agent |
| 325 | * as a secondary check should not cause the server to be marked |
| 326 | * down. */ |
| 327 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 328 | return; |
| 329 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 330 | if (check->health > 0) |
| 331 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 332 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 333 | /* 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] | 334 | 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] | 335 | } |
| 336 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 337 | /* 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] | 338 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 339 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 340 | * following conditions is true : |
| 341 | * - health check failed (check->health < rise) |
| 342 | * - agent check failed (agent->health < rise) |
| 343 | * - the server tracks a down server (track && track->state == STOPPED) |
| 344 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 345 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 346 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 347 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 348 | void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 349 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 350 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 351 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 352 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 353 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 354 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 355 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 356 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 357 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 358 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 359 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 360 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 361 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 362 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 363 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 364 | if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 365 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 366 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 367 | 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] | 368 | } |
| 369 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 370 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 371 | * if it was running or starting, and provided it isn't in maintenance and other |
| 372 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 373 | * the same as for it to be turned up. Also, only the health checks support the |
| 374 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 375 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 376 | void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 377 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 378 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 379 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 380 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 381 | return; |
| 382 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 383 | if (check->state & CHK_ST_AGENT) |
| 384 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 385 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 386 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 387 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 388 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 389 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 390 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 391 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 392 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 393 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 394 | |
Willy Tarreau | b26881a | 2017-12-23 11:16:49 +0100 | [diff] [blame] | 395 | 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] | 396 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 397 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 398 | /* note: use health_adjust() only, which first checks that the observe mode is |
Willy Tarreau | 93da4dc | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 399 | * enabled. This will take the server lock if needed. |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 400 | */ |
| 401 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 402 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 403 | int failed; |
| 404 | int expire; |
| 405 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 406 | if (s->observe >= HANA_OBS_SIZE) |
| 407 | return; |
| 408 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 409 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 410 | return; |
| 411 | |
| 412 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 413 | case 1: |
| 414 | failed = 1; |
| 415 | break; |
| 416 | |
| 417 | case 2: |
| 418 | failed = 0; |
| 419 | break; |
| 420 | |
| 421 | default: |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (!failed) { |
| 426 | /* good: clear consecutive_errors */ |
| 427 | s->consecutive_errors = 0; |
| 428 | return; |
| 429 | } |
| 430 | |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 431 | _HA_ATOMIC_ADD(&s->consecutive_errors, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 432 | |
| 433 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 434 | return; |
| 435 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 436 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 437 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 438 | |
Willy Tarreau | 93da4dc | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 439 | if (s->check.fastinter) |
| 440 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
| 441 | else |
| 442 | expire = TICK_ETERNITY; |
| 443 | |
| 444 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 445 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 446 | switch (s->onerror) { |
| 447 | case HANA_ONERR_FASTINTER: |
| 448 | /* force fastinter - nothing to do here as all modes force it */ |
| 449 | break; |
| 450 | |
| 451 | case HANA_ONERR_SUDDTH: |
| 452 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 453 | if (s->check.health > s->check.rise) |
| 454 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 455 | |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 456 | /* fall through */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 457 | |
| 458 | case HANA_ONERR_FAILCHK: |
| 459 | /* simulate a failed health check */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 460 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 461 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 462 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case HANA_ONERR_MARKDWN: |
| 466 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 467 | s->check.health = s->check.rise; |
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 | default: |
| 474 | /* write a warning? */ |
| 475 | break; |
| 476 | } |
| 477 | |
Willy Tarreau | 93da4dc | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 478 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 479 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 480 | s->consecutive_errors = 0; |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 481 | _HA_ATOMIC_ADD(&s->counters.failed_hana, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 482 | |
Christopher Faulet | fed5db8 | 2021-05-07 11:45:26 +0200 | [diff] [blame] | 483 | if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) { |
Willy Tarreau | 93da4dc | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 484 | /* requeue check task with new expire */ |
| 485 | task_schedule(s->check.task, expire); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 486 | } |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 489 | /* 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] | 490 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 491 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 492 | * Warning, this must be done only once when returning from poll, and never |
| 493 | * after an I/O error was attempted, otherwise the error queue might contain |
| 494 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 495 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 496 | * clean (including a properly closed socket). |
| 497 | */ |
| 498 | static int retrieve_errno_from_socket(struct connection *conn) |
| 499 | { |
| 500 | int skerr; |
| 501 | socklen_t lskerr = sizeof(skerr); |
| 502 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 503 | if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 504 | return 1; |
| 505 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 506 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 507 | return 0; |
| 508 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 509 | if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 510 | errno = skerr; |
| 511 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 512 | errno = unclean_errno(errno); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 513 | |
| 514 | if (!errno) { |
| 515 | /* we could not retrieve an error, that does not mean there is |
| 516 | * none. Just don't change anything and only report the prior |
| 517 | * error if any. |
| 518 | */ |
| 519 | if (conn->flags & CO_FL_ERROR) |
| 520 | return 1; |
| 521 | else |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 526 | return 1; |
| 527 | } |
| 528 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 529 | /* Tries to collect as much information as possible on the connection status, |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 530 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 531 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 532 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 533 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 534 | * to extract errno from the socket. If no error is reported, it will consider |
| 535 | * the <expired> flag. This is intended to be used when a connection error was |
| 536 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 537 | * function takes care of not updating a server status which was already set. |
| 538 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 539 | * produce a status. |
| 540 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 541 | 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] | 542 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 543 | struct conn_stream *cs = check->cs; |
| 544 | struct connection *conn = cs_conn(cs); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 545 | const char *err_msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 546 | struct buffer *chk; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 547 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 548 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 549 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 550 | return; |
| 551 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 552 | errno = unclean_errno(errno_bck); |
| 553 | if (conn && errno) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 554 | retrieve_errno_from_socket(conn); |
| 555 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 556 | if (conn && !(conn->flags & CO_FL_ERROR) && |
| 557 | !(cs->flags & CS_FL_ERROR) && !expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 558 | return; |
| 559 | |
| 560 | /* we'll try to build a meaningful error message depending on the |
| 561 | * context of the error possibly present in conn->err_code, and the |
| 562 | * socket error possibly collected above. This is useful to know the |
| 563 | * exact step of the L6 layer (eg: SSL handshake). |
| 564 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 565 | chk = get_trash_chunk(); |
| 566 | |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 567 | if (check->type == PR_O2_TCPCHK_CHK && |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 568 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 569 | step = tcpcheck_get_step_id(check, NULL); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 570 | if (!step) |
| 571 | chunk_printf(chk, " at initial connection step of tcp-check"); |
| 572 | else { |
| 573 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 574 | /* we were looking for a string */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 575 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 576 | if (check->current_step->connect.port) |
| 577 | chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 578 | else |
| 579 | chunk_appendf(chk, " (connect)"); |
| 580 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 581 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) { |
| 582 | struct tcpcheck_expect *expect = &check->current_step->expect; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 583 | |
| 584 | switch (expect->type) { |
| 585 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 586 | 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] | 587 | break; |
| 588 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 589 | 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] | 590 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 591 | case TCPCHK_EXPECT_STRING_REGEX: |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 592 | chunk_appendf(chk, " (expect regex)"); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 593 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 594 | case TCPCHK_EXPECT_BINARY_REGEX: |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 595 | chunk_appendf(chk, " (expect binary regex)"); |
| 596 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 597 | case TCPCHK_EXPECT_STRING_LF: |
| 598 | chunk_appendf(chk, " (expect log-format string)"); |
| 599 | break; |
| 600 | case TCPCHK_EXPECT_BINARY_LF: |
| 601 | chunk_appendf(chk, " (expect log-format binary)"); |
| 602 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 603 | case TCPCHK_EXPECT_HTTP_STATUS: |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 604 | chunk_appendf(chk, " (expect HTTP status codes)"); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 605 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 606 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 607 | chunk_appendf(chk, " (expect HTTP status regex)"); |
| 608 | break; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 609 | case TCPCHK_EXPECT_HTTP_HEADER: |
| 610 | chunk_appendf(chk, " (expect HTTP header pattern)"); |
| 611 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 612 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 613 | 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] | 614 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 615 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 616 | chunk_appendf(chk, " (expect HTTP body regex)"); |
| 617 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 618 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 619 | chunk_appendf(chk, " (expect log-format HTTP body)"); |
| 620 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 621 | case TCPCHK_EXPECT_CUSTOM: |
| 622 | chunk_appendf(chk, " (expect custom function)"); |
| 623 | break; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 624 | case TCPCHK_EXPECT_UNDEF: |
| 625 | chunk_appendf(chk, " (undefined expect!)"); |
| 626 | break; |
| 627 | } |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 628 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 629 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 630 | chunk_appendf(chk, " (send)"); |
| 631 | } |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 632 | |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 633 | if (check->current_step && check->current_step->comment) |
| 634 | chunk_appendf(chk, " comment: '%s'", check->current_step->comment); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 638 | if (conn && conn->err_code) { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 639 | if (unclean_errno(errno)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 640 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), |
| 641 | chk->area); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 642 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 643 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), |
| 644 | chk->area); |
| 645 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 646 | } |
| 647 | else { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 648 | if (unclean_errno(errno)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 649 | chunk_printf(&trash, "%s%s", strerror(errno), |
| 650 | chk->area); |
| 651 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 652 | } |
| 653 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 654 | err_msg = chk->area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 658 | if (check->state & CHK_ST_PORT_MISS) { |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 659 | /* NOTE: this is reported after <fall> tries */ |
| 660 | chunk_printf(chk, "No port available for the TCP connection"); |
| 661 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 662 | } |
| 663 | |
Christopher Faulet | 5e29376 | 2020-10-26 11:10:49 +0100 | [diff] [blame] | 664 | if (!conn || !conn->ctrl) { |
| 665 | /* error before any connection attempt (connection allocation error or no control layer) */ |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 666 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 667 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 668 | else if (conn->flags & CO_FL_WAIT_L4_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 669 | /* L4 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 670 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 671 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 672 | else if (expired) |
| 673 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * might be due to a server IP change. |
| 677 | * Let's trigger a DNS resolution if none are currently running. |
| 678 | */ |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 679 | if (check->server) |
| 680 | dns_trigger_resolution(check->server->dns_requester); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 681 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 682 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 683 | else if (conn->flags & CO_FL_WAIT_L6_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 684 | /* L6 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 685 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 686 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 687 | else if (expired) |
| 688 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 689 | } |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 690 | else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 691 | /* I/O error after connection was established and before we could diagnose */ |
| 692 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 693 | } |
| 694 | else if (expired) { |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 695 | enum healthcheck_status tout = HCHK_STATUS_L7TOUT; |
| 696 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 697 | /* connection established but expired check */ |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 698 | if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT && |
| 699 | check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN) |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 700 | tout = check->current_step->expect.tout_status; |
| 701 | set_server_check_status(check, tout, err_msg); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | return; |
| 705 | } |
| 706 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 707 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 708 | /* Builds the server state header used by HTTP health-checks */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 709 | int httpchk_build_status_header(struct server *s, struct buffer *buf) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 710 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 711 | int sv_state; |
| 712 | int ratio; |
| 713 | char addr[46]; |
| 714 | char port[6]; |
| 715 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 716 | "UP %d/%d", "UP", |
| 717 | "NOLB %d/%d", "NOLB", |
| 718 | "no check" }; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 719 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 720 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 721 | sv_state = 6; |
| 722 | else if (s->cur_state != SRV_ST_STOPPED) { |
| 723 | if (s->check.health == s->check.rise + s->check.fall - 1) |
| 724 | sv_state = 3; /* UP */ |
| 725 | else |
| 726 | sv_state = 2; /* going down */ |
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->cur_state == SRV_ST_STOPPING) |
| 729 | sv_state += 2; |
| 730 | } else { |
| 731 | if (s->check.health) |
| 732 | sv_state = 1; /* going up */ |
| 733 | else |
| 734 | sv_state = 0; /* DOWN */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 735 | } |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 736 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 737 | chunk_appendf(buf, srv_hlt_st[sv_state], |
| 738 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 739 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 740 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 741 | addr_to_str(&s->addr, addr, sizeof(addr)); |
| 742 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 743 | snprintf(port, sizeof(port), "%u", s->svc_port); |
| 744 | else |
| 745 | *port = 0; |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 746 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 747 | chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 748 | addr, port, s->proxy->id, s->id, |
| 749 | global.node, |
| 750 | (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 751 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 752 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 753 | s->nbpend); |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 754 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 755 | if ((s->cur_state == SRV_ST_STARTING) && |
| 756 | now.tv_sec < s->last_change + s->slowstart && |
| 757 | now.tv_sec >= s->last_change) { |
| 758 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
| 759 | chunk_appendf(buf, "; throttle=%d%%", ratio); |
| 760 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 761 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 762 | return b_data(buf); |
| 763 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 764 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 765 | /**************************************************************************/ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 766 | /***************** Health-checks based on connections *********************/ |
| 767 | /**************************************************************************/ |
| 768 | /* This function is used only for server health-checks. It handles connection |
| 769 | * status updates including errors. If necessary, it wakes the check task up. |
| 770 | * It returns 0 on normal cases, <0 if at least one close() has happened on the |
| 771 | * connection (eg: reconnect). It relies on tcpcheck_main(). |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 772 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 773 | static int wake_srv_chk(struct conn_stream *cs) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 774 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 775 | struct connection *conn = cs->conn; |
| 776 | struct check *check = cs->data; |
| 777 | struct email_alertq *q = container_of(check, typeof(*q), check); |
| 778 | int ret = 0; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 779 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 780 | if (check->server) |
| 781 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 782 | else |
| 783 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 784 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 785 | /* we may have to make progress on the TCP checks */ |
| 786 | ret = tcpcheck_main(check); |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 787 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 788 | cs = check->cs; |
| 789 | conn = cs->conn; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 790 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 791 | if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) { |
| 792 | /* We may get error reports bypassing the I/O handlers, typically |
| 793 | * the case when sending a pure TCP check which fails, then the I/O |
| 794 | * handlers above are not called. This is completely handled by the |
| 795 | * main processing task so let's simply wake it up. If we get here, |
| 796 | * we expect errno to still be valid. |
| 797 | */ |
| 798 | chk_report_conn_err(check, errno, 0); |
| 799 | task_wakeup(check->task, TASK_WOKEN_IO); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Christopher Faulet | 444b7b1 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 802 | if (check->result != CHK_RES_UNKNOWN || ret == -1) { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 803 | /* Check complete or aborted. If connection not yet closed do it |
| 804 | * now and wake the check task up to be sure the result is |
| 805 | * handled ASAP. */ |
| 806 | conn_sock_drain(conn); |
| 807 | cs_close(cs); |
| 808 | ret = -1; |
Christopher Faulet | 444b7b1 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 809 | |
| 810 | if (check->wait_list.events) |
| 811 | cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list); |
| 812 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 813 | /* We may have been scheduled to run, and the |
| 814 | * I/O handler expects to have a cs, so remove |
| 815 | * the tasklet |
| 816 | */ |
| 817 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 818 | task_wakeup(check->task, TASK_WOKEN_IO); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 819 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 820 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 821 | if (check->server) |
| 822 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 823 | else |
| 824 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 825 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 826 | /* if a connection got replaced, we must absolutely prevent the connection |
| 827 | * handler from touching its fd, and perform the FD polling updates ourselves |
| 828 | */ |
| 829 | if (ret < 0) |
| 830 | conn_cond_update_polling(conn); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 831 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 832 | return ret; |
| 833 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 834 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 835 | /* This function checks if any I/O is wanted, and if so, attempts to do so */ |
| 836 | static struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned short state) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 837 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 838 | struct check *check = ctx; |
| 839 | struct conn_stream *cs = check->cs; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 840 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 841 | wake_srv_chk(cs); |
| 842 | return NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 843 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 844 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 845 | /* manages a server health-check that uses a connection. Returns |
| 846 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 847 | * |
| 848 | * 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] | 849 | * via the out_unlock label. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 850 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 851 | static struct task *process_chk_conn(struct task *t, void *context, unsigned short state) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 852 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 853 | struct check *check = context; |
| 854 | struct proxy *proxy = check->proxy; |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 855 | struct conn_stream *cs; |
| 856 | struct connection *conn; |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 857 | int rv; |
| 858 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 859 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 860 | if (check->server) |
| 861 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 862 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 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); |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 885 | expired = 0; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 886 | } |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 887 | |
| 888 | cs = check->cs; |
| 889 | conn = cs_conn(cs); |
| 890 | |
| 891 | /* there was a test running. |
| 892 | * First, let's check whether there was an uncaught error, |
| 893 | * which can happen on connect timeout or error. |
| 894 | */ |
| 895 | if (check->result == CHK_RES_UNKNOWN) { |
| 896 | if ((conn && ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR))) || expired) { |
| 897 | chk_report_conn_err(check, 0, expired); |
| 898 | } |
| 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); |
Christopher Faulet | 444b7b1 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 907 | } |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 908 | if (check->result == CHK_RES_UNKNOWN) |
| 909 | goto out_unlock; /* timeout not reached, wait again */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 910 | } |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 911 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 912 | |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 913 | /* check complete or aborted */ |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 914 | |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 915 | check->current_step = NULL; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 916 | |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +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 | */ |
| 923 | conn_sock_drain(conn); |
| 924 | cs_close(cs); |
| 925 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 926 | |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 927 | if (cs) { |
| 928 | if (check->wait_list.events) |
| 929 | cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list); |
| 930 | /* We may have been scheduled to run, and the |
| 931 | * I/O handler expects to have a cs, so remove |
| 932 | * the tasklet |
| 933 | */ |
| 934 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 935 | cs_destroy(cs); |
| 936 | cs = check->cs = NULL; |
| 937 | conn = NULL; |
| 938 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 939 | |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 940 | if (check->sess != NULL) { |
| 941 | vars_prune(&check->vars, check->sess, NULL); |
| 942 | session_free(check->sess); |
| 943 | check->sess = NULL; |
| 944 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 945 | |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 946 | if (check->server) { |
| 947 | if (check->result == CHK_RES_FAILED) { |
| 948 | /* a failure or timeout detected */ |
| 949 | check_notify_failure(check); |
| 950 | } |
| 951 | else if (check->result == CHK_RES_CONDPASS) { |
| 952 | /* check is OK but asks for stopping mode */ |
| 953 | check_notify_stopping(check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 954 | } |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 955 | else if (check->result == CHK_RES_PASSED) { |
| 956 | /* a success was detected */ |
| 957 | check_notify_success(check); |
| 958 | } |
| 959 | } |
| 960 | task_set_affinity(t, MAX_THREADS_MASK); |
| 961 | check_release_buf(check, &check->bi); |
| 962 | check_release_buf(check, &check->bo); |
| 963 | 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] | 964 | |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 965 | if (check->server) { |
| 966 | rv = 0; |
| 967 | if (global.spread_checks > 0) { |
| 968 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 969 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 970 | } |
Christopher Faulet | 67da481 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 971 | 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] | 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 | b51e037 | 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 | b51e037 | 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 | b51e037 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1011 | /* |
| 1012 | * Allocate a buffer. If if fails, it adds the check in buffer wait queue. |
| 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 | 954827a | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 1018 | if (likely(!LIST_ADDED(&check->buf_wait.list)) && |
Christopher Faulet | b51e037 | 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 | 954827a | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 1022 | LIST_ADDQ(&ti->buffer_wq, &check->buf_wait.list); |
Christopher Faulet | b51e037 | 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 | 132b3a4 | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 1035 | offer_buffers(check->buf_wait.target, 1); |
Christopher Faulet | b51e037 | 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 | b51e037 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1043 | check->bi = BUF_NULL; |
| 1044 | check->bo = BUF_NULL; |
Willy Tarreau | 954827a | 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 | b51e037 | 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) { |
| 1065 | free(check->cs->conn); |
| 1066 | check->cs->conn = NULL; |
| 1067 | cs_free(check->cs); |
| 1068 | check->cs = NULL; |
| 1069 | } |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1072 | /* manages a server health-check. Returns the time the task accepts to wait, or |
| 1073 | * TIME_ETERNITY for infinity. |
| 1074 | */ |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1075 | struct task *process_chk(struct task *t, void *context, unsigned short state) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1076 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1077 | struct check *check = context; |
| 1078 | |
| 1079 | if (check->type == PR_O2_EXT_CHK) |
| 1080 | return process_chk_proc(t, context, state); |
| 1081 | return process_chk_conn(t, context, state); |
| 1082 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1085 | |
| 1086 | static int start_check_task(struct check *check, int mininter, |
| 1087 | int nbcheck, int srvpos) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1088 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1089 | struct task *t; |
| 1090 | unsigned long thread_mask = MAX_THREADS_MASK; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1091 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1092 | if (check->type == PR_O2_EXT_CHK) |
| 1093 | thread_mask = 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1094 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1095 | /* task for the check */ |
| 1096 | if ((t = task_new(thread_mask)) == NULL) { |
| 1097 | ha_alert("Starting [%s:%s] check: out of memory.\n", |
| 1098 | check->server->proxy->id, check->server->id); |
| 1099 | return 0; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1100 | } |
| 1101 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1102 | check->task = t; |
| 1103 | t->process = process_chk; |
| 1104 | t->context = check; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1105 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1106 | if (mininter < srv_getinter(check)) |
| 1107 | mininter = srv_getinter(check); |
| 1108 | |
| 1109 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 1110 | mininter = global.max_spread_checks; |
| 1111 | |
| 1112 | /* check this every ms */ |
| 1113 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
| 1114 | check->start = now; |
| 1115 | task_queue(t); |
| 1116 | |
| 1117 | return 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1120 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 1121 | * reached, the task automatically stops. Note that any server status change |
| 1122 | * must have updated s->last_change accordingly. |
| 1123 | */ |
| 1124 | static struct task *server_warmup(struct task *t, void *context, unsigned short state) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1125 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1126 | struct server *s = context; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1127 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1128 | /* by default, plan on stopping the task */ |
| 1129 | t->expire = TICK_ETERNITY; |
| 1130 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 1131 | (s->next_state != SRV_ST_STARTING)) |
| 1132 | return t; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1133 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1134 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1135 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1136 | /* recalculate the weights and update the state */ |
| 1137 | server_recalc_eweight(s, 1); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1138 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1139 | /* probably that we can refill this server with a bit more connections */ |
| 1140 | pendconn_grab_from_px(s); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1141 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1142 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1143 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1144 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1145 | * whichever is greater, resulting in small 5% steps. |
| 1146 | */ |
| 1147 | if (s->next_state == SRV_ST_STARTING) |
| 1148 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1149 | return t; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * Start health-check. |
| 1154 | * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case. |
| 1155 | */ |
| 1156 | static int start_checks() |
| 1157 | { |
| 1158 | |
| 1159 | struct proxy *px; |
| 1160 | struct server *s; |
| 1161 | struct task *t; |
| 1162 | int nbcheck=0, mininter=0, srvpos=0; |
| 1163 | |
| 1164 | /* 0- init the dummy frontend used to create all checks sessions */ |
| 1165 | init_new_proxy(&checks_fe); |
Christopher Faulet | 644cf33 | 2021-04-16 10:49:07 +0200 | [diff] [blame] | 1166 | checks_fe.id = strdup("CHECKS-FE"); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1167 | checks_fe.cap = PR_CAP_FE | PR_CAP_BE; |
| 1168 | checks_fe.mode = PR_MODE_TCP; |
| 1169 | checks_fe.maxconn = 0; |
| 1170 | checks_fe.conn_retries = CONN_RETRIES; |
| 1171 | checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
| 1172 | checks_fe.timeout.client = TICK_ETERNITY; |
| 1173 | |
| 1174 | /* 1- count the checkers to run simultaneously. |
| 1175 | * We also determine the minimum interval among all of those which |
| 1176 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1177 | * will be used to spread their start-up date. Those which have |
| 1178 | * a shorter interval will start independently and will not dictate |
| 1179 | * too short an interval for all others. |
| 1180 | */ |
| 1181 | for (px = proxies_list; px; px = px->next) { |
| 1182 | for (s = px->srv; s; s = s->next) { |
| 1183 | if (s->slowstart) { |
| 1184 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
| 1185 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1186 | return ERR_ALERT | ERR_FATAL; |
| 1187 | } |
| 1188 | /* We need a warmup task that will be called when the server |
| 1189 | * state switches from down to up. |
| 1190 | */ |
| 1191 | s->warmup = t; |
| 1192 | t->process = server_warmup; |
| 1193 | t->context = s; |
| 1194 | /* server can be in this state only because of */ |
| 1195 | if (s->next_state == SRV_ST_STARTING) |
| 1196 | 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] | 1197 | } |
| 1198 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1199 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1200 | nbcheck++; |
| 1201 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 1202 | (!mininter || mininter > srv_getinter(&s->check))) |
| 1203 | mininter = srv_getinter(&s->check); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1206 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1207 | nbcheck++; |
| 1208 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 1209 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 1210 | mininter = srv_getinter(&s->agent); |
| 1211 | } |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1212 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1213 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1214 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1215 | if (!nbcheck) |
| 1216 | return 0; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1217 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1218 | srand((unsigned)time(NULL)); |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1219 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1220 | /* |
| 1221 | * 2- start them as far as possible from each others. For this, we will |
| 1222 | * start them after their interval set to the min interval divided by |
| 1223 | * the number of servers, weighted by the server's position in the list. |
| 1224 | */ |
| 1225 | for (px = proxies_list; px; px = px->next) { |
| 1226 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 1227 | if (init_pid_list()) { |
| 1228 | ha_alert("Starting [%s] check: out of memory.\n", px->id); |
| 1229 | return ERR_ALERT | ERR_FATAL; |
| 1230 | } |
| 1231 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1232 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1233 | for (s = px->srv; s; s = s->next) { |
| 1234 | /* A task for the main check */ |
| 1235 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1236 | if (s->check.type == PR_O2_EXT_CHK) { |
| 1237 | if (!prepare_external_check(&s->check)) |
| 1238 | return ERR_ALERT | ERR_FATAL; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1239 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1240 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 1241 | return ERR_ALERT | ERR_FATAL; |
| 1242 | srvpos++; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 1243 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1244 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1245 | /* A task for a auxiliary agent check */ |
| 1246 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1247 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 1248 | return ERR_ALERT | ERR_FATAL; |
| 1249 | } |
| 1250 | srvpos++; |
| 1251 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1252 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1253 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1256 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1257 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1258 | /* |
| 1259 | * Return value: |
| 1260 | * the port to be used for the health check |
| 1261 | * 0 in case no port could be found for the check |
| 1262 | */ |
| 1263 | static int srv_check_healthcheck_port(struct check *chk) |
| 1264 | { |
| 1265 | int i = 0; |
| 1266 | struct server *srv = NULL; |
| 1267 | |
| 1268 | srv = chk->server; |
| 1269 | |
| 1270 | /* by default, we use the health check port ocnfigured */ |
| 1271 | if (chk->port > 0) |
| 1272 | return chk->port; |
| 1273 | |
| 1274 | /* try to get the port from check_core.addr if check.port not set */ |
| 1275 | i = get_host_port(&chk->addr); |
| 1276 | if (i > 0) |
| 1277 | return i; |
| 1278 | |
| 1279 | /* try to get the port from server address */ |
| 1280 | /* prevent MAPPORTS from working at this point, since checks could |
| 1281 | * not be performed in such case (MAPPORTS impose a relative ports |
| 1282 | * based on live traffic) |
| 1283 | */ |
| 1284 | if (srv->flags & SRV_F_MAPPORTS) |
| 1285 | return 0; |
| 1286 | |
| 1287 | i = srv->svc_port; /* by default */ |
| 1288 | if (i > 0) |
| 1289 | return i; |
| 1290 | |
| 1291 | return 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1294 | /* Initializes an health-check attached to the server <srv>. Non-zero is returned |
| 1295 | * if an error occurred. |
| 1296 | */ |
| 1297 | static int init_srv_check(struct server *srv) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1298 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1299 | const char *err; |
| 1300 | struct tcpcheck_rule *r; |
| 1301 | int ret = 0; |
Amaury Denoyelle | a940bcb | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1302 | int check_type; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1303 | |
Christopher Faulet | 0a5e713 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1304 | if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1305 | goto out; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1306 | |
Amaury Denoyelle | a940bcb | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1307 | check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1308 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1309 | /* If neither a port nor an addr was specified and no check transport |
| 1310 | * layer is forced, then the transport layer used by the checks is the |
| 1311 | * same as for the production traffic. Otherwise we use raw_sock by |
| 1312 | * default, unless one is specified. |
| 1313 | */ |
| 1314 | if (!srv->check.port && !is_addr(&srv->check.addr)) { |
| 1315 | if (!srv->check.use_ssl && srv->use_ssl != -1) { |
| 1316 | srv->check.use_ssl = srv->use_ssl; |
| 1317 | srv->check.xprt = srv->xprt; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1318 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1319 | else if (srv->check.use_ssl == 1) |
| 1320 | srv->check.xprt = xprt_get(XPRT_SSL); |
| 1321 | srv->check.send_proxy |= (srv->pp_opts); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1322 | } |
Christopher Faulet | 66163ec | 2020-05-20 22:36:24 +0200 | [diff] [blame] | 1323 | else if (srv->check.use_ssl == 1) |
| 1324 | srv->check.xprt = xprt_get(XPRT_SSL); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1325 | |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1326 | /* Inherit the mux protocol from the server if not already defined for |
| 1327 | * the check |
| 1328 | */ |
Amaury Denoyelle | a940bcb | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1329 | if (srv->mux_proto && !srv->check.mux_proto && |
| 1330 | ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) || |
| 1331 | (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] | 1332 | srv->check.mux_proto = srv->mux_proto; |
Amaury Denoyelle | a940bcb | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1333 | } |
Amaury Denoyelle | 92c8ac1 | 2020-11-13 12:34:57 +0100 | [diff] [blame] | 1334 | /* test that check proto is valid if explicitly defined */ |
| 1335 | else if (srv->check.mux_proto && |
| 1336 | ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) || |
| 1337 | (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) { |
| 1338 | ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n", |
| 1339 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1340 | ret |= ERR_ALERT | ERR_FATAL; |
| 1341 | goto out; |
| 1342 | } |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1343 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1344 | /* validate <srv> server health-check settings */ |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1345 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1346 | /* We need at least a service port, a check port or the first tcp-check |
| 1347 | * rule must be a 'connect' one when checking an IPv4/IPv6 server. |
| 1348 | */ |
| 1349 | if ((srv_check_healthcheck_port(&srv->check) != 0) || |
| 1350 | (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr)))) |
| 1351 | goto init; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1352 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1353 | if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) { |
| 1354 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n", |
| 1355 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1356 | ret |= ERR_ALERT | ERR_ABORT; |
| 1357 | goto out; |
| 1358 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1359 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1360 | /* search the first action (connect / send / expect) in the list */ |
| 1361 | r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules); |
| 1362 | if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) { |
| 1363 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port " |
| 1364 | "nor tcp_check rule 'connect' with port information.\n", |
| 1365 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1366 | ret |= ERR_ALERT | ERR_ABORT; |
| 1367 | goto out; |
| 1368 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1369 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1370 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 1371 | list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) { |
| 1372 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) { |
| 1373 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port, " |
| 1374 | "and a tcp_check rule 'connect' with no port information.\n", |
| 1375 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1376 | ret |= ERR_ALERT | ERR_ABORT; |
| 1377 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1378 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1381 | init: |
| 1382 | if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) { |
| 1383 | struct tcpcheck_ruleset *rs = NULL; |
| 1384 | struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules; |
| 1385 | //char *errmsg = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1386 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1387 | srv->proxy->options2 &= ~PR_O2_CHK_ANY; |
| 1388 | srv->proxy->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1389 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1390 | rs = find_tcpcheck_ruleset("*tcp-check"); |
| 1391 | if (!rs) { |
| 1392 | rs = create_tcpcheck_ruleset("*tcp-check"); |
| 1393 | if (rs == NULL) { |
| 1394 | ha_alert("config: %s '%s': out of memory.\n", |
| 1395 | proxy_type_str(srv->proxy), srv->proxy->id); |
| 1396 | ret |= ERR_ALERT | ERR_FATAL; |
| 1397 | goto out; |
| 1398 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1401 | free_tcpcheck_vars(&rules->preset_vars); |
| 1402 | rules->list = &rs->rules; |
| 1403 | rules->flags = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1404 | } |
| 1405 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1406 | err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY); |
| 1407 | if (err) { |
| 1408 | ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n", |
| 1409 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 1410 | ret |= ERR_ALERT | ERR_ABORT; |
| 1411 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1412 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1413 | srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 1414 | global.maxsock++; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1415 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1416 | out: |
| 1417 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1420 | /* Initializes an agent-check attached to the server <srv>. Non-zero is returned |
| 1421 | * if an error occurred. |
| 1422 | */ |
| 1423 | static int init_srv_agent_check(struct server *srv) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1424 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1425 | struct tcpcheck_rule *chk; |
| 1426 | const char *err; |
| 1427 | int ret = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1428 | |
Christopher Faulet | 0a5e713 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1429 | if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1430 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1431 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1432 | /* If there is no connect rule preceding all send / expect rules, an |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1433 | * implicit one is inserted before all others. |
| 1434 | */ |
| 1435 | chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules); |
| 1436 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 1437 | chk = calloc(1, sizeof(*chk)); |
| 1438 | if (!chk) { |
| 1439 | ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule" |
| 1440 | " to agent-check for server '%s' (out of memory).\n", |
| 1441 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1442 | ret |= ERR_ALERT | ERR_FATAL; |
| 1443 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1444 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1445 | chk->action = TCPCHK_ACT_CONNECT; |
| 1446 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
| 1447 | LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1448 | } |
| 1449 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1450 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1451 | err = init_check(&srv->agent, PR_O2_TCPCHK_CHK); |
| 1452 | if (err) { |
| 1453 | ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n", |
| 1454 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 1455 | ret |= ERR_ALERT | ERR_ABORT; |
| 1456 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1459 | if (!srv->agent.inter) |
| 1460 | srv->agent.inter = srv->check.inter; |
| 1461 | |
| 1462 | srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 1463 | global.maxsock++; |
| 1464 | |
| 1465 | out: |
| 1466 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1467 | } |
| 1468 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1469 | static void deinit_srv_check(struct server *srv) |
| 1470 | { |
| 1471 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 1472 | free_check(&srv->check); |
| 1473 | srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED; |
| 1474 | srv->do_check = 0; |
| 1475 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1476 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1477 | |
| 1478 | static void deinit_srv_agent_check(struct server *srv) |
| 1479 | { |
| 1480 | if (srv->agent.tcpcheck_rules) { |
| 1481 | free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars); |
| 1482 | free(srv->agent.tcpcheck_rules); |
| 1483 | srv->agent.tcpcheck_rules = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1484 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1485 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1486 | if (srv->agent.state & CHK_ST_CONFIGURED) |
| 1487 | free_check(&srv->agent); |
| 1488 | |
| 1489 | srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT; |
| 1490 | srv->do_agent = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1491 | } |
| 1492 | |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1493 | REGISTER_POST_SERVER_CHECK(init_srv_check); |
| 1494 | REGISTER_POST_SERVER_CHECK(init_srv_agent_check); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1495 | REGISTER_POST_CHECK(start_checks); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1496 | |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1497 | REGISTER_SERVER_DEINIT(deinit_srv_check); |
| 1498 | REGISTER_SERVER_DEINIT(deinit_srv_agent_check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1499 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1500 | |
| 1501 | /**************************************************************************/ |
| 1502 | /************************** Check sample fetches **************************/ |
| 1503 | /**************************************************************************/ |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1504 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1505 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1506 | { /* END */ }, |
| 1507 | }}; |
| 1508 | |
| 1509 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 1510 | |
| 1511 | |
| 1512 | /**************************************************************************/ |
| 1513 | /************************ Check's parsing functions ***********************/ |
| 1514 | /**************************************************************************/ |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1515 | /* Parses the "http-check" proxy keyword */ |
| 1516 | static int proxy_parse_httpcheck(char **args, int section, struct proxy *curpx, |
| 1517 | struct proxy *defpx, const char *file, int line, |
| 1518 | char **errmsg) |
| 1519 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1520 | struct tcpcheck_ruleset *rs = NULL; |
| 1521 | struct tcpcheck_rule *chk = NULL; |
| 1522 | int index, cur_arg, ret = 0; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1523 | |
| 1524 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL)) |
| 1525 | ret = 1; |
| 1526 | |
| 1527 | cur_arg = 1; |
| 1528 | if (strcmp(args[cur_arg], "disable-on-404") == 0) { |
| 1529 | /* enable a graceful server shutdown on an HTTP 404 response */ |
| 1530 | curpx->options |= PR_O_DISABLE404; |
| 1531 | if (too_many_args(1, args, errmsg, NULL)) |
| 1532 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1533 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1534 | } |
| 1535 | else if (strcmp(args[cur_arg], "send-state") == 0) { |
| 1536 | /* enable emission of the apparent state of a server in HTTP checks */ |
| 1537 | curpx->options2 |= PR_O2_CHK_SNDST; |
| 1538 | if (too_many_args(1, args, errmsg, NULL)) |
| 1539 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1540 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1541 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1542 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1543 | /* Deduce the ruleset name from the proxy info */ |
| 1544 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 1545 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 1546 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1547 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1548 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1549 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1550 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1551 | if (rs == NULL) { |
| 1552 | memprintf(errmsg, "out of memory.\n"); |
| 1553 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1554 | } |
| 1555 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1556 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1557 | index = 0; |
| 1558 | if (!LIST_ISEMPTY(&rs->rules)) { |
| 1559 | chk = LIST_PREV(&rs->rules, typeof(chk), list); |
| 1560 | if (chk->action != TCPCHK_ACT_SEND || !(chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) |
| 1561 | index = chk->index + 1; |
Christopher Faulet | 37e583d | 2021-03-12 12:00:14 +0100 | [diff] [blame] | 1562 | chk = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1563 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1564 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1565 | if (strcmp(args[cur_arg], "connect") == 0) |
| 1566 | chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 1567 | else if (strcmp(args[cur_arg], "send") == 0) |
| 1568 | chk = parse_tcpcheck_send_http(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 1569 | else if (strcmp(args[cur_arg], "expect") == 0) |
| 1570 | chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, TCPCHK_RULES_HTTP_CHK, |
| 1571 | file, line, errmsg); |
| 1572 | else if (strcmp(args[cur_arg], "comment") == 0) |
| 1573 | chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 1574 | else { |
| 1575 | struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1576 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1577 | if (!kw) { |
| 1578 | action_kw_tcp_check_build_list(&trash); |
| 1579 | memprintf(errmsg, "'%s' only supports 'disable-on-404', 'send-state', 'comment', 'connect'," |
| 1580 | " 'send', 'expect'%s%s. but got '%s'", |
| 1581 | args[0], (*trash.area ? ", " : ""), trash.area, args[1]); |
| 1582 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1583 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1584 | chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg); |
| 1585 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1586 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1587 | if (!chk) { |
| 1588 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 1589 | goto error; |
| 1590 | } |
| 1591 | ret = (*errmsg != NULL); /* Handle warning */ |
| 1592 | |
| 1593 | chk->index = index; |
| 1594 | if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK && |
| 1595 | (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 1596 | /* Use this ruleset if the proxy already has http-check enabled */ |
| 1597 | curpx->tcpcheck_rules.list = &rs->rules; |
| 1598 | curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_HTTP_RS; |
| 1599 | if (!tcpcheck_add_http_rule(chk, &curpx->tcpcheck_rules, errmsg)) { |
| 1600 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 1601 | curpx->tcpcheck_rules.list = NULL; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1602 | goto error; |
| 1603 | } |
| 1604 | } |
| 1605 | else { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1606 | /* mark this ruleset as unused for now */ |
| 1607 | curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_HTTP_RS; |
| 1608 | LIST_ADDQ(&rs->rules, &chk->list); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1609 | } |
| 1610 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1611 | out: |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1612 | return ret; |
| 1613 | |
| 1614 | error: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1615 | free_tcpcheck(chk, 0); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1616 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 1617 | return -1; |
| 1618 | } |
| 1619 | |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 1620 | /* Parses the "option tcp-check" proxy keyword */ |
| 1621 | int proxy_parse_tcp_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 1622 | const char *file, int line) |
| 1623 | { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1624 | struct tcpcheck_ruleset *rs = NULL; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 1625 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 1626 | int err_code = 0; |
| 1627 | |
| 1628 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 1629 | err_code |= ERR_WARN; |
| 1630 | |
| 1631 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 1632 | goto out; |
| 1633 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1634 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 1635 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 1636 | |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 1637 | if ((rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1638 | /* If a tcp-check rulesset is already set, do nothing */ |
| 1639 | if (rules->list) |
| 1640 | goto out; |
| 1641 | |
| 1642 | /* If a tcp-check ruleset is waiting to be used for the current proxy, |
| 1643 | * get it. |
| 1644 | */ |
| 1645 | if (rules->flags & TCPCHK_RULES_UNUSED_TCP_RS) |
| 1646 | goto curpx_ruleset; |
| 1647 | |
| 1648 | /* Otherwise, try to get the tcp-check ruleset of the default proxy */ |
| 1649 | chunk_printf(&trash, "*tcp-check-defaults_%s-%d", defpx->conf.file, defpx->conf.line); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1650 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1651 | if (rs) |
| 1652 | goto ruleset_found; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1655 | curpx_ruleset: |
| 1656 | /* Deduce the ruleset name from the proxy info */ |
| 1657 | chunk_printf(&trash, "*tcp-check-%s_%s-%d", |
| 1658 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 1659 | curpx->conf.file, curpx->conf.line); |
| 1660 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1661 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1662 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1663 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1664 | if (rs == NULL) { |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 1665 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 1666 | goto error; |
| 1667 | } |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1670 | ruleset_found: |
| 1671 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1672 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 1673 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 1674 | rules->flags |= TCPCHK_RULES_TCP_CHK; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 1675 | |
| 1676 | out: |
| 1677 | return err_code; |
| 1678 | |
| 1679 | error: |
| 1680 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1681 | goto out; |
| 1682 | } |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1683 | |
| 1684 | /* Parses the "option redis-check" proxy keyword */ |
| 1685 | int proxy_parse_redis_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 1686 | const char *file, int line) |
| 1687 | { |
| 1688 | static char *redis_req = "*1\r\n$4\r\nPING\r\n"; |
| 1689 | static char *redis_res = "+PONG\r\n"; |
| 1690 | |
| 1691 | struct tcpcheck_ruleset *rs = NULL; |
| 1692 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 1693 | struct tcpcheck_rule *chk; |
| 1694 | char *errmsg = NULL; |
| 1695 | int err_code = 0; |
| 1696 | |
| 1697 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 1698 | err_code |= ERR_WARN; |
| 1699 | |
| 1700 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 1701 | goto out; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1702 | |
| 1703 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 1704 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 1705 | |
| 1706 | free_tcpcheck_vars(&rules->preset_vars); |
| 1707 | rules->list = NULL; |
| 1708 | rules->flags = 0; |
| 1709 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1710 | rs = find_tcpcheck_ruleset("*redis-check"); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1711 | if (rs) |
| 1712 | goto ruleset_found; |
| 1713 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1714 | rs = create_tcpcheck_ruleset("*redis-check"); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1715 | if (rs == NULL) { |
| 1716 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 1717 | goto error; |
| 1718 | } |
| 1719 | |
| 1720 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", redis_req, ""}, |
| 1721 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 1722 | if (!chk) { |
| 1723 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1724 | goto error; |
| 1725 | } |
| 1726 | chk->index = 0; |
| 1727 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1728 | |
| 1729 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "string", redis_res, |
| 1730 | "error-status", "L7STS", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 1731 | "on-error", "%[res.payload(0,0),cut_crlf]", |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1732 | "on-success", "Redis server is ok", |
| 1733 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1734 | 1, curpx, &rs->rules, TCPCHK_RULES_REDIS_CHK, file, line, &errmsg); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1735 | if (!chk) { |
| 1736 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1737 | goto error; |
| 1738 | } |
| 1739 | chk->index = 1; |
| 1740 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1741 | |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1742 | ruleset_found: |
| 1743 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 1744 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1745 | rules->flags |= TCPCHK_RULES_REDIS_CHK; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1746 | |
| 1747 | out: |
| 1748 | free(errmsg); |
| 1749 | return err_code; |
| 1750 | |
| 1751 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1752 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 1753 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1754 | goto out; |
| 1755 | } |
| 1756 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1757 | |
| 1758 | /* Parses the "option ssl-hello-chk" proxy keyword */ |
| 1759 | int proxy_parse_ssl_hello_chk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 1760 | const char *file, int line) |
| 1761 | { |
| 1762 | /* This is the SSLv3 CLIENT HELLO packet used in conjunction with the |
| 1763 | * ssl-hello-chk option to ensure that the remote server speaks SSL. |
| 1764 | * |
| 1765 | * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details. |
| 1766 | */ |
| 1767 | static char sslv3_client_hello[] = { |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1768 | "16" /* ContentType : 0x16 = Handshake */ |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1769 | "0300" /* ProtocolVersion : 0x0300 = SSLv3 */ |
| 1770 | "0079" /* ContentLength : 0x79 bytes after this one */ |
| 1771 | "01" /* HanshakeType : 0x01 = CLIENT HELLO */ |
| 1772 | "000075" /* HandshakeLength : 0x75 bytes after this one */ |
| 1773 | "0300" /* Hello Version : 0x0300 = v3 */ |
| 1774 | "%[date(),htonl,hex]" /* Unix GMT Time (s) : filled with <now> (@0x0B) */ |
| 1775 | "%[str(HAPROXYSSLCHK\nHAPROXYSSLCHK\n),hex]" /* Random : must be exactly 28 bytes */ |
| 1776 | "00" /* Session ID length : empty (no session ID) */ |
| 1777 | "004E" /* Cipher Suite Length : 78 bytes after this one */ |
| 1778 | "0001" "0002" "0003" "0004" /* 39 most common ciphers : */ |
| 1779 | "0005" "0006" "0007" "0008" /* 0x01...0x1B, 0x2F...0x3A */ |
| 1780 | "0009" "000A" "000B" "000C" /* This covers RSA/DH, */ |
| 1781 | "000D" "000E" "000F" "0010" /* various bit lengths, */ |
| 1782 | "0011" "0012" "0013" "0014" /* SHA1/MD5, DES/3DES/AES... */ |
| 1783 | "0015" "0016" "0017" "0018" |
| 1784 | "0019" "001A" "001B" "002F" |
| 1785 | "0030" "0031" "0032" "0033" |
| 1786 | "0034" "0035" "0036" "0037" |
| 1787 | "0038" "0039" "003A" |
| 1788 | "01" /* Compression Length : 0x01 = 1 byte for types */ |
| 1789 | "00" /* Compression Type : 0x00 = NULL compression */ |
| 1790 | }; |
| 1791 | |
| 1792 | struct tcpcheck_ruleset *rs = NULL; |
| 1793 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 1794 | struct tcpcheck_rule *chk; |
| 1795 | char *errmsg = NULL; |
| 1796 | int err_code = 0; |
| 1797 | |
| 1798 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 1799 | err_code |= ERR_WARN; |
| 1800 | |
| 1801 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 1802 | goto out; |
| 1803 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1804 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 1805 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 1806 | |
| 1807 | free_tcpcheck_vars(&rules->preset_vars); |
| 1808 | rules->list = NULL; |
| 1809 | rules->flags = 0; |
| 1810 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1811 | rs = find_tcpcheck_ruleset("*ssl-hello-check"); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1812 | if (rs) |
| 1813 | goto ruleset_found; |
| 1814 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1815 | rs = create_tcpcheck_ruleset("*ssl-hello-check"); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1816 | if (rs == NULL) { |
| 1817 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 1818 | goto error; |
| 1819 | } |
| 1820 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 1821 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", sslv3_client_hello, ""}, |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1822 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 1823 | if (!chk) { |
| 1824 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1825 | goto error; |
| 1826 | } |
| 1827 | chk->index = 0; |
| 1828 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1829 | |
| 1830 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^1[56]", |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 1831 | "min-recv", "5", "ok-status", "L6OK", |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1832 | "error-status", "L6RSP", "tout-status", "L6TOUT", |
| 1833 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1834 | 1, curpx, &rs->rules, TCPCHK_RULES_SSL3_CHK, file, line, &errmsg); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1835 | if (!chk) { |
| 1836 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1837 | goto error; |
| 1838 | } |
| 1839 | chk->index = 1; |
| 1840 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1841 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1842 | ruleset_found: |
| 1843 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 1844 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1845 | rules->flags |= TCPCHK_RULES_SSL3_CHK; |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1846 | |
| 1847 | out: |
| 1848 | free(errmsg); |
| 1849 | return err_code; |
| 1850 | |
| 1851 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1852 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1853 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1854 | goto out; |
| 1855 | } |
| 1856 | |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1857 | /* Parses the "option smtpchk" proxy keyword */ |
| 1858 | int proxy_parse_smtpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 1859 | const char *file, int line) |
| 1860 | { |
| 1861 | static char *smtp_req = "%[var(check.smtp_cmd)]\r\n"; |
| 1862 | |
| 1863 | struct tcpcheck_ruleset *rs = NULL; |
| 1864 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 1865 | struct tcpcheck_rule *chk; |
| 1866 | struct tcpcheck_var *var = NULL; |
| 1867 | char *cmd = NULL, *errmsg = NULL; |
| 1868 | int err_code = 0; |
| 1869 | |
| 1870 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 1871 | err_code |= ERR_WARN; |
| 1872 | |
| 1873 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 1874 | goto out; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1875 | |
| 1876 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 1877 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 1878 | |
| 1879 | free_tcpcheck_vars(&rules->preset_vars); |
| 1880 | rules->list = NULL; |
| 1881 | rules->flags = 0; |
| 1882 | |
| 1883 | cur_arg += 2; |
| 1884 | if (*args[cur_arg] && *args[cur_arg+1] && |
| 1885 | (strcmp(args[cur_arg], "EHLO") == 0 || strcmp(args[cur_arg], "HELO") == 0)) { |
Tim Duesterhus | 2867b40 | 2020-06-12 15:58:48 +0200 | [diff] [blame] | 1886 | /* <EHLO|HELO> + space (1) + <host> + null byte (1) */ |
| 1887 | cmd = calloc(strlen(args[cur_arg]) + 1 + strlen(args[cur_arg+1]) + 1, sizeof(*cmd)); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1888 | if (cmd) |
| 1889 | sprintf(cmd, "%s %s", args[cur_arg], args[cur_arg+1]); |
| 1890 | } |
| 1891 | else { |
| 1892 | /* this just hits the default for now, but you could potentially expand it to allow for other stuff |
| 1893 | though, it's unlikely you'd want to send anything other than an EHLO or HELO */ |
| 1894 | cmd = strdup("HELO localhost"); |
| 1895 | } |
| 1896 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 1897 | var = create_tcpcheck_var(ist("check.smtp_cmd")); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1898 | if (cmd == NULL || var == NULL) { |
| 1899 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 1900 | goto error; |
| 1901 | } |
| 1902 | var->data.type = SMP_T_STR; |
| 1903 | var->data.u.str.area = cmd; |
| 1904 | var->data.u.str.data = strlen(cmd); |
| 1905 | LIST_INIT(&var->list); |
| 1906 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 1907 | cmd = NULL; |
| 1908 | var = NULL; |
| 1909 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1910 | rs = find_tcpcheck_ruleset("*smtp-check"); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1911 | if (rs) |
| 1912 | goto ruleset_found; |
| 1913 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1914 | rs = create_tcpcheck_ruleset("*smtp-check"); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1915 | if (rs == NULL) { |
| 1916 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 1917 | goto error; |
| 1918 | } |
| 1919 | |
| 1920 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 1921 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 1922 | if (!chk) { |
| 1923 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1924 | goto error; |
| 1925 | } |
| 1926 | chk->index = 0; |
| 1927 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1928 | |
| 1929 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^[0-9]{3}[ \r]", |
| 1930 | "min-recv", "4", |
| 1931 | "error-status", "L7RSP", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 1932 | "on-error", "%[res.payload(0,0),cut_crlf]", |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1933 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1934 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1935 | if (!chk) { |
| 1936 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1937 | goto error; |
| 1938 | } |
| 1939 | chk->index = 1; |
| 1940 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1941 | |
| 1942 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[ \r]", |
| 1943 | "min-recv", "4", |
| 1944 | "error-status", "L7STS", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 1945 | "on-error", "%[res.payload(4,0),ltrim(' '),cut_crlf]", |
| 1946 | "status-code", "res.payload(0,3)", |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1947 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1948 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1949 | if (!chk) { |
| 1950 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1951 | goto error; |
| 1952 | } |
| 1953 | chk->index = 2; |
| 1954 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1955 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 1956 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", smtp_req, ""}, |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1957 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 1958 | if (!chk) { |
| 1959 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1960 | goto error; |
| 1961 | } |
| 1962 | chk->index = 3; |
| 1963 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1964 | |
| 1965 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[- \r]", |
| 1966 | "min-recv", "4", |
| 1967 | "error-status", "L7STS", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 1968 | "on-error", "%[res.payload(4,0),ltrim(' '),cut_crlf]", |
| 1969 | "on-success", "%[res.payload(4,0),ltrim(' '),cut_crlf]", |
| 1970 | "status-code", "res.payload(0,3)", |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1971 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1972 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1973 | if (!chk) { |
| 1974 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 1975 | goto error; |
| 1976 | } |
| 1977 | chk->index = 4; |
| 1978 | LIST_ADDQ(&rs->rules, &chk->list); |
| 1979 | |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1980 | ruleset_found: |
| 1981 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 1982 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1983 | rules->flags |= TCPCHK_RULES_SMTP_CHK; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1984 | |
| 1985 | out: |
| 1986 | free(errmsg); |
| 1987 | return err_code; |
| 1988 | |
| 1989 | error: |
| 1990 | free(cmd); |
| 1991 | free(var); |
| 1992 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1993 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 1994 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1995 | goto out; |
| 1996 | } |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 1997 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 1998 | /* Parses the "option pgsql-check" proxy keyword */ |
| 1999 | int proxy_parse_pgsql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 2000 | const char *file, int line) |
| 2001 | { |
| 2002 | static char pgsql_req[] = { |
| 2003 | "%[var(check.plen),htonl,hex]" /* The packet length*/ |
| 2004 | "00030000" /* the version 3.0 */ |
| 2005 | "7573657200" /* "user" key */ |
| 2006 | "%[var(check.username),hex]00" /* the username */ |
| 2007 | "00" |
| 2008 | }; |
| 2009 | |
| 2010 | struct tcpcheck_ruleset *rs = NULL; |
| 2011 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 2012 | struct tcpcheck_rule *chk; |
| 2013 | struct tcpcheck_var *var = NULL; |
| 2014 | char *user = NULL, *errmsg = NULL; |
| 2015 | size_t packetlen = 0; |
| 2016 | int err_code = 0; |
| 2017 | |
| 2018 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 2019 | err_code |= ERR_WARN; |
| 2020 | |
| 2021 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 2022 | goto out; |
| 2023 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2024 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 2025 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 2026 | |
| 2027 | free_tcpcheck_vars(&rules->preset_vars); |
| 2028 | rules->list = NULL; |
| 2029 | rules->flags = 0; |
| 2030 | |
| 2031 | cur_arg += 2; |
| 2032 | if (!*args[cur_arg] || !*args[cur_arg+1]) { |
| 2033 | ha_alert("parsing [%s:%d] : '%s %s' expects 'user <username>' as argument.\n", |
| 2034 | file, line, args[0], args[1]); |
| 2035 | goto error; |
| 2036 | } |
| 2037 | if (strcmp(args[cur_arg], "user") == 0) { |
| 2038 | packetlen = 15 + strlen(args[cur_arg+1]); |
| 2039 | user = strdup(args[cur_arg+1]); |
| 2040 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2041 | var = create_tcpcheck_var(ist("check.username")); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2042 | if (user == NULL || var == NULL) { |
| 2043 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2044 | goto error; |
| 2045 | } |
| 2046 | var->data.type = SMP_T_STR; |
| 2047 | var->data.u.str.area = user; |
| 2048 | var->data.u.str.data = strlen(user); |
| 2049 | LIST_INIT(&var->list); |
| 2050 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 2051 | user = NULL; |
| 2052 | var = NULL; |
| 2053 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2054 | var = create_tcpcheck_var(ist("check.plen")); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2055 | if (var == NULL) { |
| 2056 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2057 | goto error; |
| 2058 | } |
| 2059 | var->data.type = SMP_T_SINT; |
| 2060 | var->data.u.sint = packetlen; |
| 2061 | LIST_INIT(&var->list); |
| 2062 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 2063 | var = NULL; |
| 2064 | } |
| 2065 | else { |
| 2066 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user'.\n", |
| 2067 | file, line, args[0], args[1]); |
| 2068 | goto error; |
| 2069 | } |
| 2070 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2071 | rs = find_tcpcheck_ruleset("*pgsql-check"); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2072 | if (rs) |
| 2073 | goto ruleset_found; |
| 2074 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2075 | rs = create_tcpcheck_ruleset("*pgsql-check"); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2076 | if (rs == NULL) { |
| 2077 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2078 | goto error; |
| 2079 | } |
| 2080 | |
| 2081 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 2082 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 2083 | if (!chk) { |
| 2084 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2085 | goto error; |
| 2086 | } |
| 2087 | chk->index = 0; |
| 2088 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2089 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 2090 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", pgsql_req, ""}, |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2091 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 2092 | if (!chk) { |
| 2093 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2094 | goto error; |
| 2095 | } |
| 2096 | chk->index = 1; |
| 2097 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2098 | |
| 2099 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "!rstring", "^E", |
| 2100 | "min-recv", "5", |
| 2101 | "error-status", "L7RSP", |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 2102 | "on-error", "%[res.payload(6,0)]", |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2103 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2104 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2105 | if (!chk) { |
| 2106 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2107 | goto error; |
| 2108 | } |
| 2109 | chk->index = 2; |
| 2110 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2111 | |
Christopher Faulet | b841c74 | 2020-04-27 18:29:49 +0200 | [diff] [blame] | 2112 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^52000000(08|0A|0C)000000(00|02|03|04|05|06)", |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2113 | "min-recv", "9", |
| 2114 | "error-status", "L7STS", |
| 2115 | "on-success", "PostgreSQL server is ok", |
| 2116 | "on-error", "PostgreSQL unknown error", |
| 2117 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2118 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2119 | if (!chk) { |
| 2120 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2121 | goto error; |
| 2122 | } |
| 2123 | chk->index = 3; |
| 2124 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2125 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2126 | ruleset_found: |
| 2127 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 2128 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 2129 | rules->flags |= TCPCHK_RULES_PGSQL_CHK; |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2130 | |
| 2131 | out: |
| 2132 | free(errmsg); |
| 2133 | return err_code; |
| 2134 | |
| 2135 | error: |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2136 | free(user); |
| 2137 | free(var); |
| 2138 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2139 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2140 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2141 | goto out; |
| 2142 | } |
| 2143 | |
| 2144 | |
| 2145 | /* Parses the "option mysql-check" proxy keyword */ |
| 2146 | int proxy_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 2147 | const char *file, int line) |
| 2148 | { |
| 2149 | /* This is an example of a MySQL >=4.0 client Authentication packet kindly provided by Cyril Bonte. |
| 2150 | * const char mysql40_client_auth_pkt[] = { |
| 2151 | * "\x0e\x00\x00" // packet length |
| 2152 | * "\x01" // packet number |
| 2153 | * "\x00\x00" // client capabilities |
| 2154 | * "\x00\x00\x01" // max packet |
| 2155 | * "haproxy\x00" // username (null terminated string) |
| 2156 | * "\x00" // filler (always 0x00) |
| 2157 | * "\x01\x00\x00" // packet length |
| 2158 | * "\x00" // packet number |
| 2159 | * "\x01" // COM_QUIT command |
| 2160 | * }; |
| 2161 | */ |
| 2162 | static char mysql40_rsname[] = "*mysql40-check"; |
| 2163 | static char mysql40_req[] = { |
| 2164 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 2165 | "0080" /* client capabilities */ |
| 2166 | "000001" /* max packet */ |
| 2167 | "%[var(check.username),hex]00" /* the username */ |
| 2168 | "00" /* filler (always 0x00) */ |
| 2169 | "010000" /* packet length*/ |
| 2170 | "00" /* sequence ID */ |
| 2171 | "01" /* COM_QUIT command */ |
| 2172 | }; |
| 2173 | |
| 2174 | /* This is an example of a MySQL >=4.1 client Authentication packet provided by Nenad Merdanovic. |
| 2175 | * const char mysql41_client_auth_pkt[] = { |
| 2176 | * "\x0e\x00\x00\" // packet length |
| 2177 | * "\x01" // packet number |
| 2178 | * "\x00\x00\x00\x00" // client capabilities |
| 2179 | * "\x00\x00\x00\x01" // max packet |
| 2180 | * "\x21" // character set (UTF-8) |
| 2181 | * char[23] // All zeroes |
| 2182 | * "haproxy\x00" // username (null terminated string) |
| 2183 | * "\x00" // filler (always 0x00) |
| 2184 | * "\x01\x00\x00" // packet length |
| 2185 | * "\x00" // packet number |
| 2186 | * "\x01" // COM_QUIT command |
| 2187 | * }; |
| 2188 | */ |
| 2189 | static char mysql41_rsname[] = "*mysql41-check"; |
| 2190 | static char mysql41_req[] = { |
| 2191 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 2192 | "00820000" /* client capabilities */ |
| 2193 | "00800001" /* max packet */ |
| 2194 | "21" /* character set (UTF-8) */ |
| 2195 | "000000000000000000000000" /* 23 bytes, al zeroes */ |
| 2196 | "0000000000000000000000" |
| 2197 | "%[var(check.username),hex]00" /* the username */ |
| 2198 | "00" /* filler (always 0x00) */ |
| 2199 | "010000" /* packet length*/ |
| 2200 | "00" /* sequence ID */ |
| 2201 | "01" /* COM_QUIT command */ |
| 2202 | }; |
| 2203 | |
| 2204 | struct tcpcheck_ruleset *rs = NULL; |
| 2205 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 2206 | struct tcpcheck_rule *chk; |
| 2207 | struct tcpcheck_var *var = NULL; |
| 2208 | char *mysql_rsname = "*mysql-check"; |
| 2209 | char *mysql_req = NULL, *hdr = NULL, *user = NULL, *errmsg = NULL; |
| 2210 | int index = 0, err_code = 0; |
| 2211 | |
| 2212 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 2213 | err_code |= ERR_WARN; |
| 2214 | |
| 2215 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 2216 | goto out; |
| 2217 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2218 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 2219 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 2220 | |
| 2221 | free_tcpcheck_vars(&rules->preset_vars); |
| 2222 | rules->list = NULL; |
| 2223 | rules->flags = 0; |
| 2224 | |
| 2225 | cur_arg += 2; |
| 2226 | if (*args[cur_arg]) { |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2227 | int packetlen, userlen; |
| 2228 | |
| 2229 | if (strcmp(args[cur_arg], "user") != 0) { |
| 2230 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user' (got '%s').\n", |
| 2231 | file, line, args[0], args[1], args[cur_arg]); |
| 2232 | goto error; |
| 2233 | } |
| 2234 | |
| 2235 | if (*(args[cur_arg+1]) == 0) { |
| 2236 | ha_alert("parsing [%s:%d] : '%s %s %s' expects <username> as argument.\n", |
| 2237 | file, line, args[0], args[1], args[cur_arg]); |
| 2238 | goto error; |
| 2239 | } |
| 2240 | |
| 2241 | hdr = calloc(4, sizeof(*hdr)); |
| 2242 | user = strdup(args[cur_arg+1]); |
| 2243 | userlen = strlen(args[cur_arg+1]); |
| 2244 | |
| 2245 | if (hdr == NULL || user == NULL) { |
| 2246 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2247 | goto error; |
| 2248 | } |
| 2249 | |
Christopher Faulet | 62f79fe | 2020-05-18 18:13:03 +0200 | [diff] [blame] | 2250 | if (!*args[cur_arg+2] || strcmp(args[cur_arg+2], "post-41") == 0) { |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2251 | packetlen = userlen + 7 + 27; |
| 2252 | mysql_req = mysql41_req; |
| 2253 | mysql_rsname = mysql41_rsname; |
| 2254 | } |
Christopher Faulet | 62f79fe | 2020-05-18 18:13:03 +0200 | [diff] [blame] | 2255 | else if (strcmp(args[cur_arg+2], "pre-41") == 0) { |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2256 | packetlen = userlen + 7; |
| 2257 | mysql_req = mysql40_req; |
| 2258 | mysql_rsname = mysql40_rsname; |
| 2259 | } |
Christopher Faulet | 62f79fe | 2020-05-18 18:13:03 +0200 | [diff] [blame] | 2260 | else { |
| 2261 | ha_alert("parsing [%s:%d] : keyword '%s' only supports 'post-41' and 'pre-41' (got '%s').\n", |
| 2262 | file, line, args[cur_arg], args[cur_arg+2]); |
| 2263 | goto error; |
| 2264 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2265 | |
| 2266 | hdr[0] = (unsigned char)(packetlen & 0xff); |
| 2267 | hdr[1] = (unsigned char)((packetlen >> 8) & 0xff); |
| 2268 | hdr[2] = (unsigned char)((packetlen >> 16) & 0xff); |
| 2269 | hdr[3] = 1; |
| 2270 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2271 | var = create_tcpcheck_var(ist("check.header")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2272 | if (var == NULL) { |
| 2273 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2274 | goto error; |
| 2275 | } |
| 2276 | var->data.type = SMP_T_STR; |
| 2277 | var->data.u.str.area = hdr; |
| 2278 | var->data.u.str.data = 4; |
| 2279 | LIST_INIT(&var->list); |
| 2280 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 2281 | hdr = NULL; |
| 2282 | var = NULL; |
| 2283 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2284 | var = create_tcpcheck_var(ist("check.username")); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2285 | if (var == NULL) { |
| 2286 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2287 | goto error; |
| 2288 | } |
| 2289 | var->data.type = SMP_T_STR; |
| 2290 | var->data.u.str.area = user; |
| 2291 | var->data.u.str.data = strlen(user); |
| 2292 | LIST_INIT(&var->list); |
| 2293 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 2294 | user = NULL; |
| 2295 | var = NULL; |
| 2296 | } |
| 2297 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2298 | rs = find_tcpcheck_ruleset(mysql_rsname); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2299 | if (rs) |
| 2300 | goto ruleset_found; |
| 2301 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2302 | rs = create_tcpcheck_ruleset(mysql_rsname); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2303 | if (rs == NULL) { |
| 2304 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2305 | goto error; |
| 2306 | } |
| 2307 | |
| 2308 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 2309 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 2310 | if (!chk) { |
| 2311 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2312 | goto error; |
| 2313 | } |
| 2314 | chk->index = index++; |
| 2315 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2316 | |
| 2317 | if (mysql_req) { |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 2318 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", mysql_req, ""}, |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2319 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 2320 | if (!chk) { |
| 2321 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2322 | goto error; |
| 2323 | } |
| 2324 | chk->index = index++; |
| 2325 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2326 | } |
| 2327 | |
| 2328 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2329 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2330 | if (!chk) { |
| 2331 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2332 | goto error; |
| 2333 | } |
| 2334 | chk->expect.custom = tcpcheck_mysql_expect_iniths; |
| 2335 | chk->index = index++; |
| 2336 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2337 | |
| 2338 | if (mysql_req) { |
| 2339 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2340 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2341 | if (!chk) { |
| 2342 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2343 | goto error; |
| 2344 | } |
| 2345 | chk->expect.custom = tcpcheck_mysql_expect_ok; |
| 2346 | chk->index = index++; |
| 2347 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2348 | } |
| 2349 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2350 | ruleset_found: |
| 2351 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 2352 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 2353 | rules->flags |= TCPCHK_RULES_MYSQL_CHK; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2354 | |
| 2355 | out: |
| 2356 | free(errmsg); |
| 2357 | return err_code; |
| 2358 | |
| 2359 | error: |
| 2360 | free(hdr); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2361 | free(user); |
| 2362 | free(var); |
| 2363 | free_tcpcheck_vars(&rules->preset_vars); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2364 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2365 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2366 | goto out; |
| 2367 | } |
| 2368 | |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2369 | int proxy_parse_ldap_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 2370 | const char *file, int line) |
| 2371 | { |
| 2372 | static char *ldap_req = "300C020101600702010304008000"; |
| 2373 | |
| 2374 | struct tcpcheck_ruleset *rs = NULL; |
| 2375 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 2376 | struct tcpcheck_rule *chk; |
| 2377 | char *errmsg = NULL; |
| 2378 | int err_code = 0; |
| 2379 | |
| 2380 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 2381 | err_code |= ERR_WARN; |
| 2382 | |
| 2383 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 2384 | goto out; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2385 | |
| 2386 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 2387 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 2388 | |
| 2389 | free_tcpcheck_vars(&rules->preset_vars); |
| 2390 | rules->list = NULL; |
| 2391 | rules->flags = 0; |
| 2392 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2393 | rs = find_tcpcheck_ruleset("*ldap-check"); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2394 | if (rs) |
| 2395 | goto ruleset_found; |
| 2396 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2397 | rs = create_tcpcheck_ruleset("*ldap-check"); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2398 | if (rs == NULL) { |
| 2399 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2400 | goto error; |
| 2401 | } |
| 2402 | |
| 2403 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", ldap_req, ""}, |
| 2404 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 2405 | if (!chk) { |
| 2406 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2407 | goto error; |
| 2408 | } |
| 2409 | chk->index = 0; |
| 2410 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2411 | |
| 2412 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^30", |
| 2413 | "min-recv", "14", |
| 2414 | "on-error", "Not LDAPv3 protocol", |
| 2415 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2416 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2417 | if (!chk) { |
| 2418 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2419 | goto error; |
| 2420 | } |
| 2421 | chk->index = 1; |
| 2422 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2423 | |
| 2424 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2425 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2426 | if (!chk) { |
| 2427 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2428 | goto error; |
| 2429 | } |
| 2430 | chk->expect.custom = tcpcheck_ldap_expect_bindrsp; |
| 2431 | chk->index = 2; |
| 2432 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2433 | |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2434 | ruleset_found: |
| 2435 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 2436 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 2437 | rules->flags |= TCPCHK_RULES_LDAP_CHK; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2438 | |
| 2439 | out: |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2440 | free(errmsg); |
| 2441 | return err_code; |
| 2442 | |
| 2443 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2444 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2445 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2446 | goto out; |
| 2447 | } |
| 2448 | |
| 2449 | int proxy_parse_spop_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 2450 | const char *file, int line) |
| 2451 | { |
| 2452 | struct tcpcheck_ruleset *rs = NULL; |
| 2453 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 2454 | struct tcpcheck_rule *chk; |
| 2455 | char *spop_req = NULL; |
| 2456 | char *errmsg = NULL; |
| 2457 | int spop_len = 0, err_code = 0; |
| 2458 | |
| 2459 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 2460 | err_code |= ERR_WARN; |
| 2461 | |
| 2462 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 2463 | goto out; |
| 2464 | |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2465 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 2466 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 2467 | |
| 2468 | free_tcpcheck_vars(&rules->preset_vars); |
| 2469 | rules->list = NULL; |
| 2470 | rules->flags = 0; |
| 2471 | |
| 2472 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2473 | rs = find_tcpcheck_ruleset("*spop-check"); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2474 | if (rs) |
| 2475 | goto ruleset_found; |
| 2476 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2477 | rs = create_tcpcheck_ruleset("*spop-check"); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2478 | if (rs == NULL) { |
| 2479 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2480 | goto error; |
| 2481 | } |
| 2482 | |
| 2483 | if (spoe_prepare_healthcheck_request(&spop_req, &spop_len) == -1) { |
| 2484 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2485 | goto error; |
| 2486 | } |
| 2487 | chunk_reset(&trash); |
| 2488 | dump_binary(&trash, spop_req, spop_len); |
| 2489 | trash.area[trash.data] = '\0'; |
| 2490 | |
| 2491 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", b_head(&trash), ""}, |
| 2492 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 2493 | if (!chk) { |
| 2494 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2495 | goto error; |
| 2496 | } |
| 2497 | chk->index = 0; |
| 2498 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2499 | |
| 2500 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", "min-recv", "4", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2501 | 1, curpx, &rs->rules, TCPCHK_RULES_SPOP_CHK, file, line, &errmsg); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2502 | if (!chk) { |
| 2503 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 2504 | goto error; |
| 2505 | } |
| 2506 | chk->expect.custom = tcpcheck_spop_expect_agenthello; |
| 2507 | chk->index = 1; |
| 2508 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2509 | |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2510 | ruleset_found: |
| 2511 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 2512 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 2513 | rules->flags |= TCPCHK_RULES_SPOP_CHK; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2514 | |
| 2515 | out: |
| 2516 | free(spop_req); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2517 | free(errmsg); |
| 2518 | return err_code; |
| 2519 | |
| 2520 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2521 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2522 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2523 | goto out; |
| 2524 | } |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 2525 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2526 | |
| 2527 | struct tcpcheck_rule *proxy_parse_httpchk_req(char **args, int cur_arg, struct proxy *px, char **errmsg) |
| 2528 | { |
| 2529 | struct tcpcheck_rule *chk = NULL; |
| 2530 | struct tcpcheck_http_hdr *hdr = NULL; |
| 2531 | char *meth = NULL, *uri = NULL, *vsn = NULL; |
| 2532 | char *hdrs, *body; |
| 2533 | |
| 2534 | hdrs = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n") : NULL); |
| 2535 | body = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n\r\n") : NULL); |
| 2536 | if (hdrs == body) |
| 2537 | hdrs = NULL; |
| 2538 | if (hdrs) { |
| 2539 | *hdrs = '\0'; |
| 2540 | hdrs +=2; |
| 2541 | } |
| 2542 | if (body) { |
| 2543 | *body = '\0'; |
| 2544 | body += 4; |
| 2545 | } |
| 2546 | if (hdrs || body) { |
| 2547 | memprintf(errmsg, "hiding headers or body at the end of the version string is deprecated." |
| 2548 | " Please, consider to use 'http-check send' directive instead."); |
| 2549 | } |
| 2550 | |
| 2551 | chk = calloc(1, sizeof(*chk)); |
| 2552 | if (!chk) { |
| 2553 | memprintf(errmsg, "out of memory"); |
| 2554 | goto error; |
| 2555 | } |
| 2556 | chk->action = TCPCHK_ACT_SEND; |
| 2557 | chk->send.type = TCPCHK_SEND_HTTP; |
| 2558 | chk->send.http.flags |= TCPCHK_SND_HTTP_FROM_OPT; |
| 2559 | chk->send.http.meth.meth = HTTP_METH_OPTIONS; |
| 2560 | LIST_INIT(&chk->send.http.hdrs); |
| 2561 | |
| 2562 | /* Copy the method, uri and version */ |
| 2563 | if (*args[cur_arg]) { |
| 2564 | if (!*args[cur_arg+1]) |
| 2565 | uri = args[cur_arg]; |
| 2566 | else |
| 2567 | meth = args[cur_arg]; |
| 2568 | } |
| 2569 | if (*args[cur_arg+1]) |
| 2570 | uri = args[cur_arg+1]; |
| 2571 | if (*args[cur_arg+2]) |
| 2572 | vsn = args[cur_arg+2]; |
| 2573 | |
| 2574 | if (meth) { |
| 2575 | chk->send.http.meth.meth = find_http_meth(meth, strlen(meth)); |
| 2576 | chk->send.http.meth.str.area = strdup(meth); |
| 2577 | chk->send.http.meth.str.data = strlen(meth); |
| 2578 | if (!chk->send.http.meth.str.area) { |
| 2579 | memprintf(errmsg, "out of memory"); |
| 2580 | goto error; |
| 2581 | } |
| 2582 | } |
| 2583 | if (uri) { |
| 2584 | chk->send.http.uri = ist2(strdup(uri), strlen(uri)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2585 | if (!isttest(chk->send.http.uri)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2586 | memprintf(errmsg, "out of memory"); |
| 2587 | goto error; |
| 2588 | } |
| 2589 | } |
| 2590 | if (vsn) { |
| 2591 | chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2592 | if (!isttest(chk->send.http.vsn)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2593 | memprintf(errmsg, "out of memory"); |
| 2594 | goto error; |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | /* Copy the header */ |
| 2599 | if (hdrs) { |
| 2600 | struct http_hdr tmp_hdrs[global.tune.max_http_hdr]; |
| 2601 | struct h1m h1m; |
| 2602 | int i, ret; |
| 2603 | |
| 2604 | /* Build and parse the request */ |
| 2605 | chunk_printf(&trash, "%s\r\n\r\n", hdrs); |
| 2606 | |
| 2607 | h1m.flags = H1_MF_HDRS_ONLY; |
| 2608 | ret = h1_headers_to_hdr_list(b_orig(&trash), b_tail(&trash), |
| 2609 | tmp_hdrs, sizeof(tmp_hdrs)/sizeof(tmp_hdrs[0]), |
| 2610 | &h1m, NULL); |
| 2611 | if (ret <= 0) { |
| 2612 | memprintf(errmsg, "unable to parse the request '%s'.", b_orig(&trash)); |
| 2613 | goto error; |
| 2614 | } |
| 2615 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2616 | for (i = 0; istlen(tmp_hdrs[i].n); i++) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2617 | hdr = calloc(1, sizeof(*hdr)); |
| 2618 | if (!hdr) { |
| 2619 | memprintf(errmsg, "out of memory"); |
| 2620 | goto error; |
| 2621 | } |
| 2622 | LIST_INIT(&hdr->value); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2623 | hdr->name = istdup(tmp_hdrs[i].n); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2624 | if (!hdr->name.ptr) { |
| 2625 | memprintf(errmsg, "out of memory"); |
| 2626 | goto error; |
| 2627 | } |
| 2628 | |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2629 | ist0(tmp_hdrs[i].v); |
| 2630 | if (!parse_logformat_string(istptr(tmp_hdrs[i].v), px, &hdr->value, 0, SMP_VAL_BE_CHK_RUL, errmsg)) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2631 | goto error; |
| 2632 | LIST_ADDQ(&chk->send.http.hdrs, &hdr->list); |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | /* Copy the body */ |
| 2637 | if (body) { |
| 2638 | chk->send.http.body = ist2(strdup(body), strlen(body)); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2639 | if (!isttest(chk->send.http.body)) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2640 | memprintf(errmsg, "out of memory"); |
| 2641 | goto error; |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | return chk; |
| 2646 | |
| 2647 | error: |
| 2648 | free_tcpcheck_http_hdr(hdr); |
| 2649 | free_tcpcheck(chk, 0); |
| 2650 | return NULL; |
| 2651 | } |
| 2652 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2653 | int proxy_parse_httpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 2654 | const char *file, int line) |
| 2655 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2656 | struct tcpcheck_ruleset *rs = NULL; |
| 2657 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 2658 | struct tcpcheck_rule *chk; |
| 2659 | char *errmsg = NULL; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2660 | int err_code = 0; |
| 2661 | |
| 2662 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 2663 | err_code |= ERR_WARN; |
| 2664 | |
| 2665 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 2666 | goto out; |
| 2667 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2668 | chk = proxy_parse_httpchk_req(args, cur_arg+2, curpx, &errmsg); |
| 2669 | if (!chk) { |
| 2670 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 2671 | goto error; |
| 2672 | } |
| 2673 | if (errmsg) { |
| 2674 | ha_warning("parsing [%s:%d]: '%s %s' : %s\n", file, line, args[0], args[1], errmsg); |
| 2675 | err_code |= ERR_WARN; |
| 2676 | free(errmsg); |
| 2677 | errmsg = NULL; |
| 2678 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2679 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2680 | no_request: |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2681 | curpx->options2 &= ~PR_O2_CHK_ANY; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2682 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2683 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2684 | free_tcpcheck_vars(&rules->preset_vars); |
| 2685 | rules->list = NULL; |
| 2686 | rules->flags |= TCPCHK_SND_HTTP_FROM_OPT; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2687 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2688 | /* Deduce the ruleset name from the proxy info */ |
| 2689 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 2690 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 2691 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2692 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2693 | rs = find_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2694 | if (rs == NULL) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2695 | rs = create_tcpcheck_ruleset(b_orig(&trash)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2696 | if (rs == NULL) { |
| 2697 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 2698 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2699 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2700 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2701 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2702 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 2703 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2704 | rules->flags |= TCPCHK_RULES_HTTP_CHK; |
| 2705 | if (!tcpcheck_add_http_rule(chk, rules, &errmsg)) { |
| 2706 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 2707 | rules->list = NULL; |
| 2708 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2709 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2710 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2711 | out: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2712 | free(errmsg); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2713 | return err_code; |
| 2714 | |
| 2715 | error: |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2716 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2717 | free_tcpcheck(chk, 0); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 2718 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2719 | goto out; |
| 2720 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2721 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2722 | /* Parse the "addr" server keyword */ |
| 2723 | static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2724 | char **errmsg) |
| 2725 | { |
| 2726 | struct sockaddr_storage *sk; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2727 | int port1, port2, err_code = 0; |
| 2728 | |
| 2729 | |
| 2730 | if (!*args[*cur_arg+1]) { |
| 2731 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]); |
| 2732 | goto error; |
| 2733 | } |
| 2734 | |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 2735 | sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL, |
| 2736 | 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] | 2737 | if (!sk) { |
| 2738 | memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg); |
| 2739 | goto error; |
| 2740 | } |
| 2741 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2742 | srv->check.addr = srv->agent.addr = *sk; |
| 2743 | srv->flags |= SRV_F_CHECKADDR; |
| 2744 | srv->flags |= SRV_F_AGENTADDR; |
| 2745 | |
| 2746 | out: |
| 2747 | return err_code; |
| 2748 | |
| 2749 | error: |
| 2750 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2751 | goto out; |
| 2752 | } |
| 2753 | |
| 2754 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2755 | /* Parse the "agent-addr" server keyword */ |
| 2756 | static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2757 | char **errmsg) |
| 2758 | { |
| 2759 | int err_code = 0; |
| 2760 | |
| 2761 | if (!*(args[*cur_arg+1])) { |
| 2762 | memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]); |
| 2763 | goto error; |
| 2764 | } |
| 2765 | if(str2ip(args[*cur_arg+1], &srv->agent.addr) == NULL) { |
| 2766 | memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]); |
| 2767 | goto error; |
| 2768 | } |
| 2769 | |
| 2770 | out: |
| 2771 | return err_code; |
| 2772 | |
| 2773 | error: |
| 2774 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2775 | goto out; |
| 2776 | } |
| 2777 | |
| 2778 | /* Parse the "agent-check" server keyword */ |
| 2779 | static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2780 | char **errmsg) |
| 2781 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2782 | struct tcpcheck_ruleset *rs = NULL; |
| 2783 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 2784 | struct tcpcheck_rule *chk; |
| 2785 | int err_code = 0; |
| 2786 | |
| 2787 | if (srv->do_agent) |
| 2788 | goto out; |
| 2789 | |
Christopher Faulet | 0a5e713 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 2790 | if (!(curpx->cap & PR_CAP_BE)) { |
| 2791 | memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability", |
| 2792 | args[*cur_arg], proxy_type_str(curpx), curpx->id); |
| 2793 | return ERR_WARN; |
| 2794 | } |
| 2795 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2796 | if (!rules) { |
| 2797 | rules = calloc(1, sizeof(*rules)); |
| 2798 | if (!rules) { |
| 2799 | memprintf(errmsg, "out of memory."); |
| 2800 | goto error; |
| 2801 | } |
| 2802 | LIST_INIT(&rules->preset_vars); |
| 2803 | srv->agent.tcpcheck_rules = rules; |
| 2804 | } |
| 2805 | rules->list = NULL; |
| 2806 | rules->flags = 0; |
| 2807 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2808 | rs = find_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2809 | if (rs) |
| 2810 | goto ruleset_found; |
| 2811 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2812 | rs = create_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2813 | if (rs == NULL) { |
| 2814 | memprintf(errmsg, "out of memory."); |
| 2815 | goto error; |
| 2816 | } |
| 2817 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 2818 | 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] | 2819 | 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg); |
| 2820 | if (!chk) { |
| 2821 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 2822 | goto error; |
| 2823 | } |
| 2824 | chk->index = 0; |
| 2825 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2826 | |
| 2827 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2828 | 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK, |
| 2829 | srv->conf.file, srv->conf.line, errmsg); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2830 | if (!chk) { |
| 2831 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 2832 | goto error; |
| 2833 | } |
| 2834 | chk->expect.custom = tcpcheck_agent_expect_reply; |
| 2835 | chk->index = 1; |
| 2836 | LIST_ADDQ(&rs->rules, &chk->list); |
| 2837 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2838 | ruleset_found: |
| 2839 | rules->list = &rs->rules; |
Christopher Faulet | b9492cf | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 2840 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 2841 | rules->flags |= TCPCHK_RULES_AGENT_CHK; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2842 | srv->do_agent = 1; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2843 | |
| 2844 | out: |
Dirkjan Bussink | 8b37d8d | 2021-06-18 19:57:49 +0000 | [diff] [blame] | 2845 | return err_code; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2846 | |
| 2847 | error: |
| 2848 | deinit_srv_agent_check(srv); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 2849 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2850 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2851 | goto out; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | /* Parse the "agent-inter" server keyword */ |
| 2855 | static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2856 | char **errmsg) |
| 2857 | { |
| 2858 | const char *err = NULL; |
| 2859 | unsigned int delay; |
| 2860 | int err_code = 0; |
| 2861 | |
| 2862 | if (!*(args[*cur_arg+1])) { |
| 2863 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 2864 | goto error; |
| 2865 | } |
| 2866 | |
| 2867 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 2868 | if (err == PARSE_TIME_OVER) { |
| 2869 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 2870 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2871 | goto error; |
| 2872 | } |
| 2873 | else if (err == PARSE_TIME_UNDER) { |
| 2874 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 2875 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2876 | goto error; |
| 2877 | } |
| 2878 | else if (err) { |
| 2879 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 2880 | *err, srv->id); |
| 2881 | goto error; |
| 2882 | } |
| 2883 | if (delay <= 0) { |
| 2884 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 2885 | delay, args[*cur_arg], srv->id); |
| 2886 | goto error; |
| 2887 | } |
| 2888 | srv->agent.inter = delay; |
| 2889 | |
| 2890 | out: |
| 2891 | return err_code; |
| 2892 | |
| 2893 | error: |
| 2894 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2895 | goto out; |
| 2896 | } |
| 2897 | |
| 2898 | /* Parse the "agent-port" server keyword */ |
| 2899 | static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2900 | char **errmsg) |
| 2901 | { |
| 2902 | int err_code = 0; |
| 2903 | |
| 2904 | if (!*(args[*cur_arg+1])) { |
| 2905 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 2906 | goto error; |
| 2907 | } |
| 2908 | |
| 2909 | global.maxsock++; |
| 2910 | srv->agent.port = atol(args[*cur_arg+1]); |
| 2911 | |
| 2912 | out: |
| 2913 | return err_code; |
| 2914 | |
| 2915 | error: |
| 2916 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2917 | goto out; |
| 2918 | } |
| 2919 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2920 | int set_srv_agent_send(struct server *srv, const char *send) |
| 2921 | { |
| 2922 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 2923 | struct tcpcheck_var *var = NULL; |
| 2924 | char *str; |
| 2925 | |
| 2926 | str = strdup(send); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2927 | var = create_tcpcheck_var(ist("check.agent_string")); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2928 | if (str == NULL || var == NULL) |
| 2929 | goto error; |
| 2930 | |
| 2931 | free_tcpcheck_vars(&rules->preset_vars); |
| 2932 | |
| 2933 | var->data.type = SMP_T_STR; |
| 2934 | var->data.u.str.area = str; |
| 2935 | var->data.u.str.data = strlen(str); |
| 2936 | LIST_INIT(&var->list); |
| 2937 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 2938 | |
| 2939 | return 1; |
| 2940 | |
| 2941 | error: |
| 2942 | free(str); |
| 2943 | free(var); |
| 2944 | return 0; |
| 2945 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2946 | |
| 2947 | /* Parse the "agent-send" server keyword */ |
| 2948 | static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2949 | char **errmsg) |
| 2950 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2951 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2952 | int err_code = 0; |
| 2953 | |
| 2954 | if (!*(args[*cur_arg+1])) { |
| 2955 | memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]); |
| 2956 | goto error; |
| 2957 | } |
| 2958 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2959 | if (!rules) { |
| 2960 | rules = calloc(1, sizeof(*rules)); |
| 2961 | if (!rules) { |
| 2962 | memprintf(errmsg, "out of memory."); |
| 2963 | goto error; |
| 2964 | } |
| 2965 | LIST_INIT(&rules->preset_vars); |
| 2966 | srv->agent.tcpcheck_rules = rules; |
| 2967 | } |
| 2968 | |
| 2969 | if (!set_srv_agent_send(srv, args[*cur_arg+1])) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2970 | memprintf(errmsg, "out of memory."); |
| 2971 | goto error; |
| 2972 | } |
| 2973 | |
| 2974 | out: |
| 2975 | return err_code; |
| 2976 | |
| 2977 | error: |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2978 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2979 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2980 | goto out; |
| 2981 | } |
| 2982 | |
| 2983 | /* Parse the "no-agent-send" server keyword */ |
| 2984 | static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2985 | char **errmsg) |
| 2986 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2987 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2988 | return 0; |
| 2989 | } |
| 2990 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2991 | /* Parse the "check" server keyword */ |
| 2992 | static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2993 | char **errmsg) |
| 2994 | { |
Christopher Faulet | 0a5e713 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 2995 | if (!(curpx->cap & PR_CAP_BE)) { |
| 2996 | memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability", |
| 2997 | args[*cur_arg], proxy_type_str(curpx), curpx->id); |
| 2998 | return ERR_WARN; |
| 2999 | } |
| 3000 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 3001 | srv->do_check = 1; |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
| 3005 | /* Parse the "check-send-proxy" server keyword */ |
| 3006 | static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3007 | char **errmsg) |
| 3008 | { |
| 3009 | srv->check.send_proxy = 1; |
| 3010 | return 0; |
| 3011 | } |
| 3012 | |
| 3013 | /* Parse the "check-via-socks4" server keyword */ |
| 3014 | static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3015 | char **errmsg) |
| 3016 | { |
| 3017 | srv->check.via_socks4 = 1; |
| 3018 | return 0; |
| 3019 | } |
| 3020 | |
| 3021 | /* Parse the "no-check" server keyword */ |
| 3022 | static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3023 | char **errmsg) |
| 3024 | { |
| 3025 | deinit_srv_check(srv); |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | /* Parse the "no-check-send-proxy" server keyword */ |
| 3030 | static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3031 | char **errmsg) |
| 3032 | { |
| 3033 | srv->check.send_proxy = 0; |
| 3034 | return 0; |
| 3035 | } |
| 3036 | |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 3037 | /* parse the "check-proto" server keyword */ |
| 3038 | static int srv_parse_check_proto(char **args, int *cur_arg, |
| 3039 | struct proxy *px, struct server *newsrv, char **err) |
| 3040 | { |
| 3041 | int err_code = 0; |
| 3042 | |
| 3043 | if (!*args[*cur_arg + 1]) { |
| 3044 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 3045 | goto error; |
| 3046 | } |
| 3047 | newsrv->check.mux_proto = get_mux_proto(ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]))); |
| 3048 | if (!newsrv->check.mux_proto) { |
| 3049 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 3050 | goto error; |
| 3051 | } |
| 3052 | |
| 3053 | out: |
| 3054 | return err_code; |
| 3055 | |
| 3056 | error: |
| 3057 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3058 | goto out; |
| 3059 | } |
| 3060 | |
| 3061 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 3062 | /* Parse the "rise" server keyword */ |
| 3063 | static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3064 | char **errmsg) |
| 3065 | { |
| 3066 | int err_code = 0; |
| 3067 | |
| 3068 | if (!*args[*cur_arg + 1]) { |
| 3069 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 3070 | goto error; |
| 3071 | } |
| 3072 | |
| 3073 | srv->check.rise = atol(args[*cur_arg+1]); |
| 3074 | if (srv->check.rise <= 0) { |
| 3075 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 3076 | goto error; |
| 3077 | } |
| 3078 | |
| 3079 | if (srv->check.health) |
| 3080 | srv->check.health = srv->check.rise; |
| 3081 | |
| 3082 | out: |
| 3083 | return err_code; |
| 3084 | |
| 3085 | error: |
| 3086 | deinit_srv_agent_check(srv); |
| 3087 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3088 | goto out; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | /* Parse the "fall" server keyword */ |
| 3092 | static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3093 | char **errmsg) |
| 3094 | { |
| 3095 | int err_code = 0; |
| 3096 | |
| 3097 | if (!*args[*cur_arg + 1]) { |
| 3098 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 3099 | goto error; |
| 3100 | } |
| 3101 | |
| 3102 | srv->check.fall = atol(args[*cur_arg+1]); |
| 3103 | if (srv->check.fall <= 0) { |
| 3104 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 3105 | goto error; |
| 3106 | } |
| 3107 | |
| 3108 | out: |
| 3109 | return err_code; |
| 3110 | |
| 3111 | error: |
| 3112 | deinit_srv_agent_check(srv); |
| 3113 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3114 | goto out; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | /* Parse the "inter" server keyword */ |
| 3118 | static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3119 | char **errmsg) |
| 3120 | { |
| 3121 | const char *err = NULL; |
| 3122 | unsigned int delay; |
| 3123 | int err_code = 0; |
| 3124 | |
| 3125 | if (!*(args[*cur_arg+1])) { |
| 3126 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 3127 | goto error; |
| 3128 | } |
| 3129 | |
| 3130 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 3131 | if (err == PARSE_TIME_OVER) { |
| 3132 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 3133 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 3134 | goto error; |
| 3135 | } |
| 3136 | else if (err == PARSE_TIME_UNDER) { |
| 3137 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 3138 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 3139 | goto error; |
| 3140 | } |
| 3141 | else if (err) { |
| 3142 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 3143 | *err, srv->id); |
| 3144 | goto error; |
| 3145 | } |
| 3146 | if (delay <= 0) { |
| 3147 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 3148 | delay, args[*cur_arg], srv->id); |
| 3149 | goto error; |
| 3150 | } |
| 3151 | srv->check.inter = delay; |
| 3152 | |
| 3153 | out: |
| 3154 | return err_code; |
| 3155 | |
| 3156 | error: |
| 3157 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3158 | goto out; |
| 3159 | } |
| 3160 | |
| 3161 | |
| 3162 | /* Parse the "fastinter" server keyword */ |
| 3163 | static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3164 | char **errmsg) |
| 3165 | { |
| 3166 | const char *err = NULL; |
| 3167 | unsigned int delay; |
| 3168 | int err_code = 0; |
| 3169 | |
| 3170 | if (!*(args[*cur_arg+1])) { |
| 3171 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 3172 | goto error; |
| 3173 | } |
| 3174 | |
| 3175 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 3176 | if (err == PARSE_TIME_OVER) { |
| 3177 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 3178 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 3179 | goto error; |
| 3180 | } |
| 3181 | else if (err == PARSE_TIME_UNDER) { |
| 3182 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 3183 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 3184 | goto error; |
| 3185 | } |
| 3186 | else if (err) { |
| 3187 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 3188 | *err, srv->id); |
| 3189 | goto error; |
| 3190 | } |
| 3191 | if (delay <= 0) { |
| 3192 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 3193 | delay, args[*cur_arg], srv->id); |
| 3194 | goto error; |
| 3195 | } |
| 3196 | srv->check.fastinter = delay; |
| 3197 | |
| 3198 | out: |
| 3199 | return err_code; |
| 3200 | |
| 3201 | error: |
| 3202 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3203 | goto out; |
| 3204 | } |
| 3205 | |
| 3206 | |
| 3207 | /* Parse the "downinter" server keyword */ |
| 3208 | static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3209 | char **errmsg) |
| 3210 | { |
| 3211 | const char *err = NULL; |
| 3212 | unsigned int delay; |
| 3213 | int err_code = 0; |
| 3214 | |
| 3215 | if (!*(args[*cur_arg+1])) { |
| 3216 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 3217 | goto error; |
| 3218 | } |
| 3219 | |
| 3220 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 3221 | if (err == PARSE_TIME_OVER) { |
| 3222 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 3223 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 3224 | goto error; |
| 3225 | } |
| 3226 | else if (err == PARSE_TIME_UNDER) { |
| 3227 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 3228 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 3229 | goto error; |
| 3230 | } |
| 3231 | else if (err) { |
| 3232 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 3233 | *err, srv->id); |
| 3234 | goto error; |
| 3235 | } |
| 3236 | if (delay <= 0) { |
| 3237 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 3238 | delay, args[*cur_arg], srv->id); |
| 3239 | goto error; |
| 3240 | } |
| 3241 | srv->check.downinter = delay; |
| 3242 | |
| 3243 | out: |
| 3244 | return err_code; |
| 3245 | |
| 3246 | error: |
| 3247 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3248 | goto out; |
| 3249 | } |
| 3250 | |
| 3251 | /* Parse the "port" server keyword */ |
| 3252 | static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 3253 | char **errmsg) |
| 3254 | { |
| 3255 | int err_code = 0; |
| 3256 | |
| 3257 | if (!*(args[*cur_arg+1])) { |
| 3258 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 3259 | goto error; |
| 3260 | } |
| 3261 | |
| 3262 | global.maxsock++; |
| 3263 | srv->check.port = atol(args[*cur_arg+1]); |
| 3264 | srv->flags |= SRV_F_CHECKPORT; |
| 3265 | |
| 3266 | out: |
| 3267 | return err_code; |
| 3268 | |
| 3269 | error: |
| 3270 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3271 | goto out; |
| 3272 | } |
| 3273 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3274 | static struct cfg_kw_list cfg_kws = {ILH, { |
Christopher Faulet | e9111b6 | 2020-04-09 18:12:08 +0200 | [diff] [blame] | 3275 | { CFG_LISTEN, "http-check", proxy_parse_httpcheck }, |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3276 | { 0, NULL, NULL }, |
| 3277 | }}; |
| 3278 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 3279 | static struct srv_kw_list srv_kws = { "CHK", { }, { |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 3280 | { "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] | 3281 | { "agent-addr", srv_parse_agent_addr, 1, 1 }, /* Enable an auxiliary agent check */ |
| 3282 | { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable agent checks */ |
| 3283 | { "agent-inter", srv_parse_agent_inter, 1, 1 }, /* Set the interval between two agent checks */ |
| 3284 | { "agent-port", srv_parse_agent_port, 1, 1 }, /* Set the TCP port used for agent checks. */ |
| 3285 | { "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] | 3286 | { "check", srv_parse_check, 0, 1 }, /* Enable health checks */ |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 3287 | { "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] | 3288 | { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* Enable PROXY protocol for health checks */ |
| 3289 | { "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] | 3290 | { "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] | 3291 | { "no-check", srv_parse_no_check, 0, 1 }, /* Disable health checks */ |
| 3292 | { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* Disable PROXY protol for health checks */ |
| 3293 | { "rise", srv_parse_check_rise, 1, 1 }, /* Set rise value for health checks */ |
| 3294 | { "fall", srv_parse_check_fall, 1, 1 }, /* Set fall value for health checks */ |
| 3295 | { "inter", srv_parse_check_inter, 1, 1 }, /* Set inter value for health checks */ |
| 3296 | { "fastinter", srv_parse_check_fastinter, 1, 1 }, /* Set fastinter value for health checks */ |
| 3297 | { "downinter", srv_parse_check_downinter, 1, 1 }, /* Set downinter value for health checks */ |
| 3298 | { "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] | 3299 | { NULL, NULL, 0 }, |
| 3300 | }}; |
| 3301 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3302 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 3303 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3304 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 3305 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3306 | * Local variables: |
| 3307 | * c-indent-level: 8 |
| 3308 | * c-basic-offset: 8 |
| 3309 | * End: |
| 3310 | */ |