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