blob: f0f18d3fdd553a793f3c4b13aac0c0433b0fb1ec [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
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 Tarreaue8c66af2008-01-13 18:40:14 +010040#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010041#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/server.h>
43#include <proto/task.h>
44
Willy Tarreau48494c02007-11-30 10:41:39 +010045/* sends a log message when a backend goes down, and also sets last
46 * change date.
47 */
48static void set_backend_down(struct proxy *be)
49{
50 be->last_change = now.tv_sec;
51 be->down_trans++;
52
53 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
54 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
55}
56
57/* Redistribute pending connections when a server goes down. The number of
58 * connections redistributed is returned.
59 */
60static int redistribute_pending(struct server *s)
61{
62 struct pendconn *pc, *pc_bck, *pc_end;
63 int xferred = 0;
64
65 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
66 struct session *sess = pc->sess;
67 if (sess->be->options & PR_O_REDISP) {
68 /* The REDISP option was specified. We will ignore
69 * cookie and force to balance or use the dispatcher.
70 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010071
72 sess->srv->redispatches++;
73 sess->be->redispatches++;
74
Willy Tarreau48494c02007-11-30 10:41:39 +010075 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010076 sess->flags |= SN_REDISP;
77
Willy Tarreau48494c02007-11-30 10:41:39 +010078 sess->srv = NULL; /* it's left to the dispatcher to choose a server */
79 http_flush_cookie_flags(&sess->txn);
80 pendconn_free(pc);
81 task_wakeup(sess->task);
82 xferred++;
83 }
84 }
85 return xferred;
86}
87
88/* Check for pending connections at the backend, and assign some of them to
89 * the server coming up. The server's weight is checked before being assigned
90 * connections it may not be able to handle. The total number of transferred
91 * connections is returned.
92 */
93static int check_for_pending(struct server *s)
94{
95 int xferred;
96
97 if (!s->eweight)
98 return 0;
99
100 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
101 struct session *sess;
102 struct pendconn *p;
103
104 p = pendconn_from_px(s->proxy);
105 if (!p)
106 break;
107 p->sess->srv = s;
108 sess = p->sess;
109 pendconn_free(p);
110 task_wakeup(sess->task);
111 }
112 return xferred;
113}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
115/* Sets server <s> down, notifies by all available means, recounts the
116 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200117 * possible to other servers. It automatically recomputes the number of
118 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 */
Willy Tarreau83749182007-04-15 20:56:27 +0200120static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121{
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 int xferred;
123
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 if (s->health == s->rise) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100125 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200126
127 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100128 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100129 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130
131 /* we might have sessions queued on this server and waiting for
132 * a connection. Those which are redispatchable will be queued
133 * to another server or to the proxy itself.
134 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100135 xferred = redistribute_pending(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136 sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s"
137 " %d sessions active, %d requeued, %d remaining in queue.\n",
138 s->state & SRV_BACKUP ? "Backup " : "",
139 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
140 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
141 s->cur_sess, xferred, s->nbpend);
142
143 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200144
Willy Tarreau48494c02007-11-30 10:41:39 +0100145 /* we don't send an alert if the server was previously paused */
146 if (srv_was_paused)
147 send_log(s->proxy, LOG_NOTICE, "%s", trash);
148 else
149 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200150
Willy Tarreau48494c02007-11-30 10:41:39 +0100151 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
152 set_backend_down(s->proxy);
153
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154 s->down_trans++;
155 }
156 s->health = 0; /* failure */
157}
158
159
160/*
161 * This function is used only for server health-checks. It handles
162 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100163 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200164 * The function itself returns 0 if it needs some polling before being called
165 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 */
Willy Tarreau83749182007-04-15 20:56:27 +0200167static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168{
Willy Tarreau6996e152007-04-30 14:37:43 +0200169 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 struct task *t = fdtab[fd].owner;
171 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172
Willy Tarreau6996e152007-04-30 14:37:43 +0200173 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
174 goto out_error;
175
176 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200177
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100178 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200180 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200181 (s->proxy->options & PR_O_SSL3_CHK) ||
182 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200184 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 * so we'll send the request, and won't wake the checker up now.
186 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200187
188 if (s->proxy->options & PR_O_SSL3_CHK) {
189 /* SSL requires that we put Unix time in the request */
190 int gmt_time = htonl(now.tv_sec);
191 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
192 }
193
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194#ifndef MSG_NOSIGNAL
195 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
196#else
197 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
198#endif
199 if (ret == s->proxy->check_len) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200200 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200201 goto out_nowake;
202 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200203 else if (ret == 0 || errno == EAGAIN)
204 goto out_poll;
205 else
206 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207 }
208 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200209 /* We have no data to send to check the connection, and
210 * getsockopt() will not inform us whether the connection
211 * is still pending. So we'll reuse connect() to check the
212 * state of the socket. This has the advantage of givig us
213 * the following info :
214 * - error
215 * - connecting (EALREADY, EINPROGRESS)
216 * - connected (EISCONN, 0)
217 */
218
219 struct sockaddr_in sa;
220
221 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
222 sa.sin_port = htons(s->check_port);
223
224 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
225 errno = 0;
226
227 if (errno == EALREADY || errno == EINPROGRESS)
228 goto out_poll;
229
230 if (errno && errno != EISCONN)
231 goto out_error;
232
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100234 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200235 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236 }
237 }
Willy Tarreau83749182007-04-15 20:56:27 +0200238 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200239 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200240 out_nowake:
241 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
242 fdtab[fd].ev &= ~FD_POLL_WR;
243 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200244 out_poll:
245 /* The connection is still pending. We'll have to poll it
246 * before attempting to go further. */
247 fdtab[fd].ev &= ~FD_POLL_WR;
248 return 0;
249 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100250 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200251 fdtab[fd].state = FD_STERROR;
252 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253}
254
255
256/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200257 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100258 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
259 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
260 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
261 * to an SSL HELLO (the principle is that this is enough to distinguish between
262 * an SSL server and a pure TCP relay). All other cases will set s->result to
263 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
264 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200265 */
Willy Tarreau83749182007-04-15 20:56:27 +0200266static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200267{
Willy Tarreau83749182007-04-15 20:56:27 +0200268 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100269 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270 struct task *t = fdtab[fd].owner;
271 struct server *s = t->context;
272 int skerr;
273 socklen_t lskerr = sizeof(skerr);
274
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100275 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200276
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100277 if (unlikely((s->result & SRV_CHK_ERROR) ||
278 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200279 (fdtab[fd].ev & FD_POLL_ERR) ||
280 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
281 (skerr != 0))) {
282 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100283 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200284 goto out_wakeup;
285 }
286
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200288 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289#else
Willy Tarreau83749182007-04-15 20:56:27 +0200290 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
291 * but the connection was closed on the remote end. Fortunately, recv still
292 * works correctly and we don't need to do the getsockopt() on linux.
293 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200294 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200296 if (unlikely(len < 0 && errno == EAGAIN)) {
297 /* we want some polling to happen first */
298 fdtab[fd].ev &= ~FD_POLL_RD;
299 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200300 }
301
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100302 /* Note: the response will only be accepted if read at once */
303 if (s->proxy->options & PR_O_HTTP_CHK) {
304 /* Check if the server speaks HTTP 1.X */
305 if ((len < strlen("HTTP/1.0 000\r")) ||
306 (memcmp(trash, "HTTP/1.", 7) != 0)) {
307 s->result |= SRV_CHK_ERROR;
308 goto out_wakeup;
309 }
310
311 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
312 if (trash[9] == '2' || trash[9] == '3')
313 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100314 else if ((s->proxy->options & PR_O_DISABLE404) &&
315 (s->state & SRV_RUNNING) &&
316 (memcmp(&trash[9], "404", 3) == 0)) {
317 /* 404 may be accepted as "stopping" only if the server was up */
318 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
319 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100320 else
321 s->result |= SRV_CHK_ERROR;
322 }
323 else if (s->proxy->options & PR_O_SSL3_CHK) {
324 /* Check for SSLv3 alert or handshake */
325 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
326 s->result |= SRV_CHK_RUNNING;
327 else
328 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200329 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100330 else if (s->proxy->options & PR_O_SMTP_CHK) {
331 /* Check for SMTP code 2xx (should be 250) */
332 if ((len >= 3) && (trash[0] == '2'))
333 s->result |= SRV_CHK_RUNNING;
334 else
335 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200336 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100337 else {
338 /* other checks are valid if the connection succeeded anyway */
339 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200340 }
Willy Tarreau83749182007-04-15 20:56:27 +0200341
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100342 out_wakeup:
343 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 fdtab[fd].state = FD_STERROR;
345
Willy Tarreauf161a342007-04-08 16:59:42 +0200346 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200347 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200348 fdtab[fd].ev &= ~FD_POLL_RD;
349 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350}
351
352/*
353 * manages a server health-check. Returns
354 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
355 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200356void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200358 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 struct server *s = t->context;
360 struct sockaddr_in sa;
Willy Tarreau48494c02007-11-30 10:41:39 +0100361 int xferred;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200363 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364
365 //fprintf(stderr, "process_chk: task=%p\n", t);
366
367 new_chk:
368 fd = s->curfd;
369 if (fd < 0) { /* no check currently running */
370 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200371 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200373 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200374 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375 }
376
377 /* we don't send any health-checks when the proxy is stopped or when
378 * the server should not be checked.
379 */
380 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200381 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200382 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200384 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200385 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 }
387
388 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100389 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
391 if ((fd < global.maxsock) &&
392 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
393 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
394 //fprintf(stderr, "process_chk: 3\n");
395
Willy Tarreau9edd1612007-10-18 18:07:48 +0200396 if (s->proxy->options & PR_O_TCP_NOLING) {
397 /* We don't want to useless data */
398 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
399 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200400
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200401 if (s->check_addr.sin_addr.s_addr)
402 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200403 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200404 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200405 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200406 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200407
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 sa.sin_port = htons(s->check_port);
410
411 /* allow specific binding :
412 * - server-specific at first
413 * - proxy-specific next
414 */
415 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100416 struct sockaddr_in *remote = NULL;
417 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100418
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100419 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
420 remote = (struct sockaddr_in *)&s->tproxy_addr;
421 flags = 3;
422 }
423 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
424 if (ret) {
425 s->result |= SRV_CHK_ERROR;
426 switch (ret) {
427 case 1:
428 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
429 s->proxy->id, s->id);
430 break;
431 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100432 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
433 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100434 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100435 }
436 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437 }
438 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100439 struct sockaddr_in *remote = NULL;
440 int ret, flags = 0;
441
Willy Tarreau163c5322006-11-14 16:18:41 +0100442 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100443 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
444 flags = 3;
445 }
446 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
447 if (ret) {
448 s->result |= SRV_CHK_ERROR;
449 switch (ret) {
450 case 1:
451 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
452 proxy_type_str(s->proxy), s->proxy->id);
453 break;
454 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100455 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
456 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100457 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100458 }
459 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 }
461
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100462 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
464 /* OK, connection in progress or established */
465
466 //fprintf(stderr, "process_chk: 4\n");
467
468 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200469 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200471 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
472 fdtab[fd].cb[DIR_RD].b = NULL;
473 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
474 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200475 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
476 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200478 fdtab[fd].ev = 0;
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 */