Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Health-checks functions. |
| 3 | * |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 5 | * Copyright 2007-2008 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> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 19 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/compat.h> |
| 27 | #include <common/config.h> |
| 28 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 29 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
| 32 | #include <types/global.h> |
| 33 | #include <types/polling.h> |
| 34 | #include <types/proxy.h> |
| 35 | #include <types/session.h> |
| 36 | |
| 37 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 38 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | #include <proto/fd.h> |
| 40 | #include <proto/log.h> |
| 41 | #include <proto/queue.h> |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 42 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 43 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 44 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/server.h> |
| 46 | #include <proto/task.h> |
| 47 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 48 | /* sends a log message when a backend goes down, and also sets last |
| 49 | * change date. |
| 50 | */ |
| 51 | static void set_backend_down(struct proxy *be) |
| 52 | { |
| 53 | be->last_change = now.tv_sec; |
| 54 | be->down_trans++; |
| 55 | |
| 56 | Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id); |
| 57 | send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id); |
| 58 | } |
| 59 | |
| 60 | /* Redistribute pending connections when a server goes down. The number of |
| 61 | * connections redistributed is returned. |
| 62 | */ |
| 63 | static int redistribute_pending(struct server *s) |
| 64 | { |
| 65 | struct pendconn *pc, *pc_bck, *pc_end; |
| 66 | int xferred = 0; |
| 67 | |
| 68 | FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) { |
| 69 | struct session *sess = pc->sess; |
| 70 | if (sess->be->options & PR_O_REDISP) { |
| 71 | /* The REDISP option was specified. We will ignore |
| 72 | * cookie and force to balance or use the dispatcher. |
| 73 | */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 74 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 75 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 76 | sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 78 | pendconn_free(pc); |
| 79 | task_wakeup(sess->task); |
| 80 | xferred++; |
| 81 | } |
| 82 | } |
| 83 | return xferred; |
| 84 | } |
| 85 | |
| 86 | /* Check for pending connections at the backend, and assign some of them to |
| 87 | * the server coming up. The server's weight is checked before being assigned |
| 88 | * connections it may not be able to handle. The total number of transferred |
| 89 | * connections is returned. |
| 90 | */ |
| 91 | static int check_for_pending(struct server *s) |
| 92 | { |
| 93 | int xferred; |
| 94 | |
| 95 | if (!s->eweight) |
| 96 | return 0; |
| 97 | |
| 98 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
| 99 | struct session *sess; |
| 100 | struct pendconn *p; |
| 101 | |
| 102 | p = pendconn_from_px(s->proxy); |
| 103 | if (!p) |
| 104 | break; |
| 105 | p->sess->srv = s; |
| 106 | sess = p->sess; |
| 107 | pendconn_free(p); |
| 108 | task_wakeup(sess->task); |
| 109 | } |
| 110 | return xferred; |
| 111 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | |
| 113 | /* Sets server <s> down, notifies by all available means, recounts the |
| 114 | * remaining servers on the proxy and transfers queued sessions whenever |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 115 | * possible to other servers. It automatically recomputes the number of |
| 116 | * servers, but not the map. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 118 | static void set_server_down(struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 120 | struct server *srv; |
| 121 | struct chunk msg; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | int xferred; |
| 123 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 124 | if (s->health == s->rise || s->tracked) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 125 | int srv_was_paused = s->state & SRV_GOINGDOWN; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 126 | |
| 127 | s->last_change = now.tv_sec; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 128 | s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 129 | s->proxy->lbprm.set_server_status_down(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | |
| 131 | /* we might have sessions queued on this server and waiting for |
| 132 | * a connection. Those which are redispatchable will be queued |
| 133 | * to another server or to the proxy itself. |
| 134 | */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 135 | xferred = redistribute_pending(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 136 | |
| 137 | msg.len = 0; |
| 138 | msg.str = trash; |
| 139 | |
| 140 | chunk_printf(&msg, sizeof(trash), |
| 141 | "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "", |
| 142 | s->proxy->id, s->id); |
| 143 | |
| 144 | if (s->tracked) |
| 145 | chunk_printf(&msg, sizeof(trash), " via %s/%s", |
| 146 | s->tracked->proxy->id, s->tracked->id); |
| 147 | |
| 148 | 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] | 149 | " %d sessions active, %d requeued, %d remaining in queue.\n", |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 150 | s->proxy->srv_act, s->proxy->srv_bck, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 151 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 152 | s->cur_sess, xferred, s->nbpend); |
| 153 | |
| 154 | Warning("%s", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 155 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 156 | /* we don't send an alert if the server was previously paused */ |
| 157 | if (srv_was_paused) |
| 158 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 159 | else |
| 160 | send_log(s->proxy, LOG_ALERT, "%s", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 162 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 163 | set_backend_down(s->proxy); |
| 164 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 165 | s->down_trans++; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 166 | |
| 167 | if (s->state && SRV_CHECKED) |
| 168 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 169 | set_server_down(srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | } |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 171 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 172 | s->health = 0; /* failure */ |
| 173 | } |
| 174 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 175 | static void set_server_up(struct server *s) { |
| 176 | |
| 177 | struct server *srv; |
| 178 | struct chunk msg; |
| 179 | int xferred; |
| 180 | |
| 181 | if (s->health == s->rise || s->tracked) { |
| 182 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 183 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 184 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 185 | s->proxy->last_change = now.tv_sec; |
| 186 | } |
| 187 | |
| 188 | if (s->last_change < now.tv_sec) // ignore negative times |
| 189 | s->down_time += now.tv_sec - s->last_change; |
| 190 | |
| 191 | s->last_change = now.tv_sec; |
| 192 | s->state |= SRV_RUNNING; |
| 193 | |
| 194 | if (s->slowstart > 0) { |
| 195 | s->state |= SRV_WARMINGUP; |
| 196 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 197 | /* For dynamic algorithms, start at the first step of the weight, |
| 198 | * without multiplying by BE_WEIGHT_SCALE. |
| 199 | */ |
| 200 | s->eweight = s->uweight; |
| 201 | if (s->proxy->lbprm.update_server_eweight) |
| 202 | s->proxy->lbprm.update_server_eweight(s); |
| 203 | } |
| 204 | } |
| 205 | s->proxy->lbprm.set_server_status_up(s); |
| 206 | |
| 207 | /* check if we can handle some connections queued at the proxy. We |
| 208 | * will take as many as we can handle. |
| 209 | */ |
| 210 | xferred = check_for_pending(s); |
| 211 | |
| 212 | msg.len = 0; |
| 213 | msg.str = trash; |
| 214 | |
| 215 | chunk_printf(&msg, sizeof(trash), |
| 216 | "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "", |
| 217 | s->proxy->id, s->id); |
| 218 | |
| 219 | if (s->tracked) |
| 220 | chunk_printf(&msg, sizeof(trash), " via %s/%s", |
| 221 | s->tracked->proxy->id, s->tracked->id); |
| 222 | |
| 223 | chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s" |
| 224 | " %d sessions requeued, %d total in queue.\n", |
| 225 | s->proxy->srv_act, s->proxy->srv_bck, |
| 226 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 227 | s->cur_sess, xferred, s->nbpend); |
| 228 | |
| 229 | Warning("%s", trash); |
| 230 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 231 | |
| 232 | if (s->state && SRV_CHECKED) |
| 233 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 234 | set_server_up(srv); |
| 235 | } |
| 236 | |
| 237 | if (s->health >= s->rise) |
| 238 | s->health = s->rise + s->fall - 1; /* OK now */ |
| 239 | |
| 240 | } |
| 241 | |
| 242 | static void set_server_disabled(struct server *s) { |
| 243 | |
| 244 | struct server *srv; |
| 245 | struct chunk msg; |
| 246 | int xferred; |
| 247 | |
| 248 | s->state |= SRV_GOINGDOWN; |
| 249 | s->proxy->lbprm.set_server_status_down(s); |
| 250 | |
| 251 | /* we might have sessions queued on this server and waiting for |
| 252 | * a connection. Those which are redispatchable will be queued |
| 253 | * to another server or to the proxy itself. |
| 254 | */ |
| 255 | xferred = redistribute_pending(s); |
| 256 | |
| 257 | msg.len = 0; |
| 258 | msg.str = trash; |
| 259 | |
| 260 | chunk_printf(&msg, sizeof(trash), |
| 261 | "Load-balancing on %sServer %s/%s is disabled", |
| 262 | s->state & SRV_BACKUP ? "Backup " : "", |
| 263 | s->proxy->id, s->id); |
| 264 | |
| 265 | if (s->tracked) |
| 266 | chunk_printf(&msg, sizeof(trash), " via %s/%s", |
| 267 | s->tracked->proxy->id, s->tracked->id); |
| 268 | |
| 269 | |
| 270 | chunk_printf(&msg, sizeof(trash),". %d active and %d backup servers online.%s" |
| 271 | " %d sessions requeued, %d total in queue.\n", |
| 272 | s->proxy->srv_act, s->proxy->srv_bck, |
| 273 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 274 | xferred, s->nbpend); |
| 275 | |
| 276 | Warning("%s", trash); |
| 277 | |
| 278 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 279 | |
| 280 | if (!s->proxy->srv_bck && !s->proxy->srv_act) |
| 281 | set_backend_down(s->proxy); |
| 282 | |
| 283 | if (s->state && SRV_CHECKED) |
| 284 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 285 | set_server_disabled(srv); |
| 286 | } |
| 287 | |
| 288 | static void set_server_enabled(struct server *s) { |
| 289 | |
| 290 | struct server *srv; |
| 291 | struct chunk msg; |
| 292 | int xferred; |
| 293 | |
| 294 | s->state &= ~SRV_GOINGDOWN; |
| 295 | s->proxy->lbprm.set_server_status_up(s); |
| 296 | |
| 297 | /* check if we can handle some connections queued at the proxy. We |
| 298 | * will take as many as we can handle. |
| 299 | */ |
| 300 | xferred = check_for_pending(s); |
| 301 | |
| 302 | msg.len = 0; |
| 303 | msg.str = trash; |
| 304 | |
| 305 | chunk_printf(&msg, sizeof(trash), |
| 306 | "Load-balancing on %sServer %s/%s is enabled again", |
| 307 | s->state & SRV_BACKUP ? "Backup " : "", |
| 308 | s->proxy->id, s->id); |
| 309 | |
| 310 | if (s->tracked) |
| 311 | chunk_printf(&msg, sizeof(trash), " via %s/%s", |
| 312 | s->tracked->proxy->id, s->tracked->id); |
| 313 | |
| 314 | chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s" |
| 315 | " %d sessions requeued, %d total in queue.\n", |
| 316 | s->proxy->srv_act, s->proxy->srv_bck, |
| 317 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 318 | xferred, s->nbpend); |
| 319 | |
| 320 | Warning("%s", trash); |
| 321 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 322 | |
| 323 | if (s->state && SRV_CHECKED) |
| 324 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
| 325 | set_server_enabled(srv); |
| 326 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * This function is used only for server health-checks. It handles |
| 330 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 331 | * 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] | 332 | * The function itself returns 0 if it needs some polling before being called |
| 333 | * again, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 335 | static int event_srv_chk_w(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 336 | { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 337 | __label__ out_wakeup, out_nowake, out_poll, out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 338 | struct task *t = fdtab[fd].owner; |
| 339 | struct server *s = t->context; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 340 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 341 | //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state)); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 342 | if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) |
| 343 | goto out_error; |
| 344 | |
| 345 | /* here, we know that the connection is established */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 346 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 347 | if (!(s->result & SRV_CHK_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 348 | /* 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] | 349 | if ((s->proxy->options & PR_O_HTTP_CHK) || |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 350 | (s->proxy->options & PR_O_SSL3_CHK) || |
| 351 | (s->proxy->options & PR_O_SMTP_CHK)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | int ret; |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 353 | /* 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] | 354 | * so we'll send the request, and won't wake the checker up now. |
| 355 | */ |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 356 | |
| 357 | if (s->proxy->options & PR_O_SSL3_CHK) { |
| 358 | /* SSL requires that we put Unix time in the request */ |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 359 | int gmt_time = htonl(date.tv_sec); |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 360 | memcpy(s->proxy->check_req + 11, &gmt_time, 4); |
| 361 | } |
| 362 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 363 | #ifndef MSG_NOSIGNAL |
| 364 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT); |
| 365 | #else |
| 366 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 367 | #endif |
| 368 | if (ret == s->proxy->check_len) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 369 | /* we allow up to <timeout.check> if nonzero for a responce */ |
| 370 | //fprintf(stderr, "event_srv_chk_w, ms=%lu\n", __tv_to_ms(&s->proxy->timeout.check)); |
| 371 | tv_add_ifset(&t->expire, &now, &s->proxy->timeout.check); |
| 372 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 373 | EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 374 | goto out_nowake; |
| 375 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 376 | else if (ret == 0 || errno == EAGAIN) |
| 377 | goto out_poll; |
| 378 | else |
| 379 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 380 | } |
| 381 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 382 | /* We have no data to send to check the connection, and |
| 383 | * getsockopt() will not inform us whether the connection |
| 384 | * is still pending. So we'll reuse connect() to check the |
| 385 | * state of the socket. This has the advantage of givig us |
| 386 | * the following info : |
| 387 | * - error |
| 388 | * - connecting (EALREADY, EINPROGRESS) |
| 389 | * - connected (EISCONN, 0) |
| 390 | */ |
| 391 | |
| 392 | struct sockaddr_in sa; |
| 393 | |
| 394 | sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr; |
| 395 | sa.sin_port = htons(s->check_port); |
| 396 | |
| 397 | if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0) |
| 398 | errno = 0; |
| 399 | |
| 400 | if (errno == EALREADY || errno == EINPROGRESS) |
| 401 | goto out_poll; |
| 402 | |
| 403 | if (errno && errno != EISCONN) |
| 404 | goto out_error; |
| 405 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 406 | /* good TCP connection is enough */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 407 | s->result |= SRV_CHK_RUNNING; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 408 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 409 | } |
| 410 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 411 | out_wakeup: |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 412 | task_wakeup(t); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 413 | out_nowake: |
| 414 | EV_FD_CLR(fd, DIR_WR); /* nothing more to write */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 415 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 416 | return 1; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 417 | out_poll: |
| 418 | /* The connection is still pending. We'll have to poll it |
| 419 | * before attempting to go further. */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 420 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 421 | return 0; |
| 422 | out_error: |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 423 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 424 | fdtab[fd].state = FD_STERROR; |
| 425 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
| 429 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 430 | * 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] | 431 | * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING |
| 432 | * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP |
| 433 | * server returns 2xx, or if an SSL server returns at least 5 bytes in response |
| 434 | * to an SSL HELLO (the principle is that this is enough to distinguish between |
| 435 | * an SSL server and a pure TCP relay). All other cases will set s->result to |
| 436 | * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after |
| 437 | * some polling, otherwise non-zero.. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 438 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 439 | static int event_srv_chk_r(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 440 | { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 441 | __label__ out_wakeup; |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 442 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 443 | struct task *t = fdtab[fd].owner; |
| 444 | struct server *s = t->context; |
| 445 | int skerr; |
| 446 | socklen_t lskerr = sizeof(skerr); |
| 447 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 448 | len = -1; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 449 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 450 | if (unlikely((s->result & SRV_CHK_ERROR) || |
| 451 | (fdtab[fd].state == FD_STERROR) || |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 452 | (fdtab[fd].ev & FD_POLL_ERR) || |
| 453 | (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) || |
| 454 | (skerr != 0))) { |
| 455 | /* in case of TCP only, this tells us if the connection failed */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 456 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 457 | goto out_wakeup; |
| 458 | } |
| 459 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 460 | #ifndef MSG_NOSIGNAL |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 461 | len = recv(fd, trash, sizeof(trash), 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 462 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 463 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 464 | * but the connection was closed on the remote end. Fortunately, recv still |
| 465 | * works correctly and we don't need to do the getsockopt() on linux. |
| 466 | */ |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 467 | len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 468 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 469 | if (unlikely(len < 0 && errno == EAGAIN)) { |
| 470 | /* we want some polling to happen first */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 471 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 472 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 475 | /* Note: the response will only be accepted if read at once */ |
| 476 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 477 | /* Check if the server speaks HTTP 1.X */ |
| 478 | if ((len < strlen("HTTP/1.0 000\r")) || |
| 479 | (memcmp(trash, "HTTP/1.", 7) != 0)) { |
| 480 | s->result |= SRV_CHK_ERROR; |
| 481 | goto out_wakeup; |
| 482 | } |
| 483 | |
| 484 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
| 485 | if (trash[9] == '2' || trash[9] == '3') |
| 486 | s->result |= SRV_CHK_RUNNING; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 487 | else if ((s->proxy->options & PR_O_DISABLE404) && |
| 488 | (s->state & SRV_RUNNING) && |
| 489 | (memcmp(&trash[9], "404", 3) == 0)) { |
| 490 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 491 | s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE; |
| 492 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 493 | else |
| 494 | s->result |= SRV_CHK_ERROR; |
| 495 | } |
| 496 | else if (s->proxy->options & PR_O_SSL3_CHK) { |
| 497 | /* Check for SSLv3 alert or handshake */ |
| 498 | if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16)) |
| 499 | s->result |= SRV_CHK_RUNNING; |
| 500 | else |
| 501 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 502 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 503 | else if (s->proxy->options & PR_O_SMTP_CHK) { |
| 504 | /* Check for SMTP code 2xx (should be 250) */ |
| 505 | if ((len >= 3) && (trash[0] == '2')) |
| 506 | s->result |= SRV_CHK_RUNNING; |
| 507 | else |
| 508 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 509 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 510 | else { |
| 511 | /* other checks are valid if the connection succeeded anyway */ |
| 512 | s->result |= SRV_CHK_RUNNING; |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 513 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 515 | out_wakeup: |
| 516 | if (s->result & SRV_CHK_ERROR) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 517 | fdtab[fd].state = FD_STERROR; |
| 518 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 519 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 520 | task_wakeup(t); |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 521 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 522 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* |
| 526 | * manages a server health-check. Returns |
| 527 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 528 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 529 | void process_chk(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 530 | { |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 531 | __label__ new_chk, out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 532 | struct server *s = t->context; |
| 533 | struct sockaddr_in sa; |
| 534 | int fd; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 535 | int rv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 536 | |
| 537 | //fprintf(stderr, "process_chk: task=%p\n", t); |
| 538 | |
| 539 | new_chk: |
| 540 | fd = s->curfd; |
| 541 | if (fd < 0) { /* no check currently running */ |
| 542 | //fprintf(stderr, "process_chk: 2\n"); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 543 | if (!tv_isle(&t->expire, &now)) { /* not good time yet */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 544 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 545 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 546 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* we don't send any health-checks when the proxy is stopped or when |
| 550 | * the server should not be checked. |
| 551 | */ |
| 552 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 553 | while (tv_isle(&t->expire, &now)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 554 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 555 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 556 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 557 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* we'll initiate a new check */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 561 | s->result = SRV_CHK_UNKNOWN; /* no result yet */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 562 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
| 563 | if ((fd < global.maxsock) && |
| 564 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 565 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 566 | //fprintf(stderr, "process_chk: 3\n"); |
| 567 | |
Willy Tarreau | 9edd161 | 2007-10-18 18:07:48 +0200 | [diff] [blame] | 568 | if (s->proxy->options & PR_O_TCP_NOLING) { |
| 569 | /* We don't want to useless data */ |
| 570 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 571 | } |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 572 | |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 573 | if (s->check_addr.sin_addr.s_addr) |
| 574 | /* we'll connect to the check addr specified on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 575 | sa = s->check_addr; |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 576 | else |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 577 | /* we'll connect to the addr on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 578 | sa = s->addr; |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 579 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 580 | /* we'll connect to the check port on the server */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 581 | sa.sin_port = htons(s->check_port); |
| 582 | |
| 583 | /* allow specific binding : |
| 584 | * - server-specific at first |
| 585 | * - proxy-specific next |
| 586 | */ |
| 587 | if (s->state & SRV_BIND_SRC) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 588 | struct sockaddr_in *remote = NULL; |
| 589 | int ret, flags = 0; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 590 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 591 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 592 | if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) { |
| 593 | remote = (struct sockaddr_in *)&s->tproxy_addr; |
| 594 | flags = 3; |
| 595 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 596 | #endif |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 597 | ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote); |
| 598 | if (ret) { |
| 599 | s->result |= SRV_CHK_ERROR; |
| 600 | switch (ret) { |
| 601 | case 1: |
| 602 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 603 | s->proxy->id, s->id); |
| 604 | break; |
| 605 | case 2: |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 606 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 607 | s->proxy->id, s->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 608 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 609 | } |
| 610 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 611 | } |
| 612 | else if (s->proxy->options & PR_O_BIND_SRC) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 613 | struct sockaddr_in *remote = NULL; |
| 614 | int ret, flags = 0; |
| 615 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 616 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 617 | if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 618 | remote = (struct sockaddr_in *)&s->proxy->tproxy_addr; |
| 619 | flags = 3; |
| 620 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 621 | #endif |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 622 | ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote); |
| 623 | if (ret) { |
| 624 | s->result |= SRV_CHK_ERROR; |
| 625 | switch (ret) { |
| 626 | case 1: |
| 627 | Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n", |
| 628 | proxy_type_str(s->proxy), s->proxy->id); |
| 629 | break; |
| 630 | case 2: |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 631 | Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n", |
| 632 | proxy_type_str(s->proxy), s->proxy->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 633 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 634 | } |
| 635 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 638 | if (s->result == SRV_CHK_UNKNOWN) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 639 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 640 | struct timeval tv_con; |
| 641 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | /* OK, connection in progress or established */ |
| 643 | |
| 644 | //fprintf(stderr, "process_chk: 4\n"); |
| 645 | |
| 646 | 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] | 647 | fd_insert(fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | fdtab[fd].owner = t; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 649 | fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r; |
| 650 | fdtab[fd].cb[DIR_RD].b = NULL; |
| 651 | fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w; |
| 652 | fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 653 | fdtab[fd].peeraddr = (struct sockaddr *)&sa; |
| 654 | fdtab[fd].peerlen = sizeof(sa); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 655 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 656 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 657 | #ifdef DEBUG_FULL |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 658 | assert (!EV_FD_ISSET(fd, DIR_RD)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 659 | #endif |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 660 | //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 661 | /* we allow up to min(inter, timeout.connect) for a connection |
| 662 | * to establish but only when timeout.check is set |
| 663 | * as it may be to short for a full check otherwise |
| 664 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 665 | tv_ms_add(&t->expire, &now, s->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 666 | |
Willy Tarreau | 6054819 | 2008-02-17 11:34:10 +0100 | [diff] [blame] | 667 | if (tv_isset(&s->proxy->timeout.check) && tv_isset(&s->proxy->timeout.connect)) { |
| 668 | tv_add(&tv_con, &now, &s->proxy->timeout.connect); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 669 | tv_bound(&t->expire, &tv_con); |
Willy Tarreau | 6054819 | 2008-02-17 11:34:10 +0100 | [diff] [blame] | 670 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 671 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 672 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 673 | *next = t->expire; |
Willy Tarreau | 8eee9c8 | 2007-05-14 03:40:11 +0200 | [diff] [blame] | 674 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 675 | } |
| 676 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 677 | s->result |= SRV_CHK_ERROR; /* a real error */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | } |
| 681 | close(fd); /* socket creation error */ |
| 682 | } |
| 683 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 684 | if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 685 | //fprintf(stderr, "process_chk: 6\n"); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 686 | while (tv_isle(&t->expire, &now)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 687 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 688 | goto new_chk; /* may be we should initialize a new check */ |
| 689 | } |
| 690 | |
| 691 | /* here, we have seen a failure */ |
| 692 | if (s->health > s->rise) { |
| 693 | s->health--; /* still good */ |
| 694 | s->failed_checks++; |
| 695 | } |
| 696 | else |
| 697 | set_server_down(s); |
| 698 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 699 | //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 700 | /* we allow up to min(inter, timeout.connect) for a connection |
| 701 | * to establish but only when timeout.check is set |
| 702 | * as it may be to short for a full check otherwise |
| 703 | */ |
| 704 | while (tv_isle(&t->expire, &now)) { |
| 705 | struct timeval tv_con; |
| 706 | |
| 707 | tv_add(&tv_con, &t->expire, &s->proxy->timeout.connect); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 708 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 709 | |
| 710 | if (tv_isset(&s->proxy->timeout.check)) |
| 711 | tv_bound(&t->expire, &tv_con); |
| 712 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 713 | goto new_chk; |
| 714 | } |
| 715 | else { |
| 716 | //fprintf(stderr, "process_chk: 8\n"); |
| 717 | /* there was a test running */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 718 | 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] | 719 | //fprintf(stderr, "process_chk: 9\n"); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 720 | |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 721 | if (s->state & SRV_WARMINGUP) { |
| 722 | if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) { |
| 723 | s->state &= ~SRV_WARMINGUP; |
| 724 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) |
| 725 | s->eweight = s->uweight * BE_WEIGHT_SCALE; |
| 726 | if (s->proxy->lbprm.update_server_eweight) |
| 727 | s->proxy->lbprm.update_server_eweight(s); |
| 728 | } |
| 729 | else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 730 | /* for dynamic algorithms, let's update the weight */ |
Willy Tarreau | 5542af6 | 2007-12-03 02:04:00 +0100 | [diff] [blame] | 731 | s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) + |
| 732 | s->slowstart - 1) / s->slowstart; |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 733 | s->eweight *= s->uweight; |
| 734 | if (s->proxy->lbprm.update_server_eweight) |
| 735 | s->proxy->lbprm.update_server_eweight(s); |
| 736 | } |
| 737 | /* probably that we can refill this server with a bit more connections */ |
| 738 | check_for_pending(s); |
| 739 | } |
| 740 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 741 | /* we may have to add/remove this server from the LB group */ |
| 742 | if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) { |
| 743 | if ((s->state & SRV_GOINGDOWN) && |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 744 | ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING)) |
| 745 | set_server_enabled(s); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 746 | else if (!(s->state & SRV_GOINGDOWN) && |
| 747 | ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) == |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 748 | (SRV_CHK_RUNNING | SRV_CHK_DISABLE))) |
| 749 | set_server_disabled(s); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 752 | if (s->health < s->rise + s->fall - 1) { |
| 753 | s->health++; /* was bad, stays for a while */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 754 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 755 | set_server_up(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 756 | } |
| 757 | s->curfd = -1; /* no check running anymore */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 758 | fd_delete(fd); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 759 | |
| 760 | rv = 0; |
| 761 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 762 | rv = srv_getinter(s) * global.spread_checks / 100; |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 763 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 764 | //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] | 765 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 766 | tv_ms_add(&t->expire, &now, srv_getinter(s) + rv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 767 | goto new_chk; |
| 768 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 769 | else if ((s->result & SRV_CHK_ERROR) || tv_isle(&t->expire, &now)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 770 | //fprintf(stderr, "process_chk: 10\n"); |
| 771 | /* failure or timeout detected */ |
| 772 | if (s->health > s->rise) { |
| 773 | s->health--; /* still good */ |
| 774 | s->failed_checks++; |
| 775 | } |
| 776 | else |
| 777 | set_server_down(s); |
| 778 | s->curfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 779 | fd_delete(fd); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 780 | |
| 781 | rv = 0; |
| 782 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 783 | rv = srv_getinter(s) * global.spread_checks / 100; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 784 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 785 | //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] | 786 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 787 | tv_ms_add(&t->expire, &now, srv_getinter(s) + rv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 788 | goto new_chk; |
| 789 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 790 | /* 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] | 791 | } |
| 792 | //fprintf(stderr, "process_chk: 11\n"); |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 793 | s->result = SRV_CHK_UNKNOWN; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 794 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 795 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 796 | out: |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 797 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 800 | /* |
| 801 | * Start health-check. |
| 802 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 803 | */ |
| 804 | int start_checks() { |
| 805 | |
| 806 | struct proxy *px; |
| 807 | struct server *s; |
| 808 | struct task *t; |
| 809 | int nbchk=0, mininter=0, srvpos=0; |
| 810 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 811 | /* 1- count the checkers to run simultaneously. |
| 812 | * We also determine the minimum interval among all of those which |
| 813 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 814 | * will be used to spread their start-up date. Those which have |
| 815 | * a shorter interval will start independantly and will not dictate |
| 816 | * too short an interval for all others. |
| 817 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 818 | for (px = proxy; px; px = px->next) { |
| 819 | for (s = px->srv; s; s = s->next) { |
| 820 | if (!(s->state & SRV_CHECKED)) |
| 821 | continue; |
| 822 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 823 | if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) && |
| 824 | (!mininter || mininter > srv_getinter(s))) |
| 825 | mininter = srv_getinter(s); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 826 | |
| 827 | nbchk++; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | if (!nbchk) |
| 832 | return 0; |
| 833 | |
| 834 | srand((unsigned)time(NULL)); |
| 835 | |
| 836 | /* |
| 837 | * 2- start them as far as possible from each others. For this, we will |
| 838 | * start them after their interval set to the min interval divided by |
| 839 | * the number of servers, weighted by the server's position in the list. |
| 840 | */ |
| 841 | for (px = proxy; px; px = px->next) { |
| 842 | for (s = px->srv; s; s = s->next) { |
| 843 | if (!(s->state & SRV_CHECKED)) |
| 844 | continue; |
| 845 | |
| 846 | if ((t = pool_alloc2(pool2_task)) == NULL) { |
| 847 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 848 | return -1; |
| 849 | } |
| 850 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 851 | s->check = t; |
| 852 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame^] | 853 | task_init(t); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 854 | t->process = process_chk; |
| 855 | t->context = s; |
| 856 | |
| 857 | /* check this every ms */ |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 858 | tv_ms_add(&t->expire, &now, |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 859 | ((mininter && mininter >= srv_getinter(s)) ? mininter : srv_getinter(s)) * srvpos / nbchk); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 860 | task_queue(t); |
| 861 | |
| 862 | srvpos++; |
| 863 | } |
| 864 | } |
| 865 | return 0; |
| 866 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 867 | |
| 868 | /* |
| 869 | * Local variables: |
| 870 | * c-indent-level: 8 |
| 871 | * c-basic-offset: 8 |
| 872 | * End: |
| 873 | */ |