blob: cd6bd3007a38a5f1a02783f6c88b3f7613330a18 [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>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020016#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020018#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <unistd.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23
Willy Tarreau2dd0d472006-06-29 17:53:05 +020024#include <common/compat.h>
25#include <common/config.h>
26#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020027#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
30#include <types/global.h>
31#include <types/polling.h>
32#include <types/proxy.h>
33#include <types/session.h>
34
35#include <proto/backend.h>
36#include <proto/fd.h>
37#include <proto/log.h>
38#include <proto/queue.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010039#include <proto/proto_http.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010040#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/server.h>
42#include <proto/task.h>
43
Willy Tarreau163c5322006-11-14 16:18:41 +010044#ifdef CONFIG_HAP_CTTPROXY
45#include <import/ip_tproxy.h>
46#endif
47
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
49/* Sets server <s> down, notifies by all available means, recounts the
50 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +020051 * possible to other servers. It automatically recomputes the number of
52 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +020053 */
Willy Tarreau83749182007-04-15 20:56:27 +020054static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020055{
56 struct pendconn *pc, *pc_bck, *pc_end;
57 struct session *sess;
58 int xferred;
59
60 s->state &= ~SRV_RUNNING;
61
62 if (s->health == s->rise) {
63 recount_servers(s->proxy);
Willy Tarreau5af3a692007-07-24 23:32:33 +020064 s->proxy->map_state |= PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
66 /* we might have sessions queued on this server and waiting for
67 * a connection. Those which are redispatchable will be queued
68 * to another server or to the proxy itself.
69 */
70 xferred = 0;
71 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
72 sess = pc->sess;
Willy Tarreaue2e27a52007-04-01 00:01:37 +020073 if ((sess->be->options & PR_O_REDISP)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 /* The REDISP option was specified. We will ignore
75 * cookie and force to balance or use the dispatcher.
76 */
77 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
78 sess->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreau3d300592007-03-18 18:34:41 +010079 http_flush_cookie_flags(&sess->txn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 pendconn_free(pc);
Willy Tarreau96bcfd72007-04-29 10:41:56 +020081 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 xferred++;
83 }
84 }
85
86 sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s"
87 " %d sessions active, %d requeued, %d remaining in queue.\n",
88 s->state & SRV_BACKUP ? "Backup " : "",
89 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
90 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
91 s->cur_sess, xferred, s->nbpend);
92
93 Warning("%s", trash);
94 send_log(s->proxy, LOG_ALERT, "%s", trash);
95
96 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +010097 Alert("%s '%s' has no server available !\n", proxy_type_str(s->proxy), s->proxy->id);
98 send_log(s->proxy, LOG_EMERG, "%s %s has no server available !\n", proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 }
100 s->down_trans++;
101 }
102 s->health = 0; /* failure */
103}
104
105
106/*
107 * This function is used only for server health-checks. It handles
108 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreau83749182007-04-15 20:56:27 +0200109 * it sends the request. In other cases, it returns 1 in s->result if the
110 * socket is OK, or -1 if an error occured.
111 * The function itself returns 0 if it needs some polling before being called
112 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 */
Willy Tarreau83749182007-04-15 20:56:27 +0200114static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115{
Willy Tarreau6996e152007-04-30 14:37:43 +0200116 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 struct task *t = fdtab[fd].owner;
118 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119
Willy Tarreau6996e152007-04-30 14:37:43 +0200120 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
121 goto out_error;
122
123 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200124
125 if (s->result != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200127 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200128 (s->proxy->options & PR_O_SSL3_CHK) ||
129 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200131 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132 * so we'll send the request, and won't wake the checker up now.
133 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200134
135 if (s->proxy->options & PR_O_SSL3_CHK) {
136 /* SSL requires that we put Unix time in the request */
137 int gmt_time = htonl(now.tv_sec);
138 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
139 }
140
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141#ifndef MSG_NOSIGNAL
142 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
143#else
144 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
145#endif
146 if (ret == s->proxy->check_len) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200147 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200148 goto out_nowake;
149 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200150 else if (ret == 0 || errno == EAGAIN)
151 goto out_poll;
152 else
153 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154 }
155 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200156 /* We have no data to send to check the connection, and
157 * getsockopt() will not inform us whether the connection
158 * is still pending. So we'll reuse connect() to check the
159 * state of the socket. This has the advantage of givig us
160 * the following info :
161 * - error
162 * - connecting (EALREADY, EINPROGRESS)
163 * - connected (EISCONN, 0)
164 */
165
166 struct sockaddr_in sa;
167
168 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
169 sa.sin_port = htons(s->check_port);
170
171 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
172 errno = 0;
173
174 if (errno == EALREADY || errno == EINPROGRESS)
175 goto out_poll;
176
177 if (errno && errno != EISCONN)
178 goto out_error;
179
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 /* good TCP connection is enough */
181 s->result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200182 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 }
184 }
Willy Tarreau83749182007-04-15 20:56:27 +0200185 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200186 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200187 out_nowake:
188 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
189 fdtab[fd].ev &= ~FD_POLL_WR;
190 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200191 out_poll:
192 /* The connection is still pending. We'll have to poll it
193 * before attempting to go further. */
194 fdtab[fd].ev &= ~FD_POLL_WR;
195 return 0;
196 out_error:
197 s->result = -1;
198 fdtab[fd].state = FD_STERROR;
199 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200}
201
202
203/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200204 * This function is used only for server health-checks. It handles the server's
205 * reply to an HTTP request or SSL HELLO. It returns 1 in s->result if the
206 * server replies HTTP 2xx or 3xx (valid responses), or if it returns at least
207 * 5 bytes in response to SSL HELLO. The principle is that this is enough to
208 * distinguish between an SSL server and a pure TCP relay. All other cases will
Willy Tarreau83749182007-04-15 20:56:27 +0200209 * return -1. The function returns 0 if it needs to be called again after some
210 * polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200211 */
Willy Tarreau83749182007-04-15 20:56:27 +0200212static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213{
Willy Tarreau83749182007-04-15 20:56:27 +0200214 __label__ out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 int len, result;
216 struct task *t = fdtab[fd].owner;
217 struct server *s = t->context;
218 int skerr;
219 socklen_t lskerr = sizeof(skerr);
220
221 result = len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200222
223 if (unlikely(fdtab[fd].state == FD_STERROR ||
224 (fdtab[fd].ev & FD_POLL_ERR) ||
225 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
226 (skerr != 0))) {
227 /* in case of TCP only, this tells us if the connection failed */
228 s->result = -1;
229 fdtab[fd].state = FD_STERROR;
230 goto out_wakeup;
231 }
232
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200234 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235#else
Willy Tarreau83749182007-04-15 20:56:27 +0200236 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
237 * but the connection was closed on the remote end. Fortunately, recv still
238 * works correctly and we don't need to do the getsockopt() on linux.
239 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200240 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200242 if (unlikely(len < 0 && errno == EAGAIN)) {
243 /* we want some polling to happen first */
244 fdtab[fd].ev &= ~FD_POLL_RD;
245 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200246 }
247
Willy Tarreau6996e152007-04-30 14:37:43 +0200248 if ((s->proxy->options & PR_O_HTTP_CHK) && (len >= sizeof("HTTP/1.0 000")) &&
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200249 (memcmp(trash, "HTTP/1.", 7) == 0) && (trash[9] == '2' || trash[9] == '3')) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200250 /* HTTP/1.X 2xx or 3xx */
Willy Tarreau83749182007-04-15 20:56:27 +0200251 result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200252 }
253 else if ((s->proxy->options & PR_O_SSL3_CHK) && (len >= 5) &&
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200254 (trash[0] == 0x15 || trash[0] == 0x16)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200255 /* SSLv3 alert or handshake */
256 result = 1;
257 }
Willy Tarreau23677902007-05-08 23:50:35 +0200258 else if ((s->proxy->options & PR_O_SMTP_CHK) && (len >= 3) &&
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200259 (trash[0] == '2')) /* 2xx (should be 250) */ {
Willy Tarreau23677902007-05-08 23:50:35 +0200260 result = 1;
261 }
Willy Tarreau83749182007-04-15 20:56:27 +0200262
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263 if (result == -1)
264 fdtab[fd].state = FD_STERROR;
265
266 if (s->result != -1)
267 s->result = result;
268
Willy Tarreau83749182007-04-15 20:56:27 +0200269 out_wakeup:
Willy Tarreauf161a342007-04-08 16:59:42 +0200270 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200271 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200272 fdtab[fd].ev &= ~FD_POLL_RD;
273 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274}
275
276/*
277 * manages a server health-check. Returns
278 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
279 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200280void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200282 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283 struct server *s = t->context;
284 struct sockaddr_in sa;
285 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200286 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287
288 //fprintf(stderr, "process_chk: task=%p\n", t);
289
290 new_chk:
291 fd = s->curfd;
292 if (fd < 0) { /* no check currently running */
293 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200294 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200296 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200297 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 }
299
300 /* we don't send any health-checks when the proxy is stopped or when
301 * the server should not be checked.
302 */
303 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200304 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200305 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200307 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200308 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309 }
310
311 /* we'll initiate a new check */
312 s->result = 0; /* no result yet */
313 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
314 if ((fd < global.maxsock) &&
315 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
316 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
317 //fprintf(stderr, "process_chk: 3\n");
318
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200319
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200320 if (s->check_addr.sin_addr.s_addr)
321 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200322 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200323 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200324 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200325 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200326
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 sa.sin_port = htons(s->check_port);
329
330 /* allow specific binding :
331 * - server-specific at first
332 * - proxy-specific next
333 */
334 if (s->state & SRV_BIND_SRC) {
335 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
336 if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) {
337 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
338 s->proxy->id, s->id);
339 s->result = -1;
340 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100341#ifdef CONFIG_HAP_CTTPROXY
342 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
343 struct in_tproxy itp1, itp2;
344 memset(&itp1, 0, sizeof(itp1));
345
346 itp1.op = TPROXY_ASSIGN;
347 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
348 itp1.v.addr.fport = s->tproxy_addr.sin_port;
349
350 /* set connect flag on socket */
351 itp2.op = TPROXY_FLAGS;
352 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
353
354 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
355 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
356 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
357 s->proxy->id, s->id);
358 s->result = -1;
359 }
360 }
361#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 }
363 else if (s->proxy->options & PR_O_BIND_SRC) {
364 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
365 if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100366 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
367 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 s->result = -1;
369 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100370#ifdef CONFIG_HAP_CTTPROXY
371 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
372 struct in_tproxy itp1, itp2;
373 memset(&itp1, 0, sizeof(itp1));
374
375 itp1.op = TPROXY_ASSIGN;
376 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
377 itp1.v.addr.fport = s->tproxy_addr.sin_port;
378
379 /* set connect flag on socket */
380 itp2.op = TPROXY_FLAGS;
381 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
382
383 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
384 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100385 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
386 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreau163c5322006-11-14 16:18:41 +0100387 s->result = -1;
388 }
389 }
390#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391 }
392
393 if (!s->result) {
394 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
395 /* OK, connection in progress or established */
396
397 //fprintf(stderr, "process_chk: 4\n");
398
399 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200400 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200402 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
403 fdtab[fd].cb[DIR_RD].b = NULL;
404 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
405 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200407 fdtab[fd].ev = 0;
Willy Tarreauf161a342007-04-08 16:59:42 +0200408 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200410 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200413 tv_ms_add(&t->expire, &now, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200415 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200416 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417 }
418 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
419 s->result = -1; /* a real error */
420 }
421 }
422 }
423 close(fd); /* socket creation error */
424 }
425
426 if (!s->result) { /* nothing done */
427 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200428 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200429 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430 goto new_chk; /* may be we should initialize a new check */
431 }
432
433 /* here, we have seen a failure */
434 if (s->health > s->rise) {
435 s->health--; /* still good */
436 s->failed_checks++;
437 }
438 else
439 set_server_down(s);
440
441 //fprintf(stderr, "process_chk: 7\n");
442 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200443 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200444 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 goto new_chk;
446 }
447 else {
448 //fprintf(stderr, "process_chk: 8\n");
449 /* there was a test running */
450 if (s->result > 0) { /* good server detected */
451 //fprintf(stderr, "process_chk: 9\n");
452 s->health++; /* was bad, stays for a while */
453 if (s->health >= s->rise) {
454 s->state |= SRV_RUNNING;
455
456 if (s->health == s->rise) {
457 int xferred;
458
459 recount_servers(s->proxy);
Willy Tarreau5af3a692007-07-24 23:32:33 +0200460 s->proxy->map_state |= PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461
462 /* check if we can handle some connections queued at the proxy. We
463 * will take as many as we can handle.
464 */
465 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
466 struct session *sess;
467 struct pendconn *p;
468
469 p = pendconn_from_px(s->proxy);
470 if (!p)
471 break;
472 p->sess->srv = s;
473 sess = p->sess;
474 pendconn_free(p);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200475 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 }
477
478 sprintf(trash,
479 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
480 " %d sessions requeued, %d total in queue.\n",
481 s->state & SRV_BACKUP ? "Backup " : "",
482 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
483 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
484 xferred, s->nbpend);
485
486 Warning("%s", trash);
487 send_log(s->proxy, LOG_NOTICE, "%s", trash);
488 }
489
490 s->health = s->rise + s->fall - 1; /* OK now */
491 }
492 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200494
495 rv = 0;
496 if (global.spread_checks > 0) {
497 rv = s->inter * global.spread_checks / 100;
498 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
499 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
500 }
501 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200502 goto new_chk;
503 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200504 else if (s->result < 0 || tv_isle(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505 //fprintf(stderr, "process_chk: 10\n");
506 /* failure or timeout detected */
507 if (s->health > s->rise) {
508 s->health--; /* still good */
509 s->failed_checks++;
510 }
511 else
512 set_server_down(s);
513 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200514 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200515
516 rv = 0;
517 if (global.spread_checks > 0) {
518 rv = s->inter * global.spread_checks / 100;
519 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200520 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200521 }
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200522 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523 goto new_chk;
524 }
525 /* if result is 0 and there's no timeout, we have to wait again */
526 }
527 //fprintf(stderr, "process_chk: 11\n");
528 s->result = 0;
529 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200530 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200531 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200532 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533}
534
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200535/*
536 * Start health-check.
537 * Returns 0 if OK, -1 if error, and prints the error in this case.
538 */
539int start_checks() {
540
541 struct proxy *px;
542 struct server *s;
543 struct task *t;
544 int nbchk=0, mininter=0, srvpos=0;
545
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200546 /* 1- count the checkers to run simultaneously.
547 * We also determine the minimum interval among all of those which
548 * have an interval larger than SRV_CHK_INTER_THRES. This interval
549 * will be used to spread their start-up date. Those which have
550 * a shorter interval will start independantly and will not dictate
551 * too short an interval for all others.
552 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200553 for (px = proxy; px; px = px->next) {
554 for (s = px->srv; s; s = s->next) {
555 if (!(s->state & SRV_CHECKED))
556 continue;
557
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200558 if ((s->inter >= SRV_CHK_INTER_THRES) &&
559 (!mininter || mininter > s->inter))
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200560 mininter = s->inter;
561
562 nbchk++;
563 }
564 }
565
566 if (!nbchk)
567 return 0;
568
569 srand((unsigned)time(NULL));
570
571 /*
572 * 2- start them as far as possible from each others. For this, we will
573 * start them after their interval set to the min interval divided by
574 * the number of servers, weighted by the server's position in the list.
575 */
576 for (px = proxy; px; px = px->next) {
577 for (s = px->srv; s; s = s->next) {
578 if (!(s->state & SRV_CHECKED))
579 continue;
580
581 if ((t = pool_alloc2(pool2_task)) == NULL) {
582 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
583 return -1;
584 }
585
586 t->wq = NULL;
587 t->qlist.p = NULL;
588 t->state = TASK_IDLE;
589 t->process = process_chk;
590 t->context = s;
591
592 /* check this every ms */
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200593 tv_ms_add(&t->expire, &now,
594 ((mininter && mininter >= s->inter) ? mininter : s->inter) * srvpos / nbchk);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200595 task_queue(t);
596
597 srvpos++;
598 }
599 }
600 return 0;
601}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602
603/*
604 * Local variables:
605 * c-indent-level: 8
606 * c-basic-offset: 8
607 * End:
608 */