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