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