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