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