Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Health-checks. |
| 3 | * |
| 4 | * Copyright 2008-2009 Krzysztof Piotr Oledzki <ole@ans.pl> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | 7eeb435 | 2013-06-14 15:28:25 +0200 | [diff] [blame] | 13 | #ifndef _TYPES_CHECKS_H |
| 14 | #define _TYPES_CHECKS_H |
| 15 | |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 16 | #include <sys/time.h> |
| 17 | |
| 18 | #include <common/config.h> |
| 19 | #include <common/mini-clist.h> |
| 20 | #include <common/regex.h> |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 21 | #include <common/buffer.h> |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 22 | |
| 23 | #include <types/connection.h> |
| 24 | #include <types/obj_type.h> |
| 25 | #include <types/task.h> |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 26 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 27 | /* enum used by check->result. Must remain in this order, as some code uses |
| 28 | * result >= CHK_RES_PASSED to declare success. |
| 29 | */ |
| 30 | enum chk_result { |
| 31 | CHK_RES_UNKNOWN = 0, /* initialized to this by default */ |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 32 | CHK_RES_NEUTRAL, /* valid check but no status information */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 33 | CHK_RES_FAILED, /* check failed */ |
| 34 | CHK_RES_PASSED, /* check succeeded and server is fully up again */ |
| 35 | CHK_RES_CONDPASS, /* check reports the server doesn't want new sessions */ |
| 36 | }; |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 37 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 38 | /* flags used by check->state */ |
| 39 | #define CHK_ST_INPROGRESS 0x0001 /* a check is currently running */ |
Willy Tarreau | 2e10f5a | 2013-12-11 20:11:55 +0100 | [diff] [blame] | 40 | #define CHK_ST_CONFIGURED 0x0002 /* this check is configured and may be enabled */ |
| 41 | #define CHK_ST_ENABLED 0x0004 /* this check is currently administratively enabled */ |
Willy Tarreau | 33a08db | 2013-12-11 21:03:31 +0100 | [diff] [blame] | 42 | #define CHK_ST_PAUSED 0x0008 /* checks are paused because of maintenance (health only) */ |
Willy Tarreau | 3343432 | 2013-12-11 21:15:19 +0100 | [diff] [blame] | 43 | #define CHK_ST_AGENT 0x0010 /* check is an agent check (otherwise it's a health check) */ |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 44 | #define CHK_ST_PORT_MISS 0x0020 /* check can't be send because no port is configured to run it */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 45 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 46 | /* check status */ |
| 47 | enum { |
| 48 | HCHK_STATUS_UNKNOWN = 0, /* Unknown */ |
| 49 | HCHK_STATUS_INI, /* Initializing */ |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 50 | HCHK_STATUS_START, /* Check started - SPECIAL STATUS */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 51 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 52 | /* Below we have finished checks */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 53 | HCHK_STATUS_CHECKED, /* DUMMY STATUS */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 54 | |
Simon Horman | b7cd8f9 | 2012-03-19 07:25:28 +0900 | [diff] [blame] | 55 | HCHK_STATUS_HANA, /* Health analyze detected enough consecutive errors */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 56 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 57 | HCHK_STATUS_SOCKERR, /* Socket error */ |
| 58 | |
| 59 | HCHK_STATUS_L4OK, /* L4 check passed, for example tcp connect */ |
| 60 | HCHK_STATUS_L4TOUT, /* L4 timeout */ |
| 61 | HCHK_STATUS_L4CON, /* L4 connection problem, for example: */ |
| 62 | /* "Connection refused" (tcp rst) or "No route to host" (icmp) */ |
| 63 | |
| 64 | HCHK_STATUS_L6OK, /* L6 check passed */ |
| 65 | HCHK_STATUS_L6TOUT, /* L6 (SSL) timeout */ |
| 66 | HCHK_STATUS_L6RSP, /* L6 invalid response - protocol error */ |
| 67 | |
| 68 | HCHK_STATUS_L7TOUT, /* L7 (HTTP/SMTP) timeout */ |
| 69 | HCHK_STATUS_L7RSP, /* L7 invalid response - protocol error */ |
| 70 | |
Simon Horman | b7cd8f9 | 2012-03-19 07:25:28 +0900 | [diff] [blame] | 71 | /* Below we have layer 5-7 data available */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 72 | HCHK_STATUS_L57DATA, /* DUMMY STATUS */ |
| 73 | HCHK_STATUS_L7OKD, /* L7 check passed */ |
| 74 | HCHK_STATUS_L7OKCD, /* L7 check conditionally passed */ |
| 75 | HCHK_STATUS_L7STS, /* L7 response error, for example HTTP 5xx */ |
| 76 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 77 | HCHK_STATUS_PROCERR, /* External process check failure */ |
| 78 | HCHK_STATUS_PROCTOUT, /* External process check timeout */ |
| 79 | HCHK_STATUS_PROCOK, /* External process check passed */ |
| 80 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 81 | HCHK_STATUS_SIZE |
| 82 | }; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 83 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 84 | /* environment variables memory requirement for different types of data */ |
Christopher Faulet | 8d18db7 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 85 | #define EXTCHK_SIZE_EVAL_INIT 0 /* size determined during the init phase, |
| 86 | * such environment variables are not updatable. */ |
| 87 | #define EXTCHK_SIZE_ULONG 20 /* max string length for an unsigned long value */ |
| 88 | #define EXTCHK_SIZE_UINT 11 /* max string length for an unsigned int value */ |
| 89 | #define EXTCHK_SIZE_ADDR INET6_ADDRSTRLEN+1 /* max string length for an address */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 90 | |
| 91 | /* external checks environment variables */ |
| 92 | enum { |
| 93 | EXTCHK_PATH = 0, |
| 94 | |
| 95 | /* Proxy specific environment variables */ |
| 96 | EXTCHK_HAPROXY_PROXY_NAME, /* the backend name */ |
| 97 | EXTCHK_HAPROXY_PROXY_ID, /* the backend id */ |
| 98 | EXTCHK_HAPROXY_PROXY_ADDR, /* the first bind address if available (or empty) */ |
| 99 | EXTCHK_HAPROXY_PROXY_PORT, /* the first bind port if available (or empty) */ |
| 100 | |
| 101 | /* Server specific environment variables */ |
| 102 | EXTCHK_HAPROXY_SERVER_NAME, /* the server name */ |
| 103 | EXTCHK_HAPROXY_SERVER_ID, /* the server id */ |
| 104 | EXTCHK_HAPROXY_SERVER_ADDR, /* the server address */ |
| 105 | EXTCHK_HAPROXY_SERVER_PORT, /* the server port if available (or empty) */ |
| 106 | EXTCHK_HAPROXY_SERVER_MAXCONN, /* the server max connections */ |
| 107 | EXTCHK_HAPROXY_SERVER_CURCONN, /* the current number of connections on the server */ |
| 108 | |
| 109 | EXTCHK_SIZE |
| 110 | }; |
| 111 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 112 | |
| 113 | /* health status for response tracking */ |
| 114 | enum { |
| 115 | HANA_STATUS_UNKNOWN = 0, |
| 116 | |
| 117 | HANA_STATUS_L4_OK, /* L4 successful connection */ |
| 118 | HANA_STATUS_L4_ERR, /* L4 unsuccessful connection */ |
| 119 | |
| 120 | HANA_STATUS_HTTP_OK, /* Correct http response */ |
| 121 | HANA_STATUS_HTTP_STS, /* Wrong http response, for example HTTP 5xx */ |
| 122 | HANA_STATUS_HTTP_HDRRSP, /* Invalid http response (headers) */ |
| 123 | HANA_STATUS_HTTP_RSP, /* Invalid http response */ |
| 124 | |
| 125 | HANA_STATUS_HTTP_READ_ERROR, /* Read error */ |
| 126 | HANA_STATUS_HTTP_READ_TIMEOUT, /* Read timeout */ |
| 127 | HANA_STATUS_HTTP_BROKEN_PIPE, /* Unexpected close from server */ |
| 128 | |
| 129 | HANA_STATUS_SIZE |
| 130 | }; |
| 131 | |
| 132 | enum { |
| 133 | HANA_ONERR_UNKNOWN = 0, |
| 134 | |
| 135 | HANA_ONERR_FASTINTER, /* Force fastinter*/ |
| 136 | HANA_ONERR_FAILCHK, /* Simulate a failed check */ |
| 137 | HANA_ONERR_SUDDTH, /* Enters sudden death - one more failed check will mark this server down */ |
| 138 | HANA_ONERR_MARKDWN, /* Mark this server down, now! */ |
| 139 | }; |
| 140 | |
| 141 | enum { |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 142 | HANA_ONMARKEDDOWN_NONE = 0, |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 143 | HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS, /* Shutdown peer sessions */ |
| 144 | }; |
| 145 | |
| 146 | enum { |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 147 | HANA_ONMARKEDUP_NONE = 0, |
| 148 | HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS, /* Shutdown peer sessions */ |
| 149 | }; |
| 150 | |
| 151 | enum { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 152 | HANA_OBS_NONE = 0, |
| 153 | |
| 154 | HANA_OBS_LAYER4, /* Observe L4 - for example tcp */ |
| 155 | HANA_OBS_LAYER7, /* Observe L7 - for example http */ |
| 156 | |
| 157 | HANA_OBS_SIZE |
| 158 | }; |
| 159 | |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 160 | struct check { |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 161 | struct xprt_ops *xprt; /* transport layer operations for health checks */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 162 | struct conn_stream *cs; /* conn_stream state for health checks */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 163 | struct buffer bi, bo; /* input and output buffers to send/recv check */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 164 | struct task *task; /* the task associated to the health check processing, NULL if disabled */ |
| 165 | struct timeval start; /* last health check start time */ |
| 166 | long duration; /* time in ms took to finish last health check */ |
| 167 | short status, code; /* check result, check code */ |
Willy Tarreau | 08eaa78 | 2017-11-26 08:44:34 +0100 | [diff] [blame] | 168 | unsigned short port; /* the port to use for the health checks */ |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 169 | char desc[HCHK_DESC_LEN]; /* health check description */ |
Christopher Faulet | 68d35ae | 2020-03-27 18:55:49 +0100 | [diff] [blame^] | 170 | char use_ssl; /* use SSL for health checks (1: on, 0: server mode, -1: off) */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 171 | int send_proxy; /* send a PROXY protocol header with checks */ |
Simon Horman | e16c1b3 | 2015-01-30 11:22:57 +0900 | [diff] [blame] | 172 | struct list *tcpcheck_rules; /* tcp-check send / expect rules */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 173 | struct tcpcheck_rule *current_step; /* current step when using tcpcheck */ |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 174 | struct tcpcheck_rule *last_started_step;/* pointer to latest tcpcheck rule started */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 175 | int inter, fastinter, downinter; /* checks: time in milliseconds */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 176 | enum chk_result result; /* health-check result : CHK_RES_* */ |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 177 | int state; /* state of the check : CHK_ST_* */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 178 | int health; /* 0 to rise-1 = bad; |
| 179 | * rise to rise+fall-1 = good */ |
| 180 | int rise, fall; /* time in iterations */ |
| 181 | int type; /* Check type, one of PR_O2_*_CHK */ |
James Brown | 55f9ff1 | 2015-10-21 18:19:05 -0700 | [diff] [blame] | 182 | int send_string_len; /* length of agent command string */ |
Willy Tarreau | 08eaa78 | 2017-11-26 08:44:34 +0100 | [diff] [blame] | 183 | char *send_string; /* optionally send a string when connecting to the agent */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 184 | struct server *server; /* back-pointer to server */ |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 185 | struct proxy *proxy; /* proxy to be used */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 186 | char **argv; /* the arguments to use if running a process-based check */ |
| 187 | char **envp; /* the environment to use if running a process-based check */ |
| 188 | struct pid_list *curpid; /* entry in pid_list used for current process-based test, or -1 if not in test */ |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 189 | struct sockaddr_storage addr; /* the address to check */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 190 | struct wait_event wait_list; /* Waiting for I/O events */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 191 | char *sni; /* Server name */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 192 | char *alpn_str; /* ALPN to use for checks */ |
| 193 | int alpn_len; /* ALPN string length */ |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 194 | |
| 195 | int via_socks4; /* check the connection via socks4 proxy */ |
Willy Tarreau | 8e85ad5 | 2013-12-11 16:45:07 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 198 | struct check_status { |
| 199 | short result; /* one of SRV_CHK_* */ |
| 200 | char *info; /* human readable short info */ |
| 201 | char *desc; /* long description */ |
| 202 | }; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 203 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 204 | struct extcheck_env { |
| 205 | char *name; /* environment variable name */ |
| 206 | int vmaxlen; /* value maximum length, used to determine the required memory allocation */ |
| 207 | }; |
| 208 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 209 | struct analyze_status { |
| 210 | char *desc; /* description */ |
| 211 | unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */ |
| 212 | }; |
Thierry FOURNIER | 7eeb435 | 2013-06-14 15:28:25 +0200 | [diff] [blame] | 213 | |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 214 | /* possible actions for tcpcheck_rule->action */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 215 | enum { |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 216 | TCPCHK_ACT_SEND = 0, /* send action, regular string format */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 217 | TCPCHK_ACT_EXPECT, /* expect action, either regular or binary string */ |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 218 | TCPCHK_ACT_CONNECT, /* connect action, to probe a new port */ |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 219 | TCPCHK_ACT_COMMENT, /* no action, simply a comment used for logs */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 220 | }; |
| 221 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 222 | /* flags used by tcpcheck_rule->conn_opts */ |
| 223 | #define TCPCHK_OPT_NONE 0x0000 /* no options specified, default */ |
| 224 | #define TCPCHK_OPT_SEND_PROXY 0x0001 /* send proxy-protocol string */ |
| 225 | #define TCPCHK_OPT_SSL 0x0002 /* SSL connection */ |
| 226 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 227 | struct tcpcheck_rule { |
| 228 | struct list list; /* list linked to from the proxy */ |
| 229 | int action; /* action: send or expect */ |
Baptiste Assmann | aa12b47 | 2015-04-25 16:16:48 +0200 | [diff] [blame] | 230 | char *comment; /* comment to be used in the logs and on the stats socket */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 231 | /* match type uses NON-NULL pointer from either string or expect_regex below */ |
| 232 | /* sent string is string */ |
| 233 | char *string; /* sent or expected string */ |
Joseph Herlant | 0b75e63 | 2018-11-25 13:28:30 -0800 | [diff] [blame] | 234 | int string_len; /* string length */ |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 235 | struct my_regex *expect_regex; /* expected */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 236 | int inverse; /* 0 = regular match, 1 = inverse match */ |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 237 | unsigned short port; /* port to connect to */ |
| 238 | unsigned short conn_opts; /* options when setting up a new connection */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 239 | }; |
| 240 | |
Thierry FOURNIER | 7eeb435 | 2013-06-14 15:28:25 +0200 | [diff] [blame] | 241 | #endif /* _TYPES_CHECKS_H */ |