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