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