blob: ec56453a54f76ab75ba4976ac5af6b9ad50aec2e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
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 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
49 * possible to other servers.
50 */
Willy Tarreau83749182007-04-15 20:56:27 +020051static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020052{
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 Tarreaue2e27a52007-04-01 00:01:37 +020070 if ((sess->be->options & PR_O_REDISP)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 /* 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 Tarreau3d300592007-03-18 18:34:41 +010076 http_flush_cookie_flags(&sess->txn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020077 pendconn_free(pc);
Willy Tarreau96bcfd72007-04-29 10:41:56 +020078 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +020079 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 Tarreau2b5652f2006-12-31 17:46:05 +010094 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 Tarreaubaaee002006-06-26 02:48:02 +020096 }
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 Tarreau83749182007-04-15 20:56:27 +0200106 * 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 Tarreaubaaee002006-06-26 02:48:02 +0200110 */
Willy Tarreau83749182007-04-15 20:56:27 +0200111static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112{
Willy Tarreau6996e152007-04-30 14:37:43 +0200113 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 struct task *t = fdtab[fd].owner;
115 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116
Willy Tarreau6996e152007-04-30 14:37:43 +0200117 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
118 goto out_error;
119
120 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200121
122 if (s->result != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200124 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200125 (s->proxy->options & PR_O_SSL3_CHK) ||
126 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200128 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 * so we'll send the request, and won't wake the checker up now.
130 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200131
132 if (s->proxy->options & PR_O_SSL3_CHK) {
133 /* SSL requires that we put Unix time in the request */
134 int gmt_time = htonl(now.tv_sec);
135 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
136 }
137
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138#ifndef MSG_NOSIGNAL
139 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
140#else
141 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
142#endif
143 if (ret == s->proxy->check_len) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200144 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200145 goto out_nowake;
146 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200147 else if (ret == 0 || errno == EAGAIN)
148 goto out_poll;
149 else
150 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 }
152 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200153 /* We have no data to send to check the connection, and
154 * getsockopt() will not inform us whether the connection
155 * is still pending. So we'll reuse connect() to check the
156 * state of the socket. This has the advantage of givig us
157 * the following info :
158 * - error
159 * - connecting (EALREADY, EINPROGRESS)
160 * - connected (EISCONN, 0)
161 */
162
163 struct sockaddr_in sa;
164
165 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
166 sa.sin_port = htons(s->check_port);
167
168 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
169 errno = 0;
170
171 if (errno == EALREADY || errno == EINPROGRESS)
172 goto out_poll;
173
174 if (errno && errno != EISCONN)
175 goto out_error;
176
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177 /* good TCP connection is enough */
178 s->result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200179 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 }
181 }
Willy Tarreau83749182007-04-15 20:56:27 +0200182 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200183 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200184 out_nowake:
185 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
186 fdtab[fd].ev &= ~FD_POLL_WR;
187 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200188 out_poll:
189 /* The connection is still pending. We'll have to poll it
190 * before attempting to go further. */
191 fdtab[fd].ev &= ~FD_POLL_WR;
192 return 0;
193 out_error:
194 s->result = -1;
195 fdtab[fd].state = FD_STERROR;
196 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197}
198
199
200/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200201 * This function is used only for server health-checks. It handles the server's
202 * reply to an HTTP request or SSL HELLO. It returns 1 in s->result if the
203 * server replies HTTP 2xx or 3xx (valid responses), or if it returns at least
204 * 5 bytes in response to SSL HELLO. The principle is that this is enough to
205 * distinguish between an SSL server and a pure TCP relay. All other cases will
Willy Tarreau83749182007-04-15 20:56:27 +0200206 * return -1. The function returns 0 if it needs to be called again after some
207 * polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 */
Willy Tarreau83749182007-04-15 20:56:27 +0200209static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210{
Willy Tarreau83749182007-04-15 20:56:27 +0200211 __label__ out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212 char reply[64];
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 Tarreau83749182007-04-15 20:56:27 +0200220
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 Tarreaubaaee002006-06-26 02:48:02 +0200231#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +0200232 len = recv(fd, reply, sizeof(reply), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233#else
Willy Tarreau83749182007-04-15 20:56:27 +0200234 /* 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 */
238 len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200240 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 Tarreaubaaee002006-06-26 02:48:02 +0200244 }
245
Willy Tarreau6996e152007-04-30 14:37:43 +0200246 if ((s->proxy->options & PR_O_HTTP_CHK) && (len >= sizeof("HTTP/1.0 000")) &&
247 (memcmp(reply, "HTTP/1.", 7) == 0) && (reply[9] == '2' || reply[9] == '3')) {
248 /* HTTP/1.X 2xx or 3xx */
Willy Tarreau83749182007-04-15 20:56:27 +0200249 result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200250 }
251 else if ((s->proxy->options & PR_O_SSL3_CHK) && (len >= 5) &&
252 (reply[0] == 0x15 || reply[0] == 0x16)) {
253 /* SSLv3 alert or handshake */
254 result = 1;
255 }
Willy Tarreau23677902007-05-08 23:50:35 +0200256 else if ((s->proxy->options & PR_O_SMTP_CHK) && (len >= 3) &&
257 (reply[0] == '2')) /* 2xx (should be 250) */ {
258 result = 1;
259 }
Willy Tarreau83749182007-04-15 20:56:27 +0200260
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261 if (result == -1)
262 fdtab[fd].state = FD_STERROR;
263
264 if (s->result != -1)
265 s->result = result;
266
Willy Tarreau83749182007-04-15 20:56:27 +0200267 out_wakeup:
Willy Tarreauf161a342007-04-08 16:59:42 +0200268 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200269 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200270 fdtab[fd].ev &= ~FD_POLL_RD;
271 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272}
273
274/*
275 * manages a server health-check. Returns
276 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
277 */
278int process_chk(struct task *t)
279{
280 struct server *s = t->context;
281 struct sockaddr_in sa;
282 int fd;
283
284 //fprintf(stderr, "process_chk: task=%p\n", t);
285
286 new_chk:
287 fd = s->curfd;
288 if (fd < 0) { /* no check currently running */
289 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200290 if (!tv_ms_le2(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291 task_queue(t); /* restore t to its place in the task list */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200292 return tv_ms_remain2(&now, &t->expire);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 }
294
295 /* we don't send any health-checks when the proxy is stopped or when
296 * the server should not be checked.
297 */
298 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200299 while (tv_ms_le2(&t->expire, &now))
300 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 task_queue(t); /* restore t to its place in the task list */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200302 return tv_ms_remain2(&now, &t->expire);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303 }
304
305 /* we'll initiate a new check */
306 s->result = 0; /* no result yet */
307 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
308 if ((fd < global.maxsock) &&
309 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
310 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
311 //fprintf(stderr, "process_chk: 3\n");
312
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200313
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200314 if (s->check_addr.sin_addr.s_addr)
315 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200316 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200317 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200318 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200319 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200320
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322 sa.sin_port = htons(s->check_port);
323
324 /* allow specific binding :
325 * - server-specific at first
326 * - proxy-specific next
327 */
328 if (s->state & SRV_BIND_SRC) {
329 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
330 if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) {
331 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
332 s->proxy->id, s->id);
333 s->result = -1;
334 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100335#ifdef CONFIG_HAP_CTTPROXY
336 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_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) {
350 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
351 s->proxy->id, s->id);
352 s->result = -1;
353 }
354 }
355#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 }
357 else if (s->proxy->options & PR_O_BIND_SRC) {
358 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
359 if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100360 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
361 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 s->result = -1;
363 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100364#ifdef CONFIG_HAP_CTTPROXY
365 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
366 struct in_tproxy itp1, itp2;
367 memset(&itp1, 0, sizeof(itp1));
368
369 itp1.op = TPROXY_ASSIGN;
370 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
371 itp1.v.addr.fport = s->tproxy_addr.sin_port;
372
373 /* set connect flag on socket */
374 itp2.op = TPROXY_FLAGS;
375 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
376
377 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
378 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100379 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
380 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreau163c5322006-11-14 16:18:41 +0100381 s->result = -1;
382 }
383 }
384#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200385 }
386
387 if (!s->result) {
388 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
389 /* OK, connection in progress or established */
390
391 //fprintf(stderr, "process_chk: 4\n");
392
393 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200394 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200396 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
397 fdtab[fd].cb[DIR_RD].b = NULL;
398 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
399 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200400 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200401 fdtab[fd].ev = 0;
Willy Tarreauf161a342007-04-08 16:59:42 +0200402 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200404 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200407 tv_ms_add(&t->expire, &now, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408 task_queue(t); /* restore t to its place in the task list */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200409 return tv_ms_remain(&now, &t->expire);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410 }
411 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
412 s->result = -1; /* a real error */
413 }
414 }
415 }
416 close(fd); /* socket creation error */
417 }
418
419 if (!s->result) { /* nothing done */
420 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200421 while (tv_ms_le2(&t->expire, &now))
422 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423 goto new_chk; /* may be we should initialize a new check */
424 }
425
426 /* here, we have seen a failure */
427 if (s->health > s->rise) {
428 s->health--; /* still good */
429 s->failed_checks++;
430 }
431 else
432 set_server_down(s);
433
434 //fprintf(stderr, "process_chk: 7\n");
435 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200436 while (tv_ms_le2(&t->expire, &now))
437 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 goto new_chk;
439 }
440 else {
441 //fprintf(stderr, "process_chk: 8\n");
442 /* there was a test running */
443 if (s->result > 0) { /* good server detected */
444 //fprintf(stderr, "process_chk: 9\n");
445 s->health++; /* was bad, stays for a while */
446 if (s->health >= s->rise) {
447 s->state |= SRV_RUNNING;
448
449 if (s->health == s->rise) {
450 int xferred;
451
452 recount_servers(s->proxy);
453 recalc_server_map(s->proxy);
454
455 /* check if we can handle some connections queued at the proxy. We
456 * will take as many as we can handle.
457 */
458 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
459 struct session *sess;
460 struct pendconn *p;
461
462 p = pendconn_from_px(s->proxy);
463 if (!p)
464 break;
465 p->sess->srv = s;
466 sess = p->sess;
467 pendconn_free(p);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200468 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 }
470
471 sprintf(trash,
472 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
473 " %d sessions requeued, %d total in queue.\n",
474 s->state & SRV_BACKUP ? "Backup " : "",
475 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
476 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
477 xferred, s->nbpend);
478
479 Warning("%s", trash);
480 send_log(s->proxy, LOG_NOTICE, "%s", trash);
481 }
482
483 s->health = s->rise + s->fall - 1; /* OK now */
484 }
485 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200486 fd_delete(fd);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200487 while (tv_ms_le2(&t->expire, &now))
488 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489 goto new_chk;
490 }
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200491 else if (s->result < 0 || tv_ms_le2(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492 //fprintf(stderr, "process_chk: 10\n");
493 /* failure or timeout detected */
494 if (s->health > s->rise) {
495 s->health--; /* still good */
496 s->failed_checks++;
497 }
498 else
499 set_server_down(s);
500 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501 fd_delete(fd);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200502 while (tv_ms_le2(&t->expire, &now))
503 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504 goto new_chk;
505 }
506 /* if result is 0 and there's no timeout, we have to wait again */
507 }
508 //fprintf(stderr, "process_chk: 11\n");
509 s->result = 0;
510 task_queue(t); /* restore t to its place in the task list */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200511 return tv_ms_remain2(&now, &t->expire);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512}
513
514
515/*
516 * Local variables:
517 * c-indent-level: 8
518 * c-basic-offset: 8
519 * End:
520 */