blob: ff02d016fe622dbb01ca310950879bd662767648 [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
Willy Tarreau6996e152007-04-30 14:37:43 +0200174 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
175 goto out_error;
176
177 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200178
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100179 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200181 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200182 (s->proxy->options & PR_O_SSL3_CHK) ||
183 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200185 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186 * so we'll send the request, and won't wake the checker up now.
187 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200188
189 if (s->proxy->options & PR_O_SSL3_CHK) {
190 /* SSL requires that we put Unix time in the request */
191 int gmt_time = htonl(now.tv_sec);
192 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
193 }
194
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195#ifndef MSG_NOSIGNAL
196 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
197#else
198 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
199#endif
200 if (ret == s->proxy->check_len) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200201 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200202 goto out_nowake;
203 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200204 else if (ret == 0 || errno == EAGAIN)
205 goto out_poll;
206 else
207 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 }
209 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200210 /* We have no data to send to check the connection, and
211 * getsockopt() will not inform us whether the connection
212 * is still pending. So we'll reuse connect() to check the
213 * state of the socket. This has the advantage of givig us
214 * the following info :
215 * - error
216 * - connecting (EALREADY, EINPROGRESS)
217 * - connected (EISCONN, 0)
218 */
219
220 struct sockaddr_in sa;
221
222 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
223 sa.sin_port = htons(s->check_port);
224
225 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
226 errno = 0;
227
228 if (errno == EALREADY || errno == EINPROGRESS)
229 goto out_poll;
230
231 if (errno && errno != EISCONN)
232 goto out_error;
233
Willy Tarreaubaaee002006-06-26 02:48:02 +0200234 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100235 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200236 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237 }
238 }
Willy Tarreau83749182007-04-15 20:56:27 +0200239 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200240 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200241 out_nowake:
242 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100243 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200244 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200245 out_poll:
246 /* The connection is still pending. We'll have to poll it
247 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100248 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200249 return 0;
250 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100251 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200252 fdtab[fd].state = FD_STERROR;
253 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254}
255
256
257/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200258 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100259 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
260 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
261 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
262 * to an SSL HELLO (the principle is that this is enough to distinguish between
263 * an SSL server and a pure TCP relay). All other cases will set s->result to
264 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
265 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266 */
Willy Tarreau83749182007-04-15 20:56:27 +0200267static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200268{
Willy Tarreau83749182007-04-15 20:56:27 +0200269 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100270 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200271 struct task *t = fdtab[fd].owner;
272 struct server *s = t->context;
273 int skerr;
274 socklen_t lskerr = sizeof(skerr);
275
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100276 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200277
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100278 if (unlikely((s->result & SRV_CHK_ERROR) ||
279 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200280 (fdtab[fd].ev & FD_POLL_ERR) ||
281 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
282 (skerr != 0))) {
283 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100284 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200285 goto out_wakeup;
286 }
287
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200289 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290#else
Willy Tarreau83749182007-04-15 20:56:27 +0200291 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
292 * but the connection was closed on the remote end. Fortunately, recv still
293 * works correctly and we don't need to do the getsockopt() on linux.
294 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200295 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200297 if (unlikely(len < 0 && errno == EAGAIN)) {
298 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100299 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200300 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 }
302
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100303 /* Note: the response will only be accepted if read at once */
304 if (s->proxy->options & PR_O_HTTP_CHK) {
305 /* Check if the server speaks HTTP 1.X */
306 if ((len < strlen("HTTP/1.0 000\r")) ||
307 (memcmp(trash, "HTTP/1.", 7) != 0)) {
308 s->result |= SRV_CHK_ERROR;
309 goto out_wakeup;
310 }
311
312 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
313 if (trash[9] == '2' || trash[9] == '3')
314 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100315 else if ((s->proxy->options & PR_O_DISABLE404) &&
316 (s->state & SRV_RUNNING) &&
317 (memcmp(&trash[9], "404", 3) == 0)) {
318 /* 404 may be accepted as "stopping" only if the server was up */
319 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
320 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100321 else
322 s->result |= SRV_CHK_ERROR;
323 }
324 else if (s->proxy->options & PR_O_SSL3_CHK) {
325 /* Check for SSLv3 alert or handshake */
326 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
327 s->result |= SRV_CHK_RUNNING;
328 else
329 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200330 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100331 else if (s->proxy->options & PR_O_SMTP_CHK) {
332 /* Check for SMTP code 2xx (should be 250) */
333 if ((len >= 3) && (trash[0] == '2'))
334 s->result |= SRV_CHK_RUNNING;
335 else
336 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200337 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100338 else {
339 /* other checks are valid if the connection succeeded anyway */
340 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200341 }
Willy Tarreau83749182007-04-15 20:56:27 +0200342
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100343 out_wakeup:
344 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345 fdtab[fd].state = FD_STERROR;
346
Willy Tarreauf161a342007-04-08 16:59:42 +0200347 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200348 task_wakeup(t);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100349 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200350 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351}
352
353/*
354 * manages a server health-check. Returns
355 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
356 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200357void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200359 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360 struct server *s = t->context;
361 struct sockaddr_in sa;
Willy Tarreau48494c02007-11-30 10:41:39 +0100362 int xferred;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200364 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365
366 //fprintf(stderr, "process_chk: task=%p\n", t);
367
368 new_chk:
369 fd = s->curfd;
370 if (fd < 0) { /* no check currently running */
371 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200372 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200373 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200374 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200375 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376 }
377
378 /* we don't send any health-checks when the proxy is stopped or when
379 * the server should not be checked.
380 */
381 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200382 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200383 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200384 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200385 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200386 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387 }
388
389 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100390 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
392 if ((fd < global.maxsock) &&
393 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
394 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
395 //fprintf(stderr, "process_chk: 3\n");
396
Willy Tarreau9edd1612007-10-18 18:07:48 +0200397 if (s->proxy->options & PR_O_TCP_NOLING) {
398 /* We don't want to useless data */
399 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
400 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200401
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200402 if (s->check_addr.sin_addr.s_addr)
403 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200404 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200405 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200406 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200407 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200408
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410 sa.sin_port = htons(s->check_port);
411
412 /* allow specific binding :
413 * - server-specific at first
414 * - proxy-specific next
415 */
416 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100417 struct sockaddr_in *remote = NULL;
418 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100419
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100420 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
421 remote = (struct sockaddr_in *)&s->tproxy_addr;
422 flags = 3;
423 }
424 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
425 if (ret) {
426 s->result |= SRV_CHK_ERROR;
427 switch (ret) {
428 case 1:
429 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
430 s->proxy->id, s->id);
431 break;
432 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100433 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
434 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100435 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100436 }
437 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 }
439 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100440 struct sockaddr_in *remote = NULL;
441 int ret, flags = 0;
442
Willy Tarreau163c5322006-11-14 16:18:41 +0100443 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100444 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
445 flags = 3;
446 }
447 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
448 if (ret) {
449 s->result |= SRV_CHK_ERROR;
450 switch (ret) {
451 case 1:
452 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
453 proxy_type_str(s->proxy), s->proxy->id);
454 break;
455 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100456 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
457 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100458 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100459 }
460 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 }
462
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100463 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
465 /* OK, connection in progress or established */
466
467 //fprintf(stderr, "process_chk: 4\n");
468
469 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200470 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200471 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200472 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
473 fdtab[fd].cb[DIR_RD].b = NULL;
474 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
475 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200476 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
477 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreauf161a342007-04-08 16:59:42 +0200479 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200481 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200483 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200484 tv_ms_add(&t->expire, &now, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200485 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200486 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200487 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488 }
489 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100490 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491 }
492 }
493 }
494 close(fd); /* socket creation error */
495 }
496
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100497 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200498 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200499 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200500 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501 goto new_chk; /* may be we should initialize a new check */
502 }
503
504 /* here, we have seen a failure */
505 if (s->health > s->rise) {
506 s->health--; /* still good */
507 s->failed_checks++;
508 }
509 else
510 set_server_down(s);
511
512 //fprintf(stderr, "process_chk: 7\n");
513 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200514 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200515 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200516 goto new_chk;
517 }
518 else {
519 //fprintf(stderr, "process_chk: 8\n");
520 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100521 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200523
Willy Tarreau9909fc12007-11-30 17:42:05 +0100524 if (s->state & SRV_WARMINGUP) {
525 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
526 s->state &= ~SRV_WARMINGUP;
527 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
528 s->eweight = s->uweight * BE_WEIGHT_SCALE;
529 if (s->proxy->lbprm.update_server_eweight)
530 s->proxy->lbprm.update_server_eweight(s);
531 }
532 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
533 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100534 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
535 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100536 s->eweight *= s->uweight;
537 if (s->proxy->lbprm.update_server_eweight)
538 s->proxy->lbprm.update_server_eweight(s);
539 }
540 /* probably that we can refill this server with a bit more connections */
541 check_for_pending(s);
542 }
543
Willy Tarreau48494c02007-11-30 10:41:39 +0100544 /* we may have to add/remove this server from the LB group */
545 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
546 if ((s->state & SRV_GOINGDOWN) &&
547 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING)) {
548 /* server enabled again */
549 s->state &= ~SRV_GOINGDOWN;
550 s->proxy->lbprm.set_server_status_up(s);
551
552 /* check if we can handle some connections queued at the proxy. We
553 * will take as many as we can handle.
554 */
555 xferred = check_for_pending(s);
556
557 sprintf(trash,
558 "Load-balancing on %sServer %s/%s is enabled again. %d active and %d backup servers online.%s"
559 " %d sessions requeued, %d total in queue.\n",
560 s->state & SRV_BACKUP ? "Backup " : "",
561 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
562 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
563 xferred, s->nbpend);
564
565 Warning("%s", trash);
566 send_log(s->proxy, LOG_NOTICE, "%s", trash);
567 }
568 else if (!(s->state & SRV_GOINGDOWN) &&
569 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
570 (SRV_CHK_RUNNING | SRV_CHK_DISABLE))) {
571 /* server disabled */
572 s->state |= SRV_GOINGDOWN;
573 s->proxy->lbprm.set_server_status_down(s);
574
575 /* we might have sessions queued on this server and waiting for
576 * a connection. Those which are redispatchable will be queued
577 * to another server or to the proxy itself.
578 */
579 xferred = redistribute_pending(s);
580
581 sprintf(trash,
582 "Load-balancing on %sServer %s/%s is disabled. %d active and %d backup servers online.%s"
583 " %d sessions requeued, %d total in queue.\n",
584 s->state & SRV_BACKUP ? "Backup " : "",
585 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
586 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
587 xferred, s->nbpend);
588
589 Warning("%s", trash);
590
591 send_log(s->proxy, LOG_NOTICE, "%s", trash);
592 if (!s->proxy->srv_bck && !s->proxy->srv_act)
593 set_backend_down(s->proxy);
594 }
595 }
596
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200597 if (s->health < s->rise + s->fall - 1) {
598 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599
600 if (s->health == s->rise) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200601 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
602 if (s->proxy->last_change < now.tv_sec) // ignore negative times
603 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
604 s->proxy->last_change = now.tv_sec;
605 }
606
Willy Tarreaub625a082007-11-26 01:15:43 +0100607 if (s->last_change < now.tv_sec) // ignore negative times
608 s->down_time += now.tv_sec - s->last_change;
609
610 s->last_change = now.tv_sec;
611 s->state |= SRV_RUNNING;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100612 if (s->slowstart > 0) {
613 s->state |= SRV_WARMINGUP;
614 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
615 /* For dynamic algorithms, start at the first step of the weight,
616 * without multiplying by BE_WEIGHT_SCALE.
617 */
618 s->eweight = s->uweight;
619 if (s->proxy->lbprm.update_server_eweight)
620 s->proxy->lbprm.update_server_eweight(s);
621 }
622 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100623 s->proxy->lbprm.set_server_status_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624
625 /* check if we can handle some connections queued at the proxy. We
626 * will take as many as we can handle.
627 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100628 xferred = check_for_pending(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629
630 sprintf(trash,
631 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
632 " %d sessions requeued, %d total in queue.\n",
633 s->state & SRV_BACKUP ? "Backup " : "",
634 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
635 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
636 xferred, s->nbpend);
637
638 Warning("%s", trash);
639 send_log(s->proxy, LOG_NOTICE, "%s", trash);
640 }
641
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200642 if (s->health >= s->rise)
643 s->health = s->rise + s->fall - 1; /* OK now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200644 }
645 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200647
648 rv = 0;
649 if (global.spread_checks > 0) {
650 rv = s->inter * global.spread_checks / 100;
651 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
652 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
653 }
654 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 goto new_chk;
656 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100657 else if ((s->result & SRV_CHK_ERROR) || tv_isle(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200658 //fprintf(stderr, "process_chk: 10\n");
659 /* failure or timeout detected */
660 if (s->health > s->rise) {
661 s->health--; /* still good */
662 s->failed_checks++;
663 }
664 else
665 set_server_down(s);
666 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200668
669 rv = 0;
670 if (global.spread_checks > 0) {
671 rv = s->inter * global.spread_checks / 100;
672 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200673 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200674 }
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200675 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 goto new_chk;
677 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100678 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679 }
680 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100681 s->result = SRV_CHK_UNKNOWN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200683 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200684 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200685 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686}
687
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200688/*
689 * Start health-check.
690 * Returns 0 if OK, -1 if error, and prints the error in this case.
691 */
692int start_checks() {
693
694 struct proxy *px;
695 struct server *s;
696 struct task *t;
697 int nbchk=0, mininter=0, srvpos=0;
698
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200699 /* 1- count the checkers to run simultaneously.
700 * We also determine the minimum interval among all of those which
701 * have an interval larger than SRV_CHK_INTER_THRES. This interval
702 * will be used to spread their start-up date. Those which have
703 * a shorter interval will start independantly and will not dictate
704 * too short an interval for all others.
705 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200706 for (px = proxy; px; px = px->next) {
707 for (s = px->srv; s; s = s->next) {
708 if (!(s->state & SRV_CHECKED))
709 continue;
710
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200711 if ((s->inter >= SRV_CHK_INTER_THRES) &&
712 (!mininter || mininter > s->inter))
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200713 mininter = s->inter;
714
715 nbchk++;
716 }
717 }
718
719 if (!nbchk)
720 return 0;
721
722 srand((unsigned)time(NULL));
723
724 /*
725 * 2- start them as far as possible from each others. For this, we will
726 * start them after their interval set to the min interval divided by
727 * the number of servers, weighted by the server's position in the list.
728 */
729 for (px = proxy; px; px = px->next) {
730 for (s = px->srv; s; s = s->next) {
731 if (!(s->state & SRV_CHECKED))
732 continue;
733
734 if ((t = pool_alloc2(pool2_task)) == NULL) {
735 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
736 return -1;
737 }
738
739 t->wq = NULL;
740 t->qlist.p = NULL;
741 t->state = TASK_IDLE;
742 t->process = process_chk;
743 t->context = s;
744
745 /* check this every ms */
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200746 tv_ms_add(&t->expire, &now,
747 ((mininter && mininter >= s->inter) ? mininter : s->inter) * srvpos / nbchk);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200748 task_queue(t);
749
750 srvpos++;
751 }
752 }
753 return 0;
754}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
756/*
757 * Local variables:
758 * c-indent-level: 8
759 * c-basic-offset: 8
760 * End:
761 */