blob: 054571e0681b4dbcc747813d9c23067e9c799a8b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
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>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020016#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020017#include <unistd.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
23#include <common/config.h>
24#include <common/mini-clist.h>
25#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#include <types/global.h>
28#include <types/polling.h>
29#include <types/proxy.h>
30#include <types/session.h>
31
32#include <proto/backend.h>
33#include <proto/fd.h>
34#include <proto/log.h>
35#include <proto/queue.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010036#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/server.h>
38#include <proto/task.h>
39
Willy Tarreau163c5322006-11-14 16:18:41 +010040#ifdef CONFIG_HAP_CTTPROXY
41#include <import/ip_tproxy.h>
42#endif
43
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
45/* Sets server <s> down, notifies by all available means, recounts the
46 * remaining servers on the proxy and transfers queued sessions whenever
47 * possible to other servers.
48 */
49void set_server_down(struct server *s)
50{
51 struct pendconn *pc, *pc_bck, *pc_end;
52 struct session *sess;
53 int xferred;
54
55 s->state &= ~SRV_RUNNING;
56
57 if (s->health == s->rise) {
58 recount_servers(s->proxy);
59 recalc_server_map(s->proxy);
60
61 /* we might have sessions queued on this server and waiting for
62 * a connection. Those which are redispatchable will be queued
63 * to another server or to the proxy itself.
64 */
65 xferred = 0;
66 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
67 sess = pc->sess;
Willy Tarreau830ff452006-12-17 19:31:23 +010068 if ((sess->be->beprm->options & PR_O_REDISP)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020069 /* The REDISP option was specified. We will ignore
70 * cookie and force to balance or use the dispatcher.
71 */
72 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
73 sess->srv = NULL; /* it's left to the dispatcher to choose a server */
74 if ((sess->flags & SN_CK_MASK) == SN_CK_VALID) {
75 sess->flags &= ~SN_CK_MASK;
76 sess->flags |= SN_CK_DOWN;
77 }
78 pendconn_free(pc);
79 task_wakeup(&rq, sess->task);
80 xferred++;
81 }
82 }
83
84 sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s"
85 " %d sessions active, %d requeued, %d remaining in queue.\n",
86 s->state & SRV_BACKUP ? "Backup " : "",
87 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
88 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
89 s->cur_sess, xferred, s->nbpend);
90
91 Warning("%s", trash);
92 send_log(s->proxy, LOG_ALERT, "%s", trash);
93
94 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +010095 Alert("%s '%s' has no server available !\n", proxy_type_str(s->proxy), s->proxy->id);
96 send_log(s->proxy, LOG_EMERG, "%s %s has no server available !\n", proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 }
98 s->down_trans++;
99 }
100 s->health = 0; /* failure */
101}
102
103
104/*
105 * This function is used only for server health-checks. It handles
106 * the connection acknowledgement. If the proxy requires HTTP health-checks,
107 * it sends the request. In other cases, it returns 1 if the socket is OK,
108 * or -1 if an error occured.
109 */
110int event_srv_chk_w(int fd)
111{
112 struct task *t = fdtab[fd].owner;
113 struct server *s = t->context;
114 int skerr;
115 socklen_t lskerr = sizeof(skerr);
116
117 skerr = 1;
118 if ((getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1)
119 || (skerr != 0)) {
120 /* in case of TCP only, this tells us if the connection failed */
121 s->result = -1;
122 fdtab[fd].state = FD_STERROR;
Willy Tarreau2a429502006-10-15 14:52:29 +0200123 MY_FD_CLR(fd, StaticWriteEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 }
125 else if (s->result != -1) {
126 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200127 if ((s->proxy->options & PR_O_HTTP_CHK) ||
128 (s->proxy->options & PR_O_SSL3_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200130 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131 * so we'll send the request, and won't wake the checker up now.
132 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200133
134 if (s->proxy->options & PR_O_SSL3_CHK) {
135 /* SSL requires that we put Unix time in the request */
136 int gmt_time = htonl(now.tv_sec);
137 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
138 }
139
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140#ifndef MSG_NOSIGNAL
141 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
142#else
143 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
144#endif
145 if (ret == s->proxy->check_len) {
Willy Tarreau2a429502006-10-15 14:52:29 +0200146 MY_FD_SET(fd, StaticReadEvent); /* prepare for reading reply */
147 MY_FD_CLR(fd, StaticWriteEvent); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148 return 0;
149 }
150 else {
151 s->result = -1;
Willy Tarreau2a429502006-10-15 14:52:29 +0200152 MY_FD_CLR(fd, StaticWriteEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153 }
154 }
155 else {
156 /* good TCP connection is enough */
157 s->result = 1;
158 }
159 }
160
161 task_wakeup(&rq, t);
162 return 0;
163}
164
165
166/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200167 * This function is used only for server health-checks. It handles the server's
168 * reply to an HTTP request or SSL HELLO. It returns 1 in s->result if the
169 * server replies HTTP 2xx or 3xx (valid responses), or if it returns at least
170 * 5 bytes in response to SSL HELLO. The principle is that this is enough to
171 * distinguish between an SSL server and a pure TCP relay. All other cases will
172 * return -1. The function returns 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 */
174int event_srv_chk_r(int fd)
175{
176 char reply[64];
177 int len, result;
178 struct task *t = fdtab[fd].owner;
179 struct server *s = t->context;
180 int skerr;
181 socklen_t lskerr = sizeof(skerr);
182
183 result = len = -1;
Willy Tarreauc6423482006-10-15 14:59:03 +0200184 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && !skerr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185#ifndef MSG_NOSIGNAL
186 len = recv(fd, reply, sizeof(reply), 0);
187#else
188 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
189 * but the connection was closed on the remote end. Fortunately, recv still
190 * works correctly and we don't need to do the getsockopt() on linux.
191 */
192 len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL);
193#endif
Willy Tarreauf3c69202006-07-09 16:42:34 +0200194 if (((s->proxy->options & PR_O_HTTP_CHK) &&
195 (len >= sizeof("HTTP/1.0 000")) &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196 !memcmp(reply, "HTTP/1.", 7) &&
197 (reply[9] == '2' || reply[9] == '3')) /* 2xx or 3xx */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200198 || ((s->proxy->options & PR_O_SSL3_CHK) && (len >= 5) &&
199 (reply[0] == 0x15 || reply[0] == 0x16))) /* alert or handshake */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200 result = 1;
201 }
202
203 if (result == -1)
204 fdtab[fd].state = FD_STERROR;
205
206 if (s->result != -1)
207 s->result = result;
208
Willy Tarreau2a429502006-10-15 14:52:29 +0200209 MY_FD_CLR(fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210 task_wakeup(&rq, t);
211 return 0;
212}
213
214/*
215 * manages a server health-check. Returns
216 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
217 */
218int process_chk(struct task *t)
219{
220 struct server *s = t->context;
221 struct sockaddr_in sa;
222 int fd;
223
224 //fprintf(stderr, "process_chk: task=%p\n", t);
225
226 new_chk:
227 fd = s->curfd;
228 if (fd < 0) { /* no check currently running */
229 //fprintf(stderr, "process_chk: 2\n");
230 if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */
231 task_queue(t); /* restore t to its place in the task list */
232 return tv_remain2(&now, &t->expire);
233 }
234
235 /* we don't send any health-checks when the proxy is stopped or when
236 * the server should not be checked.
237 */
238 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
239 while (tv_cmp2_ms(&t->expire, &now) <= 0)
240 tv_delayfrom(&t->expire, &t->expire, s->inter);
241 task_queue(t); /* restore t to its place in the task list */
242 return tv_remain2(&now, &t->expire);
243 }
244
245 /* we'll initiate a new check */
246 s->result = 0; /* no result yet */
247 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
248 if ((fd < global.maxsock) &&
249 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
250 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
251 //fprintf(stderr, "process_chk: 3\n");
252
253 /* we'll connect to the check port on the server */
254 sa = s->addr;
255 sa.sin_port = htons(s->check_port);
256
257 /* allow specific binding :
258 * - server-specific at first
259 * - proxy-specific next
260 */
261 if (s->state & SRV_BIND_SRC) {
262 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
263 if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) {
264 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
265 s->proxy->id, s->id);
266 s->result = -1;
267 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100268#ifdef CONFIG_HAP_CTTPROXY
269 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
270 struct in_tproxy itp1, itp2;
271 memset(&itp1, 0, sizeof(itp1));
272
273 itp1.op = TPROXY_ASSIGN;
274 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
275 itp1.v.addr.fport = s->tproxy_addr.sin_port;
276
277 /* set connect flag on socket */
278 itp2.op = TPROXY_FLAGS;
279 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
280
281 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
282 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
283 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
284 s->proxy->id, s->id);
285 s->result = -1;
286 }
287 }
288#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289 }
290 else if (s->proxy->options & PR_O_BIND_SRC) {
291 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
292 if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100293 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
294 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295 s->result = -1;
296 }
Willy Tarreau163c5322006-11-14 16:18:41 +0100297#ifdef CONFIG_HAP_CTTPROXY
298 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
299 struct in_tproxy itp1, itp2;
300 memset(&itp1, 0, sizeof(itp1));
301
302 itp1.op = TPROXY_ASSIGN;
303 itp1.v.addr.faddr = s->tproxy_addr.sin_addr;
304 itp1.v.addr.fport = s->tproxy_addr.sin_port;
305
306 /* set connect flag on socket */
307 itp2.op = TPROXY_FLAGS;
308 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
309
310 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
311 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100312 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
313 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreau163c5322006-11-14 16:18:41 +0100314 s->result = -1;
315 }
316 }
317#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318 }
319
320 if (!s->result) {
321 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
322 /* OK, connection in progress or established */
323
324 //fprintf(stderr, "process_chk: 4\n");
325
326 s->curfd = fd; /* that's how we know a test is in progress ;-) */
327 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200328 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
329 fdtab[fd].cb[DIR_RD].b = NULL;
330 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
331 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreau2a429502006-10-15 14:52:29 +0200333 MY_FD_SET(fd, StaticWriteEvent); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334#ifdef DEBUG_FULL
Willy Tarreau2a429502006-10-15 14:52:29 +0200335 assert (!MY_FD_ISSET(fd, StaticReadEvent));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336#endif
337 fd_insert(fd);
338 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
339 tv_delayfrom(&t->expire, &now, s->inter);
340 task_queue(t); /* restore t to its place in the task list */
341 return tv_remain(&now, &t->expire);
342 }
343 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
344 s->result = -1; /* a real error */
345 }
346 }
347 }
348 close(fd); /* socket creation error */
349 }
350
351 if (!s->result) { /* nothing done */
352 //fprintf(stderr, "process_chk: 6\n");
353 while (tv_cmp2_ms(&t->expire, &now) <= 0)
354 tv_delayfrom(&t->expire, &t->expire, s->inter);
355 goto new_chk; /* may be we should initialize a new check */
356 }
357
358 /* here, we have seen a failure */
359 if (s->health > s->rise) {
360 s->health--; /* still good */
361 s->failed_checks++;
362 }
363 else
364 set_server_down(s);
365
366 //fprintf(stderr, "process_chk: 7\n");
367 /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */
368 while (tv_cmp2_ms(&t->expire, &now) <= 0)
369 tv_delayfrom(&t->expire, &t->expire, s->inter);
370 goto new_chk;
371 }
372 else {
373 //fprintf(stderr, "process_chk: 8\n");
374 /* there was a test running */
375 if (s->result > 0) { /* good server detected */
376 //fprintf(stderr, "process_chk: 9\n");
377 s->health++; /* was bad, stays for a while */
378 if (s->health >= s->rise) {
379 s->state |= SRV_RUNNING;
380
381 if (s->health == s->rise) {
382 int xferred;
383
384 recount_servers(s->proxy);
385 recalc_server_map(s->proxy);
386
387 /* check if we can handle some connections queued at the proxy. We
388 * will take as many as we can handle.
389 */
390 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
391 struct session *sess;
392 struct pendconn *p;
393
394 p = pendconn_from_px(s->proxy);
395 if (!p)
396 break;
397 p->sess->srv = s;
398 sess = p->sess;
399 pendconn_free(p);
400 task_wakeup(&rq, sess->task);
401 }
402
403 sprintf(trash,
404 "%sServer %s/%s is UP. %d active and %d backup servers online.%s"
405 " %d sessions requeued, %d total in queue.\n",
406 s->state & SRV_BACKUP ? "Backup " : "",
407 s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck,
408 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
409 xferred, s->nbpend);
410
411 Warning("%s", trash);
412 send_log(s->proxy, LOG_NOTICE, "%s", trash);
413 }
414
415 s->health = s->rise + s->fall - 1; /* OK now */
416 }
417 s->curfd = -1; /* no check running anymore */
Willy Tarreau2a429502006-10-15 14:52:29 +0200418 //MY_FD_CLR(fd, StaticWriteEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419 fd_delete(fd);
420 while (tv_cmp2_ms(&t->expire, &now) <= 0)
421 tv_delayfrom(&t->expire, &t->expire, s->inter);
422 goto new_chk;
423 }
424 else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) {
425 //fprintf(stderr, "process_chk: 10\n");
426 /* failure or timeout detected */
427 if (s->health > s->rise) {
428 s->health--; /* still good */
429 s->failed_checks++;
430 }
431 else
432 set_server_down(s);
433 s->curfd = -1;
Willy Tarreau2a429502006-10-15 14:52:29 +0200434 //MY_FD_CLR(fd, StaticWriteEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435 fd_delete(fd);
436 while (tv_cmp2_ms(&t->expire, &now) <= 0)
437 tv_delayfrom(&t->expire, &t->expire, s->inter);
438 goto new_chk;
439 }
440 /* if result is 0 and there's no timeout, we have to wait again */
441 }
442 //fprintf(stderr, "process_chk: 11\n");
443 s->result = 0;
444 task_queue(t); /* restore t to its place in the task list */
445 return tv_remain2(&now, &t->expire);
446}
447
448
449/*
450 * Local variables:
451 * c-indent-level: 8
452 * c-basic-offset: 8
453 * End:
454 */