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