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