blob: d3aa6c8ce1be30d56b95367de2cce0b8285fe06e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreaud825eef2007-05-12 22:35:00 +02004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020016#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020018#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <unistd.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23
Willy Tarreau2dd0d472006-06-29 17:53:05 +020024#include <common/compat.h>
25#include <common/config.h>
26#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020027#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
30#include <types/global.h>
31#include <types/polling.h>
32#include <types/proxy.h>
33#include <types/session.h>
34
35#include <proto/backend.h>
36#include <proto/fd.h>
37#include <proto/log.h>
38#include <proto/queue.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010039#include <proto/proto_http.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010040#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/server.h>
42#include <proto/task.h>
43
Willy Tarreau163c5322006-11-14 16:18:41 +010044#ifdef CONFIG_HAP_CTTPROXY
45#include <import/ip_tproxy.h>
46#endif
47
Willy Tarreau48494c02007-11-30 10:41:39 +010048/* sends a log message when a backend goes down, and also sets last
49 * change date.
50 */
51static void set_backend_down(struct proxy *be)
52{
53 be->last_change = now.tv_sec;
54 be->down_trans++;
55
56 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
57 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
58}
59
60/* Redistribute pending connections when a server goes down. The number of
61 * connections redistributed is returned.
62 */
63static int redistribute_pending(struct server *s)
64{
65 struct pendconn *pc, *pc_bck, *pc_end;
66 int xferred = 0;
67
68 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
69 struct session *sess = pc->sess;
70 if (sess->be->options & PR_O_REDISP) {
71 /* The REDISP option was specified. We will ignore
72 * cookie and force to balance or use the dispatcher.
73 */
74 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
75 sess->srv = NULL; /* it's left to the dispatcher to choose a server */
76 http_flush_cookie_flags(&sess->txn);
77 pendconn_free(pc);
78 task_wakeup(sess->task);
79 xferred++;
80 }
81 }
82 return xferred;
83}
84
85/* Check for pending connections at the backend, and assign some of them to
86 * the server coming up. The server's weight is checked before being assigned
87 * connections it may not be able to handle. The total number of transferred
88 * connections is returned.
89 */
90static int check_for_pending(struct server *s)
91{
92 int xferred;
93
94 if (!s->eweight)
95 return 0;
96
97 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
98 struct session *sess;
99 struct pendconn *p;
100
101 p = pendconn_from_px(s->proxy);
102 if (!p)
103 break;
104 p->sess->srv = s;
105 sess = p->sess;
106 pendconn_free(p);
107 task_wakeup(sess->task);
108 }
109 return xferred;
110}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
112/* Sets server <s> down, notifies by all available means, recounts the
113 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200114 * possible to other servers. It automatically recomputes the number of
115 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 */
Willy Tarreau83749182007-04-15 20:56:27 +0200117static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118{
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 int xferred;
120
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121 if (s->health == s->rise) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100122 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200123
124 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100125 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100126 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127
128 /* we might have sessions queued on this server and waiting for
129 * a connection. Those which are redispatchable will be queued
130 * to another server or to the proxy itself.
131 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100132 xferred = redistribute_pending(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133 sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s"
134 " %d sessions active, %d requeued, %d remaining in queue.\n",
135 s->state & SRV_BACKUP ? "Backup " : "",
136 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
137 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
138 s->cur_sess, xferred, s->nbpend);
139
140 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200141
Willy Tarreau48494c02007-11-30 10:41:39 +0100142 /* we don't send an alert if the server was previously paused */
143 if (srv_was_paused)
144 send_log(s->proxy, LOG_NOTICE, "%s", trash);
145 else
146 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200147
Willy Tarreau48494c02007-11-30 10:41:39 +0100148 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
149 set_backend_down(s->proxy);
150
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 s->down_trans++;
152 }
153 s->health = 0; /* failure */
154}
155
156
157/*
158 * This function is used only for server health-checks. It handles
159 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100160 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200161 * The function itself returns 0 if it needs some polling before being called
162 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 */
Willy Tarreau83749182007-04-15 20:56:27 +0200164static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165{
Willy Tarreau6996e152007-04-30 14:37:43 +0200166 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167 struct task *t = fdtab[fd].owner;
168 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169
Willy Tarreau6996e152007-04-30 14:37:43 +0200170 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
171 goto out_error;
172
173 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200174
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100175 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200177 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200178 (s->proxy->options & PR_O_SSL3_CHK) ||
179 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200181 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182 * so we'll send the request, and won't wake the checker up now.
183 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200184
185 if (s->proxy->options & PR_O_SSL3_CHK) {
186 /* SSL requires that we put Unix time in the request */
187 int gmt_time = htonl(now.tv_sec);
188 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
189 }
190
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191#ifndef MSG_NOSIGNAL
192 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
193#else
194 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
195#endif
196 if (ret == s->proxy->check_len) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200197 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200198 goto out_nowake;
199 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200200 else if (ret == 0 || errno == EAGAIN)
201 goto out_poll;
202 else
203 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204 }
205 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200206 /* We have no data to send to check the connection, and
207 * getsockopt() will not inform us whether the connection
208 * is still pending. So we'll reuse connect() to check the
209 * state of the socket. This has the advantage of givig us
210 * the following info :
211 * - error
212 * - connecting (EALREADY, EINPROGRESS)
213 * - connected (EISCONN, 0)
214 */
215
216 struct sockaddr_in sa;
217
218 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
219 sa.sin_port = htons(s->check_port);
220
221 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
222 errno = 0;
223
224 if (errno == EALREADY || errno == EINPROGRESS)
225 goto out_poll;
226
227 if (errno && errno != EISCONN)
228 goto out_error;
229
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100231 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200232 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 }
234 }
Willy Tarreau83749182007-04-15 20:56:27 +0200235 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200236 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200237 out_nowake:
238 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
239 fdtab[fd].ev &= ~FD_POLL_WR;
240 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200241 out_poll:
242 /* The connection is still pending. We'll have to poll it
243 * before attempting to go further. */
244 fdtab[fd].ev &= ~FD_POLL_WR;
245 return 0;
246 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100247 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200248 fdtab[fd].state = FD_STERROR;
249 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250}
251
252
253/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200254 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100255 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
256 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
257 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
258 * to an SSL HELLO (the principle is that this is enough to distinguish between
259 * an SSL server and a pure TCP relay). All other cases will set s->result to
260 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
261 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262 */
Willy Tarreau83749182007-04-15 20:56:27 +0200263static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264{
Willy Tarreau83749182007-04-15 20:56:27 +0200265 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100266 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200267 struct task *t = fdtab[fd].owner;
268 struct server *s = t->context;
269 int skerr;
270 socklen_t lskerr = sizeof(skerr);
271
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100272 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200273
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100274 if (unlikely((s->result & SRV_CHK_ERROR) ||
275 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200276 (fdtab[fd].ev & FD_POLL_ERR) ||
277 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
278 (skerr != 0))) {
279 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100280 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200281 goto out_wakeup;
282 }
283
Willy Tarreaubaaee002006-06-26 02:48:02 +0200284#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200285 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286#else
Willy Tarreau83749182007-04-15 20:56:27 +0200287 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
288 * but the connection was closed on the remote end. Fortunately, recv still
289 * works correctly and we don't need to do the getsockopt() on linux.
290 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200291 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200293 if (unlikely(len < 0 && errno == EAGAIN)) {
294 /* we want some polling to happen first */
295 fdtab[fd].ev &= ~FD_POLL_RD;
296 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297 }
298
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100299 /* Note: the response will only be accepted if read at once */
300 if (s->proxy->options & PR_O_HTTP_CHK) {
301 /* Check if the server speaks HTTP 1.X */
302 if ((len < strlen("HTTP/1.0 000\r")) ||
303 (memcmp(trash, "HTTP/1.", 7) != 0)) {
304 s->result |= SRV_CHK_ERROR;
305 goto out_wakeup;
306 }
307
308 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
309 if (trash[9] == '2' || trash[9] == '3')
310 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100311 else if ((s->proxy->options & PR_O_DISABLE404) &&
312 (s->state & SRV_RUNNING) &&
313 (memcmp(&trash[9], "404", 3) == 0)) {
314 /* 404 may be accepted as "stopping" only if the server was up */
315 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
316 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100317 else
318 s->result |= SRV_CHK_ERROR;
319 }
320 else if (s->proxy->options & PR_O_SSL3_CHK) {
321 /* Check for SSLv3 alert or handshake */
322 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
323 s->result |= SRV_CHK_RUNNING;
324 else
325 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200326 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100327 else if (s->proxy->options & PR_O_SMTP_CHK) {
328 /* Check for SMTP code 2xx (should be 250) */
329 if ((len >= 3) && (trash[0] == '2'))
330 s->result |= SRV_CHK_RUNNING;
331 else
332 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200333 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100334 else {
335 /* other checks are valid if the connection succeeded anyway */
336 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200337 }
Willy Tarreau83749182007-04-15 20:56:27 +0200338
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100339 out_wakeup:
340 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341 fdtab[fd].state = FD_STERROR;
342
Willy Tarreauf161a342007-04-08 16:59:42 +0200343 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200344 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200345 fdtab[fd].ev &= ~FD_POLL_RD;
346 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347}
348
349/*
350 * manages a server health-check. Returns
351 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
352 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200353void process_chk(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200355 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 struct server *s = t->context;
357 struct sockaddr_in sa;
Willy Tarreau48494c02007-11-30 10:41:39 +0100358 int xferred;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200360 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361
362 //fprintf(stderr, "process_chk: task=%p\n", t);
363
364 new_chk:
365 fd = s->curfd;
366 if (fd < 0) { /* no check currently running */
367 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200368 if (!tv_isle(&t->expire, &now)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200370 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200371 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 }
373
374 /* we don't send any health-checks when the proxy is stopped or when
375 * the server should not be checked.
376 */
377 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200378 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200379 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200380 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200381 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200382 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383 }
384
385 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100386 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
388 if ((fd < global.maxsock) &&
389 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
390 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
391 //fprintf(stderr, "process_chk: 3\n");
392
Willy Tarreau9edd1612007-10-18 18:07:48 +0200393 if (s->proxy->options & PR_O_TCP_NOLING) {
394 /* We don't want to useless data */
395 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
396 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200397
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200398 if (s->check_addr.sin_addr.s_addr)
399 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200400 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200401 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200402 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200403 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200404
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 sa.sin_port = htons(s->check_port);
407
408 /* allow specific binding :
409 * - server-specific at first
410 * - proxy-specific next
411 */
412 if (s->state & SRV_BIND_SRC) {
413 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
414 if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) {
415 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
416 s->proxy->id, s->id);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100417 s->result |= SRV_CHK_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100419#ifdef CONFIG_HAP_CTTPROXY
420 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
421 struct in_tproxy itp1, itp2;
422 memset(&itp1, 0, sizeof(itp1));
423
424 itp1.op = TPROXY_ASSIGN;
425 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
426 itp1.v.addr.fport = s->tproxy_addr.sin_port;
427
428 /* set connect flag on socket */
429 itp2.op = TPROXY_FLAGS;
430 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
431
432 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
433 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
434 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
435 s->proxy->id, s->id);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100436 s->result |= SRV_CHK_ERROR;
Willy Tarreau163c5322006-11-14 16:18:41 +0100437 }
438 }
439#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 }
441 else if (s->proxy->options & PR_O_BIND_SRC) {
442 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
443 if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100444 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
445 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100446 s->result |= SRV_CHK_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100448#ifdef CONFIG_HAP_CTTPROXY
449 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
450 struct in_tproxy itp1, itp2;
451 memset(&itp1, 0, sizeof(itp1));
452
453 itp1.op = TPROXY_ASSIGN;
454 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
455 itp1.v.addr.fport = s->tproxy_addr.sin_port;
456
457 /* set connect flag on socket */
458 itp2.op = TPROXY_FLAGS;
459 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
460
461 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
462 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100463 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
464 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100465 s->result |= SRV_CHK_ERROR;
Willy Tarreau163c5322006-11-14 16:18:41 +0100466 }
467 }
468#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 }
470
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100471 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200472 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
473 /* OK, connection in progress or established */
474
475 //fprintf(stderr, "process_chk: 4\n");
476
477 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200478 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200480 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
481 fdtab[fd].cb[DIR_RD].b = NULL;
482 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
483 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200484 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
485 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200486 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200487 fdtab[fd].ev = 0;
Willy Tarreauf161a342007-04-08 16:59:42 +0200488 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200490 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200493 tv_ms_add(&t->expire, &now, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200495 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200496 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200497 }
498 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100499 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200500 }
501 }
502 }
503 close(fd); /* socket creation error */
504 }
505
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100506 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200507 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200508 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200509 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200510 goto new_chk; /* may be we should initialize a new check */
511 }
512
513 /* here, we have seen a failure */
514 if (s->health > s->rise) {
515 s->health--; /* still good */
516 s->failed_checks++;
517 }
518 else
519 set_server_down(s);
520
521 //fprintf(stderr, "process_chk: 7\n");
522 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200523 while (tv_isle(&t->expire, &now))
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200524 tv_ms_add(&t->expire, &t->expire, s->inter);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200525 goto new_chk;
526 }
527 else {
528 //fprintf(stderr, "process_chk: 8\n");
529 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100530 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200532
Willy Tarreau48494c02007-11-30 10:41:39 +0100533 /* we may have to add/remove this server from the LB group */
534 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
535 if ((s->state & SRV_GOINGDOWN) &&
536 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING)) {
537 /* server enabled again */
538 s->state &= ~SRV_GOINGDOWN;
539 s->proxy->lbprm.set_server_status_up(s);
540
541 /* check if we can handle some connections queued at the proxy. We
542 * will take as many as we can handle.
543 */
544 xferred = check_for_pending(s);
545
546 sprintf(trash,
547 "Load-balancing on %sServer %s/%s is enabled again. %d active and %d backup servers online.%s"
548 " %d sessions requeued, %d total in queue.\n",
549 s->state & SRV_BACKUP ? "Backup " : "",
550 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
551 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
552 xferred, s->nbpend);
553
554 Warning("%s", trash);
555 send_log(s->proxy, LOG_NOTICE, "%s", trash);
556 }
557 else if (!(s->state & SRV_GOINGDOWN) &&
558 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
559 (SRV_CHK_RUNNING | SRV_CHK_DISABLE))) {
560 /* server disabled */
561 s->state |= SRV_GOINGDOWN;
562 s->proxy->lbprm.set_server_status_down(s);
563
564 /* we might have sessions queued on this server and waiting for
565 * a connection. Those which are redispatchable will be queued
566 * to another server or to the proxy itself.
567 */
568 xferred = redistribute_pending(s);
569
570 sprintf(trash,
571 "Load-balancing on %sServer %s/%s is disabled. %d active and %d backup servers online.%s"
572 " %d sessions requeued, %d total in queue.\n",
573 s->state & SRV_BACKUP ? "Backup " : "",
574 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
575 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
576 xferred, s->nbpend);
577
578 Warning("%s", trash);
579
580 send_log(s->proxy, LOG_NOTICE, "%s", trash);
581 if (!s->proxy->srv_bck && !s->proxy->srv_act)
582 set_backend_down(s->proxy);
583 }
584 }
585
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200586 if (s->health < s->rise + s->fall - 1) {
587 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588
589 if (s->health == s->rise) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200590 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
591 if (s->proxy->last_change < now.tv_sec) // ignore negative times
592 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
593 s->proxy->last_change = now.tv_sec;
594 }
595
Willy Tarreaub625a082007-11-26 01:15:43 +0100596 if (s->last_change < now.tv_sec) // ignore negative times
597 s->down_time += now.tv_sec - s->last_change;
598
599 s->last_change = now.tv_sec;
600 s->state |= SRV_RUNNING;
601 s->proxy->lbprm.set_server_status_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602
603 /* check if we can handle some connections queued at the proxy. We
604 * will take as many as we can handle.
605 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100606 xferred = check_for_pending(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200607
608 sprintf(trash,
609 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
610 " %d sessions requeued, %d total in queue.\n",
611 s->state & SRV_BACKUP ? "Backup " : "",
612 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
613 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
614 xferred, s->nbpend);
615
616 Warning("%s", trash);
617 send_log(s->proxy, LOG_NOTICE, "%s", trash);
618 }
619
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200620 if (s->health >= s->rise)
621 s->health = s->rise + s->fall - 1; /* OK now */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622 }
623 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200625
626 rv = 0;
627 if (global.spread_checks > 0) {
628 rv = s->inter * global.spread_checks / 100;
629 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
630 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
631 }
632 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633 goto new_chk;
634 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100635 else if ((s->result & SRV_CHK_ERROR) || tv_isle(&t->expire, &now)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 //fprintf(stderr, "process_chk: 10\n");
637 /* failure or timeout detected */
638 if (s->health > s->rise) {
639 s->health--; /* still good */
640 s->failed_checks++;
641 }
642 else
643 set_server_down(s);
644 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200646
647 rv = 0;
648 if (global.spread_checks > 0) {
649 rv = s->inter * global.spread_checks / 100;
650 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200651 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, s->inter, global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200652 }
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200653 tv_ms_add(&t->expire, &now, s->inter + rv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654 goto new_chk;
655 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100656 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 }
658 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100659 s->result = SRV_CHK_UNKNOWN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200661 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200662 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200663 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664}
665
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200666/*
667 * Start health-check.
668 * Returns 0 if OK, -1 if error, and prints the error in this case.
669 */
670int start_checks() {
671
672 struct proxy *px;
673 struct server *s;
674 struct task *t;
675 int nbchk=0, mininter=0, srvpos=0;
676
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200677 /* 1- count the checkers to run simultaneously.
678 * We also determine the minimum interval among all of those which
679 * have an interval larger than SRV_CHK_INTER_THRES. This interval
680 * will be used to spread their start-up date. Those which have
681 * a shorter interval will start independantly and will not dictate
682 * too short an interval for all others.
683 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200684 for (px = proxy; px; px = px->next) {
685 for (s = px->srv; s; s = s->next) {
686 if (!(s->state & SRV_CHECKED))
687 continue;
688
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200689 if ((s->inter >= SRV_CHK_INTER_THRES) &&
690 (!mininter || mininter > s->inter))
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200691 mininter = s->inter;
692
693 nbchk++;
694 }
695 }
696
697 if (!nbchk)
698 return 0;
699
700 srand((unsigned)time(NULL));
701
702 /*
703 * 2- start them as far as possible from each others. For this, we will
704 * start them after their interval set to the min interval divided by
705 * the number of servers, weighted by the server's position in the list.
706 */
707 for (px = proxy; px; px = px->next) {
708 for (s = px->srv; s; s = s->next) {
709 if (!(s->state & SRV_CHECKED))
710 continue;
711
712 if ((t = pool_alloc2(pool2_task)) == NULL) {
713 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
714 return -1;
715 }
716
717 t->wq = NULL;
718 t->qlist.p = NULL;
719 t->state = TASK_IDLE;
720 t->process = process_chk;
721 t->context = s;
722
723 /* check this every ms */
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200724 tv_ms_add(&t->expire, &now,
725 ((mininter && mininter >= s->inter) ? mininter : s->inter) * srvpos / nbchk);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200726 task_queue(t);
727
728 srvpos++;
729 }
730 }
731 return 0;
732}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733
734/*
735 * Local variables:
736 * c-indent-level: 8
737 * c-basic-offset: 8
738 * End:
739 */