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