blob: fceb2c74efda6c4046a22730e8f6e4140f6b2194 [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;
210
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200211 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100212 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900213 check->desc[0] = '\0';
214 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200215 return;
216 }
217
Simon Horman4a741432013-02-23 15:35:38 +0900218 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200219 return;
220
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200221 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900222 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
223 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200224 } else
Simon Horman4a741432013-02-23 15:35:38 +0900225 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200226
Simon Horman4a741432013-02-23 15:35:38 +0900227 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200228 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900229 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200230
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100231 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900232 check->duration = -1;
233 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200234 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900235 check->duration = tv_ms_elapsed(&check->start, &now);
236 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237 }
238
Simon Horman2f1f9552013-11-25 10:46:37 +0900239 /* Failure to connect to the agent as a secondary check should not
240 * cause the server to be marked down. So only log status changes
241 * for HCHK_STATUS_* statuses */
Willy Tarreau33434322013-12-11 21:15:19 +0100242 if ((check->state & CHK_ST_AGENT) && check->status < HCHK_STATUS_L7TOUT)
Simon Horman2f1f9552013-11-25 10:46:37 +0900243 return;
244
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200245 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100246 (((check->health != 0) && (check->result == CHK_RES_FAILED)) ||
Simon Horman8c3d0be2013-11-25 10:46:40 +0900247 (((check->health != check->rise + check->fall - 1) ||
248 (!s->uweight && !(s->state & SRV_DRAIN)) ||
249 (s->uweight && (s->state & SRV_DRAIN))) &&
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100250 (check->result >= CHK_RES_PASSED)) ||
251 ((s->state & SRV_GOINGDOWN) && (check->result != CHK_RES_CONDPASS)) ||
252 (!(s->state & SRV_GOINGDOWN) && (check->result == CHK_RES_CONDPASS)))) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200253
254 int health, rise, fall, state;
255
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100256 chunk_reset(&trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200257
258 /* FIXME begin: calculate local version of the health/rise/fall/state */
Simon Horman125d0992013-02-24 17:23:38 +0900259 health = check->health;
Simon Horman58c32972013-11-25 10:46:38 +0900260 rise = check->rise;
261 fall = check->fall;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200262 state = s->state;
263
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100264 switch (check->result) {
265 case CHK_RES_FAILED:
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200266 if (health > rise) {
267 health--; /* still good */
268 } else {
269 if (health == rise)
270 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
271
272 health = 0;
273 }
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100274 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200275
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100276 case CHK_RES_PASSED:
277 case CHK_RES_CONDPASS:
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200278 if (health < rise + fall - 1) {
279 health++; /* was bad, stays for a while */
280
281 if (health == rise)
282 state |= SRV_RUNNING;
283
284 if (health >= rise)
285 health = rise + fall - 1; /* OK now */
286 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100287
288 /* clear consecutive_errors if observing is enabled */
289 if (s->onerror)
290 s->consecutive_errors = 0;
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100291 break;
292 default:
293 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200294 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200295
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100296 chunk_appendf(&trash,
297 "Health check for %sserver %s/%s %s%s",
298 s->state & SRV_BACKUP ? "backup " : "",
299 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100300 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
301 (check->result >= CHK_RES_PASSED) ? "succeeded":"failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200302
Simon Horman4a741432013-02-23 15:35:38 +0900303 server_status_printf(&trash, s, check, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200304
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100305 chunk_appendf(&trash, ", status: %d/%d %s",
306 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
307 (state & SRV_RUNNING) ? (fall) : (rise),
Simon Horman8c3d0be2013-11-25 10:46:40 +0900308 (state & SRV_RUNNING)?(s->eweight?"UP":"DRAIN"):"DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200309
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100310 Warning("%s.\n", trash.str);
311 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200312 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200313}
314
Willy Tarreau48494c02007-11-30 10:41:39 +0100315/* sends a log message when a backend goes down, and also sets last
316 * change date.
317 */
318static void set_backend_down(struct proxy *be)
319{
320 be->last_change = now.tv_sec;
321 be->down_trans++;
322
323 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
324 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
325}
326
327/* Redistribute pending connections when a server goes down. The number of
328 * connections redistributed is returned.
329 */
330static int redistribute_pending(struct server *s)
331{
332 struct pendconn *pc, *pc_bck, *pc_end;
333 int xferred = 0;
334
335 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
336 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100337 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
338 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100339 /* The REDISP option was specified. We will ignore
340 * cookie and force to balance or use the dispatcher.
341 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100342
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100343 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100344 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100345
Willy Tarreau48494c02007-11-30 10:41:39 +0100346 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200347 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100348 xferred++;
349 }
350 }
351 return xferred;
352}
353
354/* Check for pending connections at the backend, and assign some of them to
355 * the server coming up. The server's weight is checked before being assigned
356 * connections it may not be able to handle. The total number of transferred
357 * connections is returned.
358 */
359static int check_for_pending(struct server *s)
360{
361 int xferred;
362
363 if (!s->eweight)
364 return 0;
365
366 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
367 struct session *sess;
368 struct pendconn *p;
369
370 p = pendconn_from_px(s->proxy);
371 if (!p)
372 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100373 p->sess->target = &s->obj_type;
Willy Tarreau48494c02007-11-30 10:41:39 +0100374 sess = p->sess;
375 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200376 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100377 }
378 return xferred;
379}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200380
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700381/* Shutdown all connections of a server. The caller must pass a termination
382 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
383 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900384 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700385static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900386{
387 struct session *session, *session_bck;
388
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200389 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
390 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700391 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900392}
393
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700394/* Shutdown all connections of all backup servers of a proxy. The caller must
395 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
396 * the reason for the shutdown.
397 */
398static void shutdown_backup_sessions(struct proxy *px, int why)
399{
400 struct server *srv;
401
402 for (srv = px->srv; srv != NULL; srv = srv->next)
403 if (srv->state & SRV_BACKUP)
404 shutdown_sessions(srv, why);
405}
406
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407/* Sets server <s> down, notifies by all available means, recounts the
408 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200409 * possible to other servers. It automatically recomputes the number of
410 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411 */
Simon Horman4a741432013-02-23 15:35:38 +0900412void set_server_down(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413{
Simon Horman4a741432013-02-23 15:35:38 +0900414 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100415 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416 int xferred;
417
Cyril Bontécd19e512010-01-31 22:34:03 +0100418 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900419 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100420 }
421
Simon Horman58c32972013-11-25 10:46:38 +0900422 if ((s->state & SRV_RUNNING && check->health == check->rise) || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100423 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200424 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200425
426 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100427 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200428 if (s->proxy->lbprm.set_server_status_down)
429 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900431 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700432 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900433
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434 /* we might have sessions queued on this server and waiting for
435 * a connection. Those which are redispatchable will be queued
436 * to another server or to the proxy itself.
437 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100438 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100439
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100440 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100441
Cyril Bontécd19e512010-01-31 22:34:03 +0100442 if (s->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100443 chunk_appendf(&trash,
444 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
445 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100446 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100447 chunk_appendf(&trash,
448 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
449 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100450
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100451 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900452 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : 0),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100453 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100454 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100455 Warning("%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200456
Willy Tarreau48494c02007-11-30 10:41:39 +0100457 /* we don't send an alert if the server was previously paused */
458 if (srv_was_paused)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100459 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreau48494c02007-11-30 10:41:39 +0100460 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100461 send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200462
Willy Tarreaud64d2252010-10-17 17:16:42 +0200463 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100464 set_backend_down(s->proxy);
465
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200466 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100467
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100468 for (srv = s->trackers; srv; srv = srv->tracknext)
469 if (!(srv->state & SRV_MAINTAIN))
470 /* Only notify tracking servers that are not already in maintenance. */
471 set_server_down(&srv->check);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200472 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100473
Simon Horman125d0992013-02-24 17:23:38 +0900474 check->health = 0; /* failure */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475}
476
Simon Horman4a741432013-02-23 15:35:38 +0900477void set_server_up(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100478
Simon Horman4a741432013-02-23 15:35:38 +0900479 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100480 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100481 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100482 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100483
Cyril Bontécd19e512010-01-31 22:34:03 +0100484 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900485 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100486 }
487
Willy Tarreaubb9665e2013-12-14 16:14:15 +0100488 if (s->track ||
Willy Tarreau02541e82013-12-16 18:08:36 +0100489 ((s->check.state & CHK_ST_ENABLED) && (s->check.health == s->check.rise) &&
490 (s->agent.health >= s->agent.rise || !(s->agent.state & CHK_ST_ENABLED))) ||
491 ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health == s->agent.rise) &&
Willy Tarreaue24d9632013-12-28 21:21:31 +0100492 (s->check.health >= s->check.rise || !(s->check.state & CHK_ST_ENABLED))) ||
493 (!(s->agent.state & CHK_ST_ENABLED) && !(s->check.state & CHK_ST_ENABLED))) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100494 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
495 if (s->proxy->last_change < now.tv_sec) // ignore negative times
496 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
497 s->proxy->last_change = now.tv_sec;
498 }
499
500 if (s->last_change < now.tv_sec) // ignore negative times
501 s->down_time += now.tv_sec - s->last_change;
502
503 s->last_change = now.tv_sec;
504 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100505 s->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +0100506 s->check.state &= ~CHK_ST_PAUSED;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100507
508 if (s->slowstart > 0) {
509 s->state |= SRV_WARMINGUP;
Willy Tarreau2e993902011-10-31 11:53:20 +0100510 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100511 }
Willy Tarreau004e0452013-11-21 11:22:01 +0100512
513 server_recalc_eweight(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100514
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700515 /* If the server is set with "on-marked-up shutdown-backup-sessions",
516 * and it's not a backup server and its effective weight is > 0,
517 * then it can accept new connections, so we shut down all sessions
518 * on all backup servers.
519 */
520 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
521 !(s->state & SRV_BACKUP) && s->eweight)
522 shutdown_backup_sessions(s->proxy, SN_ERR_UP);
523
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100524 /* check if we can handle some connections queued at the proxy. We
525 * will take as many as we can handle.
526 */
527 xferred = check_for_pending(s);
528
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100529 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100530
Willy Tarreau45446782012-03-09 17:16:09 +0100531 if (old_state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100532 chunk_appendf(&trash,
533 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
534 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100535 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100536 chunk_appendf(&trash,
537 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
538 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100539
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100540 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900541 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100542 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100543 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100544
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100545 Warning("%s.\n", trash.str);
546 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100547
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100548 for (srv = s->trackers; srv; srv = srv->tracknext)
549 if (!(srv->state & SRV_MAINTAIN))
550 /* Only notify tracking servers if they're not in maintenance. */
551 set_server_up(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100552 }
553
Simon Horman58c32972013-11-25 10:46:38 +0900554 if (check->health >= check->rise)
555 check->health = check->rise + check->fall - 1; /* OK now */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100556
557}
558
Simon Horman4a741432013-02-23 15:35:38 +0900559static void set_server_disabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100560
Simon Horman4a741432013-02-23 15:35:38 +0900561 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100562 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100563 int xferred;
564
565 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200566 if (s->proxy->lbprm.set_server_status_down)
567 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100568
569 /* we might have sessions queued on this server and waiting for
570 * a connection. Those which are redispatchable will be queued
571 * to another server or to the proxy itself.
572 */
573 xferred = redistribute_pending(s);
574
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100575 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100576
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100577 chunk_appendf(&trash,
578 "Load-balancing on %sServer %s/%s is disabled",
579 s->state & SRV_BACKUP ? "Backup " : "",
580 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100581
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100582 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900583 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100584 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100585
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100586 Warning("%s.\n", trash.str);
587 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100588
589 if (!s->proxy->srv_bck && !s->proxy->srv_act)
590 set_backend_down(s->proxy);
591
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100592 for (srv = s->trackers; srv; srv = srv->tracknext)
593 set_server_disabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100594}
595
Simon Horman4a741432013-02-23 15:35:38 +0900596static void set_server_enabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100597
Simon Horman4a741432013-02-23 15:35:38 +0900598 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100599 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100600 int xferred;
601
602 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200603 if (s->proxy->lbprm.set_server_status_up)
604 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100605
606 /* check if we can handle some connections queued at the proxy. We
607 * will take as many as we can handle.
608 */
609 xferred = check_for_pending(s);
610
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100611 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100612
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100613 chunk_appendf(&trash,
614 "Load-balancing on %sServer %s/%s is enabled again",
615 s->state & SRV_BACKUP ? "Backup " : "",
616 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100617
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100618 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900619 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100620 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100621
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100622 Warning("%s.\n", trash.str);
623 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100624
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100625 for (srv = s->trackers; srv; srv = srv->tracknext)
626 set_server_enabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100627}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200628
Simon Hormand8583062013-11-25 10:46:33 +0900629static void check_failed(struct check *check)
630{
631 struct server *s = check->server;
632
Simon Horman2f1f9552013-11-25 10:46:37 +0900633 /* The agent secondary check should only cause a server to be marked
634 * as down if check->status is HCHK_STATUS_L7STS, which indicates
635 * that the agent returned "fail", "stopped" or "down".
636 * The implication here is that failure to connect to the agent
637 * as a secondary check should not cause the server to be marked
638 * down. */
Willy Tarreau33434322013-12-11 21:15:19 +0100639 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
Simon Horman2f1f9552013-11-25 10:46:37 +0900640 return;
641
Simon Horman58c32972013-11-25 10:46:38 +0900642 if (check->health > check->rise) {
Simon Hormand8583062013-11-25 10:46:33 +0900643 check->health--; /* still good */
644 s->counters.failed_checks++;
645 }
646 else
647 set_server_down(check);
648}
649
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100650/* note: use health_adjust() only, which first checks that the observe mode is
651 * enabled.
652 */
653void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100654{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100655 int failed;
656 int expire;
657
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100658 if (s->observe >= HANA_OBS_SIZE)
659 return;
660
Willy Tarreaubb956662013-01-24 00:37:39 +0100661 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100662 return;
663
664 switch (analyze_statuses[status].lr[s->observe - 1]) {
665 case 1:
666 failed = 1;
667 break;
668
669 case 2:
670 failed = 0;
671 break;
672
673 default:
674 return;
675 }
676
677 if (!failed) {
678 /* good: clear consecutive_errors */
679 s->consecutive_errors = 0;
680 return;
681 }
682
683 s->consecutive_errors++;
684
685 if (s->consecutive_errors < s->consecutive_errors_limit)
686 return;
687
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100688 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
689 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100690
691 switch (s->onerror) {
692 case HANA_ONERR_FASTINTER:
693 /* force fastinter - nothing to do here as all modes force it */
694 break;
695
696 case HANA_ONERR_SUDDTH:
697 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900698 if (s->check.health > s->check.rise)
699 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100700
701 /* no break - fall through */
702
703 case HANA_ONERR_FAILCHK:
704 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900705 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Simon Hormand8583062013-11-25 10:46:33 +0900706 check_failed(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100707
708 break;
709
710 case HANA_ONERR_MARKDWN:
711 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900712 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900713 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
714 set_server_down(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100715
716 break;
717
718 default:
719 /* write a warning? */
720 break;
721 }
722
723 s->consecutive_errors = 0;
724 s->counters.failed_hana++;
725
Simon Horman66183002013-02-23 10:16:43 +0900726 if (s->check.fastinter) {
727 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300728 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200729 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300730 /* requeue check task with new expire */
731 task_queue(s->check.task);
732 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100733 }
734}
735
Willy Tarreaua1dab552014-04-14 15:04:54 +0200736static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100737{
738 int sv_state;
739 int ratio;
740 int hlen = 0;
741 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
742 "UP %d/%d", "UP",
743 "NOLB %d/%d", "NOLB",
744 "no check" };
745
746 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
747 hlen += 24;
748
Willy Tarreauff5ae352013-12-11 20:36:34 +0100749 if (!(s->check.state & CHK_ST_ENABLED))
750 sv_state = 6;
Willy Tarreauef781042010-01-27 11:53:01 +0100751 else if (s->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +0900752 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100753 sv_state = 3; /* UP */
754 else
755 sv_state = 2; /* going down */
756
757 if (s->state & SRV_GOINGDOWN)
758 sv_state += 2;
759 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900760 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100761 sv_state = 1; /* going up */
762 else
763 sv_state = 0; /* DOWN */
764 }
765
Willy Tarreaua1dab552014-04-14 15:04:54 +0200766 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100767 srv_hlt_st[sv_state],
Simon Horman58c32972013-11-25 10:46:38 +0900768 (s->state & SRV_RUNNING) ? (s->check.health - s->check.rise + 1) : (s->check.health),
769 (s->state & SRV_RUNNING) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100770
Willy Tarreaua1dab552014-04-14 15:04:54 +0200771 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 +0100772 s->proxy->id, s->id,
773 global.node,
774 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
775 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
776 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
777 s->nbpend);
778
779 if ((s->state & SRV_WARMINGUP) &&
780 now.tv_sec < s->last_change + s->slowstart &&
781 now.tv_sec >= s->last_change) {
782 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200783 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100784 }
785
786 buffer[hlen++] = '\r';
787 buffer[hlen++] = '\n';
788
789 return hlen;
790}
791
Willy Tarreau20a18342013-12-05 00:31:46 +0100792/* Check the connection. If an error has already been reported or the socket is
793 * closed, keep errno intact as it is supposed to contain the valid error code.
794 * If no error is reported, check the socket's error queue using getsockopt().
795 * Warning, this must be done only once when returning from poll, and never
796 * after an I/O error was attempted, otherwise the error queue might contain
797 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
798 * socket. Returns non-zero if an error was reported, zero if everything is
799 * clean (including a properly closed socket).
800 */
801static int retrieve_errno_from_socket(struct connection *conn)
802{
803 int skerr;
804 socklen_t lskerr = sizeof(skerr);
805
806 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
807 return 1;
808
Willy Tarreau3c728722014-01-23 13:50:42 +0100809 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100810 return 0;
811
812 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
813 errno = skerr;
814
815 if (errno == EAGAIN)
816 errno = 0;
817
818 if (!errno) {
819 /* we could not retrieve an error, that does not mean there is
820 * none. Just don't change anything and only report the prior
821 * error if any.
822 */
823 if (conn->flags & CO_FL_ERROR)
824 return 1;
825 else
826 return 0;
827 }
828
829 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
830 return 1;
831}
832
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100833/* Try to collect as much information as possible on the connection status,
834 * and adjust the server status accordingly. It may make use of <errno_bck>
835 * if non-null when the caller is absolutely certain of its validity (eg:
836 * checked just after a syscall). If the caller doesn't have a valid errno,
837 * it can pass zero, and retrieve_errno_from_socket() will be called to try
838 * to extract errno from the socket. If no error is reported, it will consider
839 * the <expired> flag. This is intended to be used when a connection error was
840 * reported in conn->flags or when a timeout was reported in <expired>. The
841 * function takes care of not updating a server status which was already set.
842 * All situations where at least one of <expired> or CO_FL_ERROR are set
843 * produce a status.
844 */
845static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
846{
847 struct check *check = conn->owner;
848 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200849 struct chunk *chk;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100850
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100851 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100852 return;
853
854 errno = errno_bck;
855 if (!errno || errno == EAGAIN)
856 retrieve_errno_from_socket(conn);
857
858 if (!(conn->flags & CO_FL_ERROR) && !expired)
859 return;
860
861 /* we'll try to build a meaningful error message depending on the
862 * context of the error possibly present in conn->err_code, and the
863 * socket error possibly collected above. This is useful to know the
864 * exact step of the L6 layer (eg: SSL handshake).
865 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200866 chk = get_trash_chunk();
867
868 if (check->type == PR_O2_TCPCHK_CHK) {
869 chunk_printf(chk, " at step %d of tcp-check", tcpcheck_get_step_id(check->server));
870 /* we were looking for a string */
Baptiste Assmann69e273f2013-12-11 00:52:19 +0100871 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
872 chunk_appendf(chk, " (connect)");
873 }
874 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200875 if (check->current_step->string)
876 chunk_appendf(chk, " (string '%s')", check->current_step->string);
877 else if (check->current_step->expect_regex)
878 chunk_appendf(chk, " (expect regex)");
879 }
880 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
881 chunk_appendf(chk, " (send)");
882 }
883 }
884
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100885 if (conn->err_code) {
886 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200887 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100888 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200889 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100890 err_msg = trash.str;
891 }
892 else {
893 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200894 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100895 err_msg = trash.str;
896 }
897 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200898 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100899 }
900 }
901
902 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
903 /* L4 not established (yet) */
904 if (conn->flags & CO_FL_ERROR)
905 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
906 else if (expired)
907 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
908 }
909 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
910 /* L6 not established (yet) */
911 if (conn->flags & CO_FL_ERROR)
912 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
913 else if (expired)
914 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
915 }
916 else if (conn->flags & CO_FL_ERROR) {
917 /* I/O error after connection was established and before we could diagnose */
918 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
919 }
920 else if (expired) {
921 /* connection established but expired check */
922 if (check->type == PR_O2_SSL3_CHK)
923 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
924 else /* HTTP, SMTP, ... */
925 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
926 }
927
928 return;
929}
930
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931/*
932 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200933 * the connection acknowledgement. If the proxy requires L7 health-checks,
934 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900935 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200937static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938{
Simon Horman4a741432013-02-23 15:35:38 +0900939 struct check *check = conn->owner;
940 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900941 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200942
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100943 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100944 goto out_wakeup;
945
Willy Tarreau310987a2014-01-22 19:46:33 +0100946 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100947 return;
948
Willy Tarreau20a18342013-12-05 00:31:46 +0100949 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100950 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100951 __conn_data_stop_both(conn);
952 goto out_wakeup;
953 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100954
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100955 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100956 /* if the output is closed, we can't do anything */
957 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100958 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100959 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200960 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200961
Willy Tarreau06559ac2013-12-05 01:53:08 +0100962 /* here, we know that the connection is established. That's enough for
963 * a pure TCP check.
964 */
965 if (!check->type)
966 goto out_wakeup;
967
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200968 if (check->type == PR_O2_TCPCHK_CHK) {
969 tcpcheck_main(conn);
970 return;
971 }
972
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100973 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100974 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100975 if (conn->flags & CO_FL_ERROR) {
976 chk_report_conn_err(conn, errno, 0);
977 __conn_data_stop_both(conn);
978 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200979 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100980 if (check->bo->o)
981 return;
982 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200983
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100984 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
985 if (s->proxy->timeout.check) {
986 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
987 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200988 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100989 goto out_nowake;
990
Willy Tarreau83749182007-04-15 20:56:27 +0200991 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200992 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200993 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200994 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200995}
996
Willy Tarreaubaaee002006-06-26 02:48:02 +0200997/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200998 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200999 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +09001000 * set_server_check_status() to update check->status, check->duration
1001 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001002
1003 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
1004 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
1005 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
1006 * response to an SSL HELLO (the principle is that this is enough to
1007 * distinguish between an SSL server and a pure TCP relay). All other cases will
1008 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
1009 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001010 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001011static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012{
Simon Horman4a741432013-02-23 15:35:38 +09001013 struct check *check = conn->owner;
1014 struct server *s = check->server;
1015 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001016 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +01001017 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001018 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +02001019
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001020 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +02001021 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +02001022
Willy Tarreau310987a2014-01-22 19:46:33 +01001023 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001024 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001025
1026 if (check->type == PR_O2_TCPCHK_CHK) {
1027 tcpcheck_main(conn);
1028 return;
1029 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001030
Willy Tarreau83749182007-04-15 20:56:27 +02001031 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
1032 * but the connection was closed on the remote end. Fortunately, recv still
1033 * works correctly and we don't need to do the getsockopt() on linux.
1034 */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001035
1036 /* Set buffer to point to the end of the data already read, and check
1037 * that there is free space remaining. If the buffer is full, proceed
1038 * with running the checks without attempting another socket read.
1039 */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001040
Willy Tarreau03938182010-03-17 21:52:07 +01001041 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001042
Simon Horman4a741432013-02-23 15:35:38 +09001043 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +02001044 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +01001045 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +09001046 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +02001047 /* Report network errors only if we got no other data. Otherwise
1048 * we'll let the upper layers decide whether the response is OK
1049 * or not. It is very common that an RST sent by the server is
1050 * reported as an error just after the last data chunk.
1051 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001052 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +01001053 goto out_wakeup;
1054 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001055 }
1056
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001057
Willy Tarreau03938182010-03-17 21:52:07 +01001058 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +09001059 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +01001060 */
Simon Horman4a741432013-02-23 15:35:38 +09001061 if (check->bi->i < check->bi->size)
1062 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001063 else {
Simon Horman4a741432013-02-23 15:35:38 +09001064 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001065 done = 1; /* buffer full, don't wait for more data */
1066 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001067
Nick Chalk57b1bf72010-03-16 15:50:46 +00001068 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +09001069 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001070 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001071 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001072 goto wait_more_data;
1073
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001074 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +09001075 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
1076 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
1077 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
1078 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
1079 !isdigit((unsigned char) *(check->bi->data + 11))) {
1080 cut_crlf(check->bi->data);
1081 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001082
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001083 goto out_wakeup;
1084 }
1085
Simon Horman4a741432013-02-23 15:35:38 +09001086 check->code = str2uic(check->bi->data + 9);
1087 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +00001088
Willy Tarreaubd741542010-03-16 18:46:54 +01001089 if ((s->proxy->options & PR_O_DISABLE404) &&
Simon Horman4a741432013-02-23 15:35:38 +09001090 (s->state & SRV_RUNNING) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +00001091 /* 404 may be accepted as "stopping" only if the server was up */
1092 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001093 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001094 }
Willy Tarreaubd741542010-03-16 18:46:54 +01001095 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
1096 /* Run content verification check... We know we have at least 13 chars */
1097 if (!httpchk_expect(s, done))
1098 goto wait_more_data;
1099 }
1100 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +09001101 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +01001102 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001103 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +01001104 }
Nick Chalk57b1bf72010-03-16 15:50:46 +00001105 else {
1106 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001107 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001108 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001109 break;
1110
1111 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001112 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001113 goto wait_more_data;
1114
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001115 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +09001116 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
1117 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001118 else
Simon Horman4a741432013-02-23 15:35:38 +09001119 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001120 break;
1121
1122 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001123 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001124 goto wait_more_data;
1125
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001126 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +09001127 if ((check->bi->i < strlen("000\r")) ||
1128 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
1129 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
1130 !isdigit((unsigned char) *(check->bi->data + 2))) {
1131 cut_crlf(check->bi->data);
1132 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001133
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001134 goto out_wakeup;
1135 }
1136
Simon Horman4a741432013-02-23 15:35:38 +09001137 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001138
Simon Horman4a741432013-02-23 15:35:38 +09001139 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001140 cut_crlf(desc);
1141
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001142 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +09001143 if (*check->bi->data == '2')
1144 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001145 else
Simon Horman4a741432013-02-23 15:35:38 +09001146 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001147 break;
1148
Simon Hormana2b9dad2013-02-12 10:45:54 +09001149 case PR_O2_LB_AGENT_CHK: {
1150 short status = HCHK_STATUS_L7RSP;
1151 const char *desc = "Unknown feedback string";
1152 const char *down_cmd = NULL;
Simon Horman671b6f02013-11-25 10:46:39 +09001153 int disabled;
Willy Tarreau9809b782013-12-11 21:40:11 +01001154 char *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001155
Willy Tarreau9809b782013-12-11 21:40:11 +01001156 /* get a complete line first */
1157 p = check->bi->data;
1158 while (*p && *p != '\n' && *p != '\r')
1159 p++;
1160
1161 if (!*p) {
1162 if (!done)
1163 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001164
Willy Tarreau9809b782013-12-11 21:40:11 +01001165 /* at least inform the admin that the agent is mis-behaving */
1166 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
1167 break;
1168 }
1169 *p = 0;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001170
Simon Horman671b6f02013-11-25 10:46:39 +09001171 /*
1172 * The agent may have been disabled after a check was
1173 * initialised. If so, ignore weight changes and drain
1174 * settings from the agent. Note that the setting is
1175 * always present in the state of the agent the server,
1176 * regardless of if the agent is being run as a primary or
1177 * secondary check. That is, regardless of if the check
1178 * parameter of this function is the agent or check field
1179 * of the server.
1180 */
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001181 disabled = !(check->server->agent.state & CHK_ST_ENABLED);
Simon Horman671b6f02013-11-25 10:46:39 +09001182
Simon Horman4a741432013-02-23 15:35:38 +09001183 if (strchr(check->bi->data, '%')) {
Simon Horman671b6f02013-11-25 10:46:39 +09001184 if (disabled)
1185 break;
Simon Horman4a741432013-02-23 15:35:38 +09001186 desc = server_parse_weight_change_request(s, check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001187 if (!desc) {
1188 status = HCHK_STATUS_L7OKD;
Simon Horman4a741432013-02-23 15:35:38 +09001189 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001190 }
Simon Horman4a741432013-02-23 15:35:38 +09001191 } else if (!strcasecmp(check->bi->data, "drain")) {
Simon Horman671b6f02013-11-25 10:46:39 +09001192 if (disabled)
1193 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001194 desc = server_parse_weight_change_request(s, "0%");
1195 if (!desc) {
1196 desc = "drain";
1197 status = HCHK_STATUS_L7OKD;
1198 }
Simon Horman4a741432013-02-23 15:35:38 +09001199 } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001200 down_cmd = "down";
Simon Horman4a741432013-02-23 15:35:38 +09001201 } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001202 down_cmd = "stopped";
Simon Horman4a741432013-02-23 15:35:38 +09001203 } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001204 down_cmd = "fail";
1205 }
1206
1207 if (down_cmd) {
Simon Horman4a741432013-02-23 15:35:38 +09001208 const char *end = check->bi->data + strlen(down_cmd);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001209 /*
1210 * The command keyword must terminated the string or
1211 * be followed by a blank.
1212 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001213 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001214 status = HCHK_STATUS_L7STS;
Simon Horman80fefae2013-11-25 10:46:34 +09001215 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001216 }
1217 }
1218
Simon Horman4a741432013-02-23 15:35:38 +09001219 set_server_check_status(check, status, desc);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001220 set_server_drain_state(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001221 break;
1222 }
1223
Willy Tarreau1620ec32011-08-06 17:05:02 +02001224 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001225 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001226 goto wait_more_data;
1227
Simon Horman4a741432013-02-23 15:35:38 +09001228 if (check->bi->data[0] == 'R') {
1229 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001230 }
1231 else {
Simon Horman4a741432013-02-23 15:35:38 +09001232 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1233 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001234 else
1235 desc = "PostgreSQL unknown error";
1236
Simon Horman4a741432013-02-23 15:35:38 +09001237 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001238 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001239 break;
1240
1241 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001242 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001243 goto wait_more_data;
1244
Simon Horman4a741432013-02-23 15:35:38 +09001245 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1246 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001247 }
1248 else {
Simon Horman4a741432013-02-23 15:35:38 +09001249 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001250 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001251 break;
1252
1253 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001254 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001255 goto wait_more_data;
1256
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001257 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001258 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001259 /* We set the MySQL Version in description for information purpose
1260 * FIXME : it can be cool to use MySQL Version for other purpose,
1261 * like mark as down old MySQL server.
1262 */
Simon Horman4a741432013-02-23 15:35:38 +09001263 if (check->bi->i > 51) {
1264 desc = ltrim(check->bi->data + 5, ' ');
1265 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001266 }
1267 else {
1268 if (!done)
1269 goto wait_more_data;
1270 /* it seems we have a OK packet but without a valid length,
1271 * it must be a protocol error
1272 */
Simon Horman4a741432013-02-23 15:35:38 +09001273 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001274 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001275 }
1276 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001277 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001278 desc = ltrim(check->bi->data + 7, ' ');
1279 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001280 }
1281 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001282 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1283 (((unsigned int) *(check->bi->data + 1)) << 8) +
1284 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001285
Simon Horman4a741432013-02-23 15:35:38 +09001286 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001287 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001288 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001289 /* We have only one MySQL packet and it is a Handshake Initialization packet
1290 * but we need to have a second packet to know if it is alright
1291 */
Simon Horman4a741432013-02-23 15:35:38 +09001292 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001293 goto wait_more_data;
1294 }
1295 else {
1296 /* We have only one packet and it is an Error packet,
1297 * an error message is attached, so we can display it
1298 */
Simon Horman4a741432013-02-23 15:35:38 +09001299 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001300 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001301 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001302 }
Simon Horman4a741432013-02-23 15:35:38 +09001303 } else if (check->bi->i > first_packet_len + 4) {
1304 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1305 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1306 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001307
Simon Horman4a741432013-02-23 15:35:38 +09001308 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001309 /* We have 2 packets and that's good */
1310 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001311 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001312 /* No error packet */
1313 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001314 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001315 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001316 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001317 }
1318 else {
1319 /* An error message is attached in the Error packet
1320 * so we can display it ! :)
1321 */
Simon Horman4a741432013-02-23 15:35:38 +09001322 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001323 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001324 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001325 }
1326 }
1327 }
1328 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001329 if (!done)
1330 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001331 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001332 * it must be a protocol error
1333 */
Simon Horman4a741432013-02-23 15:35:38 +09001334 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001335 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001336 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001337 }
1338 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001339 break;
1340
1341 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001342 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001343 goto wait_more_data;
1344
1345 /* Check if the server speaks LDAP (ASN.1/BER)
1346 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1347 * http://tools.ietf.org/html/rfc4511
1348 */
1349
1350 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1351 * LDAPMessage: 0x30: SEQUENCE
1352 */
Simon Horman4a741432013-02-23 15:35:38 +09001353 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1354 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001355 }
1356 else {
1357 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001358 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001359
1360 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1361 * messageID: 0x02 0x01 0x01: INTEGER 1
1362 * protocolOp: 0x61: bindResponse
1363 */
1364 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001365 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1366 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001367
1368 goto out_wakeup;
1369 }
1370
1371 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001372 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001373
1374 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1375 * ldapResult: 0x0a 0x01: ENUMERATION
1376 */
1377 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001378 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1379 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001380
1381 goto out_wakeup;
1382 }
1383
1384 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1385 * resultCode
1386 */
Simon Horman4a741432013-02-23 15:35:38 +09001387 check->code = *(check->bi->data + msglen + 9);
1388 if (check->code) {
1389 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 +02001390 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001391 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001392 }
1393 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001394 break;
1395
1396 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001397 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001398 break;
1399 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001400
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001401 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001402 /* collect possible new errors */
1403 if (conn->flags & CO_FL_ERROR)
1404 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001405
Nick Chalk57b1bf72010-03-16 15:50:46 +00001406 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001407 *check->bi->data = '\0';
1408 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001409
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001410 /* Close the connection... We absolutely want to perform a hard close
1411 * and reset the connection if some data are pending, otherwise we end
1412 * up with many TIME_WAITs and eat all the source port range quickly.
1413 * To avoid sending RSTs all the time, we first try to drain pending
1414 * data.
1415 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001416 if (conn->xprt && conn->xprt->shutw)
1417 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001418
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001419 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001420 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001421 conn->flags |= CO_FL_ERROR;
1422
Willy Tarreaua522f802012-11-23 08:56:35 +01001423 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001424 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001425 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001426
1427 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001428 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001429}
1430
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001431/*
1432 * This function is used only for server health-checks. It handles connection
1433 * status updates including errors. If necessary, it wakes the check task up.
1434 * It always returns 0.
1435 */
1436static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001437{
Simon Horman4a741432013-02-23 15:35:38 +09001438 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001439
Willy Tarreau6c560da2012-11-24 11:14:45 +01001440 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001441 /* We may get error reports bypassing the I/O handlers, typically
1442 * the case when sending a pure TCP check which fails, then the I/O
1443 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001444 * main processing task so let's simply wake it up. If we get here,
1445 * we expect errno to still be valid.
1446 */
1447 chk_report_conn_err(conn, errno, 0);
1448
Willy Tarreau2d351b62013-12-05 02:36:25 +01001449 __conn_data_stop_both(conn);
1450 task_wakeup(check->task, TASK_WOKEN_IO);
1451 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001452 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1453 /* we may get here if only a connection probe was required : we
1454 * don't have any data to send nor anything expected in response,
1455 * so the completion of the connection establishment is enough.
1456 */
1457 task_wakeup(check->task, TASK_WOKEN_IO);
1458 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001459
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001460 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001461 /* We're here because nobody wants to handle the error, so we
1462 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001463 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001464 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001465 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001466 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001467 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001468}
1469
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001470struct data_cb check_conn_cb = {
1471 .recv = event_srv_chk_r,
1472 .send = event_srv_chk_w,
1473 .wake = wake_srv_chk,
1474};
1475
Willy Tarreaubaaee002006-06-26 02:48:02 +02001476/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001477 * updates the server's weight during a warmup stage. Once the final weight is
1478 * reached, the task automatically stops. Note that any server status change
1479 * must have updated s->last_change accordingly.
1480 */
1481static struct task *server_warmup(struct task *t)
1482{
1483 struct server *s = t->context;
1484
1485 /* by default, plan on stopping the task */
1486 t->expire = TICK_ETERNITY;
1487 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1488 return t;
1489
Willy Tarreau004e0452013-11-21 11:22:01 +01001490 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001491
1492 /* probably that we can refill this server with a bit more connections */
1493 check_for_pending(s);
1494
1495 /* get back there in 1 second or 1/20th of the slowstart interval,
1496 * whichever is greater, resulting in small 5% steps.
1497 */
1498 if (s->state & SRV_WARMINGUP)
1499 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1500 return t;
1501}
1502
1503/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001504 * manages a server health-check. Returns
1505 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1506 */
Simon Horman63a4a822012-03-19 07:24:41 +09001507static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001508{
Simon Horman4a741432013-02-23 15:35:38 +09001509 struct check *check = t->context;
1510 struct server *s = check->server;
1511 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001512 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001513 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001514 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001515
Willy Tarreau2c115e52013-12-11 19:41:16 +01001516 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001517 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001518 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001519 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001520
Simon Horman671b6f02013-11-25 10:46:39 +09001521 /* we don't send any health-checks when the proxy is
1522 * stopped, the server should not be checked or the check
1523 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001524 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001525 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01001526 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01001527 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001528
1529 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001530 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001531
Willy Tarreau2c115e52013-12-11 19:41:16 +01001532 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09001533 check->bi->p = check->bi->data;
1534 check->bi->i = 0;
1535 check->bo->p = check->bo->data;
1536 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001537
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001538 /* tcpcheck send/expect initialisation */
1539 if (check->type == PR_O2_TCPCHK_CHK)
1540 check->current_step = NULL;
1541
1542 /* prepare the check buffer.
1543 * This should not be used if check is the secondary agent check
1544 * of a server as s->proxy->check_req will relate to the
1545 * configuration of the primary check. Similarly, tcp-check uses
1546 * its own strings.
1547 */
Willy Tarreau33434322013-12-11 21:15:19 +01001548 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
Simon Horman4a741432013-02-23 15:35:38 +09001549 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001550
1551 /* we want to check if this host replies to HTTP or SSLv3 requests
1552 * so we'll send the request, and won't wake the checker up now.
1553 */
Simon Horman4a741432013-02-23 15:35:38 +09001554 if ((check->type) == PR_O2_SSL3_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001555 /* SSL requires that we put Unix time in the request */
1556 int gmt_time = htonl(date.tv_sec);
Simon Horman4a741432013-02-23 15:35:38 +09001557 memcpy(check->bo->data + 11, &gmt_time, 4);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001558 }
Simon Horman4a741432013-02-23 15:35:38 +09001559 else if ((check->type) == PR_O2_HTTP_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001560 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Willy Tarreaua1dab552014-04-14 15:04:54 +02001561 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Simon Horman4a741432013-02-23 15:35:38 +09001562 bo_putstr(check->bo, "\r\n");
1563 *check->bo->p = '\0'; /* to make gdb output easier to read */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001564 }
1565 }
1566
1567 /* prepare a new connection */
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001568 conn_init(conn);
Willy Tarreau910c6aa2013-10-24 15:08:37 +02001569 conn_prepare(conn, s->check_common.proto, s->check_common.xprt);
Willy Tarreau7abddb52013-10-24 15:31:04 +02001570 conn_attach(conn, check, &check_conn_cb);
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001571 conn->target = &s->obj_type;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001572
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001573 /* no client address */
1574 clear_addr(&conn->addr.from);
1575
Simon Horman66183002013-02-23 10:16:43 +09001576 if (is_addr(&s->check_common.addr))
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001577 /* we'll connect to the check addr specified on the server */
Simon Horman66183002013-02-23 10:16:43 +09001578 conn->addr.to = s->check_common.addr;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001579 else
1580 /* we'll connect to the addr on the server */
1581 conn->addr.to = s->addr;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001582
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001583 if (check->port) {
1584 set_host_port(&conn->addr.to, check->port);
1585 }
1586
1587 if (check->type == PR_O2_TCPCHK_CHK) {
Baptiste Assmannf621bea2014-02-03 22:38:15 +01001588 struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n;
1589 /* if first step is a 'connect', then tcpcheck_main must run it */
1590 if (r->action == TCPCHK_ACT_CONNECT) {
1591 tcpcheck_main(conn);
1592 return t;
1593 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001594 }
1595
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001596
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001597 /* It can return one of :
1598 * - SN_ERR_NONE if everything's OK
1599 * - SN_ERR_SRVTO if there are no more servers
1600 * - SN_ERR_SRVCL if the connection was refused by the server
1601 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1602 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1603 * - SN_ERR_INTERNAL for any other purely internal errors
1604 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001605 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001606 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001607 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001608 ret = SN_ERR_INTERNAL;
Simon Horman66183002013-02-23 10:16:43 +09001609 if (s->check_common.proto->connect)
Willy Tarreau57cd3e42013-10-24 22:01:26 +02001610 ret = s->check_common.proto->connect(conn, check->type, (check->type) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001611 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau57cd3e42013-10-24 22:01:26 +02001612 if (s->check.send_proxy) {
1613 conn->send_proxy_ofs = 1;
1614 conn->flags |= CO_FL_SEND_PROXY;
1615 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001616
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001617 switch (ret) {
1618 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001619 /* we allow up to min(inter, timeout.connect) for a connection
1620 * to establish but only when timeout.check is set
1621 * as it may be to short for a full check otherwise
1622 */
Simon Horman4a741432013-02-23 15:35:38 +09001623 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001624
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001625 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1626 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1627 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001628 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001629
1630 if (check->type)
1631 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1632
Willy Tarreau5a78f362012-11-23 12:47:05 +01001633 goto reschedule;
1634
1635 case SN_ERR_SRVTO: /* ETIMEDOUT */
1636 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001637 conn->flags |= CO_FL_ERROR;
1638 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001639 break;
1640 case SN_ERR_PRXCOND:
1641 case SN_ERR_RESOURCE:
1642 case SN_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01001643 conn->flags |= CO_FL_ERROR;
1644 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001645 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001646 }
1647
Willy Tarreau5a78f362012-11-23 12:47:05 +01001648 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001649
Willy Tarreau2c115e52013-12-11 19:41:16 +01001650 check->state &= ~CHK_ST_INPROGRESS;
Simon Hormand8583062013-11-25 10:46:33 +09001651 check_failed(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001652
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001653 /* we allow up to min(inter, timeout.connect) for a connection
1654 * to establish but only when timeout.check is set
1655 * as it may be to short for a full check otherwise
1656 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001657 while (tick_is_expired(t->expire, now_ms)) {
1658 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001659
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001660 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001661 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001662
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001663 if (s->proxy->timeout.check)
1664 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001665 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001666 }
1667 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001668 /* there was a test running.
1669 * First, let's check whether there was an uncaught error,
1670 * which can happen on connect timeout or error.
1671 */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001672 if (s->check.result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001673 /* good connection is enough for pure TCP check */
1674 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001675 if (check->use_ssl)
1676 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001677 else
Simon Horman4a741432013-02-23 15:35:38 +09001678 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001679 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001680 else if ((conn->flags & CO_FL_ERROR) || expired) {
1681 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001682 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001683 else
1684 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001685 }
1686
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001687 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001688 if (conn->xprt) {
1689 /* The check was aborted and the connection was not yet closed.
1690 * This can happen upon timeout, or when an external event such
1691 * as a failed response coupled with "observe layer7" caused the
1692 * server state to be suddenly changed.
1693 */
Willy Tarreau46be2e52014-01-20 12:10:52 +01001694 conn_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001695 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001696 }
1697
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001698 if (check->result == CHK_RES_FAILED) /* a failure or timeout detected */
Simon Hormand8583062013-11-25 10:46:33 +09001699 check_failed(check);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001700 else { /* check was OK */
Willy Tarreau48494c02007-11-30 10:41:39 +01001701 /* we may have to add/remove this server from the LB group */
1702 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001703 if ((s->state & SRV_GOINGDOWN) && (check->result != CHK_RES_CONDPASS))
Simon Horman4a741432013-02-23 15:35:38 +09001704 set_server_enabled(check);
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001705 else if (!(s->state & SRV_GOINGDOWN) && (check->result == CHK_RES_CONDPASS))
Simon Horman4a741432013-02-23 15:35:38 +09001706 set_server_disabled(check);
Willy Tarreau48494c02007-11-30 10:41:39 +01001707 }
1708
Willy Tarreau9f708ab2013-12-23 14:04:17 +01001709 if (!(s->state & SRV_MAINTAIN) &&
1710 check->health < check->rise + check->fall - 1) {
Simon Horman125d0992013-02-24 17:23:38 +09001711 check->health++; /* was bad, stays for a while */
Simon Horman4a741432013-02-23 15:35:38 +09001712 set_server_up(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001713 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001714 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01001715 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001716
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001717 rv = 0;
1718 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001719 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001720 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001721 }
Simon Horman4a741432013-02-23 15:35:38 +09001722 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001723 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001724
1725 reschedule:
1726 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09001727 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001728 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001729 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001730}
1731
Simon Horman5c942422013-11-25 10:46:32 +09001732static int start_check_task(struct check *check, int mininter,
1733 int nbcheck, int srvpos)
1734{
1735 struct task *t;
1736 /* task for the check */
1737 if ((t = task_new()) == NULL) {
1738 Alert("Starting [%s:%s] check: out of memory.\n",
1739 check->server->proxy->id, check->server->id);
1740 return 0;
1741 }
1742
1743 check->task = t;
1744 t->process = process_chk;
1745 t->context = check;
1746
Willy Tarreau1746eec2014-04-25 10:46:47 +02001747 if (mininter < srv_getinter(check))
1748 mininter = srv_getinter(check);
1749
1750 if (global.max_spread_checks && mininter > global.max_spread_checks)
1751 mininter = global.max_spread_checks;
1752
Simon Horman5c942422013-11-25 10:46:32 +09001753 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02001754 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09001755 check->start = now;
1756 task_queue(t);
1757
1758 return 1;
1759}
1760
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001761/*
1762 * Start health-check.
1763 * Returns 0 if OK, -1 if error, and prints the error in this case.
1764 */
1765int start_checks() {
1766
1767 struct proxy *px;
1768 struct server *s;
1769 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09001770 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001771
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001772 /* 1- count the checkers to run simultaneously.
1773 * We also determine the minimum interval among all of those which
1774 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1775 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001776 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001777 * too short an interval for all others.
1778 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001779 for (px = proxy; px; px = px->next) {
1780 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01001781 if (s->slowstart) {
1782 if ((t = task_new()) == NULL) {
1783 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1784 return -1;
1785 }
1786 /* We need a warmup task that will be called when the server
1787 * state switches from down to up.
1788 */
1789 s->warmup = t;
1790 t->process = server_warmup;
1791 t->context = s;
1792 t->expire = TICK_ETERNITY;
1793 }
1794
Willy Tarreaud8514a22013-12-11 21:10:14 +01001795 if (s->check.state & CHK_ST_CONFIGURED) {
1796 nbcheck++;
1797 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1798 (!mininter || mininter > srv_getinter(&s->check)))
1799 mininter = srv_getinter(&s->check);
1800 }
Willy Tarreau15f39102013-12-11 20:41:18 +01001801
Willy Tarreaud8514a22013-12-11 21:10:14 +01001802 if (s->agent.state & CHK_ST_CONFIGURED) {
1803 nbcheck++;
1804 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1805 (!mininter || mininter > srv_getinter(&s->agent)))
1806 mininter = srv_getinter(&s->agent);
1807 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001808 }
1809 }
1810
Simon Horman4a741432013-02-23 15:35:38 +09001811 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001812 return 0;
1813
1814 srand((unsigned)time(NULL));
1815
1816 /*
1817 * 2- start them as far as possible from each others. For this, we will
1818 * start them after their interval set to the min interval divided by
1819 * the number of servers, weighted by the server's position in the list.
1820 */
1821 for (px = proxy; px; px = px->next) {
1822 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09001823 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01001824 if (s->check.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09001825 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1826 return -1;
1827 srvpos++;
1828 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001829
Simon Hormand60d6912013-11-25 10:46:36 +09001830 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01001831 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09001832 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1833 return -1;
1834 }
1835 srvpos++;
1836 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001837 }
1838 }
1839 return 0;
1840}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001841
1842/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001843 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001844 * The buffer MUST be terminated by a null byte before calling this function.
1845 * Sets server status appropriately. The caller is responsible for ensuring
1846 * that the buffer contains at least 13 characters. If <done> is zero, we may
1847 * return 0 to indicate that data is required to decide of a match.
1848 */
1849static int httpchk_expect(struct server *s, int done)
1850{
1851 static char status_msg[] = "HTTP status check returned code <000>";
1852 char status_code[] = "000";
1853 char *contentptr;
1854 int crlf;
1855 int ret;
1856
1857 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1858 case PR_O2_EXP_STS:
1859 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001860 memcpy(status_code, s->check.bi->data + 9, 3);
1861 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001862
1863 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1864 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1865 else
1866 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1867
1868 /* we necessarily have the response, so there are no partial failures */
1869 if (s->proxy->options2 & PR_O2_EXP_INV)
1870 ret = !ret;
1871
Simon Horman4a741432013-02-23 15:35:38 +09001872 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01001873 break;
1874
1875 case PR_O2_EXP_STR:
1876 case PR_O2_EXP_RSTR:
1877 /* very simple response parser: ignore CR and only count consecutive LFs,
1878 * stop with contentptr pointing to first char after the double CRLF or
1879 * to '\0' if crlf < 2.
1880 */
1881 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001882 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001883 if (crlf >= 2)
1884 break;
1885 if (*contentptr == '\r')
1886 continue;
1887 else if (*contentptr == '\n')
1888 crlf++;
1889 else
1890 crlf = 0;
1891 }
1892
1893 /* Check that response contains a body... */
1894 if (crlf < 2) {
1895 if (!done)
1896 return 0;
1897
Simon Horman4a741432013-02-23 15:35:38 +09001898 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001899 "HTTP content check could not find a response body");
1900 return 1;
1901 }
1902
1903 /* Check that response body is not empty... */
1904 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001905 if (!done)
1906 return 0;
1907
Simon Horman4a741432013-02-23 15:35:38 +09001908 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001909 "HTTP content check found empty response body");
1910 return 1;
1911 }
1912
1913 /* Check the response content against the supplied string
1914 * or regex... */
1915 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1916 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1917 else
1918 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1919
1920 /* if we don't match, we may need to wait more */
1921 if (!ret && !done)
1922 return 0;
1923
1924 if (ret) {
1925 /* content matched */
1926 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001927 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001928 "HTTP check matched unwanted content");
1929 else
Simon Horman4a741432013-02-23 15:35:38 +09001930 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001931 "HTTP content check matched");
1932 }
1933 else {
1934 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001935 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001936 "HTTP check did not match unwanted content");
1937 else
Simon Horman4a741432013-02-23 15:35:38 +09001938 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001939 "HTTP content check did not match");
1940 }
1941 break;
1942 }
1943 return 1;
1944}
1945
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001946/*
1947 * return the id of a step in a send/expect session
1948 */
1949static int tcpcheck_get_step_id(struct server *s)
1950{
1951 struct tcpcheck_rule *cur = NULL, *next = NULL;
1952 int i = 0;
1953
Baptiste Assmann69e273f2013-12-11 00:52:19 +01001954 cur = s->check.last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001955
1956 /* no step => first step */
1957 if (cur == NULL)
1958 return 1;
1959
1960 /* increment i until current step */
1961 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
1962 if (next->list.p == &cur->list)
1963 break;
1964 ++i;
1965 }
1966
1967 return i;
1968}
1969
1970static void tcpcheck_main(struct connection *conn)
1971{
1972 char *contentptr;
1973 unsigned int contentlen;
1974 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
2078 if (is_addr(&s->check_common.addr))
2079 /* we'll connect to the check addr specified on the server */
2080 conn->addr.to = s->check_common.addr;
2081 else
2082 /* we'll connect to the addr on the server */
2083 conn->addr.to = s->addr;
2084
2085 /* protocol */
2086 proto = protocol_by_family(conn->addr.to.ss_family);
2087
2088 /* port */
2089 if (check->current_step->port)
2090 set_host_port(&conn->addr.to, check->current_step->port);
2091 else if (check->port)
2092 set_host_port(&conn->addr.to, check->port);
2093
2094#ifdef USE_OPENSSL
2095 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2096 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002097 }
2098 else {
2099 xprt = &raw_sock;
2100 }
2101#else /* USE_OPENSSL */
2102 xprt = &raw_sock;
2103#endif /* USE_OPENSSL */
2104 conn_prepare(conn, proto, xprt);
2105
2106 ret = SN_ERR_INTERNAL;
2107 if (proto->connect)
2108 ret = proto->connect(conn, check->type, (check->type) ? 0 : 2);
2109 conn->flags |= CO_FL_WAKE_DATA;
2110 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2111 conn->send_proxy_ofs = 1;
2112 conn->flags |= CO_FL_SEND_PROXY;
2113 }
2114
2115 /* It can return one of :
2116 * - SN_ERR_NONE if everything's OK
2117 * - SN_ERR_SRVTO if there are no more servers
2118 * - SN_ERR_SRVCL if the connection was refused by the server
2119 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2120 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2121 * - SN_ERR_INTERNAL for any other purely internal errors
2122 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
2123 * Note that we try to prevent the network stack from sending the ACK during the
2124 * connect() when a pure TCP check is used (without PROXY protocol).
2125 */
2126 switch (ret) {
2127 case SN_ERR_NONE:
2128 /* we allow up to min(inter, timeout.connect) for a connection
2129 * to establish but only when timeout.check is set
2130 * as it may be to short for a full check otherwise
2131 */
2132 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2133
2134 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2135 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2136 t->expire = tick_first(t->expire, t_con);
2137 }
2138 break;
2139 case SN_ERR_SRVTO: /* ETIMEDOUT */
2140 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
2141 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
2142 tcpcheck_get_step_id(s), strerror(errno));
2143 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2144 goto out_end_tcpcheck;
2145 case SN_ERR_PRXCOND:
2146 case SN_ERR_RESOURCE:
2147 case SN_ERR_INTERNAL:
2148 chunk_printf(&trash, "TCPCHK error establishing connection at step %d",
2149 tcpcheck_get_step_id(s));
2150 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2151 goto out_end_tcpcheck;
2152 }
2153
2154 /* allow next rule */
2155 cur = (struct tcpcheck_rule *)cur->list.n;
2156 check->current_step = cur;
2157
2158 /* don't do anything until the connection is established */
2159 if (!(conn->flags & CO_FL_CONNECTED)) {
2160 /* update expire time, should be done by process_chk */
2161 /* we allow up to min(inter, timeout.connect) for a connection
2162 * to establish but only when timeout.check is set
2163 * as it may be to short for a full check otherwise
2164 */
2165 while (tick_is_expired(t->expire, now_ms)) {
2166 int t_con;
2167
2168 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2169 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2170
2171 if (s->proxy->timeout.check)
2172 t->expire = tick_first(t->expire, t_con);
2173 }
2174 return;
2175 }
2176
2177 } /* end 'connect' */
2178 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2179 /* mark the step as started */
2180 check->last_started_step = check->current_step;
2181
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002182 /* reset the read buffer */
2183 if (*check->bi->data != '\0') {
2184 *check->bi->data = '\0';
2185 check->bi->i = 0;
2186 }
2187
2188 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2189 conn->flags |= CO_FL_ERROR;
2190 chk_report_conn_err(conn, 0, 0);
2191 goto out_end_tcpcheck;
2192 }
2193
Willy Tarreauabca5b62013-12-06 14:19:25 +01002194 if (check->current_step->string_len >= check->bo->size) {
2195 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2196 check->current_step->string_len, check->bo->size,
2197 tcpcheck_get_step_id(s));
2198 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2199 goto out_end_tcpcheck;
2200 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002201
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002202 /* do not try to send if there is no space */
2203 if (check->current_step->string_len >= buffer_total_space(check->bo))
2204 continue;
2205
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002206 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2207 *check->bo->p = '\0'; /* to make gdb output easier to read */
2208
Willy Tarreauabca5b62013-12-06 14:19:25 +01002209 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002210 cur = (struct tcpcheck_rule *)cur->list.n;
2211 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002212 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002213 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002214 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002215 goto out_end_tcpcheck;
2216
Willy Tarreau310987a2014-01-22 19:46:33 +01002217 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002218 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2219 done = 1;
2220 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2221 /* Report network errors only if we got no other data. Otherwise
2222 * we'll let the upper layers decide whether the response is OK
2223 * or not. It is very common that an RST sent by the server is
2224 * reported as an error just after the last data chunk.
2225 */
2226 chk_report_conn_err(conn, errno, 0);
2227 goto out_end_tcpcheck;
2228 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002229 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002230 else
2231 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002232 }
2233
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002234 /* mark the step as started */
2235 check->last_started_step = check->current_step;
2236
2237
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002238 /* Intermediate or complete response received.
2239 * Terminate string in check->bi->data buffer.
2240 */
2241 if (check->bi->i < check->bi->size) {
2242 check->bi->data[check->bi->i] = '\0';
2243 }
2244 else {
2245 check->bi->data[check->bi->i - 1] = '\0';
2246 done = 1; /* buffer full, don't wait for more data */
2247 }
2248
2249 contentptr = check->bi->data;
2250 contentlen = check->bi->i;
2251
2252 /* Check that response body is not empty... */
2253 if (*contentptr == '\0') {
2254 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)
2270 ret = my_memmem(contentptr, contentlen, cur->string, cur->string_len) != NULL;
2271 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 */