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> |
| 48 | #include <proto/task.h> |
| 49 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 50 | const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
| 51 | [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" }, |
| 52 | [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" }, |
| 53 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 54 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 55 | [HCHK_STATUS_HANA] = { SRV_CHK_ERROR, "HANA", "Health analyze" }, |
| 56 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 57 | [HCHK_STATUS_SOCKERR] = { SRV_CHK_ERROR, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 58 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 59 | [HCHK_STATUS_L4OK] = { SRV_CHK_RUNNING, "L4OK", "Layer4 check passed" }, |
| 60 | [HCHK_STATUS_L4TOUT] = { SRV_CHK_ERROR, "L4TOUT", "Layer4 timeout" }, |
| 61 | [HCHK_STATUS_L4CON] = { SRV_CHK_ERROR, "L4CON", "Layer4 connection problem" }, |
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_L6OK] = { SRV_CHK_RUNNING, "L6OK", "Layer6 check passed" }, |
| 64 | [HCHK_STATUS_L6TOUT] = { SRV_CHK_ERROR, "L6TOUT", "Layer6 timeout" }, |
| 65 | [HCHK_STATUS_L6RSP] = { SRV_CHK_ERROR, "L6RSP", "Layer6 invalid response" }, |
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_L7TOUT] = { SRV_CHK_ERROR, "L7TOUT", "Layer7 timeout" }, |
| 68 | [HCHK_STATUS_L7RSP] = { SRV_CHK_ERROR, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 69 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 70 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 71 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 72 | [HCHK_STATUS_L7OKD] = { SRV_CHK_RUNNING, "L7OK", "Layer7 check passed" }, |
| 73 | [HCHK_STATUS_L7OKCD] = { SRV_CHK_RUNNING | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" }, |
| 74 | [HCHK_STATUS_L7STS] = { SRV_CHK_ERROR, "L7STS", "Layer7 wrong status" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 77 | const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */ |
| 78 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 79 | |
| 80 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 81 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 82 | |
| 83 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 84 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 85 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 86 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 87 | |
| 88 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 89 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 90 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 91 | }; |
| 92 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Convert check_status code to description |
| 95 | */ |
| 96 | const char *get_check_status_description(short check_status) { |
| 97 | |
| 98 | const char *desc; |
| 99 | |
| 100 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 101 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 102 | else |
| 103 | desc = NULL; |
| 104 | |
| 105 | if (desc && *desc) |
| 106 | return desc; |
| 107 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 108 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Convert check_status code to short info |
| 113 | */ |
| 114 | const char *get_check_status_info(short check_status) { |
| 115 | |
| 116 | const char *info; |
| 117 | |
| 118 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 119 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 120 | else |
| 121 | info = NULL; |
| 122 | |
| 123 | if (info && *info) |
| 124 | return info; |
| 125 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 126 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 129 | const char *get_analyze_status(short analyze_status) { |
| 130 | |
| 131 | const char *desc; |
| 132 | |
| 133 | if (analyze_status < HANA_STATUS_SIZE) |
| 134 | desc = analyze_statuses[analyze_status].desc; |
| 135 | else |
| 136 | desc = NULL; |
| 137 | |
| 138 | if (desc && *desc) |
| 139 | return desc; |
| 140 | else |
| 141 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 142 | } |
| 143 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 144 | #define SSP_O_VIA 0x0001 |
| 145 | #define SSP_O_HCHK 0x0002 |
| 146 | #define SSP_O_STATUS 0x0004 |
| 147 | |
| 148 | static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) { |
| 149 | |
| 150 | if (options & SSP_O_VIA) |
| 151 | chunk_printf(msg, " via %s/%s", |
| 152 | s->tracked->proxy->id, s->tracked->id); |
| 153 | |
| 154 | |
| 155 | if (options & SSP_O_HCHK) { |
| 156 | chunk_printf(msg, ", reason: %s", get_check_status_description(s->check_status)); |
| 157 | |
| 158 | if (s->check_status >= HCHK_STATUS_L57DATA) |
| 159 | chunk_printf(msg, ", code: %d", s->check_code); |
| 160 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 161 | if (*s->check_desc) { |
| 162 | struct chunk src; |
| 163 | |
| 164 | chunk_printf(msg, ", info: \""); |
| 165 | |
| 166 | chunk_initlen(&src, s->check_desc, 0, strlen(s->check_desc)); |
| 167 | chunk_asciiencode(msg, &src, '"'); |
| 168 | |
| 169 | chunk_printf(msg, "\""); |
| 170 | } |
| 171 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 172 | if (s->check_duration >= 0) |
| 173 | chunk_printf(msg, ", check duration: %ldms", s->check_duration); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (options & SSP_O_STATUS) { |
| 177 | if (xferred) |
| 178 | chunk_printf(msg, ". %d active and %d backup servers left.%s" |
| 179 | " %d sessions active, %d requeued, %d remaining in queue.\n", |
| 180 | s->proxy->srv_act, s->proxy->srv_bck, |
| 181 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 182 | s->cur_sess, xferred, s->nbpend); |
| 183 | else |
| 184 | chunk_printf(msg, ". %d active and %d backup servers online.%s" |
| 185 | " %d sessions requeued, %d total in queue.\n", |
| 186 | s->proxy->srv_act, s->proxy->srv_bck, |
| 187 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 188 | xferred, s->nbpend); |
| 189 | } |
| 190 | } |
| 191 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 192 | /* |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 193 | * Set s->check_status, update s->check_duration and fill s->result with |
| 194 | * an adequate SRV_CHK_* value. |
| 195 | * |
| 196 | * Show information in logs about failed health check if server is UP |
| 197 | * or succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 198 | */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 199 | 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] | 200 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 201 | struct chunk msg; |
| 202 | |
| 203 | if (status == HCHK_STATUS_START) { |
| 204 | s->result = SRV_CHK_UNKNOWN; /* no result yet */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 205 | s->check_desc[0] = '\0'; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 206 | s->check_start = now; |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | if (!s->check_status) |
| 211 | return; |
| 212 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 213 | if (desc && *desc) { |
| 214 | strncpy(s->check_desc, desc, HCHK_DESC_LEN-1); |
| 215 | s->check_desc[HCHK_DESC_LEN-1] = '\0'; |
| 216 | } else |
| 217 | s->check_desc[0] = '\0'; |
| 218 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 219 | s->check_status = status; |
| 220 | if (check_statuses[status].result) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 221 | s->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 222 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 223 | if (status == HCHK_STATUS_HANA) |
| 224 | s->check_duration = -1; |
| 225 | else if (!tv_iszero(&s->check_start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 226 | /* set_server_check_status() may be called more than once */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 227 | s->check_duration = tv_ms_elapsed(&s->check_start, &now); |
| 228 | tv_zero(&s->check_start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 232 | (((s->health != 0) && (s->result & SRV_CHK_ERROR)) || |
| 233 | ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_RUNNING)) || |
| 234 | ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) || |
| 235 | (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) { |
| 236 | |
| 237 | int health, rise, fall, state; |
| 238 | |
| 239 | chunk_init(&msg, trash, sizeof(trash)); |
| 240 | |
| 241 | /* FIXME begin: calculate local version of the health/rise/fall/state */ |
| 242 | health = s->health; |
| 243 | rise = s->rise; |
| 244 | fall = s->fall; |
| 245 | state = s->state; |
| 246 | |
| 247 | if (s->result & SRV_CHK_ERROR) { |
| 248 | if (health > rise) { |
| 249 | health--; /* still good */ |
| 250 | } else { |
| 251 | if (health == rise) |
| 252 | state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
| 253 | |
| 254 | health = 0; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (s->result & SRV_CHK_RUNNING) { |
| 259 | if (health < rise + fall - 1) { |
| 260 | health++; /* was bad, stays for a while */ |
| 261 | |
| 262 | if (health == rise) |
| 263 | state |= SRV_RUNNING; |
| 264 | |
| 265 | if (health >= rise) |
| 266 | health = rise + fall - 1; /* OK now */ |
| 267 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 268 | |
| 269 | /* clear consecutive_errors if observing is enabled */ |
| 270 | if (s->onerror) |
| 271 | s->consecutive_errors = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 272 | } |
| 273 | /* FIXME end: calculate local version of the health/rise/fall/state */ |
| 274 | |
| 275 | chunk_printf(&msg, |
| 276 | "Health check for %sserver %s/%s %s%s", |
| 277 | s->state & SRV_BACKUP ? "backup " : "", |
| 278 | s->proxy->id, s->id, |
| 279 | (s->result & SRV_CHK_DISABLE)?"conditionally ":"", |
| 280 | (s->result & SRV_CHK_RUNNING)?"succeeded":"failed"); |
| 281 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 282 | server_status_printf(&msg, s, SSP_O_HCHK, -1); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 283 | |
| 284 | chunk_printf(&msg, ", status: %d/%d %s.\n", |
| 285 | (state & SRV_RUNNING) ? (health - rise + 1) : (health), |
| 286 | (state & SRV_RUNNING) ? (fall) : (rise), |
| 287 | (state & SRV_RUNNING)?"UP":"DOWN"); |
| 288 | |
| 289 | Warning("%s", trash); |
| 290 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 291 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 294 | /* sends a log message when a backend goes down, and also sets last |
| 295 | * change date. |
| 296 | */ |
| 297 | static void set_backend_down(struct proxy *be) |
| 298 | { |
| 299 | be->last_change = now.tv_sec; |
| 300 | be->down_trans++; |
| 301 | |
| 302 | Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id); |
| 303 | send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id); |
| 304 | } |
| 305 | |
| 306 | /* Redistribute pending connections when a server goes down. The number of |
| 307 | * connections redistributed is returned. |
| 308 | */ |
| 309 | static int redistribute_pending(struct server *s) |
| 310 | { |
| 311 | struct pendconn *pc, *pc_bck, *pc_end; |
| 312 | int xferred = 0; |
| 313 | |
| 314 | FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) { |
| 315 | struct session *sess = pc->sess; |
| 316 | if (sess->be->options & PR_O_REDISP) { |
| 317 | /* The REDISP option was specified. We will ignore |
| 318 | * cookie and force to balance or use the dispatcher. |
| 319 | */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 320 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 321 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 322 | sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 323 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 324 | pendconn_free(pc); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 325 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 326 | xferred++; |
| 327 | } |
| 328 | } |
| 329 | return xferred; |
| 330 | } |
| 331 | |
| 332 | /* Check for pending connections at the backend, and assign some of them to |
| 333 | * the server coming up. The server's weight is checked before being assigned |
| 334 | * connections it may not be able to handle. The total number of transferred |
| 335 | * connections is returned. |
| 336 | */ |
| 337 | static int check_for_pending(struct server *s) |
| 338 | { |
| 339 | int xferred; |
| 340 | |
| 341 | if (!s->eweight) |
| 342 | return 0; |
| 343 | |
| 344 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
| 345 | struct session *sess; |
| 346 | struct pendconn *p; |
| 347 | |
| 348 | p = pendconn_from_px(s->proxy); |
| 349 | if (!p) |
| 350 | break; |
| 351 | p->sess->srv = s; |
| 352 | sess = p->sess; |
| 353 | pendconn_free(p); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 354 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 355 | } |
| 356 | return xferred; |
| 357 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 358 | |
| 359 | /* Sets server <s> down, notifies by all available means, recounts the |
| 360 | * remaining servers on the proxy and transfers queued sessions whenever |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 361 | * possible to other servers. It automatically recomputes the number of |
| 362 | * servers, but not the map. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 363 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 364 | static void set_server_down(struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 366 | struct server *srv; |
| 367 | struct chunk msg; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | int xferred; |
| 369 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 370 | if (s->health == s->rise || s->tracked) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 371 | int srv_was_paused = s->state & SRV_GOINGDOWN; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 372 | |
| 373 | s->last_change = now.tv_sec; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 374 | s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 375 | s->proxy->lbprm.set_server_status_down(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 376 | |
| 377 | /* we might have sessions queued on this server and waiting for |
| 378 | * a connection. Those which are redispatchable will be queued |
| 379 | * to another server or to the proxy itself. |
| 380 | */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 381 | xferred = redistribute_pending(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 382 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 383 | chunk_init(&msg, trash, sizeof(trash)); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 384 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 385 | chunk_printf(&msg, |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 386 | "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "", |
| 387 | s->proxy->id, s->id); |
| 388 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 389 | server_status_printf(&msg, s, |
| 390 | (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS | |
| 391 | ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
| 392 | xferred); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 393 | |
| 394 | Warning("%s", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 395 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 396 | /* we don't send an alert if the server was previously paused */ |
| 397 | if (srv_was_paused) |
| 398 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 399 | else |
| 400 | send_log(s->proxy, LOG_ALERT, "%s", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 401 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 402 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 403 | set_backend_down(s->proxy); |
| 404 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 405 | s->counters.down_trans++; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 406 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 407 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 408 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 409 | set_server_down(srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 410 | } |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 411 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 412 | s->health = 0; /* failure */ |
| 413 | } |
| 414 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 415 | static void set_server_up(struct server *s) { |
| 416 | |
| 417 | struct server *srv; |
| 418 | struct chunk msg; |
| 419 | int xferred; |
| 420 | |
| 421 | if (s->health == s->rise || s->tracked) { |
| 422 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 423 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 424 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 425 | s->proxy->last_change = now.tv_sec; |
| 426 | } |
| 427 | |
| 428 | if (s->last_change < now.tv_sec) // ignore negative times |
| 429 | s->down_time += now.tv_sec - s->last_change; |
| 430 | |
| 431 | s->last_change = now.tv_sec; |
| 432 | s->state |= SRV_RUNNING; |
| 433 | |
| 434 | if (s->slowstart > 0) { |
| 435 | s->state |= SRV_WARMINGUP; |
| 436 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 437 | /* For dynamic algorithms, start at the first step of the weight, |
| 438 | * without multiplying by BE_WEIGHT_SCALE. |
| 439 | */ |
| 440 | s->eweight = s->uweight; |
| 441 | if (s->proxy->lbprm.update_server_eweight) |
| 442 | s->proxy->lbprm.update_server_eweight(s); |
| 443 | } |
| 444 | } |
| 445 | s->proxy->lbprm.set_server_status_up(s); |
| 446 | |
| 447 | /* check if we can handle some connections queued at the proxy. We |
| 448 | * will take as many as we can handle. |
| 449 | */ |
| 450 | xferred = check_for_pending(s); |
| 451 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 452 | chunk_init(&msg, trash, sizeof(trash)); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 453 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 454 | chunk_printf(&msg, |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 455 | "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "", |
| 456 | s->proxy->id, s->id); |
| 457 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 458 | server_status_printf(&msg, s, |
| 459 | (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS | |
| 460 | ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
| 461 | -1); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 462 | |
| 463 | Warning("%s", trash); |
| 464 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 465 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 466 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 467 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 468 | set_server_up(srv); |
| 469 | } |
| 470 | |
| 471 | if (s->health >= s->rise) |
| 472 | s->health = s->rise + s->fall - 1; /* OK now */ |
| 473 | |
| 474 | } |
| 475 | |
| 476 | static void set_server_disabled(struct server *s) { |
| 477 | |
| 478 | struct server *srv; |
| 479 | struct chunk msg; |
| 480 | int xferred; |
| 481 | |
| 482 | s->state |= SRV_GOINGDOWN; |
| 483 | s->proxy->lbprm.set_server_status_down(s); |
| 484 | |
| 485 | /* we might have sessions queued on this server and waiting for |
| 486 | * a connection. Those which are redispatchable will be queued |
| 487 | * to another server or to the proxy itself. |
| 488 | */ |
| 489 | xferred = redistribute_pending(s); |
| 490 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 491 | chunk_init(&msg, trash, sizeof(trash)); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 492 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 493 | chunk_printf(&msg, |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 494 | "Load-balancing on %sServer %s/%s is disabled", |
| 495 | s->state & SRV_BACKUP ? "Backup " : "", |
| 496 | s->proxy->id, s->id); |
| 497 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 498 | server_status_printf(&msg, s, |
| 499 | (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS | |
| 500 | ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
| 501 | -1); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 502 | |
| 503 | Warning("%s", trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 504 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 505 | |
| 506 | if (!s->proxy->srv_bck && !s->proxy->srv_act) |
| 507 | set_backend_down(s->proxy); |
| 508 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 509 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 510 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 511 | set_server_disabled(srv); |
| 512 | } |
| 513 | |
| 514 | static void set_server_enabled(struct server *s) { |
| 515 | |
| 516 | struct server *srv; |
| 517 | struct chunk msg; |
| 518 | int xferred; |
| 519 | |
| 520 | s->state &= ~SRV_GOINGDOWN; |
| 521 | s->proxy->lbprm.set_server_status_up(s); |
| 522 | |
| 523 | /* check if we can handle some connections queued at the proxy. We |
| 524 | * will take as many as we can handle. |
| 525 | */ |
| 526 | xferred = check_for_pending(s); |
| 527 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 528 | chunk_init(&msg, trash, sizeof(trash)); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 529 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 530 | chunk_printf(&msg, |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 531 | "Load-balancing on %sServer %s/%s is enabled again", |
| 532 | s->state & SRV_BACKUP ? "Backup " : "", |
| 533 | s->proxy->id, s->id); |
| 534 | |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 535 | server_status_printf(&msg, s, |
| 536 | (s->tracked?SSP_O_VIA:0) | SSP_O_STATUS | |
| 537 | ((!s->tracked && !(s->proxy->options2 & PR_O2_LOGHCHKS))?SSP_O_HCHK:0), |
| 538 | -1); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 539 | |
| 540 | Warning("%s", trash); |
| 541 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 542 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 543 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 544 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 545 | set_server_enabled(srv); |
| 546 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 547 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 548 | void health_adjust(struct server *s, short status) { |
| 549 | |
| 550 | int failed; |
| 551 | int expire; |
| 552 | |
| 553 | /* return now if observing nor health check is not enabled */ |
| 554 | if (!s->observe || !s->check) |
| 555 | return; |
| 556 | |
| 557 | if (s->observe >= HANA_OBS_SIZE) |
| 558 | return; |
| 559 | |
| 560 | if (status >= HCHK_STATUS_SIZE || !analyze_statuses[status].desc) |
| 561 | return; |
| 562 | |
| 563 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 564 | case 1: |
| 565 | failed = 1; |
| 566 | break; |
| 567 | |
| 568 | case 2: |
| 569 | failed = 0; |
| 570 | break; |
| 571 | |
| 572 | default: |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | if (!failed) { |
| 577 | /* good: clear consecutive_errors */ |
| 578 | s->consecutive_errors = 0; |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | s->consecutive_errors++; |
| 583 | |
| 584 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 585 | return; |
| 586 | |
| 587 | sprintf(trash, "Detected %d consecutive errors, last one was: %s", |
| 588 | s->consecutive_errors, get_analyze_status(status)); |
| 589 | |
| 590 | switch (s->onerror) { |
| 591 | case HANA_ONERR_FASTINTER: |
| 592 | /* force fastinter - nothing to do here as all modes force it */ |
| 593 | break; |
| 594 | |
| 595 | case HANA_ONERR_SUDDTH: |
| 596 | /* simulate a pre-fatal failed health check */ |
| 597 | if (s->health > s->rise) |
| 598 | s->health = s->rise + 1; |
| 599 | |
| 600 | /* no break - fall through */ |
| 601 | |
| 602 | case HANA_ONERR_FAILCHK: |
| 603 | /* simulate a failed health check */ |
| 604 | set_server_check_status(s, HCHK_STATUS_HANA, trash); |
| 605 | |
| 606 | if (s->health > s->rise) { |
| 607 | s->health--; /* still good */ |
| 608 | s->counters.failed_checks++; |
| 609 | } |
| 610 | else |
| 611 | set_server_down(s); |
| 612 | |
| 613 | break; |
| 614 | |
| 615 | case HANA_ONERR_MARKDWN: |
| 616 | /* mark server down */ |
| 617 | s->health = s->rise; |
| 618 | set_server_check_status(s, HCHK_STATUS_HANA, trash); |
| 619 | set_server_down(s); |
| 620 | |
| 621 | break; |
| 622 | |
| 623 | default: |
| 624 | /* write a warning? */ |
| 625 | break; |
| 626 | } |
| 627 | |
| 628 | s->consecutive_errors = 0; |
| 629 | s->counters.failed_hana++; |
| 630 | |
| 631 | if (s->fastinter) { |
| 632 | expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter)); |
| 633 | if (s->check->expire > expire) |
| 634 | s->check->expire = expire; |
| 635 | } |
| 636 | } |
| 637 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 638 | /* |
| 639 | * This function is used only for server health-checks. It handles |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 640 | * the connection acknowledgement. If the proxy requires L7 health-checks, |
| 641 | * it sends the request. In other cases, it calls set_server_check_status() |
| 642 | * to set s->check_status, s->check_duration and s->result. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 643 | * The function itself returns 0 if it needs some polling before being called |
| 644 | * again, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 645 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 646 | static int event_srv_chk_w(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 647 | { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 648 | __label__ out_wakeup, out_nowake, out_poll, out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 649 | struct task *t = fdtab[fd].owner; |
| 650 | struct server *s = t->context; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 651 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 652 | //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] | 653 | 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] | 654 | int skerr, err = errno; |
| 655 | socklen_t lskerr = sizeof(skerr); |
| 656 | |
| 657 | if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr) |
| 658 | err = skerr; |
| 659 | |
| 660 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err)); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 661 | goto out_error; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 662 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 663 | |
| 664 | /* here, we know that the connection is established */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 665 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 666 | if (!(s->result & SRV_CHK_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 667 | /* we don't want to mark 'UP' a server on which we detected an error earlier */ |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 668 | if ((s->proxy->options & PR_O_HTTP_CHK) || |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 669 | (s->proxy->options & PR_O_SSL3_CHK) || |
| 670 | (s->proxy->options & PR_O_SMTP_CHK)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 671 | int ret; |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 672 | /* 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] | 673 | * so we'll send the request, and won't wake the checker up now. |
| 674 | */ |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 675 | |
| 676 | if (s->proxy->options & PR_O_SSL3_CHK) { |
| 677 | /* SSL requires that we put Unix time in the request */ |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 678 | int gmt_time = htonl(date.tv_sec); |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 679 | memcpy(s->proxy->check_req + 11, &gmt_time, 4); |
| 680 | } |
| 681 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 682 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 683 | if (ret == s->proxy->check_len) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 684 | /* we allow up to <timeout.check> if nonzero for a responce */ |
Willy Tarreau | 7cd9d94 | 2008-12-21 13:00:41 +0100 | [diff] [blame] | 685 | if (s->proxy->timeout.check) |
| 686 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 687 | EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 688 | goto out_nowake; |
| 689 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 690 | else if (ret == 0 || errno == EAGAIN) |
| 691 | goto out_poll; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 692 | else { |
| 693 | switch (errno) { |
| 694 | case ECONNREFUSED: |
| 695 | case ENETUNREACH: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 696 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 697 | break; |
| 698 | |
| 699 | default: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 700 | set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 703 | goto out_error; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 704 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 705 | } |
| 706 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 707 | /* We have no data to send to check the connection, and |
| 708 | * getsockopt() will not inform us whether the connection |
| 709 | * is still pending. So we'll reuse connect() to check the |
| 710 | * state of the socket. This has the advantage of givig us |
| 711 | * the following info : |
| 712 | * - error |
| 713 | * - connecting (EALREADY, EINPROGRESS) |
| 714 | * - connected (EISCONN, 0) |
| 715 | */ |
| 716 | |
| 717 | struct sockaddr_in sa; |
| 718 | |
| 719 | sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr; |
| 720 | sa.sin_port = htons(s->check_port); |
| 721 | |
| 722 | if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0) |
| 723 | errno = 0; |
| 724 | |
| 725 | if (errno == EALREADY || errno == EINPROGRESS) |
| 726 | goto out_poll; |
| 727 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 728 | if (errno && errno != EISCONN) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 729 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno)); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 730 | goto out_error; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 731 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 732 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 733 | /* good TCP connection is enough */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 734 | set_server_check_status(s, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 735 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 736 | } |
| 737 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 738 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 739 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 740 | out_nowake: |
| 741 | EV_FD_CLR(fd, DIR_WR); /* nothing more to write */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 742 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 743 | return 1; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 744 | out_poll: |
| 745 | /* The connection is still pending. We'll have to poll it |
| 746 | * before attempting to go further. */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 747 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 748 | return 0; |
| 749 | out_error: |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 750 | fdtab[fd].state = FD_STERROR; |
| 751 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | |
| 755 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 756 | * This function is used only for server health-checks. It handles the server's |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 757 | * reply to an HTTP request or SSL HELLO. It calls set_server_check_status() to |
| 758 | * update s->check_status, s->check_duration and s->result. |
| 759 | |
| 760 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 761 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 762 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 763 | * response to an SSL HELLO (the principle is that this is enough to |
| 764 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 765 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 766 | * etc. |
| 767 | * |
| 768 | * The function returns 0 if it needs to be called again after some polling, |
| 769 | * otherwise non-zero.. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 770 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 771 | static int event_srv_chk_r(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 772 | { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 773 | __label__ out_wakeup; |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 774 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 775 | struct task *t = fdtab[fd].owner; |
| 776 | struct server *s = t->context; |
| 777 | int skerr; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 778 | char *desc; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 779 | socklen_t lskerr = sizeof(skerr); |
| 780 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 781 | len = -1; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 782 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 783 | if (unlikely((s->result & SRV_CHK_ERROR) || |
| 784 | (fdtab[fd].state == FD_STERROR) || |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 785 | (fdtab[fd].ev & FD_POLL_ERR) || |
| 786 | (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) || |
| 787 | (skerr != 0))) { |
| 788 | /* 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] | 789 | |
| 790 | if (!(s->result & SRV_CHK_ERROR)) |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 791 | set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 792 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 793 | goto out_wakeup; |
| 794 | } |
| 795 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 796 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 797 | * but the connection was closed on the remote end. Fortunately, recv still |
| 798 | * works correctly and we don't need to do the getsockopt() on linux. |
| 799 | */ |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 800 | len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 801 | if (unlikely(len < 0 && errno == EAGAIN)) { |
| 802 | /* we want some polling to happen first */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 803 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 804 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 805 | } |
| 806 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 807 | if (len < sizeof(trash)) |
| 808 | trash[len] = '\0'; |
| 809 | else |
| 810 | trash[len-1] = '\0'; |
| 811 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 812 | /* Note: the response will only be accepted if read at once */ |
| 813 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 814 | /* Check if the server speaks HTTP 1.X */ |
| 815 | if ((len < strlen("HTTP/1.0 000\r")) || |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 816 | (memcmp(trash, "HTTP/1.", 7) != 0 || |
| 817 | (trash[12] != ' ' && trash[12] != '\r')) || |
| 818 | !isdigit(trash[9]) || !isdigit(trash[10]) || !isdigit(trash[11])) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 819 | |
| 820 | cut_crlf(trash); |
| 821 | set_server_check_status(s, HCHK_STATUS_L7RSP, trash); |
| 822 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 823 | goto out_wakeup; |
| 824 | } |
| 825 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 826 | s->check_code = str2uic(&trash[9]); |
| 827 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 828 | desc = ltrim(&trash[12], ' '); |
| 829 | cut_crlf(desc); |
| 830 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 831 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 832 | if (trash[9] == '2' || trash[9] == '3') |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 833 | set_server_check_status(s, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 834 | else if ((s->proxy->options & PR_O_DISABLE404) && |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 835 | (s->state & SRV_RUNNING) && |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 836 | (s->check_code == 404)) |
| 837 | /* 404 may be accepted as "stopping" only if the server was up */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 838 | set_server_check_status(s, HCHK_STATUS_L7OKCD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 839 | else |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 840 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 841 | } |
| 842 | else if (s->proxy->options & PR_O_SSL3_CHK) { |
| 843 | /* Check for SSLv3 alert or handshake */ |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 844 | if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16)) |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 845 | set_server_check_status(s, HCHK_STATUS_L6OK, NULL); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 846 | else |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 847 | set_server_check_status(s, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 848 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 849 | else if (s->proxy->options & PR_O_SMTP_CHK) { |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 850 | /* Check if the server speaks SMTP */ |
| 851 | if ((len < strlen("000\r")) || |
| 852 | (trash[3] != ' ' && trash[3] != '\r') || |
| 853 | !isdigit(trash[0]) || !isdigit(trash[1]) || !isdigit(trash[2])) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 854 | |
| 855 | cut_crlf(trash); |
| 856 | set_server_check_status(s, HCHK_STATUS_L7RSP, trash); |
| 857 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 858 | goto out_wakeup; |
| 859 | } |
| 860 | |
| 861 | s->check_code = str2uic(&trash[0]); |
| 862 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 863 | desc = ltrim(&trash[3], ' '); |
| 864 | cut_crlf(desc); |
| 865 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 866 | /* Check for SMTP code 2xx (should be 250) */ |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 867 | if (trash[0] == '2') |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 868 | set_server_check_status(s, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 869 | else |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 870 | set_server_check_status(s, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 871 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 872 | else { |
| 873 | /* other checks are valid if the connection succeeded anyway */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 874 | set_server_check_status(s, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 875 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 876 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 877 | out_wakeup: |
| 878 | if (s->result & SRV_CHK_ERROR) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 879 | fdtab[fd].state = FD_STERROR; |
| 880 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 881 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 882 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 883 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 884 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
| 888 | * manages a server health-check. Returns |
| 889 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 890 | */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 891 | struct task *process_chk(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 892 | { |
Willy Tarreau | e383880 | 2009-03-21 18:58:32 +0100 | [diff] [blame] | 893 | int attempts = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 894 | struct server *s = t->context; |
| 895 | struct sockaddr_in sa; |
| 896 | int fd; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 897 | int rv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 898 | |
| 899 | //fprintf(stderr, "process_chk: task=%p\n", t); |
| 900 | |
| 901 | new_chk: |
Willy Tarreau | e383880 | 2009-03-21 18:58:32 +0100 | [diff] [blame] | 902 | if (attempts++ > 0) { |
| 903 | /* we always fail to create a server, let's stop insisting... */ |
| 904 | while (tick_is_expired(t->expire, now_ms)) |
| 905 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
| 906 | return t; |
| 907 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 908 | fd = s->curfd; |
| 909 | if (fd < 0) { /* no check currently running */ |
| 910 | //fprintf(stderr, "process_chk: 2\n"); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 911 | if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */ |
| 912 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 913 | |
| 914 | /* we don't send any health-checks when the proxy is stopped or when |
| 915 | * the server should not be checked. |
| 916 | */ |
| 917 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 918 | while (tick_is_expired(t->expire, now_ms)) |
| 919 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 920 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* we'll initiate a new check */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 924 | set_server_check_status(s, HCHK_STATUS_START, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 925 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
| 926 | if ((fd < global.maxsock) && |
| 927 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 928 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 929 | //fprintf(stderr, "process_chk: 3\n"); |
| 930 | |
Willy Tarreau | 9edd161 | 2007-10-18 18:07:48 +0200 | [diff] [blame] | 931 | if (s->proxy->options & PR_O_TCP_NOLING) { |
| 932 | /* We don't want to useless data */ |
| 933 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 934 | } |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 935 | |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 936 | if (s->check_addr.sin_addr.s_addr) |
| 937 | /* we'll connect to the check addr specified on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 938 | sa = s->check_addr; |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 939 | else |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 940 | /* we'll connect to the addr on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 941 | sa = s->addr; |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 942 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 943 | /* we'll connect to the check port on the server */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 944 | sa.sin_port = htons(s->check_port); |
| 945 | |
| 946 | /* allow specific binding : |
| 947 | * - server-specific at first |
| 948 | * - proxy-specific next |
| 949 | */ |
| 950 | if (s->state & SRV_BIND_SRC) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 951 | struct sockaddr_in *remote = NULL; |
| 952 | int ret, flags = 0; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 953 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 954 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 955 | if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) { |
| 956 | remote = (struct sockaddr_in *)&s->tproxy_addr; |
| 957 | flags = 3; |
| 958 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 959 | #endif |
Willy Tarreau | c76721d | 2009-02-04 20:20:58 +0100 | [diff] [blame] | 960 | #ifdef SO_BINDTODEVICE |
| 961 | /* Note: this might fail if not CAP_NET_RAW */ |
| 962 | if (s->iface_name) |
| 963 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 964 | s->iface_name, s->iface_len + 1); |
Willy Tarreau | c76721d | 2009-02-04 20:20:58 +0100 | [diff] [blame] | 965 | #endif |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 966 | if (s->sport_range) { |
| 967 | int bind_attempts = 10; /* should be more than enough to find a spare port */ |
| 968 | struct sockaddr_in src; |
| 969 | |
| 970 | ret = 1; |
| 971 | src = s->source_addr; |
| 972 | |
| 973 | do { |
| 974 | /* note: in case of retry, we may have to release a previously |
| 975 | * allocated port, hence this loop's construct. |
| 976 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 977 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 978 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 979 | |
| 980 | if (!bind_attempts) |
| 981 | break; |
| 982 | bind_attempts--; |
| 983 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 984 | fdinfo[fd].local_port = port_range_alloc_port(s->sport_range); |
| 985 | if (!fdinfo[fd].local_port) |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 986 | break; |
| 987 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 988 | fdinfo[fd].port_range = s->sport_range; |
| 989 | src.sin_port = htons(fdinfo[fd].local_port); |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 990 | |
| 991 | ret = tcpv4_bind_socket(fd, flags, &src, remote); |
| 992 | } while (ret != 0); /* binding NOK */ |
| 993 | } |
| 994 | else { |
| 995 | ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote); |
| 996 | } |
| 997 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 998 | if (ret) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 999 | set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1000 | switch (ret) { |
| 1001 | case 1: |
| 1002 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 1003 | s->proxy->id, s->id); |
| 1004 | break; |
| 1005 | case 2: |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1006 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 1007 | s->proxy->id, s->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1008 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1009 | } |
| 1010 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1011 | } |
| 1012 | else if (s->proxy->options & PR_O_BIND_SRC) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1013 | struct sockaddr_in *remote = NULL; |
| 1014 | int ret, flags = 0; |
| 1015 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1016 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1017 | if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1018 | remote = (struct sockaddr_in *)&s->proxy->tproxy_addr; |
| 1019 | flags = 3; |
| 1020 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1021 | #endif |
Willy Tarreau | d53f96b | 2009-02-04 18:46:54 +0100 | [diff] [blame] | 1022 | #ifdef SO_BINDTODEVICE |
| 1023 | /* Note: this might fail if not CAP_NET_RAW */ |
| 1024 | if (s->proxy->iface_name) |
| 1025 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 1026 | s->proxy->iface_name, s->proxy->iface_len + 1); |
Willy Tarreau | d53f96b | 2009-02-04 18:46:54 +0100 | [diff] [blame] | 1027 | #endif |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1028 | ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote); |
| 1029 | if (ret) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1030 | set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1031 | switch (ret) { |
| 1032 | case 1: |
| 1033 | Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n", |
| 1034 | proxy_type_str(s->proxy), s->proxy->id); |
| 1035 | break; |
| 1036 | case 2: |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 1037 | Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n", |
| 1038 | proxy_type_str(s->proxy), s->proxy->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1039 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 1040 | } |
| 1041 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1044 | if (s->result == SRV_CHK_UNKNOWN) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 1045 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 1046 | /* disabling tcp quick ack now allows |
| 1047 | * the request to leave the machine with |
| 1048 | * the first ACK. |
| 1049 | */ |
| 1050 | if (s->proxy->options2 & PR_O2_SMARTCON) |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 1051 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 1052 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1053 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
| 1054 | /* OK, connection in progress or established */ |
| 1055 | |
| 1056 | //fprintf(stderr, "process_chk: 4\n"); |
| 1057 | |
| 1058 | 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] | 1059 | fd_insert(fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1060 | fdtab[fd].owner = t; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 1061 | fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r; |
| 1062 | fdtab[fd].cb[DIR_RD].b = NULL; |
| 1063 | fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w; |
| 1064 | fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 1065 | fdinfo[fd].peeraddr = (struct sockaddr *)&sa; |
| 1066 | fdinfo[fd].peerlen = sizeof(sa); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1067 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 1068 | fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY; |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1069 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1070 | #ifdef DEBUG_FULL |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1071 | assert (!EV_FD_ISSET(fd, DIR_RD)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1072 | #endif |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1073 | //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 1074 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1075 | * to establish but only when timeout.check is set |
| 1076 | * as it may be to short for a full check otherwise |
| 1077 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1078 | t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1079 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1080 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1081 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1082 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | 6054819 | 2008-02-17 11:34:10 +0100 | [diff] [blame] | 1083 | } |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1084 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1085 | } |
| 1086 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 1087 | /* a real error */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1088 | |
| 1089 | switch (errno) { |
| 1090 | /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */ |
| 1091 | case ECONNREFUSED: |
| 1092 | case ENETUNREACH: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1093 | set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1094 | break; |
| 1095 | |
| 1096 | default: |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1097 | set_server_check_status(s, HCHK_STATUS_SOCKERR, strerror(errno)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1098 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | } |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 1102 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 1103 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1104 | close(fd); /* socket creation error */ |
| 1105 | } |
| 1106 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1107 | if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1108 | //fprintf(stderr, "process_chk: 6\n"); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1109 | while (tick_is_expired(t->expire, now_ms)) |
| 1110 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1111 | goto new_chk; /* may be we should initialize a new check */ |
| 1112 | } |
| 1113 | |
| 1114 | /* here, we have seen a failure */ |
| 1115 | if (s->health > s->rise) { |
| 1116 | s->health--; /* still good */ |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1117 | s->counters.failed_checks++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1118 | } |
| 1119 | else |
| 1120 | set_server_down(s); |
| 1121 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1122 | //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 1123 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1124 | * to establish but only when timeout.check is set |
| 1125 | * as it may be to short for a full check otherwise |
| 1126 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1127 | while (tick_is_expired(t->expire, now_ms)) { |
| 1128 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1129 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1130 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1131 | t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1132 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1133 | if (s->proxy->timeout.check) |
| 1134 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1135 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1136 | goto new_chk; |
| 1137 | } |
| 1138 | else { |
| 1139 | //fprintf(stderr, "process_chk: 8\n"); |
| 1140 | /* there was a test running */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1141 | if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1142 | //fprintf(stderr, "process_chk: 9\n"); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1143 | |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 1144 | if (s->state & SRV_WARMINGUP) { |
| 1145 | if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) { |
| 1146 | s->state &= ~SRV_WARMINGUP; |
| 1147 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) |
| 1148 | s->eweight = s->uweight * BE_WEIGHT_SCALE; |
| 1149 | if (s->proxy->lbprm.update_server_eweight) |
| 1150 | s->proxy->lbprm.update_server_eweight(s); |
| 1151 | } |
| 1152 | else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 1153 | /* for dynamic algorithms, let's update the weight */ |
Willy Tarreau | 5542af6 | 2007-12-03 02:04:00 +0100 | [diff] [blame] | 1154 | s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) + |
| 1155 | s->slowstart - 1) / s->slowstart; |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 1156 | s->eweight *= s->uweight; |
| 1157 | if (s->proxy->lbprm.update_server_eweight) |
| 1158 | s->proxy->lbprm.update_server_eweight(s); |
| 1159 | } |
| 1160 | /* probably that we can refill this server with a bit more connections */ |
| 1161 | check_for_pending(s); |
| 1162 | } |
| 1163 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1164 | /* we may have to add/remove this server from the LB group */ |
| 1165 | if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) { |
| 1166 | if ((s->state & SRV_GOINGDOWN) && |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 1167 | ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING)) |
| 1168 | set_server_enabled(s); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1169 | else if (!(s->state & SRV_GOINGDOWN) && |
| 1170 | ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) == |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 1171 | (SRV_CHK_RUNNING | SRV_CHK_DISABLE))) |
| 1172 | set_server_disabled(s); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1175 | if (s->health < s->rise + s->fall - 1) { |
| 1176 | s->health++; /* was bad, stays for a while */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1177 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 1178 | set_server_up(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1179 | } |
| 1180 | s->curfd = -1; /* no check running anymore */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1181 | fd_delete(fd); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 1182 | |
| 1183 | rv = 0; |
| 1184 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1185 | rv = srv_getinter(s) * global.spread_checks / 100; |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 1186 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1187 | //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 1188 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1189 | 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] | 1190 | goto new_chk; |
| 1191 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1192 | 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] | 1193 | if (!(s->result & SRV_CHK_ERROR)) { |
| 1194 | if (!EV_FD_ISSET(fd, DIR_RD)) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1195 | set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1196 | } else { |
| 1197 | if (s->proxy->options & PR_O_SSL3_CHK) |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1198 | set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1199 | else /* HTTP, SMTP */ |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 1200 | set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1204 | //fprintf(stderr, "process_chk: 10\n"); |
| 1205 | /* failure or timeout detected */ |
| 1206 | if (s->health > s->rise) { |
| 1207 | s->health--; /* still good */ |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1208 | s->counters.failed_checks++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1209 | } |
| 1210 | else |
| 1211 | set_server_down(s); |
| 1212 | s->curfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1213 | fd_delete(fd); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1214 | |
| 1215 | rv = 0; |
| 1216 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1217 | rv = srv_getinter(s) * global.spread_checks / 100; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1218 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1219 | //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1220 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1221 | 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] | 1222 | goto new_chk; |
| 1223 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1224 | /* 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] | 1225 | } |
| 1226 | //fprintf(stderr, "process_chk: 11\n"); |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1227 | s->result = SRV_CHK_UNKNOWN; |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1228 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1229 | } |
| 1230 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1231 | /* |
| 1232 | * Start health-check. |
| 1233 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 1234 | */ |
| 1235 | int start_checks() { |
| 1236 | |
| 1237 | struct proxy *px; |
| 1238 | struct server *s; |
| 1239 | struct task *t; |
| 1240 | int nbchk=0, mininter=0, srvpos=0; |
| 1241 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1242 | /* 1- count the checkers to run simultaneously. |
| 1243 | * We also determine the minimum interval among all of those which |
| 1244 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1245 | * will be used to spread their start-up date. Those which have |
| 1246 | * a shorter interval will start independantly and will not dictate |
| 1247 | * too short an interval for all others. |
| 1248 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1249 | for (px = proxy; px; px = px->next) { |
| 1250 | for (s = px->srv; s; s = s->next) { |
| 1251 | if (!(s->state & SRV_CHECKED)) |
| 1252 | continue; |
| 1253 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1254 | if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) && |
| 1255 | (!mininter || mininter > srv_getinter(s))) |
| 1256 | mininter = srv_getinter(s); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1257 | |
| 1258 | nbchk++; |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | if (!nbchk) |
| 1263 | return 0; |
| 1264 | |
| 1265 | srand((unsigned)time(NULL)); |
| 1266 | |
| 1267 | /* |
| 1268 | * 2- start them as far as possible from each others. For this, we will |
| 1269 | * start them after their interval set to the min interval divided by |
| 1270 | * the number of servers, weighted by the server's position in the list. |
| 1271 | */ |
| 1272 | for (px = proxy; px; px = px->next) { |
| 1273 | for (s = px->srv; s; s = s->next) { |
| 1274 | if (!(s->state & SRV_CHECKED)) |
| 1275 | continue; |
| 1276 | |
Willy Tarreau | a461318 | 2009-03-21 18:13:21 +0100 | [diff] [blame] | 1277 | if ((t = task_new()) == NULL) { |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1278 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1279 | return -1; |
| 1280 | } |
| 1281 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 1282 | s->check = t; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1283 | t->process = process_chk; |
| 1284 | t->context = s; |
| 1285 | |
| 1286 | /* check this every ms */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1287 | t->expire = tick_add(now_ms, |
| 1288 | MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ? |
| 1289 | mininter : srv_getinter(s)) * srvpos / nbchk)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1290 | s->check_start = now; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1291 | task_queue(t); |
| 1292 | |
| 1293 | srvpos++; |
| 1294 | } |
| 1295 | } |
| 1296 | return 0; |
| 1297 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1298 | |
| 1299 | /* |
| 1300 | * Local variables: |
| 1301 | * c-indent-level: 8 |
| 1302 | * c-basic-offset: 8 |
| 1303 | * End: |
| 1304 | */ |