blob: 0fbe166789bc7d42d76ba12cd65ae88bac642e1d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreaue8c66af2008-01-13 18:40:14 +01004 * Copyright 2000-2008 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
Willy Tarreaub8816082008-01-18 12:18:15 +010013#include <assert.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020017#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020018#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <unistd.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/compat.h>
26#include <common/config.h>
27#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020028#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
31#include <types/global.h>
32#include <types/polling.h>
33#include <types/proxy.h>
34#include <types/session.h>
35
36#include <proto/backend.h>
37#include <proto/fd.h>
38#include <proto/log.h>
39#include <proto/queue.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010040#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010041#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010042#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/server.h>
44#include <proto/task.h>
45
Willy Tarreau48494c02007-11-30 10:41:39 +010046/* sends a log message when a backend goes down, and also sets last
47 * change date.
48 */
49static void set_backend_down(struct proxy *be)
50{
51 be->last_change = now.tv_sec;
52 be->down_trans++;
53
54 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
55 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
56}
57
58/* Redistribute pending connections when a server goes down. The number of
59 * connections redistributed is returned.
60 */
61static int redistribute_pending(struct server *s)
62{
63 struct pendconn *pc, *pc_bck, *pc_end;
64 int xferred = 0;
65
66 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
67 struct session *sess = pc->sess;
68 if (sess->be->options & PR_O_REDISP) {
69 /* The REDISP option was specified. We will ignore
70 * cookie and force to balance or use the dispatcher.
71 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010072
73 sess->srv->redispatches++;
74 sess->be->redispatches++;
75
Willy Tarreau48494c02007-11-30 10:41:39 +010076 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010077 sess->flags |= SN_REDISP;
78
Willy Tarreau48494c02007-11-30 10:41:39 +010079 sess->srv = NULL; /* it's left to the dispatcher to choose a server */
80 http_flush_cookie_flags(&sess->txn);
81 pendconn_free(pc);
82 task_wakeup(sess->task);
83 xferred++;
84 }
85 }
86 return xferred;
87}
88
89/* Check for pending connections at the backend, and assign some of them to
90 * the server coming up. The server's weight is checked before being assigned
91 * connections it may not be able to handle. The total number of transferred
92 * connections is returned.
93 */
94static int check_for_pending(struct server *s)
95{
96 int xferred;
97
98 if (!s->eweight)
99 return 0;
100
101 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
102 struct session *sess;
103 struct pendconn *p;
104
105 p = pendconn_from_px(s->proxy);
106 if (!p)
107 break;
108 p->sess->srv = s;
109 sess = p->sess;
110 pendconn_free(p);
111 task_wakeup(sess->task);
112 }
113 return xferred;
114}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
116/* Sets server <s> down, notifies by all available means, recounts the
117 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200118 * possible to other servers. It automatically recomputes the number of
119 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 */
Willy Tarreau83749182007-04-15 20:56:27 +0200121static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122{
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123 int xferred;
124
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 if (s->health == s->rise) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100126 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200127
128 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100129 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100130 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131
132 /* we might have sessions queued on this server and waiting for
133 * a connection. Those which are redispatchable will be queued
134 * to another server or to the proxy itself.
135 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100136 xferred = redistribute_pending(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137 sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s"
138 " %d sessions active, %d requeued, %d remaining in queue.\n",
139 s->state & SRV_BACKUP ? "Backup " : "",
140 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
141 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
142 s->cur_sess, xferred, s->nbpend);
143
144 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200145
Willy Tarreau48494c02007-11-30 10:41:39 +0100146 /* we don't send an alert if the server was previously paused */
147 if (srv_was_paused)
148 send_log(s->proxy, LOG_NOTICE, "%s", trash);
149 else
150 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200151
Willy Tarreau48494c02007-11-30 10:41:39 +0100152 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
153 set_backend_down(s->proxy);
154
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155 s->down_trans++;
156 }
157 s->health = 0; /* failure */
158}
159
160
161/*
162 * This function is used only for server health-checks. It handles
163 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100164 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200165 * The function itself returns 0 if it needs some polling before being called
166 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167 */
Willy Tarreau83749182007-04-15 20:56:27 +0200168static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169{
Willy Tarreau6996e152007-04-30 14:37:43 +0200170 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171 struct task *t = fdtab[fd].owner;
172 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100174 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Willy Tarreau6996e152007-04-30 14:37:43 +0200175 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
176 goto out_error;
177
178 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200179
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100180 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200182 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200183 (s->proxy->options & PR_O_SSL3_CHK) ||
184 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200186 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 * so we'll send the request, and won't wake the checker up now.
188 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200189
190 if (s->proxy->options & PR_O_SSL3_CHK) {
191 /* SSL requires that we put Unix time in the request */
192 int gmt_time = htonl(now.tv_sec);
193 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
194 }
195
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196#ifndef MSG_NOSIGNAL
197 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
198#else
199 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
200#endif
201 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100202 /* we allow up to <timeout.check> if nonzero for a responce */
203 //fprintf(stderr, "event_srv_chk_w, ms=%lu\n", __tv_to_ms(&s->proxy->timeout.check));
204 tv_add_ifset(&t->expire, &now, &s->proxy->timeout.check);
205
Willy Tarreauf161a342007-04-08 16:59:42 +0200206 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200207 goto out_nowake;
208 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200209 else if (ret == 0 || errno == EAGAIN)
210 goto out_poll;
211 else
212 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213 }
214 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200215 /* We have no data to send to check the connection, and
216 * getsockopt() will not inform us whether the connection
217 * is still pending. So we'll reuse connect() to check the
218 * state of the socket. This has the advantage of givig us
219 * the following info :
220 * - error
221 * - connecting (EALREADY, EINPROGRESS)
222 * - connected (EISCONN, 0)
223 */
224
225 struct sockaddr_in sa;
226
227 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
228 sa.sin_port = htons(s->check_port);
229
230 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
231 errno = 0;
232
233 if (errno == EALREADY || errno == EINPROGRESS)
234 goto out_poll;
235
236 if (errno && errno != EISCONN)
237 goto out_error;
238
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100240 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200241 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242 }
243 }
Willy Tarreau83749182007-04-15 20:56:27 +0200244 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200245 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200246 out_nowake:
247 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100248 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200249 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200250 out_poll:
251 /* The connection is still pending. We'll have to poll it
252 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100253 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200254 return 0;
255 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100256 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200257 fdtab[fd].state = FD_STERROR;
258 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259}
260
261
262/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200263 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100264 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
265 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
266 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
267 * to an SSL HELLO (the principle is that this is enough to distinguish between
268 * an SSL server and a pure TCP relay). All other cases will set s->result to
269 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
270 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200271 */
Willy Tarreau83749182007-04-15 20:56:27 +0200272static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273{
Willy Tarreau83749182007-04-15 20:56:27 +0200274 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100275 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200276 struct task *t = fdtab[fd].owner;
277 struct server *s = t->context;
278 int skerr;
279 socklen_t lskerr = sizeof(skerr);
280
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100281 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200282
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100283 if (unlikely((s->result & SRV_CHK_ERROR) ||
284 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200285 (fdtab[fd].ev & FD_POLL_ERR) ||
286 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
287 (skerr != 0))) {
288 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100289 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200290 goto out_wakeup;
291 }
292
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200294 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295#else
Willy Tarreau83749182007-04-15 20:56:27 +0200296 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
297 * but the connection was closed on the remote end. Fortunately, recv still
298 * works correctly and we don't need to do the getsockopt() on linux.
299 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200300 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200302 if (unlikely(len < 0 && errno == EAGAIN)) {
303 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100304 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200305 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306 }
307
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100308 /* Note: the response will only be accepted if read at once */
309 if (s->proxy->options & PR_O_HTTP_CHK) {
310 /* Check if the server speaks HTTP 1.X */
311 if ((len < strlen("HTTP/1.0 000\r")) ||
312 (memcmp(trash, "HTTP/1.", 7) != 0)) {
313 s->result |= SRV_CHK_ERROR;
314 goto out_wakeup;
315 }
316
317 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
318 if (trash[9] == '2' || trash[9] == '3')
319 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100320 else if ((s->proxy->options & PR_O_DISABLE404) &&
321 (s->state & SRV_RUNNING) &&
322 (memcmp(&trash[9], "404", 3) == 0)) {
323 /* 404 may be accepted as "stopping" only if the server was up */
324 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
325 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100326 else
327 s->result |= SRV_CHK_ERROR;
328 }
329 else if (s->proxy->options & PR_O_SSL3_CHK) {
330 /* Check for SSLv3 alert or handshake */
331 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
332 s->result |= SRV_CHK_RUNNING;
333 else
334 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200335 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100336 else if (s->proxy->options & PR_O_SMTP_CHK) {
337 /* Check for SMTP code 2xx (should be 250) */
338 if ((len >= 3) && (trash[0] == '2'))
339 s->result |= SRV_CHK_RUNNING;
340 else
341 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200342 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100343 else {
344 /* other checks are valid if the connection succeeded anyway */
345 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200346 }
Willy Tarreau83749182007-04-15 20:56:27 +0200347
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100348 out_wakeup:
349 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350 fdtab[fd].state = FD_STERROR;
351
Willy Tarreauf161a342007-04-08 16:59:42 +0200352 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200353 task_wakeup(t);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100354 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200355 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356}
357
358/*
359 * manages a server health-check. Returns
360 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
361 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200362void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200364 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 struct server *s = t->context;
366 struct sockaddr_in sa;
Willy Tarreau48494c02007-11-30 10:41:39 +0100367 int xferred;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200369 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370
371 //fprintf(stderr, "process_chk: task=%p\n", t);
372
373 new_chk:
374 fd = s->curfd;
375 if (fd < 0) { /* no check currently running */
376 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200377 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200379 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200380 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381 }
382
383 /* we don't send any health-checks when the proxy is stopped or when
384 * the server should not be checked.
385 */
386 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200387 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200388 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200390 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200391 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 }
393
394 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100395 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
397 if ((fd < global.maxsock) &&
398 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
399 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
400 //fprintf(stderr, "process_chk: 3\n");
401
Willy Tarreau9edd1612007-10-18 18:07:48 +0200402 if (s->proxy->options & PR_O_TCP_NOLING) {
403 /* We don't want to useless data */
404 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
405 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200406
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200407 if (s->check_addr.sin_addr.s_addr)
408 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200409 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200410 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200411 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200412 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200413
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415 sa.sin_port = htons(s->check_port);
416
417 /* allow specific binding :
418 * - server-specific at first
419 * - proxy-specific next
420 */
421 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100422 struct sockaddr_in *remote = NULL;
423 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100424
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100425#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100426 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
427 remote = (struct sockaddr_in *)&s->tproxy_addr;
428 flags = 3;
429 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100430#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100431 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
432 if (ret) {
433 s->result |= SRV_CHK_ERROR;
434 switch (ret) {
435 case 1:
436 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
437 s->proxy->id, s->id);
438 break;
439 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100440 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
441 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100442 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100443 }
444 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 }
446 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100447 struct sockaddr_in *remote = NULL;
448 int ret, flags = 0;
449
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100450#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100451 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100452 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
453 flags = 3;
454 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100455#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100456 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
457 if (ret) {
458 s->result |= SRV_CHK_ERROR;
459 switch (ret) {
460 case 1:
461 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
462 proxy_type_str(s->proxy), s->proxy->id);
463 break;
464 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100465 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
466 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100467 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100468 }
469 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470 }
471
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100472 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100474 struct timeval tv_con;
475
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 /* OK, connection in progress or established */
477
478 //fprintf(stderr, "process_chk: 4\n");
479
480 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200481 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200483 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
484 fdtab[fd].cb[DIR_RD].b = NULL;
485 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
486 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200487 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
488 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreauf161a342007-04-08 16:59:42 +0200490 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200492 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100494 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
495 /* we allow up to min(inter, timeout.connect) for a connection
496 * to establish but only when timeout.check is set
497 * as it may be to short for a full check otherwise
498 */
499 tv_add(&tv_con, &now, &s->proxy->timeout.connect);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200500 tv_ms_add(&t->expire, &now, s->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100501
502 if (tv_isset(&s->proxy->timeout.check))
503 tv_bound(&t->expire, &tv_con);
504
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200506 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200507 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508 }
509 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100510 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511 }
512 }
513 }
514 close(fd); /* socket creation error */
515 }
516
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100517 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200519 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200520 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521 goto new_chk; /* may be we should initialize a new check */
522 }
523
524 /* here, we have seen a failure */
525 if (s->health > s->rise) {
526 s->health--; /* still good */
527 s->failed_checks++;
528 }
529 else
530 set_server_down(s);
531
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100532 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
533 /* we allow up to min(inter, timeout.connect) for a connection
534 * to establish but only when timeout.check is set
535 * as it may be to short for a full check otherwise
536 */
537 while (tv_isle(&t->expire, &now)) {
538 struct timeval tv_con;
539
540 tv_add(&tv_con, &t->expire, &s->proxy->timeout.connect);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200541 tv_ms_add(&t->expire, &t->expire, s->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100542
543 if (tv_isset(&s->proxy->timeout.check))
544 tv_bound(&t->expire, &tv_con);
545 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546 goto new_chk;
547 }
548 else {
549 //fprintf(stderr, "process_chk: 8\n");
550 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100551 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200553
Willy Tarreau9909fc12007-11-30 17:42:05 +0100554 if (s->state & SRV_WARMINGUP) {
555 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
556 s->state &= ~SRV_WARMINGUP;
557 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
558 s->eweight = s->uweight * BE_WEIGHT_SCALE;
559 if (s->proxy->lbprm.update_server_eweight)
560 s->proxy->lbprm.update_server_eweight(s);
561 }
562 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
563 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100564 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
565 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100566 s->eweight *= s->uweight;
567 if (s->proxy->lbprm.update_server_eweight)
568 s->proxy->lbprm.update_server_eweight(s);
569 }
570 /* probably that we can refill this server with a bit more connections */
571 check_for_pending(s);
572 }
573
Willy Tarreau48494c02007-11-30 10:41:39 +0100574 /* we may have to add/remove this server from the LB group */
575 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
576 if ((s->state & SRV_GOINGDOWN) &&
577 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING)) {
578 /* server enabled again */
579 s->state &= ~SRV_GOINGDOWN;
580 s->proxy->lbprm.set_server_status_up(s);
581
582 /* check if we can handle some connections queued at the proxy. We
583 * will take as many as we can handle.
584 */
585 xferred = check_for_pending(s);
586
587 sprintf(trash,
588 "Load-balancing on %sServer %s/%s is enabled again. %d active and %d backup servers online.%s"
589 " %d sessions requeued, %d total in queue.\n",
590 s->state & SRV_BACKUP ? "Backup " : "",
591 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
592 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
593 xferred, s->nbpend);
594
595 Warning("%s", trash);
596 send_log(s->proxy, LOG_NOTICE, "%s", trash);
597 }
598 else if (!(s->state & SRV_GOINGDOWN) &&
599 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
600 (SRV_CHK_RUNNING | SRV_CHK_DISABLE))) {
601 /* server disabled */
602 s->state |= SRV_GOINGDOWN;
603 s->proxy->lbprm.set_server_status_down(s);
604
605 /* we might have sessions queued on this server and waiting for
606 * a connection. Those which are redispatchable will be queued
607 * to another server or to the proxy itself.
608 */
609 xferred = redistribute_pending(s);
610
611 sprintf(trash,
612 "Load-balancing on %sServer %s/%s is disabled. %d active and %d backup servers online.%s"
613 " %d sessions requeued, %d total in queue.\n",
614 s->state & SRV_BACKUP ? "Backup " : "",
615 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
616 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
617 xferred, s->nbpend);
618
619 Warning("%s", trash);
620
621 send_log(s->proxy, LOG_NOTICE, "%s", trash);
622 if (!s->proxy->srv_bck && !s->proxy->srv_act)
623 set_backend_down(s->proxy);
624 }
625 }
626
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200627 if (s->health < s->rise + s->fall - 1) {
628 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629
630 if (s->health == s->rise) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200631 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
632 if (s->proxy->last_change < now.tv_sec) // ignore negative times
633 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
634 s->proxy->last_change = now.tv_sec;
635 }
636
Willy Tarreaub625a082007-11-26 01:15:43 +0100637 if (s->last_change < now.tv_sec) // ignore negative times
638 s->down_time += now.tv_sec - s->last_change;
639
640 s->last_change = now.tv_sec;
641 s->state |= SRV_RUNNING;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100642 if (s->slowstart > 0) {
643 s->state |= SRV_WARMINGUP;
644 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
645 /* For dynamic algorithms, start at the first step of the weight,
646 * without multiplying by BE_WEIGHT_SCALE.
647 */
648 s->eweight = s->uweight;
649 if (s->proxy->lbprm.update_server_eweight)
650 s->proxy->lbprm.update_server_eweight(s);
651 }
652 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100653 s->proxy->lbprm.set_server_status_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654
655 /* check if we can handle some connections queued at the proxy. We
656 * will take as many as we can handle.
657 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100658 xferred = check_for_pending(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659
660 sprintf(trash,
661 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
662 " %d sessions requeued, %d total in queue.\n",
663 s->state & SRV_BACKUP ? "Backup " : "",
664 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
665 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
666 xferred, s->nbpend);
667
668 Warning("%s", trash);
669 send_log(s->proxy, LOG_NOTICE, "%s", trash);
670 }
671
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200672 if (s->health >= s->rise)
673 s->health = s->rise + s->fall - 1; /* OK now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 }
675 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200677
678 rv = 0;
679 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100680 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200681 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100682 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200683 }
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100684 tv_ms_add(&t->expire, &now, srv_getinter(s) + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685 goto new_chk;
686 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100687 else if ((s->result & SRV_CHK_ERROR) || tv_isle(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 //fprintf(stderr, "process_chk: 10\n");
689 /* failure or timeout detected */
690 if (s->health > s->rise) {
691 s->health--; /* still good */
692 s->failed_checks++;
693 }
694 else
695 set_server_down(s);
696 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200698
699 rv = 0;
700 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100701 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200702 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100703 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200704 }
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100705 tv_ms_add(&t->expire, &now, srv_getinter(s) + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 goto new_chk;
707 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100708 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 }
710 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100711 s->result = SRV_CHK_UNKNOWN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200713 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200714 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200715 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716}
717
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200718/*
719 * Start health-check.
720 * Returns 0 if OK, -1 if error, and prints the error in this case.
721 */
722int start_checks() {
723
724 struct proxy *px;
725 struct server *s;
726 struct task *t;
727 int nbchk=0, mininter=0, srvpos=0;
728
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200729 /* 1- count the checkers to run simultaneously.
730 * We also determine the minimum interval among all of those which
731 * have an interval larger than SRV_CHK_INTER_THRES. This interval
732 * will be used to spread their start-up date. Those which have
733 * a shorter interval will start independantly and will not dictate
734 * too short an interval for all others.
735 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200736 for (px = proxy; px; px = px->next) {
737 for (s = px->srv; s; s = s->next) {
738 if (!(s->state & SRV_CHECKED))
739 continue;
740
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100741 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
742 (!mininter || mininter > srv_getinter(s)))
743 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200744
745 nbchk++;
746 }
747 }
748
749 if (!nbchk)
750 return 0;
751
752 srand((unsigned)time(NULL));
753
754 /*
755 * 2- start them as far as possible from each others. For this, we will
756 * start them after their interval set to the min interval divided by
757 * the number of servers, weighted by the server's position in the list.
758 */
759 for (px = proxy; px; px = px->next) {
760 for (s = px->srv; s; s = s->next) {
761 if (!(s->state & SRV_CHECKED))
762 continue;
763
764 if ((t = pool_alloc2(pool2_task)) == NULL) {
765 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
766 return -1;
767 }
768
769 t->wq = NULL;
770 t->qlist.p = NULL;
771 t->state = TASK_IDLE;
772 t->process = process_chk;
773 t->context = s;
774
775 /* check this every ms */
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200776 tv_ms_add(&t->expire, &now,
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100777 ((mininter && mininter >= srv_getinter(s)) ? mininter : srv_getinter(s)) * srvpos / nbchk);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200778 task_queue(t);
779
780 srvpos++;
781 }
782 }
783 return 0;
784}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785
786/*
787 * Local variables:
788 * c-indent-level: 8
789 * c-basic-offset: 8
790 * End:
791 */