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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 29 | #include <common/compat.h> |
| 30 | #include <common/config.h> |
| 31 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 32 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | |
| 35 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
| 37 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 38 | #include <proto/checks.h> |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 39 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <proto/fd.h> |
| 41 | #include <proto/log.h> |
| 42 | #include <proto/queue.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 43 | #include <proto/port_range.h> |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 44 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 45 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 46 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/server.h> |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 48 | #include <proto/session.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 49 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/task.h> |
| 51 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 52 | static int httpchk_expect(struct server *s, int done); |
| 53 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 54 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 55 | [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" }, |
| 56 | [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" }, |
| 57 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 58 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 59 | [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" }, |
| 60 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 61 | [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 62 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 63 | [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" }, |
| 64 | [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" }, |
| 65 | [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 66 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 67 | [HCHK_STATUS_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" }, |
| 68 | [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" }, |
| 69 | [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 70 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 71 | [HCHK_STATUS_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" }, |
| 72 | [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 73 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 74 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 75 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 76 | [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" }, |
| 77 | [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" }, |
| 78 | [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 81 | 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] | 82 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 83 | |
| 84 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 85 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 86 | |
| 87 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 88 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 89 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 90 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 91 | |
| 92 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 93 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 94 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 95 | }; |
| 96 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 97 | /* |
| 98 | * Convert check_status code to description |
| 99 | */ |
| 100 | const char *get_check_status_description(short check_status) { |
| 101 | |
| 102 | const char *desc; |
| 103 | |
| 104 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 105 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 106 | else |
| 107 | desc = NULL; |
| 108 | |
| 109 | if (desc && *desc) |
| 110 | return desc; |
| 111 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 112 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Convert check_status code to short info |
| 117 | */ |
| 118 | const char *get_check_status_info(short check_status) { |
| 119 | |
| 120 | const char *info; |
| 121 | |
| 122 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 123 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 124 | else |
| 125 | info = NULL; |
| 126 | |
| 127 | if (info && *info) |
| 128 | return info; |
| 129 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 130 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 133 | const char *get_analyze_status(short analyze_status) { |
| 134 | |
| 135 | const char *desc; |
| 136 | |
| 137 | if (analyze_status < HANA_STATUS_SIZE) |
| 138 | desc = analyze_statuses[analyze_status].desc; |
| 139 | else |
| 140 | desc = NULL; |
| 141 | |
| 142 | if (desc && *desc) |
| 143 | return desc; |
| 144 | else |
| 145 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 146 | } |
| 147 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 148 | #define SSP_O_HCHK 0x0002 |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 149 | |
| 150 | static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) { |
| 151 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 152 | if (s->track) |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 153 | chunk_printf(msg, " via %s/%s", |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 154 | s->track->proxy->id, s->track->id); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 155 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 156 | if (options & SSP_O_HCHK) { |
| 157 | chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status)); |
| 158 | |
| 159 | if (s->check_status >= HCHK_STATUS_L57DATA) |
| 160 | chunk_printf(msg, ", code: %d", s->check_code); |
| 161 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 162 | if (*s->check_desc) { |
| 163 | struct chunk src; |
| 164 | |
| 165 | chunk_printf(msg, ", info: \""); |
| 166 | |
| 167 | chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc)); |
| 168 | chunk_asciiencode(msg, &src, '"'); |
| 169 | |
| 170 | chunk_printf(msg, "\""); |
| 171 | } |
| 172 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 173 | if (s->check_duration >= 0) |
| 174 | chunk_printf(msg, ", check duration: %ldms", s->check_duration); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Krzysztof Piotr Oledzki | 3bb0571 | 2010-09-27 13:10:50 +0200 | [diff] [blame] | 177 | if (xferred >= 0) { |
Krzysztof Piotr Oledzki | b16a607 | 2010-01-10 21:12:58 +0100 | [diff] [blame] | 178 | if (!(s->state & SRV_RUNNING)) |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 179 | chunk_printf(msg, ". %d active and %d backup servers left.%s" |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 180 | " %d sessions active, %d requeued, %d remaining in queue", |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 181 | s->proxy->srv_act, s->proxy->srv_bck, |
| 182 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 183 | s->cur_sess, xferred, s->nbpend); |
| 184 | else |
| 185 | chunk_printf(msg, ". %d active and %d backup servers online.%s" |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 186 | " %d sessions requeued, %d total in queue", |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 187 | s->proxy->srv_act, s->proxy->srv_bck, |
| 188 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 189 | xferred, s->nbpend); |
| 190 | } |
| 191 | } |
| 192 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 193 | /* |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 194 | * Set s->check_status, update s->check_duration and fill s->result with |
| 195 | * an adequate SRV_CHK_* value. |
| 196 | * |
| 197 | * Show information in logs about failed health check if server is UP |
| 198 | * or succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 199 | */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 200 | static void set_server_check_status(struct server *s, short status, char *desc) { |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 201 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 202 | struct chunk msg; |
| 203 | |
| 204 | if (status == HCHK_STATUS_START) { |
| 205 | s->result = SRV_CHK_UNKNOWN; /* no result yet */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 206 | s->check_desc[0] = '\0'; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 207 | s->check_start = now; |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | if (!s->check_status) |
| 212 | return; |
| 213 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 214 | if (desc && *desc) { |
| 215 | strncpy(s->check_desc, desc, HCHK_DESC_LEN-1); |
| 216 | s->check_desc[HCHK_DESC_LEN-1] = '\0'; |
| 217 | } else |
| 218 | s->check_desc[0] = '\0'; |
| 219 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 220 | s->check_status = status; |
| 221 | if (check_statuses[status].result) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 222 | s->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 223 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 224 | if (status == HCHK_STATUS_HANA) |
| 225 | s->check_duration = -1; |
| 226 | else if (!tv_iszero(&s->check_start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 227 | /* set_server_check_status() may be called more than once */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 228 | s->check_duration = tv_ms_elapsed(&s->check_start, &now); |
| 229 | tv_zero(&s->check_start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 233 | (((s->health != 0) && (s->result & SRV_CHK_ERROR)) || |
| 234 | ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) || |
| 235 | ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) || |
| 236 | (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) { |
| 237 | |
| 238 | int health, rise, fall, state; |
| 239 | |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 240 | chunk_init(&msg, trash, trashlen); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 241 | |
| 242 | /* FIXME begin: calculate local version of the health/rise/fall/state */ |
| 243 | health = s->health; |
| 244 | rise = s->rise; |
| 245 | fall = s->fall; |
| 246 | state = s->state; |
| 247 | |
| 248 | if (s->result & SRV_CHK_ERROR) { |
| 249 | if (health > rise) { |
| 250 | health--; /* still good */ |
| 251 | } else { |
| 252 | if (health == rise) |
| 253 | state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
| 254 | |
| 255 | health = 0; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (s->result & SRV_CHK_RUNNING) { |
| 260 | if (health < rise + fall - 1) { |
| 261 | health++; /* was bad, stays for a while */ |
| 262 | |
| 263 | if (health == rise) |
| 264 | state |= SRV_RUNNING; |
| 265 | |
| 266 | if (health >= rise) |
| 267 | health = rise + fall - 1; /* OK now */ |
| 268 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 269 | |
| 270 | /* clear consecutive_errors if observing is enabled */ |
| 271 | if (s->onerror) |
| 272 | s->consecutive_errors = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 273 | } |
| 274 | /* FIXME end: calculate local version of the health/rise/fall/state */ |
| 275 | |
| 276 | chunk_printf(&msg, |
| 277 | "Health check for %sserver %s/%s %s%s", |
| 278 | s->state & SRV_BACKUP ? "backup " : "", |
| 279 | s->proxy->id, s->id, |
| 280 | (s->result & SRV_CHK_DISABLE)?"conditionally ":"", |
| 281 | (s->result & SRV_CHK_RUNNING)?"succeeded":"failed"); |
| 282 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 283 | server_status_printf(&msg, s, SSP_O_HCHK, -1); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 284 | |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 285 | chunk_printf(&msg, ", status: %d/%d %s", |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 286 | (state & SRV_RUNNING) ? (health - rise + 1) : (health), |
| 287 | (state & SRV_RUNNING) ? (fall) : (rise), |
| 288 | (state & SRV_RUNNING)?"UP":"DOWN"); |
| 289 | |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 290 | Warning("%s.\n", trash); |
| 291 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 292 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 295 | /* sends a log message when a backend goes down, and also sets last |
| 296 | * change date. |
| 297 | */ |
| 298 | static void set_backend_down(struct proxy *be) |
| 299 | { |
| 300 | be->last_change = now.tv_sec; |
| 301 | be->down_trans++; |
| 302 | |
| 303 | Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id); |
| 304 | send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id); |
| 305 | } |
| 306 | |
| 307 | /* Redistribute pending connections when a server goes down. The number of |
| 308 | * connections redistributed is returned. |
| 309 | */ |
| 310 | static int redistribute_pending(struct server *s) |
| 311 | { |
| 312 | struct pendconn *pc, *pc_bck, *pc_end; |
| 313 | int xferred = 0; |
| 314 | |
| 315 | FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) { |
| 316 | struct session *sess = pc->sess; |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 317 | if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP && |
| 318 | !(sess->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 319 | /* The REDISP option was specified. We will ignore |
| 320 | * cookie and force to balance or use the dispatcher. |
| 321 | */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 322 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 323 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 324 | sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 325 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 326 | pendconn_free(pc); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 327 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 328 | xferred++; |
| 329 | } |
| 330 | } |
| 331 | return xferred; |
| 332 | } |
| 333 | |
| 334 | /* Check for pending connections at the backend, and assign some of them to |
| 335 | * the server coming up. The server's weight is checked before being assigned |
| 336 | * connections it may not be able to handle. The total number of transferred |
| 337 | * connections is returned. |
| 338 | */ |
| 339 | static int check_for_pending(struct server *s) |
| 340 | { |
| 341 | int xferred; |
| 342 | |
| 343 | if (!s->eweight) |
| 344 | return 0; |
| 345 | |
| 346 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
| 347 | struct session *sess; |
| 348 | struct pendconn *p; |
| 349 | |
| 350 | p = pendconn_from_px(s->proxy); |
| 351 | if (!p) |
| 352 | break; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 353 | set_target_server(&p->sess->target, s); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 354 | sess = p->sess; |
| 355 | pendconn_free(p); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 356 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 357 | } |
| 358 | return xferred; |
| 359 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 360 | |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 361 | /* Shutdown all connections of a server. The caller must pass a termination |
| 362 | * code in <why>, which must be one of SN_ERR_* indicating the reason for the |
| 363 | * shutdown. |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 364 | */ |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 365 | static void shutdown_sessions(struct server *srv, int why) |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 366 | { |
| 367 | struct session *session, *session_bck; |
| 368 | |
Willy Tarreau | a2a64e9 | 2011-09-07 23:01:56 +0200 | [diff] [blame] | 369 | list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv) |
| 370 | if (session->srv_conn == srv) |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 371 | session_shutdown(session, why); |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 372 | } |
| 373 | |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 374 | /* Shutdown all connections of all backup servers of a proxy. The caller must |
| 375 | * pass a termination code in <why>, which must be one of SN_ERR_* indicating |
| 376 | * the reason for the shutdown. |
| 377 | */ |
| 378 | static void shutdown_backup_sessions(struct proxy *px, int why) |
| 379 | { |
| 380 | struct server *srv; |
| 381 | |
| 382 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 383 | if (srv->state & SRV_BACKUP) |
| 384 | shutdown_sessions(srv, why); |
| 385 | } |
| 386 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 387 | /* Sets server <s> down, notifies by all available means, recounts the |
| 388 | * remaining servers on the proxy and transfers queued sessions whenever |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 389 | * possible to other servers. It automatically recomputes the number of |
| 390 | * servers, but not the map. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 391 | */ |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 392 | void set_server_down(struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 393 | { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 394 | struct server *srv; |
| 395 | struct chunk msg; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 396 | int xferred; |
| 397 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 398 | if (s->state & SRV_MAINTAIN) { |
| 399 | s->health = s->rise; |
| 400 | } |
| 401 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 402 | if (s->health == s->rise || s->track) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 403 | int srv_was_paused = s->state & SRV_GOINGDOWN; |
Willy Tarreau | d64d225 | 2010-10-17 17:16:42 +0200 | [diff] [blame] | 404 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 405 | |
| 406 | s->last_change = now.tv_sec; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 407 | s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
Willy Tarreau | 9580d16 | 2012-05-19 19:07:40 +0200 | [diff] [blame] | 408 | if (s->proxy->lbprm.set_server_status_down) |
| 409 | s->proxy->lbprm.set_server_status_down(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 410 | |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 411 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 412 | shutdown_sessions(s, SN_ERR_DOWN); |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 413 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 414 | /* we might have sessions queued on this server and waiting for |
| 415 | * a connection. Those which are redispatchable will be queued |
| 416 | * to another server or to the proxy itself. |
| 417 | */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 418 | xferred = redistribute_pending(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 419 | |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 420 | chunk_init(&msg, trash, trashlen); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 421 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 422 | if (s->state & SRV_MAINTAIN) { |
| 423 | chunk_printf(&msg, |
| 424 | "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "", |
| 425 | s->proxy->id, s->id); |
| 426 | } else { |
| 427 | chunk_printf(&msg, |
| 428 | "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "", |
| 429 | s->proxy->id, s->id); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 430 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 431 | server_status_printf(&msg, s, |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 432 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 433 | xferred); |
| 434 | } |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 435 | Warning("%s.\n", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 436 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 437 | /* we don't send an alert if the server was previously paused */ |
| 438 | if (srv_was_paused) |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 439 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 440 | else |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 441 | send_log(s->proxy, LOG_ALERT, "%s.\n", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 442 | |
Willy Tarreau | d64d225 | 2010-10-17 17:16:42 +0200 | [diff] [blame] | 443 | 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] | 444 | set_backend_down(s->proxy); |
| 445 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 446 | s->counters.down_trans++; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 447 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 448 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 449 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 450 | if (! (srv->state & SRV_MAINTAIN)) |
| 451 | /* Only notify tracking servers that are not already in maintenance. */ |
| 452 | set_server_down(srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 453 | } |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 454 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 455 | s->health = 0; /* failure */ |
| 456 | } |
| 457 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 458 | void set_server_up(struct server *s) { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 459 | |
| 460 | struct server *srv; |
| 461 | struct chunk msg; |
| 462 | int xferred; |
Willy Tarreau | 4544678 | 2012-03-09 17:16:09 +0100 | [diff] [blame] | 463 | unsigned int old_state = s->state; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 464 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 465 | if (s->state & SRV_MAINTAIN) { |
| 466 | s->health = s->rise; |
| 467 | } |
| 468 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 469 | if (s->health == s->rise || s->track) { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 470 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 471 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 472 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 473 | s->proxy->last_change = now.tv_sec; |
| 474 | } |
| 475 | |
| 476 | if (s->last_change < now.tv_sec) // ignore negative times |
| 477 | s->down_time += now.tv_sec - s->last_change; |
| 478 | |
| 479 | s->last_change = now.tv_sec; |
| 480 | s->state |= SRV_RUNNING; |
Willy Tarreau | 4544678 | 2012-03-09 17:16:09 +0100 | [diff] [blame] | 481 | s->state &= ~SRV_MAINTAIN; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 482 | |
| 483 | if (s->slowstart > 0) { |
| 484 | s->state |= SRV_WARMINGUP; |
| 485 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 486 | /* For dynamic algorithms, start at the first step of the weight, |
| 487 | * without multiplying by BE_WEIGHT_SCALE. |
| 488 | */ |
| 489 | s->eweight = s->uweight; |
| 490 | if (s->proxy->lbprm.update_server_eweight) |
| 491 | s->proxy->lbprm.update_server_eweight(s); |
| 492 | } |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 493 | 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] | 494 | } |
Willy Tarreau | 9580d16 | 2012-05-19 19:07:40 +0200 | [diff] [blame] | 495 | if (s->proxy->lbprm.set_server_status_up) |
| 496 | s->proxy->lbprm.set_server_status_up(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 497 | |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 498 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 499 | * and it's not a backup server and its effective weight is > 0, |
| 500 | * then it can accept new connections, so we shut down all sessions |
| 501 | * on all backup servers. |
| 502 | */ |
| 503 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 504 | !(s->state & SRV_BACKUP) && s->eweight) |
| 505 | shutdown_backup_sessions(s->proxy, SN_ERR_UP); |
| 506 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 507 | /* check if we can handle some connections queued at the proxy. We |
| 508 | * will take as many as we can handle. |
| 509 | */ |
| 510 | xferred = check_for_pending(s); |
| 511 | |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 512 | chunk_init(&msg, trash, trashlen); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 513 | |
Willy Tarreau | 4544678 | 2012-03-09 17:16:09 +0100 | [diff] [blame] | 514 | if (old_state & SRV_MAINTAIN) { |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 515 | chunk_printf(&msg, |
| 516 | "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "", |
| 517 | s->proxy->id, s->id); |
| 518 | } else { |
| 519 | chunk_printf(&msg, |
| 520 | "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "", |
| 521 | s->proxy->id, s->id); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 522 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 523 | server_status_printf(&msg, s, |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 524 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 525 | xferred); |
| 526 | } |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 527 | |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 528 | Warning("%s.\n", trash); |
| 529 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 530 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 531 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 532 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 533 | if (! (srv->state & SRV_MAINTAIN)) |
| 534 | /* Only notify tracking servers if they're not in maintenance. */ |
| 535 | set_server_up(srv); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | if (s->health >= s->rise) |
| 539 | s->health = s->rise + s->fall - 1; /* OK now */ |
| 540 | |
| 541 | } |
| 542 | |
| 543 | static void set_server_disabled(struct server *s) { |
| 544 | |
| 545 | struct server *srv; |
| 546 | struct chunk msg; |
| 547 | int xferred; |
| 548 | |
| 549 | s->state |= SRV_GOINGDOWN; |
Willy Tarreau | 9580d16 | 2012-05-19 19:07:40 +0200 | [diff] [blame] | 550 | if (s->proxy->lbprm.set_server_status_down) |
| 551 | s->proxy->lbprm.set_server_status_down(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 552 | |
| 553 | /* we might have sessions queued on this server and waiting for |
| 554 | * a connection. Those which are redispatchable will be queued |
| 555 | * to another server or to the proxy itself. |
| 556 | */ |
| 557 | xferred = redistribute_pending(s); |
| 558 | |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 559 | chunk_init(&msg, trash, trashlen); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 560 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 561 | chunk_printf(&msg, |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 562 | "Load-balancing on %sServer %s/%s is disabled", |
| 563 | s->state & SRV_BACKUP ? "Backup " : "", |
| 564 | s->proxy->id, s->id); |
| 565 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 566 | server_status_printf(&msg, s, |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 567 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
Krzysztof Piotr Oledzki | b16a607 | 2010-01-10 21:12:58 +0100 | [diff] [blame] | 568 | xferred); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 569 | |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 570 | Warning("%s.\n", trash); |
| 571 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 572 | |
| 573 | if (!s->proxy->srv_bck && !s->proxy->srv_act) |
| 574 | set_backend_down(s->proxy); |
| 575 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 576 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 577 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 578 | set_server_disabled(srv); |
| 579 | } |
| 580 | |
| 581 | static void set_server_enabled(struct server *s) { |
| 582 | |
| 583 | struct server *srv; |
| 584 | struct chunk msg; |
| 585 | int xferred; |
| 586 | |
| 587 | s->state &= ~SRV_GOINGDOWN; |
Willy Tarreau | 9580d16 | 2012-05-19 19:07:40 +0200 | [diff] [blame] | 588 | if (s->proxy->lbprm.set_server_status_up) |
| 589 | s->proxy->lbprm.set_server_status_up(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 590 | |
| 591 | /* check if we can handle some connections queued at the proxy. We |
| 592 | * will take as many as we can handle. |
| 593 | */ |
| 594 | xferred = check_for_pending(s); |
| 595 | |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 596 | chunk_init(&msg, trash, trashlen); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 597 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 598 | chunk_printf(&msg, |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 599 | "Load-balancing on %sServer %s/%s is enabled again", |
| 600 | s->state & SRV_BACKUP ? "Backup " : "", |
| 601 | s->proxy->id, s->id); |
| 602 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 603 | server_status_printf(&msg, s, |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 604 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
Krzysztof Piotr Oledzki | b16a607 | 2010-01-10 21:12:58 +0100 | [diff] [blame] | 605 | xferred); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 606 | |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 607 | Warning("%s.\n", trash); |
| 608 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 609 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 610 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 611 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 612 | set_server_enabled(srv); |
| 613 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 614 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 615 | void health_adjust(struct server *s, short status) { |
| 616 | |
| 617 | int failed; |
| 618 | int expire; |
| 619 | |
| 620 | /* return now if observing nor health check is not enabled */ |
| 621 | if (!s->observe || !s->check) |
| 622 | return; |
| 623 | |
| 624 | if (s->observe >= HANA_OBS_SIZE) |
| 625 | return; |
| 626 | |
| 627 | if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc) |
| 628 | return; |
| 629 | |
| 630 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 631 | case 1: |
| 632 | failed = 1; |
| 633 | break; |
| 634 | |
| 635 | case 2: |
| 636 | failed = 0; |
| 637 | break; |
| 638 | |
| 639 | default: |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | if (!failed) { |
| 644 | /* good: clear consecutive_errors */ |
| 645 | s->consecutive_errors = 0; |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | s->consecutive_errors++; |
| 650 | |
| 651 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 652 | return; |
| 653 | |
| 654 | sprintf(trash, "Detected %d consecutive errors, last one was: %s", |
| 655 | s->consecutive_errors, get_analyze_status(status)); |
| 656 | |
| 657 | switch (s->onerror) { |
| 658 | case HANA_ONERR_FASTINTER: |
| 659 | /* force fastinter - nothing to do here as all modes force it */ |
| 660 | break; |
| 661 | |
| 662 | case HANA_ONERR_SUDDTH: |
| 663 | /* simulate a pre-fatal failed health check */ |
| 664 | if (s->health > s->rise) |
| 665 | s->health = s->rise + 1; |
| 666 | |
| 667 | /* no break - fall through */ |
| 668 | |
| 669 | case HANA_ONERR_FAILCHK: |
| 670 | /* simulate a failed health check */ |
| 671 | set_server_check_status(s, HCHK_STATUS_HANA, trash); |
| 672 | |
| 673 | if (s->health > s->rise) { |
| 674 | s->health--; /* still good */ |
| 675 | s->counters.failed_checks++; |
| 676 | } |
| 677 | else |
| 678 | set_server_down(s); |
| 679 | |
| 680 | break; |
| 681 | |
| 682 | case HANA_ONERR_MARKDWN: |
| 683 | /* mark server down */ |
| 684 | s->health = s->rise; |
| 685 | set_server_check_status(s, HCHK_STATUS_HANA, trash); |
| 686 | set_server_down(s); |
| 687 | |
| 688 | break; |
| 689 | |
| 690 | default: |
| 691 | /* write a warning? */ |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | s->consecutive_errors = 0; |
| 696 | s->counters.failed_hana++; |
| 697 | |
| 698 | if (s->fastinter) { |
| 699 | expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter)); |
| 700 | if (s->check->expire > expire) |
| 701 | s->check->expire = expire; |
| 702 | } |
| 703 | } |
| 704 | |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 705 | static int httpchk_build_status_header(struct server *s, char *buffer) |
| 706 | { |
| 707 | int sv_state; |
| 708 | int ratio; |
| 709 | int hlen = 0; |
| 710 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 711 | "UP %d/%d", "UP", |
| 712 | "NOLB %d/%d", "NOLB", |
| 713 | "no check" }; |
| 714 | |
| 715 | memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24); |
| 716 | hlen += 24; |
| 717 | |
| 718 | if (!(s->state & SRV_CHECKED)) |
| 719 | sv_state = 6; /* should obviously never happen */ |
| 720 | else if (s->state & SRV_RUNNING) { |
| 721 | if (s->health == s->rise + s->fall - 1) |
| 722 | sv_state = 3; /* UP */ |
| 723 | else |
| 724 | sv_state = 2; /* going down */ |
| 725 | |
| 726 | if (s->state & SRV_GOINGDOWN) |
| 727 | sv_state += 2; |
| 728 | } else { |
| 729 | if (s->health) |
| 730 | sv_state = 1; /* going up */ |
| 731 | else |
| 732 | sv_state = 0; /* DOWN */ |
| 733 | } |
| 734 | |
| 735 | hlen += sprintf(buffer + hlen, |
| 736 | srv_hlt_st[sv_state], |
| 737 | (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health), |
| 738 | (s->state & SRV_RUNNING) ? (s->fall) : (s->rise)); |
| 739 | |
| 740 | hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 741 | s->proxy->id, s->id, |
| 742 | global.node, |
| 743 | (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 744 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 745 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 746 | s->nbpend); |
| 747 | |
| 748 | if ((s->state & SRV_WARMINGUP) && |
| 749 | now.tv_sec < s->last_change + s->slowstart && |
| 750 | now.tv_sec >= s->last_change) { |
| 751 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
| 752 | hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio); |
| 753 | } |
| 754 | |
| 755 | buffer[hlen++] = '\r'; |
| 756 | buffer[hlen++] = '\n'; |
| 757 | |
| 758 | return hlen; |
| 759 | } |
| 760 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 761 | /* |
| 762 | * This function is used only for server health-checks. It handles |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 763 | * the connection acknowledgement. If the proxy requires L7 health-checks, |
| 764 | * it sends the request. In other cases, it calls set_server_check_status() |
| 765 | * to set s->check_status, s->check_duration and s->result. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 766 | */ |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 767 | static void event_srv_chk_w(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 768 | { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 769 | __label__ out_wakeup, out_nowake, out_poll, out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 770 | struct task *t = fdtab[fd].owner; |
| 771 | struct server *s = t->context; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 772 | |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 773 | if (unlikely((s->check_conn->flags & CO_FL_ERROR) || (fdtab[fd].ev & FD_POLL_ERR))) { |
Krzysztof Piotr Oledzki | 6492db5 | 2010-01-02 22:03:01 +0100 | [diff] [blame] | 774 | int skerr, err = errno; |
| 775 | socklen_t lskerr = sizeof(skerr); |
| 776 | |
| 777 | if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr) |
| 778 | err = skerr; |
| 779 | |
| 780 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err)); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 781 | goto out_error; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 782 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 783 | |
| 784 | /* here, we know that the connection is established */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 785 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 786 | if (!(s->result & SRV_CHK_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 787 | /* we don't want to mark 'UP' a server on which we detected an error earlier */ |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 788 | if (s->proxy->options2 & PR_O2_CHK_ANY) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 789 | int ret; |
Willy Tarreau | e9d8788 | 2010-01-27 11:28:42 +0100 | [diff] [blame] | 790 | const char *check_req = s->proxy->check_req; |
| 791 | int check_len = s->proxy->check_len; |
| 792 | |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 793 | /* we want to check if this host replies to HTTP or SSLv3 requests |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 794 | * so we'll send the request, and won't wake the checker up now. |
| 795 | */ |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 796 | |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 797 | if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) { |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 798 | /* SSL requires that we put Unix time in the request */ |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 799 | int gmt_time = htonl(date.tv_sec); |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 800 | memcpy(s->proxy->check_req + 11, &gmt_time, 4); |
| 801 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 802 | else if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK) { |
Willy Tarreau | e9d8788 | 2010-01-27 11:28:42 +0100 | [diff] [blame] | 803 | memcpy(trash, check_req, check_len); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 804 | |
| 805 | if (s->proxy->options2 & PR_O2_CHK_SNDST) |
| 806 | check_len += httpchk_build_status_header(s, trash + check_len); |
| 807 | |
Willy Tarreau | e9d8788 | 2010-01-27 11:28:42 +0100 | [diff] [blame] | 808 | trash[check_len++] = '\r'; |
| 809 | trash[check_len++] = '\n'; |
| 810 | trash[check_len] = '\0'; |
| 811 | check_req = trash; |
| 812 | } |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 813 | |
Willy Tarreau | e9d8788 | 2010-01-27 11:28:42 +0100 | [diff] [blame] | 814 | ret = send(fd, check_req, check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 815 | if (ret == check_len) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 816 | /* we allow up to <timeout.check> if nonzero for a responce */ |
Willy Tarreau | 1e44a49 | 2012-05-25 07:41:38 +0200 | [diff] [blame] | 817 | if (s->proxy->timeout.check) { |
Willy Tarreau | 7cd9d94 | 2008-12-21 13:00:41 +0100 | [diff] [blame] | 818 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
Willy Tarreau | 1e44a49 | 2012-05-25 07:41:38 +0200 | [diff] [blame] | 819 | task_queue(t); |
| 820 | } |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 821 | fd_want_recv(fd); /* prepare for reading reply */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 822 | goto out_nowake; |
| 823 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 824 | else if (ret == 0 || errno == EAGAIN) |
| 825 | goto out_poll; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 826 | else { |
| 827 | switch (errno) { |
| 828 | case ECONNREFUSED: |
| 829 | case ENETUNREACH: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 830 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 831 | break; |
| 832 | |
| 833 | default: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 834 | set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 837 | goto out_error; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 838 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 839 | } |
| 840 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 841 | /* We have no data to send to check the connection, and |
| 842 | * getsockopt() will not inform us whether the connection |
| 843 | * is still pending. So we'll reuse connect() to check the |
| 844 | * state of the socket. This has the advantage of givig us |
| 845 | * the following info : |
| 846 | * - error |
| 847 | * - connecting (EALREADY, EINPROGRESS) |
| 848 | * - connected (EISCONN, 0) |
| 849 | */ |
| 850 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 851 | struct sockaddr_storage sa; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 852 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 853 | if (is_addr(&s->check_addr)) |
| 854 | sa = s->check_addr; |
| 855 | else |
| 856 | sa = s->addr; |
| 857 | |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 858 | set_host_port(&sa, s->check_port); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 859 | |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 860 | if (connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) == 0) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 861 | errno = 0; |
| 862 | |
| 863 | if (errno == EALREADY || errno == EINPROGRESS) |
| 864 | goto out_poll; |
| 865 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 866 | if (errno && errno != EISCONN) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 867 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno)); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 868 | goto out_error; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 869 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 870 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 871 | /* good TCP connection is enough */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 872 | set_server_check_status(s, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 873 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 874 | } |
| 875 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 876 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 877 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 878 | out_nowake: |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 879 | fd_stop_send(fd); /* nothing more to write */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 880 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 881 | return; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 882 | out_poll: |
| 883 | /* The connection is still pending. We'll have to poll it |
| 884 | * before attempting to go further. */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 885 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 886 | fd_poll_send(fd); |
| 887 | return; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 888 | out_error: |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 889 | s->check_conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 890 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | |
| 894 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 895 | * 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] | 896 | * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls |
| 897 | * set_server_check_status() to update s->check_status, s->check_duration |
| 898 | * and s->result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 899 | |
| 900 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 901 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 902 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 903 | * response to an SSL HELLO (the principle is that this is enough to |
| 904 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 905 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 906 | * etc. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 907 | */ |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 908 | static void event_srv_chk_r(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 909 | { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 910 | __label__ out_wakeup; |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 911 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 912 | struct task *t = fdtab[fd].owner; |
| 913 | struct server *s = t->context; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 914 | char *desc; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 915 | int done; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 916 | unsigned short msglen; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 917 | |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 918 | if (unlikely((s->result & SRV_CHK_ERROR) || (s->check_conn->flags & CO_FL_ERROR))) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 919 | /* in case of TCP only, this tells us if the connection failed */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 920 | if (!(s->result & SRV_CHK_ERROR)) |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 921 | set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 922 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 923 | goto out_wakeup; |
| 924 | } |
| 925 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 926 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 927 | * but the connection was closed on the remote end. Fortunately, recv still |
| 928 | * works correctly and we don't need to do the getsockopt() on linux. |
| 929 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 930 | |
| 931 | /* Set buffer to point to the end of the data already read, and check |
| 932 | * that there is free space remaining. If the buffer is full, proceed |
| 933 | * with running the checks without attempting another socket read. |
| 934 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 935 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 936 | done = 0; |
Willy Tarreau | 43961d5 | 2010-10-04 20:39:20 +0200 | [diff] [blame] | 937 | for (len = 0; s->check_data_len < global.tune.chksize; s->check_data_len += len) { |
| 938 | len = recv(fd, s->check_data + s->check_data_len, global.tune.chksize - s->check_data_len, 0); |
Willy Tarreau | 2c7ace0 | 2010-03-16 20:32:04 +0100 | [diff] [blame] | 939 | if (len <= 0) |
| 940 | break; |
| 941 | } |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 942 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 943 | if (len == 0) |
| 944 | done = 1; /* connection hangup received */ |
| 945 | else if (len < 0 && errno != EAGAIN) { |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 946 | /* Report network errors only if we got no other data. Otherwise |
| 947 | * we'll let the upper layers decide whether the response is OK |
| 948 | * or not. It is very common that an RST sent by the server is |
| 949 | * reported as an error just after the last data chunk. |
| 950 | */ |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 951 | done = 1; |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 952 | if (!s->check_data_len) { |
| 953 | if (!(s->result & SRV_CHK_ERROR)) |
| 954 | set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL); |
| 955 | goto out_wakeup; |
| 956 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 959 | /* Intermediate or complete response received. |
| 960 | * Terminate string in check_data buffer. |
| 961 | */ |
Willy Tarreau | 43961d5 | 2010-10-04 20:39:20 +0200 | [diff] [blame] | 962 | if (s->check_data_len < global.tune.chksize) |
Willy Tarreau | 2c7ace0 | 2010-03-16 20:32:04 +0100 | [diff] [blame] | 963 | s->check_data[s->check_data_len] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 964 | else { |
Willy Tarreau | 2c7ace0 | 2010-03-16 20:32:04 +0100 | [diff] [blame] | 965 | s->check_data[s->check_data_len - 1] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 966 | done = 1; /* buffer full, don't wait for more data */ |
| 967 | } |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 968 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 969 | /* Run the checks... */ |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 970 | switch (s->proxy->options2 & PR_O2_CHK_ANY) { |
| 971 | case PR_O2_HTTP_CHK: |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 972 | if (!done && s->check_data_len < strlen("HTTP/1.0 000\r")) |
| 973 | goto wait_more_data; |
| 974 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 975 | /* Check if the server speaks HTTP 1.X */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 976 | if ((s->check_data_len < strlen("HTTP/1.0 000\r")) || |
| 977 | (memcmp(s->check_data, "HTTP/1.", 7) != 0 || |
| 978 | (*(s->check_data + 12) != ' ' && *(s->check_data + 12) != '\r')) || |
| 979 | !isdigit((unsigned char) *(s->check_data + 9)) || !isdigit((unsigned char) *(s->check_data + 10)) || |
| 980 | !isdigit((unsigned char) *(s->check_data + 11))) { |
| 981 | cut_crlf(s->check_data); |
| 982 | set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 983 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 984 | goto out_wakeup; |
| 985 | } |
| 986 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 987 | s->check_code = str2uic(s->check_data + 9); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 988 | desc = ltrim(s->check_data + 12, ' '); |
| 989 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 990 | if ((s->proxy->options & PR_O_DISABLE404) && |
| 991 | (s->state & SRV_RUNNING) && (s->check_code == 404)) { |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 992 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 993 | cut_crlf(desc); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 994 | set_server_check_status(s, HCHK_STATUS_L7OKCD, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 995 | } |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 996 | else if (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 997 | /* Run content verification check... We know we have at least 13 chars */ |
| 998 | if (!httpchk_expect(s, done)) |
| 999 | goto wait_more_data; |
| 1000 | } |
| 1001 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
| 1002 | else if (*(s->check_data + 9) == '2' || *(s->check_data + 9) == '3') { |
| 1003 | cut_crlf(desc); |
| 1004 | set_server_check_status(s, HCHK_STATUS_L7OKD, desc); |
| 1005 | } |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1006 | else { |
| 1007 | cut_crlf(desc); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1008 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1009 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1010 | break; |
| 1011 | |
| 1012 | case PR_O2_SSL3_CHK: |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1013 | if (!done && s->check_data_len < 5) |
| 1014 | goto wait_more_data; |
| 1015 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1016 | /* Check for SSLv3 alert or handshake */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1017 | if ((s->check_data_len >= 5) && (*s->check_data == 0x15 || *s->check_data == 0x16)) |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1018 | set_server_check_status(s, HCHK_STATUS_L6OK, NULL); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 1019 | else |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1020 | set_server_check_status(s, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1021 | break; |
| 1022 | |
| 1023 | case PR_O2_SMTP_CHK: |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1024 | if (!done && s->check_data_len < strlen("000\r")) |
| 1025 | goto wait_more_data; |
| 1026 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1027 | /* Check if the server speaks SMTP */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1028 | if ((s->check_data_len < strlen("000\r")) || |
| 1029 | (*(s->check_data + 3) != ' ' && *(s->check_data + 3) != '\r') || |
| 1030 | !isdigit((unsigned char) *s->check_data) || !isdigit((unsigned char) *(s->check_data + 1)) || |
| 1031 | !isdigit((unsigned char) *(s->check_data + 2))) { |
| 1032 | cut_crlf(s->check_data); |
| 1033 | set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1034 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1035 | goto out_wakeup; |
| 1036 | } |
| 1037 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1038 | s->check_code = str2uic(s->check_data); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1039 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1040 | desc = ltrim(s->check_data + 3, ' '); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1041 | cut_crlf(desc); |
| 1042 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1043 | /* Check for SMTP code 2xx (should be 250) */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1044 | if (*s->check_data == '2') |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1045 | set_server_check_status(s, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 1046 | else |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1047 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1048 | break; |
| 1049 | |
| 1050 | case PR_O2_PGSQL_CHK: |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1051 | if (!done && s->check_data_len < 9) |
| 1052 | goto wait_more_data; |
| 1053 | |
| 1054 | if (s->check_data[0] == 'R') { |
| 1055 | set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok"); |
| 1056 | } |
| 1057 | else { |
| 1058 | if ((s->check_data[0] == 'E') && (s->check_data[5]!=0) && (s->check_data[6]!=0)) |
| 1059 | desc = &s->check_data[6]; |
| 1060 | else |
| 1061 | desc = "PostgreSQL unknown error"; |
| 1062 | |
| 1063 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
| 1064 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1065 | break; |
| 1066 | |
| 1067 | case PR_O2_REDIS_CHK: |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1068 | if (!done && s->check_data_len < 7) |
| 1069 | goto wait_more_data; |
| 1070 | |
| 1071 | if (strcmp(s->check_data, "+PONG\r\n") == 0) { |
| 1072 | set_server_check_status(s, HCHK_STATUS_L7OKD, "Redis server is ok"); |
| 1073 | } |
| 1074 | else { |
| 1075 | set_server_check_status(s, HCHK_STATUS_L7STS, s->check_data); |
| 1076 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1077 | break; |
| 1078 | |
| 1079 | case PR_O2_MYSQL_CHK: |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1080 | if (!done && s->check_data_len < 5) |
| 1081 | goto wait_more_data; |
| 1082 | |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1083 | if (s->proxy->check_len == 0) { // old mode |
| 1084 | if (*(s->check_data + 4) != '\xff') { |
| 1085 | /* We set the MySQL Version in description for information purpose |
| 1086 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1087 | * like mark as down old MySQL server. |
| 1088 | */ |
| 1089 | if (s->check_data_len > 51) { |
| 1090 | desc = ltrim(s->check_data + 5, ' '); |
| 1091 | set_server_check_status(s, HCHK_STATUS_L7OKD, desc); |
| 1092 | } |
| 1093 | else { |
| 1094 | if (!done) |
| 1095 | goto wait_more_data; |
| 1096 | /* it seems we have a OK packet but without a valid length, |
| 1097 | * it must be a protocol error |
| 1098 | */ |
| 1099 | set_server_check_status(s, HCHK_STATUS_L7RSP, s->check_data); |
| 1100 | } |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1101 | } |
| 1102 | else { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1103 | /* An error message is attached in the Error packet */ |
| 1104 | desc = ltrim(s->check_data + 7, ' '); |
| 1105 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
| 1106 | } |
| 1107 | } else { |
| 1108 | unsigned int first_packet_len = ((unsigned int) *s->check_data) + |
| 1109 | (((unsigned int) *(s->check_data + 1)) << 8) + |
| 1110 | (((unsigned int) *(s->check_data + 2)) << 16); |
| 1111 | |
| 1112 | if (s->check_data_len == first_packet_len + 4) { |
| 1113 | /* MySQL Error packet always begin with field_count = 0xff */ |
| 1114 | if (*(s->check_data + 4) != '\xff') { |
| 1115 | /* We have only one MySQL packet and it is a Handshake Initialization packet |
| 1116 | * but we need to have a second packet to know if it is alright |
| 1117 | */ |
| 1118 | if (!done && s->check_data_len < first_packet_len + 5) |
| 1119 | goto wait_more_data; |
| 1120 | } |
| 1121 | else { |
| 1122 | /* We have only one packet and it is an Error packet, |
| 1123 | * an error message is attached, so we can display it |
| 1124 | */ |
| 1125 | desc = &s->check_data[7]; |
| 1126 | //Warning("onlyoneERR: %s\n", desc); |
| 1127 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
| 1128 | } |
| 1129 | } else if (s->check_data_len > first_packet_len + 4) { |
| 1130 | unsigned int second_packet_len = ((unsigned int) *(s->check_data + first_packet_len + 4)) + |
| 1131 | (((unsigned int) *(s->check_data + first_packet_len + 5)) << 8) + |
| 1132 | (((unsigned int) *(s->check_data + first_packet_len + 6)) << 16); |
| 1133 | |
| 1134 | if (s->check_data_len == first_packet_len + 4 + second_packet_len + 4 ) { |
| 1135 | /* We have 2 packets and that's good */ |
| 1136 | /* Check if the second packet is a MySQL Error packet or not */ |
| 1137 | if (*(s->check_data + first_packet_len + 8) != '\xff') { |
| 1138 | /* No error packet */ |
| 1139 | /* We set the MySQL Version in description for information purpose */ |
| 1140 | desc = &s->check_data[5]; |
| 1141 | //Warning("2packetOK: %s\n", desc); |
| 1142 | set_server_check_status(s, HCHK_STATUS_L7OKD, desc); |
| 1143 | } |
| 1144 | else { |
| 1145 | /* An error message is attached in the Error packet |
| 1146 | * so we can display it ! :) |
| 1147 | */ |
| 1148 | desc = &s->check_data[first_packet_len+11]; |
| 1149 | //Warning("2packetERR: %s\n", desc); |
| 1150 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | else { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1155 | if (!done) |
| 1156 | goto wait_more_data; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1157 | /* 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] | 1158 | * it must be a protocol error |
| 1159 | */ |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1160 | desc = &s->check_data[5]; |
| 1161 | //Warning("protoerr: %s\n", desc); |
| 1162 | set_server_check_status(s, HCHK_STATUS_L7RSP, desc); |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1163 | } |
| 1164 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1165 | break; |
| 1166 | |
| 1167 | case PR_O2_LDAP_CHK: |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1168 | if (!done && s->check_data_len < 14) |
| 1169 | goto wait_more_data; |
| 1170 | |
| 1171 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1172 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1173 | * http://tools.ietf.org/html/rfc4511 |
| 1174 | */ |
| 1175 | |
| 1176 | /* http://tools.ietf.org/html/rfc4511#section-4.1.1 |
| 1177 | * LDAPMessage: 0x30: SEQUENCE |
| 1178 | */ |
| 1179 | if ((s->check_data_len < 14) || (*(s->check_data) != '\x30')) { |
| 1180 | set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
| 1181 | } |
| 1182 | else { |
| 1183 | /* size of LDAPMessage */ |
| 1184 | msglen = (*(s->check_data + 1) & 0x80) ? (*(s->check_data + 1) & 0x7f) : 0; |
| 1185 | |
| 1186 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1187 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1188 | * protocolOp: 0x61: bindResponse |
| 1189 | */ |
| 1190 | if ((msglen > 2) || |
| 1191 | (memcmp(s->check_data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1192 | set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
| 1193 | |
| 1194 | goto out_wakeup; |
| 1195 | } |
| 1196 | |
| 1197 | /* size of bindResponse */ |
| 1198 | msglen += (*(s->check_data + msglen + 6) & 0x80) ? (*(s->check_data + msglen + 6) & 0x7f) : 0; |
| 1199 | |
| 1200 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1201 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1202 | */ |
| 1203 | if ((msglen > 4) || |
| 1204 | (memcmp(s->check_data + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1205 | set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
| 1206 | |
| 1207 | goto out_wakeup; |
| 1208 | } |
| 1209 | |
| 1210 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1211 | * resultCode |
| 1212 | */ |
| 1213 | s->check_code = *(s->check_data + msglen + 9); |
| 1214 | if (s->check_code) { |
| 1215 | set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9"); |
| 1216 | } else { |
| 1217 | set_server_check_status(s, HCHK_STATUS_L7OKD, "Success"); |
| 1218 | } |
| 1219 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1220 | break; |
| 1221 | |
| 1222 | default: |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1223 | /* other checks are valid if the connection succeeded anyway */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1224 | set_server_check_status(s, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1225 | break; |
| 1226 | } /* switch */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 1227 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1228 | out_wakeup: |
| 1229 | if (s->result & SRV_CHK_ERROR) |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 1230 | s->check_conn->flags |= CO_FL_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1231 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1232 | /* Reset the check buffer... */ |
| 1233 | *s->check_data = '\0'; |
| 1234 | s->check_data_len = 0; |
| 1235 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1236 | /* Close the connection... */ |
| 1237 | shutdown(fd, SHUT_RDWR); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 1238 | fd_stop_recv(fd); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 1239 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 1240 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1241 | return; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1242 | |
| 1243 | wait_more_data: |
| 1244 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1245 | fd_poll_recv(fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1248 | /* I/O call back for the health checks. Returns 0. */ |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1249 | static int check_iocb(int fd) |
| 1250 | { |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1251 | int e; |
| 1252 | |
| 1253 | if (!fdtab[fd].owner) |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1254 | return 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1255 | |
| 1256 | e = fdtab[fd].ev; |
| 1257 | if (e & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1258 | event_srv_chk_r(fd); |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1259 | if (e & (FD_POLL_OUT | FD_POLL_ERR)) |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1260 | event_srv_chk_w(fd); |
| 1261 | return 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1264 | /* |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1265 | * updates the server's weight during a warmup stage. Once the final weight is |
| 1266 | * reached, the task automatically stops. Note that any server status change |
| 1267 | * must have updated s->last_change accordingly. |
| 1268 | */ |
| 1269 | static struct task *server_warmup(struct task *t) |
| 1270 | { |
| 1271 | struct server *s = t->context; |
| 1272 | |
| 1273 | /* by default, plan on stopping the task */ |
| 1274 | t->expire = TICK_ETERNITY; |
| 1275 | if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP)) |
| 1276 | return t; |
| 1277 | |
| 1278 | if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) { |
| 1279 | /* go to full throttle if the slowstart interval is reached */ |
| 1280 | s->state &= ~SRV_WARMINGUP; |
| 1281 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) |
| 1282 | s->eweight = s->uweight * BE_WEIGHT_SCALE; |
| 1283 | if (s->proxy->lbprm.update_server_eweight) |
| 1284 | s->proxy->lbprm.update_server_eweight(s); |
| 1285 | } |
| 1286 | else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 1287 | /* for dynamic algorithms, let's slowly update the weight */ |
| 1288 | s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) + |
| 1289 | s->slowstart - 1) / s->slowstart; |
| 1290 | s->eweight *= s->uweight; |
| 1291 | if (s->proxy->lbprm.update_server_eweight) |
| 1292 | s->proxy->lbprm.update_server_eweight(s); |
| 1293 | } |
| 1294 | /* Note that static algorithms are already running at full throttle */ |
| 1295 | |
| 1296 | /* probably that we can refill this server with a bit more connections */ |
| 1297 | check_for_pending(s); |
| 1298 | |
| 1299 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1300 | * whichever is greater, resulting in small 5% steps. |
| 1301 | */ |
| 1302 | if (s->state & SRV_WARMINGUP) |
| 1303 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1304 | return t; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1308 | * manages a server health-check. Returns |
| 1309 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1310 | */ |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 1311 | static struct task *process_chk(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1312 | { |
Willy Tarreau | e383880 | 2009-03-21 18:58:32 +0100 | [diff] [blame] | 1313 | int attempts = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1314 | struct server *s = t->context; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1315 | struct sockaddr_storage sa; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1316 | int fd; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1317 | int rv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1318 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1319 | new_chk: |
Willy Tarreau | e383880 | 2009-03-21 18:58:32 +0100 | [diff] [blame] | 1320 | if (attempts++ > 0) { |
| 1321 | /* we always fail to create a server, let's stop insisting... */ |
| 1322 | while (tick_is_expired(t->expire, now_ms)) |
| 1323 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
| 1324 | return t; |
| 1325 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1326 | fd = s->curfd; |
| 1327 | if (fd < 0) { /* no check currently running */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1328 | if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */ |
| 1329 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1330 | |
| 1331 | /* we don't send any health-checks when the proxy is stopped or when |
| 1332 | * the server should not be checked. |
| 1333 | */ |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 1334 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1335 | while (tick_is_expired(t->expire, now_ms)) |
| 1336 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1337 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | /* we'll initiate a new check */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1341 | set_server_check_status(s, HCHK_STATUS_START, NULL); |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1342 | if ((fd = socket(s->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1343 | if ((fd < global.maxsock) && |
| 1344 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 1345 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 1346 | //fprintf(stderr, "process_chk: 3\n"); |
| 1347 | |
Willy Tarreau | 9edd161 | 2007-10-18 18:07:48 +0200 | [diff] [blame] | 1348 | if (s->proxy->options & PR_O_TCP_NOLING) { |
| 1349 | /* We don't want to useless data */ |
| 1350 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 1351 | } |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1352 | |
| 1353 | if (is_addr(&s->check_addr)) |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 1354 | /* we'll connect to the check addr specified on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 1355 | sa = s->check_addr; |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 1356 | else |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 1357 | /* we'll connect to the addr on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 1358 | sa = s->addr; |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 1359 | |
Willy Tarreau | 48da04a | 2011-08-27 11:51:36 +0200 | [diff] [blame] | 1360 | set_host_port(&sa, s->check_port); |
| 1361 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1362 | /* allow specific binding : |
| 1363 | * - server-specific at first |
| 1364 | * - proxy-specific next |
| 1365 | */ |
| 1366 | if (s->state & SRV_BIND_SRC) { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1367 | struct sockaddr_storage *remote = NULL; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1368 | int ret, flags = 0; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1369 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1370 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1371 | if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1372 | remote = &s->tproxy_addr; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1373 | flags = 3; |
| 1374 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1375 | #endif |
Willy Tarreau | c76721d | 2009-02-04 20:20:58 +0100 | [diff] [blame] | 1376 | #ifdef SO_BINDTODEVICE |
| 1377 | /* Note: this might fail if not CAP_NET_RAW */ |
| 1378 | if (s->iface_name) |
| 1379 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 1380 | s->iface_name, s->iface_len + 1); |
Willy Tarreau | c76721d | 2009-02-04 20:20:58 +0100 | [diff] [blame] | 1381 | #endif |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1382 | if (s->sport_range) { |
| 1383 | int bind_attempts = 10; /* should be more than enough to find a spare port */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1384 | struct sockaddr_storage src; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1385 | |
| 1386 | ret = 1; |
| 1387 | src = s->source_addr; |
| 1388 | |
| 1389 | do { |
| 1390 | /* note: in case of retry, we may have to release a previously |
| 1391 | * allocated port, hence this loop's construct. |
| 1392 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 1393 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 1394 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1395 | |
| 1396 | if (!bind_attempts) |
| 1397 | break; |
| 1398 | bind_attempts--; |
| 1399 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 1400 | fdinfo[fd].local_port = port_range_alloc_port(s->sport_range); |
| 1401 | if (!fdinfo[fd].local_port) |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1402 | break; |
| 1403 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 1404 | fdinfo[fd].port_range = s->sport_range; |
Willy Tarreau | 86ad42c | 2011-08-27 12:29:07 +0200 | [diff] [blame] | 1405 | set_host_port(&src, fdinfo[fd].local_port); |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1406 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1407 | ret = tcp_bind_socket(fd, flags, &src, remote); |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1408 | } while (ret != 0); /* binding NOK */ |
| 1409 | } |
| 1410 | else { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1411 | ret = tcp_bind_socket(fd, flags, &s->source_addr, remote); |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1414 | if (ret) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1415 | set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1416 | switch (ret) { |
| 1417 | case 1: |
| 1418 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 1419 | s->proxy->id, s->id); |
| 1420 | break; |
| 1421 | case 2: |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1422 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 1423 | s->proxy->id, s->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1424 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1425 | } |
| 1426 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1427 | } |
| 1428 | else if (s->proxy->options & PR_O_BIND_SRC) { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1429 | struct sockaddr_storage *remote = NULL; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1430 | int ret, flags = 0; |
| 1431 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1432 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1433 | if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1434 | remote = &s->proxy->tproxy_addr; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1435 | flags = 3; |
| 1436 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1437 | #endif |
Willy Tarreau | d53f96b | 2009-02-04 18:46:54 +0100 | [diff] [blame] | 1438 | #ifdef SO_BINDTODEVICE |
| 1439 | /* Note: this might fail if not CAP_NET_RAW */ |
| 1440 | if (s->proxy->iface_name) |
| 1441 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 1442 | s->proxy->iface_name, s->proxy->iface_len + 1); |
Willy Tarreau | d53f96b | 2009-02-04 18:46:54 +0100 | [diff] [blame] | 1443 | #endif |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1444 | ret = tcp_bind_socket(fd, flags, &s->proxy->source_addr, remote); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1445 | if (ret) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1446 | set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1447 | switch (ret) { |
| 1448 | case 1: |
| 1449 | Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n", |
| 1450 | proxy_type_str(s->proxy), s->proxy->id); |
| 1451 | break; |
| 1452 | case 2: |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 1453 | Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n", |
| 1454 | proxy_type_str(s->proxy), s->proxy->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1455 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1456 | } |
| 1457 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1458 | } |
| 1459 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1460 | if (s->result == SRV_CHK_UNKNOWN) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 1461 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 1462 | /* disabling tcp quick ack now allows |
| 1463 | * the request to leave the machine with |
| 1464 | * the first ACK. |
| 1465 | */ |
| 1466 | if (s->proxy->options2 & PR_O2_SMARTCON) |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 1467 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 1468 | #endif |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 1469 | if ((connect(fd, (struct sockaddr *)&sa, get_addr_len(&sa)) != -1) || (errno == EINPROGRESS)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1470 | /* OK, connection in progress or established */ |
| 1471 | |
| 1472 | //fprintf(stderr, "process_chk: 4\n"); |
| 1473 | |
| 1474 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 1475 | s->check_conn->flags = CO_FL_WAIT_L4_CONN; /* TCP connection pending */ |
Willy Tarreau | 7a96648 | 2007-04-15 10:58:02 +0200 | [diff] [blame] | 1476 | fd_insert(fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1477 | fdtab[fd].owner = t; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1478 | fdtab[fd].iocb = &check_iocb; |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 1479 | fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY; |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 1480 | fd_want_send(fd); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1481 | #ifdef DEBUG_FULL |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1482 | assert (!EV_FD_ISSET(fd, DIR_RD)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1483 | #endif |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1484 | //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 1485 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1486 | * to establish but only when timeout.check is set |
| 1487 | * as it may be to short for a full check otherwise |
| 1488 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1489 | t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1490 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1491 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1492 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1493 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | 6054819 | 2008-02-17 11:34:10 +0100 | [diff] [blame] | 1494 | } |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1495 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1496 | } |
| 1497 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 1498 | /* a real error */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1499 | |
| 1500 | switch (errno) { |
| 1501 | /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */ |
| 1502 | case ECONNREFUSED: |
| 1503 | case ENETUNREACH: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1504 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1505 | break; |
| 1506 | |
| 1507 | default: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1508 | set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1509 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | } |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 1513 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 1514 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1515 | close(fd); /* socket creation error */ |
| 1516 | } |
Willy Tarreau | d11ad78 | 2011-03-09 20:38:33 +0100 | [diff] [blame] | 1517 | else |
| 1518 | set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1519 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1520 | if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1521 | //fprintf(stderr, "process_chk: 6\n"); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1522 | while (tick_is_expired(t->expire, now_ms)) |
| 1523 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1524 | goto new_chk; /* may be we should initialize a new check */ |
| 1525 | } |
| 1526 | |
| 1527 | /* here, we have seen a failure */ |
| 1528 | if (s->health > s->rise) { |
| 1529 | s->health--; /* still good */ |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1530 | s->counters.failed_checks++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1531 | } |
| 1532 | else |
| 1533 | set_server_down(s); |
| 1534 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1535 | //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 1536 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1537 | * to establish but only when timeout.check is set |
| 1538 | * as it may be to short for a full check otherwise |
| 1539 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1540 | while (tick_is_expired(t->expire, now_ms)) { |
| 1541 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1542 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1543 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1544 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1546 | if (s->proxy->timeout.check) |
| 1547 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1548 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1549 | goto new_chk; |
| 1550 | } |
| 1551 | else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1552 | /* there was a test running */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1553 | if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1554 | /* we may have to add/remove this server from the LB group */ |
| 1555 | if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) { |
| 1556 | if ((s->state & SRV_GOINGDOWN) && |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 1557 | ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING)) |
| 1558 | set_server_enabled(s); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1559 | else if (!(s->state & SRV_GOINGDOWN) && |
| 1560 | ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) == |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 1561 | (SRV_CHK_RUNNING | SRV_CHK_DISABLE))) |
| 1562 | set_server_disabled(s); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1563 | } |
| 1564 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1565 | if (s->health < s->rise + s->fall - 1) { |
| 1566 | s->health++; /* was bad, stays for a while */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1567 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 1568 | set_server_up(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1569 | } |
| 1570 | s->curfd = -1; /* no check running anymore */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1571 | fd_delete(fd); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 1572 | |
| 1573 | rv = 0; |
| 1574 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1575 | rv = srv_getinter(s) * global.spread_checks / 100; |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 1576 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 1577 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1578 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1579 | goto new_chk; |
| 1580 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1581 | else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) { |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1582 | if (!(s->result & SRV_CHK_ERROR)) { |
| 1583 | if (!EV_FD_ISSET(fd, DIR_RD)) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1584 | set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1585 | } else { |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1586 | if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1587 | set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1588 | else /* HTTP, SMTP */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1589 | set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1593 | /* failure or timeout detected */ |
| 1594 | if (s->health > s->rise) { |
| 1595 | s->health--; /* still good */ |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1596 | s->counters.failed_checks++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1597 | } |
| 1598 | else |
| 1599 | set_server_down(s); |
| 1600 | s->curfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1601 | fd_delete(fd); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1602 | |
| 1603 | rv = 0; |
| 1604 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1605 | rv = srv_getinter(s) * global.spread_checks / 100; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1606 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1607 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1608 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1609 | goto new_chk; |
| 1610 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1611 | /* if result is unknown and there's no timeout, we have to wait again */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1612 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1613 | s->result = SRV_CHK_UNKNOWN; |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1614 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1615 | } |
| 1616 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1617 | /* |
| 1618 | * Start health-check. |
| 1619 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 1620 | */ |
| 1621 | int start_checks() { |
| 1622 | |
| 1623 | struct proxy *px; |
| 1624 | struct server *s; |
| 1625 | struct task *t; |
| 1626 | int nbchk=0, mininter=0, srvpos=0; |
| 1627 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1628 | /* 1- count the checkers to run simultaneously. |
| 1629 | * We also determine the minimum interval among all of those which |
| 1630 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1631 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 1632 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1633 | * too short an interval for all others. |
| 1634 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1635 | for (px = proxy; px; px = px->next) { |
| 1636 | for (s = px->srv; s; s = s->next) { |
| 1637 | if (!(s->state & SRV_CHECKED)) |
| 1638 | continue; |
| 1639 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1640 | if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) && |
| 1641 | (!mininter || mininter > srv_getinter(s))) |
| 1642 | mininter = srv_getinter(s); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1643 | |
| 1644 | nbchk++; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | if (!nbchk) |
| 1649 | return 0; |
| 1650 | |
| 1651 | srand((unsigned)time(NULL)); |
| 1652 | |
| 1653 | /* |
| 1654 | * 2- start them as far as possible from each others. For this, we will |
| 1655 | * start them after their interval set to the min interval divided by |
| 1656 | * the number of servers, weighted by the server's position in the list. |
| 1657 | */ |
| 1658 | for (px = proxy; px; px = px->next) { |
| 1659 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1660 | if (s->slowstart) { |
| 1661 | if ((t = task_new()) == NULL) { |
| 1662 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1663 | return -1; |
| 1664 | } |
| 1665 | /* We need a warmup task that will be called when the server |
| 1666 | * state switches from down to up. |
| 1667 | */ |
| 1668 | s->warmup = t; |
| 1669 | t->process = server_warmup; |
| 1670 | t->context = s; |
| 1671 | t->expire = TICK_ETERNITY; |
| 1672 | } |
| 1673 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1674 | if (!(s->state & SRV_CHECKED)) |
| 1675 | continue; |
| 1676 | |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1677 | /* one task for the checks */ |
Willy Tarreau | a461318 | 2009-03-21 18:13:21 +0100 | [diff] [blame] | 1678 | if ((t = task_new()) == NULL) { |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1679 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1680 | return -1; |
| 1681 | } |
| 1682 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 1683 | s->check = t; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1684 | t->process = process_chk; |
| 1685 | t->context = s; |
| 1686 | |
| 1687 | /* check this every ms */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1688 | t->expire = tick_add(now_ms, |
| 1689 | MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ? |
| 1690 | mininter : srv_getinter(s)) * srvpos / nbchk)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1691 | s->check_start = now; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1692 | task_queue(t); |
| 1693 | |
| 1694 | srvpos++; |
| 1695 | } |
| 1696 | } |
| 1697 | return 0; |
| 1698 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1701 | * Perform content verification check on data in s->check_data buffer. |
| 1702 | * The buffer MUST be terminated by a null byte before calling this function. |
| 1703 | * Sets server status appropriately. The caller is responsible for ensuring |
| 1704 | * that the buffer contains at least 13 characters. If <done> is zero, we may |
| 1705 | * return 0 to indicate that data is required to decide of a match. |
| 1706 | */ |
| 1707 | static int httpchk_expect(struct server *s, int done) |
| 1708 | { |
| 1709 | static char status_msg[] = "HTTP status check returned code <000>"; |
| 1710 | char status_code[] = "000"; |
| 1711 | char *contentptr; |
| 1712 | int crlf; |
| 1713 | int ret; |
| 1714 | |
| 1715 | switch (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 1716 | case PR_O2_EXP_STS: |
| 1717 | case PR_O2_EXP_RSTS: |
| 1718 | memcpy(status_code, s->check_data + 9, 3); |
| 1719 | memcpy(status_msg + strlen(status_msg) - 4, s->check_data + 9, 3); |
| 1720 | |
| 1721 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS) |
| 1722 | ret = strncmp(s->proxy->expect_str, status_code, 3) == 0; |
| 1723 | else |
| 1724 | ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0; |
| 1725 | |
| 1726 | /* we necessarily have the response, so there are no partial failures */ |
| 1727 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 1728 | ret = !ret; |
| 1729 | |
| 1730 | set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg); |
| 1731 | break; |
| 1732 | |
| 1733 | case PR_O2_EXP_STR: |
| 1734 | case PR_O2_EXP_RSTR: |
| 1735 | /* very simple response parser: ignore CR and only count consecutive LFs, |
| 1736 | * stop with contentptr pointing to first char after the double CRLF or |
| 1737 | * to '\0' if crlf < 2. |
| 1738 | */ |
| 1739 | crlf = 0; |
| 1740 | for (contentptr = s->check_data; *contentptr; contentptr++) { |
| 1741 | if (crlf >= 2) |
| 1742 | break; |
| 1743 | if (*contentptr == '\r') |
| 1744 | continue; |
| 1745 | else if (*contentptr == '\n') |
| 1746 | crlf++; |
| 1747 | else |
| 1748 | crlf = 0; |
| 1749 | } |
| 1750 | |
| 1751 | /* Check that response contains a body... */ |
| 1752 | if (crlf < 2) { |
| 1753 | if (!done) |
| 1754 | return 0; |
| 1755 | |
| 1756 | set_server_check_status(s, HCHK_STATUS_L7RSP, |
| 1757 | "HTTP content check could not find a response body"); |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
| 1761 | /* Check that response body is not empty... */ |
| 1762 | if (*contentptr == '\0') { |
Willy Tarreau | a164fb5 | 2011-04-13 09:32:41 +0200 | [diff] [blame] | 1763 | if (!done) |
| 1764 | return 0; |
| 1765 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1766 | set_server_check_status(s, HCHK_STATUS_L7RSP, |
| 1767 | "HTTP content check found empty response body"); |
| 1768 | return 1; |
| 1769 | } |
| 1770 | |
| 1771 | /* Check the response content against the supplied string |
| 1772 | * or regex... */ |
| 1773 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR) |
| 1774 | ret = strstr(contentptr, s->proxy->expect_str) != NULL; |
| 1775 | else |
| 1776 | ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0; |
| 1777 | |
| 1778 | /* if we don't match, we may need to wait more */ |
| 1779 | if (!ret && !done) |
| 1780 | return 0; |
| 1781 | |
| 1782 | if (ret) { |
| 1783 | /* content matched */ |
| 1784 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 1785 | set_server_check_status(s, HCHK_STATUS_L7RSP, |
| 1786 | "HTTP check matched unwanted content"); |
| 1787 | else |
| 1788 | set_server_check_status(s, HCHK_STATUS_L7OKD, |
| 1789 | "HTTP content check matched"); |
| 1790 | } |
| 1791 | else { |
| 1792 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 1793 | set_server_check_status(s, HCHK_STATUS_L7OKD, |
| 1794 | "HTTP check did not match unwanted content"); |
| 1795 | else |
| 1796 | set_server_check_status(s, HCHK_STATUS_L7RSP, |
| 1797 | "HTTP content check did not match"); |
| 1798 | } |
| 1799 | break; |
| 1800 | } |
| 1801 | return 1; |
| 1802 | } |
| 1803 | |
| 1804 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1805 | * Local variables: |
| 1806 | * c-indent-level: 8 |
| 1807 | * c-basic-offset: 8 |
| 1808 | * End: |
| 1809 | */ |