blob: 2fbda11822fe59135e2bd35aeb887976e0d188e3 [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 Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020033#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Baptiste Assmann69e273f2013-12-11 00:52:19 +010038#ifdef USE_OPENSSL
39#include <types/ssl_sock.h>
40#include <proto/ssl_sock.h>
41#endif /* USE_OPENSSL */
42
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090045#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/fd.h>
47#include <proto/log.h>
48#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020049#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010050#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010051#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010052#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010053#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020054#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090056#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010057#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreaubd741542010-03-16 18:46:54 +010060static int httpchk_expect(struct server *s, int done);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020061static int tcpcheck_get_step_id(struct server *);
62static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010063
Simon Horman63a4a822012-03-19 07:24:41 +090064static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010065 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
66 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020067 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020068
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010069 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010070
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010071 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010073 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
74 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
75 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010077 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
78 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
79 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020080
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010081 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
82 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020083
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020084 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020085
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010086 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
87 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
88 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020089};
90
Simon Horman63a4a822012-03-19 07:24:41 +090091static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010092 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
93
94 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
95 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
96
97 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
98 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
99 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
100 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
101
102 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
103 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
104 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
105};
106
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200107/*
108 * Convert check_status code to description
109 */
110const char *get_check_status_description(short check_status) {
111
112 const char *desc;
113
114 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200115 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200116 else
117 desc = NULL;
118
119 if (desc && *desc)
120 return desc;
121 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200122 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200123}
124
125/*
126 * Convert check_status code to short info
127 */
128const char *get_check_status_info(short check_status) {
129
130 const char *info;
131
132 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200133 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200134 else
135 info = NULL;
136
137 if (info && *info)
138 return info;
139 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200140 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200141}
142
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100143const char *get_analyze_status(short analyze_status) {
144
145 const char *desc;
146
147 if (analyze_status < HANA_STATUS_SIZE)
148 desc = analyze_statuses[analyze_status].desc;
149 else
150 desc = NULL;
151
152 if (desc && *desc)
153 return desc;
154 else
155 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
156}
157
Simon Horman4a741432013-02-23 15:35:38 +0900158static void server_status_printf(struct chunk *msg, struct server *s, struct check *check, int xferred) {
Willy Tarreau44267702011-10-28 15:35:33 +0200159 if (s->track)
Willy Tarreau77804732012-10-29 16:14:26 +0100160 chunk_appendf(msg, " via %s/%s",
Willy Tarreau44267702011-10-28 15:35:33 +0200161 s->track->proxy->id, s->track->id);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200162
Simon Horman4a741432013-02-23 15:35:38 +0900163 if (check) {
164 chunk_appendf(msg, ", reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200165
Simon Horman4a741432013-02-23 15:35:38 +0900166 if (check->status >= HCHK_STATUS_L57DATA)
167 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200168
Simon Horman4a741432013-02-23 15:35:38 +0900169 if (*check->desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200170 struct chunk src;
171
Willy Tarreau77804732012-10-29 16:14:26 +0100172 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200173
Simon Horman4a741432013-02-23 15:35:38 +0900174 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200175 chunk_asciiencode(msg, &src, '"');
176
Willy Tarreau77804732012-10-29 16:14:26 +0100177 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200178 }
179
Simon Horman4a741432013-02-23 15:35:38 +0900180 if (check->duration >= 0)
181 chunk_appendf(msg, ", check duration: %ldms", check->duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200182 }
183
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200184 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100185 if (!(s->state & SRV_RUNNING))
Willy Tarreau77804732012-10-29 16:14:26 +0100186 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100187 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200188 s->proxy->srv_act, s->proxy->srv_bck,
189 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
190 s->cur_sess, xferred, s->nbpend);
191 else
Willy Tarreau77804732012-10-29 16:14:26 +0100192 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100193 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200194 s->proxy->srv_act, s->proxy->srv_bck,
195 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
196 xferred, s->nbpend);
197 }
198}
199
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200200/*
Simon Horman4a741432013-02-23 15:35:38 +0900201 * Set check->status, update check->duration and fill check->result with
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100202 * an adequate CHK_RES_* value.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200203 *
204 * Show information in logs about failed health check if server is UP
205 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200206 */
Simon Horman4a741432013-02-23 15:35:38 +0900207static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100208{
Simon Horman4a741432013-02-23 15:35:38 +0900209 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200210 short prev_status = check->status;
Simon Horman4a741432013-02-23 15:35:38 +0900211
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200212 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100213 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900214 check->desc[0] = '\0';
215 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200216 return;
217 }
218
Simon Horman4a741432013-02-23 15:35:38 +0900219 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200220 return;
221
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200222 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900223 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
224 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200225 } else
Simon Horman4a741432013-02-23 15:35:38 +0900226 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200227
Simon Horman4a741432013-02-23 15:35:38 +0900228 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200229 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900230 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200231
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100232 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900233 check->duration = -1;
234 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200235 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900236 check->duration = tv_ms_elapsed(&check->start, &now);
237 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200238 }
239
Simon Horman2f1f9552013-11-25 10:46:37 +0900240 /* Failure to connect to the agent as a secondary check should not
241 * cause the server to be marked down. So only log status changes
242 * for HCHK_STATUS_* statuses */
Willy Tarreau33434322013-12-11 21:15:19 +0100243 if ((check->state & CHK_ST_AGENT) && check->status < HCHK_STATUS_L7TOUT)
Simon Horman2f1f9552013-11-25 10:46:37 +0900244 return;
245
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200246 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
Willy Tarreaubef1b322014-05-13 21:01:39 +0200247 ((status != prev_status) ||
248 ((check->health != 0) && (check->result == CHK_RES_FAILED)) ||
249 (((check->health != check->rise + check->fall - 1)) && (check->result >= CHK_RES_PASSED)))) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200250
251 int health, rise, fall, state;
252
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100253 chunk_reset(&trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200254
255 /* FIXME begin: calculate local version of the health/rise/fall/state */
Simon Horman125d0992013-02-24 17:23:38 +0900256 health = check->health;
Simon Horman58c32972013-11-25 10:46:38 +0900257 rise = check->rise;
258 fall = check->fall;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200259 state = s->state;
260
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100261 switch (check->result) {
262 case CHK_RES_FAILED:
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200263 if (health > rise) {
264 health--; /* still good */
265 } else {
266 if (health == rise)
267 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
268
269 health = 0;
270 }
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100271 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200272
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100273 case CHK_RES_PASSED:
274 case CHK_RES_CONDPASS:
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200275 if (health < rise + fall - 1) {
276 health++; /* was bad, stays for a while */
277
278 if (health == rise)
279 state |= SRV_RUNNING;
280
281 if (health >= rise)
282 health = rise + fall - 1; /* OK now */
283 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100284
285 /* clear consecutive_errors if observing is enabled */
286 if (s->onerror)
287 s->consecutive_errors = 0;
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100288 break;
289 default:
290 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200291 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200292
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100293 chunk_appendf(&trash,
294 "Health check for %sserver %s/%s %s%s",
295 s->state & SRV_BACKUP ? "backup " : "",
296 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100297 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
298 (check->result >= CHK_RES_PASSED) ? "succeeded":"failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200299
Simon Horman4a741432013-02-23 15:35:38 +0900300 server_status_printf(&trash, s, check, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200301
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100302 chunk_appendf(&trash, ", status: %d/%d %s",
303 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
304 (state & SRV_RUNNING) ? (fall) : (rise),
Willy Tarreaubef1b322014-05-13 21:01:39 +0200305 (state & SRV_RUNNING) ? (s->uweight?"UP":"DRAIN"):"DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200306
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100307 Warning("%s.\n", trash.str);
308 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200309 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200310}
311
Willy Tarreau48494c02007-11-30 10:41:39 +0100312/* sends a log message when a backend goes down, and also sets last
313 * change date.
314 */
315static void set_backend_down(struct proxy *be)
316{
317 be->last_change = now.tv_sec;
318 be->down_trans++;
319
320 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
321 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
322}
323
324/* Redistribute pending connections when a server goes down. The number of
325 * connections redistributed is returned.
326 */
327static int redistribute_pending(struct server *s)
328{
329 struct pendconn *pc, *pc_bck, *pc_end;
330 int xferred = 0;
331
332 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
333 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100334 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
335 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100336 /* The REDISP option was specified. We will ignore
337 * cookie and force to balance or use the dispatcher.
338 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100339
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100340 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100341 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100342
Willy Tarreau48494c02007-11-30 10:41:39 +0100343 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200344 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100345 xferred++;
346 }
347 }
348 return xferred;
349}
350
351/* Check for pending connections at the backend, and assign some of them to
352 * the server coming up. The server's weight is checked before being assigned
353 * connections it may not be able to handle. The total number of transferred
354 * connections is returned.
355 */
356static int check_for_pending(struct server *s)
357{
358 int xferred;
359
360 if (!s->eweight)
361 return 0;
362
363 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
364 struct session *sess;
365 struct pendconn *p;
366
367 p = pendconn_from_px(s->proxy);
368 if (!p)
369 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100370 p->sess->target = &s->obj_type;
Willy Tarreau48494c02007-11-30 10:41:39 +0100371 sess = p->sess;
372 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200373 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100374 }
375 return xferred;
376}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700378/* Shutdown all connections of a server. The caller must pass a termination
379 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
380 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900381 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700382static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900383{
384 struct session *session, *session_bck;
385
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200386 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
387 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700388 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900389}
390
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700391/* Shutdown all connections of all backup servers of a proxy. The caller must
392 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
393 * the reason for the shutdown.
394 */
395static void shutdown_backup_sessions(struct proxy *px, int why)
396{
397 struct server *srv;
398
399 for (srv = px->srv; srv != NULL; srv = srv->next)
400 if (srv->state & SRV_BACKUP)
401 shutdown_sessions(srv, why);
402}
403
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404/* Sets server <s> down, notifies by all available means, recounts the
405 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200406 * possible to other servers. It automatically recomputes the number of
407 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408 */
Simon Horman4a741432013-02-23 15:35:38 +0900409void set_server_down(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410{
Simon Horman4a741432013-02-23 15:35:38 +0900411 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100412 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413 int xferred;
414
Cyril Bontécd19e512010-01-31 22:34:03 +0100415 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900416 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100417 }
418
Simon Horman58c32972013-11-25 10:46:38 +0900419 if ((s->state & SRV_RUNNING && check->health == check->rise) || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100420 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200421 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200422
423 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100424 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200425 if (s->proxy->lbprm.set_server_status_down)
426 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200427
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900428 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700429 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900430
Willy Tarreaubaaee002006-06-26 02:48:02 +0200431 /* we might have sessions queued on this server and waiting for
432 * a connection. Those which are redispatchable will be queued
433 * to another server or to the proxy itself.
434 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100435 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100436
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100437 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100438
Cyril Bontécd19e512010-01-31 22:34:03 +0100439 if (s->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100440 chunk_appendf(&trash,
441 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
442 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100443 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100444 chunk_appendf(&trash,
445 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
446 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100447
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100448 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900449 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : 0),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100450 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100451 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100452 Warning("%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200453
Willy Tarreau48494c02007-11-30 10:41:39 +0100454 /* we don't send an alert if the server was previously paused */
455 if (srv_was_paused)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100456 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreau48494c02007-11-30 10:41:39 +0100457 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100458 send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200459
Willy Tarreaud64d2252010-10-17 17:16:42 +0200460 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100461 set_backend_down(s->proxy);
462
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200463 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100464
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100465 for (srv = s->trackers; srv; srv = srv->tracknext)
466 if (!(srv->state & SRV_MAINTAIN))
467 /* Only notify tracking servers that are not already in maintenance. */
468 set_server_down(&srv->check);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100470
Simon Horman125d0992013-02-24 17:23:38 +0900471 check->health = 0; /* failure */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200472}
473
Simon Horman4a741432013-02-23 15:35:38 +0900474void set_server_up(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100475
Simon Horman4a741432013-02-23 15:35:38 +0900476 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100477 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100478 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100479 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100480
Cyril Bontécd19e512010-01-31 22:34:03 +0100481 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900482 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100483 }
484
Willy Tarreaubb9665e2013-12-14 16:14:15 +0100485 if (s->track ||
Willy Tarreau02541e82013-12-16 18:08:36 +0100486 ((s->check.state & CHK_ST_ENABLED) && (s->check.health == s->check.rise) &&
487 (s->agent.health >= s->agent.rise || !(s->agent.state & CHK_ST_ENABLED))) ||
488 ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health == s->agent.rise) &&
Willy Tarreaue24d9632013-12-28 21:21:31 +0100489 (s->check.health >= s->check.rise || !(s->check.state & CHK_ST_ENABLED))) ||
490 (!(s->agent.state & CHK_ST_ENABLED) && !(s->check.state & CHK_ST_ENABLED))) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100491 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
492 if (s->proxy->last_change < now.tv_sec) // ignore negative times
493 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
494 s->proxy->last_change = now.tv_sec;
495 }
496
497 if (s->last_change < now.tv_sec) // ignore negative times
498 s->down_time += now.tv_sec - s->last_change;
499
500 s->last_change = now.tv_sec;
501 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100502 s->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +0100503 s->check.state &= ~CHK_ST_PAUSED;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100504
505 if (s->slowstart > 0) {
506 s->state |= SRV_WARMINGUP;
Willy Tarreau2e993902011-10-31 11:53:20 +0100507 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100508 }
Willy Tarreau004e0452013-11-21 11:22:01 +0100509
510 server_recalc_eweight(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100511
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700512 /* If the server is set with "on-marked-up shutdown-backup-sessions",
513 * and it's not a backup server and its effective weight is > 0,
514 * then it can accept new connections, so we shut down all sessions
515 * on all backup servers.
516 */
517 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
518 !(s->state & SRV_BACKUP) && s->eweight)
519 shutdown_backup_sessions(s->proxy, SN_ERR_UP);
520
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100521 /* check if we can handle some connections queued at the proxy. We
522 * will take as many as we can handle.
523 */
524 xferred = check_for_pending(s);
525
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100526 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100527
Willy Tarreau45446782012-03-09 17:16:09 +0100528 if (old_state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100529 chunk_appendf(&trash,
530 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
531 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100532 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100533 chunk_appendf(&trash,
534 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
535 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100536
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100537 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900538 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100539 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100540 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100541
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100542 Warning("%s.\n", trash.str);
543 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100544
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100545 for (srv = s->trackers; srv; srv = srv->tracknext)
546 if (!(srv->state & SRV_MAINTAIN))
547 /* Only notify tracking servers if they're not in maintenance. */
548 set_server_up(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100549 }
550
Simon Horman58c32972013-11-25 10:46:38 +0900551 if (check->health >= check->rise)
552 check->health = check->rise + check->fall - 1; /* OK now */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100553
554}
555
Simon Horman4a741432013-02-23 15:35:38 +0900556static void set_server_disabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100557
Simon Horman4a741432013-02-23 15:35:38 +0900558 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100559 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100560 int xferred;
561
562 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200563 if (s->proxy->lbprm.set_server_status_down)
564 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100565
566 /* we might have sessions queued on this server and waiting for
567 * a connection. Those which are redispatchable will be queued
568 * to another server or to the proxy itself.
569 */
570 xferred = redistribute_pending(s);
571
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100572 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100573
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100574 chunk_appendf(&trash,
575 "Load-balancing on %sServer %s/%s is disabled",
576 s->state & SRV_BACKUP ? "Backup " : "",
577 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100578
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100579 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900580 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100581 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100582
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100583 Warning("%s.\n", trash.str);
584 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100585
586 if (!s->proxy->srv_bck && !s->proxy->srv_act)
587 set_backend_down(s->proxy);
588
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100589 for (srv = s->trackers; srv; srv = srv->tracknext)
590 set_server_disabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100591}
592
Simon Horman4a741432013-02-23 15:35:38 +0900593static void set_server_enabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100594
Simon Horman4a741432013-02-23 15:35:38 +0900595 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100596 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100597 int xferred;
598
599 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200600 if (s->proxy->lbprm.set_server_status_up)
601 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100602
603 /* check if we can handle some connections queued at the proxy. We
604 * will take as many as we can handle.
605 */
606 xferred = check_for_pending(s);
607
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100608 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100609
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100610 chunk_appendf(&trash,
611 "Load-balancing on %sServer %s/%s is enabled again",
612 s->state & SRV_BACKUP ? "Backup " : "",
613 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100614
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100615 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900616 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100617 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100618
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100619 Warning("%s.\n", trash.str);
620 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100621
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100622 for (srv = s->trackers; srv; srv = srv->tracknext)
623 set_server_enabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100624}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625
Simon Hormand8583062013-11-25 10:46:33 +0900626static void check_failed(struct check *check)
627{
628 struct server *s = check->server;
629
Simon Horman2f1f9552013-11-25 10:46:37 +0900630 /* The agent secondary check should only cause a server to be marked
631 * as down if check->status is HCHK_STATUS_L7STS, which indicates
632 * that the agent returned "fail", "stopped" or "down".
633 * The implication here is that failure to connect to the agent
634 * as a secondary check should not cause the server to be marked
635 * down. */
Willy Tarreau33434322013-12-11 21:15:19 +0100636 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
Simon Horman2f1f9552013-11-25 10:46:37 +0900637 return;
638
Simon Horman58c32972013-11-25 10:46:38 +0900639 if (check->health > check->rise) {
Simon Hormand8583062013-11-25 10:46:33 +0900640 check->health--; /* still good */
641 s->counters.failed_checks++;
642 }
643 else
644 set_server_down(check);
645}
646
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100647/* note: use health_adjust() only, which first checks that the observe mode is
648 * enabled.
649 */
650void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100651{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100652 int failed;
653 int expire;
654
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100655 if (s->observe >= HANA_OBS_SIZE)
656 return;
657
Willy Tarreaubb956662013-01-24 00:37:39 +0100658 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100659 return;
660
661 switch (analyze_statuses[status].lr[s->observe - 1]) {
662 case 1:
663 failed = 1;
664 break;
665
666 case 2:
667 failed = 0;
668 break;
669
670 default:
671 return;
672 }
673
674 if (!failed) {
675 /* good: clear consecutive_errors */
676 s->consecutive_errors = 0;
677 return;
678 }
679
680 s->consecutive_errors++;
681
682 if (s->consecutive_errors < s->consecutive_errors_limit)
683 return;
684
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100685 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
686 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100687
688 switch (s->onerror) {
689 case HANA_ONERR_FASTINTER:
690 /* force fastinter - nothing to do here as all modes force it */
691 break;
692
693 case HANA_ONERR_SUDDTH:
694 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900695 if (s->check.health > s->check.rise)
696 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100697
698 /* no break - fall through */
699
700 case HANA_ONERR_FAILCHK:
701 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900702 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Simon Hormand8583062013-11-25 10:46:33 +0900703 check_failed(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100704
705 break;
706
707 case HANA_ONERR_MARKDWN:
708 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900709 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900710 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
711 set_server_down(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100712
713 break;
714
715 default:
716 /* write a warning? */
717 break;
718 }
719
720 s->consecutive_errors = 0;
721 s->counters.failed_hana++;
722
Simon Horman66183002013-02-23 10:16:43 +0900723 if (s->check.fastinter) {
724 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300725 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200726 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300727 /* requeue check task with new expire */
728 task_queue(s->check.task);
729 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100730 }
731}
732
Willy Tarreaua1dab552014-04-14 15:04:54 +0200733static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100734{
735 int sv_state;
736 int ratio;
737 int hlen = 0;
738 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
739 "UP %d/%d", "UP",
740 "NOLB %d/%d", "NOLB",
741 "no check" };
742
743 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
744 hlen += 24;
745
Willy Tarreauff5ae352013-12-11 20:36:34 +0100746 if (!(s->check.state & CHK_ST_ENABLED))
747 sv_state = 6;
Willy Tarreauef781042010-01-27 11:53:01 +0100748 else if (s->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +0900749 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100750 sv_state = 3; /* UP */
751 else
752 sv_state = 2; /* going down */
753
754 if (s->state & SRV_GOINGDOWN)
755 sv_state += 2;
756 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900757 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100758 sv_state = 1; /* going up */
759 else
760 sv_state = 0; /* DOWN */
761 }
762
Willy Tarreaua1dab552014-04-14 15:04:54 +0200763 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100764 srv_hlt_st[sv_state],
Simon Horman58c32972013-11-25 10:46:38 +0900765 (s->state & SRV_RUNNING) ? (s->check.health - s->check.rise + 1) : (s->check.health),
766 (s->state & SRV_RUNNING) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100767
Willy Tarreaua1dab552014-04-14 15:04:54 +0200768 hlen += snprintf(buffer + hlen, size - hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
Willy Tarreauef781042010-01-27 11:53:01 +0100769 s->proxy->id, s->id,
770 global.node,
771 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
772 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
773 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
774 s->nbpend);
775
776 if ((s->state & SRV_WARMINGUP) &&
777 now.tv_sec < s->last_change + s->slowstart &&
778 now.tv_sec >= s->last_change) {
779 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200780 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100781 }
782
783 buffer[hlen++] = '\r';
784 buffer[hlen++] = '\n';
785
786 return hlen;
787}
788
Willy Tarreau20a18342013-12-05 00:31:46 +0100789/* Check the connection. If an error has already been reported or the socket is
790 * closed, keep errno intact as it is supposed to contain the valid error code.
791 * If no error is reported, check the socket's error queue using getsockopt().
792 * Warning, this must be done only once when returning from poll, and never
793 * after an I/O error was attempted, otherwise the error queue might contain
794 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
795 * socket. Returns non-zero if an error was reported, zero if everything is
796 * clean (including a properly closed socket).
797 */
798static int retrieve_errno_from_socket(struct connection *conn)
799{
800 int skerr;
801 socklen_t lskerr = sizeof(skerr);
802
803 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
804 return 1;
805
Willy Tarreau3c728722014-01-23 13:50:42 +0100806 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100807 return 0;
808
809 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
810 errno = skerr;
811
812 if (errno == EAGAIN)
813 errno = 0;
814
815 if (!errno) {
816 /* we could not retrieve an error, that does not mean there is
817 * none. Just don't change anything and only report the prior
818 * error if any.
819 */
820 if (conn->flags & CO_FL_ERROR)
821 return 1;
822 else
823 return 0;
824 }
825
826 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
827 return 1;
828}
829
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100830/* Try to collect as much information as possible on the connection status,
831 * and adjust the server status accordingly. It may make use of <errno_bck>
832 * if non-null when the caller is absolutely certain of its validity (eg:
833 * checked just after a syscall). If the caller doesn't have a valid errno,
834 * it can pass zero, and retrieve_errno_from_socket() will be called to try
835 * to extract errno from the socket. If no error is reported, it will consider
836 * the <expired> flag. This is intended to be used when a connection error was
837 * reported in conn->flags or when a timeout was reported in <expired>. The
838 * function takes care of not updating a server status which was already set.
839 * All situations where at least one of <expired> or CO_FL_ERROR are set
840 * produce a status.
841 */
842static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
843{
844 struct check *check = conn->owner;
845 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200846 struct chunk *chk;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100847
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100848 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100849 return;
850
851 errno = errno_bck;
852 if (!errno || errno == EAGAIN)
853 retrieve_errno_from_socket(conn);
854
855 if (!(conn->flags & CO_FL_ERROR) && !expired)
856 return;
857
858 /* we'll try to build a meaningful error message depending on the
859 * context of the error possibly present in conn->err_code, and the
860 * socket error possibly collected above. This is useful to know the
861 * exact step of the L6 layer (eg: SSL handshake).
862 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200863 chk = get_trash_chunk();
864
865 if (check->type == PR_O2_TCPCHK_CHK) {
866 chunk_printf(chk, " at step %d of tcp-check", tcpcheck_get_step_id(check->server));
867 /* we were looking for a string */
Baptiste Assmann69e273f2013-12-11 00:52:19 +0100868 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
869 chunk_appendf(chk, " (connect)");
870 }
871 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200872 if (check->current_step->string)
873 chunk_appendf(chk, " (string '%s')", check->current_step->string);
874 else if (check->current_step->expect_regex)
875 chunk_appendf(chk, " (expect regex)");
876 }
877 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
878 chunk_appendf(chk, " (send)");
879 }
880 }
881
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100882 if (conn->err_code) {
883 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200884 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100885 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200886 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100887 err_msg = trash.str;
888 }
889 else {
890 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200891 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100892 err_msg = trash.str;
893 }
894 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200895 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100896 }
897 }
898
899 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
900 /* L4 not established (yet) */
901 if (conn->flags & CO_FL_ERROR)
902 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
903 else if (expired)
904 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
905 }
906 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
907 /* L6 not established (yet) */
908 if (conn->flags & CO_FL_ERROR)
909 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
910 else if (expired)
911 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
912 }
913 else if (conn->flags & CO_FL_ERROR) {
914 /* I/O error after connection was established and before we could diagnose */
915 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
916 }
917 else if (expired) {
918 /* connection established but expired check */
919 if (check->type == PR_O2_SSL3_CHK)
920 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
921 else /* HTTP, SMTP, ... */
922 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
923 }
924
925 return;
926}
927
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928/*
929 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200930 * the connection acknowledgement. If the proxy requires L7 health-checks,
931 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900932 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200934static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935{
Simon Horman4a741432013-02-23 15:35:38 +0900936 struct check *check = conn->owner;
937 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900938 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200939
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100940 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100941 goto out_wakeup;
942
Willy Tarreau310987a2014-01-22 19:46:33 +0100943 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100944 return;
945
Willy Tarreau20a18342013-12-05 00:31:46 +0100946 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100947 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100948 __conn_data_stop_both(conn);
949 goto out_wakeup;
950 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100951
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100952 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100953 /* if the output is closed, we can't do anything */
954 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100955 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100956 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200957 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200958
Willy Tarreau06559ac2013-12-05 01:53:08 +0100959 /* here, we know that the connection is established. That's enough for
960 * a pure TCP check.
961 */
962 if (!check->type)
963 goto out_wakeup;
964
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200965 if (check->type == PR_O2_TCPCHK_CHK) {
966 tcpcheck_main(conn);
967 return;
968 }
969
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100970 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100971 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100972 if (conn->flags & CO_FL_ERROR) {
973 chk_report_conn_err(conn, errno, 0);
974 __conn_data_stop_both(conn);
975 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200976 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100977 if (check->bo->o)
978 return;
979 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200980
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100981 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
982 if (s->proxy->timeout.check) {
983 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
984 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100986 goto out_nowake;
987
Willy Tarreau83749182007-04-15 20:56:27 +0200988 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200989 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200990 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200991 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992}
993
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200995 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200996 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900997 * set_server_check_status() to update check->status, check->duration
998 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200999
1000 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
1001 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
1002 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
1003 * response to an SSL HELLO (the principle is that this is enough to
1004 * distinguish between an SSL server and a pure TCP relay). All other cases will
1005 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
1006 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001008static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009{
Simon Horman4a741432013-02-23 15:35:38 +09001010 struct check *check = conn->owner;
1011 struct server *s = check->server;
1012 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001013 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +01001014 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001015 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +02001016
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001017 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +02001018 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +02001019
Willy Tarreau310987a2014-01-22 19:46:33 +01001020 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001021 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001022
1023 if (check->type == PR_O2_TCPCHK_CHK) {
1024 tcpcheck_main(conn);
1025 return;
1026 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001027
Willy Tarreau83749182007-04-15 20:56:27 +02001028 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
1029 * but the connection was closed on the remote end. Fortunately, recv still
1030 * works correctly and we don't need to do the getsockopt() on linux.
1031 */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001032
1033 /* Set buffer to point to the end of the data already read, and check
1034 * that there is free space remaining. If the buffer is full, proceed
1035 * with running the checks without attempting another socket read.
1036 */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001037
Willy Tarreau03938182010-03-17 21:52:07 +01001038 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001039
Simon Horman4a741432013-02-23 15:35:38 +09001040 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +02001041 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +01001042 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +09001043 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +02001044 /* Report network errors only if we got no other data. Otherwise
1045 * we'll let the upper layers decide whether the response is OK
1046 * or not. It is very common that an RST sent by the server is
1047 * reported as an error just after the last data chunk.
1048 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001049 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +01001050 goto out_wakeup;
1051 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001052 }
1053
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001054
Willy Tarreau03938182010-03-17 21:52:07 +01001055 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +09001056 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +01001057 */
Simon Horman4a741432013-02-23 15:35:38 +09001058 if (check->bi->i < check->bi->size)
1059 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001060 else {
Simon Horman4a741432013-02-23 15:35:38 +09001061 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001062 done = 1; /* buffer full, don't wait for more data */
1063 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001064
Nick Chalk57b1bf72010-03-16 15:50:46 +00001065 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +09001066 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001067 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001068 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001069 goto wait_more_data;
1070
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001071 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +09001072 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
1073 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
1074 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
1075 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
1076 !isdigit((unsigned char) *(check->bi->data + 11))) {
1077 cut_crlf(check->bi->data);
1078 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001079
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001080 goto out_wakeup;
1081 }
1082
Simon Horman4a741432013-02-23 15:35:38 +09001083 check->code = str2uic(check->bi->data + 9);
1084 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +00001085
Willy Tarreaubd741542010-03-16 18:46:54 +01001086 if ((s->proxy->options & PR_O_DISABLE404) &&
Simon Horman4a741432013-02-23 15:35:38 +09001087 (s->state & SRV_RUNNING) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +00001088 /* 404 may be accepted as "stopping" only if the server was up */
1089 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001090 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001091 }
Willy Tarreaubd741542010-03-16 18:46:54 +01001092 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
1093 /* Run content verification check... We know we have at least 13 chars */
1094 if (!httpchk_expect(s, done))
1095 goto wait_more_data;
1096 }
1097 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +09001098 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +01001099 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001100 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +01001101 }
Nick Chalk57b1bf72010-03-16 15:50:46 +00001102 else {
1103 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001104 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001105 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001106 break;
1107
1108 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001109 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001110 goto wait_more_data;
1111
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001112 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +09001113 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
1114 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001115 else
Simon Horman4a741432013-02-23 15:35:38 +09001116 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001117 break;
1118
1119 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001120 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001121 goto wait_more_data;
1122
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001123 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +09001124 if ((check->bi->i < strlen("000\r")) ||
1125 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
1126 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
1127 !isdigit((unsigned char) *(check->bi->data + 2))) {
1128 cut_crlf(check->bi->data);
1129 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001130
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001131 goto out_wakeup;
1132 }
1133
Simon Horman4a741432013-02-23 15:35:38 +09001134 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001135
Simon Horman4a741432013-02-23 15:35:38 +09001136 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001137 cut_crlf(desc);
1138
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001139 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +09001140 if (*check->bi->data == '2')
1141 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001142 else
Simon Horman4a741432013-02-23 15:35:38 +09001143 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001144 break;
1145
Simon Hormana2b9dad2013-02-12 10:45:54 +09001146 case PR_O2_LB_AGENT_CHK: {
1147 short status = HCHK_STATUS_L7RSP;
1148 const char *desc = "Unknown feedback string";
1149 const char *down_cmd = NULL;
Simon Horman671b6f02013-11-25 10:46:39 +09001150 int disabled;
Willy Tarreau9809b782013-12-11 21:40:11 +01001151 char *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001152
Willy Tarreau9809b782013-12-11 21:40:11 +01001153 /* get a complete line first */
1154 p = check->bi->data;
1155 while (*p && *p != '\n' && *p != '\r')
1156 p++;
1157
1158 if (!*p) {
1159 if (!done)
1160 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001161
Willy Tarreau9809b782013-12-11 21:40:11 +01001162 /* at least inform the admin that the agent is mis-behaving */
1163 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
1164 break;
1165 }
1166 *p = 0;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001167
Simon Horman671b6f02013-11-25 10:46:39 +09001168 /*
1169 * The agent may have been disabled after a check was
1170 * initialised. If so, ignore weight changes and drain
1171 * settings from the agent. Note that the setting is
1172 * always present in the state of the agent the server,
1173 * regardless of if the agent is being run as a primary or
1174 * secondary check. That is, regardless of if the check
1175 * parameter of this function is the agent or check field
1176 * of the server.
1177 */
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001178 disabled = !(check->server->agent.state & CHK_ST_ENABLED);
Simon Horman671b6f02013-11-25 10:46:39 +09001179
Simon Horman4a741432013-02-23 15:35:38 +09001180 if (strchr(check->bi->data, '%')) {
Simon Horman671b6f02013-11-25 10:46:39 +09001181 if (disabled)
1182 break;
Simon Horman4a741432013-02-23 15:35:38 +09001183 desc = server_parse_weight_change_request(s, check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001184 if (!desc) {
1185 status = HCHK_STATUS_L7OKD;
Simon Horman4a741432013-02-23 15:35:38 +09001186 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001187 }
Simon Horman4a741432013-02-23 15:35:38 +09001188 } else if (!strcasecmp(check->bi->data, "drain")) {
Simon Horman671b6f02013-11-25 10:46:39 +09001189 if (disabled)
1190 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001191 desc = server_parse_weight_change_request(s, "0%");
1192 if (!desc) {
1193 desc = "drain";
1194 status = HCHK_STATUS_L7OKD;
1195 }
Simon Horman4a741432013-02-23 15:35:38 +09001196 } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001197 down_cmd = "down";
Simon Horman4a741432013-02-23 15:35:38 +09001198 } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001199 down_cmd = "stopped";
Simon Horman4a741432013-02-23 15:35:38 +09001200 } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001201 down_cmd = "fail";
1202 }
1203
1204 if (down_cmd) {
Simon Horman4a741432013-02-23 15:35:38 +09001205 const char *end = check->bi->data + strlen(down_cmd);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001206 /*
1207 * The command keyword must terminated the string or
1208 * be followed by a blank.
1209 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001210 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001211 status = HCHK_STATUS_L7STS;
Simon Horman80fefae2013-11-25 10:46:34 +09001212 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001213 }
1214 }
1215
Simon Horman4a741432013-02-23 15:35:38 +09001216 set_server_check_status(check, status, desc);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001217 break;
1218 }
1219
Willy Tarreau1620ec32011-08-06 17:05:02 +02001220 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001221 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001222 goto wait_more_data;
1223
Simon Horman4a741432013-02-23 15:35:38 +09001224 if (check->bi->data[0] == 'R') {
1225 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001226 }
1227 else {
Simon Horman4a741432013-02-23 15:35:38 +09001228 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1229 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001230 else
1231 desc = "PostgreSQL unknown error";
1232
Simon Horman4a741432013-02-23 15:35:38 +09001233 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001234 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001235 break;
1236
1237 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001238 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001239 goto wait_more_data;
1240
Simon Horman4a741432013-02-23 15:35:38 +09001241 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1242 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001243 }
1244 else {
Simon Horman4a741432013-02-23 15:35:38 +09001245 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001246 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001247 break;
1248
1249 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001250 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001251 goto wait_more_data;
1252
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001253 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001254 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001255 /* We set the MySQL Version in description for information purpose
1256 * FIXME : it can be cool to use MySQL Version for other purpose,
1257 * like mark as down old MySQL server.
1258 */
Simon Horman4a741432013-02-23 15:35:38 +09001259 if (check->bi->i > 51) {
1260 desc = ltrim(check->bi->data + 5, ' ');
1261 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001262 }
1263 else {
1264 if (!done)
1265 goto wait_more_data;
1266 /* it seems we have a OK packet but without a valid length,
1267 * it must be a protocol error
1268 */
Simon Horman4a741432013-02-23 15:35:38 +09001269 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001270 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001271 }
1272 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001273 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001274 desc = ltrim(check->bi->data + 7, ' ');
1275 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001276 }
1277 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001278 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1279 (((unsigned int) *(check->bi->data + 1)) << 8) +
1280 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001281
Simon Horman4a741432013-02-23 15:35:38 +09001282 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001283 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001284 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001285 /* We have only one MySQL packet and it is a Handshake Initialization packet
1286 * but we need to have a second packet to know if it is alright
1287 */
Simon Horman4a741432013-02-23 15:35:38 +09001288 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001289 goto wait_more_data;
1290 }
1291 else {
1292 /* We have only one packet and it is an Error packet,
1293 * an error message is attached, so we can display it
1294 */
Simon Horman4a741432013-02-23 15:35:38 +09001295 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001296 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001297 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001298 }
Simon Horman4a741432013-02-23 15:35:38 +09001299 } else if (check->bi->i > first_packet_len + 4) {
1300 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1301 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1302 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001303
Simon Horman4a741432013-02-23 15:35:38 +09001304 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001305 /* We have 2 packets and that's good */
1306 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001307 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001308 /* No error packet */
1309 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001310 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001311 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001312 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001313 }
1314 else {
1315 /* An error message is attached in the Error packet
1316 * so we can display it ! :)
1317 */
Simon Horman4a741432013-02-23 15:35:38 +09001318 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001319 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001320 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001321 }
1322 }
1323 }
1324 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001325 if (!done)
1326 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001327 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001328 * it must be a protocol error
1329 */
Simon Horman4a741432013-02-23 15:35:38 +09001330 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001331 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001332 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001333 }
1334 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001335 break;
1336
1337 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001338 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001339 goto wait_more_data;
1340
1341 /* Check if the server speaks LDAP (ASN.1/BER)
1342 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1343 * http://tools.ietf.org/html/rfc4511
1344 */
1345
1346 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1347 * LDAPMessage: 0x30: SEQUENCE
1348 */
Simon Horman4a741432013-02-23 15:35:38 +09001349 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1350 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001351 }
1352 else {
1353 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001354 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001355
1356 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1357 * messageID: 0x02 0x01 0x01: INTEGER 1
1358 * protocolOp: 0x61: bindResponse
1359 */
1360 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001361 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1362 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001363
1364 goto out_wakeup;
1365 }
1366
1367 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001368 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001369
1370 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1371 * ldapResult: 0x0a 0x01: ENUMERATION
1372 */
1373 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001374 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1375 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001376
1377 goto out_wakeup;
1378 }
1379
1380 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1381 * resultCode
1382 */
Simon Horman4a741432013-02-23 15:35:38 +09001383 check->code = *(check->bi->data + msglen + 9);
1384 if (check->code) {
1385 set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001386 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001387 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001388 }
1389 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001390 break;
1391
1392 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001393 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001394 break;
1395 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001396
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001397 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001398 /* collect possible new errors */
1399 if (conn->flags & CO_FL_ERROR)
1400 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401
Nick Chalk57b1bf72010-03-16 15:50:46 +00001402 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001403 *check->bi->data = '\0';
1404 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001405
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001406 /* Close the connection... We absolutely want to perform a hard close
1407 * and reset the connection if some data are pending, otherwise we end
1408 * up with many TIME_WAITs and eat all the source port range quickly.
1409 * To avoid sending RSTs all the time, we first try to drain pending
1410 * data.
1411 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001412 if (conn->xprt && conn->xprt->shutw)
1413 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001414
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001415 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001416 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001417 conn->flags |= CO_FL_ERROR;
1418
Willy Tarreaua522f802012-11-23 08:56:35 +01001419 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001420 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001421 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001422
1423 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001424 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001425}
1426
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001427/*
1428 * This function is used only for server health-checks. It handles connection
1429 * status updates including errors. If necessary, it wakes the check task up.
1430 * It always returns 0.
1431 */
1432static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001433{
Simon Horman4a741432013-02-23 15:35:38 +09001434 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001435
Willy Tarreau6c560da2012-11-24 11:14:45 +01001436 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001437 /* We may get error reports bypassing the I/O handlers, typically
1438 * the case when sending a pure TCP check which fails, then the I/O
1439 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001440 * main processing task so let's simply wake it up. If we get here,
1441 * we expect errno to still be valid.
1442 */
1443 chk_report_conn_err(conn, errno, 0);
1444
Willy Tarreau2d351b62013-12-05 02:36:25 +01001445 __conn_data_stop_both(conn);
1446 task_wakeup(check->task, TASK_WOKEN_IO);
1447 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001448 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1449 /* we may get here if only a connection probe was required : we
1450 * don't have any data to send nor anything expected in response,
1451 * so the completion of the connection establishment is enough.
1452 */
1453 task_wakeup(check->task, TASK_WOKEN_IO);
1454 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001455
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001456 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001457 /* We're here because nobody wants to handle the error, so we
1458 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001459 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001460 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001461 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001462 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001463 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001464}
1465
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001466struct data_cb check_conn_cb = {
1467 .recv = event_srv_chk_r,
1468 .send = event_srv_chk_w,
1469 .wake = wake_srv_chk,
1470};
1471
Willy Tarreaubaaee002006-06-26 02:48:02 +02001472/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001473 * updates the server's weight during a warmup stage. Once the final weight is
1474 * reached, the task automatically stops. Note that any server status change
1475 * must have updated s->last_change accordingly.
1476 */
1477static struct task *server_warmup(struct task *t)
1478{
1479 struct server *s = t->context;
1480
1481 /* by default, plan on stopping the task */
1482 t->expire = TICK_ETERNITY;
1483 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1484 return t;
1485
Willy Tarreau004e0452013-11-21 11:22:01 +01001486 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001487
1488 /* probably that we can refill this server with a bit more connections */
1489 check_for_pending(s);
1490
1491 /* get back there in 1 second or 1/20th of the slowstart interval,
1492 * whichever is greater, resulting in small 5% steps.
1493 */
1494 if (s->state & SRV_WARMINGUP)
1495 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1496 return t;
1497}
1498
1499/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001500 * manages a server health-check. Returns
1501 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1502 */
Simon Horman63a4a822012-03-19 07:24:41 +09001503static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001504{
Simon Horman4a741432013-02-23 15:35:38 +09001505 struct check *check = t->context;
1506 struct server *s = check->server;
1507 struct connection *conn = check->conn;
Willy Tarreau640556c2014-05-09 23:38:15 +02001508 struct protocol *proto;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001509 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001510 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001511 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001512
Willy Tarreau2c115e52013-12-11 19:41:16 +01001513 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001514 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001515 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001516 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001517
Simon Horman671b6f02013-11-25 10:46:39 +09001518 /* we don't send any health-checks when the proxy is
1519 * stopped, the server should not be checked or the check
1520 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001521 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001522 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001523 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001524 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001525
1526 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001527 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001528
Willy Tarreau2c115e52013-12-11 19:41:16 +01001529 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001530 check->bi->p = check->bi->data;
1531 check->bi->i = 0;
1532 check->bo->p = check->bo->data;
1533 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001534
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001535 /* tcpcheck send/expect initialisation */
1536 if (check->type == PR_O2_TCPCHK_CHK)
1537 check->current_step = NULL;
1538
1539 /* prepare the check buffer.
1540 * This should not be used if check is the secondary agent check
1541 * of a server as s->proxy->check_req will relate to the
1542 * configuration of the primary check. Similarly, tcp-check uses
1543 * its own strings.
1544 */
Willy Tarreau33434322013-12-11 21:15:19 +01001545 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
Simon Horman4a741432013-02-23 15:35:38 +09001546 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001547
1548 /* we want to check if this host replies to HTTP or SSLv3 requests
1549 * so we'll send the request, and won't wake the checker up now.
1550 */
Simon Horman4a741432013-02-23 15:35:38 +09001551 if ((check->type) == PR_O2_SSL3_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001552 /* SSL requires that we put Unix time in the request */
1553 int gmt_time = htonl(date.tv_sec);
Simon Horman4a741432013-02-23 15:35:38 +09001554 memcpy(check->bo->data + 11, &gmt_time, 4);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001555 }
Simon Horman4a741432013-02-23 15:35:38 +09001556 else if ((check->type) == PR_O2_HTTP_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001557 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Willy Tarreaua1dab552014-04-14 15:04:54 +02001558 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Simon Horman4a741432013-02-23 15:35:38 +09001559 bo_putstr(check->bo, "\r\n");
1560 *check->bo->p = '\0'; /* to make gdb output easier to read */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001561 }
1562 }
1563
1564 /* prepare a new connection */
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001565 conn_init(conn);
Willy Tarreau910c6aa2013-10-24 15:08:37 +02001566 conn_prepare(conn, s->check_common.proto, s->check_common.xprt);
Willy Tarreau7abddb52013-10-24 15:31:04 +02001567 conn_attach(conn, check, &check_conn_cb);
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001568 conn->target = &s->obj_type;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001569
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001570 /* no client address */
1571 clear_addr(&conn->addr.from);
1572
Willy Tarreau640556c2014-05-09 23:38:15 +02001573 if (is_addr(&s->check_common.addr)) {
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001574 /* we'll connect to the check addr specified on the server */
Simon Horman66183002013-02-23 10:16:43 +09001575 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02001576 proto = s->check_common.proto;
1577 }
1578 else {
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001579 /* we'll connect to the addr on the server */
1580 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02001581 proto = s->proto;
1582 }
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001583
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001584 if (check->port) {
1585 set_host_port(&conn->addr.to, check->port);
1586 }
1587
1588 if (check->type == PR_O2_TCPCHK_CHK) {
Baptiste Assmannf621bea2014-02-03 22:38:15 +01001589 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1590 /* if first step is a 'connect', then tcpcheck_main must run it */
1591 if (r->action == TCPCHK_ACT_CONNECT) {
1592 tcpcheck_main(conn);
1593 return t;
1594 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001595 }
1596
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001597
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001598 /* It can return one of :
1599 * - SN_ERR_NONE if everything's OK
1600 * - SN_ERR_SRVTO if there are no more servers
1601 * - SN_ERR_SRVCL if the connection was refused by the server
1602 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1603 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1604 * - SN_ERR_INTERNAL for any other purely internal errors
1605 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001606 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001607 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001608 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001609 ret = SN_ERR_INTERNAL;
Willy Tarreau640556c2014-05-09 23:38:15 +02001610 if (proto->connect)
1611 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001612 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau57cd3e42013-10-24 22:01:26 +02001613 if (s->check.send_proxy) {
1614 conn->send_proxy_ofs = 1;
1615 conn->flags |= CO_FL_SEND_PROXY;
1616 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001617
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001618 switch (ret) {
1619 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001620 /* we allow up to min(inter, timeout.connect) for a connection
1621 * to establish but only when timeout.check is set
1622 * as it may be to short for a full check otherwise
1623 */
Simon Horman4a741432013-02-23 15:35:38 +09001624 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001625
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001626 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1627 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1628 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001629 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001630
1631 if (check->type)
1632 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1633
Willy Tarreau5a78f362012-11-23 12:47:05 +01001634 goto reschedule;
1635
1636 case SN_ERR_SRVTO: /* ETIMEDOUT */
1637 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001638 conn->flags |= CO_FL_ERROR;
1639 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001640 break;
1641 case SN_ERR_PRXCOND:
1642 case SN_ERR_RESOURCE:
1643 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001644 conn->flags |= CO_FL_ERROR;
1645 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001646 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001647 }
1648
Willy Tarreau5a78f362012-11-23 12:47:05 +01001649 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001650
Willy Tarreau2c115e52013-12-11 19:41:16 +01001651 check->state &= ~CHK_ST_INPROGRESS;
Simon Hormand8583062013-11-25 10:46:33 +09001652 check_failed(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001653
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001654 /* we allow up to min(inter, timeout.connect) for a connection
1655 * to establish but only when timeout.check is set
1656 * as it may be to short for a full check otherwise
1657 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001658 while (tick_is_expired(t->expire, now_ms)) {
1659 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001660
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001661 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001662 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001663
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001664 if (s->proxy->timeout.check)
1665 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001666 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001667 }
1668 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001669 /* there was a test running.
1670 * First, let's check whether there was an uncaught error,
1671 * which can happen on connect timeout or error.
1672 */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001673 if (s->check.result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001674 /* good connection is enough for pure TCP check */
1675 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001676 if (check->use_ssl)
1677 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001678 else
Simon Horman4a741432013-02-23 15:35:38 +09001679 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001680 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001681 else if ((conn->flags & CO_FL_ERROR) || expired) {
1682 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001683 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001684 else
1685 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001686 }
1687
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001688 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001689 if (conn->xprt) {
1690 /* The check was aborted and the connection was not yet closed.
1691 * This can happen upon timeout, or when an external event such
1692 * as a failed response coupled with "observe layer7" caused the
1693 * server state to be suddenly changed.
1694 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001695 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001696 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001697 }
1698
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001699 if (check->result == CHK_RES_FAILED) /* a failure or timeout detected */
Simon Hormand8583062013-11-25 10:46:33 +09001700 check_failed(check);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001701 else { /* check was OK */
Willy Tarreau48494c02007-11-30 10:41:39 +01001702 /* we may have to add/remove this server from the LB group */
1703 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001704 if ((s->state & SRV_GOINGDOWN) && (check->result != CHK_RES_CONDPASS))
Simon Horman4a741432013-02-23 15:35:38 +09001705 set_server_enabled(check);
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001706 else if (!(s->state & SRV_GOINGDOWN) && (check->result == CHK_RES_CONDPASS))
Simon Horman4a741432013-02-23 15:35:38 +09001707 set_server_disabled(check);
Willy Tarreau48494c02007-11-30 10:41:39 +01001708 }
1709
Willy Tarreau9f708ab2013-12-23 14:04:17 +01001710 if (!(s->state & SRV_MAINTAIN) &&
1711 check->health < check->rise + check->fall - 1) {
Simon Horman125d0992013-02-24 17:23:38 +09001712 check->health++; /* was bad, stays for a while */
Simon Horman4a741432013-02-23 15:35:38 +09001713 set_server_up(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001714 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001715 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01001716 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001717
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001718 rv = 0;
1719 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001720 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001721 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001722 }
Simon Horman4a741432013-02-23 15:35:38 +09001723 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001724 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001725
1726 reschedule:
1727 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09001728 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001729 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001730 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001731}
1732
Simon Horman5c942422013-11-25 10:46:32 +09001733static int start_check_task(struct check *check, int mininter,
1734 int nbcheck, int srvpos)
1735{
1736 struct task *t;
1737 /* task for the check */
1738 if ((t = task_new()) == NULL) {
1739 Alert("Starting [%s:%s] check: out of memory.\n",
1740 check->server->proxy->id, check->server->id);
1741 return 0;
1742 }
1743
1744 check->task = t;
1745 t->process = process_chk;
1746 t->context = check;
1747
Willy Tarreau1746eec2014-04-25 10:46:47 +02001748 if (mininter < srv_getinter(check))
1749 mininter = srv_getinter(check);
1750
1751 if (global.max_spread_checks && mininter > global.max_spread_checks)
1752 mininter = global.max_spread_checks;
1753
Simon Horman5c942422013-11-25 10:46:32 +09001754 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02001755 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09001756 check->start = now;
1757 task_queue(t);
1758
1759 return 1;
1760}
1761
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001762/*
1763 * Start health-check.
1764 * Returns 0 if OK, -1 if error, and prints the error in this case.
1765 */
1766int start_checks() {
1767
1768 struct proxy *px;
1769 struct server *s;
1770 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09001771 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001772
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001773 /* 1- count the checkers to run simultaneously.
1774 * We also determine the minimum interval among all of those which
1775 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1776 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001777 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001778 * too short an interval for all others.
1779 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001780 for (px = proxy; px; px = px->next) {
1781 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01001782 if (s->slowstart) {
1783 if ((t = task_new()) == NULL) {
1784 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1785 return -1;
1786 }
1787 /* We need a warmup task that will be called when the server
1788 * state switches from down to up.
1789 */
1790 s->warmup = t;
1791 t->process = server_warmup;
1792 t->context = s;
1793 t->expire = TICK_ETERNITY;
1794 }
1795
Willy Tarreaud8514a22013-12-11 21:10:14 +01001796 if (s->check.state & CHK_ST_CONFIGURED) {
1797 nbcheck++;
1798 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1799 (!mininter || mininter > srv_getinter(&s->check)))
1800 mininter = srv_getinter(&s->check);
1801 }
Willy Tarreau15f39102013-12-11 20:41:18 +01001802
Willy Tarreaud8514a22013-12-11 21:10:14 +01001803 if (s->agent.state & CHK_ST_CONFIGURED) {
1804 nbcheck++;
1805 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1806 (!mininter || mininter > srv_getinter(&s->agent)))
1807 mininter = srv_getinter(&s->agent);
1808 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001809 }
1810 }
1811
Simon Horman4a741432013-02-23 15:35:38 +09001812 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001813 return 0;
1814
1815 srand((unsigned)time(NULL));
1816
1817 /*
1818 * 2- start them as far as possible from each others. For this, we will
1819 * start them after their interval set to the min interval divided by
1820 * the number of servers, weighted by the server's position in the list.
1821 */
1822 for (px = proxy; px; px = px->next) {
1823 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09001824 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01001825 if (s->check.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09001826 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1827 return -1;
1828 srvpos++;
1829 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001830
Simon Hormand60d6912013-11-25 10:46:36 +09001831 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01001832 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09001833 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1834 return -1;
1835 }
1836 srvpos++;
1837 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001838 }
1839 }
1840 return 0;
1841}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001842
1843/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001844 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001845 * The buffer MUST be terminated by a null byte before calling this function.
1846 * Sets server status appropriately. The caller is responsible for ensuring
1847 * that the buffer contains at least 13 characters. If <done> is zero, we may
1848 * return 0 to indicate that data is required to decide of a match.
1849 */
1850static int httpchk_expect(struct server *s, int done)
1851{
1852 static char status_msg[] = "HTTP status check returned code <000>";
1853 char status_code[] = "000";
1854 char *contentptr;
1855 int crlf;
1856 int ret;
1857
1858 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1859 case PR_O2_EXP_STS:
1860 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001861 memcpy(status_code, s->check.bi->data + 9, 3);
1862 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001863
1864 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1865 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1866 else
1867 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1868
1869 /* we necessarily have the response, so there are no partial failures */
1870 if (s->proxy->options2 & PR_O2_EXP_INV)
1871 ret = !ret;
1872
Simon Horman4a741432013-02-23 15:35:38 +09001873 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01001874 break;
1875
1876 case PR_O2_EXP_STR:
1877 case PR_O2_EXP_RSTR:
1878 /* very simple response parser: ignore CR and only count consecutive LFs,
1879 * stop with contentptr pointing to first char after the double CRLF or
1880 * to '\0' if crlf < 2.
1881 */
1882 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001883 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001884 if (crlf >= 2)
1885 break;
1886 if (*contentptr == '\r')
1887 continue;
1888 else if (*contentptr == '\n')
1889 crlf++;
1890 else
1891 crlf = 0;
1892 }
1893
1894 /* Check that response contains a body... */
1895 if (crlf < 2) {
1896 if (!done)
1897 return 0;
1898
Simon Horman4a741432013-02-23 15:35:38 +09001899 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001900 "HTTP content check could not find a response body");
1901 return 1;
1902 }
1903
1904 /* Check that response body is not empty... */
1905 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001906 if (!done)
1907 return 0;
1908
Simon Horman4a741432013-02-23 15:35:38 +09001909 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001910 "HTTP content check found empty response body");
1911 return 1;
1912 }
1913
1914 /* Check the response content against the supplied string
1915 * or regex... */
1916 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1917 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1918 else
1919 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1920
1921 /* if we don't match, we may need to wait more */
1922 if (!ret && !done)
1923 return 0;
1924
1925 if (ret) {
1926 /* content matched */
1927 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001928 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001929 "HTTP check matched unwanted content");
1930 else
Simon Horman4a741432013-02-23 15:35:38 +09001931 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001932 "HTTP content check matched");
1933 }
1934 else {
1935 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001936 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001937 "HTTP check did not match unwanted content");
1938 else
Simon Horman4a741432013-02-23 15:35:38 +09001939 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001940 "HTTP content check did not match");
1941 }
1942 break;
1943 }
1944 return 1;
1945}
1946
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001947/*
1948 * return the id of a step in a send/expect session
1949 */
1950static int tcpcheck_get_step_id(struct server *s)
1951{
1952 struct tcpcheck_rule *cur = NULL, *next = NULL;
1953 int i = 0;
1954
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001955 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001956
1957 /* no step => first step */
1958 if (cur == NULL)
1959 return 1;
1960
1961 /* increment i until current step */
1962 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
1963 if (next->list.p == &cur->list)
1964 break;
1965 ++i;
1966 }
1967
1968 return i;
1969}
1970
1971static void tcpcheck_main(struct connection *conn)
1972{
1973 char *contentptr;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001974 struct list *head = NULL;
1975 struct tcpcheck_rule *cur = NULL;
1976 int done = 0, ret = 0;
1977
1978 struct check *check = conn->owner;
1979 struct server *s = check->server;
1980 struct task *t = check->task;
1981
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001982 /*
1983 * don't do anything until the connection is established but if we're running
1984 * first step which must be a connect
1985 */
1986 if (check->current_step && (!(conn->flags & CO_FL_CONNECTED))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001987 /* update expire time, should be done by process_chk */
1988 /* we allow up to min(inter, timeout.connect) for a connection
1989 * to establish but only when timeout.check is set
1990 * as it may be to short for a full check otherwise
1991 */
1992 while (tick_is_expired(t->expire, now_ms)) {
1993 int t_con;
1994
1995 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1996 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1997
1998 if (s->proxy->timeout.check)
1999 t->expire = tick_first(t->expire, t_con);
2000 }
2001 return;
2002 }
2003
2004 /* here, we know that the connection is established */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002005 if (check->result != CHK_RES_UNKNOWN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002006 goto out_end_tcpcheck;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002007
2008 /* head is be the first element of the double chained list */
2009 head = &s->proxy->tcpcheck_rules;
2010
2011 /* no step means first step
2012 * initialisation */
2013 if (check->current_step == NULL) {
2014 check->bo->p = check->bo->data;
2015 check->bo->o = 0;
2016 check->bi->p = check->bi->data;
2017 check->bi->i = 0;
2018 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
2019 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2020 if (s->proxy->timeout.check)
2021 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2022 }
2023 /* keep on processing step */
2024 else {
2025 cur = check->current_step;
2026 }
2027
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002028 if (conn->flags & CO_FL_HANDSHAKE)
2029 return;
2030
2031 /* It's only the rules which will enable send/recv */
2032 __conn_data_stop_both(conn);
2033
Willy Tarreauabca5b62013-12-06 14:19:25 +01002034 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002035 /* we have to try to flush the output buffer before reading, at the end,
2036 * or if we're about to send a string that does not fit in the remaining space.
2037 */
2038 if (check->bo->o &&
2039 (&cur->list == head ||
2040 check->current_step->action != TCPCHK_ACT_SEND ||
2041 check->current_step->string_len >= buffer_total_space(check->bo))) {
2042
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002043 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002044 if (conn->flags & CO_FL_ERROR) {
2045 chk_report_conn_err(conn, errno, 0);
2046 __conn_data_stop_both(conn);
2047 goto out_end_tcpcheck;
2048 }
2049 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002050 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002051 }
2052
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002053 /* did we reach the end ? If so, let's check that everything was sent */
2054 if (&cur->list == head) {
2055 if (check->bo->o)
2056 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002057 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002058 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002059
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002060 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2061 struct protocol *proto;
2062 struct xprt_ops *xprt;
2063
2064 /* mark the step as started */
2065 check->last_started_step = check->current_step;
2066 /* first, shut existing connection */
2067 conn_force_close(conn);
2068
2069 /* prepare new connection */
2070 /* initialization */
2071 conn_init(conn);
2072 conn_attach(conn, check, &check_conn_cb);
2073 conn->target = &s->obj_type;
2074
2075 /* no client address */
2076 clear_addr(&conn->addr.from);
2077
Willy Tarreau640556c2014-05-09 23:38:15 +02002078 if (is_addr(&s->check_common.addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002079 /* we'll connect to the check addr specified on the server */
2080 conn->addr.to = s->check_common.addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002081 proto = s->check_common.proto;
2082 }
2083 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002084 /* we'll connect to the addr on the server */
2085 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002086 proto = s->proto;
2087 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002088
2089 /* port */
2090 if (check->current_step->port)
2091 set_host_port(&conn->addr.to, check->current_step->port);
2092 else if (check->port)
2093 set_host_port(&conn->addr.to, check->port);
2094
2095#ifdef USE_OPENSSL
2096 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2097 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002098 }
2099 else {
2100 xprt = &raw_sock;
2101 }
2102#else /* USE_OPENSSL */
2103 xprt = &raw_sock;
2104#endif /* USE_OPENSSL */
2105 conn_prepare(conn, proto, xprt);
2106
2107 ret = SN_ERR_INTERNAL;
2108 if (proto->connect)
2109 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
2110 conn->flags |= CO_FL_WAKE_DATA;
2111 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2112 conn->send_proxy_ofs = 1;
2113 conn->flags |= CO_FL_SEND_PROXY;
2114 }
2115
2116 /* It can return one of :
2117 * - SN_ERR_NONE if everything's OK
2118 * - SN_ERR_SRVTO if there are no more servers
2119 * - SN_ERR_SRVCL if the connection was refused by the server
2120 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2121 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2122 * - SN_ERR_INTERNAL for any other purely internal errors
2123 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2124 * Note that we try to prevent the network stack from sending the ACK during the
2125 * connect() when a pure TCP check is used (without PROXY protocol).
2126 */
2127 switch (ret) {
2128 case SN_ERR_NONE:
2129 /* we allow up to min(inter, timeout.connect) for a connection
2130 * to establish but only when timeout.check is set
2131 * as it may be to short for a full check otherwise
2132 */
2133 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2134
2135 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2136 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2137 t->expire = tick_first(t->expire, t_con);
2138 }
2139 break;
2140 case SN_ERR_SRVTO: /* ETIMEDOUT */
2141 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2142 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2143 tcpcheck_get_step_id(s), strerror(errno));
2144 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2145 goto out_end_tcpcheck;
2146 case SN_ERR_PRXCOND:
2147 case SN_ERR_RESOURCE:
2148 case SN_ERR_INTERNAL:
2149 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2150 tcpcheck_get_step_id(s));
2151 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2152 goto out_end_tcpcheck;
2153 }
2154
2155 /* allow next rule */
2156 cur = (struct tcpcheck_rule *)cur->list.n;
2157 check->current_step = cur;
2158
2159 /* don't do anything until the connection is established */
2160 if (!(conn->flags & CO_FL_CONNECTED)) {
2161 /* update expire time, should be done by process_chk */
2162 /* we allow up to min(inter, timeout.connect) for a connection
2163 * to establish but only when timeout.check is set
2164 * as it may be to short for a full check otherwise
2165 */
2166 while (tick_is_expired(t->expire, now_ms)) {
2167 int t_con;
2168
2169 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2170 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2171
2172 if (s->proxy->timeout.check)
2173 t->expire = tick_first(t->expire, t_con);
2174 }
2175 return;
2176 }
2177
2178 } /* end 'connect' */
2179 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2180 /* mark the step as started */
2181 check->last_started_step = check->current_step;
2182
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002183 /* reset the read buffer */
2184 if (*check->bi->data != '\0') {
2185 *check->bi->data = '\0';
2186 check->bi->i = 0;
2187 }
2188
2189 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2190 conn->flags |= CO_FL_ERROR;
2191 chk_report_conn_err(conn, 0, 0);
2192 goto out_end_tcpcheck;
2193 }
2194
Willy Tarreauabca5b62013-12-06 14:19:25 +01002195 if (check->current_step->string_len >= check->bo->size) {
2196 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2197 check->current_step->string_len, check->bo->size,
2198 tcpcheck_get_step_id(s));
2199 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2200 goto out_end_tcpcheck;
2201 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002202
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002203 /* do not try to send if there is no space */
2204 if (check->current_step->string_len >= buffer_total_space(check->bo))
2205 continue;
2206
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002207 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2208 *check->bo->p = '\0'; /* to make gdb output easier to read */
2209
Willy Tarreauabca5b62013-12-06 14:19:25 +01002210 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002211 cur = (struct tcpcheck_rule *)cur->list.n;
2212 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002213 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002214 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002215 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002216 goto out_end_tcpcheck;
2217
Willy Tarreau310987a2014-01-22 19:46:33 +01002218 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002219 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2220 done = 1;
2221 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2222 /* Report network errors only if we got no other data. Otherwise
2223 * we'll let the upper layers decide whether the response is OK
2224 * or not. It is very common that an RST sent by the server is
2225 * reported as an error just after the last data chunk.
2226 */
2227 chk_report_conn_err(conn, errno, 0);
2228 goto out_end_tcpcheck;
2229 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002230 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002231 else
2232 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002233 }
2234
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002235 /* mark the step as started */
2236 check->last_started_step = check->current_step;
2237
2238
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002239 /* Intermediate or complete response received.
2240 * Terminate string in check->bi->data buffer.
2241 */
2242 if (check->bi->i < check->bi->size) {
2243 check->bi->data[check->bi->i] = '\0';
2244 }
2245 else {
2246 check->bi->data[check->bi->i - 1] = '\0';
2247 done = 1; /* buffer full, don't wait for more data */
2248 }
2249
2250 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002251
2252 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002253 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002254 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002255 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002256
2257 /* empty response */
2258 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2259 tcpcheck_get_step_id(s));
2260 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2261
2262 goto out_end_tcpcheck;
2263 }
2264
2265 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002266 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002267
Willy Tarreaua970c282013-12-06 12:47:19 +01002268 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002269 if (cur->string != NULL)
Willy Tarreauec6b0122014-05-13 17:57:29 +02002270 ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002271 else if (cur->expect_regex != NULL)
2272 ret = regexec(cur->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
2273
2274 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002275 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002276
2277 /* matched */
2278 if (ret) {
2279 /* matched but we did not want to => ERROR */
2280 if (cur->inverse) {
2281 /* we were looking for a string */
2282 if (cur->string != NULL) {
2283 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2284 cur->string, tcpcheck_get_step_id(s));
2285 }
2286 else {
2287 /* we were looking for a regex */
2288 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2289 tcpcheck_get_step_id(s));
2290 }
2291 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2292 goto out_end_tcpcheck;
2293 }
2294 /* matched and was supposed to => OK, next step */
2295 else {
2296 cur = (struct tcpcheck_rule*)cur->list.n;
2297 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002298 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002299 goto tcpcheck_expect;
2300 __conn_data_stop_recv(conn);
2301 }
2302 }
2303 else {
2304 /* not matched */
2305 /* not matched and was not supposed to => OK, next step */
2306 if (cur->inverse) {
2307 cur = (struct tcpcheck_rule*)cur->list.n;
2308 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002309 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002310 goto tcpcheck_expect;
2311 __conn_data_stop_recv(conn);
2312 }
2313 /* not matched but was supposed to => ERROR */
2314 else {
2315 /* we were looking for a string */
2316 if (cur->string != NULL) {
2317 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2318 cur->string, tcpcheck_get_step_id(s));
2319 }
2320 else {
2321 /* we were looking for a regex */
2322 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2323 tcpcheck_get_step_id(s));
2324 }
2325 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2326 goto out_end_tcpcheck;
2327 }
2328 }
2329 } /* end expect */
2330 } /* end loop over double chained step list */
2331
2332 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2333 goto out_end_tcpcheck;
2334
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002335 out_need_io:
2336 if (check->bo->o)
2337 __conn_data_want_send(conn);
2338
2339 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2340 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002341 return;
2342
2343 out_end_tcpcheck:
2344 /* collect possible new errors */
2345 if (conn->flags & CO_FL_ERROR)
2346 chk_report_conn_err(conn, 0, 0);
2347
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002348 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002349 check->current_step = NULL;
2350
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002351 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002352 conn->flags |= CO_FL_ERROR;
2353
2354 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002355
2356 return;
2357}
2358
2359
Willy Tarreaubd741542010-03-16 18:46:54 +01002360/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002361 * Local variables:
2362 * c-indent-level: 8
2363 * c-basic-offset: 8
2364 * End:
2365 */