blob: 5fe98ab286c3374ce87a25d6ea6833cd4c567bd2 [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
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 if (s->health == s->rise) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061
62 s->last_change = now.tv_sec;
63 s->state &= ~SRV_RUNNING;
Willy Tarreaub625a082007-11-26 01:15:43 +010064 s->proxy->lbprm.set_server_status_down(s);
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);
Krzysztof Oledzki85130942007-10-22 16:21:10 +020095
Willy Tarreaubaaee002006-06-26 02:48:02 +020096 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +020097 s->proxy->last_change = now.tv_sec;
98 s->proxy->down_trans++;
99
100 Alert("%s '%s' has no server available!\n", proxy_type_str(s->proxy), s->proxy->id);
101 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 +0200102 }
103 s->down_trans++;
104 }
105 s->health = 0; /* failure */
106}
107
108
109/*
110 * This function is used only for server health-checks. It handles
111 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreau83749182007-04-15 20:56:27 +0200112 * it sends the request. In other cases, it returns 1 in s->result if the
113 * socket is OK, or -1 if an error occured.
114 * The function itself returns 0 if it needs some polling before being called
115 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 */
Willy Tarreau83749182007-04-15 20:56:27 +0200117static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118{
Willy Tarreau6996e152007-04-30 14:37:43 +0200119 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 struct task *t = fdtab[fd].owner;
121 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122
Willy Tarreau6996e152007-04-30 14:37:43 +0200123 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
124 goto out_error;
125
126 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200127
128 if (s->result != -1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200130 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200131 (s->proxy->options & PR_O_SSL3_CHK) ||
132 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200134 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135 * so we'll send the request, and won't wake the checker up now.
136 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200137
138 if (s->proxy->options & PR_O_SSL3_CHK) {
139 /* SSL requires that we put Unix time in the request */
140 int gmt_time = htonl(now.tv_sec);
141 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
142 }
143
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144#ifndef MSG_NOSIGNAL
145 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
146#else
147 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
148#endif
149 if (ret == s->proxy->check_len) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200150 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200151 goto out_nowake;
152 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200153 else if (ret == 0 || errno == EAGAIN)
154 goto out_poll;
155 else
156 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200157 }
158 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200159 /* We have no data to send to check the connection, and
160 * getsockopt() will not inform us whether the connection
161 * is still pending. So we'll reuse connect() to check the
162 * state of the socket. This has the advantage of givig us
163 * the following info :
164 * - error
165 * - connecting (EALREADY, EINPROGRESS)
166 * - connected (EISCONN, 0)
167 */
168
169 struct sockaddr_in sa;
170
171 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
172 sa.sin_port = htons(s->check_port);
173
174 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
175 errno = 0;
176
177 if (errno == EALREADY || errno == EINPROGRESS)
178 goto out_poll;
179
180 if (errno && errno != EISCONN)
181 goto out_error;
182
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 /* good TCP connection is enough */
184 s->result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200185 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186 }
187 }
Willy Tarreau83749182007-04-15 20:56:27 +0200188 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200189 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200190 out_nowake:
191 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
192 fdtab[fd].ev &= ~FD_POLL_WR;
193 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200194 out_poll:
195 /* The connection is still pending. We'll have to poll it
196 * before attempting to go further. */
197 fdtab[fd].ev &= ~FD_POLL_WR;
198 return 0;
199 out_error:
200 s->result = -1;
201 fdtab[fd].state = FD_STERROR;
202 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203}
204
205
206/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200207 * This function is used only for server health-checks. It handles the server's
208 * reply to an HTTP request or SSL HELLO. It returns 1 in s->result if the
209 * server replies HTTP 2xx or 3xx (valid responses), or if it returns at least
210 * 5 bytes in response to SSL HELLO. The principle is that this is enough to
211 * distinguish between an SSL server and a pure TCP relay. All other cases will
Willy Tarreau83749182007-04-15 20:56:27 +0200212 * return -1. The function returns 0 if it needs to be called again after some
213 * polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200214 */
Willy Tarreau83749182007-04-15 20:56:27 +0200215static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200216{
Willy Tarreau83749182007-04-15 20:56:27 +0200217 __label__ out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 int len, result;
219 struct task *t = fdtab[fd].owner;
220 struct server *s = t->context;
221 int skerr;
222 socklen_t lskerr = sizeof(skerr);
223
224 result = len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200225
226 if (unlikely(fdtab[fd].state == FD_STERROR ||
227 (fdtab[fd].ev & FD_POLL_ERR) ||
228 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
229 (skerr != 0))) {
230 /* in case of TCP only, this tells us if the connection failed */
231 s->result = -1;
232 fdtab[fd].state = FD_STERROR;
233 goto out_wakeup;
234 }
235
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200237 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238#else
Willy Tarreau83749182007-04-15 20:56:27 +0200239 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
240 * but the connection was closed on the remote end. Fortunately, recv still
241 * works correctly and we don't need to do the getsockopt() on linux.
242 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200243 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200245 if (unlikely(len < 0 && errno == EAGAIN)) {
246 /* we want some polling to happen first */
247 fdtab[fd].ev &= ~FD_POLL_RD;
248 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249 }
250
Willy Tarreau6996e152007-04-30 14:37:43 +0200251 if ((s->proxy->options & PR_O_HTTP_CHK) && (len >= sizeof("HTTP/1.0 000")) &&
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200252 (memcmp(trash, "HTTP/1.", 7) == 0) && (trash[9] == '2' || trash[9] == '3')) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200253 /* HTTP/1.X 2xx or 3xx */
Willy Tarreau83749182007-04-15 20:56:27 +0200254 result = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200255 }
256 else if ((s->proxy->options & PR_O_SSL3_CHK) && (len >= 5) &&
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200257 (trash[0] == 0x15 || trash[0] == 0x16)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200258 /* SSLv3 alert or handshake */
259 result = 1;
260 }
Willy Tarreau23677902007-05-08 23:50:35 +0200261 else if ((s->proxy->options & PR_O_SMTP_CHK) && (len >= 3) &&
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200262 (trash[0] == '2')) /* 2xx (should be 250) */ {
Willy Tarreau23677902007-05-08 23:50:35 +0200263 result = 1;
264 }
Willy Tarreau83749182007-04-15 20:56:27 +0200265
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266 if (result == -1)
267 fdtab[fd].state = FD_STERROR;
268
269 if (s->result != -1)
270 s->result = result;
271
Willy Tarreau83749182007-04-15 20:56:27 +0200272 out_wakeup:
Willy Tarreauf161a342007-04-08 16:59:42 +0200273 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200274 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200275 fdtab[fd].ev &= ~FD_POLL_RD;
276 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277}
278
279/*
280 * manages a server health-check. Returns
281 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
282 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200283void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200284{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200285 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 struct server *s = t->context;
287 struct sockaddr_in sa;
288 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200289 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290
291 //fprintf(stderr, "process_chk: task=%p\n", t);
292
293 new_chk:
294 fd = s->curfd;
295 if (fd < 0) { /* no check currently running */
296 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200297 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200299 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200300 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 }
302
303 /* we don't send any health-checks when the proxy is stopped or when
304 * the server should not be checked.
305 */
306 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200307 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200308 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200310 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200311 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 }
313
314 /* we'll initiate a new check */
315 s->result = 0; /* no result yet */
316 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
317 if ((fd < global.maxsock) &&
318 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
319 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
320 //fprintf(stderr, "process_chk: 3\n");
321
Willy Tarreau9edd1612007-10-18 18:07:48 +0200322 if (s->proxy->options & PR_O_TCP_NOLING) {
323 /* We don't want to useless data */
324 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
325 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200326
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200327 if (s->check_addr.sin_addr.s_addr)
328 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200329 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200330 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200331 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200332 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200333
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 sa.sin_port = htons(s->check_port);
336
337 /* allow specific binding :
338 * - server-specific at first
339 * - proxy-specific next
340 */
341 if (s->state & SRV_BIND_SRC) {
342 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
343 if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) {
344 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
345 s->proxy->id, s->id);
346 s->result = -1;
347 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100348#ifdef CONFIG_HAP_CTTPROXY
349 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
350 struct in_tproxy itp1, itp2;
351 memset(&itp1, 0, sizeof(itp1));
352
353 itp1.op = TPROXY_ASSIGN;
354 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
355 itp1.v.addr.fport = s->tproxy_addr.sin_port;
356
357 /* set connect flag on socket */
358 itp2.op = TPROXY_FLAGS;
359 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
360
361 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
362 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
363 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
364 s->proxy->id, s->id);
365 s->result = -1;
366 }
367 }
368#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369 }
370 else if (s->proxy->options & PR_O_BIND_SRC) {
371 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
372 if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100373 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
374 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375 s->result = -1;
376 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100377#ifdef CONFIG_HAP_CTTPROXY
378 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
379 struct in_tproxy itp1, itp2;
380 memset(&itp1, 0, sizeof(itp1));
381
382 itp1.op = TPROXY_ASSIGN;
383 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
384 itp1.v.addr.fport = s->tproxy_addr.sin_port;
385
386 /* set connect flag on socket */
387 itp2.op = TPROXY_FLAGS;
388 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
389
390 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
391 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100392 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
393 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreau163c5322006-11-14 16:18:41 +0100394 s->result = -1;
395 }
396 }
397#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398 }
399
400 if (!s->result) {
401 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
402 /* OK, connection in progress or established */
403
404 //fprintf(stderr, "process_chk: 4\n");
405
406 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200407 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200409 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
410 fdtab[fd].cb[DIR_RD].b = NULL;
411 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
412 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200413 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
414 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200416 fdtab[fd].ev = 0;
Willy Tarreauf161a342007-04-08 16:59:42 +0200417 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200419 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200421 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200422 tv_ms_add(&t->expire, &now, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200424 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200425 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426 }
427 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
428 s->result = -1; /* a real error */
429 }
430 }
431 }
432 close(fd); /* socket creation error */
433 }
434
435 if (!s->result) { /* nothing done */
436 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200437 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200438 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 goto new_chk; /* may be we should initialize a new check */
440 }
441
442 /* here, we have seen a failure */
443 if (s->health > s->rise) {
444 s->health--; /* still good */
445 s->failed_checks++;
446 }
447 else
448 set_server_down(s);
449
450 //fprintf(stderr, "process_chk: 7\n");
451 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200452 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200453 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454 goto new_chk;
455 }
456 else {
457 //fprintf(stderr, "process_chk: 8\n");
458 /* there was a test running */
459 if (s->result > 0) { /* good server detected */
460 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200461
462 if (s->health < s->rise + s->fall - 1) {
463 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
465 if (s->health == s->rise) {
466 int xferred;
467
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200468 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
469 if (s->proxy->last_change < now.tv_sec) // ignore negative times
470 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
471 s->proxy->last_change = now.tv_sec;
472 }
473
Willy Tarreaub625a082007-11-26 01:15:43 +0100474 if (s->last_change < now.tv_sec) // ignore negative times
475 s->down_time += now.tv_sec - s->last_change;
476
477 s->last_change = now.tv_sec;
478 s->state |= SRV_RUNNING;
479 s->proxy->lbprm.set_server_status_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480
481 /* check if we can handle some connections queued at the proxy. We
482 * will take as many as we can handle.
483 */
484 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
485 struct session *sess;
486 struct pendconn *p;
487
488 p = pendconn_from_px(s->proxy);
489 if (!p)
490 break;
491 p->sess->srv = s;
492 sess = p->sess;
493 pendconn_free(p);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200494 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495 }
496
497 sprintf(trash,
498 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
499 " %d sessions requeued, %d total in queue.\n",
500 s->state & SRV_BACKUP ? "Backup " : "",
501 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
502 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
503 xferred, s->nbpend);
504
505 Warning("%s", trash);
506 send_log(s->proxy, LOG_NOTICE, "%s", trash);
507 }
508
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200509 if (s->health >= s->rise)
510 s->health = s->rise + s->fall - 1; /* OK now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511 }
512 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200514
515 rv = 0;
516 if (global.spread_checks > 0) {
517 rv = s->inter * global.spread_checks / 100;
518 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
519 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
520 }
521 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522 goto new_chk;
523 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200524 else if (s->result < 0 || tv_isle(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200525 //fprintf(stderr, "process_chk: 10\n");
526 /* failure or timeout detected */
527 if (s->health > s->rise) {
528 s->health--; /* still good */
529 s->failed_checks++;
530 }
531 else
532 set_server_down(s);
533 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200534 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200535
536 rv = 0;
537 if (global.spread_checks > 0) {
538 rv = s->inter * global.spread_checks / 100;
539 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200540 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200541 }
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200542 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 goto new_chk;
544 }
545 /* if result is 0 and there's no timeout, we have to wait again */
546 }
547 //fprintf(stderr, "process_chk: 11\n");
548 s->result = 0;
549 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200550 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200551 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200552 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553}
554
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200555/*
556 * Start health-check.
557 * Returns 0 if OK, -1 if error, and prints the error in this case.
558 */
559int start_checks() {
560
561 struct proxy *px;
562 struct server *s;
563 struct task *t;
564 int nbchk=0, mininter=0, srvpos=0;
565
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200566 /* 1- count the checkers to run simultaneously.
567 * We also determine the minimum interval among all of those which
568 * have an interval larger than SRV_CHK_INTER_THRES. This interval
569 * will be used to spread their start-up date. Those which have
570 * a shorter interval will start independantly and will not dictate
571 * too short an interval for all others.
572 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200573 for (px = proxy; px; px = px->next) {
574 for (s = px->srv; s; s = s->next) {
575 if (!(s->state & SRV_CHECKED))
576 continue;
577
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200578 if ((s->inter >= SRV_CHK_INTER_THRES) &&
579 (!mininter || mininter > s->inter))
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200580 mininter = s->inter;
581
582 nbchk++;
583 }
584 }
585
586 if (!nbchk)
587 return 0;
588
589 srand((unsigned)time(NULL));
590
591 /*
592 * 2- start them as far as possible from each others. For this, we will
593 * start them after their interval set to the min interval divided by
594 * the number of servers, weighted by the server's position in the list.
595 */
596 for (px = proxy; px; px = px->next) {
597 for (s = px->srv; s; s = s->next) {
598 if (!(s->state & SRV_CHECKED))
599 continue;
600
601 if ((t = pool_alloc2(pool2_task)) == NULL) {
602 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
603 return -1;
604 }
605
606 t->wq = NULL;
607 t->qlist.p = NULL;
608 t->state = TASK_IDLE;
609 t->process = process_chk;
610 t->context = s;
611
612 /* check this every ms */
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200613 tv_ms_add(&t->expire, &now,
614 ((mininter && mininter >= s->inter) ? mininter : s->inter) * srvpos / nbchk);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200615 task_queue(t);
616
617 srvpos++;
618 }
619 }
620 return 0;
621}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622
623/*
624 * Local variables:
625 * c-indent-level: 8
626 * c-basic-offset: 8
627 * End:
628 */