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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
| 49 | /* Sets server <s> down, notifies by all available means, recounts the |
| 50 | * remaining servers on the proxy and transfers queued sessions whenever |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 51 | * possible to other servers. It automatically recomputes the number of |
| 52 | * servers, but not the map. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 54 | static void set_server_down(struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | { |
| 56 | struct pendconn *pc, *pc_bck, *pc_end; |
| 57 | struct session *sess; |
| 58 | int xferred; |
| 59 | |
| 60 | s->state &= ~SRV_RUNNING; |
| 61 | |
| 62 | if (s->health == s->rise) { |
| 63 | recount_servers(s->proxy); |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 64 | s->proxy->map_state |= PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | |
| 66 | /* we might have sessions queued on this server and waiting for |
| 67 | * a connection. Those which are redispatchable will be queued |
| 68 | * to another server or to the proxy itself. |
| 69 | */ |
| 70 | xferred = 0; |
| 71 | FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) { |
| 72 | sess = pc->sess; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 73 | if ((sess->be->options & PR_O_REDISP)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | /* The REDISP option was specified. We will ignore |
| 75 | * cookie and force to balance or use the dispatcher. |
| 76 | */ |
| 77 | sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 78 | sess->srv = NULL; /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 79 | http_flush_cookie_flags(&sess->txn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | pendconn_free(pc); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 81 | task_wakeup(sess->task); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | xferred++; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s" |
| 87 | " %d sessions active, %d requeued, %d remaining in queue.\n", |
| 88 | s->state & SRV_BACKUP ? "Backup " : "", |
| 89 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 90 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 91 | s->cur_sess, xferred, s->nbpend); |
| 92 | |
| 93 | Warning("%s", trash); |
| 94 | send_log(s->proxy, LOG_ALERT, "%s", trash); |
| 95 | |
| 96 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 97 | Alert("%s '%s' has no server available !\n", proxy_type_str(s->proxy), s->proxy->id); |
| 98 | send_log(s->proxy, LOG_EMERG, "%s %s has no server available !\n", proxy_type_str(s->proxy), s->proxy->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | } |
| 100 | s->down_trans++; |
| 101 | } |
| 102 | s->health = 0; /* failure */ |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * This function is used only for server health-checks. It handles |
| 108 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 109 | * it sends the request. In other cases, it returns 1 in s->result if the |
| 110 | * socket is OK, or -1 if an error occured. |
| 111 | * The function itself returns 0 if it needs some polling before being called |
| 112 | * again, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 114 | static int event_srv_chk_w(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 116 | __label__ out_wakeup, out_nowake, out_poll, out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | struct task *t = fdtab[fd].owner; |
| 118 | struct server *s = t->context; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 120 | if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) |
| 121 | goto out_error; |
| 122 | |
| 123 | /* here, we know that the connection is established */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 124 | |
| 125 | if (s->result != -1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | /* 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] | 127 | if ((s->proxy->options & PR_O_HTTP_CHK) || |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 128 | (s->proxy->options & PR_O_SSL3_CHK) || |
| 129 | (s->proxy->options & PR_O_SMTP_CHK)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | int ret; |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 131 | /* 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] | 132 | * so we'll send the request, and won't wake the checker up now. |
| 133 | */ |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 134 | |
| 135 | if (s->proxy->options & PR_O_SSL3_CHK) { |
| 136 | /* SSL requires that we put Unix time in the request */ |
| 137 | int gmt_time = htonl(now.tv_sec); |
| 138 | memcpy(s->proxy->check_req + 11, &gmt_time, 4); |
| 139 | } |
| 140 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 141 | #ifndef MSG_NOSIGNAL |
| 142 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT); |
| 143 | #else |
| 144 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 145 | #endif |
| 146 | if (ret == s->proxy->check_len) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 147 | EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 148 | goto out_nowake; |
| 149 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 150 | else if (ret == 0 || errno == EAGAIN) |
| 151 | goto out_poll; |
| 152 | else |
| 153 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | } |
| 155 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 156 | /* We have no data to send to check the connection, and |
| 157 | * getsockopt() will not inform us whether the connection |
| 158 | * is still pending. So we'll reuse connect() to check the |
| 159 | * state of the socket. This has the advantage of givig us |
| 160 | * the following info : |
| 161 | * - error |
| 162 | * - connecting (EALREADY, EINPROGRESS) |
| 163 | * - connected (EISCONN, 0) |
| 164 | */ |
| 165 | |
| 166 | struct sockaddr_in sa; |
| 167 | |
| 168 | sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr; |
| 169 | sa.sin_port = htons(s->check_port); |
| 170 | |
| 171 | if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0) |
| 172 | errno = 0; |
| 173 | |
| 174 | if (errno == EALREADY || errno == EINPROGRESS) |
| 175 | goto out_poll; |
| 176 | |
| 177 | if (errno && errno != EISCONN) |
| 178 | goto out_error; |
| 179 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 180 | /* good TCP connection is enough */ |
| 181 | s->result = 1; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 182 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 183 | } |
| 184 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 185 | out_wakeup: |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 186 | task_wakeup(t); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 187 | out_nowake: |
| 188 | EV_FD_CLR(fd, DIR_WR); /* nothing more to write */ |
| 189 | fdtab[fd].ev &= ~FD_POLL_WR; |
| 190 | return 1; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 191 | out_poll: |
| 192 | /* The connection is still pending. We'll have to poll it |
| 193 | * before attempting to go further. */ |
| 194 | fdtab[fd].ev &= ~FD_POLL_WR; |
| 195 | return 0; |
| 196 | out_error: |
| 197 | s->result = -1; |
| 198 | fdtab[fd].state = FD_STERROR; |
| 199 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
| 203 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 204 | * This function is used only for server health-checks. It handles the server's |
| 205 | * reply to an HTTP request or SSL HELLO. It returns 1 in s->result if the |
| 206 | * server replies HTTP 2xx or 3xx (valid responses), or if it returns at least |
| 207 | * 5 bytes in response to SSL HELLO. The principle is that this is enough to |
| 208 | * distinguish between an SSL server and a pure TCP relay. All other cases will |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 209 | * return -1. The function returns 0 if it needs to be called again after some |
| 210 | * polling, otherwise non-zero.. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 211 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 212 | static int event_srv_chk_r(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 213 | { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 214 | __label__ out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 215 | int len, result; |
| 216 | struct task *t = fdtab[fd].owner; |
| 217 | struct server *s = t->context; |
| 218 | int skerr; |
| 219 | socklen_t lskerr = sizeof(skerr); |
| 220 | |
| 221 | result = len = -1; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 222 | |
| 223 | if (unlikely(fdtab[fd].state == FD_STERROR || |
| 224 | (fdtab[fd].ev & FD_POLL_ERR) || |
| 225 | (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) || |
| 226 | (skerr != 0))) { |
| 227 | /* in case of TCP only, this tells us if the connection failed */ |
| 228 | s->result = -1; |
| 229 | fdtab[fd].state = FD_STERROR; |
| 230 | goto out_wakeup; |
| 231 | } |
| 232 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 233 | #ifndef MSG_NOSIGNAL |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 234 | len = recv(fd, trash, sizeof(trash), 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 235 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 236 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 237 | * but the connection was closed on the remote end. Fortunately, recv still |
| 238 | * works correctly and we don't need to do the getsockopt() on linux. |
| 239 | */ |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 240 | len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 241 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 242 | if (unlikely(len < 0 && errno == EAGAIN)) { |
| 243 | /* we want some polling to happen first */ |
| 244 | fdtab[fd].ev &= ~FD_POLL_RD; |
| 245 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 248 | if ((s->proxy->options & PR_O_HTTP_CHK) && (len >= sizeof("HTTP/1.0 000")) && |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 249 | (memcmp(trash, "HTTP/1.", 7) == 0) && (trash[9] == '2' || trash[9] == '3')) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 250 | /* HTTP/1.X 2xx or 3xx */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 251 | result = 1; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 252 | } |
| 253 | else if ((s->proxy->options & PR_O_SSL3_CHK) && (len >= 5) && |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 254 | (trash[0] == 0x15 || trash[0] == 0x16)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 255 | /* SSLv3 alert or handshake */ |
| 256 | result = 1; |
| 257 | } |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 258 | else if ((s->proxy->options & PR_O_SMTP_CHK) && (len >= 3) && |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 259 | (trash[0] == '2')) /* 2xx (should be 250) */ { |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 260 | result = 1; |
| 261 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | if (result == -1) |
| 264 | fdtab[fd].state = FD_STERROR; |
| 265 | |
| 266 | if (s->result != -1) |
| 267 | s->result = result; |
| 268 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 269 | out_wakeup: |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 270 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 271 | task_wakeup(t); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 272 | fdtab[fd].ev &= ~FD_POLL_RD; |
| 273 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * manages a server health-check. Returns |
| 278 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 279 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 280 | void process_chk(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 281 | { |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 282 | __label__ new_chk, out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 283 | struct server *s = t->context; |
| 284 | struct sockaddr_in sa; |
| 285 | int fd; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 286 | int rv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 287 | |
| 288 | //fprintf(stderr, "process_chk: task=%p\n", t); |
| 289 | |
| 290 | new_chk: |
| 291 | fd = s->curfd; |
| 292 | if (fd < 0) { /* no check currently running */ |
| 293 | //fprintf(stderr, "process_chk: 2\n"); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 294 | if (!tv_isle(&t->expire, &now)) { /* not good time yet */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 295 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 296 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 297 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* we don't send any health-checks when the proxy is stopped or when |
| 301 | * the server should not be checked. |
| 302 | */ |
| 303 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 304 | while (tv_isle(&t->expire, &now)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 305 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 306 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 307 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 308 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* we'll initiate a new check */ |
| 312 | s->result = 0; /* no result yet */ |
| 313 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
| 314 | if ((fd < global.maxsock) && |
| 315 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 316 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 317 | //fprintf(stderr, "process_chk: 3\n"); |
| 318 | |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 319 | |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 320 | if (s->check_addr.sin_addr.s_addr) |
| 321 | /* we'll connect to the check addr specified on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 322 | sa = s->check_addr; |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 323 | else |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 324 | /* we'll connect to the addr on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 325 | sa = s->addr; |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 326 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 327 | /* we'll connect to the check port on the server */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 328 | sa.sin_port = htons(s->check_port); |
| 329 | |
| 330 | /* allow specific binding : |
| 331 | * - server-specific at first |
| 332 | * - proxy-specific next |
| 333 | */ |
| 334 | if (s->state & SRV_BIND_SRC) { |
| 335 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 336 | if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) { |
| 337 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 338 | s->proxy->id, s->id); |
| 339 | s->result = -1; |
| 340 | } |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 341 | #ifdef CONFIG_HAP_CTTPROXY |
| 342 | if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) { |
| 343 | struct in_tproxy itp1, itp2; |
| 344 | memset(&itp1, 0, sizeof(itp1)); |
| 345 | |
| 346 | itp1.op = TPROXY_ASSIGN; |
| 347 | itp1.v.addr.faddr = s->tproxy_addr.sin_addr; |
| 348 | itp1.v.addr.fport = s->tproxy_addr.sin_port; |
| 349 | |
| 350 | /* set connect flag on socket */ |
| 351 | itp2.op = TPROXY_FLAGS; |
| 352 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 353 | |
| 354 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 355 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
| 356 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 357 | s->proxy->id, s->id); |
| 358 | s->result = -1; |
| 359 | } |
| 360 | } |
| 361 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 362 | } |
| 363 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 364 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 365 | 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] | 366 | Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n", |
| 367 | proxy_type_str(s->proxy), s->proxy->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | s->result = -1; |
| 369 | } |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 370 | #ifdef CONFIG_HAP_CTTPROXY |
| 371 | if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) { |
| 372 | struct in_tproxy itp1, itp2; |
| 373 | memset(&itp1, 0, sizeof(itp1)); |
| 374 | |
| 375 | itp1.op = TPROXY_ASSIGN; |
| 376 | itp1.v.addr.faddr = s->tproxy_addr.sin_addr; |
| 377 | itp1.v.addr.fport = s->tproxy_addr.sin_port; |
| 378 | |
| 379 | /* set connect flag on socket */ |
| 380 | itp2.op = TPROXY_FLAGS; |
| 381 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 382 | |
| 383 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 384 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 385 | Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n", |
| 386 | proxy_type_str(s->proxy), s->proxy->id); |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 387 | s->result = -1; |
| 388 | } |
| 389 | } |
| 390 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if (!s->result) { |
| 394 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
| 395 | /* OK, connection in progress or established */ |
| 396 | |
| 397 | //fprintf(stderr, "process_chk: 4\n"); |
| 398 | |
| 399 | 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] | 400 | fd_insert(fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 401 | fdtab[fd].owner = t; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 402 | fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r; |
| 403 | fdtab[fd].cb[DIR_RD].b = NULL; |
| 404 | fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w; |
| 405 | fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 406 | fdtab[fd].peeraddr = (struct sockaddr *)&sa; |
| 407 | fdtab[fd].peerlen = sizeof(sa); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 408 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 409 | fdtab[fd].ev = 0; |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 410 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | #ifdef DEBUG_FULL |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 412 | assert (!EV_FD_ISSET(fd, DIR_RD)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 414 | /* 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] | 415 | tv_ms_add(&t->expire, &now, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 416 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 417 | *next = t->expire; |
Willy Tarreau | 8eee9c8 | 2007-05-14 03:40:11 +0200 | [diff] [blame] | 418 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 419 | } |
| 420 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 421 | s->result = -1; /* a real error */ |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | close(fd); /* socket creation error */ |
| 426 | } |
| 427 | |
| 428 | if (!s->result) { /* nothing done */ |
| 429 | //fprintf(stderr, "process_chk: 6\n"); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 430 | while (tv_isle(&t->expire, &now)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 431 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 432 | goto new_chk; /* may be we should initialize a new check */ |
| 433 | } |
| 434 | |
| 435 | /* here, we have seen a failure */ |
| 436 | if (s->health > s->rise) { |
| 437 | s->health--; /* still good */ |
| 438 | s->failed_checks++; |
| 439 | } |
| 440 | else |
| 441 | set_server_down(s); |
| 442 | |
| 443 | //fprintf(stderr, "process_chk: 7\n"); |
| 444 | /* 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] | 445 | while (tv_isle(&t->expire, &now)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 446 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 447 | goto new_chk; |
| 448 | } |
| 449 | else { |
| 450 | //fprintf(stderr, "process_chk: 8\n"); |
| 451 | /* there was a test running */ |
| 452 | if (s->result > 0) { /* good server detected */ |
| 453 | //fprintf(stderr, "process_chk: 9\n"); |
| 454 | s->health++; /* was bad, stays for a while */ |
| 455 | if (s->health >= s->rise) { |
| 456 | s->state |= SRV_RUNNING; |
| 457 | |
| 458 | if (s->health == s->rise) { |
| 459 | int xferred; |
| 460 | |
| 461 | recount_servers(s->proxy); |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 462 | s->proxy->map_state |= PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 463 | |
| 464 | /* check if we can handle some connections queued at the proxy. We |
| 465 | * will take as many as we can handle. |
| 466 | */ |
| 467 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
| 468 | struct session *sess; |
| 469 | struct pendconn *p; |
| 470 | |
| 471 | p = pendconn_from_px(s->proxy); |
| 472 | if (!p) |
| 473 | break; |
| 474 | p->sess->srv = s; |
| 475 | sess = p->sess; |
| 476 | pendconn_free(p); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 477 | task_wakeup(sess->task); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | sprintf(trash, |
| 481 | "%sServer %s/%s is UP. %d active and %d backup servers online.%s" |
| 482 | " %d sessions requeued, %d total in queue.\n", |
| 483 | s->state & SRV_BACKUP ? "Backup " : "", |
| 484 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 485 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 486 | xferred, s->nbpend); |
| 487 | |
| 488 | Warning("%s", trash); |
| 489 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 490 | } |
| 491 | |
| 492 | s->health = s->rise + s->fall - 1; /* OK now */ |
| 493 | } |
| 494 | s->curfd = -1; /* no check running anymore */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 495 | fd_delete(fd); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 496 | |
| 497 | rv = 0; |
| 498 | if (global.spread_checks > 0) { |
| 499 | rv = s->inter * global.spread_checks / 100; |
| 500 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
| 501 | //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv); |
| 502 | } |
| 503 | tv_ms_add(&t->expire, &now, s->inter + rv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 504 | goto new_chk; |
| 505 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 506 | else if (s->result < 0 || tv_isle(&t->expire, &now)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 507 | //fprintf(stderr, "process_chk: 10\n"); |
| 508 | /* failure or timeout detected */ |
| 509 | if (s->health > s->rise) { |
| 510 | s->health--; /* still good */ |
| 511 | s->failed_checks++; |
| 512 | } |
| 513 | else |
| 514 | set_server_down(s); |
| 515 | s->curfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 516 | fd_delete(fd); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 517 | |
| 518 | rv = 0; |
| 519 | if (global.spread_checks > 0) { |
| 520 | rv = s->inter * global.spread_checks / 100; |
| 521 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 522 | //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] | 523 | } |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 524 | tv_ms_add(&t->expire, &now, s->inter + rv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 525 | goto new_chk; |
| 526 | } |
| 527 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 528 | } |
| 529 | //fprintf(stderr, "process_chk: 11\n"); |
| 530 | s->result = 0; |
| 531 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 532 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 533 | out: |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 534 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 537 | /* |
| 538 | * Start health-check. |
| 539 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 540 | */ |
| 541 | int start_checks() { |
| 542 | |
| 543 | struct proxy *px; |
| 544 | struct server *s; |
| 545 | struct task *t; |
| 546 | int nbchk=0, mininter=0, srvpos=0; |
| 547 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 548 | /* 1- count the checkers to run simultaneously. |
| 549 | * We also determine the minimum interval among all of those which |
| 550 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 551 | * will be used to spread their start-up date. Those which have |
| 552 | * a shorter interval will start independantly and will not dictate |
| 553 | * too short an interval for all others. |
| 554 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 555 | for (px = proxy; px; px = px->next) { |
| 556 | for (s = px->srv; s; s = s->next) { |
| 557 | if (!(s->state & SRV_CHECKED)) |
| 558 | continue; |
| 559 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 560 | if ((s->inter >= SRV_CHK_INTER_THRES) && |
| 561 | (!mininter || mininter > s->inter)) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 562 | mininter = s->inter; |
| 563 | |
| 564 | nbchk++; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if (!nbchk) |
| 569 | return 0; |
| 570 | |
| 571 | srand((unsigned)time(NULL)); |
| 572 | |
| 573 | /* |
| 574 | * 2- start them as far as possible from each others. For this, we will |
| 575 | * start them after their interval set to the min interval divided by |
| 576 | * the number of servers, weighted by the server's position in the list. |
| 577 | */ |
| 578 | for (px = proxy; px; px = px->next) { |
| 579 | for (s = px->srv; s; s = s->next) { |
| 580 | if (!(s->state & SRV_CHECKED)) |
| 581 | continue; |
| 582 | |
| 583 | if ((t = pool_alloc2(pool2_task)) == NULL) { |
| 584 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 585 | return -1; |
| 586 | } |
| 587 | |
| 588 | t->wq = NULL; |
| 589 | t->qlist.p = NULL; |
| 590 | t->state = TASK_IDLE; |
| 591 | t->process = process_chk; |
| 592 | t->context = s; |
| 593 | |
| 594 | /* check this every ms */ |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 595 | tv_ms_add(&t->expire, &now, |
| 596 | ((mininter && mininter >= s->inter) ? mininter : s->inter) * srvpos / nbchk); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 597 | task_queue(t); |
| 598 | |
| 599 | srvpos++; |
| 600 | } |
| 601 | } |
| 602 | return 0; |
| 603 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * Local variables: |
| 607 | * c-indent-level: 8 |
| 608 | * c-basic-offset: 8 |
| 609 | * End: |
| 610 | */ |