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