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