blob: 2c581ef1c8bf147171eb97532dbb23885f523edc [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreaud825eef2007-05-12 22:35:00 +02004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreau2dd0d472006-06-29 17:53:05 +020016#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020017#include <unistd.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
23#include <common/config.h>
24#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020025#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
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 Tarreau3d300592007-03-18 18:34:41 +010037#include <proto/proto_http.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010038#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/server.h>
40#include <proto/task.h>
41
Willy Tarreau163c5322006-11-14 16:18:41 +010042#ifdef CONFIG_HAP_CTTPROXY
43#include <import/ip_tproxy.h>
44#endif
45
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
47/* Sets server <s> down, notifies by all available means, recounts the
48 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +020049 * possible to other servers. It automatically recomputes the number of
50 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 */
Willy Tarreau83749182007-04-15 20:56:27 +020052static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020053{
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 Tarreau5af3a692007-07-24 23:32:33 +020062 s->proxy->map_state |= PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
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 Tarreaue2e27a52007-04-01 00:01:37 +020071 if ((sess->be->options & PR_O_REDISP)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020072 /* 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 Tarreau3d300592007-03-18 18:34:41 +010077 http_flush_cookie_flags(&sess->txn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020078 pendconn_free(pc);
Willy Tarreau96bcfd72007-04-29 10:41:56 +020079 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 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 Tarreau2b5652f2006-12-31 17:46:05 +010095 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 Tarreaubaaee002006-06-26 02:48:02 +020097 }
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 Tarreau83749182007-04-15 20:56:27 +0200107 * 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 Tarreaubaaee002006-06-26 02:48:02 +0200111 */
Willy Tarreau83749182007-04-15 20:56:27 +0200112static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113{
Willy Tarreau6996e152007-04-30 14:37:43 +0200114 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 struct task *t = fdtab[fd].owner;
116 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117
Willy Tarreau6996e152007-04-30 14:37:43 +0200118 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 Tarreau83749182007-04-15 20:56:27 +0200122
123 if (s->result != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200125 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200126 (s->proxy->options & PR_O_SSL3_CHK) ||
127 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200129 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130 * so we'll send the request, and won't wake the checker up now.
131 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200132
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 Tarreaubaaee002006-06-26 02:48:02 +0200139#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 Tarreauf161a342007-04-08 16:59:42 +0200145 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200146 goto out_nowake;
147 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200148 else if (ret == 0 || errno == EAGAIN)
149 goto out_poll;
150 else
151 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200152 }
153 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200154 /* 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 Tarreaubaaee002006-06-26 02:48:02 +0200178 /* good TCP connection is enough */
179 s->result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200180 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181 }
182 }
Willy Tarreau83749182007-04-15 20:56:27 +0200183 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200184 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200185 out_nowake:
186 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
187 fdtab[fd].ev &= ~FD_POLL_WR;
188 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200189 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 Tarreaubaaee002006-06-26 02:48:02 +0200198}
199
200
201/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200202 * 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 Tarreau83749182007-04-15 20:56:27 +0200207 * return -1. The function returns 0 if it needs to be called again after some
208 * polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209 */
Willy Tarreau83749182007-04-15 20:56:27 +0200210static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200211{
Willy Tarreau83749182007-04-15 20:56:27 +0200212 __label__ out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213 char reply[64];
214 int len, result;
215 struct task *t = fdtab[fd].owner;
216 struct server *s = t->context;
217 int skerr;
218 socklen_t lskerr = sizeof(skerr);
219
220 result = len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200221
222 if (unlikely(fdtab[fd].state == FD_STERROR ||
223 (fdtab[fd].ev & FD_POLL_ERR) ||
224 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
225 (skerr != 0))) {
226 /* in case of TCP only, this tells us if the connection failed */
227 s->result = -1;
228 fdtab[fd].state = FD_STERROR;
229 goto out_wakeup;
230 }
231
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +0200233 len = recv(fd, reply, sizeof(reply), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200234#else
Willy Tarreau83749182007-04-15 20:56:27 +0200235 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
236 * but the connection was closed on the remote end. Fortunately, recv still
237 * works correctly and we don't need to do the getsockopt() on linux.
238 */
239 len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200241 if (unlikely(len < 0 && errno == EAGAIN)) {
242 /* we want some polling to happen first */
243 fdtab[fd].ev &= ~FD_POLL_RD;
244 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 }
246
Willy Tarreau6996e152007-04-30 14:37:43 +0200247 if ((s->proxy->options & PR_O_HTTP_CHK) && (len >= sizeof("HTTP/1.0 000")) &&
248 (memcmp(reply, "HTTP/1.", 7) == 0) && (reply[9] == '2' || reply[9] == '3')) {
249 /* HTTP/1.X 2xx or 3xx */
Willy Tarreau83749182007-04-15 20:56:27 +0200250 result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200251 }
252 else if ((s->proxy->options & PR_O_SSL3_CHK) && (len >= 5) &&
253 (reply[0] == 0x15 || reply[0] == 0x16)) {
254 /* SSLv3 alert or handshake */
255 result = 1;
256 }
Willy Tarreau23677902007-05-08 23:50:35 +0200257 else if ((s->proxy->options & PR_O_SMTP_CHK) && (len >= 3) &&
258 (reply[0] == '2')) /* 2xx (should be 250) */ {
259 result = 1;
260 }
Willy Tarreau83749182007-04-15 20:56:27 +0200261
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262 if (result == -1)
263 fdtab[fd].state = FD_STERROR;
264
265 if (s->result != -1)
266 s->result = result;
267
Willy Tarreau83749182007-04-15 20:56:27 +0200268 out_wakeup:
Willy Tarreauf161a342007-04-08 16:59:42 +0200269 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200270 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200271 fdtab[fd].ev &= ~FD_POLL_RD;
272 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273}
274
275/*
276 * manages a server health-check. Returns
277 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
278 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200279void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200281 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282 struct server *s = t->context;
283 struct sockaddr_in sa;
284 int fd;
285
286 //fprintf(stderr, "process_chk: task=%p\n", t);
287
288 new_chk:
289 fd = s->curfd;
290 if (fd < 0) { /* no check currently running */
291 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200292 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200294 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200295 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296 }
297
298 /* we don't send any health-checks when the proxy is stopped or when
299 * the server should not be checked.
300 */
301 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200302 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200303 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200305 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200306 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307 }
308
309 /* we'll initiate a new check */
310 s->result = 0; /* no result yet */
311 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
312 if ((fd < global.maxsock) &&
313 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
314 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
315 //fprintf(stderr, "process_chk: 3\n");
316
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200317
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200318 if (s->check_addr.sin_addr.s_addr)
319 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200320 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200321 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200322 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200323 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200324
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326 sa.sin_port = htons(s->check_port);
327
328 /* allow specific binding :
329 * - server-specific at first
330 * - proxy-specific next
331 */
332 if (s->state & SRV_BIND_SRC) {
333 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
334 if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) {
335 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
336 s->proxy->id, s->id);
337 s->result = -1;
338 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100339#ifdef CONFIG_HAP_CTTPROXY
340 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
341 struct in_tproxy itp1, itp2;
342 memset(&itp1, 0, sizeof(itp1));
343
344 itp1.op = TPROXY_ASSIGN;
345 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
346 itp1.v.addr.fport = s->tproxy_addr.sin_port;
347
348 /* set connect flag on socket */
349 itp2.op = TPROXY_FLAGS;
350 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
351
352 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
353 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
354 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
355 s->proxy->id, s->id);
356 s->result = -1;
357 }
358 }
359#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360 }
361 else if (s->proxy->options & PR_O_BIND_SRC) {
362 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
363 if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100364 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
365 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366 s->result = -1;
367 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100368#ifdef CONFIG_HAP_CTTPROXY
369 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
370 struct in_tproxy itp1, itp2;
371 memset(&itp1, 0, sizeof(itp1));
372
373 itp1.op = TPROXY_ASSIGN;
374 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
375 itp1.v.addr.fport = s->tproxy_addr.sin_port;
376
377 /* set connect flag on socket */
378 itp2.op = TPROXY_FLAGS;
379 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
380
381 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
382 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100383 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
384 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreau163c5322006-11-14 16:18:41 +0100385 s->result = -1;
386 }
387 }
388#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389 }
390
391 if (!s->result) {
392 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
393 /* OK, connection in progress or established */
394
395 //fprintf(stderr, "process_chk: 4\n");
396
397 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200398 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200400 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
401 fdtab[fd].cb[DIR_RD].b = NULL;
402 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
403 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200405 fdtab[fd].ev = 0;
Willy Tarreauf161a342007-04-08 16:59:42 +0200406 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200408 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200411 tv_ms_add(&t->expire, &now, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200413 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200414 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415 }
416 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
417 s->result = -1; /* a real error */
418 }
419 }
420 }
421 close(fd); /* socket creation error */
422 }
423
424 if (!s->result) { /* nothing done */
425 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200426 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200427 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428 goto new_chk; /* may be we should initialize a new check */
429 }
430
431 /* here, we have seen a failure */
432 if (s->health > s->rise) {
433 s->health--; /* still good */
434 s->failed_checks++;
435 }
436 else
437 set_server_down(s);
438
439 //fprintf(stderr, "process_chk: 7\n");
440 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200441 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200442 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443 goto new_chk;
444 }
445 else {
446 //fprintf(stderr, "process_chk: 8\n");
447 /* there was a test running */
448 if (s->result > 0) { /* good server detected */
449 //fprintf(stderr, "process_chk: 9\n");
450 s->health++; /* was bad, stays for a while */
451 if (s->health >= s->rise) {
452 s->state |= SRV_RUNNING;
453
454 if (s->health == s->rise) {
455 int xferred;
456
457 recount_servers(s->proxy);
Willy Tarreau5af3a692007-07-24 23:32:33 +0200458 s->proxy->map_state |= PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459
460 /* check if we can handle some connections queued at the proxy. We
461 * will take as many as we can handle.
462 */
463 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
464 struct session *sess;
465 struct pendconn *p;
466
467 p = pendconn_from_px(s->proxy);
468 if (!p)
469 break;
470 p->sess->srv = s;
471 sess = p->sess;
472 pendconn_free(p);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200473 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 }
475
476 sprintf(trash,
477 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
478 " %d sessions requeued, %d total in queue.\n",
479 s->state & SRV_BACKUP ? "Backup " : "",
480 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
481 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
482 xferred, s->nbpend);
483
484 Warning("%s", trash);
485 send_log(s->proxy, LOG_NOTICE, "%s", trash);
486 }
487
488 s->health = s->rise + s->fall - 1; /* OK now */
489 }
490 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491 fd_delete(fd);
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200492 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200493 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494 goto new_chk;
495 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200496 else if (s->result < 0 || tv_isle(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200497 //fprintf(stderr, "process_chk: 10\n");
498 /* failure or timeout detected */
499 if (s->health > s->rise) {
500 s->health--; /* still good */
501 s->failed_checks++;
502 }
503 else
504 set_server_down(s);
505 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506 fd_delete(fd);
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200507 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200508 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200509 goto new_chk;
510 }
511 /* if result is 0 and there's no timeout, we have to wait again */
512 }
513 //fprintf(stderr, "process_chk: 11\n");
514 s->result = 0;
515 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200516 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200517 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200518 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519}
520
521
522/*
523 * Local variables:
524 * c-indent-level: 8
525 * c-basic-offset: 8
526 * End:
527 */