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> |
Willy Tarreau | 9b39dc5 | 2014-07-08 00:54:10 +0200 | [diff] [blame] | 18 | #include <signal.h> |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 19 | #include <stdarg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 23 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <unistd.h> |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 25 | #include <sys/resource.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 27 | #include <sys/types.h> |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 28 | #include <sys/wait.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 30 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
| 32 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 33 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 34 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/compat.h> |
| 36 | #include <common/config.h> |
| 37 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 38 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 39 | #include <common/time.h> |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 40 | #include <common/hathreads.h> |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 41 | #include <common/http.h> |
| 42 | #include <common/h1.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
| 44 | #include <types/global.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 45 | #include <types/dns.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 46 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 48 | #include <proto/action.h> |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 49 | #include <proto/arg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 51 | #include <proto/checks.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 52 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/fd.h> |
| 54 | #include <proto/log.h> |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 55 | #include <proto/mux_pt.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/queue.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 57 | #include <proto/port_range.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 58 | #include <proto/proto_tcp.h> |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 59 | #include <proto/protocol.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 60 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/server.h> |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 62 | #include <proto/signal.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 63 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/task.h> |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 65 | #include <proto/vars.h> |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 66 | #include <proto/log.h> |
| 67 | #include <proto/dns.h> |
| 68 | #include <proto/proto_udp.h> |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 69 | #include <proto/ssl_sock.h> |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 70 | #include <proto/sample.h> |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 71 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 72 | static int tcpcheck_get_step_id(struct check *, struct tcpcheck_rule *); |
Willy Tarreau | 6bdcab0 | 2017-10-04 18:41:00 +0200 | [diff] [blame] | 73 | static int tcpcheck_main(struct check *); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 74 | static void __event_srv_chk_w(struct conn_stream *cs); |
Olivier Houchard | 4501c3e | 2018-08-28 19:36:18 +0200 | [diff] [blame] | 75 | static int wake_srv_chk(struct conn_stream *cs); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 76 | static void __event_srv_chk_r(struct conn_stream *cs); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 77 | |
Christopher Faulet | 31c30fd | 2020-03-26 21:10:03 +0100 | [diff] [blame] | 78 | static int srv_check_healthcheck_port(struct check *chk); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 79 | static struct tcpcheck_rule *parse_tcpcheck_connect(char **args, int cur_arg, struct proxy *px, |
| 80 | struct list *rules, const char *file, int line, |
| 81 | char **errmsg); |
| 82 | static struct tcpcheck_rule *parse_tcpcheck_expect(char **args, int cur_arg, struct proxy *px, |
| 83 | struct list *rules, unsigned int proto, |
| 84 | const char *file, int line, char **errmsg); |
Christopher Faulet | 31c30fd | 2020-03-26 21:10:03 +0100 | [diff] [blame] | 85 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 86 | /* Global list to share all tcp-checks */ |
| 87 | struct list tcpchecks_list = LIST_HEAD_INIT(tcpchecks_list); |
| 88 | |
| 89 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 90 | DECLARE_STATIC_POOL(pool_head_email_alert, "email_alert", sizeof(struct email_alert)); |
| 91 | DECLARE_STATIC_POOL(pool_head_tcpcheck_rule, "tcpcheck_rule", sizeof(struct tcpcheck_rule)); |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 92 | |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 93 | /* Dummy frontend used to create all checks sessions. */ |
| 94 | static struct proxy checks_fe; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 95 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 96 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 97 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 98 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 99 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 101 | /* Below we have finished checks */ |
| 102 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 103 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 104 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 105 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 106 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 107 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 108 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 109 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 111 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 112 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 113 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 114 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 115 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 116 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 117 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 118 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 119 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 120 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 121 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 122 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 123 | |
| 124 | [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" }, |
| 125 | [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" }, |
Cyril Bonté | 77010d8 | 2014-08-07 01:55:37 +0200 | [diff] [blame] | 126 | [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 129 | const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = { |
| 130 | [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT }, |
| 131 | [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 132 | [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 133 | [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT }, |
| 134 | [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT }, |
| 135 | [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 136 | [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT }, |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 137 | [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_ADDR }, |
| 138 | [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_UINT }, |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 139 | [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT }, |
| 140 | [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG }, |
| 141 | }; |
| 142 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 143 | 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] | 144 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 145 | |
| 146 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 147 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 148 | |
| 149 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 150 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 151 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 152 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 153 | |
| 154 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 155 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 156 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 157 | }; |
| 158 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 159 | /* checks if <err> is a real error for errno or one that can be ignored, and |
| 160 | * return 0 for these ones or <err> for real ones. |
| 161 | */ |
| 162 | static inline int unclean_errno(int err) |
| 163 | { |
| 164 | if (err == EAGAIN || err == EINPROGRESS || |
| 165 | err == EISCONN || err == EALREADY) |
| 166 | return 0; |
| 167 | return err; |
| 168 | } |
| 169 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 170 | /* |
| 171 | * Convert check_status code to description |
| 172 | */ |
| 173 | const char *get_check_status_description(short check_status) { |
| 174 | |
| 175 | const char *desc; |
| 176 | |
| 177 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 178 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 179 | else |
| 180 | desc = NULL; |
| 181 | |
| 182 | if (desc && *desc) |
| 183 | return desc; |
| 184 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 185 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Convert check_status code to short info |
| 190 | */ |
| 191 | const char *get_check_status_info(short check_status) { |
| 192 | |
| 193 | const char *info; |
| 194 | |
| 195 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 196 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 197 | else |
| 198 | info = NULL; |
| 199 | |
| 200 | if (info && *info) |
| 201 | return info; |
| 202 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 203 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 206 | const char *get_analyze_status(short analyze_status) { |
| 207 | |
| 208 | const char *desc; |
| 209 | |
| 210 | if (analyze_status < HANA_STATUS_SIZE) |
| 211 | desc = analyze_statuses[analyze_status].desc; |
| 212 | else |
| 213 | desc = NULL; |
| 214 | |
| 215 | if (desc && *desc) |
| 216 | return desc; |
| 217 | else |
| 218 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 219 | } |
| 220 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 221 | /* |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 222 | * Set check->status, update check->duration and fill check->result with |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 223 | * an adequate CHK_RES_* value. The new check->health is computed based |
| 224 | * on the result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 225 | * |
| 226 | * Show information in logs about failed health check if server is UP |
| 227 | * or succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 228 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 229 | static 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] | 230 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 231 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 232 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 233 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 234 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 235 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 236 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 237 | check->desc[0] = '\0'; |
| 238 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 242 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 243 | return; |
| 244 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 245 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 246 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 247 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 248 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 249 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 250 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 251 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 252 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 253 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 254 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 255 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 256 | check->duration = -1; |
| 257 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 258 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 259 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 260 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 263 | /* no change is expected if no state change occurred */ |
| 264 | if (check->result == CHK_RES_NEUTRAL) |
| 265 | return; |
| 266 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 267 | /* If the check was really just sending a mail, it won't have an |
| 268 | * associated server, so we're done now. |
| 269 | */ |
| 270 | if (!s) |
| 271 | return; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 272 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 273 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 274 | switch (check->result) { |
| 275 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 276 | /* Failure to connect to the agent as a secondary check should not |
| 277 | * cause the server to be marked down. |
| 278 | */ |
| 279 | if ((!(check->state & CHK_ST_AGENT) || |
Simon Horman | eaabd52 | 2015-02-26 11:26:17 +0900 | [diff] [blame] | 280 | (check->status >= HCHK_STATUS_L57DATA)) && |
Christopher Faulet | b119a79 | 2018-05-02 12:12:45 +0200 | [diff] [blame] | 281 | (check->health > 0)) { |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 282 | _HA_ATOMIC_ADD(&s->counters.failed_checks, 1); |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 283 | report = 1; |
| 284 | check->health--; |
| 285 | if (check->health < check->rise) |
| 286 | check->health = 0; |
| 287 | } |
| 288 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 289 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 290 | case CHK_RES_PASSED: |
| 291 | case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */ |
| 292 | if ((check->health < check->rise + check->fall - 1) && |
| 293 | (check->result == CHK_RES_PASSED || check->health > 0)) { |
| 294 | report = 1; |
| 295 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 296 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 297 | if (check->health >= check->rise) |
| 298 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 299 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 300 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 301 | /* clear consecutive_errors if observing is enabled */ |
| 302 | if (s->onerror) |
| 303 | s->consecutive_errors = 0; |
| 304 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 305 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 306 | default: |
| 307 | break; |
| 308 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 309 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 310 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 311 | (status != prev_status || report)) { |
| 312 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 313 | "%s check for %sserver %s/%s %s%s", |
| 314 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 315 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 316 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 317 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 318 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 319 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 320 | srv_append_status(&trash, s, check, -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 321 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 322 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 323 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 324 | (check->health >= check->rise) ? check->fall : check->rise, |
| 325 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 326 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 327 | ha_warning("%s.\n", trash.area); |
| 328 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
| 329 | send_email_alert(s, LOG_INFO, "%s", trash.area); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 330 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 333 | /* Marks the check <check>'s server down if the current check is already failed |
| 334 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 335 | */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 336 | static void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 337 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 338 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 339 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 340 | /* The agent secondary check should only cause a server to be marked |
| 341 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 342 | * that the agent returned "fail", "stopped" or "down". |
| 343 | * The implication here is that failure to connect to the agent |
| 344 | * as a secondary check should not cause the server to be marked |
| 345 | * down. */ |
| 346 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 347 | return; |
| 348 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 349 | if (check->health > 0) |
| 350 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 351 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 352 | /* 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] | 353 | 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] | 354 | } |
| 355 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 356 | /* 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] | 357 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 358 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 359 | * following conditions is true : |
| 360 | * - health check failed (check->health < rise) |
| 361 | * - agent check failed (agent->health < rise) |
| 362 | * - the server tracks a down server (track && track->state == STOPPED) |
| 363 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 364 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 365 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 366 | */ |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 367 | static void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 368 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 369 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 370 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 371 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 372 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 373 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 374 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 375 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 376 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 377 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 378 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 379 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 380 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 381 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 382 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 383 | if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 384 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 385 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 386 | 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] | 387 | } |
| 388 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 389 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 390 | * if it was running or starting, and provided it isn't in maintenance and other |
| 391 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 392 | * the same as for it to be turned up. Also, only the health checks support the |
| 393 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 394 | */ |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 395 | static void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 396 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 397 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 398 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 399 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 400 | return; |
| 401 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 402 | if (check->state & CHK_ST_AGENT) |
| 403 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 404 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 405 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 406 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 407 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 408 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 409 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 410 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 411 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 412 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 413 | |
Willy Tarreau | b26881a | 2017-12-23 11:16:49 +0100 | [diff] [blame] | 414 | 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] | 415 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 416 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 417 | /* note: use health_adjust() only, which first checks that the observe mode is |
| 418 | * enabled. |
| 419 | */ |
| 420 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 421 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 422 | int failed; |
| 423 | int expire; |
| 424 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 425 | if (s->observe >= HANA_OBS_SIZE) |
| 426 | return; |
| 427 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 428 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 429 | return; |
| 430 | |
| 431 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 432 | case 1: |
| 433 | failed = 1; |
| 434 | break; |
| 435 | |
| 436 | case 2: |
| 437 | failed = 0; |
| 438 | break; |
| 439 | |
| 440 | default: |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | if (!failed) { |
| 445 | /* good: clear consecutive_errors */ |
| 446 | s->consecutive_errors = 0; |
| 447 | return; |
| 448 | } |
| 449 | |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 450 | _HA_ATOMIC_ADD(&s->consecutive_errors, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 451 | |
| 452 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 453 | return; |
| 454 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 455 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 456 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 457 | |
| 458 | switch (s->onerror) { |
| 459 | case HANA_ONERR_FASTINTER: |
| 460 | /* force fastinter - nothing to do here as all modes force it */ |
| 461 | break; |
| 462 | |
| 463 | case HANA_ONERR_SUDDTH: |
| 464 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 465 | if (s->check.health > s->check.rise) |
| 466 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 467 | |
| 468 | /* no break - fall through */ |
| 469 | |
| 470 | case HANA_ONERR_FAILCHK: |
| 471 | /* simulate a failed health check */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 472 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 473 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 474 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 475 | break; |
| 476 | |
| 477 | case HANA_ONERR_MARKDWN: |
| 478 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 479 | s->check.health = s->check.rise; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 480 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 481 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 482 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 483 | break; |
| 484 | |
| 485 | default: |
| 486 | /* write a warning? */ |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | s->consecutive_errors = 0; |
Olivier Houchard | 7059c55 | 2019-03-08 18:49:32 +0100 | [diff] [blame] | 491 | _HA_ATOMIC_ADD(&s->counters.failed_hana, 1); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 492 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 493 | if (s->check.fastinter) { |
| 494 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 495 | if (s->check.task->expire > expire) { |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 496 | s->check.task->expire = expire; |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 497 | /* requeue check task with new expire */ |
| 498 | task_queue(s->check.task); |
| 499 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 503 | static int httpchk_build_status_header(struct server *s, char *buffer, int size) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 504 | { |
| 505 | int sv_state; |
| 506 | int ratio; |
| 507 | int hlen = 0; |
Joseph Lynch | 514061c | 2015-01-15 17:52:59 -0800 | [diff] [blame] | 508 | char addr[46]; |
| 509 | char port[6]; |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 510 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 511 | "UP %d/%d", "UP", |
| 512 | "NOLB %d/%d", "NOLB", |
| 513 | "no check" }; |
| 514 | |
| 515 | memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24); |
| 516 | hlen += 24; |
| 517 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 518 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 519 | sv_state = 6; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 520 | else if (s->cur_state != SRV_ST_STOPPED) { |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 521 | if (s->check.health == s->check.rise + s->check.fall - 1) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 522 | sv_state = 3; /* UP */ |
| 523 | else |
| 524 | sv_state = 2; /* going down */ |
| 525 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 526 | if (s->cur_state == SRV_ST_STOPPING) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 527 | sv_state += 2; |
| 528 | } else { |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 529 | if (s->check.health) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 530 | sv_state = 1; /* going up */ |
| 531 | else |
| 532 | sv_state = 0; /* DOWN */ |
| 533 | } |
| 534 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 535 | hlen += snprintf(buffer + hlen, size - hlen, |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 536 | srv_hlt_st[sv_state], |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 537 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 538 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 539 | |
Joseph Lynch | 514061c | 2015-01-15 17:52:59 -0800 | [diff] [blame] | 540 | addr_to_str(&s->addr, addr, sizeof(addr)); |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 541 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 542 | snprintf(port, sizeof(port), "%u", s->svc_port); |
| 543 | else |
| 544 | *port = 0; |
Joseph Lynch | 514061c | 2015-01-15 17:52:59 -0800 | [diff] [blame] | 545 | |
| 546 | hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 547 | addr, port, s->proxy->id, s->id, |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 548 | global.node, |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 549 | (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 550 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 551 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 552 | s->nbpend); |
| 553 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 554 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 555 | now.tv_sec < s->last_change + s->slowstart && |
| 556 | now.tv_sec >= s->last_change) { |
| 557 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 558 | hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | buffer[hlen++] = '\r'; |
| 562 | buffer[hlen++] = '\n'; |
| 563 | |
| 564 | return hlen; |
| 565 | } |
| 566 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 567 | /* Check the connection. If an error has already been reported or the socket is |
| 568 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 569 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 570 | * Warning, this must be done only once when returning from poll, and never |
| 571 | * after an I/O error was attempted, otherwise the error queue might contain |
| 572 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 573 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 574 | * clean (including a properly closed socket). |
| 575 | */ |
| 576 | static int retrieve_errno_from_socket(struct connection *conn) |
| 577 | { |
| 578 | int skerr; |
| 579 | socklen_t lskerr = sizeof(skerr); |
| 580 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 581 | if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 582 | return 1; |
| 583 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 584 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 585 | return 0; |
| 586 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 587 | if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 588 | errno = skerr; |
| 589 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 590 | errno = unclean_errno(errno); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 591 | |
| 592 | if (!errno) { |
| 593 | /* we could not retrieve an error, that does not mean there is |
| 594 | * none. Just don't change anything and only report the prior |
| 595 | * error if any. |
| 596 | */ |
| 597 | if (conn->flags & CO_FL_ERROR) |
| 598 | return 1; |
| 599 | else |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 604 | return 1; |
| 605 | } |
| 606 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 607 | /* Try to collect as much information as possible on the connection status, |
| 608 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 609 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 610 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 611 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 612 | * to extract errno from the socket. If no error is reported, it will consider |
| 613 | * the <expired> flag. This is intended to be used when a connection error was |
| 614 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 615 | * function takes care of not updating a server status which was already set. |
| 616 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 617 | * produce a status. |
| 618 | */ |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 619 | static 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] | 620 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 621 | struct conn_stream *cs = check->cs; |
| 622 | struct connection *conn = cs_conn(cs); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 623 | const char *err_msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 624 | struct buffer *chk; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 625 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 626 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 627 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 628 | return; |
| 629 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 630 | errno = unclean_errno(errno_bck); |
| 631 | if (conn && errno) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 632 | retrieve_errno_from_socket(conn); |
| 633 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 634 | if (conn && !(conn->flags & CO_FL_ERROR) && |
| 635 | !(cs->flags & CS_FL_ERROR) && !expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 636 | return; |
| 637 | |
| 638 | /* we'll try to build a meaningful error message depending on the |
| 639 | * context of the error possibly present in conn->err_code, and the |
| 640 | * socket error possibly collected above. This is useful to know the |
| 641 | * exact step of the L6 layer (eg: SSL handshake). |
| 642 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 643 | chk = get_trash_chunk(); |
| 644 | |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 645 | if (check->type == PR_O2_TCPCHK_CHK && |
| 646 | !(check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK)) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 647 | step = tcpcheck_get_step_id(check, NULL); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 648 | if (!step) |
| 649 | chunk_printf(chk, " at initial connection step of tcp-check"); |
| 650 | else { |
| 651 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 652 | /* we were looking for a string */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 653 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 654 | if (check->current_step->connect.port) |
| 655 | chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 656 | else |
| 657 | chunk_appendf(chk, " (connect)"); |
| 658 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 659 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) { |
| 660 | struct tcpcheck_expect *expect = &check->current_step->expect; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 661 | |
| 662 | switch (expect->type) { |
| 663 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 664 | chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), expect->data.ptr); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 665 | break; |
| 666 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 667 | chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), expect->data.ptr); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 668 | break; |
| 669 | case TCPCHK_EXPECT_REGEX: |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 670 | chunk_appendf(chk, " (expect regex)"); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 671 | break; |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 672 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 673 | chunk_appendf(chk, " (expect binary regex)"); |
| 674 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 675 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 676 | chunk_appendf(chk, " (expect HTTP status '%.*s')", (unsigned int)istlen(expect->data), expect->data.ptr); |
| 677 | break; |
| 678 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 679 | chunk_appendf(chk, " (expect HTTP status regex)"); |
| 680 | break; |
| 681 | case TCPCHK_EXPECT_HTTP_BODY: |
| 682 | chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), expect->data.ptr); |
| 683 | break; |
| 684 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
| 685 | chunk_appendf(chk, " (expect HTTP body regex)"); |
| 686 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 687 | case TCPCHK_EXPECT_CUSTOM: |
| 688 | chunk_appendf(chk, " (expect custom function)"); |
| 689 | break; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 690 | case TCPCHK_EXPECT_UNDEF: |
| 691 | chunk_appendf(chk, " (undefined expect!)"); |
| 692 | break; |
| 693 | } |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 694 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 695 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 696 | chunk_appendf(chk, " (send)"); |
| 697 | } |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 698 | |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 699 | if (check->current_step && check->current_step->comment) |
| 700 | chunk_appendf(chk, " comment: '%s'", check->current_step->comment); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 704 | if (conn && conn->err_code) { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 705 | if (unclean_errno(errno)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 706 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), |
| 707 | chk->area); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 708 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 709 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), |
| 710 | chk->area); |
| 711 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 712 | } |
| 713 | else { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 714 | if (unclean_errno(errno)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 715 | chunk_printf(&trash, "%s%s", strerror(errno), |
| 716 | chk->area); |
| 717 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 718 | } |
| 719 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 720 | err_msg = chk->area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 724 | if (check->state & CHK_ST_PORT_MISS) { |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 725 | /* NOTE: this is reported after <fall> tries */ |
| 726 | chunk_printf(chk, "No port available for the TCP connection"); |
| 727 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 728 | } |
| 729 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 730 | if (!conn) { |
| 731 | /* connection allocation error before the connection was established */ |
| 732 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 733 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 734 | else if (conn->flags & CO_FL_WAIT_L4_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 735 | /* L4 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 736 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 737 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 738 | else if (expired) |
| 739 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * might be due to a server IP change. |
| 743 | * Let's trigger a DNS resolution if none are currently running. |
| 744 | */ |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 745 | if (check->server) |
| 746 | dns_trigger_resolution(check->server->dns_requester); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 747 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 748 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 749 | else if (conn->flags & CO_FL_WAIT_L6_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 750 | /* L6 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 751 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 752 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 753 | else if (expired) |
| 754 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 755 | } |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 756 | else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 757 | /* I/O error after connection was established and before we could diagnose */ |
| 758 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 759 | } |
| 760 | else if (expired) { |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 761 | enum healthcheck_status tout = HCHK_STATUS_L7TOUT; |
| 762 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 763 | /* connection established but expired check */ |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 764 | if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) |
| 765 | tout = check->current_step->expect.tout_status; |
| 766 | set_server_check_status(check, tout, err_msg); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | return; |
| 770 | } |
| 771 | |
Olivier Houchard | 5c110b9 | 2018-08-14 17:04:58 +0200 | [diff] [blame] | 772 | /* This function checks if any I/O is wanted, and if so, attempts to do so */ |
| 773 | static struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned short state) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 774 | { |
Olivier Houchard | 26e1a8f | 2018-09-12 15:15:12 +0200 | [diff] [blame] | 775 | struct check *check = ctx; |
| 776 | struct conn_stream *cs = check->cs; |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 777 | struct email_alertq *q = container_of(check, typeof(*q), check); |
Olivier Houchard | bc89ad8 | 2019-07-09 17:28:51 +0200 | [diff] [blame] | 778 | int ret = 0; |
Olivier Houchard | 4501c3e | 2018-08-28 19:36:18 +0200 | [diff] [blame] | 779 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 780 | if (!(check->wait_list.events & SUB_RETRY_SEND)) |
Olivier Houchard | bc89ad8 | 2019-07-09 17:28:51 +0200 | [diff] [blame] | 781 | ret = wake_srv_chk(cs); |
| 782 | if (ret == 0 && !(check->wait_list.events & SUB_RETRY_RECV)) { |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 783 | if (check->server) |
| 784 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 785 | else |
| 786 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 787 | __event_srv_chk_r(cs); |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 788 | if (check->server) |
| 789 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 790 | else |
| 791 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 792 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
| 795 | |
| 796 | /* same as above but protected by the server lock. |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 797 | * |
| 798 | * Please do NOT place any return statement in this function and only leave |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 799 | * via the out label. NOTE THAT THIS FUNCTION DOESN'T LOCK, YOU PROBABLY WANT |
| 800 | * TO USE event_srv_chk_w() instead. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 801 | */ |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 802 | static void __event_srv_chk_w(struct conn_stream *cs) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 803 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 804 | struct connection *conn = cs->conn; |
| 805 | struct check *check = cs->data; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 806 | struct server *s = check->server; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 807 | struct task *t = check->task; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 808 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 809 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 810 | goto out_wakeup; |
| 811 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 812 | if (retrieve_errno_from_socket(conn)) { |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 813 | chk_report_conn_err(check, errno, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 814 | goto out_wakeup; |
| 815 | } |
Krzysztof Piotr Oledzki | 6492db5 | 2010-01-02 22:03:01 +0100 | [diff] [blame] | 816 | |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 817 | /* here, we know that the connection is established. That's enough for |
| 818 | * a pure TCP check. |
| 819 | */ |
| 820 | if (!check->type) |
| 821 | goto out_wakeup; |
| 822 | |
Willy Tarreau | c09572f | 2017-10-04 11:58:22 +0200 | [diff] [blame] | 823 | /* wake() will take care of calling tcpcheck_main() */ |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 824 | if (check->type == PR_O2_TCPCHK_CHK) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 825 | goto out; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 826 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 827 | if (b_data(&check->bo)) { |
Olivier Houchard | ed0f207 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 828 | cs->conn->mux->snd_buf(cs, &check->bo, b_data(&check->bo), 0); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 829 | b_realign_if_empty(&check->bo); |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 830 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) { |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 831 | chk_report_conn_err(check, errno, 0); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 832 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 833 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 834 | if (b_data(&check->bo)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 835 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 836 | goto out; |
| 837 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 838 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 839 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 840 | /* full request sent, we allow up to <timeout.check> if nonzero for a response */ |
| 841 | if (s->proxy->timeout.check) { |
| 842 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 843 | task_queue(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 844 | } |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 845 | goto out; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 846 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 847 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 848 | task_wakeup(t, TASK_WOKEN_IO); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 849 | out: |
| 850 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 853 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 854 | * This function is used only for server health-checks. It handles the server's |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 855 | * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 856 | * set_server_check_status() to update check->status, check->duration |
| 857 | * and check->result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 858 | |
| 859 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 860 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 861 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 862 | * response to an SSL HELLO (the principle is that this is enough to |
| 863 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 864 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 865 | * etc. |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 866 | * |
| 867 | * Please do NOT place any return statement in this function and only leave |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 868 | * via the out label. |
| 869 | * |
| 870 | * This must be called with the server lock held. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 871 | */ |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 872 | static void __event_srv_chk_r(struct conn_stream *cs) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 873 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 874 | struct connection *conn = cs->conn; |
| 875 | struct check *check = cs->data; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 876 | struct task *t = check->task; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 877 | int done; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 878 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 879 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 880 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 881 | |
Willy Tarreau | c09572f | 2017-10-04 11:58:22 +0200 | [diff] [blame] | 882 | /* wake() will take care of calling tcpcheck_main() */ |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 883 | if (check->type == PR_O2_TCPCHK_CHK) |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 884 | goto out; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 885 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 886 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 887 | * but the connection was closed on the remote end. Fortunately, recv still |
| 888 | * works correctly and we don't need to do the getsockopt() on linux. |
| 889 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 890 | |
| 891 | /* Set buffer to point to the end of the data already read, and check |
| 892 | * that there is free space remaining. If the buffer is full, proceed |
| 893 | * with running the checks without attempting another socket read. |
| 894 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 895 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 896 | done = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 897 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 898 | cs->conn->mux->rcv_buf(cs, &check->bi, b_size(&check->bi), 0); |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 899 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH) || cs->flags & CS_FL_ERROR) { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 900 | done = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 901 | if ((conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) && !b_data(&check->bi)) { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 902 | /* Report network errors only if we got no other data. Otherwise |
| 903 | * we'll let the upper layers decide whether the response is OK |
| 904 | * or not. It is very common that an RST sent by the server is |
| 905 | * reported as an error just after the last data chunk. |
| 906 | */ |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 907 | chk_report_conn_err(check, errno, 0); |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 908 | goto out_wakeup; |
| 909 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Willy Tarreau | 0f0393f | 2019-09-24 10:43:03 +0200 | [diff] [blame] | 912 | /* the rest of the code below expects the connection to be ready! */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 913 | if (conn->flags & CO_FL_WAIT_XPRT && !done) |
Willy Tarreau | 0f0393f | 2019-09-24 10:43:03 +0200 | [diff] [blame] | 914 | goto wait_more_data; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 915 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 916 | /* Intermediate or complete response received. |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 917 | * Terminate string in b_head(&check->bi) buffer. |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 918 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 919 | if (b_data(&check->bi) < b_size(&check->bi)) |
| 920 | b_head(&check->bi)[b_data(&check->bi)] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 921 | else { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 922 | b_head(&check->bi)[b_data(&check->bi) - 1] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 923 | done = 1; /* buffer full, don't wait for more data */ |
| 924 | } |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 925 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 926 | /* Run the checks... */ |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 927 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 928 | /* good connection is enough for pure TCP check */ |
| 929 | if (!(conn->flags & CO_FL_WAIT_XPRT) && !check->type) { |
| 930 | if (check->use_ssl == 1) |
| 931 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
| 932 | else |
| 933 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
| 934 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 935 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 936 | out_wakeup: |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 937 | /* collect possible new errors */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 938 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 939 | chk_report_conn_err(check, 0, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 940 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 941 | /* Reset the check buffer... */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 942 | *b_head(&check->bi) = '\0'; |
| 943 | b_reset(&check->bi); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 944 | |
Steven Davidovitz | 544d481 | 2017-03-08 11:06:20 -0800 | [diff] [blame] | 945 | /* Close the connection... We still attempt to nicely close if, |
| 946 | * for instance, SSL needs to send a "close notify." Later, we perform |
| 947 | * a hard close and reset the connection if some data are pending, |
| 948 | * otherwise we end up with many TIME_WAITs and eat all the source port |
| 949 | * range quickly. To avoid sending RSTs all the time, we first try to |
| 950 | * drain pending data. |
Willy Tarreau | fd29cc5 | 2012-11-23 09:18:20 +0100 | [diff] [blame] | 951 | */ |
Olivier Houchard | 6c7e96a | 2019-07-02 16:35:18 +0200 | [diff] [blame] | 952 | /* Call cs_shutr() first, to add the CO_FL_SOCK_RD_SH flag on the |
| 953 | * connection, to make sure cs_shutw() will not lead to a shutdown() |
| 954 | * that would provoke TIME_WAITs. |
| 955 | */ |
| 956 | cs_shutr(cs, CS_SHR_DRAIN); |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 957 | cs_shutw(cs, CS_SHW_NORMAL); |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 958 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 959 | /* OK, let's not stay here forever */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 960 | if (check->result == CHK_RES_FAILED) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 961 | conn->flags |= CO_FL_ERROR; |
| 962 | |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 963 | task_wakeup(t, TASK_WOKEN_IO); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 964 | out: |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 965 | return; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 966 | |
| 967 | wait_more_data: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 968 | cs->conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 969 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 972 | /* |
| 973 | * This function is used only for server health-checks. It handles connection |
| 974 | * status updates including errors. If necessary, it wakes the check task up. |
Willy Tarreau | 6bdcab0 | 2017-10-04 18:41:00 +0200 | [diff] [blame] | 975 | * It returns 0 on normal cases, <0 if at least one close() has happened on the |
| 976 | * connection (eg: reconnect). |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 977 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 978 | static int wake_srv_chk(struct conn_stream *cs) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 979 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 980 | struct connection *conn = cs->conn; |
| 981 | struct check *check = cs->data; |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 982 | struct email_alertq *q = container_of(check, typeof(*q), check); |
Willy Tarreau | 6bdcab0 | 2017-10-04 18:41:00 +0200 | [diff] [blame] | 983 | int ret = 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 984 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 985 | if (check->server) |
| 986 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 987 | else |
| 988 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Emeric Brun | f6ba17d | 2017-11-02 14:35:27 +0100 | [diff] [blame] | 989 | |
Willy Tarreau | c09572f | 2017-10-04 11:58:22 +0200 | [diff] [blame] | 990 | /* we may have to make progress on the TCP checks */ |
Willy Tarreau | 6bdcab0 | 2017-10-04 18:41:00 +0200 | [diff] [blame] | 991 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 992 | ret = tcpcheck_main(check); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 993 | cs = check->cs; |
Willy Tarreau | 543abd4 | 2018-09-20 11:25:12 +0200 | [diff] [blame] | 994 | conn = cs->conn; |
Willy Tarreau | c594039 | 2019-09-05 17:38:40 +0200 | [diff] [blame] | 995 | } else { |
| 996 | if (!(check->wait_list.events & SUB_RETRY_SEND)) |
| 997 | __event_srv_chk_w(cs); |
| 998 | if (!(check->wait_list.events & SUB_RETRY_RECV)) |
| 999 | __event_srv_chk_r(cs); |
| 1000 | } |
Willy Tarreau | c09572f | 2017-10-04 11:58:22 +0200 | [diff] [blame] | 1001 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 1002 | if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) { |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1003 | /* We may get error reports bypassing the I/O handlers, typically |
| 1004 | * the case when sending a pure TCP check which fails, then the I/O |
| 1005 | * handlers above are not called. This is completely handled by the |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1006 | * main processing task so let's simply wake it up. If we get here, |
| 1007 | * we expect errno to still be valid. |
| 1008 | */ |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 1009 | chk_report_conn_err(check, errno, 0); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1010 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1011 | } |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 1012 | else if (!(conn->flags & CO_FL_WAIT_XPRT) && !check->type) { |
Willy Tarreau | 3be293f | 2014-02-05 18:31:24 +0100 | [diff] [blame] | 1013 | /* we may get here if only a connection probe was required : we |
| 1014 | * don't have any data to send nor anything expected in response, |
| 1015 | * so the completion of the connection establishment is enough. |
| 1016 | */ |
| 1017 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1018 | } |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1019 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1020 | if (check->result != CHK_RES_UNKNOWN) { |
Christopher Faulet | 774c486 | 2019-01-21 14:15:50 +0100 | [diff] [blame] | 1021 | /* Check complete or aborted. If connection not yet closed do it |
| 1022 | * now and wake the check task up to be sure the result is |
| 1023 | * handled ASAP. */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 1024 | conn_sock_drain(conn); |
Willy Tarreau | a553ae9 | 2017-10-05 18:52:17 +0200 | [diff] [blame] | 1025 | cs_close(cs); |
Willy Tarreau | 6bdcab0 | 2017-10-04 18:41:00 +0200 | [diff] [blame] | 1026 | ret = -1; |
Olivier Houchard | f494957 | 2019-07-02 17:42:22 +0200 | [diff] [blame] | 1027 | /* We may have been scheduled to run, and the |
| 1028 | * I/O handler expects to have a cs, so remove |
| 1029 | * the tasklet |
| 1030 | */ |
| 1031 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
Christopher Faulet | 774c486 | 2019-01-21 14:15:50 +0100 | [diff] [blame] | 1032 | task_wakeup(check->task, TASK_WOKEN_IO); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1033 | } |
Willy Tarreau | 6bdcab0 | 2017-10-04 18:41:00 +0200 | [diff] [blame] | 1034 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 1035 | if (check->server) |
| 1036 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 1037 | else |
| 1038 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Emeric Brun | f6ba17d | 2017-11-02 14:35:27 +0100 | [diff] [blame] | 1039 | |
Willy Tarreau | 6bdcab0 | 2017-10-04 18:41:00 +0200 | [diff] [blame] | 1040 | /* if a connection got replaced, we must absolutely prevent the connection |
| 1041 | * handler from touching its fd, and perform the FD polling updates ourselves |
| 1042 | */ |
| 1043 | if (ret < 0) |
| 1044 | conn_cond_update_polling(conn); |
| 1045 | |
| 1046 | return ret; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1049 | struct data_cb check_conn_cb = { |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1050 | .wake = wake_srv_chk, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 1051 | .name = "CHCK", |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1052 | }; |
| 1053 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1054 | /* |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1055 | * updates the server's weight during a warmup stage. Once the final weight is |
| 1056 | * reached, the task automatically stops. Note that any server status change |
| 1057 | * must have updated s->last_change accordingly. |
| 1058 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1059 | static struct task *server_warmup(struct task *t, void *context, unsigned short state) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1060 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1061 | struct server *s = context; |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1062 | |
| 1063 | /* by default, plan on stopping the task */ |
| 1064 | t->expire = TICK_ETERNITY; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1065 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 1066 | (s->next_state != SRV_ST_STARTING)) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1067 | return t; |
| 1068 | |
Willy Tarreau | 4fc49a9 | 2019-05-05 06:54:22 +0200 | [diff] [blame] | 1069 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 1070 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1071 | /* recalculate the weights and update the state */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1072 | server_recalc_eweight(s, 1); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1073 | |
| 1074 | /* probably that we can refill this server with a bit more connections */ |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1075 | pendconn_grab_from_px(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1076 | |
Willy Tarreau | 4fc49a9 | 2019-05-05 06:54:22 +0200 | [diff] [blame] | 1077 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 1078 | |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1079 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1080 | * whichever is greater, resulting in small 5% steps. |
| 1081 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1082 | if (s->next_state == SRV_ST_STARTING) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1083 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1084 | return t; |
| 1085 | } |
| 1086 | |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 1087 | /* returns the first NON-COMMENT tcp-check rule from list <list> or NULL if |
| 1088 | * none was found. |
| 1089 | */ |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 1090 | static struct tcpcheck_rule *get_first_tcpcheck_rule(struct tcpcheck_rules *rules) |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 1091 | { |
| 1092 | struct tcpcheck_rule *r; |
| 1093 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 1094 | list_for_each_entry(r, rules->list, list) { |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1095 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
Willy Tarreau | 894c642 | 2017-10-04 15:58:52 +0200 | [diff] [blame] | 1096 | return r; |
| 1097 | } |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | |
Christopher Faulet | 95226db | 2020-04-15 11:34:04 +0200 | [diff] [blame] | 1101 | /* returns the last NON-COMMENT tcp-check rule from list <list> or NULL if none |
| 1102 | * was found. |
| 1103 | */ |
| 1104 | static struct tcpcheck_rule *get_last_tcpcheck_rule(struct tcpcheck_rules *rules) |
| 1105 | { |
| 1106 | struct tcpcheck_rule *r; |
| 1107 | |
| 1108 | list_for_each_entry_rev(r, rules->list, list) { |
| 1109 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
| 1110 | return r; |
| 1111 | } |
| 1112 | return NULL; |
| 1113 | } |
| 1114 | |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 1115 | /* returns the NON-COMMENT tcp-check rule from list <list> following <start> or |
| 1116 | * NULL if non was found. If <start> is NULL, it relies on |
| 1117 | * get_first_tcpcheck_rule(). |
| 1118 | */ |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 1119 | static struct tcpcheck_rule *get_next_tcpcheck_rule(struct tcpcheck_rules *rules, struct tcpcheck_rule *start) |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 1120 | { |
| 1121 | struct tcpcheck_rule *r; |
| 1122 | |
| 1123 | if (!start) |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 1124 | return get_first_tcpcheck_rule(rules); |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 1125 | |
| 1126 | r = LIST_NEXT(&start->list, typeof(r), list); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 1127 | list_for_each_entry_from(r, rules->list, list) { |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1128 | if (r->action != TCPCHK_ACT_COMMENT && r->action != TCPCHK_ACT_ACTION_KW) |
Gaetan Rivet | 1d22d7e | 2020-02-14 17:47:08 +0100 | [diff] [blame] | 1129 | return r; |
| 1130 | } |
| 1131 | return NULL; |
| 1132 | } |
| 1133 | |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1134 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1135 | * establish a server health-check that makes use of a connection. |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1136 | * |
| 1137 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1138 | * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called |
| 1139 | * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called |
| 1140 | * - SF_ERR_SRVTO if there are no more servers |
| 1141 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 1142 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1143 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1144 | * - SF_ERR_INTERNAL for any other purely internal errors |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 1145 | * - SF_ERR_CHK_PORT if no port could be found to run a health check on an AF_INET* socket |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 1146 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1147 | * Note that we try to prevent the network stack from sending the ACK during the |
| 1148 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 1149 | */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1150 | static int connect_conn_chk(struct task *t) |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1151 | { |
| 1152 | struct check *check = t->context; |
| 1153 | struct server *s = check->server; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1154 | struct conn_stream *cs = check->cs; |
| 1155 | struct connection *conn = cs_conn(cs); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1156 | struct protocol *proto; |
| 1157 | int ret; |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 1158 | int connflags = 0; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1159 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1160 | /* we cannot have a connection here */ |
| 1161 | if (conn) |
| 1162 | return SF_ERR_INTERNAL; |
| 1163 | |
Willy Tarreau | f411cce | 2017-10-04 16:21:19 +0200 | [diff] [blame] | 1164 | /* for tcp-checks, the initial connection setup is handled separately as |
| 1165 | * it may be sent to a specific port and not to the server's. |
| 1166 | */ |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 1167 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 1168 | /* tcpcheck initialisation */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 1169 | check->current_step = NULL; |
Willy Tarreau | f411cce | 2017-10-04 16:21:19 +0200 | [diff] [blame] | 1170 | tcpcheck_main(check); |
| 1171 | return SF_ERR_UP; |
| 1172 | } |
| 1173 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1174 | /* prepare a new connection */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1175 | cs = check->cs = cs_new(NULL); |
| 1176 | if (!check->cs) |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1177 | return SF_ERR_RESOURCE; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1178 | conn = cs->conn; |
Olivier Houchard | 26e1a8f | 2018-09-12 15:15:12 +0200 | [diff] [blame] | 1179 | /* Maybe there were an older connection we were waiting on */ |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1180 | check->wait_list.events = 0; |
Olivier Houchard | ff1e9f3 | 2019-09-20 17:18:35 +0200 | [diff] [blame] | 1181 | tasklet_set_tid(check->wait_list.tasklet, tid); |
| 1182 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1183 | |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 1184 | if (!sockaddr_alloc(&conn->dst)) |
| 1185 | return SF_ERR_RESOURCE; |
| 1186 | |
Simon Horman | 41f5876 | 2015-01-30 11:22:56 +0900 | [diff] [blame] | 1187 | if (is_addr(&check->addr)) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1188 | /* we'll connect to the check addr specified on the server */ |
Willy Tarreau | b3c81cb | 2019-07-17 16:54:52 +0200 | [diff] [blame] | 1189 | *conn->dst = check->addr; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1190 | } |
| 1191 | else { |
| 1192 | /* we'll connect to the addr on the server */ |
Willy Tarreau | b3c81cb | 2019-07-17 16:54:52 +0200 | [diff] [blame] | 1193 | *conn->dst = s->addr; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1194 | } |
| 1195 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1196 | if (s->check.via_socks4 && (s->flags & SRV_F_SOCKS4_PROXY)) { |
| 1197 | conn->send_proxy_ofs = 1; |
| 1198 | conn->flags |= CO_FL_SOCKS4; |
| 1199 | } |
| 1200 | |
Willy Tarreau | b3c81cb | 2019-07-17 16:54:52 +0200 | [diff] [blame] | 1201 | proto = protocol_by_family(conn->dst->ss_family); |
Olivier Houchard | 6377a00 | 2017-12-01 22:04:05 +0100 | [diff] [blame] | 1202 | conn->target = &s->obj_type; |
| 1203 | |
Willy Tarreau | b3c81cb | 2019-07-17 16:54:52 +0200 | [diff] [blame] | 1204 | if ((conn->dst->ss_family == AF_INET) || (conn->dst->ss_family == AF_INET6)) { |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 1205 | int i = 0; |
| 1206 | |
| 1207 | i = srv_check_healthcheck_port(check); |
Olivier Houchard | 6377a00 | 2017-12-01 22:04:05 +0100 | [diff] [blame] | 1208 | if (i == 0) |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 1209 | return SF_ERR_CHK_PORT; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 1210 | |
Willy Tarreau | b3c81cb | 2019-07-17 16:54:52 +0200 | [diff] [blame] | 1211 | set_host_port(conn->dst, i); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1212 | } |
| 1213 | |
Thierry FOURNIER | bb2ae64 | 2015-01-14 11:31:49 +0100 | [diff] [blame] | 1214 | /* no client address */ |
Thierry FOURNIER | bb2ae64 | 2015-01-14 11:31:49 +0100 | [diff] [blame] | 1215 | |
Willy Tarreau | be37315 | 2018-09-06 11:45:30 +0200 | [diff] [blame] | 1216 | conn_prepare(conn, proto, check->xprt); |
Olivier Houchard | f67be93 | 2019-01-29 15:47:43 +0100 | [diff] [blame] | 1217 | if (conn_install_mux(conn, &mux_pt_ops, cs, s->proxy, NULL) < 0) |
| 1218 | return SF_ERR_RESOURCE; |
Willy Tarreau | be37315 | 2018-09-06 11:45:30 +0200 | [diff] [blame] | 1219 | cs_attach(cs, check, &check_conn_cb); |
| 1220 | |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 1221 | /* only plain tcp check supports quick ACK */ |
| 1222 | connflags |= (check->type ? CONNECT_HAS_DATA : CONNECT_DELACK_ALWAYS); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1223 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1224 | ret = SF_ERR_INTERNAL; |
Olivier Houchard | b68fda4 | 2017-08-04 18:39:01 +0200 | [diff] [blame] | 1225 | if (proto && proto->connect) |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 1226 | ret = proto->connect(conn, connflags); |
Willy Tarreau | 16257f6 | 2017-11-02 15:45:00 +0100 | [diff] [blame] | 1227 | |
Willy Tarreau | 16257f6 | 2017-11-02 15:45:00 +0100 | [diff] [blame] | 1228 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 1229 | #ifdef USE_OPENSSL |
Olivier Houchard | a48437b | 2019-01-29 16:37:52 +0100 | [diff] [blame] | 1230 | if (ret == SF_ERR_NONE) { |
| 1231 | if (s->check.sni) |
| 1232 | ssl_sock_set_servername(conn, s->check.sni); |
| 1233 | if (s->check.alpn_str) |
| 1234 | ssl_sock_set_alpn(conn, (unsigned char *)s->check.alpn_str, |
| 1235 | s->check.alpn_len); |
| 1236 | } |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 1237 | #endif |
Willy Tarreau | f494977 | 2017-05-06 08:45:28 +0200 | [diff] [blame] | 1238 | if (s->check.send_proxy && !(check->state & CHK_ST_AGENT)) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1239 | conn->send_proxy_ofs = 1; |
| 1240 | conn->flags |= CO_FL_SEND_PROXY; |
Olivier Houchard | 37d7897 | 2019-12-30 15:13:42 +0100 | [diff] [blame] | 1241 | } |
| 1242 | if (conn->flags & (CO_FL_SEND_PROXY | CO_FL_SOCKS4) && |
| 1243 | conn_ctrl_ready(conn)) { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 1244 | if (xprt_add_hs(conn) < 0) |
| 1245 | ret = SF_ERR_RESOURCE; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | return ret; |
| 1249 | } |
| 1250 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1251 | static struct list pid_list = LIST_HEAD_INIT(pid_list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1252 | static struct pool_head *pool_head_pid_list; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 1253 | __decl_spinlock(pid_list_lock); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1254 | |
| 1255 | void block_sigchld(void) |
| 1256 | { |
| 1257 | sigset_t set; |
| 1258 | sigemptyset(&set); |
| 1259 | sigaddset(&set, SIGCHLD); |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 1260 | assert(ha_sigmask(SIG_BLOCK, &set, NULL) == 0); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | void unblock_sigchld(void) |
| 1264 | { |
| 1265 | sigset_t set; |
| 1266 | sigemptyset(&set); |
Willy Tarreau | ebc9244 | 2016-06-21 17:29:46 +0200 | [diff] [blame] | 1267 | sigaddset(&set, SIGCHLD); |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 1268 | assert(ha_sigmask(SIG_UNBLOCK, &set, NULL) == 0); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1269 | } |
| 1270 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1271 | static struct pid_list *pid_list_add(pid_t pid, struct task *t) |
| 1272 | { |
| 1273 | struct pid_list *elem; |
| 1274 | struct check *check = t->context; |
| 1275 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1276 | elem = pool_alloc(pool_head_pid_list); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1277 | if (!elem) |
| 1278 | return NULL; |
| 1279 | elem->pid = pid; |
| 1280 | elem->t = t; |
| 1281 | elem->exited = 0; |
| 1282 | check->curpid = elem; |
| 1283 | LIST_INIT(&elem->list); |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 1284 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1285 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1286 | LIST_ADD(&pid_list, &elem->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1287 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 1288 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1289 | return elem; |
| 1290 | } |
| 1291 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1292 | static void pid_list_del(struct pid_list *elem) |
| 1293 | { |
| 1294 | struct check *check; |
| 1295 | |
| 1296 | if (!elem) |
| 1297 | return; |
| 1298 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1299 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1300 | LIST_DEL(&elem->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1301 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 1302 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1303 | if (!elem->exited) |
| 1304 | kill(elem->pid, SIGTERM); |
| 1305 | |
| 1306 | check = elem->t->context; |
| 1307 | check->curpid = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1308 | pool_free(pool_head_pid_list, elem); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* Called from inside SIGCHLD handler, SIGCHLD is blocked */ |
| 1312 | static void pid_list_expire(pid_t pid, int status) |
| 1313 | { |
| 1314 | struct pid_list *elem; |
| 1315 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1316 | HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1317 | list_for_each_entry(elem, &pid_list, list) { |
| 1318 | if (elem->pid == pid) { |
| 1319 | elem->t->expire = now_ms; |
| 1320 | elem->status = status; |
| 1321 | elem->exited = 1; |
Cyril Bonté | 9dbcfab | 2014-08-07 01:55:39 +0200 | [diff] [blame] | 1322 | task_wakeup(elem->t, TASK_WOKEN_IO); |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 1323 | break; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1324 | } |
| 1325 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1326 | HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1327 | } |
| 1328 | |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 1329 | static void sigchld_handler(struct sig_handler *sh) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1330 | { |
| 1331 | pid_t pid; |
| 1332 | int status; |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 1333 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1334 | while ((pid = waitpid(0, &status, WNOHANG)) > 0) |
| 1335 | pid_list_expire(pid, status); |
| 1336 | } |
| 1337 | |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 1338 | static int init_pid_list(void) |
| 1339 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1340 | if (pool_head_pid_list != NULL) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1341 | /* Nothing to do */ |
| 1342 | return 0; |
| 1343 | |
Willy Tarreau | 48d6bf2 | 2016-06-21 16:27:34 +0200 | [diff] [blame] | 1344 | if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1345 | ha_alert("Failed to set signal handler for external health checks: %s. Aborting.\n", |
| 1346 | strerror(errno)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1347 | return 1; |
| 1348 | } |
| 1349 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1350 | pool_head_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED); |
| 1351 | if (pool_head_pid_list == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1352 | ha_alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n", |
| 1353 | strerror(errno)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1354 | return 1; |
| 1355 | } |
| 1356 | |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1360 | /* helper macro to set an environment variable and jump to a specific label on failure. */ |
| 1361 | #define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; } |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1362 | |
| 1363 | /* |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1364 | * helper function to allocate enough memory to store an environment variable. |
| 1365 | * It will also check that the environment variable is updatable, and silently |
| 1366 | * fail if not. |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1367 | */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1368 | static int extchk_setenv(struct check *check, int idx, const char *value) |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1369 | { |
| 1370 | int len, ret; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1371 | char *envname; |
| 1372 | int vmaxlen; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1373 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1374 | if (idx < 0 || idx >= EXTCHK_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1375 | ha_alert("Illegal environment variable index %d. Aborting.\n", idx); |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1376 | return 1; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1377 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1378 | |
| 1379 | envname = extcheck_envs[idx].name; |
| 1380 | vmaxlen = extcheck_envs[idx].vmaxlen; |
| 1381 | |
| 1382 | /* Check if the environment variable is already set, and silently reject |
| 1383 | * the update if this one is not updatable. */ |
| 1384 | if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx])) |
| 1385 | return 0; |
| 1386 | |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1387 | /* Instead of sending NOT_USED, sending an empty value is preferable */ |
| 1388 | if (strcmp(value, "NOT_USED") == 0) { |
| 1389 | value = ""; |
| 1390 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1391 | |
| 1392 | len = strlen(envname) + 1; |
| 1393 | if (vmaxlen == EXTCHK_SIZE_EVAL_INIT) |
| 1394 | len += strlen(value); |
| 1395 | else |
| 1396 | len += vmaxlen; |
| 1397 | |
| 1398 | if (!check->envp[idx]) |
| 1399 | check->envp[idx] = malloc(len + 1); |
| 1400 | |
| 1401 | if (!check->envp[idx]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1402 | ha_alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1403 | return 1; |
| 1404 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1405 | ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1406 | if (ret < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1407 | ha_alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno)); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1408 | return 1; |
| 1409 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1410 | else if (ret > len) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1411 | ha_alert("Environment variable '%s' was truncated. Aborting.\n", envname); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1412 | return 1; |
| 1413 | } |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1414 | return 0; |
| 1415 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1416 | |
| 1417 | static int prepare_external_check(struct check *check) |
| 1418 | { |
| 1419 | struct server *s = check->server; |
| 1420 | struct proxy *px = s->proxy; |
| 1421 | struct listener *listener = NULL, *l; |
| 1422 | int i; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1423 | const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1424 | char buf[256]; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1425 | |
| 1426 | list_for_each_entry(l, &px->conf.listeners, by_fe) |
| 1427 | /* Use the first INET, INET6 or UNIX listener */ |
| 1428 | if (l->addr.ss_family == AF_INET || |
| 1429 | l->addr.ss_family == AF_INET6 || |
| 1430 | l->addr.ss_family == AF_UNIX) { |
| 1431 | listener = l; |
| 1432 | break; |
| 1433 | } |
| 1434 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1435 | check->curpid = NULL; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1436 | check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *)); |
| 1437 | if (!check->envp) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1438 | ha_alert("Failed to allocate memory for environment variables. Aborting\n"); |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1439 | goto err; |
| 1440 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1441 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1442 | check->argv = calloc(6, sizeof(char *)); |
| 1443 | if (!check->argv) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1444 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1445 | goto err; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1446 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1447 | |
| 1448 | check->argv[0] = px->check_command; |
| 1449 | |
Cyril Bonté | 777be86 | 2014-12-02 21:21:35 +0100 | [diff] [blame] | 1450 | if (!listener) { |
| 1451 | check->argv[1] = strdup("NOT_USED"); |
| 1452 | check->argv[2] = strdup("NOT_USED"); |
| 1453 | } |
| 1454 | else if (listener->addr.ss_family == AF_INET || |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1455 | listener->addr.ss_family == AF_INET6) { |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1456 | addr_to_str(&listener->addr, buf, sizeof(buf)); |
| 1457 | check->argv[1] = strdup(buf); |
| 1458 | port_to_str(&listener->addr, buf, sizeof(buf)); |
| 1459 | check->argv[2] = strdup(buf); |
Cyril Bonté | 777be86 | 2014-12-02 21:21:35 +0100 | [diff] [blame] | 1460 | } |
| 1461 | else if (listener->addr.ss_family == AF_UNIX) { |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1462 | const struct sockaddr_un *un; |
| 1463 | |
| 1464 | un = (struct sockaddr_un *)&listener->addr; |
| 1465 | check->argv[1] = strdup(un->sun_path); |
| 1466 | check->argv[2] = strdup("NOT_USED"); |
Cyril Bonté | 777be86 | 2014-12-02 21:21:35 +0100 | [diff] [blame] | 1467 | } |
| 1468 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1469 | ha_alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1470 | goto err; |
| 1471 | } |
| 1472 | |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1473 | if (!check->argv[1] || !check->argv[2]) { |
| 1474 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1475 | goto err; |
| 1476 | } |
| 1477 | |
| 1478 | check->argv[3] = calloc(EXTCHK_SIZE_ADDR, sizeof(*check->argv[3])); |
| 1479 | check->argv[4] = calloc(EXTCHK_SIZE_UINT, sizeof(*check->argv[4])); |
| 1480 | if (!check->argv[3] || !check->argv[4]) { |
| 1481 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1482 | goto err; |
| 1483 | } |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 1484 | |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1485 | addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR); |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 1486 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1487 | snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1488 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1489 | for (i = 0; i < 5; i++) { |
| 1490 | if (!check->argv[i]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1491 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1492 | goto err; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1493 | } |
| 1494 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1495 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1496 | EXTCHK_SETENV(check, EXTCHK_PATH, path, err); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1497 | /* Add proxy environment variables */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1498 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err); |
| 1499 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err); |
| 1500 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err); |
| 1501 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1502 | /* Add server environment variables */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1503 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err); |
| 1504 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err); |
| 1505 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err); |
| 1506 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err); |
| 1507 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err); |
| 1508 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err); |
| 1509 | |
| 1510 | /* Ensure that we don't leave any hole in check->envp */ |
| 1511 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 1512 | if (!check->envp[i]) |
| 1513 | EXTCHK_SETENV(check, i, "", err); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1514 | |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1515 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1516 | err: |
| 1517 | if (check->envp) { |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1518 | for (i = 0; i < EXTCHK_SIZE; i++) |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1519 | free(check->envp[i]); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1520 | free(check->envp); |
| 1521 | check->envp = NULL; |
| 1522 | } |
| 1523 | |
| 1524 | if (check->argv) { |
| 1525 | for (i = 1; i < 5; i++) |
| 1526 | free(check->argv[i]); |
| 1527 | free(check->argv); |
| 1528 | check->argv = NULL; |
| 1529 | } |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1530 | return 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1531 | } |
| 1532 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1533 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1534 | * establish a server health-check that makes use of a process. |
| 1535 | * |
| 1536 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1537 | * - SF_ERR_NONE if everything's OK |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1538 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 1539 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1540 | * |
| 1541 | * Blocks and then unblocks SIGCHLD |
| 1542 | */ |
| 1543 | static int connect_proc_chk(struct task *t) |
| 1544 | { |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1545 | char buf[256]; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1546 | struct check *check = t->context; |
| 1547 | struct server *s = check->server; |
| 1548 | struct proxy *px = s->proxy; |
| 1549 | int status; |
| 1550 | pid_t pid; |
| 1551 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1552 | status = SF_ERR_RESOURCE; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1553 | |
| 1554 | block_sigchld(); |
| 1555 | |
| 1556 | pid = fork(); |
| 1557 | if (pid < 0) { |
Willy Tarreau | d96f112 | 2019-12-03 07:07:36 +0100 | [diff] [blame] | 1558 | ha_alert("Failed to fork process for external health check%s: %s. Aborting.\n", |
| 1559 | (global.tune.options & GTUNE_INSECURE_FORK) ? |
| 1560 | "" : " (likely caused by missing 'insecure-fork-wanted')", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1561 | strerror(errno)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1562 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1563 | goto out; |
| 1564 | } |
| 1565 | if (pid == 0) { |
| 1566 | /* Child */ |
| 1567 | extern char **environ; |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 1568 | struct rlimit limit; |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1569 | int fd; |
| 1570 | |
| 1571 | /* close all FDs. Keep stdin/stdout/stderr in verbose mode */ |
| 1572 | fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3; |
| 1573 | |
Willy Tarreau | 2555ccf | 2019-02-21 22:22:06 +0100 | [diff] [blame] | 1574 | my_closefrom(fd); |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 1575 | |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 1576 | /* restore the initial FD limits */ |
| 1577 | limit.rlim_cur = rlim_fd_cur_at_boot; |
| 1578 | limit.rlim_max = rlim_fd_max_at_boot; |
| 1579 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 1580 | getrlimit(RLIMIT_NOFILE, &limit); |
| 1581 | ha_warning("External check: failed to restore initial FD limits (cur=%u max=%u), using cur=%u max=%u\n", |
| 1582 | rlim_fd_cur_at_boot, rlim_fd_max_at_boot, |
| 1583 | (unsigned int)limit.rlim_cur, (unsigned int)limit.rlim_max); |
| 1584 | } |
| 1585 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1586 | environ = check->envp; |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1587 | |
| 1588 | /* Update some environment variables and command args: curconn, server addr and server port */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1589 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf))); |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1590 | |
| 1591 | addr_to_str(&s->addr, check->argv[3], EXTCHK_SIZE_ADDR); |
| 1592 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3]); |
| 1593 | |
| 1594 | *check->argv[4] = 0; |
| 1595 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 1596 | snprintf(check->argv[4], EXTCHK_SIZE_UINT, "%u", s->svc_port); |
| 1597 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4]); |
| 1598 | |
Willy Tarreau | 2df8cad | 2019-07-01 07:51:29 +0200 | [diff] [blame] | 1599 | haproxy_unblock_signals(); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1600 | execvp(px->check_command, check->argv); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1601 | ha_alert("Failed to exec process for external health check: %s. Aborting.\n", |
| 1602 | strerror(errno)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1603 | exit(-1); |
| 1604 | } |
| 1605 | |
| 1606 | /* Parent */ |
| 1607 | if (check->result == CHK_RES_UNKNOWN) { |
| 1608 | if (pid_list_add(pid, t) != NULL) { |
| 1609 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1610 | |
| 1611 | if (px->timeout.check && px->timeout.connect) { |
| 1612 | int t_con = tick_add(now_ms, px->timeout.connect); |
| 1613 | t->expire = tick_first(t->expire, t_con); |
| 1614 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1615 | status = SF_ERR_NONE; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1616 | goto out; |
| 1617 | } |
| 1618 | else { |
| 1619 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1620 | } |
| 1621 | kill(pid, SIGTERM); /* process creation error */ |
| 1622 | } |
| 1623 | else |
| 1624 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1625 | |
| 1626 | out: |
| 1627 | unblock_sigchld(); |
| 1628 | return status; |
| 1629 | } |
| 1630 | |
| 1631 | /* |
Willy Tarreau | 1e62e2a | 2017-10-04 15:07:02 +0200 | [diff] [blame] | 1632 | * manages a server health-check that uses an external process. Returns |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1633 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1634 | * |
| 1635 | * Please do NOT place any return statement in this function and only leave |
| 1636 | * via the out_unlock label. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1637 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1638 | static struct task *process_chk_proc(struct task *t, void *context, unsigned short state) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1639 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1640 | struct check *check = context; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1641 | struct server *s = check->server; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1642 | int rv; |
| 1643 | int ret; |
| 1644 | int expired = tick_is_expired(t->expire, now_ms); |
| 1645 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1646 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1647 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 1648 | /* no check currently running */ |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1649 | if (!expired) /* woke up too early */ |
| 1650 | goto out_unlock; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1651 | |
| 1652 | /* we don't send any health-checks when the proxy is |
| 1653 | * stopped, the server should not be checked or the check |
| 1654 | * is disabled. |
| 1655 | */ |
| 1656 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 1657 | s->proxy->state == PR_STSTOPPED) |
| 1658 | goto reschedule; |
| 1659 | |
| 1660 | /* we'll initiate a new check */ |
| 1661 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
| 1662 | |
| 1663 | check->state |= CHK_ST_INPROGRESS; |
| 1664 | |
Simon Horman | dbf7019 | 2015-01-30 11:22:53 +0900 | [diff] [blame] | 1665 | ret = connect_proc_chk(t); |
Willy Tarreau | d7c3fbd | 2017-10-04 15:19:26 +0200 | [diff] [blame] | 1666 | if (ret == SF_ERR_NONE) { |
Willy Tarreau | 1e62e2a | 2017-10-04 15:07:02 +0200 | [diff] [blame] | 1667 | /* the process was forked, we allow up to min(inter, |
| 1668 | * timeout.connect) for it to report its status, but |
| 1669 | * only when timeout.check is set as it may be to short |
| 1670 | * for a full check otherwise. |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1671 | */ |
| 1672 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1673 | |
| 1674 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1675 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1676 | t->expire = tick_first(t->expire, t_con); |
| 1677 | } |
Christopher Faulet | 88ce5d1 | 2017-10-20 15:41:18 +0200 | [diff] [blame] | 1678 | task_set_affinity(t, tid_bit); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1679 | goto reschedule; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1680 | } |
| 1681 | |
Willy Tarreau | 1e62e2a | 2017-10-04 15:07:02 +0200 | [diff] [blame] | 1682 | /* here, we failed to start the check */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1683 | |
| 1684 | check->state &= ~CHK_ST_INPROGRESS; |
| 1685 | check_notify_failure(check); |
| 1686 | |
| 1687 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1688 | * to establish but only when timeout.check is set |
| 1689 | * as it may be to short for a full check otherwise |
| 1690 | */ |
| 1691 | while (tick_is_expired(t->expire, now_ms)) { |
| 1692 | int t_con; |
| 1693 | |
| 1694 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1695 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1696 | |
| 1697 | if (s->proxy->timeout.check) |
| 1698 | t->expire = tick_first(t->expire, t_con); |
| 1699 | } |
| 1700 | } |
| 1701 | else { |
| 1702 | /* there was a test running. |
| 1703 | * First, let's check whether there was an uncaught error, |
| 1704 | * which can happen on connect timeout or error. |
| 1705 | */ |
| 1706 | if (check->result == CHK_RES_UNKNOWN) { |
| 1707 | /* good connection is enough for pure TCP check */ |
| 1708 | struct pid_list *elem = check->curpid; |
| 1709 | int status = HCHK_STATUS_UNKNOWN; |
| 1710 | |
| 1711 | if (elem->exited) { |
| 1712 | status = elem->status; /* Save in case the process exits between use below */ |
| 1713 | if (!WIFEXITED(status)) |
| 1714 | check->code = -1; |
| 1715 | else |
| 1716 | check->code = WEXITSTATUS(status); |
| 1717 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 1718 | status = HCHK_STATUS_PROCERR; |
| 1719 | else |
| 1720 | status = HCHK_STATUS_PROCOK; |
| 1721 | } else if (expired) { |
| 1722 | status = HCHK_STATUS_PROCTOUT; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1723 | ha_warning("kill %d\n", (int)elem->pid); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1724 | kill(elem->pid, SIGTERM); |
| 1725 | } |
| 1726 | set_server_check_status(check, status, NULL); |
| 1727 | } |
| 1728 | |
| 1729 | if (check->result == CHK_RES_FAILED) { |
| 1730 | /* a failure or timeout detected */ |
| 1731 | check_notify_failure(check); |
| 1732 | } |
| 1733 | else if (check->result == CHK_RES_CONDPASS) { |
| 1734 | /* check is OK but asks for stopping mode */ |
| 1735 | check_notify_stopping(check); |
| 1736 | } |
| 1737 | else if (check->result == CHK_RES_PASSED) { |
| 1738 | /* a success was detected */ |
| 1739 | check_notify_success(check); |
| 1740 | } |
Willy Tarreau | 6dd4ac8 | 2019-09-03 18:55:02 +0200 | [diff] [blame] | 1741 | task_set_affinity(t, 1); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1742 | check->state &= ~CHK_ST_INPROGRESS; |
| 1743 | |
| 1744 | pid_list_del(check->curpid); |
| 1745 | |
| 1746 | rv = 0; |
| 1747 | if (global.spread_checks > 0) { |
| 1748 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 5a6d3e7 | 2020-03-08 17:53:53 +0100 | [diff] [blame] | 1749 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1750 | } |
| 1751 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
| 1752 | } |
| 1753 | |
| 1754 | reschedule: |
| 1755 | while (tick_is_expired(t->expire, now_ms)) |
| 1756 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1757 | |
| 1758 | out_unlock: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1759 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1760 | return t; |
| 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * manages a server health-check that uses a connection. Returns |
| 1765 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1766 | * |
| 1767 | * Please do NOT place any return statement in this function and only leave |
| 1768 | * via the out_unlock label. |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1769 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1770 | static struct task *process_chk_conn(struct task *t, void *context, unsigned short state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1771 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1772 | struct check *check = context; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 1773 | struct proxy *proxy = check->proxy; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1774 | struct conn_stream *cs = check->cs; |
| 1775 | struct connection *conn = cs_conn(cs); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1776 | int rv; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1777 | int ret; |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1778 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1779 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 1780 | if (check->server) |
| 1781 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1782 | if (!(check->state & CHK_ST_INPROGRESS)) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1783 | /* no check currently running */ |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1784 | if (!expired) /* woke up too early */ |
| 1785 | goto out_unlock; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1786 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1787 | /* we don't send any health-checks when the proxy is |
| 1788 | * stopped, the server should not be checked or the check |
| 1789 | * is disabled. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1790 | */ |
Willy Tarreau | 0d924cc | 2013-12-11 21:26:24 +0100 | [diff] [blame] | 1791 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 1792 | proxy->state == PR_STSTOPPED) |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1793 | goto reschedule; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1794 | |
| 1795 | /* we'll initiate a new check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1796 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1797 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1798 | check->state |= CHK_ST_INPROGRESS; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1799 | b_reset(&check->bi); |
| 1800 | b_reset(&check->bo); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1801 | |
Olivier Houchard | aebeff7 | 2019-11-29 16:18:51 +0100 | [diff] [blame] | 1802 | task_set_affinity(t, tid_bit); |
Simon Horman | dbf7019 | 2015-01-30 11:22:53 +0900 | [diff] [blame] | 1803 | ret = connect_conn_chk(t); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1804 | cs = check->cs; |
| 1805 | conn = cs_conn(cs); |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1806 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1807 | switch (ret) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1808 | case SF_ERR_UP: |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1809 | goto out_unlock; |
| 1810 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1811 | case SF_ERR_NONE: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1812 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1813 | * to establish but only when timeout.check is set |
| 1814 | * as it may be to short for a full check otherwise |
| 1815 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1816 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 1817 | if (proxy->timeout.check && proxy->timeout.connect) { |
| 1818 | int t_con = tick_add(now_ms, proxy->timeout.connect); |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1819 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1820 | } |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1821 | |
Willy Tarreau | cc705a6 | 2019-09-05 17:51:30 +0200 | [diff] [blame] | 1822 | if (check->type) { |
| 1823 | /* send the request if we have one. We avoid receiving |
| 1824 | * if not connected, unless we didn't subscribe for |
| 1825 | * sending since otherwise we won't be woken up. |
| 1826 | */ |
| 1827 | __event_srv_chk_w(cs); |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 1828 | if (!(conn->flags & CO_FL_WAIT_XPRT) || |
Willy Tarreau | c594039 | 2019-09-05 17:38:40 +0200 | [diff] [blame] | 1829 | !(check->wait_list.events & SUB_RETRY_SEND)) |
| 1830 | __event_srv_chk_r(cs); |
Willy Tarreau | cc705a6 | 2019-09-05 17:51:30 +0200 | [diff] [blame] | 1831 | } |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1832 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1833 | goto reschedule; |
| 1834 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1835 | case SF_ERR_SRVTO: /* ETIMEDOUT */ |
| 1836 | case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1837 | if (conn) |
| 1838 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 1839 | chk_report_conn_err(check, errno, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1840 | break; |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 1841 | /* should share same code than cases below */ |
| 1842 | case SF_ERR_CHK_PORT: |
| 1843 | check->state |= CHK_ST_PORT_MISS; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1844 | case SF_ERR_PRXCOND: |
| 1845 | case SF_ERR_RESOURCE: |
| 1846 | case SF_ERR_INTERNAL: |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1847 | if (conn) |
| 1848 | conn->flags |= CO_FL_ERROR; |
| 1849 | chk_report_conn_err(check, conn ? 0 : ENOMEM, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1850 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1853 | /* here, we have seen a synchronous error, no fd was allocated */ |
Olivier Houchard | aebeff7 | 2019-11-29 16:18:51 +0100 | [diff] [blame] | 1854 | task_set_affinity(t, MAX_THREADS_MASK); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1855 | if (cs) { |
Olivier Houchard | 4906554 | 2019-05-31 19:20:36 +0200 | [diff] [blame] | 1856 | if (check->wait_list.events) |
| 1857 | cs->conn->xprt->unsubscribe(cs->conn, |
| 1858 | cs->conn->xprt_ctx, |
| 1859 | check->wait_list.events, |
| 1860 | &check->wait_list); |
| 1861 | /* We may have been scheduled to run, and the |
| 1862 | * I/O handler expects to have a cs, so remove |
| 1863 | * the tasklet |
| 1864 | */ |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 1865 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1866 | cs_destroy(cs); |
| 1867 | cs = check->cs = NULL; |
| 1868 | conn = NULL; |
Olivier Houchard | 390485a | 2017-10-24 19:03:30 +0200 | [diff] [blame] | 1869 | } |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1870 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1871 | check->state &= ~CHK_ST_INPROGRESS; |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1872 | check_notify_failure(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1873 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1874 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1875 | * to establish but only when timeout.check is set |
| 1876 | * as it may be to short for a full check otherwise |
| 1877 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1878 | while (tick_is_expired(t->expire, now_ms)) { |
| 1879 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1880 | |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 1881 | t_con = tick_add(t->expire, proxy->timeout.connect); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1882 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 1883 | if (proxy->timeout.check) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1884 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1885 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1886 | } |
| 1887 | else { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1888 | /* there was a test running. |
| 1889 | * First, let's check whether there was an uncaught error, |
| 1890 | * which can happen on connect timeout or error. |
| 1891 | */ |
Simon Horman | ccaabcd | 2014-06-20 12:29:47 +0900 | [diff] [blame] | 1892 | if (check->result == CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1893 | /* good connection is enough for pure TCP check */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 1894 | if (!(conn->flags & CO_FL_WAIT_XPRT) && !check->type) { |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 1895 | if (check->use_ssl == 1) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1896 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1897 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1898 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1899 | } |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 1900 | else if ((conn->flags & CO_FL_ERROR) || cs->flags & CS_FL_ERROR || expired) { |
Willy Tarreau | b5259bf | 2017-10-04 14:47:29 +0200 | [diff] [blame] | 1901 | chk_report_conn_err(check, 0, expired); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1902 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1903 | else |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1904 | goto out_unlock; /* timeout not reached, wait again */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1907 | /* check complete or aborted */ |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 1908 | |
| 1909 | check->current_step = NULL; |
| 1910 | if (check->sess != NULL) { |
| 1911 | session_free(check->sess); |
| 1912 | check->sess = NULL; |
| 1913 | } |
| 1914 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1915 | if (conn && conn->xprt) { |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1916 | /* The check was aborted and the connection was not yet closed. |
| 1917 | * This can happen upon timeout, or when an external event such |
| 1918 | * as a failed response coupled with "observe layer7" caused the |
| 1919 | * server state to be suddenly changed. |
| 1920 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 1921 | conn_sock_drain(conn); |
Willy Tarreau | a553ae9 | 2017-10-05 18:52:17 +0200 | [diff] [blame] | 1922 | cs_close(cs); |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1923 | } |
| 1924 | |
Willy Tarreau | ac59f36 | 2017-10-08 11:10:19 +0200 | [diff] [blame] | 1925 | if (cs) { |
Olivier Houchard | 4906554 | 2019-05-31 19:20:36 +0200 | [diff] [blame] | 1926 | if (check->wait_list.events) |
| 1927 | cs->conn->xprt->unsubscribe(cs->conn, |
| 1928 | cs->conn->xprt_ctx, |
| 1929 | check->wait_list.events, |
| 1930 | &check->wait_list); |
| 1931 | /* We may have been scheduled to run, and the |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 1932 | * I/O handler expects to have a cs, so remove |
| 1933 | * the tasklet |
| 1934 | */ |
| 1935 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1936 | cs_destroy(cs); |
| 1937 | cs = check->cs = NULL; |
| 1938 | conn = NULL; |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 1939 | } |
| 1940 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 1941 | if (check->server) { |
| 1942 | if (check->result == CHK_RES_FAILED) { |
| 1943 | /* a failure or timeout detected */ |
| 1944 | check_notify_failure(check); |
| 1945 | } |
| 1946 | else if (check->result == CHK_RES_CONDPASS) { |
| 1947 | /* check is OK but asks for stopping mode */ |
| 1948 | check_notify_stopping(check); |
| 1949 | } |
| 1950 | else if (check->result == CHK_RES_PASSED) { |
| 1951 | /* a success was detected */ |
| 1952 | check_notify_success(check); |
| 1953 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1954 | } |
Christopher Faulet | 88ce5d1 | 2017-10-20 15:41:18 +0200 | [diff] [blame] | 1955 | task_set_affinity(t, MAX_THREADS_MASK); |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1956 | check->state &= ~CHK_ST_INPROGRESS; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1957 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 1958 | if (check->server) { |
| 1959 | rv = 0; |
| 1960 | if (global.spread_checks > 0) { |
| 1961 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 5a6d3e7 | 2020-03-08 17:53:53 +0100 | [diff] [blame] | 1962 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 1963 | } |
| 1964 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1965 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1966 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1967 | |
| 1968 | reschedule: |
| 1969 | while (tick_is_expired(t->expire, now_ms)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1970 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 62ac84f | 2017-11-05 10:11:13 +0100 | [diff] [blame] | 1971 | out_unlock: |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 1972 | if (check->server) |
| 1973 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1974 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1977 | /* |
| 1978 | * manages a server health-check. Returns |
| 1979 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1980 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1981 | static struct task *process_chk(struct task *t, void *context, unsigned short state) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1982 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1983 | struct check *check = context; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1984 | |
| 1985 | if (check->type == PR_O2_EXT_CHK) |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1986 | return process_chk_proc(t, context, state); |
| 1987 | return process_chk_conn(t, context, state); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 1988 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1989 | } |
| 1990 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1991 | static int start_check_task(struct check *check, int mininter, |
| 1992 | int nbcheck, int srvpos) |
| 1993 | { |
| 1994 | struct task *t; |
Willy Tarreau | 6dd4ac8 | 2019-09-03 18:55:02 +0200 | [diff] [blame] | 1995 | unsigned long thread_mask = MAX_THREADS_MASK; |
| 1996 | |
| 1997 | if (check->type == PR_O2_EXT_CHK) |
| 1998 | thread_mask = 1; |
| 1999 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2000 | /* task for the check */ |
Willy Tarreau | 6dd4ac8 | 2019-09-03 18:55:02 +0200 | [diff] [blame] | 2001 | if ((t = task_new(thread_mask)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2002 | ha_alert("Starting [%s:%s] check: out of memory.\n", |
| 2003 | check->server->proxy->id, check->server->id); |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | check->task = t; |
| 2008 | t->process = process_chk; |
| 2009 | t->context = check; |
| 2010 | |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2011 | if (mininter < srv_getinter(check)) |
| 2012 | mininter = srv_getinter(check); |
| 2013 | |
| 2014 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 2015 | mininter = global.max_spread_checks; |
| 2016 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2017 | /* check this every ms */ |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2018 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2019 | check->start = now; |
| 2020 | task_queue(t); |
| 2021 | |
| 2022 | return 1; |
| 2023 | } |
| 2024 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2025 | /* |
| 2026 | * Start health-check. |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 2027 | * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case. |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2028 | */ |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 2029 | static int start_checks() |
| 2030 | { |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2031 | |
| 2032 | struct proxy *px; |
| 2033 | struct server *s; |
| 2034 | struct task *t; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2035 | int nbcheck=0, mininter=0, srvpos=0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2036 | |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 2037 | /* 0- init the dummy frontend used to create all checks sessions */ |
| 2038 | init_new_proxy(&checks_fe); |
| 2039 | checks_fe.cap = PR_CAP_FE | PR_CAP_BE; |
| 2040 | checks_fe.mode = PR_MODE_TCP; |
| 2041 | checks_fe.maxconn = 0; |
| 2042 | checks_fe.conn_retries = CONN_RETRIES; |
| 2043 | checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
| 2044 | checks_fe.timeout.client = TICK_ETERNITY; |
| 2045 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2046 | /* 1- count the checkers to run simultaneously. |
| 2047 | * We also determine the minimum interval among all of those which |
| 2048 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 2049 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 2050 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2051 | * too short an interval for all others. |
| 2052 | */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2053 | for (px = proxies_list; px; px = px->next) { |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2054 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 2055 | if (s->slowstart) { |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2056 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2057 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 2058 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 2059 | } |
| 2060 | /* We need a warmup task that will be called when the server |
| 2061 | * state switches from down to up. |
| 2062 | */ |
| 2063 | s->warmup = t; |
| 2064 | t->process = server_warmup; |
| 2065 | t->context = s; |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 2066 | /* server can be in this state only because of */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2067 | if (s->next_state == SRV_ST_STARTING) |
Baptiste Assmann | 6076d1c | 2015-09-17 22:53:59 +0200 | [diff] [blame] | 2068 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20))); |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 2069 | } |
| 2070 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2071 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 2072 | nbcheck++; |
| 2073 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 2074 | (!mininter || mininter > srv_getinter(&s->check))) |
| 2075 | mininter = srv_getinter(&s->check); |
| 2076 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 2077 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2078 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 2079 | nbcheck++; |
| 2080 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 2081 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 2082 | mininter = srv_getinter(&s->agent); |
| 2083 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2084 | } |
| 2085 | } |
| 2086 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2087 | if (!nbcheck) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2088 | return 0; |
| 2089 | |
| 2090 | srand((unsigned)time(NULL)); |
| 2091 | |
| 2092 | /* |
| 2093 | * 2- start them as far as possible from each others. For this, we will |
| 2094 | * start them after their interval set to the min interval divided by |
| 2095 | * the number of servers, weighted by the server's position in the list. |
| 2096 | */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2097 | for (px = proxies_list; px; px = px->next) { |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2098 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 2099 | if (init_pid_list()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2100 | ha_alert("Starting [%s] check: out of memory.\n", px->id); |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 2101 | return ERR_ALERT | ERR_FATAL; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2105 | for (s = px->srv; s; s = s->next) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2106 | /* A task for the main check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2107 | if (s->check.state & CHK_ST_CONFIGURED) { |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 2108 | if (s->check.type == PR_O2_EXT_CHK) { |
| 2109 | if (!prepare_external_check(&s->check)) |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 2110 | return ERR_ALERT | ERR_FATAL; |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 2111 | } |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2112 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 2113 | return ERR_ALERT | ERR_FATAL; |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2114 | srvpos++; |
| 2115 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2116 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2117 | /* A task for a auxiliary agent check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2118 | if (s->agent.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2119 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
Willy Tarreau | 865c514 | 2016-12-21 20:04:48 +0100 | [diff] [blame] | 2120 | return ERR_ALERT | ERR_FATAL; |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2121 | } |
| 2122 | srvpos++; |
| 2123 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | return 0; |
| 2127 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2128 | |
| 2129 | /* |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2130 | * return the id of a step in a send/expect session |
| 2131 | */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2132 | static int tcpcheck_get_step_id(struct check *check, struct tcpcheck_rule *rule) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2133 | { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2134 | if (!rule) |
| 2135 | rule = check->current_step; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 2136 | |
Christopher Faulet | 3c29aa6 | 2020-03-24 13:31:19 +0100 | [diff] [blame] | 2137 | /* no last started step => first step */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2138 | if (!rule) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2139 | return 1; |
| 2140 | |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2141 | /* last step is the first implicit connect */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2142 | if (rule->index == 0 && |
| 2143 | rule->action == TCPCHK_ACT_CONNECT && |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2144 | (rule->connect.options & TCPCHK_OPT_IMPLICIT)) |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2145 | return 0; |
| 2146 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2147 | return rule->index + 1; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2148 | } |
| 2149 | |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2150 | static void tcpcheck_onerror_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule, |
| 2151 | int match, struct ist info) |
| 2152 | { |
| 2153 | struct sample *smp; |
| 2154 | |
| 2155 | if (istlen(info)) { |
| 2156 | chunk_strncat(msg, info.ptr, info.len); |
| 2157 | goto comment; |
| 2158 | } |
| 2159 | else if (!LIST_ISEMPTY(&rule->expect.onerror_fmt)) { |
| 2160 | msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), &rule->expect.onerror_fmt); |
| 2161 | goto comment; |
| 2162 | } |
| 2163 | |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 2164 | if (check->type == PR_O2_TCPCHK_CHK && (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK)) |
| 2165 | goto comment; |
| 2166 | |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2167 | chunk_strcat(msg, (match ? "TCPCHK matched unwanted content" : "TCPCHK did not match content")); |
| 2168 | switch (rule->expect.type) { |
| 2169 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2170 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 2171 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 2172 | chunk_appendf(msg, " '%.*s' at step %d", (unsigned int)istlen(rule->expect.data), rule->expect.data.ptr, |
| 2173 | tcpcheck_get_step_id(check, rule)); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2174 | break; |
| 2175 | case TCPCHK_EXPECT_BINARY: |
| 2176 | chunk_appendf(msg, " (binary) at step %d", tcpcheck_get_step_id(check, rule)); |
| 2177 | break; |
| 2178 | case TCPCHK_EXPECT_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2179 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 2180 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2181 | chunk_appendf(msg, " (regex) at step %d", tcpcheck_get_step_id(check, rule)); |
| 2182 | break; |
| 2183 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 2184 | chunk_appendf(msg, " (binary regex) at step %d", tcpcheck_get_step_id(check, rule)); |
| 2185 | |
| 2186 | /* If references to the matched text were made, divide the |
| 2187 | * offsets by 2 to match offset of the original response buffer. |
| 2188 | */ |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 2189 | if (rule->expect.flags & TCPCHK_EXPT_FL_CAP) { |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2190 | int i; |
| 2191 | |
| 2192 | for (i = 1; i < MAX_MATCH && pmatch[i].rm_so != -1; i++) { |
| 2193 | pmatch[i].rm_so /= 2; /* at first matched char. */ |
| 2194 | pmatch[i].rm_eo /= 2; /* at last matched char. */ |
| 2195 | } |
| 2196 | } |
| 2197 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 2198 | case TCPCHK_EXPECT_CUSTOM: |
| 2199 | chunk_appendf(msg, " (custom function) at step %d", tcpcheck_get_step_id(check, rule)); |
| 2200 | break; |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2201 | case TCPCHK_EXPECT_UNDEF: |
| 2202 | /* Should never happen. */ |
| 2203 | return; |
| 2204 | } |
| 2205 | |
| 2206 | comment: |
| 2207 | if (rule->comment) { |
| 2208 | chunk_strcat(msg, " comment: "); |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 2209 | if (rule->expect.flags & TCPCHK_EXPT_FL_CAP) { |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2210 | int ret = exp_replace(b_tail(msg), b_room(msg), b_head(&check->bi), rule->comment, pmatch); |
| 2211 | if (ret != -1) /* ignore comment if too large */ |
| 2212 | msg->data += ret; |
| 2213 | } |
| 2214 | else |
| 2215 | chunk_strcat(msg, rule->comment); |
| 2216 | } |
| 2217 | |
| 2218 | if (rule->expect.status_expr) { |
| 2219 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL, |
| 2220 | rule->expect.status_expr, SMP_T_SINT); |
| 2221 | if (smp) |
| 2222 | check->code = smp->data.u.sint; |
| 2223 | } |
| 2224 | |
| 2225 | *(b_tail(msg)) = '\0'; |
| 2226 | } |
| 2227 | |
| 2228 | static void tcpcheck_onsuccess_message(struct buffer *msg, struct check *check, struct tcpcheck_rule *rule, |
| 2229 | struct ist info) |
| 2230 | { |
| 2231 | struct sample *smp; |
| 2232 | |
| 2233 | if (istlen(info)) |
| 2234 | chunk_strncat(msg, info.ptr, info.len); |
| 2235 | if (!LIST_ISEMPTY(&rule->expect.onsuccess_fmt)) |
| 2236 | msg->data += sess_build_logline(check->sess, NULL, b_tail(msg), b_room(msg), |
| 2237 | &rule->expect.onsuccess_fmt); |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 2238 | else if (check->type == PR_O2_TCPCHK_CHK && !(check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK)) |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 2239 | chunk_strcat(msg, "(tcp-check)"); |
| 2240 | |
| 2241 | if (rule->expect.status_expr) { |
| 2242 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, SMP_OPT_DIR_RES | SMP_OPT_FINAL, |
| 2243 | rule->expect.status_expr, SMP_T_SINT); |
| 2244 | if (smp) |
| 2245 | check->code = smp->data.u.sint; |
| 2246 | } |
| 2247 | |
| 2248 | *(b_tail(msg)) = '\0'; |
| 2249 | } |
| 2250 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2251 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_packet(struct check *check, struct tcpcheck_rule *rule, |
| 2252 | unsigned int offset, int last_read) |
| 2253 | { |
| 2254 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2255 | enum healthcheck_status status; |
| 2256 | struct buffer *msg = NULL; |
| 2257 | struct ist desc = ist(NULL); |
| 2258 | unsigned int err = 0, plen = 0; |
| 2259 | |
| 2260 | |
| 2261 | /* 3 Bytes for the packet length and 1 byte for the sequence id */ |
| 2262 | if (!last_read && b_data(&check->bi) < offset+4) { |
| 2263 | if (!last_read) |
| 2264 | goto wait_more_data; |
| 2265 | |
| 2266 | /* invalid length or truncated response */ |
| 2267 | status = HCHK_STATUS_L7RSP; |
| 2268 | goto error; |
| 2269 | } |
| 2270 | |
| 2271 | plen = ((unsigned char) *b_peek(&check->bi, offset)) + |
| 2272 | (((unsigned char) *(b_peek(&check->bi, offset+1))) << 8) + |
| 2273 | (((unsigned char) *(b_peek(&check->bi, offset+2))) << 16); |
| 2274 | |
| 2275 | if (b_data(&check->bi) < offset+plen+4) { |
| 2276 | if (!last_read) |
| 2277 | goto wait_more_data; |
| 2278 | |
| 2279 | /* invalid length or truncated response */ |
| 2280 | status = HCHK_STATUS_L7RSP; |
| 2281 | goto error; |
| 2282 | } |
| 2283 | |
| 2284 | if (*b_peek(&check->bi, offset+4) == '\xff') { |
| 2285 | /* MySQL Error packet always begin with field_count = 0xff */ |
| 2286 | status = HCHK_STATUS_L7STS; |
| 2287 | err = ((unsigned char) *b_peek(&check->bi, offset+5)) + |
| 2288 | (((unsigned char) *(b_peek(&check->bi, offset+6))) << 8); |
| 2289 | desc = ist2(b_peek(&check->bi, offset+7), b_data(&check->bi) - offset - 7); |
| 2290 | goto error; |
| 2291 | } |
| 2292 | |
| 2293 | if (get_next_tcpcheck_rule(check->tcpcheck_rules, rule) != NULL) { |
| 2294 | /* Not the last rule, continue */ |
| 2295 | goto out; |
| 2296 | } |
| 2297 | |
| 2298 | /* We set the MySQL Version in description for information purpose |
| 2299 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 2300 | * like mark as down old MySQL server. |
| 2301 | */ |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 2302 | set_server_check_status(check, rule->expect.ok_status, b_peek(&check->bi, 5)); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2303 | |
| 2304 | out: |
| 2305 | free_trash_chunk(msg); |
| 2306 | return ret; |
| 2307 | |
| 2308 | error: |
| 2309 | ret = TCPCHK_EVAL_STOP; |
| 2310 | check->code = err; |
| 2311 | msg = alloc_trash_chunk(); |
| 2312 | if (msg) |
| 2313 | tcpcheck_onerror_message(msg, check, rule, 0, desc); |
| 2314 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 2315 | goto out; |
| 2316 | |
| 2317 | wait_more_data: |
| 2318 | ret = TCPCHK_EVAL_WAIT; |
| 2319 | goto out; |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_iniths(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 2324 | { |
| 2325 | return tcpcheck_mysql_expect_packet(check, rule, 0, last_read); |
| 2326 | } |
| 2327 | |
| 2328 | static enum tcpcheck_eval_ret tcpcheck_mysql_expect_ok(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 2329 | { |
| 2330 | unsigned int hslen = 0; |
| 2331 | |
| 2332 | hslen = 4 + ((unsigned char) *b_head(&check->bi)) + |
| 2333 | (((unsigned char) *(b_peek(&check->bi, 1))) << 8) + |
| 2334 | (((unsigned char) *(b_peek(&check->bi, 2))) << 16); |
| 2335 | |
| 2336 | return tcpcheck_mysql_expect_packet(check, rule, hslen, last_read); |
| 2337 | } |
| 2338 | |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2339 | static enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 2340 | { |
| 2341 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2342 | enum healthcheck_status status; |
| 2343 | struct buffer *msg = NULL; |
| 2344 | struct ist desc = ist(NULL); |
| 2345 | unsigned short msglen = 0; |
| 2346 | |
| 2347 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 2348 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 2349 | * http://tools.ietf.org/html/rfc4511 |
| 2350 | */ |
| 2351 | /* size of LDAPMessage */ |
| 2352 | msglen = (*(b_head(&check->bi) + 1) & 0x80) ? (*(b_head(&check->bi) + 1) & 0x7f) : 0; |
| 2353 | |
| 2354 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 2355 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 2356 | * protocolOp: 0x61: bindResponse |
| 2357 | */ |
| 2358 | if ((msglen > 2) || (memcmp(b_head(&check->bi) + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 2359 | status = HCHK_STATUS_L7RSP; |
| 2360 | desc = ist("Not LDAPv3 protocol"); |
| 2361 | goto error; |
| 2362 | } |
| 2363 | |
| 2364 | /* size of bindResponse */ |
| 2365 | msglen += (*(b_head(&check->bi) + msglen + 6) & 0x80) ? (*(b_head(&check->bi) + msglen + 6) & 0x7f) : 0; |
| 2366 | |
| 2367 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 2368 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 2369 | */ |
| 2370 | if ((msglen > 4) || (memcmp(b_head(&check->bi) + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 2371 | status = HCHK_STATUS_L7RSP; |
| 2372 | desc = ist("Not LDAPv3 protocol"); |
| 2373 | goto error; |
| 2374 | } |
| 2375 | |
| 2376 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 2377 | * resultCode |
| 2378 | */ |
| 2379 | check->code = *(b_head(&check->bi) + msglen + 9); |
| 2380 | if (check->code) { |
| 2381 | status = HCHK_STATUS_L7STS; |
| 2382 | desc = ist("See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9"); |
| 2383 | goto error; |
| 2384 | } |
| 2385 | |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 2386 | set_server_check_status(check, rule->expect.ok_status, "Success"); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 2387 | |
| 2388 | out: |
| 2389 | free_trash_chunk(msg); |
| 2390 | return ret; |
| 2391 | |
| 2392 | error: |
| 2393 | ret = TCPCHK_EVAL_STOP; |
| 2394 | msg = alloc_trash_chunk(); |
| 2395 | if (msg) |
| 2396 | tcpcheck_onerror_message(msg, check, rule, 0, desc); |
| 2397 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 2398 | goto out; |
| 2399 | |
| 2400 | wait_more_data: |
| 2401 | ret = TCPCHK_EVAL_WAIT; |
| 2402 | goto out; |
| 2403 | } |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 2404 | |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2405 | |
| 2406 | static enum tcpcheck_eval_ret tcpcheck_spop_expect_agenthello(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 2407 | { |
| 2408 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2409 | enum healthcheck_status status; |
| 2410 | struct buffer *msg = NULL; |
| 2411 | struct ist desc = ist(NULL); |
| 2412 | unsigned int framesz; |
| 2413 | |
| 2414 | |
| 2415 | memcpy(&framesz, b_head(&check->bi), 4); |
| 2416 | framesz = ntohl(framesz); |
| 2417 | |
| 2418 | if (!last_read && b_data(&check->bi) < (4+framesz)) |
| 2419 | goto wait_more_data; |
| 2420 | |
| 2421 | memset(b_orig(&trash), 0, b_size(&trash)); |
| 2422 | if (spoe_handle_healthcheck_response(b_peek(&check->bi, 4), framesz, b_orig(&trash), HCHK_DESC_LEN) == -1) { |
| 2423 | status = HCHK_STATUS_L7RSP; |
| 2424 | desc = ist2(b_orig(&trash), strlen(b_orig(&trash))); |
| 2425 | goto error; |
| 2426 | } |
| 2427 | |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 2428 | set_server_check_status(check, rule->expect.ok_status, "SPOA server is ok"); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2429 | |
| 2430 | out: |
| 2431 | free_trash_chunk(msg); |
| 2432 | return ret; |
| 2433 | |
| 2434 | error: |
| 2435 | ret = TCPCHK_EVAL_STOP; |
| 2436 | msg = alloc_trash_chunk(); |
| 2437 | if (msg) |
| 2438 | tcpcheck_onerror_message(msg, check, rule, 0, desc); |
| 2439 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
| 2440 | goto out; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2441 | |
| 2442 | wait_more_data: |
| 2443 | ret = TCPCHK_EVAL_WAIT; |
| 2444 | goto out; |
| 2445 | } |
| 2446 | |
| 2447 | static enum tcpcheck_eval_ret tcpcheck_agent_expect_reply(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 2448 | { |
| 2449 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_STOP; |
| 2450 | enum healthcheck_status status = HCHK_STATUS_CHECKED; |
| 2451 | const char *hs = NULL; /* health status */ |
| 2452 | const char *as = NULL; /* admin status */ |
| 2453 | const char *ps = NULL; /* performance status */ |
| 2454 | const char *cs = NULL; /* maxconn */ |
| 2455 | const char *err = NULL; /* first error to report */ |
| 2456 | const char *wrn = NULL; /* first warning to report */ |
| 2457 | char *cmd, *p; |
| 2458 | |
| 2459 | /* We're getting an agent check response. The agent could |
| 2460 | * have been disabled in the mean time with a long check |
| 2461 | * still pending. It is important that we ignore the whole |
| 2462 | * response. |
| 2463 | */ |
| 2464 | if (!(check->state & CHK_ST_ENABLED)) |
| 2465 | goto out; |
| 2466 | |
| 2467 | /* The agent supports strings made of a single line ended by the |
| 2468 | * first CR ('\r') or LF ('\n'). This line is composed of words |
| 2469 | * delimited by spaces (' '), tabs ('\t'), or commas (','). The |
| 2470 | * line may optionally contained a description of a state change |
| 2471 | * after a sharp ('#'), which is only considered if a health state |
| 2472 | * is announced. |
| 2473 | * |
| 2474 | * Words may be composed of : |
| 2475 | * - a numeric weight suffixed by the percent character ('%'). |
| 2476 | * - a health status among "up", "down", "stopped", and "fail". |
| 2477 | * - an admin status among "ready", "drain", "maint". |
| 2478 | * |
| 2479 | * These words may appear in any order. If multiple words of the |
| 2480 | * same category appear, the last one wins. |
| 2481 | */ |
| 2482 | |
| 2483 | p = b_head(&check->bi); |
| 2484 | while (*p && *p != '\n' && *p != '\r') |
| 2485 | p++; |
| 2486 | |
| 2487 | if (!*p) { |
| 2488 | if (!last_read) |
| 2489 | goto wait_more_data; |
| 2490 | |
| 2491 | /* at least inform the admin that the agent is mis-behaving */ |
| 2492 | set_server_check_status(check, check->status, "Ignoring incomplete line from agent"); |
| 2493 | goto out; |
| 2494 | } |
| 2495 | |
| 2496 | *p = 0; |
| 2497 | cmd = b_head(&check->bi); |
| 2498 | |
| 2499 | while (*cmd) { |
| 2500 | /* look for next word */ |
| 2501 | if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') { |
| 2502 | cmd++; |
| 2503 | continue; |
| 2504 | } |
| 2505 | |
| 2506 | if (*cmd == '#') { |
| 2507 | /* this is the beginning of a health status description, |
| 2508 | * skip the sharp and blanks. |
| 2509 | */ |
| 2510 | cmd++; |
| 2511 | while (*cmd == '\t' || *cmd == ' ') |
| 2512 | cmd++; |
| 2513 | break; |
| 2514 | } |
| 2515 | |
| 2516 | /* find the end of the word so that we have a null-terminated |
| 2517 | * word between <cmd> and <p>. |
| 2518 | */ |
| 2519 | p = cmd + 1; |
| 2520 | while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',') |
| 2521 | p++; |
| 2522 | if (*p) |
| 2523 | *p++ = 0; |
| 2524 | |
| 2525 | /* first, health statuses */ |
| 2526 | if (strcasecmp(cmd, "up") == 0) { |
| 2527 | check->server->check.health = check->server->check.rise + check->server->check.fall - 1; |
| 2528 | status = HCHK_STATUS_L7OKD; |
| 2529 | hs = cmd; |
| 2530 | } |
| 2531 | else if (strcasecmp(cmd, "down") == 0) { |
| 2532 | check->server->check.health = 0; |
| 2533 | status = HCHK_STATUS_L7STS; |
| 2534 | hs = cmd; |
| 2535 | } |
| 2536 | else if (strcasecmp(cmd, "stopped") == 0) { |
| 2537 | check->server->check.health = 0; |
| 2538 | status = HCHK_STATUS_L7STS; |
| 2539 | hs = cmd; |
| 2540 | } |
| 2541 | else if (strcasecmp(cmd, "fail") == 0) { |
| 2542 | check->server->check.health = 0; |
| 2543 | status = HCHK_STATUS_L7STS; |
| 2544 | hs = cmd; |
| 2545 | } |
| 2546 | /* admin statuses */ |
| 2547 | else if (strcasecmp(cmd, "ready") == 0) { |
| 2548 | as = cmd; |
| 2549 | } |
| 2550 | else if (strcasecmp(cmd, "drain") == 0) { |
| 2551 | as = cmd; |
| 2552 | } |
| 2553 | else if (strcasecmp(cmd, "maint") == 0) { |
| 2554 | as = cmd; |
| 2555 | } |
| 2556 | /* try to parse a weight here and keep the last one */ |
| 2557 | else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) { |
| 2558 | ps = cmd; |
| 2559 | } |
| 2560 | /* try to parse a maxconn here */ |
| 2561 | else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) { |
| 2562 | cs = cmd; |
| 2563 | } |
| 2564 | else { |
| 2565 | /* keep a copy of the first error */ |
| 2566 | if (!err) |
| 2567 | err = cmd; |
| 2568 | } |
| 2569 | /* skip to next word */ |
| 2570 | cmd = p; |
| 2571 | } |
| 2572 | /* here, cmd points either to \0 or to the beginning of a |
| 2573 | * description. Skip possible leading spaces. |
| 2574 | */ |
| 2575 | while (*cmd == ' ' || *cmd == '\n') |
| 2576 | cmd++; |
| 2577 | |
| 2578 | /* First, update the admin status so that we avoid sending other |
| 2579 | * possibly useless warnings and can also update the health if |
| 2580 | * present after going back up. |
| 2581 | */ |
| 2582 | if (as) { |
| 2583 | if (strcasecmp(as, "drain") == 0) |
| 2584 | srv_adm_set_drain(check->server); |
| 2585 | else if (strcasecmp(as, "maint") == 0) |
| 2586 | srv_adm_set_maint(check->server); |
| 2587 | else |
| 2588 | srv_adm_set_ready(check->server); |
| 2589 | } |
| 2590 | |
| 2591 | /* now change weights */ |
| 2592 | if (ps) { |
| 2593 | const char *msg; |
| 2594 | |
| 2595 | msg = server_parse_weight_change_request(check->server, ps); |
| 2596 | if (!wrn || !*wrn) |
| 2597 | wrn = msg; |
| 2598 | } |
| 2599 | |
| 2600 | if (cs) { |
| 2601 | const char *msg; |
| 2602 | |
| 2603 | cs += strlen("maxconn:"); |
| 2604 | |
| 2605 | msg = server_parse_maxconn_change_request(check->server, cs); |
| 2606 | if (!wrn || !*wrn) |
| 2607 | wrn = msg; |
| 2608 | } |
| 2609 | |
| 2610 | /* and finally health status */ |
| 2611 | if (hs) { |
| 2612 | /* We'll report some of the warnings and errors we have |
| 2613 | * here. Down reports are critical, we leave them untouched. |
| 2614 | * Lack of report, or report of 'UP' leaves the room for |
| 2615 | * ERR first, then WARN. |
| 2616 | */ |
| 2617 | const char *msg = cmd; |
| 2618 | struct buffer *t; |
| 2619 | |
| 2620 | if (!*msg || status == HCHK_STATUS_L7OKD) { |
| 2621 | if (err && *err) |
| 2622 | msg = err; |
| 2623 | else if (wrn && *wrn) |
| 2624 | msg = wrn; |
| 2625 | } |
| 2626 | |
| 2627 | t = get_trash_chunk(); |
| 2628 | chunk_printf(t, "via agent : %s%s%s%s", |
| 2629 | hs, *msg ? " (" : "", |
| 2630 | msg, *msg ? ")" : ""); |
| 2631 | set_server_check_status(check, status, t->area); |
| 2632 | } |
| 2633 | else if (err && *err) { |
| 2634 | /* No status change but we'd like to report something odd. |
| 2635 | * Just report the current state and copy the message. |
| 2636 | */ |
| 2637 | chunk_printf(&trash, "agent reports an error : %s", err); |
| 2638 | set_server_check_status(check, status/*check->status*/, trash.area); |
| 2639 | } |
| 2640 | else if (wrn && *wrn) { |
| 2641 | /* No status change but we'd like to report something odd. |
| 2642 | * Just report the current state and copy the message. |
| 2643 | */ |
| 2644 | chunk_printf(&trash, "agent warns : %s", wrn); |
| 2645 | set_server_check_status(check, status/*check->status*/, trash.area); |
| 2646 | } |
| 2647 | else |
| 2648 | set_server_check_status(check, status, NULL); |
| 2649 | |
| 2650 | out: |
| 2651 | return ret; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 2652 | |
| 2653 | wait_more_data: |
| 2654 | ret = TCPCHK_EVAL_WAIT; |
| 2655 | goto out; |
| 2656 | } |
| 2657 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2658 | /* Evaluate a TCPCHK_ACT_CONNECT rule. It returns 1 to evaluate the next rule, 0 |
| 2659 | * to wait and -1 to stop the check. */ |
| 2660 | static enum tcpcheck_eval_ret tcpcheck_eval_connect(struct check *check, struct tcpcheck_rule *rule) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2661 | { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2662 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2663 | struct tcpcheck_connect *connect = &rule->connect; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 2664 | struct proxy *proxy = check->proxy; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2665 | struct server *s = check->server; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2666 | struct task *t = check->task; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2667 | struct conn_stream *cs; |
| 2668 | struct connection *conn = NULL; |
| 2669 | struct protocol *proto; |
| 2670 | struct xprt_ops *xprt; |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 2671 | int status, port; |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2672 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2673 | /* For a connect action we'll create a new connection. We may also have |
| 2674 | * to kill a previous one. But we don't want to leave *without* a |
| 2675 | * connection if we came here from the connection layer, hence with a |
| 2676 | * connection. Thus we'll proceed in the following order : |
| 2677 | * 1: close but not release previous connection (handled by the caller) |
| 2678 | * 2: try to get a new connection |
| 2679 | * 3: release and replace the old one on success |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2680 | */ |
Willy Tarreau | 449f952 | 2015-05-13 15:39:48 +0200 | [diff] [blame] | 2681 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2682 | /* 2- prepare new connection */ |
| 2683 | cs = cs_new(NULL); |
| 2684 | if (!cs) { |
| 2685 | chunk_printf(&trash, "TCPCHK error allocating connection at step %d", |
| 2686 | tcpcheck_get_step_id(check, rule)); |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 2687 | if (rule->comment) |
| 2688 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2689 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area); |
| 2690 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | b610285 | 2017-11-28 10:06:29 +0100 | [diff] [blame] | 2691 | goto out; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2692 | } |
| 2693 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2694 | /* 3- release and replace the old one on success */ |
| 2695 | if (check->cs) { |
| 2696 | if (check->wait_list.events) |
| 2697 | cs->conn->xprt->unsubscribe(cs->conn, cs->conn->xprt_ctx, |
| 2698 | check->wait_list.events, &check->wait_list); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2699 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2700 | /* We may have been scheduled to run, and the I/O handler |
| 2701 | * expects to have a cs, so remove the tasklet |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2702 | */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2703 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 2704 | cs_destroy(check->cs); |
| 2705 | } |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 2706 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2707 | tasklet_set_tid(check->wait_list.tasklet, tid); |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2708 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2709 | check->cs = cs; |
| 2710 | conn = cs->conn; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2711 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2712 | /* Maybe there were an older connection we were waiting on */ |
| 2713 | check->wait_list.events = 0; |
| 2714 | conn->target = s ? &s->obj_type : &proxy->obj_type; |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 2715 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2716 | /* no client address */ |
| 2717 | if (!sockaddr_alloc(&conn->dst)) { |
| 2718 | status = SF_ERR_RESOURCE; |
| 2719 | goto fail_check; |
| 2720 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2721 | |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 2722 | /* connect to the connect rule addr if specified, otherwise the check |
| 2723 | * addr if specified on the server. otherwise, use the server addr |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2724 | */ |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 2725 | *conn->dst = (is_addr(&connect->addr) |
| 2726 | ? connect->addr |
| 2727 | : (is_addr(&check->addr) ? check->addr : s->addr)); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2728 | proto = protocol_by_family(conn->dst->ss_family); |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2729 | |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 2730 | port = 0; |
| 2731 | if (!port && connect->port) |
| 2732 | port = connect->port; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 2733 | if (!port && connect->port_expr) { |
| 2734 | struct sample *smp; |
| 2735 | |
| 2736 | smp = sample_fetch_as_type(check->proxy, check->sess, NULL, |
| 2737 | SMP_OPT_DIR_REQ | SMP_OPT_FINAL, |
| 2738 | connect->port_expr, SMP_T_SINT); |
| 2739 | if (smp) |
| 2740 | port = smp->data.u.sint; |
| 2741 | } |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 2742 | if (!port && is_inet_addr(&connect->addr)) |
| 2743 | port = get_host_port(&connect->addr); |
| 2744 | if (!port && check->port) |
| 2745 | port = check->port; |
| 2746 | if (!port && is_inet_addr(&check->addr)) |
| 2747 | port = get_host_port(&check->addr); |
| 2748 | if (!port) |
| 2749 | port = s->svc_port; |
| 2750 | set_host_port(conn->dst, port); |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2751 | |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2752 | xprt = ((connect->options & TCPCHK_OPT_SSL) |
| 2753 | ? xprt_get(XPRT_SSL) |
| 2754 | : ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) ? check->xprt : xprt_get(XPRT_RAW))); |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2755 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2756 | conn_prepare(conn, proto, xprt); |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 2757 | if (conn_install_mux(conn, &mux_pt_ops, cs, proxy, check->sess) < 0) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2758 | status = SF_ERR_RESOURCE; |
| 2759 | goto fail_check; |
| 2760 | } |
| 2761 | cs_attach(cs, check, &check_conn_cb); |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 2762 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2763 | status = SF_ERR_INTERNAL; |
| 2764 | if (proto && proto->connect) { |
| 2765 | struct tcpcheck_rule *next; |
| 2766 | int flags = CONNECT_HAS_DATA; |
Olivier Houchard | ff1e9f3 | 2019-09-20 17:18:35 +0200 | [diff] [blame] | 2767 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2768 | next = get_next_tcpcheck_rule(check->tcpcheck_rules, rule); |
| 2769 | if (!next || next->action != TCPCHK_ACT_EXPECT) |
| 2770 | flags |= CONNECT_DELACK_ALWAYS; |
| 2771 | status = proto->connect(conn, flags); |
| 2772 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2773 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2774 | #ifdef USE_OPENSSL |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2775 | if (status == SF_ERR_NONE) { |
| 2776 | if (connect->sni) |
| 2777 | ssl_sock_set_servername(conn, connect->sni); |
| 2778 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.sni) |
| 2779 | ssl_sock_set_servername(conn, s->check.sni); |
| 2780 | |
| 2781 | if (connect->alpn) |
| 2782 | ssl_sock_set_alpn(conn, (unsigned char *)connect->alpn, connect->alpn_len); |
| 2783 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.alpn_str) |
| 2784 | ssl_sock_set_alpn(conn, (unsigned char *)s->check.alpn_str, s->check.alpn_len); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2785 | } |
Christopher Faulet | 79b31d4 | 2020-03-30 13:00:05 +0200 | [diff] [blame] | 2786 | #endif |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2787 | if ((connect->options & TCPCHK_OPT_SOCKS4) && (s->flags & SRV_F_SOCKS4_PROXY)) { |
| 2788 | conn->send_proxy_ofs = 1; |
| 2789 | conn->flags |= CO_FL_SOCKS4; |
| 2790 | } |
| 2791 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.via_socks4 && (s->flags & SRV_F_SOCKS4_PROXY)) { |
| 2792 | conn->send_proxy_ofs = 1; |
| 2793 | conn->flags |= CO_FL_SOCKS4; |
| 2794 | } |
| 2795 | |
| 2796 | if (connect->options & TCPCHK_OPT_SEND_PROXY) { |
| 2797 | conn->send_proxy_ofs = 1; |
| 2798 | conn->flags |= CO_FL_SEND_PROXY; |
| 2799 | } |
| 2800 | else if ((connect->options & TCPCHK_OPT_DEFAULT_CONNECT) && s->check.send_proxy && !(check->state & CHK_ST_AGENT)) { |
| 2801 | conn->send_proxy_ofs = 1; |
| 2802 | conn->flags |= CO_FL_SEND_PROXY; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2803 | } |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 2804 | |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 2805 | if (conn_ctrl_ready(conn) && (connect->options & TCPCHK_OPT_LINGER)) { |
| 2806 | /* Some servers don't like reset on close */ |
| 2807 | fdtab[cs->conn->handle.fd].linger_risk = 0; |
| 2808 | } |
| 2809 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2810 | if (conn_ctrl_ready(conn) && (conn->flags & (CO_FL_SEND_PROXY | CO_FL_SOCKS4))) { |
| 2811 | if (xprt_add_hs(conn) < 0) |
| 2812 | status = SF_ERR_RESOURCE; |
| 2813 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2814 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2815 | fail_check: |
| 2816 | /* It can return one of : |
| 2817 | * - SF_ERR_NONE if everything's OK |
| 2818 | * - SF_ERR_SRVTO if there are no more servers |
| 2819 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 2820 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 2821 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 2822 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 2823 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 2824 | * Note that we try to prevent the network stack from sending the ACK during the |
| 2825 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 2826 | */ |
| 2827 | switch (status) { |
| 2828 | case SF_ERR_NONE: |
| 2829 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2830 | * to establish but only when timeout.check is set as it may be |
| 2831 | * to short for a full check otherwise |
| 2832 | */ |
| 2833 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2834 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2835 | if (proxy->timeout.check && proxy->timeout.connect) { |
| 2836 | int t_con = tick_add(now_ms, proxy->timeout.connect); |
| 2837 | t->expire = tick_first(t->expire, t_con); |
| 2838 | } |
| 2839 | break; |
| 2840 | case SF_ERR_SRVTO: /* ETIMEDOUT */ |
| 2841 | case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 2842 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s", |
| 2843 | tcpcheck_get_step_id(check, rule), strerror(errno)); |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 2844 | if (rule->comment) |
| 2845 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2846 | set_server_check_status(check, HCHK_STATUS_L4CON, trash.area); |
| 2847 | ret = TCPCHK_EVAL_STOP; |
| 2848 | goto out; |
| 2849 | case SF_ERR_PRXCOND: |
| 2850 | case SF_ERR_RESOURCE: |
| 2851 | case SF_ERR_INTERNAL: |
| 2852 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d", |
| 2853 | tcpcheck_get_step_id(check, rule)); |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 2854 | if (rule->comment) |
| 2855 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2856 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area); |
| 2857 | ret = TCPCHK_EVAL_STOP; |
| 2858 | goto out; |
| 2859 | } |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2860 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2861 | /* don't do anything until the connection is established */ |
| 2862 | if (conn->flags & CO_FL_WAIT_XPRT) { |
| 2863 | ret = TCPCHK_EVAL_WAIT; |
| 2864 | goto out; |
| 2865 | } |
Willy Tarreau | be37315 | 2018-09-06 11:45:30 +0200 | [diff] [blame] | 2866 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2867 | out: |
| 2868 | if (conn && check->result == CHK_RES_FAILED) |
| 2869 | conn->flags |= CO_FL_ERROR; |
| 2870 | return ret; |
| 2871 | } |
Willy Tarreau | 2ab5c38 | 2019-07-17 18:48:07 +0200 | [diff] [blame] | 2872 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2873 | /* Evaluate a TCPCHK_ACT_SEND rule. It returns 1 to evaluate the next rule, 0 |
| 2874 | * to wait and -1 to stop the check. */ |
| 2875 | static enum tcpcheck_eval_ret tcpcheck_eval_send(struct check *check, struct tcpcheck_rule *rule) |
| 2876 | { |
| 2877 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 2878 | struct tcpcheck_send *send = &rule->send; |
| 2879 | struct conn_stream *cs = check->cs; |
| 2880 | struct connection *conn = cs_conn(cs); |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 2881 | struct buffer *tmp = NULL; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2882 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2883 | /* reset the read & write buffer */ |
| 2884 | b_reset(&check->bi); |
| 2885 | b_reset(&check->bo); |
Gaetan Rivet | 08fdcb3 | 2020-02-28 11:04:21 +0100 | [diff] [blame] | 2886 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2887 | switch (send->type) { |
| 2888 | case TCPCHK_SEND_STRING: |
| 2889 | case TCPCHK_SEND_BINARY: |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 2890 | if (istlen(send->data) >= b_size(&check->bo)) { |
| 2891 | chunk_printf(&trash, "tcp-check send : string too large (%u) for buffer size (%u) at step %d", |
| 2892 | (unsigned int)istlen(send->data), (unsigned int)b_size(&check->bo), |
| 2893 | tcpcheck_get_step_id(check, rule)); |
| 2894 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2895 | ret = TCPCHK_EVAL_STOP; |
| 2896 | goto out; |
| 2897 | } |
| 2898 | b_putist(&check->bo, send->data); |
| 2899 | break; |
| 2900 | case TCPCHK_SEND_STRING_LF: |
| 2901 | check->bo.data = sess_build_logline(check->sess, NULL, b_orig(&check->bo), b_size(&check->bo), &rule->send.fmt); |
| 2902 | if (!b_data(&check->bo)) |
| 2903 | goto out; |
| 2904 | break; |
| 2905 | case TCPCHK_SEND_BINARY_LF: |
| 2906 | tmp = alloc_trash_chunk(); |
| 2907 | if (!tmp) |
| 2908 | goto error_lf; |
| 2909 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &rule->send.fmt); |
| 2910 | if (!b_data(tmp)) |
| 2911 | goto out; |
| 2912 | tmp->area[tmp->data] = '\0'; |
| 2913 | b_set_data(&check->bo, b_size(&check->bo)); |
| 2914 | if (parse_binary(b_orig(tmp), &check->bo.area, (int *)&check->bo.data, NULL) == 0) |
| 2915 | goto error_lf; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2916 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2917 | case TCPCHK_SEND_HTTP: { |
| 2918 | struct ist meth, uri, vsn; |
| 2919 | |
| 2920 | meth = ((send->http.meth.meth == HTTP_METH_OTHER) |
| 2921 | ? ist2(send->http.meth.str.area, send->http.meth.str.data) |
| 2922 | : http_known_methods[send->http.meth.meth]); |
| 2923 | uri = (isttest(send->http.uri) ? send->http.uri : ist("/")); // TODO: handle uri_fmt |
| 2924 | vsn = (isttest(send->http.vsn) ? send->http.vsn : ist("HTTP/1.0")); |
| 2925 | |
| 2926 | chunk_istcat(&check->bo, meth); |
| 2927 | check->bo.area[check->bo.data++] = ' '; |
| 2928 | chunk_istcat(&check->bo, uri); |
| 2929 | check->bo.area[check->bo.data++] = ' '; |
| 2930 | chunk_istcat(&check->bo, vsn); |
| 2931 | chunk_istcat(&check->bo, ist("\r\n")); |
| 2932 | chunk_istcat(&check->bo, ist("Connection: close\r\n")); |
| 2933 | if (isttest(send->http.body)) { |
| 2934 | // TODO: handle body_fmt |
| 2935 | chunk_appendf(&check->bo, "Content-Length: %s\r\n", ultoa(istlen(send->http.body))); |
| 2936 | } |
| 2937 | if (check->proxy->options2 & PR_O2_CHK_SNDST) { |
| 2938 | trash.data = httpchk_build_status_header(check->server, b_orig(&trash), b_size(&trash)); |
| 2939 | chunk_cat(&check->bo, &trash); |
| 2940 | } |
| 2941 | if (!LIST_ISEMPTY(&send->http.hdrs)) { |
| 2942 | struct tcpcheck_http_hdr *hdr; |
| 2943 | |
| 2944 | tmp = alloc_trash_chunk(); |
| 2945 | if (!tmp) |
| 2946 | goto error_lf; |
| 2947 | list_for_each_entry(hdr, &send->http.hdrs, list) { |
| 2948 | chunk_reset(tmp); |
| 2949 | tmp->data = sess_build_logline(check->sess, NULL, b_orig(tmp), b_size(tmp), &hdr->value); |
| 2950 | if (!b_data(tmp)) |
| 2951 | continue; |
| 2952 | chunk_istcat(&check->bo, hdr->name); |
| 2953 | check->bo.area[check->bo.data++] = ' '; |
| 2954 | chunk_cat(&check->bo, tmp); |
| 2955 | chunk_istcat(&check->bo, ist("\r\n")); |
| 2956 | } |
| 2957 | } |
| 2958 | chunk_istcat(&check->bo, ist("\r\n")); |
| 2959 | if (isttest(send->http.body)) { |
| 2960 | // TODO: handle body_fmt |
| 2961 | chunk_istcat(&check->bo, send->http.body); |
| 2962 | } |
| 2963 | break; |
| 2964 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2965 | case TCPCHK_SEND_UNDEF: |
| 2966 | /* Should never happen. */ |
| 2967 | ret = TCPCHK_EVAL_STOP; |
| 2968 | goto out; |
| 2969 | }; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 2970 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2971 | if (conn->mux->snd_buf(cs, &check->bo, b_data(&check->bo), 0) <= 0) { |
| 2972 | ret = TCPCHK_EVAL_WAIT; |
| 2973 | if ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR)) |
| 2974 | ret = TCPCHK_EVAL_STOP; |
| 2975 | goto out; |
| 2976 | } |
| 2977 | if (b_data(&check->bo)) { |
| 2978 | cs->conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 2979 | ret = TCPCHK_EVAL_WAIT; |
| 2980 | goto out; |
| 2981 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2982 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2983 | out: |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 2984 | free_trash_chunk(tmp); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 2985 | return ret; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 2986 | |
| 2987 | error_lf: |
| 2988 | chunk_printf(&trash, "tcp-check send : failed to build log-format string at step %d", |
| 2989 | tcpcheck_get_step_id(check, rule)); |
| 2990 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 2991 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 2992 | goto out; |
| 2993 | |
| 2994 | } |
| 2995 | |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 2996 | /* */ |
| 2997 | static enum tcpcheck_eval_ret tcpcheck_eval_recv(struct check *check, struct tcpcheck_rule *rule) |
| 2998 | { |
| 2999 | struct conn_stream *cs = check->cs; |
| 3000 | struct connection *conn = cs_conn(cs); |
| 3001 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 3002 | size_t max, read, cur_read = 0; |
| 3003 | int is_empty; |
| 3004 | int read_poll = MAX_READ_POLL_LOOPS; |
| 3005 | |
| 3006 | if (check->wait_list.events & SUB_RETRY_RECV) |
| 3007 | goto wait_more_data; |
| 3008 | |
| 3009 | if (cs->flags & CS_FL_EOS) |
| 3010 | goto end_recv; |
| 3011 | |
| 3012 | /* errors on the connection and the conn-stream were already checked */ |
| 3013 | |
| 3014 | /* prepare to detect if the mux needs more room */ |
| 3015 | cs->flags &= ~CS_FL_WANT_ROOM; |
| 3016 | |
| 3017 | while ((cs->flags & CS_FL_RCV_MORE) || |
| 3018 | (!(conn->flags & CO_FL_ERROR) && !(cs->flags & (CS_FL_ERROR|CS_FL_EOS)))) { |
| 3019 | max = b_room(&check->bi); |
| 3020 | read = conn->mux->rcv_buf(cs, &check->bi, max, 0); |
| 3021 | cur_read += read; |
| 3022 | if (!read || |
| 3023 | (cs->flags & CS_FL_WANT_ROOM) || |
| 3024 | (--read_poll <= 0) || |
| 3025 | (read < max && read >= global.tune.recv_enough)) |
| 3026 | break; |
| 3027 | } |
| 3028 | |
| 3029 | end_recv: |
| 3030 | is_empty = !b_data(&check->bi); |
| 3031 | if (is_empty && ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR))) { |
| 3032 | /* Report network errors only if we got no other data. Otherwise |
| 3033 | * we'll let the upper layers decide whether the response is OK |
| 3034 | * or not. It is very common that an RST sent by the server is |
| 3035 | * reported as an error just after the last data chunk. |
| 3036 | */ |
| 3037 | goto stop; |
| 3038 | } |
| 3039 | if (!cur_read) { |
| 3040 | if (!(cs->flags & (CS_FL_WANT_ROOM|CS_FL_ERROR|CS_FL_EOS))) { |
| 3041 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
| 3042 | goto wait_more_data; |
| 3043 | } |
| 3044 | if (is_empty) { |
| 3045 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 3046 | tcpcheck_get_step_id(check, rule)); |
| 3047 | if (rule->comment) |
| 3048 | chunk_appendf(&trash, " comment: '%s'", rule->comment); |
| 3049 | set_server_check_status(check, rule->expect.err_status, trash.area); |
| 3050 | goto stop; |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | out: |
| 3055 | return ret; |
| 3056 | |
| 3057 | stop: |
| 3058 | ret = TCPCHK_EVAL_STOP; |
| 3059 | goto out; |
| 3060 | |
| 3061 | wait_more_data: |
| 3062 | ret = TCPCHK_EVAL_WAIT; |
| 3063 | goto out; |
| 3064 | } |
| 3065 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3066 | static enum tcpcheck_eval_ret tcpcheck_eval_expect_http(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 3067 | { |
| 3068 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 3069 | struct tcpcheck_expect *expect = &rule->expect; |
| 3070 | struct buffer *msg = NULL; |
| 3071 | enum healthcheck_status status; |
| 3072 | struct ist desc = ist(NULL); |
| 3073 | char *body; |
| 3074 | size_t body_len; |
| 3075 | int match, inverse; |
| 3076 | |
| 3077 | last_read |= b_full(&check->bi); |
| 3078 | |
| 3079 | /* Must at least receive the status line (HTTP/1.X XXX.) */ |
| 3080 | if (!last_read && b_data(&check->bi) < 13) |
| 3081 | goto wait_more_data; |
| 3082 | |
| 3083 | /* Check if the server speaks HTTP 1.X */ |
| 3084 | if (b_data(&check->bi) < 13 || |
| 3085 | memcmp(b_head(&check->bi), "HTTP/1.", 7) != 0 || |
| 3086 | (*b_peek(&check->bi, 12) != ' ' && *b_peek(&check->bi, 12) != '\r') || |
| 3087 | !isdigit((unsigned char) *b_peek(&check->bi, 9)) || !isdigit((unsigned char) *b_peek(&check->bi, 10)) || |
| 3088 | !isdigit((unsigned char) *b_peek(&check->bi, 11))) { |
| 3089 | status = HCHK_STATUS_L7RSP; |
| 3090 | desc = ist2(b_head(&check->bi), my_memcspn(b_head(&check->bi), b_data(&check->bi), "\r\n", 2)); |
| 3091 | goto error; |
| 3092 | } |
| 3093 | |
| 3094 | check->code = strl2uic(b_peek(&check->bi, 9), 3); |
| 3095 | |
| 3096 | if (check->server && |
| 3097 | (check->server->proxy->options & PR_O_DISABLE404) && |
| 3098 | (check->server->next_state != SRV_ST_STOPPED) && |
| 3099 | (check->code == 404)) { |
| 3100 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 3101 | goto out; |
| 3102 | } |
| 3103 | |
| 3104 | inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV); |
| 3105 | /* Make GCC happy ; initialize match to a failure state. */ |
| 3106 | match = inverse; |
| 3107 | |
| 3108 | switch (expect->type) { |
| 3109 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 3110 | match = my_memmem(b_peek(&check->bi, 9), 3, expect->data.ptr, istlen(expect->data)) != NULL; |
| 3111 | |
| 3112 | /* Set status and description in case of error */ |
| 3113 | status = HCHK_STATUS_L7STS; |
| 3114 | desc = ist2(b_peek(&check->bi, 12), my_memcspn(b_peek(&check->bi, 12), b_data(&check->bi) - 12, "\r\n", 2)); |
| 3115 | break; |
| 3116 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 3117 | match = regex_exec2(expect->regex, b_peek(&check->bi, 9), 3); |
| 3118 | |
| 3119 | /* Set status and description in case of error */ |
| 3120 | status = HCHK_STATUS_L7STS; |
| 3121 | desc = ist2(b_peek(&check->bi, 12), my_memcspn(b_peek(&check->bi, 12), b_data(&check->bi) - 12, "\r\n", 2)); |
| 3122 | break; |
| 3123 | case TCPCHK_EXPECT_HTTP_BODY: |
| 3124 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
| 3125 | body = (char *)my_memmem(b_head(&check->bi), b_data(&check->bi), "\r\n\r\n", 4); |
| 3126 | if (!body) { |
| 3127 | if (!last_read) |
| 3128 | goto wait_more_data; |
| 3129 | |
| 3130 | status = HCHK_STATUS_L7RSP; |
| 3131 | desc = ist("HTTP content check could not find a response body"); |
| 3132 | goto error; |
| 3133 | } |
| 3134 | body += 4; |
| 3135 | body_len = b_tail(&check->bi) - body; |
| 3136 | |
| 3137 | if (!last_read && |
| 3138 | ((expect->type == TCPCHK_EXPECT_HTTP_BODY && body_len < istlen(expect->data)) || |
| 3139 | (expect->min_recv > 0 && body_len < expect->min_recv))) { |
| 3140 | ret = TCPCHK_EVAL_WAIT; |
| 3141 | goto out; |
| 3142 | } |
| 3143 | |
| 3144 | if (expect->type ==TCPCHK_EXPECT_HTTP_BODY) |
| 3145 | match = my_memmem(body, body_len, expect->data.ptr, istlen(expect->data)) != NULL; |
| 3146 | else |
| 3147 | match = regex_exec2(expect->regex, body, body_len); |
| 3148 | |
| 3149 | /* Set status and description in case of error */ |
| 3150 | status = HCHK_STATUS_L7RSP; |
| 3151 | desc = (inverse |
| 3152 | ? ist("HTTP check matched unwanted content") |
| 3153 | : ist("HTTP content check did not match")); |
| 3154 | break; |
| 3155 | |
| 3156 | default: |
| 3157 | /* should never happen */ |
| 3158 | status = HCHK_STATUS_L7RSP; |
| 3159 | goto error; |
| 3160 | } |
| 3161 | |
| 3162 | /* Wait for more data on mismatch only if no minimum is defined (-1), |
| 3163 | * otherwise the absence of match is already conclusive. |
| 3164 | */ |
| 3165 | if (!match && !last_read && (expect->min_recv == -1)) { |
| 3166 | ret = TCPCHK_EVAL_WAIT; |
| 3167 | goto out; |
| 3168 | } |
| 3169 | |
| 3170 | if (!(match ^ inverse)) |
| 3171 | goto error; |
| 3172 | |
| 3173 | out: |
| 3174 | free_trash_chunk(msg); |
| 3175 | return ret; |
| 3176 | |
| 3177 | error: |
| 3178 | ret = TCPCHK_EVAL_STOP; |
| 3179 | msg = alloc_trash_chunk(); |
| 3180 | if (msg) |
| 3181 | tcpcheck_onerror_message(msg, check, rule, 0, desc); |
| 3182 | set_server_check_status(check, status, (msg ? b_head(msg) : NULL)); |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3183 | goto out; |
| 3184 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3185 | wait_more_data: |
| 3186 | ret = TCPCHK_EVAL_WAIT; |
| 3187 | goto out; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3188 | } |
Gaetan Rivet | f8ba677 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3189 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3190 | /* Evaluate a TCPCHK_ACT_EXPECT rule. It returns 1 to evaluate the next rule, 0 |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3191 | * to wait and -1 to stop the check. |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3192 | */ |
| 3193 | static enum tcpcheck_eval_ret tcpcheck_eval_expect(struct check *check, struct tcpcheck_rule *rule, int last_read) |
| 3194 | { |
| 3195 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 3196 | struct tcpcheck_expect *expect = &rule->expect; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 3197 | struct buffer *msg = NULL; |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 3198 | int match, inverse; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 3199 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3200 | last_read |= b_full(&check->bi); |
| 3201 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3202 | /* The current expect might need more data than the previous one, check again |
| 3203 | * that the minimum amount data required to match is respected. |
| 3204 | */ |
| 3205 | if (!last_read) { |
| 3206 | if ((expect->type == TCPCHK_EXPECT_STRING || expect->type == TCPCHK_EXPECT_BINARY) && |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 3207 | (b_data(&check->bi) < istlen(expect->data))) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3208 | ret = TCPCHK_EVAL_WAIT; |
| 3209 | goto out; |
| 3210 | } |
| 3211 | if (expect->min_recv > 0 && (b_data(&check->bi) < expect->min_recv)) { |
| 3212 | ret = TCPCHK_EVAL_WAIT; |
| 3213 | goto out; |
| 3214 | } |
| 3215 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 3216 | |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 3217 | inverse = !!(expect->flags & TCPCHK_EXPT_FL_INV); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3218 | /* Make GCC happy ; initialize match to a failure state. */ |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 3219 | match = inverse; |
Willy Tarreau | f2c8735 | 2015-05-13 12:08:21 +0200 | [diff] [blame] | 3220 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3221 | switch (expect->type) { |
| 3222 | case TCPCHK_EXPECT_STRING: |
| 3223 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 3224 | match = my_memmem(b_head(&check->bi), b_data(&check->bi), expect->data.ptr, istlen(expect->data)) != NULL; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3225 | break; |
| 3226 | case TCPCHK_EXPECT_REGEX: |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 3227 | if (expect->flags & TCPCHK_EXPT_FL_CAP) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3228 | match = regex_exec_match2(expect->regex, b_head(&check->bi), MIN(b_data(&check->bi), b_size(&check->bi)-1), |
| 3229 | MAX_MATCH, pmatch, 0); |
| 3230 | else |
| 3231 | match = regex_exec2(expect->regex, b_head(&check->bi), MIN(b_data(&check->bi), b_size(&check->bi)-1)); |
| 3232 | break; |
Willy Tarreau | 7df8ca6 | 2019-07-15 10:57:51 +0200 | [diff] [blame] | 3233 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3234 | case TCPCHK_EXPECT_REGEX_BINARY: |
| 3235 | chunk_reset(&trash); |
| 3236 | dump_binary(&trash, b_head(&check->bi), b_data(&check->bi)); |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 3237 | if (expect->flags & TCPCHK_EXPT_FL_CAP) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3238 | match = regex_exec_match2(expect->regex, b_head(&trash), MIN(b_data(&trash), b_size(&trash)-1), |
| 3239 | MAX_MATCH, pmatch, 0); |
| 3240 | else |
| 3241 | match = regex_exec2(expect->regex, b_head(&trash), MIN(b_data(&trash), b_size(&trash)-1)); |
| 3242 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 3243 | case TCPCHK_EXPECT_CUSTOM: |
| 3244 | if (expect->custom) |
| 3245 | ret = expect->custom(check, rule, last_read); |
| 3246 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3247 | default: |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3248 | /* Should never happen. */ |
| 3249 | ret = TCPCHK_EVAL_STOP; |
| 3250 | goto out; |
| 3251 | } |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3252 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 3253 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3254 | /* Wait for more data on mismatch only if no minimum is defined (-1), |
| 3255 | * otherwise the absence of match is already conclusive. |
| 3256 | */ |
| 3257 | if (!match && !last_read && (expect->min_recv == -1)) { |
| 3258 | ret = TCPCHK_EVAL_WAIT; |
| 3259 | goto out; |
| 3260 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3261 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3262 | /* Result as expected, next rule. */ |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 3263 | if (match ^ inverse) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3264 | goto out; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3265 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 3266 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3267 | /* From this point on, we matched something we did not want, this is an error state. */ |
| 3268 | ret = TCPCHK_EVAL_STOP; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 3269 | msg = alloc_trash_chunk(); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 3270 | if (msg) |
| 3271 | tcpcheck_onerror_message(msg, check, rule, match, ist(NULL)); |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 3272 | set_server_check_status(check, expect->err_status, (msg ? b_head(msg) : NULL)); |
Christopher Faulet | 206368d | 2020-04-03 14:51:06 +0200 | [diff] [blame] | 3273 | free_trash_chunk(msg); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3274 | ret = TCPCHK_EVAL_STOP; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3275 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3276 | out: |
| 3277 | return ret; |
| 3278 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3279 | |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3280 | /* Evaluate a TCPCHK_ACT_ACTION_KW rule. It returns 1 to evaluate the next rule, 0 |
| 3281 | * to wait and -1 to stop the check. |
| 3282 | */ |
| 3283 | static enum tcpcheck_eval_ret tcpcheck_eval_action_kw(struct check *check, struct tcpcheck_rule *rule) |
| 3284 | { |
| 3285 | enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE; |
| 3286 | struct act_rule *act_rule; |
| 3287 | enum act_return act_ret; |
| 3288 | |
| 3289 | act_rule =rule->action_kw.rule; |
| 3290 | act_ret = act_rule->action_ptr(act_rule, check->proxy, check->sess, NULL, 0); |
| 3291 | if (act_ret != ACT_RET_CONT) { |
| 3292 | chunk_printf(&trash, "TCPCHK ACTION unexpected result at step %d\n", |
| 3293 | tcpcheck_get_step_id(check, rule)); |
| 3294 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.area); |
| 3295 | ret = TCPCHK_EVAL_STOP; |
| 3296 | } |
| 3297 | |
| 3298 | return ret; |
| 3299 | } |
| 3300 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3301 | /* proceed with next steps for the TCP checks <check>. Note that this is called |
| 3302 | * both from the connection's wake() callback and from the check scheduling task. |
| 3303 | * It returns 0 on normal cases, or <0 if a close() has happened on an existing |
| 3304 | * connection, presenting the risk of an fd replacement. |
| 3305 | * |
| 3306 | * Please do NOT place any return statement in this function and only leave |
| 3307 | * via the out_end_tcpcheck label after setting retcode. |
| 3308 | */ |
| 3309 | static int tcpcheck_main(struct check *check) |
| 3310 | { |
| 3311 | struct tcpcheck_rule *rule; |
| 3312 | struct conn_stream *cs = check->cs; |
| 3313 | struct connection *conn = cs_conn(cs); |
| 3314 | int must_read = 1, last_read = 0; |
| 3315 | int ret, retcode = 0; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3316 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3317 | /* here, we know that the check is complete or that it failed */ |
| 3318 | if (check->result != CHK_RES_UNKNOWN) |
| 3319 | goto out_end_tcpcheck; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3320 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3321 | /* 1- check for connection error, if any */ |
| 3322 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
| 3323 | goto out_end_tcpcheck; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3324 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3325 | /* 2- check if we are waiting for the connection establishment. It only |
| 3326 | * happens during TCPCHK_ACT_CONNECT. */ |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3327 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 3328 | rule = LIST_NEXT(&check->current_step->list, typeof(rule), list); |
| 3329 | if (conn && (conn->flags & CO_FL_WAIT_XPRT)) { |
| 3330 | if (rule->action == TCPCHK_ACT_SEND) |
| 3331 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 3332 | else if (rule->action == TCPCHK_ACT_EXPECT) |
| 3333 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3334 | goto out; |
| 3335 | } |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | /* 3- check for pending outgoing data. It only happens during |
| 3339 | * TCPCHK_ACT_SEND. */ |
| 3340 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
| 3341 | if (conn && b_data(&check->bo)) { |
| 3342 | ret = conn->mux->snd_buf(cs, &check->bo, b_data(&check->bo), 0); |
| 3343 | if (ret <= 0) { |
| 3344 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
| 3345 | goto out_end_tcpcheck; |
| 3346 | goto out; |
| 3347 | } |
| 3348 | if (b_data(&check->bo)) { |
| 3349 | cs->conn->mux->subscribe(cs, SUB_RETRY_SEND, &check->wait_list); |
| 3350 | goto out; |
| 3351 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3352 | } |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3353 | rule = LIST_NEXT(&check->current_step->list, typeof(rule), list); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3354 | } |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3355 | |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3356 | /* 4- check if a rule must be resume. It happens if check->current_step |
| 3357 | * is defined. */ |
| 3358 | else if (check->current_step) |
| 3359 | rule = check->current_step; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3360 | |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3361 | /* 5- It is the first evaluation. We must create a session and preset |
| 3362 | * tcp-check variables */ |
| 3363 | else { |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3364 | struct tcpcheck_var *var; |
| 3365 | |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 3366 | /* First evaluation, create a session */ |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 3367 | check->sess = session_new(&checks_fe, NULL, (check->server ? &check->server->obj_type : NULL)); |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 3368 | if (!check->sess) { |
| 3369 | chunk_printf(&trash, "TCPCHK error allocating check session"); |
| 3370 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.area); |
| 3371 | goto out_end_tcpcheck; |
| 3372 | } |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 3373 | vars_init(&check->vars, SCOPE_CHECK); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3374 | rule = LIST_NEXT(check->tcpcheck_rules->list, typeof(rule), list); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3375 | |
| 3376 | /* Preset tcp-check variables */ |
| 3377 | list_for_each_entry(var, &check->tcpcheck_rules->preset_vars, list) { |
| 3378 | struct sample smp; |
| 3379 | |
| 3380 | memset(&smp, 0, sizeof(smp)); |
| 3381 | smp_set_owner(&smp, check->proxy, check->sess, NULL, SMP_OPT_FINAL); |
| 3382 | smp.data = var->data; |
| 3383 | vars_set_by_name_ifexist(var->name.ptr, var->name.len, &smp); |
| 3384 | } |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 3385 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3386 | |
Christopher Faulet | 370e0f1 | 2020-04-16 09:52:42 +0200 | [diff] [blame] | 3387 | /* Now evaluate the tcp-check rules */ |
| 3388 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3389 | list_for_each_entry_from(rule, check->tcpcheck_rules->list, list) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3390 | enum tcpcheck_eval_ret eval_ret; |
Willy Tarreau | f2c8735 | 2015-05-13 12:08:21 +0200 | [diff] [blame] | 3391 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3392 | check->code = 0; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3393 | switch (rule->action) { |
| 3394 | case TCPCHK_ACT_CONNECT: |
| 3395 | check->current_step = rule; |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 3396 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3397 | /* close but not release yet previous connection */ |
| 3398 | if (check->cs) { |
| 3399 | cs_close(check->cs); |
| 3400 | retcode = -1; /* do not reuse the fd in the caller! */ |
Gaetan Rivet | 9e47fa4 | 2020-02-26 15:59:22 +0100 | [diff] [blame] | 3401 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3402 | eval_ret = tcpcheck_eval_connect(check, rule); |
| 3403 | must_read = 1; last_read = 0; |
| 3404 | break; |
| 3405 | case TCPCHK_ACT_SEND: |
| 3406 | check->current_step = rule; |
| 3407 | eval_ret = tcpcheck_eval_send(check, rule); |
| 3408 | must_read = 1; |
| 3409 | break; |
| 3410 | case TCPCHK_ACT_EXPECT: |
| 3411 | check->current_step = rule; |
| 3412 | if (must_read) { |
| 3413 | if (check->proxy->timeout.check) |
| 3414 | check->task->expire = tick_add_ifset(now_ms, check->proxy->timeout.check); |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 3415 | |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3416 | eval_ret = tcpcheck_eval_recv(check, rule); |
| 3417 | if (eval_ret == TCPCHK_EVAL_STOP) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3418 | goto out_end_tcpcheck; |
Christopher Faulet | f9585d8 | 2020-04-16 13:25:58 +0200 | [diff] [blame] | 3419 | else if (eval_ret == TCPCHK_EVAL_WAIT) |
| 3420 | goto out; |
| 3421 | last_read = ((conn->flags & CO_FL_ERROR) || (cs->flags & (CS_FL_ERROR|CS_FL_EOS))); |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3422 | must_read = 0; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3423 | } |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3424 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3425 | eval_ret = ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK |
| 3426 | ? tcpcheck_eval_expect_http(check, rule, last_read) |
| 3427 | : tcpcheck_eval_expect(check, rule, last_read)); |
| 3428 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3429 | if (eval_ret == TCPCHK_EVAL_WAIT) { |
| 3430 | check->current_step = rule->expect.head; |
| 3431 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &check->wait_list); |
Gaetan Rivet | 9dcb09f | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3432 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3433 | break; |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3434 | case TCPCHK_ACT_ACTION_KW: |
| 3435 | /* Don't update the current step */ |
| 3436 | eval_ret = tcpcheck_eval_action_kw(check, rule); |
| 3437 | break; |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3438 | default: |
| 3439 | /* Otherwise, just go to the next one and don't update |
| 3440 | * the current step |
| 3441 | */ |
| 3442 | eval_ret = TCPCHK_EVAL_CONTINUE; |
| 3443 | break; |
| 3444 | } |
Baptiste Assmann | 248f117 | 2018-03-01 21:49:01 +0100 | [diff] [blame] | 3445 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3446 | switch (eval_ret) { |
| 3447 | case TCPCHK_EVAL_CONTINUE: |
| 3448 | break; |
| 3449 | case TCPCHK_EVAL_WAIT: |
| 3450 | goto out; |
| 3451 | case TCPCHK_EVAL_STOP: |
| 3452 | goto out_end_tcpcheck; |
Baptiste Assmann | 248f117 | 2018-03-01 21:49:01 +0100 | [diff] [blame] | 3453 | } |
Baptiste Assmann | 248f117 | 2018-03-01 21:49:01 +0100 | [diff] [blame] | 3454 | } |
| 3455 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3456 | /* All rules was evaluated */ |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3457 | if (check->current_step) { |
| 3458 | rule = check->current_step; |
| 3459 | |
| 3460 | if (rule->action == TCPCHK_ACT_EXPECT) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3461 | struct buffer *msg; |
| 3462 | |
| 3463 | if (check->server && |
| 3464 | (check->server->proxy->options & PR_O_DISABLE404) && |
| 3465 | (check->server->next_state != SRV_ST_STOPPED) && |
| 3466 | (check->code == 404)) { |
| 3467 | set_server_check_status(check, HCHK_STATUS_L7OKCD, NULL); |
| 3468 | goto out_end_tcpcheck; |
| 3469 | } |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3470 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3471 | msg = alloc_trash_chunk(); |
Christopher Faulet | df38f88 | 2020-04-07 16:04:38 +0200 | [diff] [blame] | 3472 | if (msg) |
| 3473 | tcpcheck_onsuccess_message(msg, check, rule, ist(NULL)); |
| 3474 | set_server_check_status(check, rule->expect.ok_status, |
| 3475 | (msg ? b_head(msg) : "(tcp-check)")); |
| 3476 | free_trash_chunk(msg); |
| 3477 | } |
| 3478 | else if (rule->action == TCPCHK_ACT_CONNECT) { |
| 3479 | const char *msg = ((rule->connect.options & TCPCHK_OPT_IMPLICIT) ? NULL : "(tcp-check)"); |
| 3480 | enum healthcheck_status status = ((conn && ssl_sock_is_ssl(conn)) ? HCHK_STATUS_L6OK : HCHK_STATUS_L4OK); |
| 3481 | |
| 3482 | set_server_check_status(check, status, msg); |
| 3483 | } |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 3484 | } |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 3485 | else |
| 3486 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3487 | |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3488 | out_end_tcpcheck: |
Willy Tarreau | ef91c93 | 2019-07-23 14:37:47 +0200 | [diff] [blame] | 3489 | if ((conn && conn->flags & CO_FL_ERROR) || (cs && cs->flags & CS_FL_ERROR)) |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3490 | chk_report_conn_err(check, errno, 0); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3491 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 3492 | /* cleanup before leaving */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 3493 | check->current_step = NULL; |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 3494 | if (check->sess != NULL) { |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 3495 | vars_prune(&check->vars, check->sess, NULL); |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 3496 | session_free(check->sess); |
| 3497 | check->sess = NULL; |
| 3498 | } |
| 3499 | out: |
| 3500 | return retcode; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 3501 | } |
| 3502 | |
Christopher Faulet | 31c30fd | 2020-03-26 21:10:03 +0100 | [diff] [blame] | 3503 | static const char *init_check(struct check *check, int type) |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3504 | { |
| 3505 | check->type = type; |
| 3506 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3507 | b_reset(&check->bi); check->bi.size = global.tune.chksize; |
| 3508 | b_reset(&check->bo); check->bo.size = global.tune.chksize; |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3509 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3510 | check->bi.area = calloc(check->bi.size, sizeof(char)); |
| 3511 | check->bo.area = calloc(check->bo.size, sizeof(char)); |
| 3512 | |
| 3513 | if (!check->bi.area || !check->bo.area) |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3514 | return "out of memory while allocating check buffer"; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3515 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3516 | check->wait_list.tasklet = tasklet_new(); |
| 3517 | if (!check->wait_list.tasklet) |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 3518 | return "out of memory while allocating check tasklet"; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3519 | check->wait_list.events = 0; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3520 | check->wait_list.tasklet->process = event_srv_chk_io; |
| 3521 | check->wait_list.tasklet->context = check; |
Simon Horman | b1900d5 | 2015-01-30 11:22:54 +0900 | [diff] [blame] | 3522 | return NULL; |
| 3523 | } |
| 3524 | |
Simon Horman | bfb5d33 | 2015-01-30 11:22:55 +0900 | [diff] [blame] | 3525 | void free_check(struct check *check) |
| 3526 | { |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 3527 | task_destroy(check->task); |
| 3528 | if (check->wait_list.tasklet) |
| 3529 | tasklet_free(check->wait_list.tasklet); |
| 3530 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3531 | free(check->bi.area); |
| 3532 | free(check->bo.area); |
Christopher Faulet | 23d86d1 | 2018-01-25 11:36:35 +0100 | [diff] [blame] | 3533 | if (check->cs) { |
| 3534 | free(check->cs->conn); |
| 3535 | check->cs->conn = NULL; |
| 3536 | cs_free(check->cs); |
| 3537 | check->cs = NULL; |
| 3538 | } |
Simon Horman | bfb5d33 | 2015-01-30 11:22:55 +0900 | [diff] [blame] | 3539 | } |
| 3540 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3541 | static void free_tcpcheck_fmt(struct list *fmt) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3542 | { |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3543 | struct logformat_node *lf, *lfb; |
| 3544 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3545 | list_for_each_entry_safe(lf, lfb, fmt, list) { |
| 3546 | LIST_DEL(&lf->list); |
| 3547 | release_sample_expr(lf->expr); |
| 3548 | free(lf->arg); |
| 3549 | free(lf); |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | static void free_tcpcheck_http_hdr(struct tcpcheck_http_hdr *hdr) |
| 3554 | { |
| 3555 | if (!hdr) |
| 3556 | return; |
| 3557 | |
| 3558 | free_tcpcheck_fmt(&hdr->value); |
| 3559 | free(hdr->name.ptr); |
| 3560 | free(hdr); |
| 3561 | } |
| 3562 | |
| 3563 | static void free_tcpcheck_http_hdrs(struct list *hdrs) |
| 3564 | { |
| 3565 | struct tcpcheck_http_hdr *hdr, *bhdr; |
| 3566 | |
| 3567 | list_for_each_entry_safe(hdr, bhdr, hdrs, list) { |
| 3568 | LIST_DEL(&hdr->list); |
| 3569 | free_tcpcheck_http_hdr(hdr); |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | static void free_tcpcheck(struct tcpcheck_rule *rule, int in_pool) |
| 3574 | { |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3575 | if (!rule) |
| 3576 | return; |
| 3577 | |
| 3578 | free(rule->comment); |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3579 | switch (rule->action) { |
| 3580 | case TCPCHK_ACT_SEND: |
| 3581 | switch (rule->send.type) { |
| 3582 | case TCPCHK_SEND_STRING: |
| 3583 | case TCPCHK_SEND_BINARY: |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3584 | free(rule->send.data.ptr); |
| 3585 | break; |
| 3586 | case TCPCHK_SEND_STRING_LF: |
| 3587 | case TCPCHK_SEND_BINARY_LF: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3588 | free_tcpcheck_fmt(&rule->send.fmt); |
| 3589 | break; |
| 3590 | case TCPCHK_SEND_HTTP: |
| 3591 | free(rule->send.http.meth.str.area); |
| 3592 | if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
| 3593 | free(rule->send.http.uri.ptr); |
| 3594 | else |
| 3595 | free_tcpcheck_fmt(&rule->send.http.uri_fmt); |
| 3596 | free(rule->send.http.vsn.ptr); |
| 3597 | free_tcpcheck_http_hdrs(&rule->send.http.hdrs); |
| 3598 | if (!(rule->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
| 3599 | free(rule->send.http.body.ptr); |
| 3600 | else |
| 3601 | free_tcpcheck_fmt(&rule->send.http.body_fmt); |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3602 | break; |
| 3603 | case TCPCHK_SEND_UNDEF: |
| 3604 | break; |
| 3605 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3606 | break; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3607 | case TCPCHK_ACT_EXPECT: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3608 | free_tcpcheck_fmt(&rule->expect.onerror_fmt); |
| 3609 | free_tcpcheck_fmt(&rule->expect.onsuccess_fmt); |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 3610 | release_sample_expr(rule->expect.status_expr); |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3611 | switch (rule->expect.type) { |
| 3612 | case TCPCHK_EXPECT_STRING: |
| 3613 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3614 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 3615 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 3616 | free(rule->expect.data.ptr); |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3617 | break; |
| 3618 | case TCPCHK_EXPECT_REGEX: |
| 3619 | case TCPCHK_EXPECT_REGEX_BINARY: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 3620 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 3621 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3622 | regex_free(rule->expect.regex); |
| 3623 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 3624 | case TCPCHK_EXPECT_CUSTOM: |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3625 | case TCPCHK_EXPECT_UNDEF: |
| 3626 | break; |
| 3627 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3628 | break; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3629 | case TCPCHK_ACT_CONNECT: |
Christopher Faulet | 79b31d4 | 2020-03-30 13:00:05 +0200 | [diff] [blame] | 3630 | free(rule->connect.sni); |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 3631 | free(rule->connect.alpn); |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 3632 | release_sample_expr(rule->connect.port_expr); |
Christopher Faulet | 79b31d4 | 2020-03-30 13:00:05 +0200 | [diff] [blame] | 3633 | break; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3634 | case TCPCHK_ACT_COMMENT: |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3635 | break; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 3636 | case TCPCHK_ACT_ACTION_KW: |
| 3637 | free(rule->action_kw.rule); |
| 3638 | break; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3639 | } |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3640 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 3641 | if (in_pool) |
| 3642 | pool_free(pool_head_tcpcheck_rule, rule); |
| 3643 | else |
| 3644 | free(rule); |
| 3645 | } |
| 3646 | |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3647 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 3648 | static struct tcpcheck_var *tcpcheck_var_create(const char *name) |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3649 | { |
| 3650 | struct tcpcheck_var *var = NULL; |
| 3651 | |
| 3652 | var = calloc(1, sizeof(*var)); |
| 3653 | if (var == NULL) |
| 3654 | return NULL; |
| 3655 | |
| 3656 | var->name = ist2(strdup(name), strlen(name)); |
| 3657 | if (var->name.ptr == NULL) { |
| 3658 | free(var); |
| 3659 | return NULL; |
| 3660 | } |
| 3661 | |
| 3662 | LIST_INIT(&var->list); |
| 3663 | return var; |
| 3664 | } |
| 3665 | |
| 3666 | static void tcpcheck_var_release(struct tcpcheck_var *var) |
| 3667 | { |
| 3668 | if (!var) |
| 3669 | return; |
| 3670 | |
| 3671 | free(var->name.ptr); |
| 3672 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) |
| 3673 | free(var->data.u.str.area); |
| 3674 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) |
| 3675 | free(var->data.u.meth.str.area); |
| 3676 | free(var); |
| 3677 | } |
| 3678 | |
| 3679 | int dup_tcpcheck_vars(struct list *dst, struct list *src) |
| 3680 | { |
| 3681 | struct tcpcheck_var *var, *new = NULL; |
| 3682 | |
| 3683 | list_for_each_entry(var, src, list) { |
| 3684 | new = tcpcheck_var_create(var->name.ptr); |
| 3685 | if (!new) |
| 3686 | goto error; |
| 3687 | new->data.type = var->data.type; |
| 3688 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) { |
| 3689 | if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL) |
| 3690 | goto error; |
| 3691 | if (var->data.type == SMP_T_STR) |
| 3692 | new->data.u.str.area[new->data.u.str.data] = 0; |
| 3693 | } |
| 3694 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
| 3695 | if (chunk_dup(&new->data.u.str, &var->data.u.str) == NULL) |
| 3696 | goto error; |
| 3697 | new->data.u.str.area[new->data.u.str.data] = 0; |
| 3698 | new->data.u.meth.meth = var->data.u.meth.meth; |
| 3699 | } |
| 3700 | else |
| 3701 | new->data.u = var->data.u; |
| 3702 | LIST_ADDQ(dst, &new->list); |
| 3703 | } |
| 3704 | return 1; |
| 3705 | |
| 3706 | error: |
| 3707 | free(new); |
| 3708 | return 0; |
| 3709 | } |
| 3710 | |
| 3711 | static void free_tcpcheck_vars(struct list *vars) |
| 3712 | { |
| 3713 | struct tcpcheck_var *var, *back; |
| 3714 | |
| 3715 | list_for_each_entry_safe(var, back, vars, list) { |
| 3716 | LIST_DEL(&var->list); |
| 3717 | tcpcheck_var_release(var); |
| 3718 | } |
| 3719 | } |
| 3720 | |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3721 | void email_alert_free(struct email_alert *alert) |
| 3722 | { |
| 3723 | struct tcpcheck_rule *rule, *back; |
| 3724 | |
| 3725 | if (!alert) |
| 3726 | return; |
| 3727 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3728 | if (alert->rules.list) { |
| 3729 | list_for_each_entry_safe(rule, back, alert->rules.list, list) { |
| 3730 | LIST_DEL(&rule->list); |
| 3731 | free_tcpcheck(rule, 1); |
| 3732 | } |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3733 | free_tcpcheck_vars(&alert->rules.preset_vars); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3734 | free(alert->rules.list); |
| 3735 | alert->rules.list = NULL; |
Christopher Faulet | de1a75b | 2017-10-23 15:38:19 +0200 | [diff] [blame] | 3736 | } |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3737 | pool_free(pool_head_email_alert, alert); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3738 | } |
| 3739 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3740 | static struct task *process_email_alert(struct task *t, void *context, unsigned short state) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3741 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3742 | struct check *check = context; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3743 | struct email_alertq *q; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3744 | struct email_alert *alert; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3745 | |
| 3746 | q = container_of(check, typeof(*q), check); |
| 3747 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3748 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3749 | while (1) { |
| 3750 | if (!(check->state & CHK_ST_ENABLED)) { |
| 3751 | if (LIST_ISEMPTY(&q->email_alerts)) { |
| 3752 | /* All alerts processed, queue the task */ |
| 3753 | t->expire = TICK_ETERNITY; |
| 3754 | task_queue(t); |
Christopher Faulet | c2a89a6 | 2017-10-23 15:54:24 +0200 | [diff] [blame] | 3755 | goto end; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3756 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3757 | |
| 3758 | alert = LIST_NEXT(&q->email_alerts, typeof(alert), list); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3759 | LIST_DEL(&alert->list); |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3760 | t->expire = now_ms; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3761 | check->tcpcheck_rules = &alert->rules; |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 3762 | check->status = HCHK_STATUS_INI; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3763 | check->state |= CHK_ST_ENABLED; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3764 | } |
| 3765 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3766 | process_chk(t, context, state); |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3767 | if (check->state & CHK_ST_INPROGRESS) |
| 3768 | break; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3769 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3770 | alert = container_of(check->tcpcheck_rules, typeof(*alert), rules); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3771 | email_alert_free(alert); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3772 | check->tcpcheck_rules = NULL; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3773 | check->server = NULL; |
| 3774 | check->state &= ~CHK_ST_ENABLED; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3775 | } |
Christopher Faulet | c2a89a6 | 2017-10-23 15:54:24 +0200 | [diff] [blame] | 3776 | end: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3777 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3778 | return t; |
| 3779 | } |
| 3780 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3781 | /* Initializes mailer alerts for the proxy <p> using <mls> parameters. |
| 3782 | * |
| 3783 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 3784 | * filled. |
| 3785 | */ |
| 3786 | int init_email_alert(struct mailers *mls, struct proxy *p, char **err) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3787 | { |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3788 | struct mailer *mailer; |
| 3789 | struct email_alertq *queues; |
| 3790 | const char *err_str; |
| 3791 | int i = 0; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3792 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3793 | if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) { |
| 3794 | memprintf(err, "out of memory while allocating mailer alerts queues"); |
mildis | 5ab01cb | 2018-10-02 16:46:34 +0200 | [diff] [blame] | 3795 | goto fail_no_queue; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3796 | } |
| 3797 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3798 | for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) { |
| 3799 | struct email_alertq *q = &queues[i]; |
| 3800 | struct check *check = &q->check; |
| 3801 | struct task *t; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3802 | |
| 3803 | LIST_INIT(&q->email_alerts); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3804 | HA_SPIN_INIT(&q->lock); |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3805 | check->inter = mls->timeout.mail; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3806 | check->rise = DEF_AGENT_RISETIME; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 3807 | check->proxy = p; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3808 | check->fall = DEF_AGENT_FALLTIME; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3809 | if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) { |
| 3810 | memprintf(err, "%s", err_str); |
| 3811 | goto error; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | check->xprt = mailer->xprt; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3815 | check->addr = mailer->addr; |
Christopher Faulet | b797ae1 | 2018-03-27 15:35:35 +0200 | [diff] [blame] | 3816 | check->port = get_host_port(&mailer->addr); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3817 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 3818 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3819 | memprintf(err, "out of memory while allocating mailer alerts task"); |
| 3820 | goto error; |
| 3821 | } |
| 3822 | |
| 3823 | check->task = t; |
| 3824 | t->process = process_email_alert; |
| 3825 | t->context = check; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3826 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3827 | /* check this in one ms */ |
| 3828 | t->expire = TICK_ETERNITY; |
| 3829 | check->start = now; |
| 3830 | task_queue(t); |
| 3831 | } |
| 3832 | |
| 3833 | mls->users++; |
| 3834 | free(p->email_alert.mailers.name); |
| 3835 | p->email_alert.mailers.m = mls; |
| 3836 | p->email_alert.queues = queues; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3837 | return 0; |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3838 | |
| 3839 | error: |
| 3840 | for (i = 0; i < mls->count; i++) { |
| 3841 | struct email_alertq *q = &queues[i]; |
| 3842 | struct check *check = &q->check; |
| 3843 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3844 | free_check(check); |
| 3845 | } |
| 3846 | free(queues); |
mildis | 5ab01cb | 2018-10-02 16:46:34 +0200 | [diff] [blame] | 3847 | fail_no_queue: |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3848 | return 1; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3852 | static int add_tcpcheck_expect_str(struct tcpcheck_rules *rules, const char *str) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3853 | { |
Gaetan Rivet | 4038b94 | 2020-02-26 16:19:40 +0100 | [diff] [blame] | 3854 | struct tcpcheck_rule *tcpcheck, *prev_check; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3855 | struct tcpcheck_expect *expect; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3856 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3857 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3858 | return 0; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 3859 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3860 | tcpcheck->action = TCPCHK_ACT_EXPECT; |
| 3861 | |
| 3862 | expect = &tcpcheck->expect; |
| 3863 | expect->type = TCPCHK_EXPECT_STRING; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 3864 | LIST_INIT(&expect->onerror_fmt); |
| 3865 | LIST_INIT(&expect->onsuccess_fmt); |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 3866 | expect->ok_status = HCHK_STATUS_L7OKD; |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 3867 | expect->err_status = HCHK_STATUS_L7RSP; |
| 3868 | expect->tout_status = HCHK_STATUS_L7TOUT; |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 3869 | expect->data = ist2(strdup(str), strlen(str)); |
| 3870 | if (!expect->data.ptr) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3871 | pool_free(pool_head_tcpcheck_rule, tcpcheck); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3872 | return 0; |
| 3873 | } |
| 3874 | |
Gaetan Rivet | 4038b94 | 2020-02-26 16:19:40 +0100 | [diff] [blame] | 3875 | /* All tcp-check expect points back to the first inverse expect rule |
| 3876 | * in a chain of one or more expect rule, potentially itself. |
| 3877 | */ |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3878 | tcpcheck->expect.head = tcpcheck; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3879 | list_for_each_entry_rev(prev_check, rules->list, list) { |
Gaetan Rivet | 4038b94 | 2020-02-26 16:19:40 +0100 | [diff] [blame] | 3880 | if (prev_check->action == TCPCHK_ACT_EXPECT) { |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 3881 | if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV) |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 3882 | tcpcheck->expect.head = prev_check; |
Gaetan Rivet | 4038b94 | 2020-02-26 16:19:40 +0100 | [diff] [blame] | 3883 | continue; |
| 3884 | } |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 3885 | if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW) |
Gaetan Rivet | 4038b94 | 2020-02-26 16:19:40 +0100 | [diff] [blame] | 3886 | break; |
| 3887 | } |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3888 | LIST_ADDQ(rules->list, &tcpcheck->list); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3889 | return 1; |
| 3890 | } |
| 3891 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3892 | static int add_tcpcheck_send_strs(struct tcpcheck_rules *rules, const char * const *strs) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3893 | { |
| 3894 | struct tcpcheck_rule *tcpcheck; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3895 | struct tcpcheck_send *send; |
Willy Tarreau | 64345aa | 2016-08-10 19:29:09 +0200 | [diff] [blame] | 3896 | const char *in; |
| 3897 | char *dst; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3898 | int i; |
| 3899 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3900 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3901 | return 0; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 3902 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 3903 | tcpcheck->action = TCPCHK_ACT_SEND; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3904 | |
| 3905 | send = &tcpcheck->send; |
| 3906 | send->type = TCPCHK_SEND_STRING; |
| 3907 | |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3908 | for (i = 0; strs[i]; i++) |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3909 | send->data.len += strlen(strs[i]); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3910 | |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3911 | send->data.ptr = malloc(send->data.len + 1); |
| 3912 | if (!isttest(send->data)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3913 | pool_free(pool_head_tcpcheck_rule, tcpcheck); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3914 | return 0; |
| 3915 | } |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3916 | |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 3917 | dst = send->data.ptr; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3918 | for (i = 0; strs[i]; i++) |
Willy Tarreau | 64345aa | 2016-08-10 19:29:09 +0200 | [diff] [blame] | 3919 | for (in = strs[i]; (*dst = *in++); dst++); |
| 3920 | *dst = 0; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3921 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3922 | LIST_ADDQ(rules->list, &tcpcheck->list); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3923 | return 1; |
| 3924 | } |
| 3925 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3926 | static int enqueue_one_email_alert(struct proxy *p, struct server *s, |
| 3927 | struct email_alertq *q, const char *msg) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3928 | { |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 3929 | struct email_alert *alert; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3930 | struct tcpcheck_rule *tcpcheck; |
| 3931 | struct check *check = &q->check; |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3932 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3933 | if ((alert = pool_alloc(pool_head_email_alert)) == NULL) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3934 | goto error; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 3935 | LIST_INIT(&alert->list); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3936 | alert->rules.flags = 0; |
| 3937 | alert->rules.list = calloc(1, sizeof(*alert->rules.list)); |
| 3938 | if (!alert->rules.list) |
| 3939 | goto error; |
| 3940 | LIST_INIT(alert->rules.list); |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 3941 | LIST_INIT(&alert->rules.preset_vars); /* unused for email alerts */ |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 3942 | alert->srv = s; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 3943 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3944 | if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3945 | goto error; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 3946 | memset(tcpcheck, 0, sizeof(*tcpcheck)); |
| 3947 | tcpcheck->action = TCPCHK_ACT_CONNECT; |
| 3948 | tcpcheck->comment = NULL; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 3949 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3950 | LIST_ADDQ(alert->rules.list, &tcpcheck->list); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3951 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3952 | if (!add_tcpcheck_expect_str(&alert->rules, "220 ")) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3953 | goto error; |
| 3954 | |
| 3955 | { |
| 3956 | const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" }; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3957 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3958 | goto error; |
| 3959 | } |
| 3960 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3961 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3962 | goto error; |
| 3963 | |
| 3964 | { |
| 3965 | const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" }; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3966 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3967 | goto error; |
| 3968 | } |
| 3969 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3970 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3971 | goto error; |
| 3972 | |
| 3973 | { |
| 3974 | const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" }; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3975 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3976 | goto error; |
| 3977 | } |
| 3978 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3979 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3980 | goto error; |
| 3981 | |
| 3982 | { |
| 3983 | const char * const strs[2] = { "DATA\r\n" }; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3984 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3985 | goto error; |
| 3986 | } |
| 3987 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 3988 | if (!add_tcpcheck_expect_str(&alert->rules, "354 ")) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 3989 | goto error; |
| 3990 | |
| 3991 | { |
| 3992 | struct tm tm; |
| 3993 | char datestr[48]; |
| 3994 | const char * const strs[18] = { |
Pieter Baauw | 5e0964e | 2016-02-13 16:27:35 +0100 | [diff] [blame] | 3995 | "From: ", p->email_alert.from, "\r\n", |
| 3996 | "To: ", p->email_alert.to, "\r\n", |
| 3997 | "Date: ", datestr, "\r\n", |
| 3998 | "Subject: [HAproxy Alert] ", msg, "\r\n", |
| 3999 | "\r\n", |
| 4000 | msg, "\r\n", |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4001 | "\r\n", |
Pieter Baauw | ed35c37 | 2015-07-22 19:51:54 +0200 | [diff] [blame] | 4002 | ".\r\n", |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4003 | NULL |
| 4004 | }; |
| 4005 | |
| 4006 | get_localtime(date.tv_sec, &tm); |
| 4007 | |
| 4008 | if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) { |
| 4009 | goto error; |
| 4010 | } |
| 4011 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4012 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4013 | goto error; |
| 4014 | } |
| 4015 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4016 | if (!add_tcpcheck_expect_str(&alert->rules, "250 ")) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4017 | goto error; |
| 4018 | |
| 4019 | { |
| 4020 | const char * const strs[2] = { "QUIT\r\n" }; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4021 | if (!add_tcpcheck_send_strs(&alert->rules, strs)) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4022 | goto error; |
| 4023 | } |
| 4024 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4025 | if (!add_tcpcheck_expect_str(&alert->rules, "221 ")) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4026 | goto error; |
| 4027 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4028 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 4029 | task_wakeup(check->task, TASK_WOKEN_MSG); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4030 | LIST_ADDQ(&q->email_alerts, &alert->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4031 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4032 | return 1; |
| 4033 | |
| 4034 | error: |
| 4035 | email_alert_free(alert); |
| 4036 | return 0; |
| 4037 | } |
| 4038 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 4039 | static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4040 | { |
| 4041 | int i; |
| 4042 | struct mailer *mailer; |
| 4043 | |
| 4044 | for (i = 0, mailer = p->email_alert.mailers.m->mailer_list; |
| 4045 | i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) { |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 4046 | if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4047 | ha_alert("Email alert [%s] could not be enqueued: out of memory\n", p->id); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4048 | return; |
| 4049 | } |
| 4050 | } |
| 4051 | |
| 4052 | return; |
| 4053 | } |
| 4054 | |
| 4055 | /* |
| 4056 | * Send email alert if configured. |
| 4057 | */ |
Simon Horman | 64e3416 | 2015-02-06 11:11:57 +0900 | [diff] [blame] | 4058 | void send_email_alert(struct server *s, int level, const char *format, ...) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4059 | { |
| 4060 | va_list argp; |
| 4061 | char buf[1024]; |
| 4062 | int len; |
| 4063 | struct proxy *p = s->proxy; |
| 4064 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 4065 | if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL) |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4066 | return; |
| 4067 | |
| 4068 | va_start(argp, format); |
| 4069 | len = vsnprintf(buf, sizeof(buf), format, argp); |
| 4070 | va_end(argp); |
| 4071 | |
Thierry FOURNIER | 62c8a21 | 2017-02-09 12:19:27 +0100 | [diff] [blame] | 4072 | if (len < 0 || len >= sizeof(buf)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4073 | ha_alert("Email alert [%s] could not format message\n", p->id); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4074 | return; |
| 4075 | } |
| 4076 | |
Christopher Faulet | 0108bb3 | 2017-10-20 21:34:32 +0200 | [diff] [blame] | 4077 | enqueue_email_alert(p, s, buf); |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 4078 | } |
| 4079 | |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4080 | /* |
| 4081 | * Return value: |
| 4082 | * the port to be used for the health check |
| 4083 | * 0 in case no port could be found for the check |
| 4084 | */ |
Christopher Faulet | 31c30fd | 2020-03-26 21:10:03 +0100 | [diff] [blame] | 4085 | static int srv_check_healthcheck_port(struct check *chk) |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4086 | { |
| 4087 | int i = 0; |
| 4088 | struct server *srv = NULL; |
| 4089 | |
| 4090 | srv = chk->server; |
| 4091 | |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4092 | /* by default, we use the health check port ocnfigured */ |
| 4093 | if (chk->port > 0) |
| 4094 | return chk->port; |
| 4095 | |
| 4096 | /* try to get the port from check_core.addr if check.port not set */ |
| 4097 | i = get_host_port(&chk->addr); |
| 4098 | if (i > 0) |
| 4099 | return i; |
| 4100 | |
| 4101 | /* try to get the port from server address */ |
| 4102 | /* prevent MAPPORTS from working at this point, since checks could |
| 4103 | * not be performed in such case (MAPPORTS impose a relative ports |
| 4104 | * based on live traffic) |
| 4105 | */ |
| 4106 | if (srv->flags & SRV_F_MAPPORTS) |
| 4107 | return 0; |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 4108 | |
| 4109 | i = srv->svc_port; /* by default */ |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 4110 | if (i > 0) |
| 4111 | return i; |
| 4112 | |
| 4113 | return 0; |
| 4114 | } |
| 4115 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 4116 | REGISTER_POST_CHECK(start_checks); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 4117 | |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4118 | static int check_proxy_tcpcheck(struct proxy *px) |
| 4119 | { |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4120 | struct tcpcheck_rule *chk, *back; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4121 | char *comment = NULL, *errmsg = NULL; |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4122 | enum tcpcheck_rule_type prev_action = TCPCHK_ACT_COMMENT; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4123 | int ret = 0; |
| 4124 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4125 | if (!(px->cap & PR_CAP_BE) || (px->options2 & PR_O2_CHK_ANY) != PR_O2_TCPCHK_CHK) { |
| 4126 | deinit_proxy_tcpcheck(px); |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4127 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | free(px->check_command); |
| 4131 | free(px->check_path); |
| 4132 | px->check_command = px->check_path = NULL; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4133 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4134 | if (!px->tcpcheck_rules.list) { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4135 | ha_alert("config : proxy '%s' : tcp-check configured but no ruleset defined.\n", px->id); |
| 4136 | ret |= ERR_ALERT | ERR_FATAL; |
| 4137 | goto out; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4140 | /* HTTP ruleset */ |
| 4141 | if ((px->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 4142 | struct tcpcheck_rule *next; |
| 4143 | |
| 4144 | /* move remaining send rule from "option httpchk" line to the right place */ |
| 4145 | chk = get_first_tcpcheck_rule(&px->tcpcheck_rules); |
| 4146 | if (chk && chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 4147 | next = get_next_tcpcheck_rule(&px->tcpcheck_rules, chk); |
| 4148 | if (next && next->action == TCPCHK_ACT_CONNECT) { |
| 4149 | LIST_DEL(&chk->list); |
| 4150 | LIST_ADD(&next->list, &chk->list); |
| 4151 | chk->index = next->index; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | /* add implicit expect rule if the last one is a send. */ |
| 4156 | chk = get_last_tcpcheck_rule(&px->tcpcheck_rules); |
| 4157 | if (chk && chk->action == TCPCHK_ACT_SEND) { |
| 4158 | next = parse_tcpcheck_expect((char *[]){"http-check", "expect", "rstatus", "^[23]", ""}, |
| 4159 | 1, px, px->tcpcheck_rules.list, TCPCHK_RULES_HTTP_CHK, |
| 4160 | px->conf.file, px->conf.line, &errmsg); |
| 4161 | if (!next) { |
| 4162 | ha_alert("config : proxy '%s': unable to add implicit http-check expect rule " |
| 4163 | "(%s).\n", px->id, errmsg); |
| 4164 | free(errmsg); |
| 4165 | ret |= ERR_ALERT | ERR_FATAL; |
| 4166 | goto out; |
| 4167 | } |
| 4168 | LIST_ADDQ(px->tcpcheck_rules.list, &next->list); |
| 4169 | next->index = chk->index; |
| 4170 | } |
| 4171 | } |
| 4172 | |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4173 | /* If there is no connect rule preceeding all send / expect rules, an |
| 4174 | * implicit one is inserted before all others |
| 4175 | */ |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4176 | chk = get_first_tcpcheck_rule(&px->tcpcheck_rules); |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4177 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 4178 | chk = calloc(1, sizeof(*chk)); |
| 4179 | if (!chk) { |
| 4180 | ha_alert("config : proxy '%s': unable to add implicit tcp-check connect rule " |
| 4181 | "(out of memory).\n", px->id); |
| 4182 | ret |= ERR_ALERT | ERR_FATAL; |
| 4183 | goto out; |
| 4184 | } |
| 4185 | chk->action = TCPCHK_ACT_CONNECT; |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 4186 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4187 | LIST_ADD(px->tcpcheck_rules.list, &chk->list); |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4188 | } |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 4189 | |
| 4190 | /* Now remove comment rules */ |
| 4191 | list_for_each_entry_safe(chk, back, px->tcpcheck_rules.list, list) { |
| 4192 | if (chk->action != prev_action && prev_action != TCPCHK_ACT_COMMENT) { |
| 4193 | free(comment); |
| 4194 | comment = NULL; |
| 4195 | } |
| 4196 | |
| 4197 | prev_action = chk->action; |
| 4198 | switch (chk->action) { |
| 4199 | case TCPCHK_ACT_COMMENT: |
| 4200 | free(comment); |
| 4201 | comment = chk->comment; |
| 4202 | LIST_DEL(&chk->list); |
| 4203 | free(chk); |
| 4204 | break; |
| 4205 | case TCPCHK_ACT_CONNECT: |
| 4206 | if (!chk->comment && comment) |
| 4207 | chk->comment = strdup(comment); |
| 4208 | /* fall though */ |
| 4209 | case TCPCHK_ACT_ACTION_KW: |
| 4210 | free(comment); |
| 4211 | comment = NULL; |
| 4212 | break; |
| 4213 | case TCPCHK_ACT_SEND: |
| 4214 | case TCPCHK_ACT_EXPECT: |
| 4215 | if (!chk->comment && comment) |
| 4216 | chk->comment = strdup(comment); |
| 4217 | break; |
| 4218 | } |
| 4219 | } |
| 4220 | free(comment); |
| 4221 | comment = NULL; |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4222 | |
| 4223 | out: |
| 4224 | return ret; |
| 4225 | } |
| 4226 | |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4227 | static int init_srv_check(struct server *srv) |
| 4228 | { |
| 4229 | const char *err; |
| 4230 | struct tcpcheck_rule *r; |
| 4231 | int ret = 0; |
| 4232 | |
| 4233 | if (!srv->do_check) |
| 4234 | goto out; |
| 4235 | |
| 4236 | |
| 4237 | /* If neither a port nor an addr was specified and no check transport |
| 4238 | * layer is forced, then the transport layer used by the checks is the |
| 4239 | * same as for the production traffic. Otherwise we use raw_sock by |
| 4240 | * default, unless one is specified. |
| 4241 | */ |
| 4242 | if (!srv->check.port && !is_addr(&srv->check.addr)) { |
| 4243 | if (!srv->check.use_ssl && srv->use_ssl != -1) { |
| 4244 | srv->check.use_ssl = srv->use_ssl; |
| 4245 | srv->check.xprt = srv->xprt; |
| 4246 | } |
| 4247 | else if (srv->check.use_ssl == 1) |
| 4248 | srv->check.xprt = xprt_get(XPRT_SSL); |
| 4249 | |
| 4250 | srv->check.send_proxy |= (srv->pp_opts); |
| 4251 | } |
| 4252 | |
| 4253 | /* validate <srv> server health-check settings */ |
| 4254 | |
| 4255 | /* We need at least a service port, a check port or the first tcp-check |
| 4256 | * rule must be a 'connect' one when checking an IPv4/IPv6 server. |
| 4257 | */ |
| 4258 | if ((srv_check_healthcheck_port(&srv->check) != 0) || |
| 4259 | (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr)))) |
| 4260 | goto init; |
| 4261 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4262 | if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) { |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4263 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n", |
| 4264 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4265 | ret |= ERR_ALERT | ERR_ABORT; |
| 4266 | goto out; |
| 4267 | } |
| 4268 | |
| 4269 | /* search the first action (connect / send / expect) in the list */ |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4270 | r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4271 | if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) { |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4272 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port " |
| 4273 | "nor tcp_check rule 'connect' with port information.\n", |
| 4274 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4275 | ret |= ERR_ALERT | ERR_ABORT; |
| 4276 | goto out; |
| 4277 | } |
| 4278 | |
| 4279 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4280 | list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) { |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4281 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port || !get_host_port(&r->connect.addr))) { |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4282 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port, " |
| 4283 | "and a tcp_check rule 'connect' with no port information.\n", |
| 4284 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4285 | ret |= ERR_ALERT | ERR_ABORT; |
| 4286 | goto out; |
| 4287 | } |
| 4288 | } |
| 4289 | |
| 4290 | init: |
| 4291 | err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY); |
| 4292 | if (err) { |
| 4293 | ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n", |
| 4294 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 4295 | ret |= ERR_ALERT | ERR_ABORT; |
| 4296 | goto out; |
| 4297 | } |
| 4298 | srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 4299 | global.maxsock++; |
| 4300 | |
| 4301 | out: |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
| 4305 | static int init_srv_agent_check(struct server *srv) |
| 4306 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4307 | struct tcpcheck_rule *chk; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4308 | const char *err; |
| 4309 | int ret = 0; |
| 4310 | |
| 4311 | if (!srv->do_agent) |
| 4312 | goto out; |
| 4313 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4314 | /* If there is no connect rule preceeding all send / expect rules, an |
| 4315 | * implicit one is inserted before all others. |
| 4316 | */ |
| 4317 | chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules); |
| 4318 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 4319 | chk = calloc(1, sizeof(*chk)); |
| 4320 | if (!chk) { |
| 4321 | ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule" |
| 4322 | " to agent-check for server '%s' (out of memory).\n", |
| 4323 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 4324 | ret |= ERR_ALERT | ERR_FATAL; |
| 4325 | goto out; |
| 4326 | } |
| 4327 | chk->action = TCPCHK_ACT_CONNECT; |
| 4328 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
| 4329 | LIST_ADD(srv->agent.tcpcheck_rules->list, &chk->list); |
| 4330 | } |
| 4331 | |
| 4332 | |
| 4333 | err = init_check(&srv->agent, PR_O2_TCPCHK_CHK); |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4334 | if (err) { |
| 4335 | ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n", |
| 4336 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 4337 | ret |= ERR_ALERT | ERR_ABORT; |
| 4338 | goto out; |
| 4339 | } |
| 4340 | |
| 4341 | if (!srv->agent.inter) |
| 4342 | srv->agent.inter = srv->check.inter; |
| 4343 | |
| 4344 | srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 4345 | global.maxsock++; |
| 4346 | |
| 4347 | out: |
| 4348 | return ret; |
| 4349 | } |
| 4350 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4351 | void deinit_proxy_tcpcheck(struct proxy *px) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4352 | { |
Christopher Faulet | 7a1e2e1 | 2020-04-02 18:05:11 +0200 | [diff] [blame] | 4353 | free_tcpcheck_vars(&px->tcpcheck_rules.preset_vars); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4354 | px->tcpcheck_rules.flags = 0; |
| 4355 | px->tcpcheck_rules.list = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4356 | } |
| 4357 | |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4358 | static void deinit_srv_check(struct server *srv) |
| 4359 | { |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 4360 | if (srv->check.state & CHK_ST_CONFIGURED) |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4361 | free_check(&srv->check); |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 4362 | srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED; |
| 4363 | srv->do_check = 0; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | |
| 4367 | static void deinit_srv_agent_check(struct server *srv) |
| 4368 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4369 | if (srv->agent.tcpcheck_rules) { |
| 4370 | free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars); |
| 4371 | free(srv->agent.tcpcheck_rules); |
| 4372 | srv->agent.tcpcheck_rules = NULL; |
| 4373 | } |
| 4374 | |
| 4375 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4376 | free_check(&srv->agent); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4377 | |
| 4378 | srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT; |
| 4379 | srv->do_agent = 0; |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4380 | } |
| 4381 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4382 | static void deinit_tcpchecks() |
| 4383 | { |
| 4384 | struct tcpcheck_ruleset *rs, *rsb; |
| 4385 | struct tcpcheck_rule *r, *rb; |
| 4386 | |
| 4387 | list_for_each_entry_safe(rs, rsb, &tcpchecks_list, list) { |
| 4388 | LIST_DEL(&rs->list); |
| 4389 | list_for_each_entry_safe(r, rb, &rs->rules, list) { |
| 4390 | LIST_DEL(&r->list); |
| 4391 | free_tcpcheck(r, 0); |
| 4392 | } |
| 4393 | free(rs->name); |
| 4394 | free(rs); |
| 4395 | } |
| 4396 | } |
| 4397 | |
Christopher Faulet | a202d1d | 2020-03-26 17:38:49 +0100 | [diff] [blame] | 4398 | |
| 4399 | REGISTER_POST_PROXY_CHECK(check_proxy_tcpcheck); |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4400 | REGISTER_POST_SERVER_CHECK(init_srv_check); |
| 4401 | REGISTER_POST_SERVER_CHECK(init_srv_agent_check); |
| 4402 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4403 | REGISTER_PROXY_DEINIT(deinit_proxy_tcpcheck); |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4404 | REGISTER_SERVER_DEINIT(deinit_srv_check); |
| 4405 | REGISTER_SERVER_DEINIT(deinit_srv_agent_check); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4406 | REGISTER_POST_DEINIT(deinit_tcpchecks); |
Christopher Faulet | 8892e5d | 2020-03-26 19:48:20 +0100 | [diff] [blame] | 4407 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 4408 | static struct tcpcheck_ruleset *tcpcheck_ruleset_lookup(const char *name) |
| 4409 | { |
| 4410 | struct tcpcheck_ruleset *rs; |
| 4411 | |
| 4412 | list_for_each_entry(rs, &tcpchecks_list, list) { |
| 4413 | if (strcmp(rs->name, name) == 0) |
| 4414 | return rs; |
| 4415 | } |
| 4416 | return NULL; |
| 4417 | } |
| 4418 | |
| 4419 | static struct tcpcheck_ruleset *tcpcheck_ruleset_create(const char *name) |
| 4420 | { |
| 4421 | struct tcpcheck_ruleset *rs; |
| 4422 | |
| 4423 | rs = calloc(1, sizeof(*rs)); |
| 4424 | if (rs == NULL) |
| 4425 | return NULL; |
| 4426 | |
| 4427 | rs->name = strdup(name); |
| 4428 | if (rs->name == NULL) { |
| 4429 | free(rs); |
| 4430 | return NULL; |
| 4431 | } |
| 4432 | |
| 4433 | LIST_INIT(&rs->list); |
| 4434 | LIST_INIT(&rs->rules); |
| 4435 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 4436 | return rs; |
| 4437 | } |
| 4438 | |
| 4439 | static void tcpcheck_ruleset_release(struct tcpcheck_ruleset *rs) |
| 4440 | { |
| 4441 | struct tcpcheck_rule *r, *rb; |
| 4442 | if (!rs) |
| 4443 | return; |
| 4444 | |
| 4445 | LIST_DEL(&rs->list); |
| 4446 | list_for_each_entry_safe(r, rb, &rs->rules, list) { |
| 4447 | LIST_DEL(&r->list); |
| 4448 | free_tcpcheck(r, 0); |
| 4449 | } |
| 4450 | free(rs->name); |
| 4451 | free(rs); |
| 4452 | } |
| 4453 | |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 4454 | /* extracts check payload at a fixed position and length */ |
| 4455 | static int |
| 4456 | smp_fetch_chk_payload(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 4457 | { |
| 4458 | unsigned int buf_offset = ((arg_p[0].type == ARGT_SINT) ? arg_p[0].data.sint : 0); |
| 4459 | unsigned int buf_size = ((arg_p[1].type == ARGT_SINT) ? arg_p[1].data.sint : 0); |
| 4460 | struct server *srv = (smp->sess ? objt_server(smp->sess->origin) : NULL); |
| 4461 | struct buffer *buf; |
| 4462 | |
| 4463 | if (!srv || !srv->do_check) |
| 4464 | return 0; |
| 4465 | |
| 4466 | buf = &srv->check.bi; |
| 4467 | if (buf_offset > b_data(buf)) |
| 4468 | goto no_match; |
| 4469 | if (buf_offset + buf_size > b_data(buf)) |
| 4470 | buf_size = 0; |
| 4471 | |
| 4472 | /* init chunk as read only */ |
| 4473 | smp->data.type = SMP_T_STR; |
| 4474 | smp->flags = SMP_F_VOLATILE | SMP_F_CONST; |
| 4475 | chunk_initlen(&smp->data.u.str, b_head(buf) + buf_offset, 0, (buf_size ? buf_size : (b_data(buf) - buf_offset))); |
| 4476 | |
| 4477 | return 1; |
| 4478 | |
| 4479 | no_match: |
| 4480 | smp->flags = 0; |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
| 4484 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 4485 | { "check.payload", smp_fetch_chk_payload, ARG2(0,SINT,SINT), NULL, SMP_T_STR, SMP_USE_INTRN }, |
| 4486 | { /* END */ }, |
| 4487 | }}; |
| 4488 | |
| 4489 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 4490 | |
| 4491 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4492 | struct action_kw_list tcp_check_keywords = { |
| 4493 | .list = LIST_HEAD_INIT(tcp_check_keywords.list), |
| 4494 | }; |
| 4495 | |
| 4496 | /* Return the struct action_kw associated to a keyword */ |
| 4497 | static struct action_kw *action_kw_tcp_check_lookup(const char *kw) |
| 4498 | { |
| 4499 | return action_lookup(&tcp_check_keywords.list, kw); |
| 4500 | } |
| 4501 | |
| 4502 | static void action_kw_tcp_check_build_list(struct buffer *chk) |
| 4503 | { |
| 4504 | action_build_list(&tcp_check_keywords.list, chk); |
| 4505 | } |
| 4506 | |
| 4507 | /* Create a tcp-check rule resulting from parsing a custom keyword. */ |
| 4508 | static struct tcpcheck_rule *parse_tcpcheck_action(char **args, int cur_arg, struct proxy *px, |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4509 | struct list *rules, struct action_kw *kw, |
| 4510 | const char *file, int line, char **errmsg) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4511 | { |
| 4512 | struct tcpcheck_rule *chk = NULL; |
| 4513 | struct act_rule *actrule = NULL; |
| 4514 | |
| 4515 | actrule = calloc(1, sizeof(*actrule)); |
| 4516 | if (!actrule) { |
| 4517 | memprintf(errmsg, "out of memory"); |
| 4518 | goto error; |
| 4519 | } |
| 4520 | actrule->kw = kw; |
| 4521 | actrule->from = ACT_F_TCP_CHK; |
| 4522 | |
| 4523 | cur_arg++; |
| 4524 | if (kw->parse((const char **)args, &cur_arg, px, actrule, errmsg) == ACT_RET_PRS_ERR) { |
| 4525 | memprintf(errmsg, "'%s' : %s", kw->kw, *errmsg); |
| 4526 | goto error; |
| 4527 | } |
| 4528 | |
| 4529 | chk = calloc(1, sizeof(*chk)); |
| 4530 | if (!chk) { |
| 4531 | memprintf(errmsg, "out of memory"); |
| 4532 | goto error; |
| 4533 | } |
| 4534 | chk->action = TCPCHK_ACT_ACTION_KW; |
| 4535 | chk->action_kw.rule = actrule; |
| 4536 | return chk; |
| 4537 | |
| 4538 | error: |
| 4539 | free(actrule); |
| 4540 | return NULL; |
| 4541 | } |
| 4542 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4543 | static struct tcpcheck_rule *parse_tcpcheck_connect(char **args, int cur_arg, struct proxy *px, struct list *rules, |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4544 | const char *file, int line, char **errmsg) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4545 | { |
| 4546 | struct tcpcheck_rule *chk = NULL; |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4547 | struct sockaddr_storage *sk = NULL; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 4548 | char *comment = NULL, *sni = NULL, *alpn = NULL; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4549 | struct sample_expr *port_expr = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4550 | unsigned short conn_opts = 0; |
| 4551 | long port = 0; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 4552 | int alpn_len = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4553 | |
| 4554 | list_for_each_entry(chk, rules, list) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4555 | if (chk->action == TCPCHK_ACT_CONNECT) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4556 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4557 | if (chk->action == TCPCHK_ACT_COMMENT || |
| 4558 | chk->action == TCPCHK_ACT_ACTION_KW || |
| 4559 | (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT))) |
| 4560 | continue; |
| 4561 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 4562 | memprintf(errmsg, "first step MUST also be a 'connect', " |
| 4563 | "optionnaly preceded by a 'set-var', an 'unset-var' or a 'comment', " |
| 4564 | "when there is a 'connect' step in the tcp-check ruleset"); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4565 | goto error; |
| 4566 | } |
| 4567 | |
| 4568 | cur_arg++; |
| 4569 | while (*(args[cur_arg])) { |
Christopher Faulet | bb591a1 | 2020-04-01 16:52:17 +0200 | [diff] [blame] | 4570 | if (strcmp(args[cur_arg], "default") == 0) |
| 4571 | conn_opts |= TCPCHK_OPT_DEFAULT_CONNECT; |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4572 | else if (strcmp(args[cur_arg], "addr") == 0) { |
| 4573 | int port1, port2; |
| 4574 | struct protocol *proto; |
| 4575 | |
| 4576 | if (!*(args[cur_arg+1])) { |
| 4577 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[cur_arg]); |
| 4578 | goto error; |
| 4579 | } |
| 4580 | |
| 4581 | sk = str2sa_range(args[cur_arg+1], NULL, &port1, &port2, errmsg, NULL, NULL, 1); |
| 4582 | if (!sk) { |
| 4583 | memprintf(errmsg, "'%s' : %s.", args[cur_arg], *errmsg); |
| 4584 | goto error; |
| 4585 | } |
| 4586 | |
| 4587 | proto = protocol_by_family(sk->ss_family); |
| 4588 | if (!proto || !proto->connect) { |
| 4589 | memprintf(errmsg, "'%s' : connect() not supported for this address family.\n", |
| 4590 | args[cur_arg]); |
| 4591 | goto error; |
| 4592 | } |
| 4593 | |
| 4594 | if (port1 != port2) { |
| 4595 | memprintf(errmsg, "'%s' : port ranges and offsets are not allowed in '%s'\n", |
| 4596 | args[cur_arg], args[cur_arg+1]); |
| 4597 | goto error; |
| 4598 | } |
| 4599 | |
| 4600 | cur_arg++; |
| 4601 | } |
Christopher Faulet | 4dce592 | 2020-03-30 13:54:42 +0200 | [diff] [blame] | 4602 | else if (strcmp(args[cur_arg], "port") == 0) { |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4603 | const char *p, *end; |
| 4604 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4605 | if (!*(args[cur_arg+1])) { |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4606 | memprintf(errmsg, "'%s' expects a port number or a sample expression as argument.", args[cur_arg]); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4607 | goto error; |
| 4608 | } |
| 4609 | cur_arg++; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4610 | |
| 4611 | port = 0; |
| 4612 | release_sample_expr(port_expr); |
| 4613 | p = args[cur_arg]; end = p + strlen(p); |
| 4614 | port = read_uint(&p, end); |
| 4615 | if (p != end) { |
| 4616 | int idx = 0; |
| 4617 | |
| 4618 | px->conf.args.ctx = ARGC_SRV; |
| 4619 | port_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx, |
| 4620 | file, line, errmsg, &px->conf.args, NULL); |
| 4621 | |
| 4622 | if (!port_expr) { |
| 4623 | memprintf(errmsg, "error detected while parsing port expression : %s", *errmsg); |
| 4624 | goto error; |
| 4625 | } |
| 4626 | if (!(port_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) { |
| 4627 | memprintf(errmsg, "error detected while parsing port expression : " |
| 4628 | " fetch method '%s' extracts information from '%s', " |
| 4629 | "none of which is available here.\n", |
| 4630 | args[cur_arg], sample_src_names(port_expr->fetch->use)); |
| 4631 | goto error; |
| 4632 | } |
| 4633 | px->http_needed |= !!(port_expr->fetch->use & SMP_USE_HTTP_ANY); |
| 4634 | } |
| 4635 | else if (port > 65535 || port < 1) { |
| 4636 | memprintf(errmsg, "expects a valid TCP port (from range 1 to 65535) or a sample expression, got %s.", |
| 4637 | args[cur_arg]); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4638 | goto error; |
| 4639 | } |
| 4640 | } |
| 4641 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 4642 | if (!*(args[cur_arg+1])) { |
| 4643 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4644 | goto error; |
| 4645 | } |
| 4646 | cur_arg++; |
| 4647 | free(comment); |
| 4648 | comment = strdup(args[cur_arg]); |
| 4649 | if (!comment) { |
| 4650 | memprintf(errmsg, "out of memory"); |
| 4651 | goto error; |
| 4652 | } |
| 4653 | } |
| 4654 | else if (strcmp(args[cur_arg], "send-proxy") == 0) |
| 4655 | conn_opts |= TCPCHK_OPT_SEND_PROXY; |
Christopher Faulet | 085426a | 2020-03-30 13:07:02 +0200 | [diff] [blame] | 4656 | else if (strcmp(args[cur_arg], "via-socks4") == 0) |
| 4657 | conn_opts |= TCPCHK_OPT_SOCKS4; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4658 | else if (strcmp(args[cur_arg], "linger") == 0) |
| 4659 | conn_opts |= TCPCHK_OPT_LINGER; |
| 4660 | #ifdef USE_OPENSSL |
| 4661 | else if (strcmp(args[cur_arg], "ssl") == 0) { |
| 4662 | px->options |= PR_O_TCPCHK_SSL; |
| 4663 | conn_opts |= TCPCHK_OPT_SSL; |
| 4664 | } |
Christopher Faulet | 79b31d4 | 2020-03-30 13:00:05 +0200 | [diff] [blame] | 4665 | else if (strcmp(args[cur_arg], "sni") == 0) { |
| 4666 | if (!*(args[cur_arg+1])) { |
| 4667 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4668 | goto error; |
| 4669 | } |
| 4670 | cur_arg++; |
| 4671 | free(sni); |
| 4672 | sni = strdup(args[cur_arg]); |
| 4673 | if (!sni) { |
| 4674 | memprintf(errmsg, "out of memory"); |
| 4675 | goto error; |
| 4676 | } |
| 4677 | } |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 4678 | else if (strcmp(args[cur_arg], "alpn") == 0) { |
| 4679 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4680 | free(alpn); |
| 4681 | if (ssl_sock_parse_alpn(args[cur_arg + 1], &alpn, &alpn_len, errmsg)) { |
| 4682 | memprintf(errmsg, "'%s' : %s", args[cur_arg], *errmsg); |
| 4683 | goto error; |
| 4684 | } |
| 4685 | cur_arg++; |
| 4686 | #else |
| 4687 | memprintf(errmsg, "'%s' : library does not support TLS ALPN extension.", args[cur_arg]); |
| 4688 | goto error; |
| 4689 | #endif |
| 4690 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4691 | #endif /* USE_OPENSSL */ |
| 4692 | |
| 4693 | else { |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4694 | memprintf(errmsg, "expects 'comment', 'port', 'addr', 'send-proxy'" |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4695 | #ifdef USE_OPENSSL |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 4696 | ", 'ssl', 'sni', 'alpn'" |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4697 | #endif /* USE_OPENSSL */ |
Christopher Faulet | 4dce592 | 2020-03-30 13:54:42 +0200 | [diff] [blame] | 4698 | " or 'via-socks4', 'linger', 'default' but got '%s' as argument.", |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4699 | args[cur_arg]); |
| 4700 | goto error; |
| 4701 | } |
| 4702 | cur_arg++; |
| 4703 | } |
| 4704 | |
| 4705 | chk = calloc(1, sizeof(*chk)); |
| 4706 | if (!chk) { |
| 4707 | memprintf(errmsg, "out of memory"); |
| 4708 | goto error; |
| 4709 | } |
Gaetan Rivet | 06d963a | 2020-02-21 18:49:05 +0100 | [diff] [blame] | 4710 | chk->action = TCPCHK_ACT_CONNECT; |
| 4711 | chk->comment = comment; |
| 4712 | chk->connect.port = port; |
| 4713 | chk->connect.options = conn_opts; |
Christopher Faulet | 79b31d4 | 2020-03-30 13:00:05 +0200 | [diff] [blame] | 4714 | chk->connect.sni = sni; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 4715 | chk->connect.alpn = alpn; |
| 4716 | chk->connect.alpn_len= alpn_len; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4717 | chk->connect.port_expr= port_expr; |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 4718 | if (sk) |
| 4719 | chk->connect.addr = *sk; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4720 | return chk; |
| 4721 | |
| 4722 | error: |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 4723 | free(alpn); |
Christopher Faulet | 79b31d4 | 2020-03-30 13:00:05 +0200 | [diff] [blame] | 4724 | free(sni); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4725 | free(comment); |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 4726 | release_sample_expr(port_expr); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4727 | return NULL; |
| 4728 | } |
| 4729 | |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4730 | static struct tcpcheck_rule *parse_tcpcheck_send(char **args, int cur_arg, struct proxy *px, struct list *rules, |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 4731 | const char *file, int line, char **errmsg) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4732 | { |
| 4733 | struct tcpcheck_rule *chk = NULL; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4734 | char *comment = NULL, *data = NULL; |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 4735 | enum tcpcheck_send_type type = TCPCHK_SEND_UNDEF; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4736 | |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 4737 | type = ((strcmp(args[cur_arg], "send-binary") == 0) ? TCPCHK_SEND_BINARY : TCPCHK_SEND_STRING); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4738 | if (!*(args[cur_arg+1])) { |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 4739 | memprintf(errmsg, "'%s' expects a %s as argument", |
| 4740 | (type == TCPCHK_SEND_BINARY ? "binary string": "string"), args[cur_arg]); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4741 | goto error; |
| 4742 | } |
| 4743 | |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4744 | data = args[cur_arg+1]; |
| 4745 | |
| 4746 | cur_arg += 2; |
| 4747 | while (*(args[cur_arg])) { |
| 4748 | if (strcmp(args[cur_arg], "comment") == 0) { |
| 4749 | if (!*(args[cur_arg+1])) { |
| 4750 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4751 | goto error; |
| 4752 | } |
| 4753 | cur_arg++; |
| 4754 | free(comment); |
| 4755 | comment = strdup(args[cur_arg]); |
| 4756 | if (!comment) { |
| 4757 | memprintf(errmsg, "out of memory"); |
| 4758 | goto error; |
| 4759 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4760 | } |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4761 | else if (strcmp(args[cur_arg], "log-format") == 0) { |
| 4762 | if (type == TCPCHK_SEND_BINARY) |
| 4763 | type = TCPCHK_SEND_BINARY_LF; |
| 4764 | else if (type == TCPCHK_SEND_STRING) |
| 4765 | type = TCPCHK_SEND_STRING_LF; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4766 | } |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4767 | else { |
| 4768 | memprintf(errmsg, "expects 'comment', 'log-format' but got '%s' as argument.", |
| 4769 | args[cur_arg]); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4770 | goto error; |
| 4771 | } |
| 4772 | cur_arg++; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | chk = calloc(1, sizeof(*chk)); |
| 4776 | if (!chk) { |
| 4777 | memprintf(errmsg, "out of memory"); |
| 4778 | goto error; |
| 4779 | } |
Gaetan Rivet | 48219dc | 2020-02-21 18:41:28 +0100 | [diff] [blame] | 4780 | chk->action = TCPCHK_ACT_SEND; |
| 4781 | chk->comment = comment; |
| 4782 | chk->send.type = type; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4783 | |
| 4784 | switch (chk->send.type) { |
| 4785 | case TCPCHK_SEND_STRING: |
| 4786 | chk->send.data = ist2(strdup(data), strlen(data)); |
| 4787 | if (!isttest(chk->send.data)) { |
| 4788 | memprintf(errmsg, "out of memory"); |
| 4789 | goto error; |
| 4790 | } |
| 4791 | break; |
| 4792 | case TCPCHK_SEND_BINARY: |
| 4793 | if (parse_binary(data, &chk->send.data.ptr, (int *)&chk->send.data.len, errmsg) == 0) { |
| 4794 | memprintf(errmsg, "'%s' invalid binary string (%s).\n", data, *errmsg); |
| 4795 | goto error; |
| 4796 | } |
| 4797 | break; |
| 4798 | case TCPCHK_SEND_STRING_LF: |
| 4799 | case TCPCHK_SEND_BINARY_LF: |
| 4800 | LIST_INIT(&chk->send.fmt); |
| 4801 | px->conf.args.ctx = ARGC_SRV; |
| 4802 | if (!parse_logformat_string(data, px, &chk->send.fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 4803 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", data, *errmsg); |
| 4804 | goto error; |
| 4805 | } |
| 4806 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4807 | case TCPCHK_SEND_HTTP: |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4808 | case TCPCHK_SEND_UNDEF: |
| 4809 | goto error; |
| 4810 | } |
| 4811 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4812 | return chk; |
| 4813 | |
| 4814 | error: |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 4815 | free(chk); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4816 | free(comment); |
| 4817 | return NULL; |
| 4818 | } |
| 4819 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4820 | static struct tcpcheck_rule *parse_tcpcheck_send_http(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 4821 | const char *file, int line, char **errmsg) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4822 | { |
| 4823 | struct tcpcheck_rule *chk = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4824 | struct tcpcheck_http_hdr *hdr = NULL; |
| 4825 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 4826 | char *meth = NULL, *uri = NULL, *vsn = NULL; |
| 4827 | char *body = NULL, *comment = NULL; |
| 4828 | unsigned int flags = 0; |
| 4829 | int i = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 4830 | |
| 4831 | cur_arg++; |
| 4832 | while (*(args[cur_arg])) { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 4833 | if (strcmp(args[cur_arg], "meth") == 0) { |
| 4834 | if (!*(args[cur_arg+1])) { |
| 4835 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4836 | goto error; |
| 4837 | } |
| 4838 | cur_arg++; |
| 4839 | meth = args[cur_arg]; |
| 4840 | } |
| 4841 | else if (strcmp(args[cur_arg], "uri") == 0) { |
| 4842 | if (!*(args[cur_arg+1])) { |
| 4843 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4844 | goto error; |
| 4845 | } |
| 4846 | cur_arg++; |
| 4847 | uri = args[cur_arg]; |
| 4848 | // TODO: log-format uri |
| 4849 | } |
| 4850 | else if (strcmp(args[cur_arg], "vsn") == 0) { |
| 4851 | if (!*(args[cur_arg+1])) { |
| 4852 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4853 | goto error; |
| 4854 | } |
| 4855 | cur_arg++; |
| 4856 | vsn = args[cur_arg]; |
| 4857 | } |
| 4858 | else if (strcmp(args[cur_arg], "hdr") == 0) { |
| 4859 | if (!*args[cur_arg+1] || !*args[cur_arg+2]) { |
| 4860 | memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg]); |
| 4861 | goto error; |
| 4862 | } |
| 4863 | hdrs[i].n = ist2(args[cur_arg+1], strlen(args[cur_arg+1])); |
| 4864 | hdrs[i].v = ist2(args[cur_arg+2], strlen(args[cur_arg+2])); |
| 4865 | i++; |
| 4866 | cur_arg += 2; |
| 4867 | } |
| 4868 | else if (strcmp(args[cur_arg], "body") == 0) { |
| 4869 | if (!*(args[cur_arg+1])) { |
| 4870 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4871 | goto error; |
| 4872 | } |
| 4873 | cur_arg++; |
| 4874 | body = args[cur_arg]; |
| 4875 | // TODO: log-format body |
| 4876 | } |
| 4877 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 4878 | if (!*(args[cur_arg+1])) { |
| 4879 | memprintf(errmsg, "'%s' expects a string as argument.", args[cur_arg]); |
| 4880 | goto error; |
| 4881 | } |
| 4882 | cur_arg++; |
| 4883 | free(comment); |
| 4884 | comment = strdup(args[cur_arg]); |
| 4885 | if (!comment) { |
| 4886 | memprintf(errmsg, "out of memory"); |
| 4887 | goto error; |
| 4888 | } |
| 4889 | } |
| 4890 | else { |
| 4891 | memprintf(errmsg, "expects 'comment', 'meth', 'uri', 'hdr' and 'body' but got '%s' as argument.", |
| 4892 | args[cur_arg]); |
| 4893 | goto error; |
| 4894 | } |
| 4895 | cur_arg++; |
| 4896 | } |
| 4897 | |
| 4898 | hdrs[i].n = hdrs[i].v = IST_NULL; |
| 4899 | |
| 4900 | chk = calloc(1, sizeof(*chk)); |
| 4901 | if (!chk) { |
| 4902 | memprintf(errmsg, "out of memory"); |
| 4903 | goto error; |
| 4904 | } |
| 4905 | chk->action = TCPCHK_ACT_SEND; |
| 4906 | chk->comment = comment; comment = NULL; |
| 4907 | chk->send.type = TCPCHK_SEND_HTTP; |
| 4908 | chk->send.http.flags = flags; |
| 4909 | LIST_INIT(&chk->send.http.hdrs); |
| 4910 | |
| 4911 | if (meth) { |
| 4912 | chk->send.http.meth.meth = find_http_meth(meth, strlen(meth)); |
| 4913 | chk->send.http.meth.str.area = strdup(meth); |
| 4914 | chk->send.http.meth.str.data = strlen(meth); |
| 4915 | if (!chk->send.http.meth.str.area) { |
| 4916 | memprintf(errmsg, "out of memory"); |
| 4917 | goto error; |
| 4918 | } |
| 4919 | } |
| 4920 | if (uri) { |
| 4921 | chk->send.http.uri = ist2(strdup(uri), strlen(uri)); |
| 4922 | if (!isttest(chk->send.http.uri)) { |
| 4923 | memprintf(errmsg, "out of memory"); |
| 4924 | goto error; |
| 4925 | } |
| 4926 | } |
| 4927 | if (vsn) { |
| 4928 | chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn)); |
| 4929 | if (!isttest(chk->send.http.vsn)) { |
| 4930 | memprintf(errmsg, "out of memory"); |
| 4931 | goto error; |
| 4932 | } |
| 4933 | } |
| 4934 | for (i = 0; hdrs[i].n.len; i++) { |
| 4935 | hdr = calloc(1, sizeof(*hdr)); |
| 4936 | if (!hdr) { |
| 4937 | memprintf(errmsg, "out of memory"); |
| 4938 | goto error; |
| 4939 | } |
| 4940 | LIST_INIT(&hdr->value); |
| 4941 | hdr->name = ist2(strdup(hdrs[i].n.ptr), hdrs[i].n.len); |
| 4942 | if (!hdr->name.ptr) { |
| 4943 | memprintf(errmsg, "out of memory"); |
| 4944 | goto error; |
| 4945 | } |
| 4946 | |
| 4947 | hdrs[i].v.ptr[hdrs[i].v.len] = '\0'; |
| 4948 | if (!parse_logformat_string(hdrs[i].v.ptr, px, &hdr->value, 0, SMP_VAL_BE_CHK_RUL, errmsg)) |
| 4949 | goto error; |
| 4950 | LIST_ADDQ(&chk->send.http.hdrs, &hdr->list); |
| 4951 | hdr = NULL; |
| 4952 | } |
| 4953 | |
| 4954 | if (body) { |
| 4955 | chk->send.http.body = ist2(strdup(body), strlen(body)); |
| 4956 | if (!isttest(chk->send.http.body)) { |
| 4957 | memprintf(errmsg, "out of memory"); |
| 4958 | goto error; |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | return chk; |
| 4963 | |
| 4964 | error: |
| 4965 | free_tcpcheck_http_hdr(hdr); |
| 4966 | free_tcpcheck(chk, 0); |
| 4967 | free(comment); |
| 4968 | return NULL; |
| 4969 | } |
| 4970 | |
| 4971 | static void tcpcheck_overwrite_send_http_rule(struct tcpcheck_rule *old, struct tcpcheck_rule *new) |
| 4972 | { |
| 4973 | struct logformat_node *lf, *lfb; |
| 4974 | struct tcpcheck_http_hdr *hdr, *bhdr; |
| 4975 | |
| 4976 | |
| 4977 | if (new->send.http.meth.str.area) { |
| 4978 | free(old->send.http.meth.str.area); |
| 4979 | old->send.http.meth.meth = new->send.http.meth.meth; |
| 4980 | old->send.http.meth.str.area = new->send.http.meth.str.area; |
| 4981 | old->send.http.meth.str.data = new->send.http.meth.str.data; |
| 4982 | new->send.http.meth.str = BUF_NULL; |
| 4983 | } |
| 4984 | |
| 4985 | if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && isttest(new->send.http.uri)) { |
| 4986 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
| 4987 | free(old->send.http.uri.ptr); |
| 4988 | else |
| 4989 | free_tcpcheck_fmt(&old->send.http.uri_fmt); |
| 4990 | old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_URI_FMT; |
| 4991 | old->send.http.uri = new->send.http.uri; |
| 4992 | new->send.http.uri = IST_NULL; |
| 4993 | } |
| 4994 | else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT) && !LIST_ISEMPTY(&new->send.http.uri_fmt)) { |
| 4995 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_URI_FMT)) |
| 4996 | free(old->send.http.uri.ptr); |
| 4997 | else |
| 4998 | free_tcpcheck_fmt(&old->send.http.uri_fmt); |
| 4999 | old->send.http.flags |= TCPCHK_SND_HTTP_FL_URI_FMT; |
| 5000 | LIST_INIT(&old->send.http.uri_fmt); |
| 5001 | list_for_each_entry_safe(lf, lfb, &new->send.http.uri_fmt, list) { |
| 5002 | LIST_DEL(&lf->list); |
| 5003 | LIST_ADDQ(&old->send.http.uri_fmt, &lf->list); |
| 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | if (isttest(new->send.http.vsn)) { |
| 5008 | free(old->send.http.vsn.ptr); |
| 5009 | old->send.http.vsn = new->send.http.vsn; |
| 5010 | new->send.http.vsn = IST_NULL; |
| 5011 | } |
| 5012 | |
| 5013 | free_tcpcheck_http_hdrs(&old->send.http.hdrs); |
| 5014 | list_for_each_entry_safe(hdr, bhdr, &new->send.http.hdrs, list) { |
| 5015 | LIST_DEL(&hdr->list); |
| 5016 | LIST_ADDQ(&old->send.http.hdrs, &hdr->list); |
| 5017 | } |
| 5018 | |
| 5019 | if (!(new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && isttest(new->send.http.body)) { |
| 5020 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
| 5021 | free(old->send.http.body.ptr); |
| 5022 | else |
| 5023 | free_tcpcheck_fmt(&old->send.http.body_fmt); |
| 5024 | old->send.http.flags &= ~TCPCHK_SND_HTTP_FL_BODY_FMT; |
| 5025 | old->send.http.body = new->send.http.body; |
| 5026 | new->send.http.body = IST_NULL; |
| 5027 | } |
| 5028 | else if ((new->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT) && !LIST_ISEMPTY(&new->send.http.body_fmt)) { |
| 5029 | if (!(old->send.http.flags & TCPCHK_SND_HTTP_FL_BODY_FMT)) |
| 5030 | free(old->send.http.body.ptr); |
| 5031 | else |
| 5032 | free_tcpcheck_fmt(&old->send.http.body_fmt); |
| 5033 | old->send.http.flags |= TCPCHK_SND_HTTP_FL_BODY_FMT; |
| 5034 | LIST_INIT(&old->send.http.body_fmt); |
| 5035 | list_for_each_entry_safe(lf, lfb, &new->send.http.body_fmt, list) { |
| 5036 | LIST_DEL(&lf->list); |
| 5037 | LIST_ADDQ(&old->send.http.body_fmt, &lf->list); |
| 5038 | } |
| 5039 | } |
| 5040 | } |
| 5041 | |
| 5042 | static int tcpcheck_add_http_rule(struct tcpcheck_rule *chk, struct tcpcheck_rules *rules, char **errmsg) |
| 5043 | { |
| 5044 | struct tcpcheck_rule *r; |
| 5045 | |
| 5046 | if (chk->action == TCPCHK_ACT_SEND && (chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 5047 | r = get_first_tcpcheck_rule(rules); |
| 5048 | if (r && r->action == TCPCHK_ACT_CONNECT) |
| 5049 | r = get_next_tcpcheck_rule(rules, r); |
| 5050 | if (!r || r->action != TCPCHK_ACT_SEND) |
| 5051 | LIST_ADD(rules->list, &chk->list); |
| 5052 | else if (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT) { |
| 5053 | LIST_DEL(&r->list); |
| 5054 | free_tcpcheck(r, 0); |
| 5055 | LIST_ADD(rules->list, &chk->list); |
| 5056 | } |
| 5057 | else { |
| 5058 | tcpcheck_overwrite_send_http_rule(r, chk); |
| 5059 | free_tcpcheck(chk, 0); |
| 5060 | } |
| 5061 | } |
| 5062 | else { |
| 5063 | r = get_last_tcpcheck_rule(rules); |
| 5064 | if (!r || (r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT))) |
| 5065 | /* no error */; |
| 5066 | else if (r->action != TCPCHK_ACT_CONNECT && chk->action == TCPCHK_ACT_SEND) { |
| 5067 | memprintf(errmsg, "unable to add http-check send rule at step %d (missing connect rule).", |
| 5068 | chk->index+1); |
| 5069 | return 0; |
| 5070 | } |
| 5071 | else if (r->action != TCPCHK_ACT_SEND && chk->action == TCPCHK_ACT_EXPECT) { |
| 5072 | memprintf(errmsg, "unable to add http-check expect rule at step %d (missing send rule).", |
| 5073 | chk->index+1); |
| 5074 | return 0; |
| 5075 | } |
| 5076 | else if (r->action != TCPCHK_ACT_EXPECT && chk->action == TCPCHK_ACT_CONNECT) { |
| 5077 | memprintf(errmsg, "unable to add http-check connect rule at step %d (missing expect rule).", |
| 5078 | chk->index+1); |
| 5079 | return 0; |
| 5080 | } |
| 5081 | |
| 5082 | if (chk->action == TCPCHK_ACT_SEND) { |
| 5083 | r = get_first_tcpcheck_rule(rules); |
| 5084 | if (r && r->action == TCPCHK_ACT_SEND && (r->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) { |
| 5085 | tcpcheck_overwrite_send_http_rule(r, chk); |
| 5086 | free_tcpcheck(chk, 0); |
| 5087 | LIST_DEL(&r->list); |
| 5088 | r->send.http.flags &= ~TCPCHK_SND_HTTP_FROM_OPT; |
| 5089 | chk = r; |
| 5090 | } |
| 5091 | } |
| 5092 | LIST_ADDQ(rules->list, &chk->list); |
| 5093 | } |
| 5094 | return 1; |
| 5095 | } |
| 5096 | |
| 5097 | static struct tcpcheck_rule *parse_tcpcheck_comment(char **args, int cur_arg, struct proxy *px, struct list *rules, |
| 5098 | const char *file, int line, char **errmsg) |
| 5099 | { |
| 5100 | struct tcpcheck_rule *chk = NULL; |
| 5101 | char *comment = NULL; |
| 5102 | |
| 5103 | if (!*(args[cur_arg+1])) { |
| 5104 | memprintf(errmsg, "expects a string as argument"); |
| 5105 | goto error; |
| 5106 | } |
| 5107 | cur_arg++; |
| 5108 | comment = strdup(args[cur_arg]); |
| 5109 | if (!comment) { |
| 5110 | memprintf(errmsg, "out of memory"); |
| 5111 | goto error; |
| 5112 | } |
| 5113 | |
| 5114 | chk = calloc(1, sizeof(*chk)); |
| 5115 | if (!chk) { |
| 5116 | memprintf(errmsg, "out of memory"); |
| 5117 | goto error; |
| 5118 | } |
| 5119 | chk->action = TCPCHK_ACT_COMMENT; |
| 5120 | chk->comment = comment; |
| 5121 | return chk; |
| 5122 | |
| 5123 | error: |
| 5124 | free(comment); |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | |
| 5128 | static struct tcpcheck_rule *parse_tcpcheck_expect(char **args, int cur_arg, struct proxy *px, |
| 5129 | struct list *rules, unsigned int proto, |
| 5130 | const char *file, int line, char **errmsg) |
| 5131 | { |
| 5132 | struct tcpcheck_rule *prev_check, *chk = NULL; |
| 5133 | struct sample_expr *status_expr = NULL; |
| 5134 | char *str, *on_success_msg, *on_error_msg, *comment, *pattern; |
| 5135 | enum tcpcheck_expect_type type = TCPCHK_EXPECT_UNDEF; |
| 5136 | enum healthcheck_status ok_st = HCHK_STATUS_L7OKD; |
| 5137 | enum healthcheck_status err_st = HCHK_STATUS_L7RSP; |
| 5138 | enum healthcheck_status tout_st = HCHK_STATUS_L7TOUT; |
| 5139 | long min_recv = -1; |
| 5140 | int inverse = 0, with_capture = 0; |
| 5141 | |
| 5142 | str = on_success_msg = on_error_msg = comment = pattern = NULL; |
| 5143 | if (!*(args[cur_arg+1])) { |
| 5144 | memprintf(errmsg, "expects at least a matching pattern as arguments"); |
| 5145 | goto error; |
| 5146 | } |
| 5147 | |
| 5148 | cur_arg++; |
| 5149 | while (*(args[cur_arg])) { |
| 5150 | int in_pattern = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5151 | |
| 5152 | rescan: |
| 5153 | if (strcmp(args[cur_arg], "min-recv") == 0) { |
| 5154 | if (in_pattern) { |
| 5155 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5156 | goto error; |
| 5157 | } |
| 5158 | if (!*(args[cur_arg+1])) { |
| 5159 | memprintf(errmsg, "'%s' expects a integer as argument", args[cur_arg]); |
| 5160 | goto error; |
| 5161 | } |
| 5162 | /* Use an signed integer here because of chksize */ |
| 5163 | cur_arg++; |
| 5164 | min_recv = atol(args[cur_arg]); |
| 5165 | if (min_recv < -1 || min_recv > INT_MAX) { |
| 5166 | memprintf(errmsg, "'%s' expects -1 or an integer from 0 to INT_MAX" , args[cur_arg-1]); |
| 5167 | goto error; |
| 5168 | } |
| 5169 | } |
| 5170 | else if (*(args[cur_arg]) == '!') { |
| 5171 | in_pattern = 1; |
| 5172 | while (*(args[cur_arg]) == '!') { |
| 5173 | inverse = !inverse; |
| 5174 | args[cur_arg]++; |
| 5175 | } |
| 5176 | if (!*(args[cur_arg])) |
| 5177 | cur_arg++; |
| 5178 | goto rescan; |
| 5179 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5180 | else if (strcmp(args[cur_arg], "string") == 0 || strcmp(args[cur_arg], "rstring") == 0) { |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5181 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 5182 | memprintf(errmsg, "only on pattern expected"); |
| 5183 | goto error; |
| 5184 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5185 | if (proto != TCPCHK_RULES_HTTP_CHK) |
| 5186 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_STRING : TCPCHK_EXPECT_REGEX); |
| 5187 | else |
| 5188 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_BODY : TCPCHK_EXPECT_HTTP_REGEX_BODY); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5189 | |
| 5190 | if (!*(args[cur_arg+1])) { |
| 5191 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 5192 | goto error; |
| 5193 | } |
| 5194 | cur_arg++; |
| 5195 | pattern = args[cur_arg]; |
| 5196 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5197 | else if (strcmp(args[cur_arg], "binary") == 0 || strcmp(args[cur_arg], "rbinary") == 0) { |
| 5198 | if (proto == TCPCHK_RULES_HTTP_CHK) |
| 5199 | goto bad_http_kw; |
| 5200 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 5201 | memprintf(errmsg, "only on pattern expected"); |
| 5202 | goto error; |
| 5203 | } |
| 5204 | type = ((*(args[cur_arg]) == 'b') ? TCPCHK_EXPECT_BINARY : TCPCHK_EXPECT_REGEX_BINARY); |
| 5205 | |
| 5206 | if (!*(args[cur_arg+1])) { |
| 5207 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 5208 | goto error; |
| 5209 | } |
| 5210 | cur_arg++; |
| 5211 | pattern = args[cur_arg]; |
| 5212 | } |
| 5213 | else if (strcmp(args[cur_arg], "status") == 0 || strcmp(args[cur_arg], "rstatus") == 0) { |
| 5214 | if (proto != TCPCHK_RULES_HTTP_CHK) |
| 5215 | goto bad_tcp_kw; |
| 5216 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 5217 | memprintf(errmsg, "only on pattern expected"); |
| 5218 | goto error; |
| 5219 | } |
| 5220 | type = ((*(args[cur_arg]) == 's') ? TCPCHK_EXPECT_HTTP_STATUS : TCPCHK_EXPECT_HTTP_REGEX_STATUS); |
| 5221 | |
| 5222 | if (!*(args[cur_arg+1])) { |
| 5223 | memprintf(errmsg, "'%s' expects a <pattern> as argument", args[cur_arg]); |
| 5224 | goto error; |
| 5225 | } |
| 5226 | cur_arg++; |
| 5227 | pattern = args[cur_arg]; |
| 5228 | } |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 5229 | else if (strcmp(args[cur_arg], "custom") == 0) { |
| 5230 | if (in_pattern) { |
| 5231 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5232 | goto error; |
| 5233 | } |
| 5234 | if (type != TCPCHK_EXPECT_UNDEF) { |
| 5235 | memprintf(errmsg, "only on pattern expected"); |
| 5236 | goto error; |
| 5237 | } |
| 5238 | type = TCPCHK_EXPECT_CUSTOM; |
| 5239 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5240 | else if (strcmp(args[cur_arg], "comment") == 0) { |
| 5241 | if (in_pattern) { |
| 5242 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5243 | goto error; |
| 5244 | } |
| 5245 | if (!*(args[cur_arg+1])) { |
| 5246 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 5247 | goto error; |
| 5248 | } |
| 5249 | cur_arg++; |
| 5250 | free(comment); |
| 5251 | comment = strdup(args[cur_arg]); |
| 5252 | if (!comment) { |
| 5253 | memprintf(errmsg, "out of memory"); |
| 5254 | goto error; |
| 5255 | } |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 5256 | } |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5257 | else if (strcmp(args[cur_arg], "on-success") == 0) { |
| 5258 | if (in_pattern) { |
| 5259 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5260 | goto error; |
| 5261 | } |
| 5262 | if (!*(args[cur_arg+1])) { |
| 5263 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 5264 | goto error; |
| 5265 | } |
| 5266 | cur_arg++; |
| 5267 | free(on_success_msg); |
| 5268 | on_success_msg = strdup(args[cur_arg]); |
| 5269 | if (!on_success_msg) { |
| 5270 | memprintf(errmsg, "out of memory"); |
| 5271 | goto error; |
| 5272 | } |
| 5273 | } |
| 5274 | else if (strcmp(args[cur_arg], "on-error") == 0) { |
| 5275 | if (in_pattern) { |
| 5276 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5277 | goto error; |
| 5278 | } |
| 5279 | if (!*(args[cur_arg+1])) { |
| 5280 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 5281 | goto error; |
| 5282 | } |
| 5283 | cur_arg++; |
| 5284 | free(on_error_msg); |
| 5285 | on_error_msg = strdup(args[cur_arg]); |
| 5286 | if (!on_error_msg) { |
| 5287 | memprintf(errmsg, "out of memory"); |
| 5288 | goto error; |
| 5289 | } |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 5290 | } |
| 5291 | else if (strcmp(args[cur_arg], "ok-status") == 0) { |
| 5292 | if (in_pattern) { |
| 5293 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5294 | goto error; |
| 5295 | } |
| 5296 | if (!*(args[cur_arg+1])) { |
| 5297 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 5298 | goto error; |
| 5299 | } |
| 5300 | if (strcasecmp(args[cur_arg+1], "L7OK") == 0) |
| 5301 | ok_st = HCHK_STATUS_L7OKD; |
| 5302 | else if (strcasecmp(args[cur_arg+1], "L7OKC") == 0) |
| 5303 | ok_st = HCHK_STATUS_L7OKCD; |
| 5304 | else if (strcasecmp(args[cur_arg+1], "L6OK") == 0) |
| 5305 | ok_st = HCHK_STATUS_L6OK; |
| 5306 | else if (strcasecmp(args[cur_arg+1], "L4OK") == 0) |
| 5307 | ok_st = HCHK_STATUS_L4OK; |
| 5308 | else { |
| 5309 | memprintf(errmsg, "'%s' only supports 'L4OK', 'L6OK', 'L7OK' or 'L7OKC' status (got '%s').", |
| 5310 | args[cur_arg], args[cur_arg+1]); |
| 5311 | goto error; |
| 5312 | } |
| 5313 | cur_arg++; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5314 | } |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 5315 | else if (strcmp(args[cur_arg], "error-status") == 0) { |
| 5316 | if (in_pattern) { |
| 5317 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5318 | goto error; |
| 5319 | } |
| 5320 | if (!*(args[cur_arg+1])) { |
| 5321 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 5322 | goto error; |
| 5323 | } |
| 5324 | if (strcasecmp(args[cur_arg+1], "L7RSP") == 0) |
| 5325 | err_st = HCHK_STATUS_L7RSP; |
| 5326 | else if (strcasecmp(args[cur_arg+1], "L7STS") == 0) |
| 5327 | err_st = HCHK_STATUS_L7STS; |
| 5328 | else if (strcasecmp(args[cur_arg+1], "L6RSP") == 0) |
| 5329 | err_st = HCHK_STATUS_L6RSP; |
| 5330 | else if (strcasecmp(args[cur_arg+1], "L4CON") == 0) |
| 5331 | err_st = HCHK_STATUS_L4CON; |
| 5332 | else { |
| 5333 | memprintf(errmsg, "'%s' only supports 'L4CON', 'L6RSP', 'L7RSP' or 'L7STS' status (got '%s').", |
| 5334 | args[cur_arg], args[cur_arg+1]); |
| 5335 | goto error; |
| 5336 | } |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5337 | cur_arg++; |
| 5338 | } |
| 5339 | else if (strcmp(args[cur_arg], "status-code") == 0) { |
| 5340 | int idx = 0; |
| 5341 | |
| 5342 | if (in_pattern) { |
| 5343 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5344 | goto error; |
| 5345 | } |
| 5346 | if (!*(args[cur_arg+1])) { |
| 5347 | memprintf(errmsg, "'%s' expects an expression as argument", args[cur_arg]); |
| 5348 | goto error; |
| 5349 | } |
| 5350 | |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 5351 | cur_arg++; |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5352 | release_sample_expr(status_expr); |
| 5353 | px->conf.args.ctx = ARGC_SRV; |
| 5354 | status_expr = sample_parse_expr((char *[]){args[cur_arg], NULL}, &idx, |
| 5355 | file, line, errmsg, &px->conf.args, NULL); |
| 5356 | if (!status_expr) { |
| 5357 | memprintf(errmsg, "error detected while parsing status-code expression : %s", *errmsg); |
| 5358 | goto error; |
| 5359 | } |
| 5360 | if (!(status_expr->fetch->val & SMP_VAL_BE_CHK_RUL)) { |
| 5361 | memprintf(errmsg, "error detected while parsing status-code expression : " |
| 5362 | " fetch method '%s' extracts information from '%s', " |
| 5363 | "none of which is available here.\n", |
| 5364 | args[cur_arg], sample_src_names(status_expr->fetch->use)); |
| 5365 | goto error; |
| 5366 | } |
| 5367 | px->http_needed |= !!(status_expr->fetch->use & SMP_USE_HTTP_ANY); |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 5368 | } |
| 5369 | else if (strcmp(args[cur_arg], "tout-status") == 0) { |
| 5370 | if (in_pattern) { |
| 5371 | memprintf(errmsg, "[!] not supported with '%s'", args[cur_arg]); |
| 5372 | goto error; |
| 5373 | } |
| 5374 | if (!*(args[cur_arg+1])) { |
| 5375 | memprintf(errmsg, "'%s' expects a string as argument", args[cur_arg]); |
| 5376 | goto error; |
| 5377 | } |
| 5378 | if (strcasecmp(args[cur_arg+1], "L7TOUT") == 0) |
| 5379 | tout_st = HCHK_STATUS_L7TOUT; |
| 5380 | else if (strcasecmp(args[cur_arg+1], "L6TOUT") == 0) |
| 5381 | tout_st = HCHK_STATUS_L6TOUT; |
| 5382 | else if (strcasecmp(args[cur_arg+1], "L4TOUT") == 0) |
| 5383 | tout_st = HCHK_STATUS_L4TOUT; |
| 5384 | else { |
| 5385 | memprintf(errmsg, "'%s' only supports 'L4TOUT', 'L6TOUT' or 'L7TOUT' status (got '%s').", |
| 5386 | args[cur_arg], args[cur_arg+1]); |
| 5387 | goto error; |
| 5388 | } |
| 5389 | cur_arg++; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5390 | } |
| 5391 | else { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5392 | if (proto == TCPCHK_RULES_HTTP_CHK) { |
| 5393 | bad_http_kw: |
| 5394 | memprintf(errmsg, "'only supports min-recv, [!]string', '[!]rstring', '[!]status', '[!]rstatus'" |
| 5395 | " or comment but got '%s' as argument.", args[cur_arg]); |
| 5396 | } |
| 5397 | else { |
| 5398 | bad_tcp_kw: |
| 5399 | memprintf(errmsg, "'only supports min-recv, '[!]binary', '[!]string', '[!]rstring', '[!]rbinary'" |
| 5400 | " or comment but got '%s' as argument.", args[cur_arg]); |
| 5401 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5402 | goto error; |
| 5403 | } |
| 5404 | |
| 5405 | cur_arg++; |
| 5406 | } |
| 5407 | |
| 5408 | if (comment) { |
| 5409 | char *p = comment; |
| 5410 | |
| 5411 | while (*p) { |
| 5412 | if (*p == '\\') { |
| 5413 | p++; |
| 5414 | if (!*p || !isdigit((unsigned char)*p) || |
| 5415 | (*p == 'x' && (!*(p+1) || !*(p+2) || !ishex(*(p+1)) || !ishex(*(p+2))))) { |
| 5416 | memprintf(errmsg, "invalid backreference in 'comment' argument"); |
| 5417 | goto error; |
| 5418 | } |
| 5419 | with_capture = 1; |
| 5420 | } |
| 5421 | p++; |
| 5422 | } |
| 5423 | if (with_capture && !inverse) |
| 5424 | memprintf(errmsg, "using backreference in a positive expect comment is useless"); |
| 5425 | } |
| 5426 | |
| 5427 | chk = calloc(1, sizeof(*chk)); |
| 5428 | if (!chk) { |
| 5429 | memprintf(errmsg, "out of memory"); |
| 5430 | goto error; |
| 5431 | } |
| 5432 | chk->action = TCPCHK_ACT_EXPECT; |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5433 | LIST_INIT(&chk->expect.onerror_fmt); |
| 5434 | LIST_INIT(&chk->expect.onsuccess_fmt); |
| 5435 | chk->comment = comment; comment = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5436 | chk->expect.type = type; |
| 5437 | chk->expect.min_recv = min_recv; |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 5438 | chk->expect.flags |= (inverse ? TCPCHK_EXPT_FL_INV : 0); |
| 5439 | chk->expect.flags |= (with_capture ? TCPCHK_EXPT_FL_CAP : 0); |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 5440 | chk->expect.ok_status = ok_st; |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 5441 | chk->expect.err_status = err_st; |
| 5442 | chk->expect.tout_status = tout_st; |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5443 | chk->expect.status_expr = status_expr; status_expr = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5444 | |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5445 | if (on_success_msg) { |
| 5446 | px->conf.args.ctx = ARGC_SRV; |
| 5447 | if (!parse_logformat_string(on_success_msg, px, &chk->expect.onsuccess_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 5448 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_success_msg, *errmsg); |
| 5449 | goto error; |
| 5450 | } |
| 5451 | free(on_success_msg); |
| 5452 | on_success_msg = NULL; |
| 5453 | } |
| 5454 | if (on_error_msg) { |
| 5455 | px->conf.args.ctx = ARGC_SRV; |
| 5456 | if (!parse_logformat_string(on_error_msg, px, &chk->expect.onerror_fmt, 0, SMP_VAL_BE_CHK_RUL, errmsg)) { |
| 5457 | memprintf(errmsg, "'%s' invalid log-format string (%s).\n", on_error_msg, *errmsg); |
| 5458 | goto error; |
| 5459 | } |
| 5460 | free(on_error_msg); |
| 5461 | on_error_msg = NULL; |
| 5462 | } |
| 5463 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5464 | switch (chk->expect.type) { |
| 5465 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5466 | case TCPCHK_EXPECT_HTTP_STATUS: |
| 5467 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 5468 | chk->expect.data = ist2(strdup(pattern), strlen(pattern)); |
| 5469 | if (!chk->expect.data.ptr) { |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5470 | memprintf(errmsg, "out of memory"); |
| 5471 | goto error; |
| 5472 | } |
| 5473 | break; |
| 5474 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | f930e4c | 2020-04-10 09:20:02 +0200 | [diff] [blame] | 5475 | if (parse_binary(pattern, &chk->expect.data.ptr, (int *)&chk->expect.data.len, errmsg) == 0) { |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5476 | memprintf(errmsg, "invalid binary string (%s)", *errmsg); |
| 5477 | goto error; |
| 5478 | } |
| 5479 | case TCPCHK_EXPECT_REGEX: |
| 5480 | case TCPCHK_EXPECT_REGEX_BINARY: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5481 | case TCPCHK_EXPECT_HTTP_REGEX_STATUS: |
| 5482 | case TCPCHK_EXPECT_HTTP_REGEX_BODY: |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5483 | chk->expect.regex = regex_comp(pattern, 1, with_capture, errmsg); |
| 5484 | if (!chk->expect.regex) |
| 5485 | goto error; |
| 5486 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 5487 | case TCPCHK_EXPECT_CUSTOM: |
| 5488 | chk->expect.custom = NULL; /* Must be defined by the caller ! */ |
| 5489 | break; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5490 | case TCPCHK_EXPECT_UNDEF: |
| 5491 | free(chk); |
| 5492 | memprintf(errmsg, "pattern not found"); |
| 5493 | goto error; |
| 5494 | } |
| 5495 | |
| 5496 | /* All tcp-check expect points back to the first inverse expect rule in |
| 5497 | * a chain of one or more expect rule, potentially itself. |
| 5498 | */ |
| 5499 | chk->expect.head = chk; |
| 5500 | list_for_each_entry_rev(prev_check, rules, list) { |
| 5501 | if (prev_check->action == TCPCHK_ACT_EXPECT) { |
Christopher Faulet | 12d5740 | 2020-04-10 09:58:42 +0200 | [diff] [blame] | 5502 | if (prev_check->expect.flags & TCPCHK_EXPT_FL_INV) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5503 | chk->expect.head = prev_check; |
| 5504 | continue; |
| 5505 | } |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 5506 | if (prev_check->action != TCPCHK_ACT_COMMENT && prev_check->action != TCPCHK_ACT_ACTION_KW) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5507 | break; |
| 5508 | } |
| 5509 | return chk; |
| 5510 | |
| 5511 | error: |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5512 | free_tcpcheck(chk, 0); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5513 | free(str); |
| 5514 | free(comment); |
Christopher Faulet | be52b4d | 2020-04-01 16:30:22 +0200 | [diff] [blame] | 5515 | free(on_success_msg); |
| 5516 | free(on_error_msg); |
Christopher Faulet | 98cc57c | 2020-04-01 20:52:31 +0200 | [diff] [blame] | 5517 | release_sample_expr(status_expr); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5518 | return NULL; |
| 5519 | } |
| 5520 | |
| 5521 | /* Parses the "tcp-check" proxy keyword */ |
| 5522 | static int proxy_parse_tcpcheck(char **args, int section, struct proxy *curpx, |
| 5523 | struct proxy *defpx, const char *file, int line, |
| 5524 | char **errmsg) |
| 5525 | { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5526 | struct tcpcheck_ruleset *rs = NULL; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5527 | struct tcpcheck_rule *chk = NULL; |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5528 | int index, cur_arg, ret = 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5529 | |
| 5530 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL)) |
| 5531 | ret = 1; |
| 5532 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5533 | /* Deduce the ruleset name from the proxy info */ |
| 5534 | chunk_printf(&trash, "*tcp-check-%s_%s-%d", |
| 5535 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 5536 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 5537 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5538 | rs = tcpcheck_ruleset_lookup(b_orig(&trash)); |
| 5539 | if (rs == NULL) { |
| 5540 | rs = tcpcheck_ruleset_create(b_orig(&trash)); |
| 5541 | if (rs == NULL) { |
| 5542 | memprintf(errmsg, "out of memory.\n"); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5543 | goto error; |
| 5544 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5545 | } |
| 5546 | |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5547 | index = 0; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5548 | if (!LIST_ISEMPTY(&rs->rules)) { |
| 5549 | chk = LIST_PREV(&rs->rules, typeof(chk), list); |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5550 | index = chk->index + 1; |
| 5551 | } |
| 5552 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5553 | cur_arg = 1; |
| 5554 | if (strcmp(args[cur_arg], "connect") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5555 | chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5556 | else if (strcmp(args[cur_arg], "send") == 0 || strcmp(args[cur_arg], "send-binary") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5557 | chk = parse_tcpcheck_send(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5558 | else if (strcmp(args[cur_arg], "expect") == 0) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5559 | chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, 0, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5560 | else if (strcmp(args[cur_arg], "comment") == 0) |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5561 | chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5562 | else { |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 5563 | struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]); |
| 5564 | |
| 5565 | if (!kw) { |
| 5566 | action_kw_tcp_check_build_list(&trash); |
| 5567 | memprintf(errmsg, "'%s' only supports 'comment', 'connect', 'send', 'send-binary', 'expect'" |
| 5568 | "%s%s. but got '%s'", |
| 5569 | args[0], (*trash.area ? ", " : ""), trash.area, args[1]); |
| 5570 | goto error; |
| 5571 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5572 | chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | if (!chk) { |
| 5576 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 5577 | goto error; |
| 5578 | } |
| 5579 | ret = (*errmsg != NULL); /* Handle warning */ |
| 5580 | |
| 5581 | /* No error: add the tcp-check rule in the list */ |
Gaetan Rivet | 5301b01 | 2020-02-25 17:19:17 +0100 | [diff] [blame] | 5582 | chk->index = index; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5583 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5584 | |
| 5585 | if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK && |
| 5586 | !(curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK)) { |
| 5587 | /* Use this ruleset if the proxy already has tcp-check enabled */ |
| 5588 | curpx->tcpcheck_rules.list = &rs->rules; |
| 5589 | curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_TCP_RS; |
| 5590 | } |
| 5591 | else { |
| 5592 | /* mark this ruleset as unused for now */ |
| 5593 | curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_TCP_RS; |
| 5594 | } |
| 5595 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5596 | return ret; |
| 5597 | |
| 5598 | error: |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5599 | free_tcpcheck(chk, 0); |
| 5600 | tcpcheck_ruleset_release(rs); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 5601 | return -1; |
| 5602 | } |
| 5603 | |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5604 | /* Parses the "http-check" proxy keyword */ |
| 5605 | static int proxy_parse_httpcheck(char **args, int section, struct proxy *curpx, |
| 5606 | struct proxy *defpx, const char *file, int line, |
| 5607 | char **errmsg) |
| 5608 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5609 | struct tcpcheck_ruleset *rs = NULL; |
| 5610 | struct tcpcheck_rule *chk = NULL; |
| 5611 | int index, cur_arg, ret = 0; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5612 | |
| 5613 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[0], NULL)) |
| 5614 | ret = 1; |
| 5615 | |
| 5616 | cur_arg = 1; |
| 5617 | if (strcmp(args[cur_arg], "disable-on-404") == 0) { |
| 5618 | /* enable a graceful server shutdown on an HTTP 404 response */ |
| 5619 | curpx->options |= PR_O_DISABLE404; |
| 5620 | if (too_many_args(1, args, errmsg, NULL)) |
| 5621 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5622 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5623 | } |
| 5624 | else if (strcmp(args[cur_arg], "send-state") == 0) { |
| 5625 | /* enable emission of the apparent state of a server in HTTP checks */ |
| 5626 | curpx->options2 |= PR_O2_CHK_SNDST; |
| 5627 | if (too_many_args(1, args, errmsg, NULL)) |
| 5628 | goto error; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5629 | goto out; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5630 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5631 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5632 | /* Deduce the ruleset name from the proxy info */ |
| 5633 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 5634 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 5635 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5636 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5637 | rs = tcpcheck_ruleset_lookup(b_orig(&trash)); |
| 5638 | if (rs == NULL) { |
| 5639 | rs = tcpcheck_ruleset_create(b_orig(&trash)); |
| 5640 | if (rs == NULL) { |
| 5641 | memprintf(errmsg, "out of memory.\n"); |
| 5642 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5643 | } |
| 5644 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5645 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5646 | index = 0; |
| 5647 | if (!LIST_ISEMPTY(&rs->rules)) { |
| 5648 | chk = LIST_PREV(&rs->rules, typeof(chk), list); |
| 5649 | if (chk->action != TCPCHK_ACT_SEND || !(chk->send.http.flags & TCPCHK_SND_HTTP_FROM_OPT)) |
| 5650 | index = chk->index + 1; |
| 5651 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5652 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5653 | if (strcmp(args[cur_arg], "connect") == 0) |
| 5654 | chk = parse_tcpcheck_connect(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 5655 | else if (strcmp(args[cur_arg], "send") == 0) |
| 5656 | chk = parse_tcpcheck_send_http(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 5657 | else if (strcmp(args[cur_arg], "expect") == 0) |
| 5658 | chk = parse_tcpcheck_expect(args, cur_arg, curpx, &rs->rules, TCPCHK_RULES_HTTP_CHK, |
| 5659 | file, line, errmsg); |
| 5660 | else if (strcmp(args[cur_arg], "comment") == 0) |
| 5661 | chk = parse_tcpcheck_comment(args, cur_arg, curpx, &rs->rules, file, line, errmsg); |
| 5662 | else { |
| 5663 | struct action_kw *kw = action_kw_tcp_check_lookup(args[cur_arg]); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5664 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5665 | if (!kw) { |
| 5666 | action_kw_tcp_check_build_list(&trash); |
| 5667 | memprintf(errmsg, "'%s' only supports 'disable-on-404', 'send-state', 'comment', 'connect'," |
| 5668 | " 'send', 'expect'%s%s. but got '%s'", |
| 5669 | args[0], (*trash.area ? ", " : ""), trash.area, args[1]); |
| 5670 | goto error; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5671 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5672 | chk = parse_tcpcheck_action(args, cur_arg, curpx, &rs->rules, kw, file, line, errmsg); |
| 5673 | } |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5674 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5675 | if (!chk) { |
| 5676 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 5677 | goto error; |
| 5678 | } |
| 5679 | ret = (*errmsg != NULL); /* Handle warning */ |
| 5680 | |
| 5681 | chk->index = index; |
| 5682 | if ((curpx->options2 & PR_O2_CHK_ANY) == PR_O2_TCPCHK_CHK && |
| 5683 | (curpx->tcpcheck_rules.flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 5684 | /* Use this ruleset if the proxy already has http-check enabled */ |
| 5685 | curpx->tcpcheck_rules.list = &rs->rules; |
| 5686 | curpx->tcpcheck_rules.flags &= ~TCPCHK_RULES_UNUSED_HTTP_RS; |
| 5687 | if (!tcpcheck_add_http_rule(chk, &curpx->tcpcheck_rules, errmsg)) { |
| 5688 | memprintf(errmsg, "'%s %s' : %s.", args[0], args[1], *errmsg); |
| 5689 | curpx->tcpcheck_rules.list = NULL; |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5690 | goto error; |
| 5691 | } |
| 5692 | } |
| 5693 | else { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5694 | /* mark this ruleset as unused for now */ |
| 5695 | curpx->tcpcheck_rules.flags |= TCPCHK_RULES_UNUSED_HTTP_RS; |
| 5696 | LIST_ADDQ(&rs->rules, &chk->list); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5697 | } |
| 5698 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5699 | out: |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5700 | return ret; |
| 5701 | |
| 5702 | error: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5703 | free_tcpcheck(chk, 0); |
| 5704 | tcpcheck_ruleset_release(rs); |
Christopher Faulet | 51b129f | 2020-04-09 15:54:18 +0200 | [diff] [blame] | 5705 | return -1; |
| 5706 | } |
| 5707 | |
Christopher Faulet | e9111b6 | 2020-04-09 18:12:08 +0200 | [diff] [blame] | 5708 | /* Parses the "external-check" proxy keyword */ |
| 5709 | static int proxy_parse_extcheck(char **args, int section, struct proxy *curpx, |
| 5710 | struct proxy *defpx, const char *file, int line, |
| 5711 | char **errmsg) |
| 5712 | { |
| 5713 | int cur_arg, ret = 0; |
| 5714 | |
| 5715 | cur_arg = 1; |
| 5716 | if (!*(args[cur_arg])) { |
| 5717 | memprintf(errmsg, "missing argument after '%s'.\n", args[0]); |
| 5718 | goto error; |
| 5719 | } |
| 5720 | |
| 5721 | if (strcmp(args[cur_arg], "command") == 0) { |
| 5722 | if (too_many_args(2, args, errmsg, NULL)) |
| 5723 | goto error; |
| 5724 | if (!*(args[cur_arg+1])) { |
| 5725 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 5726 | goto error; |
| 5727 | } |
| 5728 | free(curpx->check_command); |
| 5729 | curpx->check_command = strdup(args[cur_arg+1]); |
| 5730 | } |
| 5731 | else if (strcmp(args[cur_arg], "path") == 0) { |
| 5732 | if (too_many_args(2, args, errmsg, NULL)) |
| 5733 | goto error; |
| 5734 | if (!*(args[cur_arg+1])) { |
| 5735 | memprintf(errmsg, "missing argument after '%s'.", args[cur_arg]); |
| 5736 | goto error; |
| 5737 | } |
| 5738 | free(curpx->check_path); |
| 5739 | curpx->check_path = strdup(args[cur_arg+1]); |
| 5740 | } |
| 5741 | else { |
| 5742 | memprintf(errmsg, "'%s' only supports 'command' and 'path'. but got '%s'.", |
| 5743 | args[0], args[1]); |
| 5744 | goto error; |
| 5745 | } |
| 5746 | |
| 5747 | ret = (*errmsg != NULL); /* Handle warning */ |
| 5748 | return ret; |
| 5749 | |
| 5750 | error: |
| 5751 | return -1; |
| 5752 | } |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5753 | |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5754 | /* Parses the "option tcp-check" proxy keyword */ |
| 5755 | int proxy_parse_tcp_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5756 | const char *file, int line) |
| 5757 | { |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5758 | struct tcpcheck_ruleset *rs = NULL; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5759 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 5760 | int err_code = 0; |
| 5761 | |
| 5762 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 5763 | err_code |= ERR_WARN; |
| 5764 | |
| 5765 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 5766 | goto out; |
| 5767 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5768 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 5769 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 5770 | |
| 5771 | if (!(rules->flags & TCPCHK_RULES_PROTO_CHK)) { |
| 5772 | /* If a tcp-check rulesset is already set, do nothing */ |
| 5773 | if (rules->list) |
| 5774 | goto out; |
| 5775 | |
| 5776 | /* If a tcp-check ruleset is waiting to be used for the current proxy, |
| 5777 | * get it. |
| 5778 | */ |
| 5779 | if (rules->flags & TCPCHK_RULES_UNUSED_TCP_RS) |
| 5780 | goto curpx_ruleset; |
| 5781 | |
| 5782 | /* Otherwise, try to get the tcp-check ruleset of the default proxy */ |
| 5783 | chunk_printf(&trash, "*tcp-check-defaults_%s-%d", defpx->conf.file, defpx->conf.line); |
| 5784 | rs = tcpcheck_ruleset_lookup(b_orig(&trash)); |
| 5785 | if (rs) |
| 5786 | goto ruleset_found; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5787 | } |
| 5788 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5789 | curpx_ruleset: |
| 5790 | /* Deduce the ruleset name from the proxy info */ |
| 5791 | chunk_printf(&trash, "*tcp-check-%s_%s-%d", |
| 5792 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 5793 | curpx->conf.file, curpx->conf.line); |
| 5794 | |
| 5795 | rs = tcpcheck_ruleset_lookup(b_orig(&trash)); |
| 5796 | if (rs == NULL) { |
| 5797 | rs = tcpcheck_ruleset_create(b_orig(&trash)); |
| 5798 | if (rs == NULL) { |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5799 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 5800 | goto error; |
| 5801 | } |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5802 | } |
| 5803 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5804 | ruleset_found: |
| 5805 | free_tcpcheck_vars(&rules->preset_vars); |
| 5806 | rules->list = NULL; |
| 5807 | rules->flags = 0; |
| 5808 | |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5809 | rules->list = &rs->rules; |
Christopher Faulet | 430e480 | 2020-04-09 15:28:16 +0200 | [diff] [blame] | 5810 | |
| 5811 | out: |
| 5812 | return err_code; |
| 5813 | |
| 5814 | error: |
| 5815 | err_code |= ERR_ALERT | ERR_FATAL; |
| 5816 | goto out; |
| 5817 | } |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5818 | |
| 5819 | /* Parses the "option redis-check" proxy keyword */ |
| 5820 | int proxy_parse_redis_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5821 | const char *file, int line) |
| 5822 | { |
| 5823 | static char *redis_req = "*1\r\n$4\r\nPING\r\n"; |
| 5824 | static char *redis_res = "+PONG\r\n"; |
| 5825 | |
| 5826 | struct tcpcheck_ruleset *rs = NULL; |
| 5827 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 5828 | struct tcpcheck_rule *chk; |
| 5829 | char *errmsg = NULL; |
| 5830 | int err_code = 0; |
| 5831 | |
| 5832 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 5833 | err_code |= ERR_WARN; |
| 5834 | |
| 5835 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 5836 | goto out; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5837 | |
| 5838 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 5839 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 5840 | |
| 5841 | free_tcpcheck_vars(&rules->preset_vars); |
| 5842 | rules->list = NULL; |
| 5843 | rules->flags = 0; |
| 5844 | |
| 5845 | rs = tcpcheck_ruleset_lookup("*redis-check"); |
| 5846 | if (rs) |
| 5847 | goto ruleset_found; |
| 5848 | |
| 5849 | rs = tcpcheck_ruleset_create("*redis-check"); |
| 5850 | if (rs == NULL) { |
| 5851 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 5852 | goto error; |
| 5853 | } |
| 5854 | |
| 5855 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", redis_req, ""}, |
| 5856 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 5857 | if (!chk) { |
| 5858 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5859 | goto error; |
| 5860 | } |
| 5861 | chk->index = 0; |
| 5862 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5863 | |
| 5864 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "string", redis_res, |
| 5865 | "error-status", "L7STS", |
| 5866 | "on-error", "%[check.payload(),cut_crlf]", |
| 5867 | "on-success", "Redis server is ok", |
| 5868 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5869 | 1, curpx, &rs->rules, TCPCHK_RULES_REDIS_CHK, file, line, &errmsg); |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5870 | if (!chk) { |
| 5871 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5872 | goto error; |
| 5873 | } |
| 5874 | chk->index = 1; |
| 5875 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5876 | |
| 5877 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 5878 | |
| 5879 | ruleset_found: |
| 5880 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5881 | rules->flags |= TCPCHK_RULES_REDIS_CHK; |
Christopher Faulet | 33f05df | 2020-04-01 11:08:50 +0200 | [diff] [blame] | 5882 | |
| 5883 | out: |
| 5884 | free(errmsg); |
| 5885 | return err_code; |
| 5886 | |
| 5887 | error: |
| 5888 | tcpcheck_ruleset_release(rs); |
| 5889 | err_code |= ERR_ALERT | ERR_FATAL; |
| 5890 | goto out; |
| 5891 | } |
| 5892 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5893 | |
| 5894 | /* Parses the "option ssl-hello-chk" proxy keyword */ |
| 5895 | int proxy_parse_ssl_hello_chk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5896 | const char *file, int line) |
| 5897 | { |
| 5898 | /* This is the SSLv3 CLIENT HELLO packet used in conjunction with the |
| 5899 | * ssl-hello-chk option to ensure that the remote server speaks SSL. |
| 5900 | * |
| 5901 | * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details. |
| 5902 | */ |
| 5903 | static char sslv3_client_hello[] = { |
| 5904 | "16" /* ContentType : 0x16 = Hanshake */ |
| 5905 | "0300" /* ProtocolVersion : 0x0300 = SSLv3 */ |
| 5906 | "0079" /* ContentLength : 0x79 bytes after this one */ |
| 5907 | "01" /* HanshakeType : 0x01 = CLIENT HELLO */ |
| 5908 | "000075" /* HandshakeLength : 0x75 bytes after this one */ |
| 5909 | "0300" /* Hello Version : 0x0300 = v3 */ |
| 5910 | "%[date(),htonl,hex]" /* Unix GMT Time (s) : filled with <now> (@0x0B) */ |
| 5911 | "%[str(HAPROXYSSLCHK\nHAPROXYSSLCHK\n),hex]" /* Random : must be exactly 28 bytes */ |
| 5912 | "00" /* Session ID length : empty (no session ID) */ |
| 5913 | "004E" /* Cipher Suite Length : 78 bytes after this one */ |
| 5914 | "0001" "0002" "0003" "0004" /* 39 most common ciphers : */ |
| 5915 | "0005" "0006" "0007" "0008" /* 0x01...0x1B, 0x2F...0x3A */ |
| 5916 | "0009" "000A" "000B" "000C" /* This covers RSA/DH, */ |
| 5917 | "000D" "000E" "000F" "0010" /* various bit lengths, */ |
| 5918 | "0011" "0012" "0013" "0014" /* SHA1/MD5, DES/3DES/AES... */ |
| 5919 | "0015" "0016" "0017" "0018" |
| 5920 | "0019" "001A" "001B" "002F" |
| 5921 | "0030" "0031" "0032" "0033" |
| 5922 | "0034" "0035" "0036" "0037" |
| 5923 | "0038" "0039" "003A" |
| 5924 | "01" /* Compression Length : 0x01 = 1 byte for types */ |
| 5925 | "00" /* Compression Type : 0x00 = NULL compression */ |
| 5926 | }; |
| 5927 | |
| 5928 | struct tcpcheck_ruleset *rs = NULL; |
| 5929 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 5930 | struct tcpcheck_rule *chk; |
| 5931 | char *errmsg = NULL; |
| 5932 | int err_code = 0; |
| 5933 | |
| 5934 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 5935 | err_code |= ERR_WARN; |
| 5936 | |
| 5937 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 5938 | goto out; |
| 5939 | |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5940 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 5941 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 5942 | |
| 5943 | free_tcpcheck_vars(&rules->preset_vars); |
| 5944 | rules->list = NULL; |
| 5945 | rules->flags = 0; |
| 5946 | |
| 5947 | rs = tcpcheck_ruleset_lookup("*ssl-hello-check"); |
| 5948 | if (rs) |
| 5949 | goto ruleset_found; |
| 5950 | |
| 5951 | rs = tcpcheck_ruleset_create("*ssl-hello-check"); |
| 5952 | if (rs == NULL) { |
| 5953 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 5954 | goto error; |
| 5955 | } |
| 5956 | |
| 5957 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", sslv3_client_hello, "log-format", ""}, |
| 5958 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 5959 | if (!chk) { |
| 5960 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5961 | goto error; |
| 5962 | } |
| 5963 | chk->index = 0; |
| 5964 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5965 | |
| 5966 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^1[56]", |
Christopher Faulet | ec07e38 | 2020-04-07 14:56:26 +0200 | [diff] [blame] | 5967 | "min-recv", "5", "ok-status", "L6OK", |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5968 | "error-status", "L6RSP", "tout-status", "L6TOUT", |
| 5969 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 5970 | 1, curpx, &rs->rules, TCPCHK_RULES_SSL3_CHK, file, line, &errmsg); |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5971 | if (!chk) { |
| 5972 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 5973 | goto error; |
| 5974 | } |
| 5975 | chk->index = 1; |
| 5976 | LIST_ADDQ(&rs->rules, &chk->list); |
| 5977 | |
| 5978 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 5979 | |
| 5980 | ruleset_found: |
| 5981 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 5982 | rules->flags |= TCPCHK_RULES_SSL3_CHK; |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 5983 | |
| 5984 | out: |
| 5985 | free(errmsg); |
| 5986 | return err_code; |
| 5987 | |
| 5988 | error: |
| 5989 | tcpcheck_ruleset_release(rs); |
| 5990 | err_code |= ERR_ALERT | ERR_FATAL; |
| 5991 | goto out; |
| 5992 | } |
| 5993 | |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 5994 | /* Parses the "option smtpchk" proxy keyword */ |
| 5995 | int proxy_parse_smtpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 5996 | const char *file, int line) |
| 5997 | { |
| 5998 | static char *smtp_req = "%[var(check.smtp_cmd)]\r\n"; |
| 5999 | |
| 6000 | struct tcpcheck_ruleset *rs = NULL; |
| 6001 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6002 | struct tcpcheck_rule *chk; |
| 6003 | struct tcpcheck_var *var = NULL; |
| 6004 | char *cmd = NULL, *errmsg = NULL; |
| 6005 | int err_code = 0; |
| 6006 | |
| 6007 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6008 | err_code |= ERR_WARN; |
| 6009 | |
| 6010 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 6011 | goto out; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6012 | |
| 6013 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6014 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6015 | |
| 6016 | free_tcpcheck_vars(&rules->preset_vars); |
| 6017 | rules->list = NULL; |
| 6018 | rules->flags = 0; |
| 6019 | |
| 6020 | cur_arg += 2; |
| 6021 | if (*args[cur_arg] && *args[cur_arg+1] && |
| 6022 | (strcmp(args[cur_arg], "EHLO") == 0 || strcmp(args[cur_arg], "HELO") == 0)) { |
| 6023 | cmd = calloc(strlen(args[cur_arg]) + strlen(args[cur_arg+1]) + 1, sizeof(*cmd)); |
| 6024 | if (cmd) |
| 6025 | sprintf(cmd, "%s %s", args[cur_arg], args[cur_arg+1]); |
| 6026 | } |
| 6027 | else { |
| 6028 | /* this just hits the default for now, but you could potentially expand it to allow for other stuff |
| 6029 | though, it's unlikely you'd want to send anything other than an EHLO or HELO */ |
| 6030 | cmd = strdup("HELO localhost"); |
| 6031 | } |
| 6032 | |
| 6033 | var = tcpcheck_var_create("check.smtp_cmd"); |
| 6034 | if (cmd == NULL || var == NULL) { |
| 6035 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6036 | goto error; |
| 6037 | } |
| 6038 | var->data.type = SMP_T_STR; |
| 6039 | var->data.u.str.area = cmd; |
| 6040 | var->data.u.str.data = strlen(cmd); |
| 6041 | LIST_INIT(&var->list); |
| 6042 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6043 | cmd = NULL; |
| 6044 | var = NULL; |
| 6045 | |
| 6046 | rs = tcpcheck_ruleset_lookup("*smtp-check"); |
| 6047 | if (rs) |
| 6048 | goto ruleset_found; |
| 6049 | |
| 6050 | rs = tcpcheck_ruleset_create("*smtp-check"); |
| 6051 | if (rs == NULL) { |
| 6052 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6053 | goto error; |
| 6054 | } |
| 6055 | |
| 6056 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6057 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6058 | if (!chk) { |
| 6059 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6060 | goto error; |
| 6061 | } |
| 6062 | chk->index = 0; |
| 6063 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6064 | |
| 6065 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^[0-9]{3}[ \r]", |
| 6066 | "min-recv", "4", |
| 6067 | "error-status", "L7RSP", |
| 6068 | "on-error", "%[check.payload(),cut_crlf]", |
| 6069 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6070 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6071 | if (!chk) { |
| 6072 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6073 | goto error; |
| 6074 | } |
| 6075 | chk->index = 1; |
| 6076 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6077 | |
| 6078 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[ \r]", |
| 6079 | "min-recv", "4", |
| 6080 | "error-status", "L7STS", |
| 6081 | "on-error", "%[check.payload(4,0),ltrim(' '),cut_crlf]", |
| 6082 | "status-code", "check.payload(0,3)", |
| 6083 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6084 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6085 | if (!chk) { |
| 6086 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6087 | goto error; |
| 6088 | } |
| 6089 | chk->index = 2; |
| 6090 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6091 | |
| 6092 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", smtp_req, "log-format", ""}, |
| 6093 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6094 | if (!chk) { |
| 6095 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6096 | goto error; |
| 6097 | } |
| 6098 | chk->index = 3; |
| 6099 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6100 | |
| 6101 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rstring", "^2[0-9]{2}[- \r]", |
| 6102 | "min-recv", "4", |
| 6103 | "error-status", "L7STS", |
| 6104 | "on-error", "%[check.payload(4,0),ltrim(' '),cut_crlf]", |
| 6105 | "on-success", "%[check.payload(4,0),ltrim(' '),cut_crlf]", |
| 6106 | "status-code", "check.payload(0,3)", |
| 6107 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6108 | 1, curpx, &rs->rules, TCPCHK_RULES_SMTP_CHK, file, line, &errmsg); |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6109 | if (!chk) { |
| 6110 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6111 | goto error; |
| 6112 | } |
| 6113 | chk->index = 4; |
| 6114 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6115 | |
| 6116 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 6117 | |
| 6118 | ruleset_found: |
| 6119 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6120 | rules->flags |= TCPCHK_RULES_SMTP_CHK; |
Christopher Faulet | fbcc77c | 2020-04-01 20:54:05 +0200 | [diff] [blame] | 6121 | |
| 6122 | out: |
| 6123 | free(errmsg); |
| 6124 | return err_code; |
| 6125 | |
| 6126 | error: |
| 6127 | free(cmd); |
| 6128 | free(var); |
| 6129 | free_tcpcheck_vars(&rules->preset_vars); |
| 6130 | tcpcheck_ruleset_release(rs); |
| 6131 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6132 | goto out; |
| 6133 | } |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 6134 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6135 | /* Parses the "option pgsql-check" proxy keyword */ |
| 6136 | int proxy_parse_pgsql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6137 | const char *file, int line) |
| 6138 | { |
| 6139 | static char pgsql_req[] = { |
| 6140 | "%[var(check.plen),htonl,hex]" /* The packet length*/ |
| 6141 | "00030000" /* the version 3.0 */ |
| 6142 | "7573657200" /* "user" key */ |
| 6143 | "%[var(check.username),hex]00" /* the username */ |
| 6144 | "00" |
| 6145 | }; |
| 6146 | |
| 6147 | struct tcpcheck_ruleset *rs = NULL; |
| 6148 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6149 | struct tcpcheck_rule *chk; |
| 6150 | struct tcpcheck_var *var = NULL; |
| 6151 | char *user = NULL, *errmsg = NULL; |
| 6152 | size_t packetlen = 0; |
| 6153 | int err_code = 0; |
| 6154 | |
| 6155 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6156 | err_code |= ERR_WARN; |
| 6157 | |
| 6158 | if (alertif_too_many_args_idx(2, 1, file, line, args, &err_code)) |
| 6159 | goto out; |
| 6160 | |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6161 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6162 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6163 | |
| 6164 | free_tcpcheck_vars(&rules->preset_vars); |
| 6165 | rules->list = NULL; |
| 6166 | rules->flags = 0; |
| 6167 | |
| 6168 | cur_arg += 2; |
| 6169 | if (!*args[cur_arg] || !*args[cur_arg+1]) { |
| 6170 | ha_alert("parsing [%s:%d] : '%s %s' expects 'user <username>' as argument.\n", |
| 6171 | file, line, args[0], args[1]); |
| 6172 | goto error; |
| 6173 | } |
| 6174 | if (strcmp(args[cur_arg], "user") == 0) { |
| 6175 | packetlen = 15 + strlen(args[cur_arg+1]); |
| 6176 | user = strdup(args[cur_arg+1]); |
| 6177 | |
| 6178 | var = tcpcheck_var_create("check.username"); |
| 6179 | if (user == NULL || var == NULL) { |
| 6180 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6181 | goto error; |
| 6182 | } |
| 6183 | var->data.type = SMP_T_STR; |
| 6184 | var->data.u.str.area = user; |
| 6185 | var->data.u.str.data = strlen(user); |
| 6186 | LIST_INIT(&var->list); |
| 6187 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6188 | user = NULL; |
| 6189 | var = NULL; |
| 6190 | |
| 6191 | var = tcpcheck_var_create("check.plen"); |
| 6192 | if (var == NULL) { |
| 6193 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6194 | goto error; |
| 6195 | } |
| 6196 | var->data.type = SMP_T_SINT; |
| 6197 | var->data.u.sint = packetlen; |
| 6198 | LIST_INIT(&var->list); |
| 6199 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6200 | var = NULL; |
| 6201 | } |
| 6202 | else { |
| 6203 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user'.\n", |
| 6204 | file, line, args[0], args[1]); |
| 6205 | goto error; |
| 6206 | } |
| 6207 | |
| 6208 | rs = tcpcheck_ruleset_lookup("*pgsql-check"); |
| 6209 | if (rs) |
| 6210 | goto ruleset_found; |
| 6211 | |
| 6212 | rs = tcpcheck_ruleset_create("*pgsql-check"); |
| 6213 | if (rs == NULL) { |
| 6214 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6215 | goto error; |
| 6216 | } |
| 6217 | |
| 6218 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6219 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6220 | if (!chk) { |
| 6221 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6222 | goto error; |
| 6223 | } |
| 6224 | chk->index = 0; |
| 6225 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6226 | |
| 6227 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", pgsql_req, "log-format", ""}, |
| 6228 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6229 | if (!chk) { |
| 6230 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6231 | goto error; |
| 6232 | } |
| 6233 | chk->index = 1; |
| 6234 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6235 | |
| 6236 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "!rstring", "^E", |
| 6237 | "min-recv", "5", |
| 6238 | "error-status", "L7RSP", |
| 6239 | "on-error", "%[check.payload(6,0)]", |
| 6240 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6241 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6242 | if (!chk) { |
| 6243 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6244 | goto error; |
| 6245 | } |
| 6246 | chk->index = 2; |
| 6247 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6248 | |
| 6249 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^520000000800000000", |
| 6250 | "min-recv", "9", |
| 6251 | "error-status", "L7STS", |
| 6252 | "on-success", "PostgreSQL server is ok", |
| 6253 | "on-error", "PostgreSQL unknown error", |
| 6254 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6255 | 1, curpx, &rs->rules, TCPCHK_RULES_PGSQL_CHK, file, line, &errmsg); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6256 | if (!chk) { |
| 6257 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6258 | goto error; |
| 6259 | } |
| 6260 | chk->index = 3; |
| 6261 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6262 | |
| 6263 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 6264 | |
| 6265 | ruleset_found: |
| 6266 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6267 | rules->flags |= TCPCHK_RULES_PGSQL_CHK; |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6268 | |
| 6269 | out: |
| 6270 | free(errmsg); |
| 6271 | return err_code; |
| 6272 | |
| 6273 | error: |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6274 | free(user); |
| 6275 | free(var); |
| 6276 | free_tcpcheck_vars(&rules->preset_vars); |
| 6277 | tcpcheck_ruleset_release(rs); |
| 6278 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6279 | goto out; |
| 6280 | } |
| 6281 | |
| 6282 | |
| 6283 | /* Parses the "option mysql-check" proxy keyword */ |
| 6284 | int proxy_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6285 | const char *file, int line) |
| 6286 | { |
| 6287 | /* This is an example of a MySQL >=4.0 client Authentication packet kindly provided by Cyril Bonte. |
| 6288 | * const char mysql40_client_auth_pkt[] = { |
| 6289 | * "\x0e\x00\x00" // packet length |
| 6290 | * "\x01" // packet number |
| 6291 | * "\x00\x00" // client capabilities |
| 6292 | * "\x00\x00\x01" // max packet |
| 6293 | * "haproxy\x00" // username (null terminated string) |
| 6294 | * "\x00" // filler (always 0x00) |
| 6295 | * "\x01\x00\x00" // packet length |
| 6296 | * "\x00" // packet number |
| 6297 | * "\x01" // COM_QUIT command |
| 6298 | * }; |
| 6299 | */ |
| 6300 | static char mysql40_rsname[] = "*mysql40-check"; |
| 6301 | static char mysql40_req[] = { |
| 6302 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 6303 | "0080" /* client capabilities */ |
| 6304 | "000001" /* max packet */ |
| 6305 | "%[var(check.username),hex]00" /* the username */ |
| 6306 | "00" /* filler (always 0x00) */ |
| 6307 | "010000" /* packet length*/ |
| 6308 | "00" /* sequence ID */ |
| 6309 | "01" /* COM_QUIT command */ |
| 6310 | }; |
| 6311 | |
| 6312 | /* This is an example of a MySQL >=4.1 client Authentication packet provided by Nenad Merdanovic. |
| 6313 | * const char mysql41_client_auth_pkt[] = { |
| 6314 | * "\x0e\x00\x00\" // packet length |
| 6315 | * "\x01" // packet number |
| 6316 | * "\x00\x00\x00\x00" // client capabilities |
| 6317 | * "\x00\x00\x00\x01" // max packet |
| 6318 | * "\x21" // character set (UTF-8) |
| 6319 | * char[23] // All zeroes |
| 6320 | * "haproxy\x00" // username (null terminated string) |
| 6321 | * "\x00" // filler (always 0x00) |
| 6322 | * "\x01\x00\x00" // packet length |
| 6323 | * "\x00" // packet number |
| 6324 | * "\x01" // COM_QUIT command |
| 6325 | * }; |
| 6326 | */ |
| 6327 | static char mysql41_rsname[] = "*mysql41-check"; |
| 6328 | static char mysql41_req[] = { |
| 6329 | "%[var(check.header),hex]" /* 3 bytes for the packet length and 1 byte for the sequence ID */ |
| 6330 | "00820000" /* client capabilities */ |
| 6331 | "00800001" /* max packet */ |
| 6332 | "21" /* character set (UTF-8) */ |
| 6333 | "000000000000000000000000" /* 23 bytes, al zeroes */ |
| 6334 | "0000000000000000000000" |
| 6335 | "%[var(check.username),hex]00" /* the username */ |
| 6336 | "00" /* filler (always 0x00) */ |
| 6337 | "010000" /* packet length*/ |
| 6338 | "00" /* sequence ID */ |
| 6339 | "01" /* COM_QUIT command */ |
| 6340 | }; |
| 6341 | |
| 6342 | struct tcpcheck_ruleset *rs = NULL; |
| 6343 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6344 | struct tcpcheck_rule *chk; |
| 6345 | struct tcpcheck_var *var = NULL; |
| 6346 | char *mysql_rsname = "*mysql-check"; |
| 6347 | char *mysql_req = NULL, *hdr = NULL, *user = NULL, *errmsg = NULL; |
| 6348 | int index = 0, err_code = 0; |
| 6349 | |
| 6350 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6351 | err_code |= ERR_WARN; |
| 6352 | |
| 6353 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 6354 | goto out; |
| 6355 | |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6356 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6357 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6358 | |
| 6359 | free_tcpcheck_vars(&rules->preset_vars); |
| 6360 | rules->list = NULL; |
| 6361 | rules->flags = 0; |
| 6362 | |
| 6363 | cur_arg += 2; |
| 6364 | if (*args[cur_arg]) { |
| 6365 | char *user; |
| 6366 | int packetlen, userlen; |
| 6367 | |
| 6368 | if (strcmp(args[cur_arg], "user") != 0) { |
| 6369 | ha_alert("parsing [%s:%d] : '%s %s' only supports optional values: 'user' (got '%s').\n", |
| 6370 | file, line, args[0], args[1], args[cur_arg]); |
| 6371 | goto error; |
| 6372 | } |
| 6373 | |
| 6374 | if (*(args[cur_arg+1]) == 0) { |
| 6375 | ha_alert("parsing [%s:%d] : '%s %s %s' expects <username> as argument.\n", |
| 6376 | file, line, args[0], args[1], args[cur_arg]); |
| 6377 | goto error; |
| 6378 | } |
| 6379 | |
| 6380 | hdr = calloc(4, sizeof(*hdr)); |
| 6381 | user = strdup(args[cur_arg+1]); |
| 6382 | userlen = strlen(args[cur_arg+1]); |
| 6383 | |
| 6384 | if (hdr == NULL || user == NULL) { |
| 6385 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6386 | goto error; |
| 6387 | } |
| 6388 | |
| 6389 | if (*args[cur_arg+2]) { |
| 6390 | if (strcmp(args[cur_arg+2], "post-41") != 0) { |
| 6391 | ha_alert("parsing [%s:%d] : keyword '%s' only supports option 'post-41' (got '%s').\n", |
| 6392 | file, line, args[cur_arg], args[cur_arg+2]); |
| 6393 | goto error; |
| 6394 | } |
| 6395 | packetlen = userlen + 7 + 27; |
| 6396 | mysql_req = mysql41_req; |
| 6397 | mysql_rsname = mysql41_rsname; |
| 6398 | } |
| 6399 | else { |
| 6400 | packetlen = userlen + 7; |
| 6401 | mysql_req = mysql40_req; |
| 6402 | mysql_rsname = mysql40_rsname; |
| 6403 | } |
| 6404 | |
| 6405 | hdr[0] = (unsigned char)(packetlen & 0xff); |
| 6406 | hdr[1] = (unsigned char)((packetlen >> 8) & 0xff); |
| 6407 | hdr[2] = (unsigned char)((packetlen >> 16) & 0xff); |
| 6408 | hdr[3] = 1; |
| 6409 | |
| 6410 | var = tcpcheck_var_create("check.header"); |
| 6411 | if (var == NULL) { |
| 6412 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6413 | goto error; |
| 6414 | } |
| 6415 | var->data.type = SMP_T_STR; |
| 6416 | var->data.u.str.area = hdr; |
| 6417 | var->data.u.str.data = 4; |
| 6418 | LIST_INIT(&var->list); |
| 6419 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6420 | hdr = NULL; |
| 6421 | var = NULL; |
| 6422 | |
| 6423 | var = tcpcheck_var_create("check.username"); |
| 6424 | if (var == NULL) { |
| 6425 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6426 | goto error; |
| 6427 | } |
| 6428 | var->data.type = SMP_T_STR; |
| 6429 | var->data.u.str.area = user; |
| 6430 | var->data.u.str.data = strlen(user); |
| 6431 | LIST_INIT(&var->list); |
| 6432 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 6433 | user = NULL; |
| 6434 | var = NULL; |
| 6435 | } |
| 6436 | |
| 6437 | rs = tcpcheck_ruleset_lookup(mysql_rsname); |
| 6438 | if (rs) |
| 6439 | goto ruleset_found; |
| 6440 | |
| 6441 | rs = tcpcheck_ruleset_create(mysql_rsname); |
| 6442 | if (rs == NULL) { |
| 6443 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6444 | goto error; |
| 6445 | } |
| 6446 | |
| 6447 | chk = parse_tcpcheck_connect((char *[]){"tcp-check", "connect", "default", "linger", ""}, |
| 6448 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6449 | if (!chk) { |
| 6450 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6451 | goto error; |
| 6452 | } |
| 6453 | chk->index = index++; |
| 6454 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6455 | |
| 6456 | if (mysql_req) { |
| 6457 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", mysql_req, "log-format", ""}, |
| 6458 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6459 | if (!chk) { |
| 6460 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6461 | goto error; |
| 6462 | } |
| 6463 | chk->index = index++; |
| 6464 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6465 | } |
| 6466 | |
| 6467 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6468 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6469 | if (!chk) { |
| 6470 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6471 | goto error; |
| 6472 | } |
| 6473 | chk->expect.custom = tcpcheck_mysql_expect_iniths; |
| 6474 | chk->index = index++; |
| 6475 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6476 | |
| 6477 | if (mysql_req) { |
| 6478 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6479 | 1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg); |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6480 | if (!chk) { |
| 6481 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6482 | goto error; |
| 6483 | } |
| 6484 | chk->expect.custom = tcpcheck_mysql_expect_ok; |
| 6485 | chk->index = index++; |
| 6486 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6487 | } |
| 6488 | |
| 6489 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 6490 | |
| 6491 | ruleset_found: |
| 6492 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6493 | rules->flags |= TCPCHK_RULES_MYSQL_CHK; |
Christopher Faulet | f2b3be5 | 2020-04-02 18:07:37 +0200 | [diff] [blame] | 6494 | |
| 6495 | out: |
| 6496 | free(errmsg); |
| 6497 | return err_code; |
| 6498 | |
| 6499 | error: |
| 6500 | free(hdr); |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6501 | free(user); |
| 6502 | free(var); |
| 6503 | free_tcpcheck_vars(&rules->preset_vars); |
| 6504 | tcpcheck_ruleset_release(rs); |
| 6505 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6506 | goto out; |
| 6507 | } |
| 6508 | |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6509 | int proxy_parse_ldap_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6510 | const char *file, int line) |
| 6511 | { |
| 6512 | static char *ldap_req = "300C020101600702010304008000"; |
| 6513 | |
| 6514 | struct tcpcheck_ruleset *rs = NULL; |
| 6515 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6516 | struct tcpcheck_rule *chk; |
| 6517 | char *errmsg = NULL; |
| 6518 | int err_code = 0; |
| 6519 | |
| 6520 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6521 | err_code |= ERR_WARN; |
| 6522 | |
| 6523 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6524 | goto out; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6525 | |
| 6526 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6527 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6528 | |
| 6529 | free_tcpcheck_vars(&rules->preset_vars); |
| 6530 | rules->list = NULL; |
| 6531 | rules->flags = 0; |
| 6532 | |
| 6533 | rs = tcpcheck_ruleset_lookup("*ldap-check"); |
| 6534 | if (rs) |
| 6535 | goto ruleset_found; |
| 6536 | |
| 6537 | rs = tcpcheck_ruleset_create("*ldap-check"); |
| 6538 | if (rs == NULL) { |
| 6539 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6540 | goto error; |
| 6541 | } |
| 6542 | |
| 6543 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", ldap_req, ""}, |
| 6544 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6545 | if (!chk) { |
| 6546 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6547 | goto error; |
| 6548 | } |
| 6549 | chk->index = 0; |
| 6550 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6551 | |
| 6552 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "rbinary", "^30", |
| 6553 | "min-recv", "14", |
| 6554 | "on-error", "Not LDAPv3 protocol", |
| 6555 | ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6556 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6557 | if (!chk) { |
| 6558 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6559 | goto error; |
| 6560 | } |
| 6561 | chk->index = 1; |
| 6562 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6563 | |
| 6564 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6565 | 1, curpx, &rs->rules, TCPCHK_RULES_LDAP_CHK, file, line, &errmsg); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6566 | if (!chk) { |
| 6567 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6568 | goto error; |
| 6569 | } |
| 6570 | chk->expect.custom = tcpcheck_ldap_expect_bindrsp; |
| 6571 | chk->index = 2; |
| 6572 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6573 | |
| 6574 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 6575 | |
| 6576 | ruleset_found: |
| 6577 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6578 | rules->flags |= TCPCHK_RULES_LDAP_CHK; |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6579 | |
| 6580 | out: |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6581 | free(errmsg); |
| 6582 | return err_code; |
| 6583 | |
| 6584 | error: |
| 6585 | tcpcheck_ruleset_release(rs); |
| 6586 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6587 | goto out; |
| 6588 | } |
| 6589 | |
| 6590 | int proxy_parse_spop_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6591 | const char *file, int line) |
| 6592 | { |
| 6593 | struct tcpcheck_ruleset *rs = NULL; |
| 6594 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6595 | struct tcpcheck_rule *chk; |
| 6596 | char *spop_req = NULL; |
| 6597 | char *errmsg = NULL; |
| 6598 | int spop_len = 0, err_code = 0; |
| 6599 | |
| 6600 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6601 | err_code |= ERR_WARN; |
| 6602 | |
| 6603 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6604 | goto out; |
| 6605 | |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6606 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6607 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
| 6608 | |
| 6609 | free_tcpcheck_vars(&rules->preset_vars); |
| 6610 | rules->list = NULL; |
| 6611 | rules->flags = 0; |
| 6612 | |
| 6613 | |
| 6614 | rs = tcpcheck_ruleset_lookup("*spop-check"); |
| 6615 | if (rs) |
| 6616 | goto ruleset_found; |
| 6617 | |
| 6618 | rs = tcpcheck_ruleset_create("*spop-check"); |
| 6619 | if (rs == NULL) { |
| 6620 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6621 | goto error; |
| 6622 | } |
| 6623 | |
| 6624 | if (spoe_prepare_healthcheck_request(&spop_req, &spop_len) == -1) { |
| 6625 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6626 | goto error; |
| 6627 | } |
| 6628 | chunk_reset(&trash); |
| 6629 | dump_binary(&trash, spop_req, spop_len); |
| 6630 | trash.area[trash.data] = '\0'; |
| 6631 | |
| 6632 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary", b_head(&trash), ""}, |
| 6633 | 1, curpx, &rs->rules, file, line, &errmsg); |
| 6634 | if (!chk) { |
| 6635 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6636 | goto error; |
| 6637 | } |
| 6638 | chk->index = 0; |
| 6639 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6640 | |
| 6641 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", "min-recv", "4", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6642 | 1, curpx, &rs->rules, TCPCHK_RULES_SPOP_CHK, file, line, &errmsg); |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6643 | if (!chk) { |
| 6644 | ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg); |
| 6645 | goto error; |
| 6646 | } |
| 6647 | chk->expect.custom = tcpcheck_spop_expect_agenthello; |
| 6648 | chk->index = 1; |
| 6649 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6650 | |
| 6651 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 6652 | |
| 6653 | ruleset_found: |
| 6654 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 6655 | rules->flags |= TCPCHK_RULES_SPOP_CHK; |
Christopher Faulet | 267b01b | 2020-04-04 10:27:09 +0200 | [diff] [blame] | 6656 | |
| 6657 | out: |
| 6658 | free(spop_req); |
Christopher Faulet | 1997eca | 2020-04-03 23:13:50 +0200 | [diff] [blame] | 6659 | free(errmsg); |
| 6660 | return err_code; |
| 6661 | |
| 6662 | error: |
| 6663 | tcpcheck_ruleset_release(rs); |
| 6664 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6665 | goto out; |
| 6666 | } |
Christopher Faulet | ce35507 | 2020-04-02 11:44:39 +0200 | [diff] [blame] | 6667 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6668 | |
| 6669 | struct tcpcheck_rule *proxy_parse_httpchk_req(char **args, int cur_arg, struct proxy *px, char **errmsg) |
| 6670 | { |
| 6671 | struct tcpcheck_rule *chk = NULL; |
| 6672 | struct tcpcheck_http_hdr *hdr = NULL; |
| 6673 | char *meth = NULL, *uri = NULL, *vsn = NULL; |
| 6674 | char *hdrs, *body; |
| 6675 | |
| 6676 | hdrs = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n") : NULL); |
| 6677 | body = (*args[cur_arg+2] ? strstr(args[cur_arg+2], "\r\n\r\n") : NULL); |
| 6678 | if (hdrs == body) |
| 6679 | hdrs = NULL; |
| 6680 | if (hdrs) { |
| 6681 | *hdrs = '\0'; |
| 6682 | hdrs +=2; |
| 6683 | } |
| 6684 | if (body) { |
| 6685 | *body = '\0'; |
| 6686 | body += 4; |
| 6687 | } |
| 6688 | if (hdrs || body) { |
| 6689 | memprintf(errmsg, "hiding headers or body at the end of the version string is deprecated." |
| 6690 | " Please, consider to use 'http-check send' directive instead."); |
| 6691 | } |
| 6692 | |
| 6693 | chk = calloc(1, sizeof(*chk)); |
| 6694 | if (!chk) { |
| 6695 | memprintf(errmsg, "out of memory"); |
| 6696 | goto error; |
| 6697 | } |
| 6698 | chk->action = TCPCHK_ACT_SEND; |
| 6699 | chk->send.type = TCPCHK_SEND_HTTP; |
| 6700 | chk->send.http.flags |= TCPCHK_SND_HTTP_FROM_OPT; |
| 6701 | chk->send.http.meth.meth = HTTP_METH_OPTIONS; |
| 6702 | LIST_INIT(&chk->send.http.hdrs); |
| 6703 | |
| 6704 | /* Copy the method, uri and version */ |
| 6705 | if (*args[cur_arg]) { |
| 6706 | if (!*args[cur_arg+1]) |
| 6707 | uri = args[cur_arg]; |
| 6708 | else |
| 6709 | meth = args[cur_arg]; |
| 6710 | } |
| 6711 | if (*args[cur_arg+1]) |
| 6712 | uri = args[cur_arg+1]; |
| 6713 | if (*args[cur_arg+2]) |
| 6714 | vsn = args[cur_arg+2]; |
| 6715 | |
| 6716 | if (meth) { |
| 6717 | chk->send.http.meth.meth = find_http_meth(meth, strlen(meth)); |
| 6718 | chk->send.http.meth.str.area = strdup(meth); |
| 6719 | chk->send.http.meth.str.data = strlen(meth); |
| 6720 | if (!chk->send.http.meth.str.area) { |
| 6721 | memprintf(errmsg, "out of memory"); |
| 6722 | goto error; |
| 6723 | } |
| 6724 | } |
| 6725 | if (uri) { |
| 6726 | chk->send.http.uri = ist2(strdup(uri), strlen(uri)); |
| 6727 | if (!chk->send.http.uri.ptr) { |
| 6728 | memprintf(errmsg, "out of memory"); |
| 6729 | goto error; |
| 6730 | } |
| 6731 | } |
| 6732 | if (vsn) { |
| 6733 | chk->send.http.vsn = ist2(strdup(vsn), strlen(vsn)); |
| 6734 | if (!chk->send.http.vsn.ptr) { |
| 6735 | memprintf(errmsg, "out of memory"); |
| 6736 | goto error; |
| 6737 | } |
| 6738 | } |
| 6739 | |
| 6740 | /* Copy the header */ |
| 6741 | if (hdrs) { |
| 6742 | struct http_hdr tmp_hdrs[global.tune.max_http_hdr]; |
| 6743 | struct h1m h1m; |
| 6744 | int i, ret; |
| 6745 | |
| 6746 | /* Build and parse the request */ |
| 6747 | chunk_printf(&trash, "%s\r\n\r\n", hdrs); |
| 6748 | |
| 6749 | h1m.flags = H1_MF_HDRS_ONLY; |
| 6750 | ret = h1_headers_to_hdr_list(b_orig(&trash), b_tail(&trash), |
| 6751 | tmp_hdrs, sizeof(tmp_hdrs)/sizeof(tmp_hdrs[0]), |
| 6752 | &h1m, NULL); |
| 6753 | if (ret <= 0) { |
| 6754 | memprintf(errmsg, "unable to parse the request '%s'.", b_orig(&trash)); |
| 6755 | goto error; |
| 6756 | } |
| 6757 | |
| 6758 | for (i = 0; tmp_hdrs[i].n.len; i++) { |
| 6759 | hdr = calloc(1, sizeof(*hdr)); |
| 6760 | if (!hdr) { |
| 6761 | memprintf(errmsg, "out of memory"); |
| 6762 | goto error; |
| 6763 | } |
| 6764 | LIST_INIT(&hdr->value); |
| 6765 | hdr->name = ist2(strdup(tmp_hdrs[i].n.ptr), tmp_hdrs[i].n.len); |
| 6766 | if (!hdr->name.ptr) { |
| 6767 | memprintf(errmsg, "out of memory"); |
| 6768 | goto error; |
| 6769 | } |
| 6770 | |
| 6771 | tmp_hdrs[i].v.ptr[tmp_hdrs[i].v.len] = '\0'; |
| 6772 | if (!parse_logformat_string(tmp_hdrs[i].v.ptr, px, &hdr->value, 0, SMP_VAL_BE_CHK_RUL, errmsg)) |
| 6773 | goto error; |
| 6774 | LIST_ADDQ(&chk->send.http.hdrs, &hdr->list); |
| 6775 | } |
| 6776 | } |
| 6777 | |
| 6778 | /* Copy the body */ |
| 6779 | if (body) { |
| 6780 | chk->send.http.body = ist2(strdup(body), strlen(body)); |
| 6781 | if (!chk->send.http.body.ptr) { |
| 6782 | memprintf(errmsg, "out of memory"); |
| 6783 | goto error; |
| 6784 | } |
| 6785 | } |
| 6786 | |
| 6787 | return chk; |
| 6788 | |
| 6789 | error: |
| 6790 | free_tcpcheck_http_hdr(hdr); |
| 6791 | free_tcpcheck(chk, 0); |
| 6792 | return NULL; |
| 6793 | } |
| 6794 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6795 | int proxy_parse_httpchk_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6796 | const char *file, int line) |
| 6797 | { |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6798 | struct tcpcheck_ruleset *rs = NULL; |
| 6799 | struct tcpcheck_rules *rules = &curpx->tcpcheck_rules; |
| 6800 | struct tcpcheck_rule *chk; |
| 6801 | char *errmsg = NULL; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6802 | int err_code = 0; |
| 6803 | |
| 6804 | if (warnifnotcap(curpx, PR_CAP_BE, file, line, args[cur_arg+1], NULL)) |
| 6805 | err_code |= ERR_WARN; |
| 6806 | |
| 6807 | if (alertif_too_many_args_idx(3, 1, file, line, args, &err_code)) |
| 6808 | goto out; |
| 6809 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6810 | chk = proxy_parse_httpchk_req(args, cur_arg+2, curpx, &errmsg); |
| 6811 | if (!chk) { |
| 6812 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 6813 | goto error; |
| 6814 | } |
| 6815 | if (errmsg) { |
| 6816 | ha_warning("parsing [%s:%d]: '%s %s' : %s\n", file, line, args[0], args[1], errmsg); |
| 6817 | err_code |= ERR_WARN; |
| 6818 | free(errmsg); |
| 6819 | errmsg = NULL; |
| 6820 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6821 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6822 | no_request: |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6823 | curpx->options2 &= ~PR_O2_CHK_ANY; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6824 | curpx->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6825 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6826 | free_tcpcheck_vars(&rules->preset_vars); |
| 6827 | rules->list = NULL; |
| 6828 | rules->flags |= TCPCHK_SND_HTTP_FROM_OPT; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6829 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6830 | /* Deduce the ruleset name from the proxy info */ |
| 6831 | chunk_printf(&trash, "*http-check-%s_%s-%d", |
| 6832 | ((curpx == defpx) ? "defaults" : curpx->id), |
| 6833 | curpx->conf.file, curpx->conf.line); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6834 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6835 | rs = tcpcheck_ruleset_lookup(b_orig(&trash)); |
| 6836 | if (rs == NULL) { |
| 6837 | rs = tcpcheck_ruleset_create(b_orig(&trash)); |
| 6838 | if (rs == NULL) { |
| 6839 | ha_alert("parsing [%s:%d] : out of memory.\n", file, line); |
| 6840 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6841 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6842 | } |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6843 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6844 | rules->list = &rs->rules; |
| 6845 | rules->flags |= TCPCHK_RULES_HTTP_CHK; |
| 6846 | if (!tcpcheck_add_http_rule(chk, rules, &errmsg)) { |
| 6847 | ha_alert("parsing [%s:%d] : '%s %s' : %s.\n", file, line, args[0], args[1], errmsg); |
| 6848 | rules->list = NULL; |
| 6849 | goto error; |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6850 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6851 | |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6852 | out: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6853 | free(errmsg); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6854 | return err_code; |
| 6855 | |
| 6856 | error: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6857 | tcpcheck_ruleset_release(rs); |
| 6858 | free_tcpcheck(chk, 0); |
Christopher Faulet | 6c2a743 | 2020-04-09 14:48:48 +0200 | [diff] [blame] | 6859 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6860 | goto out; |
| 6861 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 6862 | |
Christopher Faulet | 6f55791 | 2020-04-09 15:58:50 +0200 | [diff] [blame] | 6863 | int proxy_parse_external_check_opt(char **args, int cur_arg, struct proxy *curpx, struct proxy *defpx, |
| 6864 | const char *file, int line) |
| 6865 | { |
| 6866 | int err_code = 0; |
| 6867 | |
Christopher Faulet | 6f55791 | 2020-04-09 15:58:50 +0200 | [diff] [blame] | 6868 | curpx->options2 &= ~PR_O2_CHK_ANY; |
| 6869 | curpx->options2 |= PR_O2_EXT_CHK; |
| 6870 | if (alertif_too_many_args_idx(0, 1, file, line, args, &err_code)) |
| 6871 | goto out; |
| 6872 | |
| 6873 | out: |
| 6874 | return err_code; |
| 6875 | } |
| 6876 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 6877 | /* Parse the "addr" server keyword */ |
| 6878 | static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 6879 | char **errmsg) |
| 6880 | { |
| 6881 | struct sockaddr_storage *sk; |
| 6882 | struct protocol *proto; |
| 6883 | int port1, port2, err_code = 0; |
| 6884 | |
| 6885 | |
| 6886 | if (!*args[*cur_arg+1]) { |
| 6887 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]); |
| 6888 | goto error; |
| 6889 | } |
| 6890 | |
| 6891 | sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, errmsg, NULL, NULL, 1); |
| 6892 | if (!sk) { |
| 6893 | memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg); |
| 6894 | goto error; |
| 6895 | } |
| 6896 | |
| 6897 | proto = protocol_by_family(sk->ss_family); |
| 6898 | if (!proto || !proto->connect) { |
| 6899 | memprintf(errmsg, "'%s %s' : connect() not supported for this address family.", |
| 6900 | args[*cur_arg], args[*cur_arg+1]); |
| 6901 | goto error; |
| 6902 | } |
| 6903 | |
| 6904 | if (port1 != port2) { |
| 6905 | memprintf(errmsg, "'%s' : port ranges and offsets are not allowed in '%s'.", |
| 6906 | args[*cur_arg], args[*cur_arg+1]); |
| 6907 | goto error; |
| 6908 | } |
| 6909 | |
| 6910 | srv->check.addr = srv->agent.addr = *sk; |
| 6911 | srv->flags |= SRV_F_CHECKADDR; |
| 6912 | srv->flags |= SRV_F_AGENTADDR; |
| 6913 | |
| 6914 | out: |
| 6915 | return err_code; |
| 6916 | |
| 6917 | error: |
| 6918 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6919 | goto out; |
| 6920 | } |
| 6921 | |
| 6922 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 6923 | /* Parse the "agent-addr" server keyword */ |
| 6924 | static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 6925 | char **errmsg) |
| 6926 | { |
| 6927 | int err_code = 0; |
| 6928 | |
| 6929 | if (!*(args[*cur_arg+1])) { |
| 6930 | memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]); |
| 6931 | goto error; |
| 6932 | } |
| 6933 | if(str2ip(args[*cur_arg+1], &srv->agent.addr) == NULL) { |
| 6934 | memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]); |
| 6935 | goto error; |
| 6936 | } |
| 6937 | |
| 6938 | out: |
| 6939 | return err_code; |
| 6940 | |
| 6941 | error: |
| 6942 | err_code |= ERR_ALERT | ERR_FATAL; |
| 6943 | goto out; |
| 6944 | } |
| 6945 | |
| 6946 | /* Parse the "agent-check" server keyword */ |
| 6947 | static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 6948 | char **errmsg) |
| 6949 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6950 | struct tcpcheck_ruleset *rs = NULL; |
| 6951 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 6952 | struct tcpcheck_rule *chk; |
| 6953 | int err_code = 0; |
| 6954 | |
| 6955 | if (srv->do_agent) |
| 6956 | goto out; |
| 6957 | |
| 6958 | if (!rules) { |
| 6959 | rules = calloc(1, sizeof(*rules)); |
| 6960 | if (!rules) { |
| 6961 | memprintf(errmsg, "out of memory."); |
| 6962 | goto error; |
| 6963 | } |
| 6964 | LIST_INIT(&rules->preset_vars); |
| 6965 | srv->agent.tcpcheck_rules = rules; |
| 6966 | } |
| 6967 | rules->list = NULL; |
| 6968 | rules->flags = 0; |
| 6969 | |
| 6970 | rs = tcpcheck_ruleset_lookup("*agent-check"); |
| 6971 | if (rs) |
| 6972 | goto ruleset_found; |
| 6973 | |
| 6974 | rs = tcpcheck_ruleset_create("*agent-check"); |
| 6975 | if (rs == NULL) { |
| 6976 | memprintf(errmsg, "out of memory."); |
| 6977 | goto error; |
| 6978 | } |
| 6979 | |
| 6980 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send", "%[var(check.agent_string)]", "log-format", ""}, |
| 6981 | 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg); |
| 6982 | if (!chk) { |
| 6983 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 6984 | goto error; |
| 6985 | } |
| 6986 | chk->index = 0; |
| 6987 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6988 | |
| 6989 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 6990 | 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK, |
| 6991 | srv->conf.file, srv->conf.line, errmsg); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 6992 | if (!chk) { |
| 6993 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 6994 | goto error; |
| 6995 | } |
| 6996 | chk->expect.custom = tcpcheck_agent_expect_reply; |
| 6997 | chk->index = 1; |
| 6998 | LIST_ADDQ(&rs->rules, &chk->list); |
| 6999 | |
| 7000 | LIST_ADDQ(&tcpchecks_list, &rs->list); |
| 7001 | |
| 7002 | ruleset_found: |
| 7003 | rules->list = &rs->rules; |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 7004 | rules->flags |= TCPCHK_RULES_AGENT_CHK; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7005 | srv->do_agent = 1; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7006 | |
| 7007 | out: |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7008 | return 0; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7009 | |
| 7010 | error: |
| 7011 | deinit_srv_agent_check(srv); |
| 7012 | tcpcheck_ruleset_release(rs); |
| 7013 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7014 | goto out; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7015 | } |
| 7016 | |
| 7017 | /* Parse the "agent-inter" server keyword */ |
| 7018 | static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7019 | char **errmsg) |
| 7020 | { |
| 7021 | const char *err = NULL; |
| 7022 | unsigned int delay; |
| 7023 | int err_code = 0; |
| 7024 | |
| 7025 | if (!*(args[*cur_arg+1])) { |
| 7026 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7027 | goto error; |
| 7028 | } |
| 7029 | |
| 7030 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7031 | if (err == PARSE_TIME_OVER) { |
| 7032 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7033 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7034 | goto error; |
| 7035 | } |
| 7036 | else if (err == PARSE_TIME_UNDER) { |
| 7037 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7038 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7039 | goto error; |
| 7040 | } |
| 7041 | else if (err) { |
| 7042 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7043 | *err, srv->id); |
| 7044 | goto error; |
| 7045 | } |
| 7046 | if (delay <= 0) { |
| 7047 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7048 | delay, args[*cur_arg], srv->id); |
| 7049 | goto error; |
| 7050 | } |
| 7051 | srv->agent.inter = delay; |
| 7052 | |
| 7053 | out: |
| 7054 | return err_code; |
| 7055 | |
| 7056 | error: |
| 7057 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7058 | goto out; |
| 7059 | } |
| 7060 | |
| 7061 | /* Parse the "agent-port" server keyword */ |
| 7062 | static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7063 | char **errmsg) |
| 7064 | { |
| 7065 | int err_code = 0; |
| 7066 | |
| 7067 | if (!*(args[*cur_arg+1])) { |
| 7068 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 7069 | goto error; |
| 7070 | } |
| 7071 | |
| 7072 | global.maxsock++; |
| 7073 | srv->agent.port = atol(args[*cur_arg+1]); |
| 7074 | |
| 7075 | out: |
| 7076 | return err_code; |
| 7077 | |
| 7078 | error: |
| 7079 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7080 | goto out; |
| 7081 | } |
| 7082 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7083 | int set_srv_agent_send(struct server *srv, const char *send) |
| 7084 | { |
| 7085 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 7086 | struct tcpcheck_var *var = NULL; |
| 7087 | char *str; |
| 7088 | |
| 7089 | str = strdup(send); |
| 7090 | var = tcpcheck_var_create("check.agent_string"); |
| 7091 | if (str == NULL || var == NULL) |
| 7092 | goto error; |
| 7093 | |
| 7094 | free_tcpcheck_vars(&rules->preset_vars); |
| 7095 | |
| 7096 | var->data.type = SMP_T_STR; |
| 7097 | var->data.u.str.area = str; |
| 7098 | var->data.u.str.data = strlen(str); |
| 7099 | LIST_INIT(&var->list); |
| 7100 | LIST_ADDQ(&rules->preset_vars, &var->list); |
| 7101 | |
| 7102 | return 1; |
| 7103 | |
| 7104 | error: |
| 7105 | free(str); |
| 7106 | free(var); |
| 7107 | return 0; |
| 7108 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7109 | |
| 7110 | /* Parse the "agent-send" server keyword */ |
| 7111 | static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7112 | char **errmsg) |
| 7113 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7114 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7115 | int err_code = 0; |
| 7116 | |
| 7117 | if (!*(args[*cur_arg+1])) { |
| 7118 | memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]); |
| 7119 | goto error; |
| 7120 | } |
| 7121 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7122 | if (!rules) { |
| 7123 | rules = calloc(1, sizeof(*rules)); |
| 7124 | if (!rules) { |
| 7125 | memprintf(errmsg, "out of memory."); |
| 7126 | goto error; |
| 7127 | } |
| 7128 | LIST_INIT(&rules->preset_vars); |
| 7129 | srv->agent.tcpcheck_rules = rules; |
| 7130 | } |
| 7131 | |
| 7132 | if (!set_srv_agent_send(srv, args[*cur_arg+1])) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7133 | memprintf(errmsg, "out of memory."); |
| 7134 | goto error; |
| 7135 | } |
| 7136 | |
| 7137 | out: |
| 7138 | return err_code; |
| 7139 | |
| 7140 | error: |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7141 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7142 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7143 | goto out; |
| 7144 | } |
| 7145 | |
| 7146 | /* Parse the "no-agent-send" server keyword */ |
| 7147 | static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7148 | char **errmsg) |
| 7149 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 7150 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7151 | return 0; |
| 7152 | } |
| 7153 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7154 | /* Parse the "check" server keyword */ |
| 7155 | static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7156 | char **errmsg) |
| 7157 | { |
| 7158 | srv->do_check = 1; |
| 7159 | return 0; |
| 7160 | } |
| 7161 | |
| 7162 | /* Parse the "check-send-proxy" server keyword */ |
| 7163 | static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7164 | char **errmsg) |
| 7165 | { |
| 7166 | srv->check.send_proxy = 1; |
| 7167 | return 0; |
| 7168 | } |
| 7169 | |
| 7170 | /* Parse the "check-via-socks4" server keyword */ |
| 7171 | static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7172 | char **errmsg) |
| 7173 | { |
| 7174 | srv->check.via_socks4 = 1; |
| 7175 | return 0; |
| 7176 | } |
| 7177 | |
| 7178 | /* Parse the "no-check" server keyword */ |
| 7179 | static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7180 | char **errmsg) |
| 7181 | { |
| 7182 | deinit_srv_check(srv); |
| 7183 | return 0; |
| 7184 | } |
| 7185 | |
| 7186 | /* Parse the "no-check-send-proxy" server keyword */ |
| 7187 | static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7188 | char **errmsg) |
| 7189 | { |
| 7190 | srv->check.send_proxy = 0; |
| 7191 | return 0; |
| 7192 | } |
| 7193 | |
| 7194 | /* Parse the "rise" server keyword */ |
| 7195 | static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7196 | char **errmsg) |
| 7197 | { |
| 7198 | int err_code = 0; |
| 7199 | |
| 7200 | if (!*args[*cur_arg + 1]) { |
| 7201 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 7202 | goto error; |
| 7203 | } |
| 7204 | |
| 7205 | srv->check.rise = atol(args[*cur_arg+1]); |
| 7206 | if (srv->check.rise <= 0) { |
| 7207 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 7208 | goto error; |
| 7209 | } |
| 7210 | |
| 7211 | if (srv->check.health) |
| 7212 | srv->check.health = srv->check.rise; |
| 7213 | |
| 7214 | out: |
| 7215 | return err_code; |
| 7216 | |
| 7217 | error: |
| 7218 | deinit_srv_agent_check(srv); |
| 7219 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7220 | goto out; |
| 7221 | return 0; |
| 7222 | } |
| 7223 | |
| 7224 | /* Parse the "fall" server keyword */ |
| 7225 | static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7226 | char **errmsg) |
| 7227 | { |
| 7228 | int err_code = 0; |
| 7229 | |
| 7230 | if (!*args[*cur_arg + 1]) { |
| 7231 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 7232 | goto error; |
| 7233 | } |
| 7234 | |
| 7235 | srv->check.fall = atol(args[*cur_arg+1]); |
| 7236 | if (srv->check.fall <= 0) { |
| 7237 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 7238 | goto error; |
| 7239 | } |
| 7240 | |
| 7241 | out: |
| 7242 | return err_code; |
| 7243 | |
| 7244 | error: |
| 7245 | deinit_srv_agent_check(srv); |
| 7246 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7247 | goto out; |
| 7248 | return 0; |
| 7249 | } |
| 7250 | |
| 7251 | /* Parse the "inter" server keyword */ |
| 7252 | static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7253 | char **errmsg) |
| 7254 | { |
| 7255 | const char *err = NULL; |
| 7256 | unsigned int delay; |
| 7257 | int err_code = 0; |
| 7258 | |
| 7259 | if (!*(args[*cur_arg+1])) { |
| 7260 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7261 | goto error; |
| 7262 | } |
| 7263 | |
| 7264 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7265 | if (err == PARSE_TIME_OVER) { |
| 7266 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7267 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7268 | goto error; |
| 7269 | } |
| 7270 | else if (err == PARSE_TIME_UNDER) { |
| 7271 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7272 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7273 | goto error; |
| 7274 | } |
| 7275 | else if (err) { |
| 7276 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7277 | *err, srv->id); |
| 7278 | goto error; |
| 7279 | } |
| 7280 | if (delay <= 0) { |
| 7281 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7282 | delay, args[*cur_arg], srv->id); |
| 7283 | goto error; |
| 7284 | } |
| 7285 | srv->check.inter = delay; |
| 7286 | |
| 7287 | out: |
| 7288 | return err_code; |
| 7289 | |
| 7290 | error: |
| 7291 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7292 | goto out; |
| 7293 | } |
| 7294 | |
| 7295 | |
| 7296 | /* Parse the "fastinter" server keyword */ |
| 7297 | static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7298 | char **errmsg) |
| 7299 | { |
| 7300 | const char *err = NULL; |
| 7301 | unsigned int delay; |
| 7302 | int err_code = 0; |
| 7303 | |
| 7304 | if (!*(args[*cur_arg+1])) { |
| 7305 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7306 | goto error; |
| 7307 | } |
| 7308 | |
| 7309 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7310 | if (err == PARSE_TIME_OVER) { |
| 7311 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7312 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7313 | goto error; |
| 7314 | } |
| 7315 | else if (err == PARSE_TIME_UNDER) { |
| 7316 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7317 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7318 | goto error; |
| 7319 | } |
| 7320 | else if (err) { |
| 7321 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7322 | *err, srv->id); |
| 7323 | goto error; |
| 7324 | } |
| 7325 | if (delay <= 0) { |
| 7326 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7327 | delay, args[*cur_arg], srv->id); |
| 7328 | goto error; |
| 7329 | } |
| 7330 | srv->check.fastinter = delay; |
| 7331 | |
| 7332 | out: |
| 7333 | return err_code; |
| 7334 | |
| 7335 | error: |
| 7336 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7337 | goto out; |
| 7338 | } |
| 7339 | |
| 7340 | |
| 7341 | /* Parse the "downinter" server keyword */ |
| 7342 | static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7343 | char **errmsg) |
| 7344 | { |
| 7345 | const char *err = NULL; |
| 7346 | unsigned int delay; |
| 7347 | int err_code = 0; |
| 7348 | |
| 7349 | if (!*(args[*cur_arg+1])) { |
| 7350 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 7351 | goto error; |
| 7352 | } |
| 7353 | |
| 7354 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 7355 | if (err == PARSE_TIME_OVER) { |
| 7356 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 7357 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7358 | goto error; |
| 7359 | } |
| 7360 | else if (err == PARSE_TIME_UNDER) { |
| 7361 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 7362 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 7363 | goto error; |
| 7364 | } |
| 7365 | else if (err) { |
| 7366 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 7367 | *err, srv->id); |
| 7368 | goto error; |
| 7369 | } |
| 7370 | if (delay <= 0) { |
| 7371 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 7372 | delay, args[*cur_arg], srv->id); |
| 7373 | goto error; |
| 7374 | } |
| 7375 | srv->check.downinter = delay; |
| 7376 | |
| 7377 | out: |
| 7378 | return err_code; |
| 7379 | |
| 7380 | error: |
| 7381 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7382 | goto out; |
| 7383 | } |
| 7384 | |
| 7385 | /* Parse the "port" server keyword */ |
| 7386 | static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 7387 | char **errmsg) |
| 7388 | { |
| 7389 | int err_code = 0; |
| 7390 | |
| 7391 | if (!*(args[*cur_arg+1])) { |
| 7392 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 7393 | goto error; |
| 7394 | } |
| 7395 | |
| 7396 | global.maxsock++; |
| 7397 | srv->check.port = atol(args[*cur_arg+1]); |
| 7398 | srv->flags |= SRV_F_CHECKPORT; |
| 7399 | |
| 7400 | out: |
| 7401 | return err_code; |
| 7402 | |
| 7403 | error: |
| 7404 | err_code |= ERR_ALERT | ERR_FATAL; |
| 7405 | goto out; |
| 7406 | } |
| 7407 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7408 | static struct cfg_kw_list cfg_kws = {ILH, { |
Christopher Faulet | e9111b6 | 2020-04-09 18:12:08 +0200 | [diff] [blame] | 7409 | { CFG_LISTEN, "tcp-check", proxy_parse_tcpcheck }, |
| 7410 | { CFG_LISTEN, "http-check", proxy_parse_httpcheck }, |
| 7411 | { CFG_LISTEN, "external-check", proxy_parse_extcheck }, |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7412 | { 0, NULL, NULL }, |
| 7413 | }}; |
| 7414 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7415 | static struct srv_kw_list srv_kws = { "CHK", { }, { |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 7416 | { "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] | 7417 | { "agent-addr", srv_parse_agent_addr, 1, 1 }, /* Enable an auxiliary agent check */ |
| 7418 | { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable agent checks */ |
| 7419 | { "agent-inter", srv_parse_agent_inter, 1, 1 }, /* Set the interval between two agent checks */ |
| 7420 | { "agent-port", srv_parse_agent_port, 1, 1 }, /* Set the TCP port used for agent checks. */ |
| 7421 | { "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] | 7422 | { "check", srv_parse_check, 0, 1 }, /* Enable health checks */ |
| 7423 | { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* Enable PROXY protocol for health checks */ |
| 7424 | { "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] | 7425 | { "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] | 7426 | { "no-check", srv_parse_no_check, 0, 1 }, /* Disable health checks */ |
| 7427 | { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* Disable PROXY protol for health checks */ |
| 7428 | { "rise", srv_parse_check_rise, 1, 1 }, /* Set rise value for health checks */ |
| 7429 | { "fall", srv_parse_check_fall, 1, 1 }, /* Set fall value for health checks */ |
| 7430 | { "inter", srv_parse_check_inter, 1, 1 }, /* Set inter value for health checks */ |
| 7431 | { "fastinter", srv_parse_check_fastinter, 1, 1 }, /* Set fastinter value for health checks */ |
| 7432 | { "downinter", srv_parse_check_downinter, 1, 1 }, /* Set downinter value for health checks */ |
| 7433 | { "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] | 7434 | { NULL, NULL, 0 }, |
| 7435 | }}; |
| 7436 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7437 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 7438 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 7439 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 7440 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7441 | * Local variables: |
| 7442 | * c-indent-level: 8 |
| 7443 | * c-basic-offset: 8 |
| 7444 | * End: |
| 7445 | */ |