blob: 278f0c1c2a47890a32df9ea0ff12ada5e827b11a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
18#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
23#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040024#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020026#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <arpa/inet.h>
28
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020032#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
35#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020038#include <proto/checks.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010039#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/fd.h>
41#include <proto/log.h>
42#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020043#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010044#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010045#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010046#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/server.h>
48#include <proto/task.h>
49
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020050const char *check_status_description[HCHK_STATUS_SIZE] = {
51 [HCHK_STATUS_UNKNOWN] = "Unknown",
52
53 [HCHK_STATUS_INI] = "Initializing",
54
55 [HCHK_STATUS_SOCKERR] = "Socket error",
56
57 [HCHK_STATUS_L4OK] = "Layer4 check passed",
58 [HCHK_STATUS_L4TOUT] = "Layer4 timeout",
59 [HCHK_STATUS_L4CON] = "Layer4 connection problem",
60
61 [HCHK_STATUS_L6OK] = "Layer6 check passed",
62 [HCHK_STATUS_L6TOUT] = "Layer6 timeout",
63 [HCHK_STATUS_L6RSP] = "Layer6 invalid response",
64
65 [HCHK_STATUS_L7TOUT] = "Layer7 timeout",
66 [HCHK_STATUS_L7RSP] = "Layer7 invalid response",
67
68 [HCHK_STATUS_L7OKD] = "Layer7 check passed",
69 [HCHK_STATUS_L7OKCD] = "Layer7 check conditionally passed",
70 [HCHK_STATUS_L7STS] = "Layer7 wrong status",
71};
72
73
74const char *check_status_info[HCHK_STATUS_SIZE] = {
75 [HCHK_STATUS_UNKNOWN] = "UNK",
76
77 [HCHK_STATUS_INI] = "INI",
78
79 [HCHK_STATUS_SOCKERR] = "SOCKERR",
80
81 [HCHK_STATUS_L4OK] = "L4OK",
82 [HCHK_STATUS_L4TOUT] = "L4TOUT",
83 [HCHK_STATUS_L4CON] = "L4CON",
84
85 [HCHK_STATUS_L6OK] = "L6OK",
86 [HCHK_STATUS_L6TOUT] = "L6TOUT",
87 [HCHK_STATUS_L6RSP] = "L6RSP",
88
89 [HCHK_STATUS_L7TOUT] = "L7TOUT",
90 [HCHK_STATUS_L7RSP] = "L7RSP",
91
92 [HCHK_STATUS_L7OKD] = "L7OK",
93 [HCHK_STATUS_L7OKCD] = "L7OKC",
94 [HCHK_STATUS_L7STS] = "L7STS",
95};
96
97/*
98 * Convert check_status code to description
99 */
100const char *get_check_status_description(short check_status) {
101
102 const char *desc;
103
104 if (check_status < HCHK_STATUS_SIZE)
105 desc = check_status_description[check_status];
106 else
107 desc = NULL;
108
109 if (desc && *desc)
110 return desc;
111 else
112 return check_status_description[HCHK_STATUS_UNKNOWN];
113}
114
115/*
116 * Convert check_status code to short info
117 */
118const char *get_check_status_info(short check_status) {
119
120 const char *info;
121
122 if (check_status < HCHK_STATUS_SIZE)
123 info = check_status_info[check_status];
124 else
125 info = NULL;
126
127 if (info && *info)
128 return info;
129 else
130 return check_status_info[HCHK_STATUS_UNKNOWN];
131}
132
133/*
134 * Set check_status and update check_duration
135 */
136static void set_server_check_status(struct server *s, short status) {
137
138 if (tv_iszero(&s->check_start))
139 return;
140
141 s->check_status = status;
142 s->check_duration = tv_ms_elapsed(&s->check_start, &now);
143 tv_zero(&s->check_start);
144}
145
Willy Tarreau48494c02007-11-30 10:41:39 +0100146/* sends a log message when a backend goes down, and also sets last
147 * change date.
148 */
149static void set_backend_down(struct proxy *be)
150{
151 be->last_change = now.tv_sec;
152 be->down_trans++;
153
154 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
155 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
156}
157
158/* Redistribute pending connections when a server goes down. The number of
159 * connections redistributed is returned.
160 */
161static int redistribute_pending(struct server *s)
162{
163 struct pendconn *pc, *pc_bck, *pc_end;
164 int xferred = 0;
165
166 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
167 struct session *sess = pc->sess;
168 if (sess->be->options & PR_O_REDISP) {
169 /* The REDISP option was specified. We will ignore
170 * cookie and force to balance or use the dispatcher.
171 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100172
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100173 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100174 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100175
Willy Tarreau48494c02007-11-30 10:41:39 +0100176 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200177 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100178 xferred++;
179 }
180 }
181 return xferred;
182}
183
184/* Check for pending connections at the backend, and assign some of them to
185 * the server coming up. The server's weight is checked before being assigned
186 * connections it may not be able to handle. The total number of transferred
187 * connections is returned.
188 */
189static int check_for_pending(struct server *s)
190{
191 int xferred;
192
193 if (!s->eweight)
194 return 0;
195
196 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
197 struct session *sess;
198 struct pendconn *p;
199
200 p = pendconn_from_px(s->proxy);
201 if (!p)
202 break;
203 p->sess->srv = s;
204 sess = p->sess;
205 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200206 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100207 }
208 return xferred;
209}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210
211/* Sets server <s> down, notifies by all available means, recounts the
212 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200213 * possible to other servers. It automatically recomputes the number of
214 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 */
Willy Tarreau83749182007-04-15 20:56:27 +0200216static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100218 struct server *srv;
219 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200220 int xferred;
221
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100222 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100223 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200224
225 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100226 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100227 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228
229 /* we might have sessions queued on this server and waiting for
230 * a connection. Those which are redispatchable will be queued
231 * to another server or to the proxy itself.
232 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100233 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100234
235 msg.len = 0;
236 msg.str = trash;
237
238 chunk_printf(&msg, sizeof(trash),
239 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
240 s->proxy->id, s->id);
241
242 if (s->tracked)
243 chunk_printf(&msg, sizeof(trash), " via %s/%s",
244 s->tracked->proxy->id, s->tracked->id);
245
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200246 chunk_printf(&msg, sizeof(trash), ", reason: %s", get_check_status_description(s->check_status));
247 if (s->check_status >= HCHK_STATUS_L57DATA)
248 chunk_printf(&msg, sizeof(trash), ", code: %d", s->check_code);
249
250 chunk_printf(&msg, sizeof(trash), ", check duration: %lums", s->check_duration);
251
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100252 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers left.%s"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253 " %d sessions active, %d requeued, %d remaining in queue.\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100254 s->proxy->srv_act, s->proxy->srv_bck,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
256 s->cur_sess, xferred, s->nbpend);
257
258 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200259
Willy Tarreau48494c02007-11-30 10:41:39 +0100260 /* we don't send an alert if the server was previously paused */
261 if (srv_was_paused)
262 send_log(s->proxy, LOG_NOTICE, "%s", trash);
263 else
264 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200265
Willy Tarreau48494c02007-11-30 10:41:39 +0100266 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
267 set_backend_down(s->proxy);
268
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 s->down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100270
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100271 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100272 for(srv = s->tracknext; srv; srv = srv->tracknext)
273 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100275
Willy Tarreaubaaee002006-06-26 02:48:02 +0200276 s->health = 0; /* failure */
277}
278
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100279static void set_server_up(struct server *s) {
280
281 struct server *srv;
282 struct chunk msg;
283 int xferred;
284
285 if (s->health == s->rise || s->tracked) {
286 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
287 if (s->proxy->last_change < now.tv_sec) // ignore negative times
288 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
289 s->proxy->last_change = now.tv_sec;
290 }
291
292 if (s->last_change < now.tv_sec) // ignore negative times
293 s->down_time += now.tv_sec - s->last_change;
294
295 s->last_change = now.tv_sec;
296 s->state |= SRV_RUNNING;
297
298 if (s->slowstart > 0) {
299 s->state |= SRV_WARMINGUP;
300 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
301 /* For dynamic algorithms, start at the first step of the weight,
302 * without multiplying by BE_WEIGHT_SCALE.
303 */
304 s->eweight = s->uweight;
305 if (s->proxy->lbprm.update_server_eweight)
306 s->proxy->lbprm.update_server_eweight(s);
307 }
308 }
309 s->proxy->lbprm.set_server_status_up(s);
310
311 /* check if we can handle some connections queued at the proxy. We
312 * will take as many as we can handle.
313 */
314 xferred = check_for_pending(s);
315
316 msg.len = 0;
317 msg.str = trash;
318
319 chunk_printf(&msg, sizeof(trash),
320 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
321 s->proxy->id, s->id);
322
323 if (s->tracked)
324 chunk_printf(&msg, sizeof(trash), " via %s/%s",
325 s->tracked->proxy->id, s->tracked->id);
326
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200327 chunk_printf(&msg, sizeof(trash), ", reason: %s", get_check_status_description(s->check_status));
328 if (s->check_status >= HCHK_STATUS_L57DATA)
329 chunk_printf(&msg, sizeof(trash), ", code: %d", s->check_code);
330
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100331 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
332 " %d sessions requeued, %d total in queue.\n",
333 s->proxy->srv_act, s->proxy->srv_bck,
334 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreau1772ece2009-04-03 14:49:12 +0200335 xferred, s->nbpend);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100336
337 Warning("%s", trash);
338 send_log(s->proxy, LOG_NOTICE, "%s", trash);
339
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100340 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100341 for(srv = s->tracknext; srv; srv = srv->tracknext)
342 set_server_up(srv);
343 }
344
345 if (s->health >= s->rise)
346 s->health = s->rise + s->fall - 1; /* OK now */
347
348}
349
350static void set_server_disabled(struct server *s) {
351
352 struct server *srv;
353 struct chunk msg;
354 int xferred;
355
356 s->state |= SRV_GOINGDOWN;
357 s->proxy->lbprm.set_server_status_down(s);
358
359 /* we might have sessions queued on this server and waiting for
360 * a connection. Those which are redispatchable will be queued
361 * to another server or to the proxy itself.
362 */
363 xferred = redistribute_pending(s);
364
365 msg.len = 0;
366 msg.str = trash;
367
368 chunk_printf(&msg, sizeof(trash),
369 "Load-balancing on %sServer %s/%s is disabled",
370 s->state & SRV_BACKUP ? "Backup " : "",
371 s->proxy->id, s->id);
372
373 if (s->tracked)
374 chunk_printf(&msg, sizeof(trash), " via %s/%s",
375 s->tracked->proxy->id, s->tracked->id);
376
377
378 chunk_printf(&msg, sizeof(trash),". %d active and %d backup servers online.%s"
379 " %d sessions requeued, %d total in queue.\n",
380 s->proxy->srv_act, s->proxy->srv_bck,
381 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
382 xferred, s->nbpend);
383
384 Warning("%s", trash);
385
386 send_log(s->proxy, LOG_NOTICE, "%s", trash);
387
388 if (!s->proxy->srv_bck && !s->proxy->srv_act)
389 set_backend_down(s->proxy);
390
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100391 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100392 for(srv = s->tracknext; srv; srv = srv->tracknext)
393 set_server_disabled(srv);
394}
395
396static void set_server_enabled(struct server *s) {
397
398 struct server *srv;
399 struct chunk msg;
400 int xferred;
401
402 s->state &= ~SRV_GOINGDOWN;
403 s->proxy->lbprm.set_server_status_up(s);
404
405 /* check if we can handle some connections queued at the proxy. We
406 * will take as many as we can handle.
407 */
408 xferred = check_for_pending(s);
409
410 msg.len = 0;
411 msg.str = trash;
412
413 chunk_printf(&msg, sizeof(trash),
414 "Load-balancing on %sServer %s/%s is enabled again",
415 s->state & SRV_BACKUP ? "Backup " : "",
416 s->proxy->id, s->id);
417
418 if (s->tracked)
419 chunk_printf(&msg, sizeof(trash), " via %s/%s",
420 s->tracked->proxy->id, s->tracked->id);
421
422 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
423 " %d sessions requeued, %d total in queue.\n",
424 s->proxy->srv_act, s->proxy->srv_bck,
425 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
426 xferred, s->nbpend);
427
428 Warning("%s", trash);
429 send_log(s->proxy, LOG_NOTICE, "%s", trash);
430
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100431 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100432 for(srv = s->tracknext; srv; srv = srv->tracknext)
433 set_server_enabled(srv);
434}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435
436/*
437 * This function is used only for server health-checks. It handles
438 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100439 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200440 * The function itself returns 0 if it needs some polling before being called
441 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 */
Willy Tarreau83749182007-04-15 20:56:27 +0200443static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444{
Willy Tarreau6996e152007-04-30 14:37:43 +0200445 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446 struct task *t = fdtab[fd].owner;
447 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100449 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200450 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
451 set_server_check_status(s, HCHK_STATUS_L4CON);
Willy Tarreau6996e152007-04-30 14:37:43 +0200452 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200453 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200454
455 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200456
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100457 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200459 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200460 (s->proxy->options & PR_O_SSL3_CHK) ||
461 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200463 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464 * so we'll send the request, and won't wake the checker up now.
465 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200466
467 if (s->proxy->options & PR_O_SSL3_CHK) {
468 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200469 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200470 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
471 }
472
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100475 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau7cd9d942008-12-21 13:00:41 +0100476 if (s->proxy->timeout.check)
477 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200478 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200479 goto out_nowake;
480 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200481 else if (ret == 0 || errno == EAGAIN)
482 goto out_poll;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200483 else {
484 switch (errno) {
485 case ECONNREFUSED:
486 case ENETUNREACH:
487 set_server_check_status(s, HCHK_STATUS_L4CON);
488 break;
489
490 default:
491 set_server_check_status(s, HCHK_STATUS_SOCKERR);
492 }
493
Willy Tarreau6996e152007-04-30 14:37:43 +0200494 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200495 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200496 }
497 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200498 /* We have no data to send to check the connection, and
499 * getsockopt() will not inform us whether the connection
500 * is still pending. So we'll reuse connect() to check the
501 * state of the socket. This has the advantage of givig us
502 * the following info :
503 * - error
504 * - connecting (EALREADY, EINPROGRESS)
505 * - connected (EISCONN, 0)
506 */
507
508 struct sockaddr_in sa;
509
510 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
511 sa.sin_port = htons(s->check_port);
512
513 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
514 errno = 0;
515
516 if (errno == EALREADY || errno == EINPROGRESS)
517 goto out_poll;
518
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200519 if (errno && errno != EISCONN) {
520 set_server_check_status(s, HCHK_STATUS_L4CON);
Willy Tarreau6996e152007-04-30 14:37:43 +0200521 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200522 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200523
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100525 s->result |= SRV_CHK_RUNNING;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200526 set_server_check_status(s, HCHK_STATUS_L4OK);
Willy Tarreau6996e152007-04-30 14:37:43 +0200527 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528 }
529 }
Willy Tarreau83749182007-04-15 20:56:27 +0200530 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200531 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200532 out_nowake:
533 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100534 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200535 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200536 out_poll:
537 /* The connection is still pending. We'll have to poll it
538 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100539 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200540 return 0;
541 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100542 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200543 /* set_server_check_status() called bofore goto into this label */
Willy Tarreau6996e152007-04-30 14:37:43 +0200544 fdtab[fd].state = FD_STERROR;
545 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546}
547
548
549/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200550 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100551 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
552 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
553 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
554 * to an SSL HELLO (the principle is that this is enough to distinguish between
555 * an SSL server and a pure TCP relay). All other cases will set s->result to
556 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
557 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558 */
Willy Tarreau83749182007-04-15 20:56:27 +0200559static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200560{
Willy Tarreau83749182007-04-15 20:56:27 +0200561 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100562 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563 struct task *t = fdtab[fd].owner;
564 struct server *s = t->context;
565 int skerr;
566 socklen_t lskerr = sizeof(skerr);
567
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100568 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200569
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100570 if (unlikely((s->result & SRV_CHK_ERROR) ||
571 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200572 (fdtab[fd].ev & FD_POLL_ERR) ||
573 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
574 (skerr != 0))) {
575 /* in case of TCP only, this tells us if the connection failed */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200576
577 if (!(s->result & SRV_CHK_ERROR))
578 set_server_check_status(s, HCHK_STATUS_SOCKERR);
579
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100580 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200581
Willy Tarreau83749182007-04-15 20:56:27 +0200582 goto out_wakeup;
583 }
584
Willy Tarreau83749182007-04-15 20:56:27 +0200585 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
586 * but the connection was closed on the remote end. Fortunately, recv still
587 * works correctly and we don't need to do the getsockopt() on linux.
588 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200589 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreau83749182007-04-15 20:56:27 +0200590 if (unlikely(len < 0 && errno == EAGAIN)) {
591 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100592 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200593 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594 }
595
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100596 /* Note: the response will only be accepted if read at once */
597 if (s->proxy->options & PR_O_HTTP_CHK) {
598 /* Check if the server speaks HTTP 1.X */
599 if ((len < strlen("HTTP/1.0 000\r")) ||
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200600 (memcmp(trash, "HTTP/1.", 7) != 0 ||
601 (trash[12] != ' ' && trash[12] != '\r')) ||
602 !isdigit(trash[9]) || !isdigit(trash[10]) || !isdigit(trash[11])) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100603 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200604 set_server_check_status(s, HCHK_STATUS_L7RSP);
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100605 goto out_wakeup;
606 }
607
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200608 s->check_code = str2uic(&trash[9]);
609
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100610 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200611 if (trash[9] == '2' || trash[9] == '3') {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100612 s->result |= SRV_CHK_RUNNING;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200613 set_server_check_status(s, HCHK_STATUS_L7OKD);
614 } else if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau48494c02007-11-30 10:41:39 +0100615 (s->state & SRV_RUNNING) &&
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200616 (s->check_code == 404)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100617 /* 404 may be accepted as "stopping" only if the server was up */
618 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200619 set_server_check_status(s, HCHK_STATUS_L7OKCD);
Willy Tarreau48494c02007-11-30 10:41:39 +0100620 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200621 else {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100622 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200623 set_server_check_status(s, HCHK_STATUS_L7STS);
624 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100625 }
626 else if (s->proxy->options & PR_O_SSL3_CHK) {
627 /* Check for SSLv3 alert or handshake */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200628 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16)) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100629 s->result |= SRV_CHK_RUNNING;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200630 set_server_check_status(s, HCHK_STATUS_L6OK);
631 } else {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100632 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200633 set_server_check_status(s, HCHK_STATUS_L6RSP);
634 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200635 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100636 else if (s->proxy->options & PR_O_SMTP_CHK) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200637 /* Check if the server speaks SMTP */
638 if ((len < strlen("000\r")) ||
639 (trash[3] != ' ' && trash[3] != '\r') ||
640 !isdigit(trash[0]) || !isdigit(trash[1]) || !isdigit(trash[2])) {
641 s->result |= SRV_CHK_ERROR;
642 set_server_check_status(s, HCHK_STATUS_L7RSP);
643 goto out_wakeup;
644 }
645
646 s->check_code = str2uic(&trash[0]);
647
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100648 /* Check for SMTP code 2xx (should be 250) */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200649 if (trash[0] == '2') {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100650 s->result |= SRV_CHK_RUNNING;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200651 set_server_check_status(s, HCHK_STATUS_L7OKD);
652 } else {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100653 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200654 set_server_check_status(s, HCHK_STATUS_L7STS);
655 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200656 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100657 else {
658 /* other checks are valid if the connection succeeded anyway */
659 s->result |= SRV_CHK_RUNNING;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200660 set_server_check_status(s, HCHK_STATUS_L4OK);
Willy Tarreau23677902007-05-08 23:50:35 +0200661 }
Willy Tarreau83749182007-04-15 20:56:27 +0200662
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100663 out_wakeup:
664 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665 fdtab[fd].state = FD_STERROR;
666
Willy Tarreauf161a342007-04-08 16:59:42 +0200667 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200668 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100669 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200670 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671}
672
673/*
674 * manages a server health-check. Returns
675 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
676 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100677struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678{
Willy Tarreaue3838802009-03-21 18:58:32 +0100679 int attempts = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 struct server *s = t->context;
681 struct sockaddr_in sa;
682 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200683 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684
685 //fprintf(stderr, "process_chk: task=%p\n", t);
686
687 new_chk:
Willy Tarreaue3838802009-03-21 18:58:32 +0100688 if (attempts++ > 0) {
689 /* we always fail to create a server, let's stop insisting... */
690 while (tick_is_expired(t->expire, now_ms))
691 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
692 return t;
693 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 fd = s->curfd;
695 if (fd < 0) { /* no check currently running */
696 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau26c25062009-03-08 09:38:41 +0100697 if (!tick_is_expired(t->expire, now_ms)) /* woke up too early */
698 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699
700 /* we don't send any health-checks when the proxy is stopped or when
701 * the server should not be checked.
702 */
703 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200704 while (tick_is_expired(t->expire, now_ms))
705 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau26c25062009-03-08 09:38:41 +0100706 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200707 }
708
709 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100710 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200711 s->check_start = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
713 if ((fd < global.maxsock) &&
714 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
715 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
716 //fprintf(stderr, "process_chk: 3\n");
717
Willy Tarreau9edd1612007-10-18 18:07:48 +0200718 if (s->proxy->options & PR_O_TCP_NOLING) {
719 /* We don't want to useless data */
720 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
721 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200722
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200723 if (s->check_addr.sin_addr.s_addr)
724 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200725 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200726 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200727 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200728 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200729
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 sa.sin_port = htons(s->check_port);
732
733 /* allow specific binding :
734 * - server-specific at first
735 * - proxy-specific next
736 */
737 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100738 struct sockaddr_in *remote = NULL;
739 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100740
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100741#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100742 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
743 remote = (struct sockaddr_in *)&s->tproxy_addr;
744 flags = 3;
745 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100746#endif
Willy Tarreauc76721d2009-02-04 20:20:58 +0100747#ifdef SO_BINDTODEVICE
748 /* Note: this might fail if not CAP_NET_RAW */
749 if (s->iface_name)
750 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100751 s->iface_name, s->iface_len + 1);
Willy Tarreauc76721d2009-02-04 20:20:58 +0100752#endif
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200753 if (s->sport_range) {
754 int bind_attempts = 10; /* should be more than enough to find a spare port */
755 struct sockaddr_in src;
756
757 ret = 1;
758 src = s->source_addr;
759
760 do {
761 /* note: in case of retry, we may have to release a previously
762 * allocated port, hence this loop's construct.
763 */
764 port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port);
765 fdtab[fd].port_range = NULL;
766
767 if (!bind_attempts)
768 break;
769 bind_attempts--;
770
771 fdtab[fd].local_port = port_range_alloc_port(s->sport_range);
772 if (!fdtab[fd].local_port)
773 break;
774
775 fdtab[fd].port_range = s->sport_range;
776 src.sin_port = htons(fdtab[fd].local_port);
777
778 ret = tcpv4_bind_socket(fd, flags, &src, remote);
779 } while (ret != 0); /* binding NOK */
780 }
781 else {
782 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
783 }
784
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100785 if (ret) {
786 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200787 set_server_check_status(s, HCHK_STATUS_SOCKERR);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100788 switch (ret) {
789 case 1:
790 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
791 s->proxy->id, s->id);
792 break;
793 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100794 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
795 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100796 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100797 }
798 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 }
800 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100801 struct sockaddr_in *remote = NULL;
802 int ret, flags = 0;
803
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100804#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100805 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100806 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
807 flags = 3;
808 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100809#endif
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100810#ifdef SO_BINDTODEVICE
811 /* Note: this might fail if not CAP_NET_RAW */
812 if (s->proxy->iface_name)
813 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100814 s->proxy->iface_name, s->proxy->iface_len + 1);
Willy Tarreaud53f96b2009-02-04 18:46:54 +0100815#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100816 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
817 if (ret) {
818 s->result |= SRV_CHK_ERROR;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200819 set_server_check_status(s, HCHK_STATUS_SOCKERR);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100820 switch (ret) {
821 case 1:
822 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
823 proxy_type_str(s->proxy), s->proxy->id);
824 break;
825 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100826 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
827 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100828 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100829 }
830 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200831 }
832
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100833 if (s->result == SRV_CHK_UNKNOWN) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400834#if defined(TCP_QUICKACK)
Willy Tarreau1274bc42009-07-15 07:16:31 +0200835 /* disabling tcp quick ack now allows
836 * the request to leave the machine with
837 * the first ACK.
838 */
839 if (s->proxy->options2 & PR_O2_SMARTCON)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400840 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau1274bc42009-07-15 07:16:31 +0200841#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200842 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
843 /* OK, connection in progress or established */
844
845 //fprintf(stderr, "process_chk: 4\n");
846
847 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200848 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200850 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
851 fdtab[fd].cb[DIR_RD].b = NULL;
852 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
853 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200854 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
855 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200857 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreauf161a342007-04-08 16:59:42 +0200858 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200860 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100862 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
863 /* we allow up to min(inter, timeout.connect) for a connection
864 * to establish but only when timeout.check is set
865 * as it may be to short for a full check otherwise
866 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200867 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100868
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200869 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
870 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
871 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +0100872 }
Willy Tarreau26c25062009-03-08 09:38:41 +0100873 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874 }
875 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100876 s->result |= SRV_CHK_ERROR; /* a real error */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200877
878 switch (errno) {
879 /* FIXME: is it possible to get ECONNREFUSED/ENETUNREACH with O_NONBLOCK? */
880 case ECONNREFUSED:
881 case ENETUNREACH:
882 set_server_check_status(s, HCHK_STATUS_L4CON);
883 break;
884
885 default:
886 set_server_check_status(s, HCHK_STATUS_SOCKERR);
887 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888 }
889 }
890 }
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200891 port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port);
892 fdtab[fd].port_range = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 close(fd); /* socket creation error */
894 }
895
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100896 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200898 while (tick_is_expired(t->expire, now_ms))
899 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900 goto new_chk; /* may be we should initialize a new check */
901 }
902
903 /* here, we have seen a failure */
904 if (s->health > s->rise) {
905 s->health--; /* still good */
906 s->failed_checks++;
907 }
908 else
909 set_server_down(s);
910
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100911 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
912 /* we allow up to min(inter, timeout.connect) for a connection
913 * to establish but only when timeout.check is set
914 * as it may be to short for a full check otherwise
915 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200916 while (tick_is_expired(t->expire, now_ms)) {
917 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100918
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200919 t_con = tick_add(t->expire, s->proxy->timeout.connect);
920 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100921
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200922 if (s->proxy->timeout.check)
923 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100924 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925 goto new_chk;
926 }
927 else {
928 //fprintf(stderr, "process_chk: 8\n");
929 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100930 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200932
Willy Tarreau9909fc12007-11-30 17:42:05 +0100933 if (s->state & SRV_WARMINGUP) {
934 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
935 s->state &= ~SRV_WARMINGUP;
936 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
937 s->eweight = s->uweight * BE_WEIGHT_SCALE;
938 if (s->proxy->lbprm.update_server_eweight)
939 s->proxy->lbprm.update_server_eweight(s);
940 }
941 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
942 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100943 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
944 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100945 s->eweight *= s->uweight;
946 if (s->proxy->lbprm.update_server_eweight)
947 s->proxy->lbprm.update_server_eweight(s);
948 }
949 /* probably that we can refill this server with a bit more connections */
950 check_for_pending(s);
951 }
952
Willy Tarreau48494c02007-11-30 10:41:39 +0100953 /* we may have to add/remove this server from the LB group */
954 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
955 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100956 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
957 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100958 else if (!(s->state & SRV_GOINGDOWN) &&
959 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100960 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
961 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100962 }
963
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200964 if (s->health < s->rise + s->fall - 1) {
965 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200966
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100967 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200968 }
969 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200970 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200971
972 rv = 0;
973 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100974 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200975 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100976 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200977 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200978 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200979 goto new_chk;
980 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200981 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200982 if (!(s->result & SRV_CHK_ERROR)) {
983 if (!EV_FD_ISSET(fd, DIR_RD)) {
984 set_server_check_status(s, HCHK_STATUS_L4TOUT);
985 } else {
986 if (s->proxy->options & PR_O_SSL3_CHK)
987 set_server_check_status(s, HCHK_STATUS_L6TOUT);
988 else /* HTTP, SMTP */
989 set_server_check_status(s, HCHK_STATUS_L7TOUT);
990 }
991 }
992
Willy Tarreaubaaee002006-06-26 02:48:02 +0200993 //fprintf(stderr, "process_chk: 10\n");
994 /* failure or timeout detected */
995 if (s->health > s->rise) {
996 s->health--; /* still good */
997 s->failed_checks++;
998 }
999 else
1000 set_server_down(s);
1001 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001003
1004 rv = 0;
1005 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001006 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001007 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001008 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001009 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001010 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 goto new_chk;
1012 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001013 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 }
1015 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001016 s->result = SRV_CHK_UNKNOWN;
Willy Tarreau26c25062009-03-08 09:38:41 +01001017 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018}
1019
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001020/*
1021 * Start health-check.
1022 * Returns 0 if OK, -1 if error, and prints the error in this case.
1023 */
1024int start_checks() {
1025
1026 struct proxy *px;
1027 struct server *s;
1028 struct task *t;
1029 int nbchk=0, mininter=0, srvpos=0;
1030
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001031 /* 1- count the checkers to run simultaneously.
1032 * We also determine the minimum interval among all of those which
1033 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1034 * will be used to spread their start-up date. Those which have
1035 * a shorter interval will start independantly and will not dictate
1036 * too short an interval for all others.
1037 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001038 for (px = proxy; px; px = px->next) {
1039 for (s = px->srv; s; s = s->next) {
1040 if (!(s->state & SRV_CHECKED))
1041 continue;
1042
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001043 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1044 (!mininter || mininter > srv_getinter(s)))
1045 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001046
1047 nbchk++;
1048 }
1049 }
1050
1051 if (!nbchk)
1052 return 0;
1053
1054 srand((unsigned)time(NULL));
1055
1056 /*
1057 * 2- start them as far as possible from each others. For this, we will
1058 * start them after their interval set to the min interval divided by
1059 * the number of servers, weighted by the server's position in the list.
1060 */
1061 for (px = proxy; px; px = px->next) {
1062 for (s = px->srv; s; s = s->next) {
1063 if (!(s->state & SRV_CHECKED))
1064 continue;
1065
Willy Tarreaua4613182009-03-21 18:13:21 +01001066 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001067 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1068 return -1;
1069 }
1070
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001071 s->check = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001072 t->process = process_chk;
1073 t->context = s;
1074
1075 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001076 t->expire = tick_add(now_ms,
1077 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1078 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001079 s->check_start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001080 task_queue(t);
1081
1082 srvpos++;
1083 }
1084 }
1085 return 0;
1086}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087
1088/*
1089 * Local variables:
1090 * c-indent-level: 8
1091 * c-basic-offset: 8
1092 * End:
1093 */