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