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