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> |
| 18 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 21 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 24 | #include <sys/types.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
| 32 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 33 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
| 36 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 38 | #ifdef USE_OPENSSL |
| 39 | #include <types/ssl_sock.h> |
| 40 | #include <proto/ssl_sock.h> |
| 41 | #endif /* USE_OPENSSL */ |
| 42 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 44 | #include <proto/checks.h> |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 45 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | #include <proto/fd.h> |
| 47 | #include <proto/log.h> |
| 48 | #include <proto/queue.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 49 | #include <proto/port_range.h> |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 50 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 51 | #include <proto/proto_tcp.h> |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 52 | #include <proto/protocol.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 53 | #include <proto/proxy.h> |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 54 | #include <proto/raw_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/server.h> |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 56 | #include <proto/session.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 57 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/task.h> |
| 59 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 60 | static int httpchk_expect(struct server *s, int done); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 61 | static int tcpcheck_get_step_id(struct server *); |
| 62 | static void tcpcheck_main(struct connection *); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 63 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 64 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 65 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 66 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 67 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 69 | /* Below we have finished checks */ |
| 70 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 71 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 72 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 73 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 75 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 76 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 77 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 79 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 80 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 81 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 83 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 84 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 85 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 86 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 87 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 88 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 89 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 90 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 93 | 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] | 94 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 95 | |
| 96 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 97 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 98 | |
| 99 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 100 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 101 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 102 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 103 | |
| 104 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 105 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 106 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 107 | }; |
| 108 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Convert check_status code to description |
| 111 | */ |
| 112 | const char *get_check_status_description(short check_status) { |
| 113 | |
| 114 | const char *desc; |
| 115 | |
| 116 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 117 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 118 | else |
| 119 | desc = NULL; |
| 120 | |
| 121 | if (desc && *desc) |
| 122 | return desc; |
| 123 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 124 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Convert check_status code to short info |
| 129 | */ |
| 130 | const char *get_check_status_info(short check_status) { |
| 131 | |
| 132 | const char *info; |
| 133 | |
| 134 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 135 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 136 | else |
| 137 | info = NULL; |
| 138 | |
| 139 | if (info && *info) |
| 140 | return info; |
| 141 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 142 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 145 | const char *get_analyze_status(short analyze_status) { |
| 146 | |
| 147 | const char *desc; |
| 148 | |
| 149 | if (analyze_status < HANA_STATUS_SIZE) |
| 150 | desc = analyze_statuses[analyze_status].desc; |
| 151 | else |
| 152 | desc = NULL; |
| 153 | |
| 154 | if (desc && *desc) |
| 155 | return desc; |
| 156 | else |
| 157 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 158 | } |
| 159 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 160 | /* Builds a string containing some information about the health check's result. |
| 161 | * The output string is allocated from the trash chunks. If the check is NULL, |
| 162 | * NULL is returned. This is designed to be used when emitting logs about health |
| 163 | * checks. |
Willy Tarreau | ddd329c | 2014-05-16 16:46:12 +0200 | [diff] [blame] | 164 | */ |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 165 | static const char *check_reason_string(struct check *check) |
Willy Tarreau | ddd329c | 2014-05-16 16:46:12 +0200 | [diff] [blame] | 166 | { |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 167 | struct chunk *msg; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 168 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 169 | if (!check) |
| 170 | return NULL; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 171 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 172 | msg = get_trash_chunk(); |
| 173 | chunk_printf(msg, "reason: %s", get_check_status_description(check->status)); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 174 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 175 | if (check->status >= HCHK_STATUS_L57DATA) |
| 176 | chunk_appendf(msg, ", code: %d", check->code); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 177 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 178 | if (*check->desc) { |
| 179 | struct chunk src; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 181 | chunk_appendf(msg, ", info: \""); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 182 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 183 | chunk_initlen(&src, check->desc, 0, strlen(check->desc)); |
| 184 | chunk_asciiencode(msg, &src, '"'); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 185 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 186 | chunk_appendf(msg, "\""); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 189 | if (check->duration >= 0) |
| 190 | chunk_appendf(msg, ", check duration: %ldms", check->duration); |
| 191 | |
| 192 | return msg->str; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 195 | /* |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 196 | * Set check->status, update check->duration and fill check->result with |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 197 | * an adequate CHK_RES_* value. The new check->health is computed based |
| 198 | * on the result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 199 | * |
| 200 | * Show information in logs about failed health check if server is UP |
| 201 | * or succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 202 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 203 | 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] | 204 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 205 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 206 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 207 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 208 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 209 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 210 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 211 | check->desc[0] = '\0'; |
| 212 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 216 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 217 | return; |
| 218 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 219 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 220 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 221 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 222 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 223 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 224 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 225 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 226 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 227 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 228 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 229 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 230 | check->duration = -1; |
| 231 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 232 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 233 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 234 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 237 | /* no change is expected if no state change occurred */ |
| 238 | if (check->result == CHK_RES_NEUTRAL) |
| 239 | return; |
| 240 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 241 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 242 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 243 | switch (check->result) { |
| 244 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 245 | /* Failure to connect to the agent as a secondary check should not |
| 246 | * cause the server to be marked down. |
| 247 | */ |
| 248 | if ((!(check->state & CHK_ST_AGENT) || |
Simon Horman | bfb8f88 | 2015-02-26 11:26:17 +0900 | [diff] [blame] | 249 | (check->status >= HCHK_STATUS_L57DATA)) && |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 250 | (check->health >= check->rise)) { |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 251 | s->counters.failed_checks++; |
| 252 | report = 1; |
| 253 | check->health--; |
| 254 | if (check->health < check->rise) |
| 255 | check->health = 0; |
| 256 | } |
| 257 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 258 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 259 | case CHK_RES_PASSED: |
| 260 | case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */ |
| 261 | if ((check->health < check->rise + check->fall - 1) && |
| 262 | (check->result == CHK_RES_PASSED || check->health > 0)) { |
| 263 | report = 1; |
| 264 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 265 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 266 | if (check->health >= check->rise) |
| 267 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 268 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 269 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 270 | /* clear consecutive_errors if observing is enabled */ |
| 271 | if (s->onerror) |
| 272 | s->consecutive_errors = 0; |
| 273 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 274 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 275 | default: |
| 276 | break; |
| 277 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 278 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 279 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 280 | (status != prev_status || report)) { |
| 281 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 282 | "%s check for %sserver %s/%s %s%s", |
| 283 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 284 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 285 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 286 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 287 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 288 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 289 | srv_append_status(&trash, s, check_reason_string(check), -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 291 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 292 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 293 | (check->health >= check->rise) ? check->fall : check->rise, |
| 294 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 295 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 296 | Warning("%s.\n", trash.str); |
| 297 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 298 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 301 | /* Marks the check <check>'s server down if the current check is already failed |
| 302 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 303 | */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 304 | static void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 305 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 306 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 307 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 308 | /* The agent secondary check should only cause a server to be marked |
| 309 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 310 | * that the agent returned "fail", "stopped" or "down". |
| 311 | * The implication here is that failure to connect to the agent |
| 312 | * as a secondary check should not cause the server to be marked |
| 313 | * down. */ |
| 314 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 315 | return; |
| 316 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 317 | if (check->health > 0) |
| 318 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 319 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 320 | /* We only report a reason for the check if we did not do so previously */ |
| 321 | srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 324 | /* 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] | 325 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 326 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 327 | * following conditions is true : |
| 328 | * - health check failed (check->health < rise) |
| 329 | * - agent check failed (agent->health < rise) |
| 330 | * - the server tracks a down server (track && track->state == STOPPED) |
| 331 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 332 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 333 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 334 | */ |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 335 | static void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 336 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 337 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 338 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 339 | if (s->admin & SRV_ADMF_MAINT) |
| 340 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 341 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 342 | if (s->track && s->track->state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 343 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 344 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 345 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 346 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 347 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 348 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 349 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 350 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 351 | if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING) |
| 352 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 353 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 354 | srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 357 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 358 | * if it was running or starting, and provided it isn't in maintenance and other |
| 359 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 360 | * the same as for it to be turned up. Also, only the health checks support the |
| 361 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 362 | */ |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 363 | static void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 364 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 365 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 366 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 367 | if (s->admin & SRV_ADMF_MAINT) |
| 368 | return; |
| 369 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 370 | if (check->state & CHK_ST_AGENT) |
| 371 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 372 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 373 | if (s->track && s->track->state == SRV_ST_STOPPED) |
| 374 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 375 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 376 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 377 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 378 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 379 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 380 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 381 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 382 | srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 383 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 384 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 385 | /* note: use health_adjust() only, which first checks that the observe mode is |
| 386 | * enabled. |
| 387 | */ |
| 388 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 389 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 390 | int failed; |
| 391 | int expire; |
| 392 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 393 | if (s->observe >= HANA_OBS_SIZE) |
| 394 | return; |
| 395 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 396 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 397 | return; |
| 398 | |
| 399 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 400 | case 1: |
| 401 | failed = 1; |
| 402 | break; |
| 403 | |
| 404 | case 2: |
| 405 | failed = 0; |
| 406 | break; |
| 407 | |
| 408 | default: |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | if (!failed) { |
| 413 | /* good: clear consecutive_errors */ |
| 414 | s->consecutive_errors = 0; |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | s->consecutive_errors++; |
| 419 | |
| 420 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 421 | return; |
| 422 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 423 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 424 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 425 | |
| 426 | switch (s->onerror) { |
| 427 | case HANA_ONERR_FASTINTER: |
| 428 | /* force fastinter - nothing to do here as all modes force it */ |
| 429 | break; |
| 430 | |
| 431 | case HANA_ONERR_SUDDTH: |
| 432 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 433 | if (s->check.health > s->check.rise) |
| 434 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 435 | |
| 436 | /* no break - fall through */ |
| 437 | |
| 438 | case HANA_ONERR_FAILCHK: |
| 439 | /* simulate a failed health check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 440 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 441 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 442 | break; |
| 443 | |
| 444 | case HANA_ONERR_MARKDWN: |
| 445 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 446 | s->check.health = s->check.rise; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 447 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 448 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | default: |
| 452 | /* write a warning? */ |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | s->consecutive_errors = 0; |
| 457 | s->counters.failed_hana++; |
| 458 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 459 | if (s->check.fastinter) { |
| 460 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 461 | if (s->check.task->expire > expire) { |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 462 | s->check.task->expire = expire; |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 463 | /* requeue check task with new expire */ |
| 464 | task_queue(s->check.task); |
| 465 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 469 | 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] | 470 | { |
| 471 | int sv_state; |
| 472 | int ratio; |
| 473 | int hlen = 0; |
| 474 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 475 | "UP %d/%d", "UP", |
| 476 | "NOLB %d/%d", "NOLB", |
| 477 | "no check" }; |
| 478 | |
| 479 | memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24); |
| 480 | hlen += 24; |
| 481 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 482 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 483 | sv_state = 6; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 484 | else if (s->state != SRV_ST_STOPPED) { |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 485 | if (s->check.health == s->check.rise + s->check.fall - 1) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 486 | sv_state = 3; /* UP */ |
| 487 | else |
| 488 | sv_state = 2; /* going down */ |
| 489 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 490 | if (s->state == SRV_ST_STOPPING) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 491 | sv_state += 2; |
| 492 | } else { |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 493 | if (s->check.health) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 494 | sv_state = 1; /* going up */ |
| 495 | else |
| 496 | sv_state = 0; /* DOWN */ |
| 497 | } |
| 498 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 499 | hlen += snprintf(buffer + hlen, size - hlen, |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 500 | srv_hlt_st[sv_state], |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 501 | (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 502 | (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 503 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 504 | hlen += snprintf(buffer + hlen, size - hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 505 | s->proxy->id, s->id, |
| 506 | global.node, |
| 507 | (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 508 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 509 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 510 | s->nbpend); |
| 511 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 512 | if ((s->state == SRV_ST_STARTING) && |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 513 | now.tv_sec < s->last_change + s->slowstart && |
| 514 | now.tv_sec >= s->last_change) { |
| 515 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 516 | hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | buffer[hlen++] = '\r'; |
| 520 | buffer[hlen++] = '\n'; |
| 521 | |
| 522 | return hlen; |
| 523 | } |
| 524 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 525 | /* Check the connection. If an error has already been reported or the socket is |
| 526 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 527 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 528 | * Warning, this must be done only once when returning from poll, and never |
| 529 | * after an I/O error was attempted, otherwise the error queue might contain |
| 530 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 531 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 532 | * clean (including a properly closed socket). |
| 533 | */ |
| 534 | static int retrieve_errno_from_socket(struct connection *conn) |
| 535 | { |
| 536 | int skerr; |
| 537 | socklen_t lskerr = sizeof(skerr); |
| 538 | |
| 539 | if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl)) |
| 540 | return 1; |
| 541 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 542 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 543 | return 0; |
| 544 | |
| 545 | if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
| 546 | errno = skerr; |
| 547 | |
| 548 | if (errno == EAGAIN) |
| 549 | errno = 0; |
| 550 | |
| 551 | if (!errno) { |
| 552 | /* we could not retrieve an error, that does not mean there is |
| 553 | * none. Just don't change anything and only report the prior |
| 554 | * error if any. |
| 555 | */ |
| 556 | if (conn->flags & CO_FL_ERROR) |
| 557 | return 1; |
| 558 | else |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 563 | return 1; |
| 564 | } |
| 565 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 566 | /* Try to collect as much information as possible on the connection status, |
| 567 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 568 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 569 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 570 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 571 | * to extract errno from the socket. If no error is reported, it will consider |
| 572 | * the <expired> flag. This is intended to be used when a connection error was |
| 573 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 574 | * function takes care of not updating a server status which was already set. |
| 575 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 576 | * produce a status. |
| 577 | */ |
| 578 | static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired) |
| 579 | { |
| 580 | struct check *check = conn->owner; |
| 581 | const char *err_msg; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 582 | struct chunk *chk; |
Willy Tarreau | 90055f2 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 583 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 584 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 585 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 586 | return; |
| 587 | |
| 588 | errno = errno_bck; |
| 589 | if (!errno || errno == EAGAIN) |
| 590 | retrieve_errno_from_socket(conn); |
| 591 | |
| 592 | if (!(conn->flags & CO_FL_ERROR) && !expired) |
| 593 | return; |
| 594 | |
| 595 | /* we'll try to build a meaningful error message depending on the |
| 596 | * context of the error possibly present in conn->err_code, and the |
| 597 | * socket error possibly collected above. This is useful to know the |
| 598 | * exact step of the L6 layer (eg: SSL handshake). |
| 599 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 600 | chk = get_trash_chunk(); |
| 601 | |
| 602 | if (check->type == PR_O2_TCPCHK_CHK) { |
Willy Tarreau | 90055f2 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 603 | step = tcpcheck_get_step_id(check->server); |
| 604 | if (!step) |
| 605 | chunk_printf(chk, " at initial connection step of tcp-check"); |
| 606 | else { |
| 607 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 608 | /* we were looking for a string */ |
| 609 | if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) { |
| 610 | if (check->last_started_step->port) |
| 611 | chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port); |
| 612 | else |
| 613 | chunk_appendf(chk, " (connect)"); |
| 614 | } |
| 615 | else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) { |
| 616 | if (check->last_started_step->string) |
| 617 | chunk_appendf(chk, " (string '%s')", check->last_started_step->string); |
| 618 | else if (check->last_started_step->expect_regex) |
| 619 | chunk_appendf(chk, " (expect regex)"); |
| 620 | } |
| 621 | else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) { |
| 622 | chunk_appendf(chk, " (send)"); |
| 623 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 627 | if (conn->err_code) { |
| 628 | if (errno && errno != EAGAIN) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 629 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 630 | else |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 631 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 632 | err_msg = trash.str; |
| 633 | } |
| 634 | else { |
| 635 | if (errno && errno != EAGAIN) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 636 | chunk_printf(&trash, "%s%s", strerror(errno), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 637 | err_msg = trash.str; |
| 638 | } |
| 639 | else { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 640 | err_msg = chk->str; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) { |
| 645 | /* L4 not established (yet) */ |
| 646 | if (conn->flags & CO_FL_ERROR) |
| 647 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 648 | else if (expired) |
| 649 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
| 650 | } |
| 651 | else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) { |
| 652 | /* L6 not established (yet) */ |
| 653 | if (conn->flags & CO_FL_ERROR) |
| 654 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 655 | else if (expired) |
| 656 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 657 | } |
| 658 | else if (conn->flags & CO_FL_ERROR) { |
| 659 | /* I/O error after connection was established and before we could diagnose */ |
| 660 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 661 | } |
| 662 | else if (expired) { |
| 663 | /* connection established but expired check */ |
| 664 | if (check->type == PR_O2_SSL3_CHK) |
| 665 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 666 | else /* HTTP, SMTP, ... */ |
| 667 | set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg); |
| 668 | } |
| 669 | |
| 670 | return; |
| 671 | } |
| 672 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 673 | /* |
| 674 | * This function is used only for server health-checks. It handles |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 675 | * the connection acknowledgement. If the proxy requires L7 health-checks, |
| 676 | * it sends the request. In other cases, it calls set_server_check_status() |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 677 | * to set check->status, check->duration and check->result. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 678 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 679 | static void event_srv_chk_w(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 680 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 681 | struct check *check = conn->owner; |
| 682 | struct server *s = check->server; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 683 | struct task *t = check->task; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 684 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 685 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 686 | goto out_wakeup; |
| 687 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 688 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 689 | return; |
| 690 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 691 | if (retrieve_errno_from_socket(conn)) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 692 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 693 | __conn_data_stop_both(conn); |
| 694 | goto out_wakeup; |
| 695 | } |
Krzysztof Piotr Oledzki | 6492db5 | 2010-01-02 22:03:01 +0100 | [diff] [blame] | 696 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 697 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 698 | /* if the output is closed, we can't do anything */ |
| 699 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 700 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 701 | goto out_wakeup; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 702 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 703 | |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 704 | /* here, we know that the connection is established. That's enough for |
| 705 | * a pure TCP check. |
| 706 | */ |
| 707 | if (!check->type) |
| 708 | goto out_wakeup; |
| 709 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 710 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 711 | tcpcheck_main(conn); |
| 712 | return; |
| 713 | } |
| 714 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 715 | if (check->bo->o) { |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 716 | conn->xprt->snd_buf(conn, check->bo, 0); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 717 | if (conn->flags & CO_FL_ERROR) { |
| 718 | chk_report_conn_err(conn, errno, 0); |
| 719 | __conn_data_stop_both(conn); |
| 720 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 721 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 722 | if (check->bo->o) |
| 723 | return; |
| 724 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 725 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 726 | /* full request sent, we allow up to <timeout.check> if nonzero for a response */ |
| 727 | if (s->proxy->timeout.check) { |
| 728 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 729 | task_queue(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 730 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 731 | goto out_nowake; |
| 732 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 733 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 734 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 735 | out_nowake: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 736 | __conn_data_stop_send(conn); /* nothing more to write */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 739 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 740 | * 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] | 741 | * 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] | 742 | * set_server_check_status() to update check->status, check->duration |
| 743 | * and check->result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 744 | |
| 745 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 746 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 747 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 748 | * response to an SSL HELLO (the principle is that this is enough to |
| 749 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 750 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 751 | * etc. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 752 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 753 | static void event_srv_chk_r(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 754 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 755 | struct check *check = conn->owner; |
| 756 | struct server *s = check->server; |
| 757 | struct task *t = check->task; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 758 | char *desc; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 759 | int done; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 760 | unsigned short msglen; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 761 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 762 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 763 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 764 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 765 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 766 | return; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 767 | |
| 768 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 769 | tcpcheck_main(conn); |
| 770 | return; |
| 771 | } |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 772 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 773 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 774 | * but the connection was closed on the remote end. Fortunately, recv still |
| 775 | * works correctly and we don't need to do the getsockopt() on linux. |
| 776 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 777 | |
| 778 | /* Set buffer to point to the end of the data already read, and check |
| 779 | * that there is free space remaining. If the buffer is full, proceed |
| 780 | * with running the checks without attempting another socket read. |
| 781 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 782 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 783 | done = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 784 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 785 | conn->xprt->rcv_buf(conn, check->bi, check->bi->size); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 786 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 787 | done = 1; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 788 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 789 | /* Report network errors only if we got no other data. Otherwise |
| 790 | * we'll let the upper layers decide whether the response is OK |
| 791 | * or not. It is very common that an RST sent by the server is |
| 792 | * reported as an error just after the last data chunk. |
| 793 | */ |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 794 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 795 | goto out_wakeup; |
| 796 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 799 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 800 | /* Intermediate or complete response received. |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 801 | * Terminate string in check->bi->data buffer. |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 802 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 803 | if (check->bi->i < check->bi->size) |
| 804 | check->bi->data[check->bi->i] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 805 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 806 | check->bi->data[check->bi->i - 1] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 807 | done = 1; /* buffer full, don't wait for more data */ |
| 808 | } |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 809 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 810 | /* Run the checks... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 811 | switch (check->type) { |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 812 | case PR_O2_HTTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 813 | if (!done && check->bi->i < strlen("HTTP/1.0 000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 814 | goto wait_more_data; |
| 815 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 816 | /* Check if the server speaks HTTP 1.X */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 817 | if ((check->bi->i < strlen("HTTP/1.0 000\r")) || |
| 818 | (memcmp(check->bi->data, "HTTP/1.", 7) != 0 || |
| 819 | (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) || |
| 820 | !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) || |
| 821 | !isdigit((unsigned char) *(check->bi->data + 11))) { |
| 822 | cut_crlf(check->bi->data); |
| 823 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 824 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 825 | goto out_wakeup; |
| 826 | } |
| 827 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 828 | check->code = str2uic(check->bi->data + 9); |
| 829 | desc = ltrim(check->bi->data + 12, ' '); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 830 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 831 | if ((s->proxy->options & PR_O_DISABLE404) && |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 832 | (s->state != SRV_ST_STOPPED) && (check->code == 404)) { |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 833 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 834 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 835 | set_server_check_status(check, HCHK_STATUS_L7OKCD, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 836 | } |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 837 | else if (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 838 | /* Run content verification check... We know we have at least 13 chars */ |
| 839 | if (!httpchk_expect(s, done)) |
| 840 | goto wait_more_data; |
| 841 | } |
| 842 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 843 | else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 844 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 845 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 846 | } |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 847 | else { |
| 848 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 849 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 850 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 851 | break; |
| 852 | |
| 853 | case PR_O2_SSL3_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 854 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 855 | goto wait_more_data; |
| 856 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 857 | /* Check for SSLv3 alert or handshake */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 858 | if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16)) |
| 859 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 860 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 861 | set_server_check_status(check, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 862 | break; |
| 863 | |
| 864 | case PR_O2_SMTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 865 | if (!done && check->bi->i < strlen("000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 866 | goto wait_more_data; |
| 867 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 868 | /* Check if the server speaks SMTP */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 869 | if ((check->bi->i < strlen("000\r")) || |
| 870 | (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') || |
| 871 | !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) || |
| 872 | !isdigit((unsigned char) *(check->bi->data + 2))) { |
| 873 | cut_crlf(check->bi->data); |
| 874 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 875 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 876 | goto out_wakeup; |
| 877 | } |
| 878 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 879 | check->code = str2uic(check->bi->data); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 880 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 881 | desc = ltrim(check->bi->data + 3, ' '); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 882 | cut_crlf(desc); |
| 883 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 884 | /* Check for SMTP code 2xx (should be 250) */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 885 | if (*check->bi->data == '2') |
| 886 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 887 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 888 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 889 | break; |
| 890 | |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 891 | case PR_O2_LB_AGENT_CHK: { |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 892 | int status = HCHK_STATUS_CHECKED; |
| 893 | const char *hs = NULL; /* health status */ |
| 894 | const char *as = NULL; /* admin status */ |
| 895 | const char *ps = NULL; /* performance status */ |
| 896 | const char *err = NULL; /* first error to report */ |
| 897 | const char *wrn = NULL; /* first warning to report */ |
| 898 | char *cmd, *p; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 899 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 900 | /* We're getting an agent check response. The agent could |
| 901 | * have been disabled in the mean time with a long check |
| 902 | * still pending. It is important that we ignore the whole |
| 903 | * response. |
| 904 | */ |
| 905 | if (!(check->server->agent.state & CHK_ST_ENABLED)) |
| 906 | break; |
| 907 | |
| 908 | /* The agent supports strings made of a single line ended by the |
| 909 | * first CR ('\r') or LF ('\n'). This line is composed of words |
| 910 | * delimited by spaces (' '), tabs ('\t'), or commas (','). The |
| 911 | * line may optionally contained a description of a state change |
| 912 | * after a sharp ('#'), which is only considered if a health state |
| 913 | * is announced. |
| 914 | * |
| 915 | * Words may be composed of : |
| 916 | * - a numeric weight suffixed by the percent character ('%'). |
| 917 | * - a health status among "up", "down", "stopped", and "fail". |
| 918 | * - an admin status among "ready", "drain", "maint". |
| 919 | * |
| 920 | * These words may appear in any order. If multiple words of the |
| 921 | * same category appear, the last one wins. |
| 922 | */ |
| 923 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 924 | p = check->bi->data; |
| 925 | while (*p && *p != '\n' && *p != '\r') |
| 926 | p++; |
| 927 | |
| 928 | if (!*p) { |
| 929 | if (!done) |
| 930 | goto wait_more_data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 931 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 932 | /* at least inform the admin that the agent is mis-behaving */ |
| 933 | set_server_check_status(check, check->status, "Ignoring incomplete line from agent"); |
| 934 | break; |
| 935 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 936 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 937 | *p = 0; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 938 | cmd = check->bi->data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 939 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 940 | while (*cmd) { |
| 941 | /* look for next word */ |
| 942 | if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') { |
| 943 | cmd++; |
| 944 | continue; |
| 945 | } |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 946 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 947 | if (*cmd == '#') { |
| 948 | /* this is the beginning of a health status description, |
| 949 | * skip the sharp and blanks. |
| 950 | */ |
| 951 | cmd++; |
| 952 | while (*cmd == '\t' || *cmd == ' ') |
| 953 | cmd++; |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 954 | break; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 955 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 956 | |
| 957 | /* find the end of the word so that we have a null-terminated |
| 958 | * word between <cmd> and <p>. |
| 959 | */ |
| 960 | p = cmd + 1; |
| 961 | while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',') |
| 962 | p++; |
| 963 | if (*p) |
| 964 | *p++ = 0; |
| 965 | |
| 966 | /* first, health statuses */ |
| 967 | if (strcasecmp(cmd, "up") == 0) { |
| 968 | check->health = check->rise + check->fall - 1; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 969 | status = HCHK_STATUS_L7OKD; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 970 | hs = cmd; |
| 971 | } |
| 972 | else if (strcasecmp(cmd, "down") == 0) { |
| 973 | check->health = 0; |
| 974 | status = HCHK_STATUS_L7STS; |
| 975 | hs = cmd; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 976 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 977 | else if (strcasecmp(cmd, "stopped") == 0) { |
| 978 | check->health = 0; |
| 979 | status = HCHK_STATUS_L7STS; |
| 980 | hs = cmd; |
| 981 | } |
| 982 | else if (strcasecmp(cmd, "fail") == 0) { |
| 983 | check->health = 0; |
| 984 | status = HCHK_STATUS_L7STS; |
| 985 | hs = cmd; |
| 986 | } |
| 987 | /* admin statuses */ |
| 988 | else if (strcasecmp(cmd, "ready") == 0) { |
| 989 | as = cmd; |
| 990 | } |
| 991 | else if (strcasecmp(cmd, "drain") == 0) { |
| 992 | as = cmd; |
| 993 | } |
| 994 | else if (strcasecmp(cmd, "maint") == 0) { |
| 995 | as = cmd; |
| 996 | } |
| 997 | /* else try to parse a weight here and keep the last one */ |
| 998 | else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) { |
| 999 | ps = cmd; |
| 1000 | } |
| 1001 | else { |
| 1002 | /* keep a copy of the first error */ |
| 1003 | if (!err) |
| 1004 | err = cmd; |
| 1005 | } |
| 1006 | /* skip to next word */ |
| 1007 | cmd = p; |
| 1008 | } |
| 1009 | /* here, cmd points either to \0 or to the beginning of a |
| 1010 | * description. Skip possible leading spaces. |
| 1011 | */ |
| 1012 | while (*cmd == ' ' || *cmd == '\n') |
| 1013 | cmd++; |
| 1014 | |
| 1015 | /* First, update the admin status so that we avoid sending other |
| 1016 | * possibly useless warnings and can also update the health if |
| 1017 | * present after going back up. |
| 1018 | */ |
| 1019 | if (as) { |
| 1020 | if (strcasecmp(as, "drain") == 0) |
| 1021 | srv_adm_set_drain(check->server); |
| 1022 | else if (strcasecmp(as, "maint") == 0) |
| 1023 | srv_adm_set_maint(check->server); |
| 1024 | else |
| 1025 | srv_adm_set_ready(check->server); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1026 | } |
| 1027 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1028 | /* now change weights */ |
| 1029 | if (ps) { |
| 1030 | const char *msg; |
| 1031 | |
| 1032 | msg = server_parse_weight_change_request(s, ps); |
| 1033 | if (!wrn || !*wrn) |
| 1034 | wrn = msg; |
| 1035 | } |
| 1036 | |
| 1037 | /* and finally health status */ |
| 1038 | if (hs) { |
| 1039 | /* We'll report some of the warnings and errors we have |
| 1040 | * here. Down reports are critical, we leave them untouched. |
| 1041 | * Lack of report, or report of 'UP' leaves the room for |
| 1042 | * ERR first, then WARN. |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1043 | */ |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1044 | const char *msg = cmd; |
| 1045 | struct chunk *t; |
| 1046 | |
| 1047 | if (!*msg || status == HCHK_STATUS_L7OKD) { |
| 1048 | if (err && *err) |
| 1049 | msg = err; |
| 1050 | else if (wrn && *wrn) |
| 1051 | msg = wrn; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1052 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1053 | |
| 1054 | t = get_trash_chunk(); |
| 1055 | chunk_printf(t, "via agent : %s%s%s%s", |
| 1056 | hs, *msg ? " (" : "", |
| 1057 | msg, *msg ? ")" : ""); |
| 1058 | |
| 1059 | set_server_check_status(check, status, t->str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1060 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1061 | else if (err && *err) { |
| 1062 | /* No status change but we'd like to report something odd. |
| 1063 | * Just report the current state and copy the message. |
| 1064 | */ |
| 1065 | chunk_printf(&trash, "agent reports an error : %s", err); |
| 1066 | set_server_check_status(check, status/*check->status*/, trash.str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1067 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1068 | } |
| 1069 | else if (wrn && *wrn) { |
| 1070 | /* No status change but we'd like to report something odd. |
| 1071 | * Just report the current state and copy the message. |
| 1072 | */ |
| 1073 | chunk_printf(&trash, "agent warns : %s", wrn); |
| 1074 | set_server_check_status(check, status/*check->status*/, trash.str); |
| 1075 | } |
| 1076 | else |
| 1077 | set_server_check_status(check, status, NULL); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1078 | break; |
| 1079 | } |
| 1080 | |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1081 | case PR_O2_PGSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1082 | if (!done && check->bi->i < 9) |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1083 | goto wait_more_data; |
| 1084 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1085 | if (check->bi->data[0] == 'R') { |
| 1086 | set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok"); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1087 | } |
| 1088 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1089 | if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0)) |
| 1090 | desc = &check->bi->data[6]; |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1091 | else |
| 1092 | desc = "PostgreSQL unknown error"; |
| 1093 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1094 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1095 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1096 | break; |
| 1097 | |
| 1098 | case PR_O2_REDIS_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1099 | if (!done && check->bi->i < 7) |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1100 | goto wait_more_data; |
| 1101 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1102 | if (strcmp(check->bi->data, "+PONG\r\n") == 0) { |
| 1103 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok"); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1104 | } |
| 1105 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1106 | set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1107 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1108 | break; |
| 1109 | |
| 1110 | case PR_O2_MYSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1111 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1112 | goto wait_more_data; |
| 1113 | |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1114 | if (s->proxy->check_len == 0) { // old mode |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1115 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1116 | /* We set the MySQL Version in description for information purpose |
| 1117 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1118 | * like mark as down old MySQL server. |
| 1119 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1120 | if (check->bi->i > 51) { |
| 1121 | desc = ltrim(check->bi->data + 5, ' '); |
| 1122 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1123 | } |
| 1124 | else { |
| 1125 | if (!done) |
| 1126 | goto wait_more_data; |
| 1127 | /* it seems we have a OK packet but without a valid length, |
| 1128 | * it must be a protocol error |
| 1129 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1130 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1131 | } |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1132 | } |
| 1133 | else { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1134 | /* An error message is attached in the Error packet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1135 | desc = ltrim(check->bi->data + 7, ' '); |
| 1136 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1137 | } |
| 1138 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1139 | unsigned int first_packet_len = ((unsigned int) *check->bi->data) + |
| 1140 | (((unsigned int) *(check->bi->data + 1)) << 8) + |
| 1141 | (((unsigned int) *(check->bi->data + 2)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1142 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1143 | if (check->bi->i == first_packet_len + 4) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1144 | /* MySQL Error packet always begin with field_count = 0xff */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1145 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1146 | /* We have only one MySQL packet and it is a Handshake Initialization packet |
| 1147 | * but we need to have a second packet to know if it is alright |
| 1148 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1149 | if (!done && check->bi->i < first_packet_len + 5) |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1150 | goto wait_more_data; |
| 1151 | } |
| 1152 | else { |
| 1153 | /* We have only one packet and it is an Error packet, |
| 1154 | * an error message is attached, so we can display it |
| 1155 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1156 | desc = &check->bi->data[7]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1157 | //Warning("onlyoneERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1158 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1159 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1160 | } else if (check->bi->i > first_packet_len + 4) { |
| 1161 | unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) + |
| 1162 | (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) + |
| 1163 | (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1164 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1165 | if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1166 | /* We have 2 packets and that's good */ |
| 1167 | /* Check if the second packet is a MySQL Error packet or not */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1168 | if (*(check->bi->data + first_packet_len + 8) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1169 | /* No error packet */ |
| 1170 | /* We set the MySQL Version in description for information purpose */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1171 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1172 | //Warning("2packetOK: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1173 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1174 | } |
| 1175 | else { |
| 1176 | /* An error message is attached in the Error packet |
| 1177 | * so we can display it ! :) |
| 1178 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1179 | desc = &check->bi->data[first_packet_len+11]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1180 | //Warning("2packetERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1181 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | else { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1186 | if (!done) |
| 1187 | goto wait_more_data; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1188 | /* it seems we have a Handshake Initialization packet but without a valid length, |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1189 | * it must be a protocol error |
| 1190 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1191 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1192 | //Warning("protoerr: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1193 | set_server_check_status(check, HCHK_STATUS_L7RSP, desc); |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1194 | } |
| 1195 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1196 | break; |
| 1197 | |
| 1198 | case PR_O2_LDAP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1199 | if (!done && check->bi->i < 14) |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1200 | goto wait_more_data; |
| 1201 | |
| 1202 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1203 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1204 | * http://tools.ietf.org/html/rfc4511 |
| 1205 | */ |
| 1206 | |
| 1207 | /* http://tools.ietf.org/html/rfc4511#section-4.1.1 |
| 1208 | * LDAPMessage: 0x30: SEQUENCE |
| 1209 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1210 | if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) { |
| 1211 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1212 | } |
| 1213 | else { |
| 1214 | /* size of LDAPMessage */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1215 | msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1216 | |
| 1217 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1218 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1219 | * protocolOp: 0x61: bindResponse |
| 1220 | */ |
| 1221 | if ((msglen > 2) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1222 | (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1223 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1224 | |
| 1225 | goto out_wakeup; |
| 1226 | } |
| 1227 | |
| 1228 | /* size of bindResponse */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1229 | msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1230 | |
| 1231 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1232 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1233 | */ |
| 1234 | if ((msglen > 4) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1235 | (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1236 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1237 | |
| 1238 | goto out_wakeup; |
| 1239 | } |
| 1240 | |
| 1241 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1242 | * resultCode |
| 1243 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1244 | check->code = *(check->bi->data + msglen + 9); |
| 1245 | if (check->code) { |
| 1246 | set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1247 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1248 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Success"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1249 | } |
| 1250 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1251 | break; |
| 1252 | |
| 1253 | default: |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1254 | /* for other checks (eg: pure TCP), delegate to the main task */ |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1255 | break; |
| 1256 | } /* switch */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 1257 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1258 | out_wakeup: |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1259 | /* collect possible new errors */ |
| 1260 | if (conn->flags & CO_FL_ERROR) |
| 1261 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1262 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1263 | /* Reset the check buffer... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1264 | *check->bi->data = '\0'; |
| 1265 | check->bi->i = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1266 | |
Willy Tarreau | fd29cc5 | 2012-11-23 09:18:20 +0100 | [diff] [blame] | 1267 | /* Close the connection... We absolutely want to perform a hard close |
| 1268 | * and reset the connection if some data are pending, otherwise we end |
| 1269 | * up with many TIME_WAITs and eat all the source port range quickly. |
| 1270 | * To avoid sending RSTs all the time, we first try to drain pending |
| 1271 | * data. |
| 1272 | */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1273 | if (conn->xprt && conn->xprt->shutw) |
| 1274 | conn->xprt->shutw(conn, 0); |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 1275 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1276 | /* OK, let's not stay here forever */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1277 | if (check->result == CHK_RES_FAILED) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1278 | conn->flags |= CO_FL_ERROR; |
| 1279 | |
Willy Tarreau | a522f80 | 2012-11-23 08:56:35 +0100 | [diff] [blame] | 1280 | __conn_data_stop_both(conn); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 1281 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1282 | return; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1283 | |
| 1284 | wait_more_data: |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 1285 | __conn_data_want_recv(conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1286 | } |
| 1287 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1288 | /* |
| 1289 | * This function is used only for server health-checks. It handles connection |
| 1290 | * status updates including errors. If necessary, it wakes the check task up. |
| 1291 | * It always returns 0. |
| 1292 | */ |
| 1293 | static int wake_srv_chk(struct connection *conn) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1294 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1295 | struct check *check = conn->owner; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1296 | |
Willy Tarreau | 6c560da | 2012-11-24 11:14:45 +0100 | [diff] [blame] | 1297 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1298 | /* We may get error reports bypassing the I/O handlers, typically |
| 1299 | * the case when sending a pure TCP check which fails, then the I/O |
| 1300 | * handlers above are not called. This is completely handled by the |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1301 | * main processing task so let's simply wake it up. If we get here, |
| 1302 | * we expect errno to still be valid. |
| 1303 | */ |
| 1304 | chk_report_conn_err(conn, errno, 0); |
| 1305 | |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1306 | __conn_data_stop_both(conn); |
| 1307 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1308 | } |
Willy Tarreau | 3be293f | 2014-02-05 18:31:24 +0100 | [diff] [blame] | 1309 | else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) { |
| 1310 | /* we may get here if only a connection probe was required : we |
| 1311 | * don't have any data to send nor anything expected in response, |
| 1312 | * so the completion of the connection establishment is enough. |
| 1313 | */ |
| 1314 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1315 | } |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1317 | if (check->result != CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1318 | /* We're here because nobody wants to handle the error, so we |
| 1319 | * sure want to abort the hard way. |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1320 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1321 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1322 | conn_force_close(conn); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1323 | } |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1324 | return 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1327 | struct data_cb check_conn_cb = { |
| 1328 | .recv = event_srv_chk_r, |
| 1329 | .send = event_srv_chk_w, |
| 1330 | .wake = wake_srv_chk, |
| 1331 | }; |
| 1332 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1333 | /* |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1334 | * updates the server's weight during a warmup stage. Once the final weight is |
| 1335 | * reached, the task automatically stops. Note that any server status change |
| 1336 | * must have updated s->last_change accordingly. |
| 1337 | */ |
| 1338 | static struct task *server_warmup(struct task *t) |
| 1339 | { |
| 1340 | struct server *s = t->context; |
| 1341 | |
| 1342 | /* by default, plan on stopping the task */ |
| 1343 | t->expire = TICK_ETERNITY; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 1344 | if ((s->admin & SRV_ADMF_MAINT) || |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1345 | (s->state != SRV_ST_STARTING)) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1346 | return t; |
| 1347 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1348 | /* recalculate the weights and update the state */ |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1349 | server_recalc_eweight(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1350 | |
| 1351 | /* probably that we can refill this server with a bit more connections */ |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1352 | pendconn_grab_from_px(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1353 | |
| 1354 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1355 | * whichever is greater, resulting in small 5% steps. |
| 1356 | */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1357 | if (s->state == SRV_ST_STARTING) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1358 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1359 | return t; |
| 1360 | } |
| 1361 | |
| 1362 | /* |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1363 | * establish a server health-check. |
| 1364 | * |
| 1365 | * It can return one of : |
| 1366 | * - SN_ERR_NONE if everything's OK and tcpcheck_main() was not called |
| 1367 | * - SN_ERR_UP if if everything's OK and tcpcheck_main() was called |
| 1368 | * - SN_ERR_SRVTO if there are no more servers |
| 1369 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1370 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1371 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1372 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1373 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1374 | * Note that we try to prevent the network stack from sending the ACK during the |
| 1375 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 1376 | */ |
| 1377 | static int connect_chk(struct task *t) |
| 1378 | { |
| 1379 | struct check *check = t->context; |
| 1380 | struct server *s = check->server; |
| 1381 | struct connection *conn = check->conn; |
| 1382 | struct protocol *proto; |
| 1383 | int ret; |
Willy Tarreau | 02c6212 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1384 | int quickack; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1385 | |
| 1386 | /* tcpcheck send/expect initialisation */ |
| 1387 | if (check->type == PR_O2_TCPCHK_CHK) |
| 1388 | check->current_step = NULL; |
| 1389 | |
| 1390 | /* prepare the check buffer. |
| 1391 | * This should not be used if check is the secondary agent check |
| 1392 | * of a server as s->proxy->check_req will relate to the |
| 1393 | * configuration of the primary check. Similarly, tcp-check uses |
| 1394 | * its own strings. |
| 1395 | */ |
| 1396 | if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) { |
| 1397 | bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len); |
| 1398 | |
| 1399 | /* we want to check if this host replies to HTTP or SSLv3 requests |
| 1400 | * so we'll send the request, and won't wake the checker up now. |
| 1401 | */ |
| 1402 | if ((check->type) == PR_O2_SSL3_CHK) { |
| 1403 | /* SSL requires that we put Unix time in the request */ |
| 1404 | int gmt_time = htonl(date.tv_sec); |
| 1405 | memcpy(check->bo->data + 11, &gmt_time, 4); |
| 1406 | } |
| 1407 | else if ((check->type) == PR_O2_HTTP_CHK) { |
| 1408 | if (s->proxy->options2 & PR_O2_CHK_SNDST) |
| 1409 | bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size)); |
Cyril Bonté | a448e16 | 2015-01-30 00:07:07 +0100 | [diff] [blame] | 1410 | /* prevent HTTP keep-alive when "http-check expect" is used */ |
| 1411 | if (s->proxy->options2 & PR_O2_EXP_TYPE) |
| 1412 | bo_putstr(check->bo, "Connection: close\r\n"); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1413 | bo_putstr(check->bo, "\r\n"); |
| 1414 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /* prepare a new connection */ |
| 1419 | conn_init(conn); |
Cyril Bonté | 1f96a87 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 1420 | conn_prepare(conn, s->check_common.proto, check->xprt); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1421 | conn_attach(conn, check, &check_conn_cb); |
| 1422 | conn->target = &s->obj_type; |
| 1423 | |
| 1424 | /* no client address */ |
| 1425 | clear_addr(&conn->addr.from); |
| 1426 | |
| 1427 | if (is_addr(&s->check_common.addr)) { |
| 1428 | |
| 1429 | /* we'll connect to the check addr specified on the server */ |
| 1430 | conn->addr.to = s->check_common.addr; |
| 1431 | proto = s->check_common.proto; |
| 1432 | } |
| 1433 | else { |
| 1434 | /* we'll connect to the addr on the server */ |
| 1435 | conn->addr.to = s->addr; |
| 1436 | proto = s->proto; |
| 1437 | } |
| 1438 | |
| 1439 | if (check->port) { |
| 1440 | set_host_port(&conn->addr.to, check->port); |
| 1441 | } |
| 1442 | |
Willy Tarreau | 02c6212 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1443 | /* only plain tcp-check supports quick ACK */ |
| 1444 | quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK; |
| 1445 | |
Willy Tarreau | 619a6ae | 2014-12-08 11:52:28 +0100 | [diff] [blame] | 1446 | if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(&s->proxy->tcpcheck_rules)) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1447 | struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n; |
| 1448 | /* if first step is a 'connect', then tcpcheck_main must run it */ |
| 1449 | if (r->action == TCPCHK_ACT_CONNECT) { |
| 1450 | tcpcheck_main(conn); |
| 1451 | return SN_ERR_UP; |
| 1452 | } |
Willy Tarreau | 02c6212 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1453 | if (r->action == TCPCHK_ACT_EXPECT) |
| 1454 | quickack = 0; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | ret = SN_ERR_INTERNAL; |
| 1458 | if (proto->connect) |
Willy Tarreau | 02c6212 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1459 | ret = proto->connect(conn, check->type, quickack ? 2 : 0); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1460 | conn->flags |= CO_FL_WAKE_DATA; |
| 1461 | if (s->check.send_proxy) { |
| 1462 | conn->send_proxy_ofs = 1; |
| 1463 | conn->flags |= CO_FL_SEND_PROXY; |
| 1464 | } |
| 1465 | |
| 1466 | return ret; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1470 | * manages a server health-check. Returns |
| 1471 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1472 | */ |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 1473 | static struct task *process_chk(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1474 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1475 | struct check *check = t->context; |
| 1476 | struct server *s = check->server; |
| 1477 | struct connection *conn = check->conn; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1478 | int rv; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1479 | int ret; |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1480 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1481 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1482 | if (!(check->state & CHK_ST_INPROGRESS)) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1483 | /* no check currently running */ |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1484 | if (!expired) /* woke up too early */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1485 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1486 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1487 | /* we don't send any health-checks when the proxy is |
| 1488 | * stopped, the server should not be checked or the check |
| 1489 | * is disabled. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1490 | */ |
Willy Tarreau | 0d924cc | 2013-12-11 21:26:24 +0100 | [diff] [blame] | 1491 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Willy Tarreau | 33a08db | 2013-12-11 21:03:31 +0100 | [diff] [blame] | 1492 | s->proxy->state == PR_STSTOPPED) |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1493 | goto reschedule; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1494 | |
| 1495 | /* we'll initiate a new check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1496 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1497 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1498 | check->state |= CHK_ST_INPROGRESS; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1499 | check->bi->p = check->bi->data; |
| 1500 | check->bi->i = 0; |
| 1501 | check->bo->p = check->bo->data; |
| 1502 | check->bo->o = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1503 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1504 | ret = connect_chk(t); |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1505 | switch (ret) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1506 | case SN_ERR_UP: |
| 1507 | return t; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1508 | case SN_ERR_NONE: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1509 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1510 | * to establish but only when timeout.check is set |
| 1511 | * as it may be to short for a full check otherwise |
| 1512 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1513 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1514 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1515 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1516 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1517 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1518 | } |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1519 | |
| 1520 | if (check->type) |
| 1521 | conn_data_want_recv(conn); /* prepare for reading a possible reply */ |
| 1522 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1523 | goto reschedule; |
| 1524 | |
| 1525 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1526 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1527 | conn->flags |= CO_FL_ERROR; |
| 1528 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1529 | break; |
| 1530 | case SN_ERR_PRXCOND: |
| 1531 | case SN_ERR_RESOURCE: |
| 1532 | case SN_ERR_INTERNAL: |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1533 | conn->flags |= CO_FL_ERROR; |
| 1534 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1535 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1538 | /* here, we have seen a synchronous error, no fd was allocated */ |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1539 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1540 | check->state &= ~CHK_ST_INPROGRESS; |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1541 | check_notify_failure(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1542 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1543 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1544 | * to establish but only when timeout.check is set |
| 1545 | * as it may be to short for a full check otherwise |
| 1546 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1547 | while (tick_is_expired(t->expire, now_ms)) { |
| 1548 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1549 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1550 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1551 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1552 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1553 | if (s->proxy->timeout.check) |
| 1554 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1555 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1556 | } |
| 1557 | else { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1558 | /* there was a test running. |
| 1559 | * First, let's check whether there was an uncaught error, |
| 1560 | * which can happen on connect timeout or error. |
| 1561 | */ |
Simon Horman | 9ac7cab | 2014-06-20 12:29:47 +0900 | [diff] [blame] | 1562 | if (check->result == CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1563 | /* good connection is enough for pure TCP check */ |
| 1564 | if ((conn->flags & CO_FL_CONNECTED) && !check->type) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1565 | if (check->use_ssl) |
| 1566 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1567 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1568 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1569 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1570 | else if ((conn->flags & CO_FL_ERROR) || expired) { |
| 1571 | chk_report_conn_err(conn, 0, expired); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1572 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1573 | else |
| 1574 | goto out_wait; /* timeout not reached, wait again */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1577 | /* check complete or aborted */ |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1578 | if (conn->xprt) { |
| 1579 | /* The check was aborted and the connection was not yet closed. |
| 1580 | * This can happen upon timeout, or when an external event such |
| 1581 | * as a failed response coupled with "observe layer7" caused the |
| 1582 | * server state to be suddenly changed. |
| 1583 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1584 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1585 | conn_force_close(conn); |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1586 | } |
| 1587 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1588 | if (check->result == CHK_RES_FAILED) { |
| 1589 | /* a failure or timeout detected */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1590 | check_notify_failure(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1591 | } |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 1592 | else if (check->result == CHK_RES_CONDPASS) { |
| 1593 | /* check is OK but asks for stopping mode */ |
| 1594 | check_notify_stopping(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1595 | } |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 1596 | else if (check->result == CHK_RES_PASSED) { |
| 1597 | /* a success was detected */ |
| 1598 | check_notify_success(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1599 | } |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1600 | check->state &= ~CHK_ST_INPROGRESS; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1601 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1602 | rv = 0; |
| 1603 | if (global.spread_checks > 0) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1604 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1605 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1606 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1607 | 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] | 1608 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1609 | |
| 1610 | reschedule: |
| 1611 | while (tick_is_expired(t->expire, now_ms)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1612 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1613 | out_wait: |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1614 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1615 | } |
| 1616 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1617 | static int start_check_task(struct check *check, int mininter, |
| 1618 | int nbcheck, int srvpos) |
| 1619 | { |
| 1620 | struct task *t; |
| 1621 | /* task for the check */ |
| 1622 | if ((t = task_new()) == NULL) { |
| 1623 | Alert("Starting [%s:%s] check: out of memory.\n", |
| 1624 | check->server->proxy->id, check->server->id); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | check->task = t; |
| 1629 | t->process = process_chk; |
| 1630 | t->context = check; |
| 1631 | |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1632 | if (mininter < srv_getinter(check)) |
| 1633 | mininter = srv_getinter(check); |
| 1634 | |
| 1635 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 1636 | mininter = global.max_spread_checks; |
| 1637 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1638 | /* check this every ms */ |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1639 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1640 | check->start = now; |
| 1641 | task_queue(t); |
| 1642 | |
| 1643 | return 1; |
| 1644 | } |
| 1645 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1646 | /* |
| 1647 | * Start health-check. |
| 1648 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 1649 | */ |
| 1650 | int start_checks() { |
| 1651 | |
| 1652 | struct proxy *px; |
| 1653 | struct server *s; |
| 1654 | struct task *t; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1655 | int nbcheck=0, mininter=0, srvpos=0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1656 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1657 | /* 1- count the checkers to run simultaneously. |
| 1658 | * We also determine the minimum interval among all of those which |
| 1659 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1660 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 1661 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1662 | * too short an interval for all others. |
| 1663 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1664 | for (px = proxy; px; px = px->next) { |
| 1665 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 1666 | if (s->slowstart) { |
| 1667 | if ((t = task_new()) == NULL) { |
| 1668 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1669 | return -1; |
| 1670 | } |
| 1671 | /* We need a warmup task that will be called when the server |
| 1672 | * state switches from down to up. |
| 1673 | */ |
| 1674 | s->warmup = t; |
| 1675 | t->process = server_warmup; |
| 1676 | t->context = s; |
| 1677 | t->expire = TICK_ETERNITY; |
| 1678 | } |
| 1679 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 1680 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1681 | nbcheck++; |
| 1682 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 1683 | (!mininter || mininter > srv_getinter(&s->check))) |
| 1684 | mininter = srv_getinter(&s->check); |
| 1685 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 1686 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 1687 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1688 | nbcheck++; |
| 1689 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 1690 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 1691 | mininter = srv_getinter(&s->agent); |
| 1692 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1696 | if (!nbcheck) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1697 | return 0; |
| 1698 | |
| 1699 | srand((unsigned)time(NULL)); |
| 1700 | |
| 1701 | /* |
| 1702 | * 2- start them as far as possible from each others. For this, we will |
| 1703 | * start them after their interval set to the min interval divided by |
| 1704 | * the number of servers, weighted by the server's position in the list. |
| 1705 | */ |
| 1706 | for (px = proxy; px; px = px->next) { |
| 1707 | for (s = px->srv; s; s = s->next) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1708 | /* A task for the main check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 1709 | if (s->check.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1710 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 1711 | return -1; |
| 1712 | srvpos++; |
| 1713 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1714 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1715 | /* A task for a auxiliary agent check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 1716 | if (s->agent.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1717 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 1718 | return -1; |
| 1719 | } |
| 1720 | srvpos++; |
| 1721 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | return 0; |
| 1725 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1726 | |
| 1727 | /* |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 1728 | * Perform content verification check on data in s->check.buffer buffer. |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1729 | * The buffer MUST be terminated by a null byte before calling this function. |
| 1730 | * Sets server status appropriately. The caller is responsible for ensuring |
| 1731 | * that the buffer contains at least 13 characters. If <done> is zero, we may |
| 1732 | * return 0 to indicate that data is required to decide of a match. |
| 1733 | */ |
| 1734 | static int httpchk_expect(struct server *s, int done) |
| 1735 | { |
| 1736 | static char status_msg[] = "HTTP status check returned code <000>"; |
| 1737 | char status_code[] = "000"; |
| 1738 | char *contentptr; |
| 1739 | int crlf; |
| 1740 | int ret; |
| 1741 | |
| 1742 | switch (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 1743 | case PR_O2_EXP_STS: |
| 1744 | case PR_O2_EXP_RSTS: |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1745 | memcpy(status_code, s->check.bi->data + 9, 3); |
| 1746 | memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1747 | |
| 1748 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS) |
| 1749 | ret = strncmp(s->proxy->expect_str, status_code, 3) == 0; |
| 1750 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 1751 | ret = regex_exec(s->proxy->expect_regex, status_code); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1752 | |
| 1753 | /* we necessarily have the response, so there are no partial failures */ |
| 1754 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 1755 | ret = !ret; |
| 1756 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1757 | set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1758 | break; |
| 1759 | |
| 1760 | case PR_O2_EXP_STR: |
| 1761 | case PR_O2_EXP_RSTR: |
| 1762 | /* very simple response parser: ignore CR and only count consecutive LFs, |
| 1763 | * stop with contentptr pointing to first char after the double CRLF or |
| 1764 | * to '\0' if crlf < 2. |
| 1765 | */ |
| 1766 | crlf = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1767 | for (contentptr = s->check.bi->data; *contentptr; contentptr++) { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1768 | if (crlf >= 2) |
| 1769 | break; |
| 1770 | if (*contentptr == '\r') |
| 1771 | continue; |
| 1772 | else if (*contentptr == '\n') |
| 1773 | crlf++; |
| 1774 | else |
| 1775 | crlf = 0; |
| 1776 | } |
| 1777 | |
| 1778 | /* Check that response contains a body... */ |
| 1779 | if (crlf < 2) { |
| 1780 | if (!done) |
| 1781 | return 0; |
| 1782 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1783 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1784 | "HTTP content check could not find a response body"); |
| 1785 | return 1; |
| 1786 | } |
| 1787 | |
| 1788 | /* Check that response body is not empty... */ |
| 1789 | if (*contentptr == '\0') { |
Willy Tarreau | a164fb5 | 2011-04-13 09:32:41 +0200 | [diff] [blame] | 1790 | if (!done) |
| 1791 | return 0; |
| 1792 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1793 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1794 | "HTTP content check found empty response body"); |
| 1795 | return 1; |
| 1796 | } |
| 1797 | |
| 1798 | /* Check the response content against the supplied string |
| 1799 | * or regex... */ |
| 1800 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR) |
| 1801 | ret = strstr(contentptr, s->proxy->expect_str) != NULL; |
| 1802 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 1803 | ret = regex_exec(s->proxy->expect_regex, contentptr); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1804 | |
| 1805 | /* if we don't match, we may need to wait more */ |
| 1806 | if (!ret && !done) |
| 1807 | return 0; |
| 1808 | |
| 1809 | if (ret) { |
| 1810 | /* content matched */ |
| 1811 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1812 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1813 | "HTTP check matched unwanted content"); |
| 1814 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1815 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1816 | "HTTP content check matched"); |
| 1817 | } |
| 1818 | else { |
| 1819 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1820 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1821 | "HTTP check did not match unwanted content"); |
| 1822 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1823 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1824 | "HTTP content check did not match"); |
| 1825 | } |
| 1826 | break; |
| 1827 | } |
| 1828 | return 1; |
| 1829 | } |
| 1830 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1831 | /* |
| 1832 | * return the id of a step in a send/expect session |
| 1833 | */ |
| 1834 | static int tcpcheck_get_step_id(struct server *s) |
| 1835 | { |
| 1836 | struct tcpcheck_rule *cur = NULL, *next = NULL; |
| 1837 | int i = 0; |
| 1838 | |
Willy Tarreau | 90055f2 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 1839 | /* not even started anything yet => step 0 = initial connect */ |
| 1840 | if (!s->check.current_step) |
| 1841 | return 0; |
| 1842 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1843 | cur = s->check.last_started_step; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1844 | |
| 1845 | /* no step => first step */ |
| 1846 | if (cur == NULL) |
| 1847 | return 1; |
| 1848 | |
| 1849 | /* increment i until current step */ |
| 1850 | list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) { |
| 1851 | if (next->list.p == &cur->list) |
| 1852 | break; |
| 1853 | ++i; |
| 1854 | } |
| 1855 | |
| 1856 | return i; |
| 1857 | } |
| 1858 | |
| 1859 | static void tcpcheck_main(struct connection *conn) |
| 1860 | { |
| 1861 | char *contentptr; |
Willy Tarreau | 02c6212 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1862 | struct tcpcheck_rule *cur, *next; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1863 | int done = 0, ret = 0; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1864 | struct check *check = conn->owner; |
| 1865 | struct server *s = check->server; |
| 1866 | struct task *t = check->task; |
Willy Tarreau | e61737a | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 1867 | struct list *head = &s->proxy->tcpcheck_rules; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1868 | |
Willy Tarreau | e61737a | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 1869 | /* here, we know that the check is complete or that it failed */ |
| 1870 | if (check->result != CHK_RES_UNKNOWN) |
| 1871 | goto out_end_tcpcheck; |
| 1872 | |
| 1873 | /* We have 4 possibilities here : |
| 1874 | * 1. we've not yet attempted step 1, and step 1 is a connect, so no |
| 1875 | * connection attempt was made yet ; |
| 1876 | * 2. we've not yet attempted step 1, and step 1 is a not connect or |
| 1877 | * does not exist (no rule), so a connection attempt was made |
| 1878 | * before coming here. |
| 1879 | * 3. we're coming back after having started with step 1, so we may |
| 1880 | * be waiting for a connection attempt to complete. |
| 1881 | * 4. the connection + handshake are complete |
| 1882 | * |
| 1883 | * #2 and #3 are quite similar, we want both the connection and the |
| 1884 | * handshake to complete before going any further. Thus we must always |
| 1885 | * wait for a connection to complete unless we're before and existing |
| 1886 | * step 1. |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1887 | */ |
Willy Tarreau | e61737a | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 1888 | if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) && |
| 1889 | (check->current_step || LIST_ISEMPTY(head))) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1890 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1891 | * to establish but only when timeout.check is set |
| 1892 | * as it may be to short for a full check otherwise |
| 1893 | */ |
| 1894 | while (tick_is_expired(t->expire, now_ms)) { |
| 1895 | int t_con; |
| 1896 | |
| 1897 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1898 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1899 | |
| 1900 | if (s->proxy->timeout.check) |
| 1901 | t->expire = tick_first(t->expire, t_con); |
| 1902 | } |
| 1903 | return; |
| 1904 | } |
| 1905 | |
Willy Tarreau | e61737a | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 1906 | /* special case: option tcp-check with no rule, a connect is enough */ |
| 1907 | if (LIST_ISEMPTY(head)) { |
| 1908 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1909 | goto out_end_tcpcheck; |
Willy Tarreau | e61737a | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 1910 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1911 | |
Willy Tarreau | 90055f2 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 1912 | /* no step means first step initialisation */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1913 | if (check->current_step == NULL) { |
Willy Tarreau | 90055f2 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 1914 | check->last_started_step = NULL; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1915 | check->bo->p = check->bo->data; |
| 1916 | check->bo->o = 0; |
| 1917 | check->bi->p = check->bi->data; |
| 1918 | check->bi->i = 0; |
| 1919 | cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list); |
| 1920 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1921 | if (s->proxy->timeout.check) |
| 1922 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 1923 | } |
| 1924 | /* keep on processing step */ |
| 1925 | else { |
| 1926 | cur = check->current_step; |
| 1927 | } |
| 1928 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1929 | /* It's only the rules which will enable send/recv */ |
| 1930 | __conn_data_stop_both(conn); |
| 1931 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1932 | while (1) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1933 | /* we have to try to flush the output buffer before reading, at the end, |
| 1934 | * or if we're about to send a string that does not fit in the remaining space. |
| 1935 | */ |
| 1936 | if (check->bo->o && |
| 1937 | (&cur->list == head || |
| 1938 | check->current_step->action != TCPCHK_ACT_SEND || |
| 1939 | check->current_step->string_len >= buffer_total_space(check->bo))) { |
| 1940 | |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 1941 | if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1942 | if (conn->flags & CO_FL_ERROR) { |
| 1943 | chk_report_conn_err(conn, errno, 0); |
| 1944 | __conn_data_stop_both(conn); |
| 1945 | goto out_end_tcpcheck; |
| 1946 | } |
| 1947 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1948 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1951 | /* did we reach the end ? If so, let's check that everything was sent */ |
| 1952 | if (&cur->list == head) { |
| 1953 | if (check->bo->o) |
| 1954 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1955 | break; |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1956 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1957 | |
Willy Tarreau | 02c6212 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1958 | /* have 'next' point to the next rule or NULL if we're on the last one */ |
| 1959 | next = (struct tcpcheck_rule *)cur->list.n; |
| 1960 | if (&next->list == head) |
| 1961 | next = NULL; |
| 1962 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1963 | if (check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 1964 | struct protocol *proto; |
| 1965 | struct xprt_ops *xprt; |
| 1966 | |
| 1967 | /* mark the step as started */ |
| 1968 | check->last_started_step = check->current_step; |
| 1969 | /* first, shut existing connection */ |
| 1970 | conn_force_close(conn); |
| 1971 | |
| 1972 | /* prepare new connection */ |
| 1973 | /* initialization */ |
| 1974 | conn_init(conn); |
| 1975 | conn_attach(conn, check, &check_conn_cb); |
| 1976 | conn->target = &s->obj_type; |
| 1977 | |
| 1978 | /* no client address */ |
| 1979 | clear_addr(&conn->addr.from); |
| 1980 | |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1981 | if (is_addr(&s->check_common.addr)) { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1982 | /* we'll connect to the check addr specified on the server */ |
| 1983 | conn->addr.to = s->check_common.addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1984 | proto = s->check_common.proto; |
| 1985 | } |
| 1986 | else { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1987 | /* we'll connect to the addr on the server */ |
| 1988 | conn->addr.to = s->addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1989 | proto = s->proto; |
| 1990 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1991 | |
| 1992 | /* port */ |
| 1993 | if (check->current_step->port) |
| 1994 | set_host_port(&conn->addr.to, check->current_step->port); |
| 1995 | else if (check->port) |
| 1996 | set_host_port(&conn->addr.to, check->port); |
| 1997 | |
| 1998 | #ifdef USE_OPENSSL |
| 1999 | if (check->current_step->conn_opts & TCPCHK_OPT_SSL) { |
| 2000 | xprt = &ssl_sock; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2001 | } |
| 2002 | else { |
| 2003 | xprt = &raw_sock; |
| 2004 | } |
| 2005 | #else /* USE_OPENSSL */ |
| 2006 | xprt = &raw_sock; |
| 2007 | #endif /* USE_OPENSSL */ |
| 2008 | conn_prepare(conn, proto, xprt); |
| 2009 | |
| 2010 | ret = SN_ERR_INTERNAL; |
| 2011 | if (proto->connect) |
Willy Tarreau | 02c6212 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 2012 | ret = proto->connect(conn, |
| 2013 | 1 /* I/O polling is always needed */, |
| 2014 | (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2); |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2015 | conn->flags |= CO_FL_WAKE_DATA; |
| 2016 | if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) { |
| 2017 | conn->send_proxy_ofs = 1; |
| 2018 | conn->flags |= CO_FL_SEND_PROXY; |
| 2019 | } |
| 2020 | |
| 2021 | /* It can return one of : |
| 2022 | * - SN_ERR_NONE if everything's OK |
| 2023 | * - SN_ERR_SRVTO if there are no more servers |
| 2024 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 2025 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 2026 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 2027 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 2028 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 2029 | * Note that we try to prevent the network stack from sending the ACK during the |
| 2030 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 2031 | */ |
| 2032 | switch (ret) { |
| 2033 | case SN_ERR_NONE: |
| 2034 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2035 | * to establish but only when timeout.check is set |
| 2036 | * as it may be to short for a full check otherwise |
| 2037 | */ |
| 2038 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2039 | |
| 2040 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 2041 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 2042 | t->expire = tick_first(t->expire, t_con); |
| 2043 | } |
| 2044 | break; |
| 2045 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 2046 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 2047 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s", |
| 2048 | tcpcheck_get_step_id(s), strerror(errno)); |
| 2049 | set_server_check_status(check, HCHK_STATUS_L4CON, trash.str); |
| 2050 | goto out_end_tcpcheck; |
| 2051 | case SN_ERR_PRXCOND: |
| 2052 | case SN_ERR_RESOURCE: |
| 2053 | case SN_ERR_INTERNAL: |
| 2054 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d", |
| 2055 | tcpcheck_get_step_id(s)); |
| 2056 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str); |
| 2057 | goto out_end_tcpcheck; |
| 2058 | } |
| 2059 | |
| 2060 | /* allow next rule */ |
| 2061 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2062 | check->current_step = cur; |
| 2063 | |
| 2064 | /* don't do anything until the connection is established */ |
| 2065 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 2066 | /* update expire time, should be done by process_chk */ |
| 2067 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2068 | * to establish but only when timeout.check is set |
| 2069 | * as it may be to short for a full check otherwise |
| 2070 | */ |
| 2071 | while (tick_is_expired(t->expire, now_ms)) { |
| 2072 | int t_con; |
| 2073 | |
| 2074 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2075 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2076 | |
| 2077 | if (s->proxy->timeout.check) |
| 2078 | t->expire = tick_first(t->expire, t_con); |
| 2079 | } |
| 2080 | return; |
| 2081 | } |
| 2082 | |
| 2083 | } /* end 'connect' */ |
| 2084 | else if (check->current_step->action == TCPCHK_ACT_SEND) { |
| 2085 | /* mark the step as started */ |
| 2086 | check->last_started_step = check->current_step; |
| 2087 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2088 | /* reset the read buffer */ |
| 2089 | if (*check->bi->data != '\0') { |
| 2090 | *check->bi->data = '\0'; |
| 2091 | check->bi->i = 0; |
| 2092 | } |
| 2093 | |
| 2094 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
| 2095 | conn->flags |= CO_FL_ERROR; |
| 2096 | chk_report_conn_err(conn, 0, 0); |
| 2097 | goto out_end_tcpcheck; |
| 2098 | } |
| 2099 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2100 | if (check->current_step->string_len >= check->bo->size) { |
| 2101 | chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d", |
| 2102 | check->current_step->string_len, check->bo->size, |
| 2103 | tcpcheck_get_step_id(s)); |
| 2104 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2105 | goto out_end_tcpcheck; |
| 2106 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2107 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2108 | /* do not try to send if there is no space */ |
| 2109 | if (check->current_step->string_len >= buffer_total_space(check->bo)) |
| 2110 | continue; |
| 2111 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2112 | bo_putblk(check->bo, check->current_step->string, check->current_step->string_len); |
| 2113 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 2114 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2115 | /* go to next rule and try to send */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2116 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2117 | check->current_step = cur; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2118 | } /* end 'send' */ |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2119 | else if (check->current_step->action == TCPCHK_ACT_EXPECT) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2120 | if (unlikely(check->result == CHK_RES_FAILED)) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2121 | goto out_end_tcpcheck; |
| 2122 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 2123 | if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2124 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
| 2125 | done = 1; |
| 2126 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
| 2127 | /* Report network errors only if we got no other data. Otherwise |
| 2128 | * we'll let the upper layers decide whether the response is OK |
| 2129 | * or not. It is very common that an RST sent by the server is |
| 2130 | * reported as an error just after the last data chunk. |
| 2131 | */ |
| 2132 | chk_report_conn_err(conn, errno, 0); |
| 2133 | goto out_end_tcpcheck; |
| 2134 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2135 | } |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2136 | else |
| 2137 | goto out_need_io; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2138 | } |
| 2139 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2140 | /* mark the step as started */ |
| 2141 | check->last_started_step = check->current_step; |
| 2142 | |
| 2143 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2144 | /* Intermediate or complete response received. |
| 2145 | * Terminate string in check->bi->data buffer. |
| 2146 | */ |
| 2147 | if (check->bi->i < check->bi->size) { |
| 2148 | check->bi->data[check->bi->i] = '\0'; |
| 2149 | } |
| 2150 | else { |
| 2151 | check->bi->data[check->bi->i - 1] = '\0'; |
| 2152 | done = 1; /* buffer full, don't wait for more data */ |
| 2153 | } |
| 2154 | |
| 2155 | contentptr = check->bi->data; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2156 | |
| 2157 | /* Check that response body is not empty... */ |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2158 | if (!check->bi->i) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2159 | if (!done) |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2160 | continue; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2161 | |
| 2162 | /* empty response */ |
| 2163 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 2164 | tcpcheck_get_step_id(s)); |
| 2165 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2166 | |
| 2167 | goto out_end_tcpcheck; |
| 2168 | } |
| 2169 | |
| 2170 | if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) ) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2171 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2172 | |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2173 | tcpcheck_expect: |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2174 | if (cur->string != NULL) |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2175 | ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2176 | else if (cur->expect_regex != NULL) |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2177 | ret = regex_exec(cur->expect_regex, contentptr); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2178 | |
| 2179 | if (!ret && !done) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2180 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2181 | |
| 2182 | /* matched */ |
| 2183 | if (ret) { |
| 2184 | /* matched but we did not want to => ERROR */ |
| 2185 | if (cur->inverse) { |
| 2186 | /* we were looking for a string */ |
| 2187 | if (cur->string != NULL) { |
| 2188 | chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d", |
| 2189 | cur->string, tcpcheck_get_step_id(s)); |
| 2190 | } |
| 2191 | else { |
| 2192 | /* we were looking for a regex */ |
| 2193 | chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", |
| 2194 | tcpcheck_get_step_id(s)); |
| 2195 | } |
| 2196 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2197 | goto out_end_tcpcheck; |
| 2198 | } |
| 2199 | /* matched and was supposed to => OK, next step */ |
| 2200 | else { |
| 2201 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2202 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2203 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2204 | goto tcpcheck_expect; |
| 2205 | __conn_data_stop_recv(conn); |
| 2206 | } |
| 2207 | } |
| 2208 | else { |
| 2209 | /* not matched */ |
| 2210 | /* not matched and was not supposed to => OK, next step */ |
| 2211 | if (cur->inverse) { |
| 2212 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2213 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2214 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2215 | goto tcpcheck_expect; |
| 2216 | __conn_data_stop_recv(conn); |
| 2217 | } |
| 2218 | /* not matched but was supposed to => ERROR */ |
| 2219 | else { |
| 2220 | /* we were looking for a string */ |
| 2221 | if (cur->string != NULL) { |
| 2222 | chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d", |
| 2223 | cur->string, tcpcheck_get_step_id(s)); |
| 2224 | } |
| 2225 | else { |
| 2226 | /* we were looking for a regex */ |
| 2227 | chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d", |
| 2228 | tcpcheck_get_step_id(s)); |
| 2229 | } |
| 2230 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2231 | goto out_end_tcpcheck; |
| 2232 | } |
| 2233 | } |
| 2234 | } /* end expect */ |
| 2235 | } /* end loop over double chained step list */ |
| 2236 | |
| 2237 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
| 2238 | goto out_end_tcpcheck; |
| 2239 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2240 | out_need_io: |
| 2241 | if (check->bo->o) |
| 2242 | __conn_data_want_send(conn); |
| 2243 | |
| 2244 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
| 2245 | __conn_data_want_recv(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2246 | return; |
| 2247 | |
| 2248 | out_end_tcpcheck: |
| 2249 | /* collect possible new errors */ |
| 2250 | if (conn->flags & CO_FL_ERROR) |
| 2251 | chk_report_conn_err(conn, 0, 0); |
| 2252 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2253 | /* cleanup before leaving */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2254 | check->current_step = NULL; |
| 2255 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2256 | if (check->result == CHK_RES_FAILED) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2257 | conn->flags |= CO_FL_ERROR; |
| 2258 | |
| 2259 | __conn_data_stop_both(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2260 | |
| 2261 | return; |
| 2262 | } |
| 2263 | |
| 2264 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2265 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2266 | * Local variables: |
| 2267 | * c-indent-level: 8 |
| 2268 | * c-basic-offset: 8 |
| 2269 | * End: |
| 2270 | */ |