blob: 4af3db726178f54815b7e8d6098a0ec4a218a8fb [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
38#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020039#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090040#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/fd.h>
42#include <proto/log.h>
43#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020044#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010045#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010046#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010047#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020048#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090050#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010051#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/task.h>
53
Willy Tarreaubd741542010-03-16 18:46:54 +010054static int httpchk_expect(struct server *s, int done);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020055static int tcpcheck_get_step_id(struct server *);
56static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010057
Simon Horman63a4a822012-03-19 07:24:41 +090058static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020059 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
60 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
61 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020062
Willy Tarreaud3aac702012-11-23 11:32:12 +010063 [HCHK_STATUS_HANA] = { SRV_CHK_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010064
Willy Tarreaud3aac702012-11-23 11:32:12 +010065 [HCHK_STATUS_SOCKERR] = { SRV_CHK_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020066
Willy Tarreaud3aac702012-11-23 11:32:12 +010067 [HCHK_STATUS_L4OK] = { SRV_CHK_PASSED, "L4OK", "Layer4 check passed" },
68 [HCHK_STATUS_L4TOUT] = { SRV_CHK_FAILED, "L4TOUT", "Layer4 timeout" },
69 [HCHK_STATUS_L4CON] = { SRV_CHK_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020070
Willy Tarreaud3aac702012-11-23 11:32:12 +010071 [HCHK_STATUS_L6OK] = { SRV_CHK_PASSED, "L6OK", "Layer6 check passed" },
72 [HCHK_STATUS_L6TOUT] = { SRV_CHK_FAILED, "L6TOUT", "Layer6 timeout" },
73 [HCHK_STATUS_L6RSP] = { SRV_CHK_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020074
Willy Tarreaud3aac702012-11-23 11:32:12 +010075 [HCHK_STATUS_L7TOUT] = { SRV_CHK_FAILED, "L7TOUT", "Layer7 timeout" },
76 [HCHK_STATUS_L7RSP] = { SRV_CHK_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020077
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020078 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020079
Willy Tarreaud3aac702012-11-23 11:32:12 +010080 [HCHK_STATUS_L7OKD] = { SRV_CHK_PASSED, "L7OK", "Layer7 check passed" },
81 [HCHK_STATUS_L7OKCD] = { SRV_CHK_PASSED | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
82 [HCHK_STATUS_L7STS] = { SRV_CHK_FAILED, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020083};
84
Simon Horman63a4a822012-03-19 07:24:41 +090085static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010086 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
87
88 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
89 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
90
91 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
92 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
93 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
94 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
95
96 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
97 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
98 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
99};
100
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200101/*
102 * Convert check_status code to description
103 */
104const char *get_check_status_description(short check_status) {
105
106 const char *desc;
107
108 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200109 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200110 else
111 desc = NULL;
112
113 if (desc && *desc)
114 return desc;
115 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200116 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200117}
118
119/*
120 * Convert check_status code to short info
121 */
122const char *get_check_status_info(short check_status) {
123
124 const char *info;
125
126 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200127 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200128 else
129 info = NULL;
130
131 if (info && *info)
132 return info;
133 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200134 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200135}
136
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100137const char *get_analyze_status(short analyze_status) {
138
139 const char *desc;
140
141 if (analyze_status < HANA_STATUS_SIZE)
142 desc = analyze_statuses[analyze_status].desc;
143 else
144 desc = NULL;
145
146 if (desc && *desc)
147 return desc;
148 else
149 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
150}
151
Simon Horman4a741432013-02-23 15:35:38 +0900152static void server_status_printf(struct chunk *msg, struct server *s, struct check *check, int xferred) {
Willy Tarreau44267702011-10-28 15:35:33 +0200153 if (s->track)
Willy Tarreau77804732012-10-29 16:14:26 +0100154 chunk_appendf(msg, " via %s/%s",
Willy Tarreau44267702011-10-28 15:35:33 +0200155 s->track->proxy->id, s->track->id);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200156
Simon Horman4a741432013-02-23 15:35:38 +0900157 if (check) {
158 chunk_appendf(msg, ", reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200159
Simon Horman4a741432013-02-23 15:35:38 +0900160 if (check->status >= HCHK_STATUS_L57DATA)
161 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200162
Simon Horman4a741432013-02-23 15:35:38 +0900163 if (*check->desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200164 struct chunk src;
165
Willy Tarreau77804732012-10-29 16:14:26 +0100166 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200167
Simon Horman4a741432013-02-23 15:35:38 +0900168 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200169 chunk_asciiencode(msg, &src, '"');
170
Willy Tarreau77804732012-10-29 16:14:26 +0100171 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200172 }
173
Simon Horman4a741432013-02-23 15:35:38 +0900174 if (check->duration >= 0)
175 chunk_appendf(msg, ", check duration: %ldms", check->duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200176 }
177
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200178 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100179 if (!(s->state & SRV_RUNNING))
Willy Tarreau77804732012-10-29 16:14:26 +0100180 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100181 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200182 s->proxy->srv_act, s->proxy->srv_bck,
183 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
184 s->cur_sess, xferred, s->nbpend);
185 else
Willy Tarreau77804732012-10-29 16:14:26 +0100186 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100187 " %d sessions requeued, %d total 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 xferred, s->nbpend);
191 }
192}
193
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200194/*
Simon Horman4a741432013-02-23 15:35:38 +0900195 * Set check->status, update check->duration and fill check->result with
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200196 * an adequate SRV_CHK_* value.
197 *
198 * Show information in logs about failed health check if server is UP
199 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200200 */
Simon Horman4a741432013-02-23 15:35:38 +0900201static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100202{
Simon Horman4a741432013-02-23 15:35:38 +0900203 struct server *s = check->server;
204
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200205 if (status == HCHK_STATUS_START) {
Simon Horman4a741432013-02-23 15:35:38 +0900206 check->result = SRV_CHK_UNKNOWN; /* no result yet */
207 check->desc[0] = '\0';
208 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200209 return;
210 }
211
Simon Horman4a741432013-02-23 15:35:38 +0900212 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200213 return;
214
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200215 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900216 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
217 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200218 } else
Simon Horman4a741432013-02-23 15:35:38 +0900219 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200220
Simon Horman4a741432013-02-23 15:35:38 +0900221 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200222 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900223 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200224
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100225 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900226 check->duration = -1;
227 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200228 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900229 check->duration = tv_ms_elapsed(&check->start, &now);
230 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200231 }
232
Simon Horman2f1f9552013-11-25 10:46:37 +0900233 /* Failure to connect to the agent as a secondary check should not
234 * cause the server to be marked down. So only log status changes
235 * for HCHK_STATUS_* statuses */
236 if (check == &s->agent && check->status < HCHK_STATUS_L7TOUT)
237 return;
238
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200239 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
Simon Horman125d0992013-02-24 17:23:38 +0900240 (((check->health != 0) && (check->result & SRV_CHK_FAILED)) ||
Simon Horman8c3d0be2013-11-25 10:46:40 +0900241 (((check->health != check->rise + check->fall - 1) ||
242 (!s->uweight && !(s->state & SRV_DRAIN)) ||
243 (s->uweight && (s->state & SRV_DRAIN))) &&
244 (check->result & SRV_CHK_PASSED)) ||
Simon Horman4a741432013-02-23 15:35:38 +0900245 ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE)) ||
246 (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE)))) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200247
248 int health, rise, fall, state;
249
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100250 chunk_reset(&trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200251
252 /* FIXME begin: calculate local version of the health/rise/fall/state */
Simon Horman125d0992013-02-24 17:23:38 +0900253 health = check->health;
Simon Horman58c32972013-11-25 10:46:38 +0900254 rise = check->rise;
255 fall = check->fall;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200256 state = s->state;
257
Simon Horman4a741432013-02-23 15:35:38 +0900258 if (check->result & SRV_CHK_FAILED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200259 if (health > rise) {
260 health--; /* still good */
261 } else {
262 if (health == rise)
263 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
264
265 health = 0;
266 }
267 }
268
Simon Horman4a741432013-02-23 15:35:38 +0900269 if (check->result & SRV_CHK_PASSED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200270 if (health < rise + fall - 1) {
271 health++; /* was bad, stays for a while */
272
273 if (health == rise)
274 state |= SRV_RUNNING;
275
276 if (health >= rise)
277 health = rise + fall - 1; /* OK now */
278 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100279
280 /* clear consecutive_errors if observing is enabled */
281 if (s->onerror)
282 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200283 }
284 /* FIXME end: calculate local version of the health/rise/fall/state */
285
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100286 chunk_appendf(&trash,
287 "Health check for %sserver %s/%s %s%s",
288 s->state & SRV_BACKUP ? "backup " : "",
289 s->proxy->id, s->id,
Simon Horman4a741432013-02-23 15:35:38 +0900290 (check->result & SRV_CHK_DISABLE)?"conditionally ":"",
291 (check->result & SRV_CHK_PASSED)?"succeeded":"failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200292
Simon Horman4a741432013-02-23 15:35:38 +0900293 server_status_printf(&trash, s, check, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200294
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100295 chunk_appendf(&trash, ", status: %d/%d %s",
296 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
297 (state & SRV_RUNNING) ? (fall) : (rise),
Simon Horman8c3d0be2013-11-25 10:46:40 +0900298 (state & SRV_RUNNING)?(s->eweight?"UP":"DRAIN"):"DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200299
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100300 Warning("%s.\n", trash.str);
301 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200302 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200303}
304
Willy Tarreau48494c02007-11-30 10:41:39 +0100305/* sends a log message when a backend goes down, and also sets last
306 * change date.
307 */
308static void set_backend_down(struct proxy *be)
309{
310 be->last_change = now.tv_sec;
311 be->down_trans++;
312
313 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
314 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
315}
316
317/* Redistribute pending connections when a server goes down. The number of
318 * connections redistributed is returned.
319 */
320static int redistribute_pending(struct server *s)
321{
322 struct pendconn *pc, *pc_bck, *pc_end;
323 int xferred = 0;
324
325 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
326 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100327 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
328 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100329 /* The REDISP option was specified. We will ignore
330 * cookie and force to balance or use the dispatcher.
331 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100332
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100333 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100334 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100335
Willy Tarreau48494c02007-11-30 10:41:39 +0100336 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200337 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100338 xferred++;
339 }
340 }
341 return xferred;
342}
343
344/* Check for pending connections at the backend, and assign some of them to
345 * the server coming up. The server's weight is checked before being assigned
346 * connections it may not be able to handle. The total number of transferred
347 * connections is returned.
348 */
349static int check_for_pending(struct server *s)
350{
351 int xferred;
352
353 if (!s->eweight)
354 return 0;
355
356 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
357 struct session *sess;
358 struct pendconn *p;
359
360 p = pendconn_from_px(s->proxy);
361 if (!p)
362 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100363 p->sess->target = &s->obj_type;
Willy Tarreau48494c02007-11-30 10:41:39 +0100364 sess = p->sess;
365 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200366 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100367 }
368 return xferred;
369}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700371/* Shutdown all connections of a server. The caller must pass a termination
372 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
373 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900374 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700375static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900376{
377 struct session *session, *session_bck;
378
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200379 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
380 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700381 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900382}
383
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700384/* Shutdown all connections of all backup servers of a proxy. The caller must
385 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
386 * the reason for the shutdown.
387 */
388static void shutdown_backup_sessions(struct proxy *px, int why)
389{
390 struct server *srv;
391
392 for (srv = px->srv; srv != NULL; srv = srv->next)
393 if (srv->state & SRV_BACKUP)
394 shutdown_sessions(srv, why);
395}
396
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397/* Sets server <s> down, notifies by all available means, recounts the
398 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200399 * possible to other servers. It automatically recomputes the number of
400 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401 */
Simon Horman4a741432013-02-23 15:35:38 +0900402void set_server_down(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403{
Simon Horman4a741432013-02-23 15:35:38 +0900404 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100405 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 int xferred;
407
Cyril Bontécd19e512010-01-31 22:34:03 +0100408 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900409 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100410 }
411
Simon Horman58c32972013-11-25 10:46:38 +0900412 if ((s->state & SRV_RUNNING && check->health == check->rise) || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100413 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200414 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200415
416 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100417 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200418 if (s->proxy->lbprm.set_server_status_down)
419 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900421 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700422 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900423
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424 /* we might have sessions queued on this server and waiting for
425 * a connection. Those which are redispatchable will be queued
426 * to another server or to the proxy itself.
427 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100428 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100429
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100430 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100431
Cyril Bontécd19e512010-01-31 22:34:03 +0100432 if (s->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100433 chunk_appendf(&trash,
434 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
435 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100436 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100437 chunk_appendf(&trash,
438 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
439 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100440
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100441 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900442 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : 0),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100443 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100444 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100445 Warning("%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200446
Willy Tarreau48494c02007-11-30 10:41:39 +0100447 /* we don't send an alert if the server was previously paused */
448 if (srv_was_paused)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100449 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreau48494c02007-11-30 10:41:39 +0100450 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100451 send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200452
Willy Tarreaud64d2252010-10-17 17:16:42 +0200453 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100454 set_backend_down(s->proxy);
455
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200456 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100457
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100458 if (s->state & SRV_CHECKED)
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100459 for (srv = s->tracknext; srv; srv = srv->tracknext)
460 if (!(srv->state & SRV_MAINTAIN))
Cyril Bontécd19e512010-01-31 22:34:03 +0100461 /* Only notify tracking servers that are not already in maintenance. */
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100462 set_server_down(&srv->check);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100464
Simon Horman125d0992013-02-24 17:23:38 +0900465 check->health = 0; /* failure */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466}
467
Simon Horman4a741432013-02-23 15:35:38 +0900468void set_server_up(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100469
Simon Horman4a741432013-02-23 15:35:38 +0900470 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100471 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100472 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100473 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100474
Cyril Bontécd19e512010-01-31 22:34:03 +0100475 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900476 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100477 }
478
Simon Horman58c32972013-11-25 10:46:38 +0900479 if ((s->check.health >= s->check.rise && s->agent.health >= s->agent.rise &&
480 check->health == check->rise) || s->track) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100481 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
482 if (s->proxy->last_change < now.tv_sec) // ignore negative times
483 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
484 s->proxy->last_change = now.tv_sec;
485 }
486
487 if (s->last_change < now.tv_sec) // ignore negative times
488 s->down_time += now.tv_sec - s->last_change;
489
490 s->last_change = now.tv_sec;
491 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100492 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100493
494 if (s->slowstart > 0) {
495 s->state |= SRV_WARMINGUP;
Willy Tarreau2e993902011-10-31 11:53:20 +0100496 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100497 }
Willy Tarreau004e0452013-11-21 11:22:01 +0100498
499 server_recalc_eweight(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100500
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700501 /* If the server is set with "on-marked-up shutdown-backup-sessions",
502 * and it's not a backup server and its effective weight is > 0,
503 * then it can accept new connections, so we shut down all sessions
504 * on all backup servers.
505 */
506 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
507 !(s->state & SRV_BACKUP) && s->eweight)
508 shutdown_backup_sessions(s->proxy, SN_ERR_UP);
509
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100510 /* check if we can handle some connections queued at the proxy. We
511 * will take as many as we can handle.
512 */
513 xferred = check_for_pending(s);
514
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100515 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100516
Willy Tarreau45446782012-03-09 17:16:09 +0100517 if (old_state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100518 chunk_appendf(&trash,
519 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
520 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100521 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100522 chunk_appendf(&trash,
523 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
524 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100525
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100526 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900527 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100528 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100529 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100530
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100531 Warning("%s.\n", trash.str);
532 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100533
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100534 if (s->state & SRV_CHECKED)
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100535 for (srv = s->tracknext; srv; srv = srv->tracknext)
536 if (!(srv->state & SRV_MAINTAIN))
Cyril Bontécd19e512010-01-31 22:34:03 +0100537 /* Only notify tracking servers if they're not in maintenance. */
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100538 set_server_up(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100539 }
540
Simon Horman58c32972013-11-25 10:46:38 +0900541 if (check->health >= check->rise)
542 check->health = check->rise + check->fall - 1; /* OK now */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100543
544}
545
Simon Horman4a741432013-02-23 15:35:38 +0900546static void set_server_disabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100547
Simon Horman4a741432013-02-23 15:35:38 +0900548 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100549 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100550 int xferred;
551
552 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200553 if (s->proxy->lbprm.set_server_status_down)
554 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100555
556 /* we might have sessions queued on this server and waiting for
557 * a connection. Those which are redispatchable will be queued
558 * to another server or to the proxy itself.
559 */
560 xferred = redistribute_pending(s);
561
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100562 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100563
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100564 chunk_appendf(&trash,
565 "Load-balancing on %sServer %s/%s is disabled",
566 s->state & SRV_BACKUP ? "Backup " : "",
567 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100568
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100569 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900570 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100571 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100572
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100573 Warning("%s.\n", trash.str);
574 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100575
576 if (!s->proxy->srv_bck && !s->proxy->srv_act)
577 set_backend_down(s->proxy);
578
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100579 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100580 for(srv = s->tracknext; srv; srv = srv->tracknext)
Willy Tarreau81cf08c2013-11-28 11:27:16 +0100581 set_server_disabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100582}
583
Simon Horman4a741432013-02-23 15:35:38 +0900584static void set_server_enabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100585
Simon Horman4a741432013-02-23 15:35:38 +0900586 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100587 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100588 int xferred;
589
590 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200591 if (s->proxy->lbprm.set_server_status_up)
592 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100593
594 /* check if we can handle some connections queued at the proxy. We
595 * will take as many as we can handle.
596 */
597 xferred = check_for_pending(s);
598
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100599 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100600
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100601 chunk_appendf(&trash,
602 "Load-balancing on %sServer %s/%s is enabled again",
603 s->state & SRV_BACKUP ? "Backup " : "",
604 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100605
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100606 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900607 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100608 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100609
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100610 Warning("%s.\n", trash.str);
611 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100612
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100613 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100614 for(srv = s->tracknext; srv; srv = srv->tracknext)
Willy Tarreau81cf08c2013-11-28 11:27:16 +0100615 set_server_enabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100616}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617
Simon Hormand8583062013-11-25 10:46:33 +0900618static void check_failed(struct check *check)
619{
620 struct server *s = check->server;
621
Simon Horman2f1f9552013-11-25 10:46:37 +0900622 /* The agent secondary check should only cause a server to be marked
623 * as down if check->status is HCHK_STATUS_L7STS, which indicates
624 * that the agent returned "fail", "stopped" or "down".
625 * The implication here is that failure to connect to the agent
626 * as a secondary check should not cause the server to be marked
627 * down. */
628 if (check == &s->agent && check->status != HCHK_STATUS_L7STS)
629 return;
630
Simon Horman58c32972013-11-25 10:46:38 +0900631 if (check->health > check->rise) {
Simon Hormand8583062013-11-25 10:46:33 +0900632 check->health--; /* still good */
633 s->counters.failed_checks++;
634 }
635 else
636 set_server_down(check);
637}
638
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100639void health_adjust(struct server *s, short status)
640{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100641 int failed;
642 int expire;
643
644 /* return now if observing nor health check is not enabled */
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200645 if (!s->observe || !s->check.task)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100646 return;
647
648 if (s->observe >= HANA_OBS_SIZE)
649 return;
650
Willy Tarreaubb956662013-01-24 00:37:39 +0100651 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100652 return;
653
654 switch (analyze_statuses[status].lr[s->observe - 1]) {
655 case 1:
656 failed = 1;
657 break;
658
659 case 2:
660 failed = 0;
661 break;
662
663 default:
664 return;
665 }
666
667 if (!failed) {
668 /* good: clear consecutive_errors */
669 s->consecutive_errors = 0;
670 return;
671 }
672
673 s->consecutive_errors++;
674
675 if (s->consecutive_errors < s->consecutive_errors_limit)
676 return;
677
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100678 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
679 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100680
681 switch (s->onerror) {
682 case HANA_ONERR_FASTINTER:
683 /* force fastinter - nothing to do here as all modes force it */
684 break;
685
686 case HANA_ONERR_SUDDTH:
687 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900688 if (s->check.health > s->check.rise)
689 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100690
691 /* no break - fall through */
692
693 case HANA_ONERR_FAILCHK:
694 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900695 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Simon Hormand8583062013-11-25 10:46:33 +0900696 check_failed(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100697
698 break;
699
700 case HANA_ONERR_MARKDWN:
701 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900702 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900703 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
704 set_server_down(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100705
706 break;
707
708 default:
709 /* write a warning? */
710 break;
711 }
712
713 s->consecutive_errors = 0;
714 s->counters.failed_hana++;
715
Simon Horman66183002013-02-23 10:16:43 +0900716 if (s->check.fastinter) {
717 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300718 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200719 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300720 /* requeue check task with new expire */
721 task_queue(s->check.task);
722 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100723 }
724}
725
Willy Tarreauef781042010-01-27 11:53:01 +0100726static int httpchk_build_status_header(struct server *s, char *buffer)
727{
728 int sv_state;
729 int ratio;
730 int hlen = 0;
731 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
732 "UP %d/%d", "UP",
733 "NOLB %d/%d", "NOLB",
734 "no check" };
735
736 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
737 hlen += 24;
738
739 if (!(s->state & SRV_CHECKED))
740 sv_state = 6; /* should obviously never happen */
741 else if (s->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +0900742 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100743 sv_state = 3; /* UP */
744 else
745 sv_state = 2; /* going down */
746
747 if (s->state & SRV_GOINGDOWN)
748 sv_state += 2;
749 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900750 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100751 sv_state = 1; /* going up */
752 else
753 sv_state = 0; /* DOWN */
754 }
755
756 hlen += sprintf(buffer + hlen,
757 srv_hlt_st[sv_state],
Simon Horman58c32972013-11-25 10:46:38 +0900758 (s->state & SRV_RUNNING) ? (s->check.health - s->check.rise + 1) : (s->check.health),
759 (s->state & SRV_RUNNING) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100760
761 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
762 s->proxy->id, s->id,
763 global.node,
764 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
765 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
766 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
767 s->nbpend);
768
769 if ((s->state & SRV_WARMINGUP) &&
770 now.tv_sec < s->last_change + s->slowstart &&
771 now.tv_sec >= s->last_change) {
772 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
773 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
774 }
775
776 buffer[hlen++] = '\r';
777 buffer[hlen++] = '\n';
778
779 return hlen;
780}
781
Willy Tarreau20a18342013-12-05 00:31:46 +0100782/* Check the connection. If an error has already been reported or the socket is
783 * closed, keep errno intact as it is supposed to contain the valid error code.
784 * If no error is reported, check the socket's error queue using getsockopt().
785 * Warning, this must be done only once when returning from poll, and never
786 * after an I/O error was attempted, otherwise the error queue might contain
787 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
788 * socket. Returns non-zero if an error was reported, zero if everything is
789 * clean (including a properly closed socket).
790 */
791static int retrieve_errno_from_socket(struct connection *conn)
792{
793 int skerr;
794 socklen_t lskerr = sizeof(skerr);
795
796 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
797 return 1;
798
Willy Tarreauf79c8172013-10-21 16:30:56 +0200799 if (!(conn->flags & CO_FL_CTRL_READY) || !conn->ctrl)
Willy Tarreau20a18342013-12-05 00:31:46 +0100800 return 0;
801
802 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
803 errno = skerr;
804
805 if (errno == EAGAIN)
806 errno = 0;
807
808 if (!errno) {
809 /* we could not retrieve an error, that does not mean there is
810 * none. Just don't change anything and only report the prior
811 * error if any.
812 */
813 if (conn->flags & CO_FL_ERROR)
814 return 1;
815 else
816 return 0;
817 }
818
819 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
820 return 1;
821}
822
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100823/* Try to collect as much information as possible on the connection status,
824 * and adjust the server status accordingly. It may make use of <errno_bck>
825 * if non-null when the caller is absolutely certain of its validity (eg:
826 * checked just after a syscall). If the caller doesn't have a valid errno,
827 * it can pass zero, and retrieve_errno_from_socket() will be called to try
828 * to extract errno from the socket. If no error is reported, it will consider
829 * the <expired> flag. This is intended to be used when a connection error was
830 * reported in conn->flags or when a timeout was reported in <expired>. The
831 * function takes care of not updating a server status which was already set.
832 * All situations where at least one of <expired> or CO_FL_ERROR are set
833 * produce a status.
834 */
835static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
836{
837 struct check *check = conn->owner;
838 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200839 struct chunk *chk;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100840
841 if (check->result != SRV_CHK_UNKNOWN)
842 return;
843
844 errno = errno_bck;
845 if (!errno || errno == EAGAIN)
846 retrieve_errno_from_socket(conn);
847
848 if (!(conn->flags & CO_FL_ERROR) && !expired)
849 return;
850
851 /* we'll try to build a meaningful error message depending on the
852 * context of the error possibly present in conn->err_code, and the
853 * socket error possibly collected above. This is useful to know the
854 * exact step of the L6 layer (eg: SSL handshake).
855 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200856 chk = get_trash_chunk();
857
858 if (check->type == PR_O2_TCPCHK_CHK) {
859 chunk_printf(chk, " at step %d of tcp-check", tcpcheck_get_step_id(check->server));
860 /* we were looking for a string */
861 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
862 if (check->current_step->string)
863 chunk_appendf(chk, " (string '%s')", check->current_step->string);
864 else if (check->current_step->expect_regex)
865 chunk_appendf(chk, " (expect regex)");
866 }
867 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
868 chunk_appendf(chk, " (send)");
869 }
870 }
871
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100872 if (conn->err_code) {
873 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200874 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100875 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200876 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100877 err_msg = trash.str;
878 }
879 else {
880 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200881 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100882 err_msg = trash.str;
883 }
884 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200885 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100886 }
887 }
888
889 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
890 /* L4 not established (yet) */
891 if (conn->flags & CO_FL_ERROR)
892 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
893 else if (expired)
894 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
895 }
896 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
897 /* L6 not established (yet) */
898 if (conn->flags & CO_FL_ERROR)
899 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
900 else if (expired)
901 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
902 }
903 else if (conn->flags & CO_FL_ERROR) {
904 /* I/O error after connection was established and before we could diagnose */
905 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
906 }
907 else if (expired) {
908 /* connection established but expired check */
909 if (check->type == PR_O2_SSL3_CHK)
910 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
911 else /* HTTP, SMTP, ... */
912 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
913 }
914
915 return;
916}
917
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918/*
919 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200920 * the connection acknowledgement. If the proxy requires L7 health-checks,
921 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900922 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200923 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200924static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925{
Simon Horman4a741432013-02-23 15:35:38 +0900926 struct check *check = conn->owner;
927 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900928 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200929
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100930 if (unlikely(check->result & SRV_CHK_FAILED))
931 goto out_wakeup;
932
933 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_WR))
934 return;
935
Willy Tarreau20a18342013-12-05 00:31:46 +0100936 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100937 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100938 __conn_data_stop_both(conn);
939 goto out_wakeup;
940 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100941
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100942 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100943 /* if the output is closed, we can't do anything */
944 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100945 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100946 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200947 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200948
Willy Tarreau06559ac2013-12-05 01:53:08 +0100949 /* here, we know that the connection is established. That's enough for
950 * a pure TCP check.
951 */
952 if (!check->type)
953 goto out_wakeup;
954
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200955 if (check->type == PR_O2_TCPCHK_CHK) {
956 tcpcheck_main(conn);
957 return;
958 }
959
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100960 if (check->bo->o) {
961 conn->xprt->snd_buf(conn, check->bo, MSG_DONTWAIT | MSG_NOSIGNAL);
962 if (conn->flags & CO_FL_ERROR) {
963 chk_report_conn_err(conn, errno, 0);
964 __conn_data_stop_both(conn);
965 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200966 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100967 if (check->bo->o)
968 return;
969 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200970
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100971 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
972 if (s->proxy->timeout.check) {
973 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
974 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100976 goto out_nowake;
977
Willy Tarreau83749182007-04-15 20:56:27 +0200978 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200979 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200980 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200981 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200982}
983
Willy Tarreaubaaee002006-06-26 02:48:02 +0200984/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200985 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200986 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900987 * set_server_check_status() to update check->status, check->duration
988 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200989
990 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
991 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
992 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
993 * response to an SSL HELLO (the principle is that this is enough to
994 * distinguish between an SSL server and a pure TCP relay). All other cases will
995 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
996 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200997 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200998static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999{
Simon Horman4a741432013-02-23 15:35:38 +09001000 struct check *check = conn->owner;
1001 struct server *s = check->server;
1002 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001003 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +01001004 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001005 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +02001006
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001007 if (unlikely(check->result & SRV_CHK_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +02001008 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +02001009
Willy Tarreau5a78f362012-11-23 12:47:05 +01001010 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_RD))
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001011 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001012
1013 if (check->type == PR_O2_TCPCHK_CHK) {
1014 tcpcheck_main(conn);
1015 return;
1016 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001017
Willy Tarreau83749182007-04-15 20:56:27 +02001018 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
1019 * but the connection was closed on the remote end. Fortunately, recv still
1020 * works correctly and we don't need to do the getsockopt() on linux.
1021 */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001022
1023 /* Set buffer to point to the end of the data already read, and check
1024 * that there is free space remaining. If the buffer is full, proceed
1025 * with running the checks without attempting another socket read.
1026 */
Nick Chalk57b1bf72010-03-16 15:50:46 +00001027
Willy Tarreau03938182010-03-17 21:52:07 +01001028 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001029
Simon Horman4a741432013-02-23 15:35:38 +09001030 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +02001031 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +01001032 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +09001033 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +02001034 /* Report network errors only if we got no other data. Otherwise
1035 * we'll let the upper layers decide whether the response is OK
1036 * or not. It is very common that an RST sent by the server is
1037 * reported as an error just after the last data chunk.
1038 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001039 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +01001040 goto out_wakeup;
1041 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042 }
1043
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001044
Willy Tarreau03938182010-03-17 21:52:07 +01001045 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +09001046 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +01001047 */
Simon Horman4a741432013-02-23 15:35:38 +09001048 if (check->bi->i < check->bi->size)
1049 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001050 else {
Simon Horman4a741432013-02-23 15:35:38 +09001051 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001052 done = 1; /* buffer full, don't wait for more data */
1053 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001054
Nick Chalk57b1bf72010-03-16 15:50:46 +00001055 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +09001056 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001057 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001058 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001059 goto wait_more_data;
1060
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001061 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +09001062 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
1063 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
1064 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
1065 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
1066 !isdigit((unsigned char) *(check->bi->data + 11))) {
1067 cut_crlf(check->bi->data);
1068 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001069
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001070 goto out_wakeup;
1071 }
1072
Simon Horman4a741432013-02-23 15:35:38 +09001073 check->code = str2uic(check->bi->data + 9);
1074 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +00001075
Willy Tarreaubd741542010-03-16 18:46:54 +01001076 if ((s->proxy->options & PR_O_DISABLE404) &&
Simon Horman4a741432013-02-23 15:35:38 +09001077 (s->state & SRV_RUNNING) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +00001078 /* 404 may be accepted as "stopping" only if the server was up */
1079 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001080 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001081 }
Willy Tarreaubd741542010-03-16 18:46:54 +01001082 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
1083 /* Run content verification check... We know we have at least 13 chars */
1084 if (!httpchk_expect(s, done))
1085 goto wait_more_data;
1086 }
1087 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +09001088 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +01001089 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001090 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +01001091 }
Nick Chalk57b1bf72010-03-16 15:50:46 +00001092 else {
1093 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001094 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001095 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001096 break;
1097
1098 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001099 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001100 goto wait_more_data;
1101
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001102 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +09001103 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
1104 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001105 else
Simon Horman4a741432013-02-23 15:35:38 +09001106 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001107 break;
1108
1109 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001110 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001111 goto wait_more_data;
1112
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001113 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +09001114 if ((check->bi->i < strlen("000\r")) ||
1115 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
1116 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
1117 !isdigit((unsigned char) *(check->bi->data + 2))) {
1118 cut_crlf(check->bi->data);
1119 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001120
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001121 goto out_wakeup;
1122 }
1123
Simon Horman4a741432013-02-23 15:35:38 +09001124 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001125
Simon Horman4a741432013-02-23 15:35:38 +09001126 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001127 cut_crlf(desc);
1128
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001129 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +09001130 if (*check->bi->data == '2')
1131 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001132 else
Simon Horman4a741432013-02-23 15:35:38 +09001133 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001134 break;
1135
Simon Hormana2b9dad2013-02-12 10:45:54 +09001136 case PR_O2_LB_AGENT_CHK: {
1137 short status = HCHK_STATUS_L7RSP;
1138 const char *desc = "Unknown feedback string";
1139 const char *down_cmd = NULL;
Simon Horman671b6f02013-11-25 10:46:39 +09001140 int disabled;
Willy Tarreau9809b782013-12-11 21:40:11 +01001141 char *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001142
Willy Tarreau9809b782013-12-11 21:40:11 +01001143 /* get a complete line first */
1144 p = check->bi->data;
1145 while (*p && *p != '\n' && *p != '\r')
1146 p++;
1147
1148 if (!*p) {
1149 if (!done)
1150 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001151
Willy Tarreau9809b782013-12-11 21:40:11 +01001152 /* at least inform the admin that the agent is mis-behaving */
1153 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
1154 break;
1155 }
1156 *p = 0;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001157
Simon Horman671b6f02013-11-25 10:46:39 +09001158 /*
1159 * The agent may have been disabled after a check was
1160 * initialised. If so, ignore weight changes and drain
1161 * settings from the agent. Note that the setting is
1162 * always present in the state of the agent the server,
1163 * regardless of if the agent is being run as a primary or
1164 * secondary check. That is, regardless of if the check
1165 * parameter of this function is the agent or check field
1166 * of the server.
1167 */
1168 disabled = check->server->agent.state & CHK_STATE_DISABLED;
1169
Simon Horman4a741432013-02-23 15:35:38 +09001170 if (strchr(check->bi->data, '%')) {
Simon Horman671b6f02013-11-25 10:46:39 +09001171 if (disabled)
1172 break;
Simon Horman4a741432013-02-23 15:35:38 +09001173 desc = server_parse_weight_change_request(s, check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001174 if (!desc) {
1175 status = HCHK_STATUS_L7OKD;
Simon Horman4a741432013-02-23 15:35:38 +09001176 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001177 }
Simon Horman4a741432013-02-23 15:35:38 +09001178 } else if (!strcasecmp(check->bi->data, "drain")) {
Simon Horman671b6f02013-11-25 10:46:39 +09001179 if (disabled)
1180 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001181 desc = server_parse_weight_change_request(s, "0%");
1182 if (!desc) {
1183 desc = "drain";
1184 status = HCHK_STATUS_L7OKD;
1185 }
Simon Horman4a741432013-02-23 15:35:38 +09001186 } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001187 down_cmd = "down";
Simon Horman4a741432013-02-23 15:35:38 +09001188 } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001189 down_cmd = "stopped";
Simon Horman4a741432013-02-23 15:35:38 +09001190 } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001191 down_cmd = "fail";
1192 }
1193
1194 if (down_cmd) {
Simon Horman4a741432013-02-23 15:35:38 +09001195 const char *end = check->bi->data + strlen(down_cmd);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001196 /*
1197 * The command keyword must terminated the string or
1198 * be followed by a blank.
1199 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001200 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001201 status = HCHK_STATUS_L7STS;
Simon Horman80fefae2013-11-25 10:46:34 +09001202 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001203 }
1204 }
1205
Simon Horman4a741432013-02-23 15:35:38 +09001206 set_server_check_status(check, status, desc);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001207 set_server_drain_state(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001208 break;
1209 }
1210
Willy Tarreau1620ec32011-08-06 17:05:02 +02001211 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001212 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001213 goto wait_more_data;
1214
Simon Horman4a741432013-02-23 15:35:38 +09001215 if (check->bi->data[0] == 'R') {
1216 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001217 }
1218 else {
Simon Horman4a741432013-02-23 15:35:38 +09001219 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1220 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001221 else
1222 desc = "PostgreSQL unknown error";
1223
Simon Horman4a741432013-02-23 15:35:38 +09001224 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001225 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001226 break;
1227
1228 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001229 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001230 goto wait_more_data;
1231
Simon Horman4a741432013-02-23 15:35:38 +09001232 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1233 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001234 }
1235 else {
Simon Horman4a741432013-02-23 15:35:38 +09001236 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001237 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001238 break;
1239
1240 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001241 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001242 goto wait_more_data;
1243
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001244 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001245 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001246 /* We set the MySQL Version in description for information purpose
1247 * FIXME : it can be cool to use MySQL Version for other purpose,
1248 * like mark as down old MySQL server.
1249 */
Simon Horman4a741432013-02-23 15:35:38 +09001250 if (check->bi->i > 51) {
1251 desc = ltrim(check->bi->data + 5, ' ');
1252 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001253 }
1254 else {
1255 if (!done)
1256 goto wait_more_data;
1257 /* it seems we have a OK packet but without a valid length,
1258 * it must be a protocol error
1259 */
Simon Horman4a741432013-02-23 15:35:38 +09001260 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001261 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001262 }
1263 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001264 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001265 desc = ltrim(check->bi->data + 7, ' ');
1266 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001267 }
1268 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001269 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1270 (((unsigned int) *(check->bi->data + 1)) << 8) +
1271 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001272
Simon Horman4a741432013-02-23 15:35:38 +09001273 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001274 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001275 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001276 /* We have only one MySQL packet and it is a Handshake Initialization packet
1277 * but we need to have a second packet to know if it is alright
1278 */
Simon Horman4a741432013-02-23 15:35:38 +09001279 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001280 goto wait_more_data;
1281 }
1282 else {
1283 /* We have only one packet and it is an Error packet,
1284 * an error message is attached, so we can display it
1285 */
Simon Horman4a741432013-02-23 15:35:38 +09001286 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001287 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001288 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001289 }
Simon Horman4a741432013-02-23 15:35:38 +09001290 } else if (check->bi->i > first_packet_len + 4) {
1291 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1292 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1293 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001294
Simon Horman4a741432013-02-23 15:35:38 +09001295 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001296 /* We have 2 packets and that's good */
1297 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001298 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001299 /* No error packet */
1300 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001301 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001302 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001303 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001304 }
1305 else {
1306 /* An error message is attached in the Error packet
1307 * so we can display it ! :)
1308 */
Simon Horman4a741432013-02-23 15:35:38 +09001309 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001310 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001311 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001312 }
1313 }
1314 }
1315 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001316 if (!done)
1317 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001318 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001319 * it must be a protocol error
1320 */
Simon Horman4a741432013-02-23 15:35:38 +09001321 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001322 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001323 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001324 }
1325 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001326 break;
1327
1328 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001329 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001330 goto wait_more_data;
1331
1332 /* Check if the server speaks LDAP (ASN.1/BER)
1333 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1334 * http://tools.ietf.org/html/rfc4511
1335 */
1336
1337 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1338 * LDAPMessage: 0x30: SEQUENCE
1339 */
Simon Horman4a741432013-02-23 15:35:38 +09001340 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1341 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001342 }
1343 else {
1344 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001345 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001346
1347 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1348 * messageID: 0x02 0x01 0x01: INTEGER 1
1349 * protocolOp: 0x61: bindResponse
1350 */
1351 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001352 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1353 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001354
1355 goto out_wakeup;
1356 }
1357
1358 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001359 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001360
1361 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1362 * ldapResult: 0x0a 0x01: ENUMERATION
1363 */
1364 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001365 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 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 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1372 * resultCode
1373 */
Simon Horman4a741432013-02-23 15:35:38 +09001374 check->code = *(check->bi->data + msglen + 9);
1375 if (check->code) {
1376 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 +02001377 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001378 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001379 }
1380 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001381 break;
1382
1383 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001384 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001385 break;
1386 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001387
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001388 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001389 /* collect possible new errors */
1390 if (conn->flags & CO_FL_ERROR)
1391 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001392
Nick Chalk57b1bf72010-03-16 15:50:46 +00001393 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001394 *check->bi->data = '\0';
1395 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001396
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001397 /* Close the connection... We absolutely want to perform a hard close
1398 * and reset the connection if some data are pending, otherwise we end
1399 * up with many TIME_WAITs and eat all the source port range quickly.
1400 * To avoid sending RSTs all the time, we first try to drain pending
1401 * data.
1402 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001403 if (conn->xprt && conn->xprt->shutw)
1404 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001405
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001406 /* OK, let's not stay here forever */
1407 if (check->result & SRV_CHK_FAILED)
1408 conn->flags |= CO_FL_ERROR;
1409
Willy Tarreaua522f802012-11-23 08:56:35 +01001410 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001411 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001412 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001413
1414 wait_more_data:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001415 __conn_data_poll_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001416}
1417
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001418/*
1419 * This function is used only for server health-checks. It handles connection
1420 * status updates including errors. If necessary, it wakes the check task up.
1421 * It always returns 0.
1422 */
1423static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001424{
Simon Horman4a741432013-02-23 15:35:38 +09001425 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001426
Willy Tarreau6c560da2012-11-24 11:14:45 +01001427 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001428 /* We may get error reports bypassing the I/O handlers, typically
1429 * the case when sending a pure TCP check which fails, then the I/O
1430 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001431 * main processing task so let's simply wake it up. If we get here,
1432 * we expect errno to still be valid.
1433 */
1434 chk_report_conn_err(conn, errno, 0);
1435
Willy Tarreau2d351b62013-12-05 02:36:25 +01001436 __conn_data_stop_both(conn);
1437 task_wakeup(check->task, TASK_WOKEN_IO);
1438 }
1439
1440 if (check->result & (SRV_CHK_FAILED|SRV_CHK_PASSED)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001441 /* We're here because nobody wants to handle the error, so we
1442 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001443 */
Willy Tarreauf79c8172013-10-21 16:30:56 +02001444 if ((conn->flags & CO_FL_CTRL_READY) && !(conn->flags & CO_FL_SOCK_RD_SH)) {
Willy Tarreau2d351b62013-12-05 02:36:25 +01001445 if (conn->flags & CO_FL_WAIT_RD || !conn->ctrl->drain || !conn->ctrl->drain(conn->t.sock.fd))
1446 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1447 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001448 }
Willy Tarreauf79c8172013-10-21 16:30:56 +02001449 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001450 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001451 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001452}
1453
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001454struct data_cb check_conn_cb = {
1455 .recv = event_srv_chk_r,
1456 .send = event_srv_chk_w,
1457 .wake = wake_srv_chk,
1458};
1459
Willy Tarreaubaaee002006-06-26 02:48:02 +02001460/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001461 * updates the server's weight during a warmup stage. Once the final weight is
1462 * reached, the task automatically stops. Note that any server status change
1463 * must have updated s->last_change accordingly.
1464 */
1465static struct task *server_warmup(struct task *t)
1466{
1467 struct server *s = t->context;
1468
1469 /* by default, plan on stopping the task */
1470 t->expire = TICK_ETERNITY;
1471 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1472 return t;
1473
Willy Tarreau004e0452013-11-21 11:22:01 +01001474 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001475
1476 /* probably that we can refill this server with a bit more connections */
1477 check_for_pending(s);
1478
1479 /* get back there in 1 second or 1/20th of the slowstart interval,
1480 * whichever is greater, resulting in small 5% steps.
1481 */
1482 if (s->state & SRV_WARMINGUP)
1483 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1484 return t;
1485}
1486
1487/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001488 * manages a server health-check. Returns
1489 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1490 */
Simon Horman63a4a822012-03-19 07:24:41 +09001491static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001492{
Simon Horman4a741432013-02-23 15:35:38 +09001493 struct check *check = t->context;
1494 struct server *s = check->server;
1495 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001496 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001497 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001498 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001499
Simon Hormancd5d7b62013-02-24 17:23:38 +09001500 if (!(check->state & CHK_STATE_RUNNING)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001501 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001502 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001503 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001504
Simon Horman671b6f02013-11-25 10:46:39 +09001505 /* we don't send any health-checks when the proxy is
1506 * stopped, the server should not be checked or the check
1507 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001508 */
Simon Horman671b6f02013-11-25 10:46:39 +09001509 if (!(s->state & SRV_CHECKED) ||
1510 s->proxy->state == PR_STSTOPPED ||
1511 (s->state & SRV_MAINTAIN) ||
1512 (check->state & CHK_STATE_DISABLED))
Willy Tarreau5a78f362012-11-23 12:47:05 +01001513 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001514
1515 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001516 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001517
Simon Hormancd5d7b62013-02-24 17:23:38 +09001518 check->state |= CHK_STATE_RUNNING;
Simon Horman4a741432013-02-23 15:35:38 +09001519 check->bi->p = check->bi->data;
1520 check->bi->i = 0;
1521 check->bo->p = check->bo->data;
1522 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001523
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001524 /* tcpcheck send/expect initialisation */
1525 if (check->type == PR_O2_TCPCHK_CHK)
1526 check->current_step = NULL;
1527
1528 /* prepare the check buffer.
1529 * This should not be used if check is the secondary agent check
1530 * of a server as s->proxy->check_req will relate to the
1531 * configuration of the primary check. Similarly, tcp-check uses
1532 * its own strings.
1533 */
1534 if (check->type && check->type != PR_O2_TCPCHK_CHK && check != &s->agent) {
Simon Horman4a741432013-02-23 15:35:38 +09001535 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001536
1537 /* we want to check if this host replies to HTTP or SSLv3 requests
1538 * so we'll send the request, and won't wake the checker up now.
1539 */
Simon Horman4a741432013-02-23 15:35:38 +09001540 if ((check->type) == PR_O2_SSL3_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001541 /* SSL requires that we put Unix time in the request */
1542 int gmt_time = htonl(date.tv_sec);
Simon Horman4a741432013-02-23 15:35:38 +09001543 memcpy(check->bo->data + 11, &gmt_time, 4);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001544 }
Simon Horman4a741432013-02-23 15:35:38 +09001545 else if ((check->type) == PR_O2_HTTP_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001546 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Simon Horman4a741432013-02-23 15:35:38 +09001547 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str));
1548 bo_putstr(check->bo, "\r\n");
1549 *check->bo->p = '\0'; /* to make gdb output easier to read */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001550 }
1551 }
1552
1553 /* prepare a new connection */
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001554 conn_init(conn);
Willy Tarreau910c6aa2013-10-24 15:08:37 +02001555 conn_prepare(conn, s->check_common.proto, s->check_common.xprt);
Willy Tarreau7abddb52013-10-24 15:31:04 +02001556 conn_attach(conn, check, &check_conn_cb);
Willy Tarreau4bdae8a2013-10-14 17:29:15 +02001557 conn->target = &s->obj_type;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001558
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001559 /* no client address */
1560 clear_addr(&conn->addr.from);
1561
Simon Horman66183002013-02-23 10:16:43 +09001562 if (is_addr(&s->check_common.addr))
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001563 /* we'll connect to the check addr specified on the server */
Simon Horman66183002013-02-23 10:16:43 +09001564 conn->addr.to = s->check_common.addr;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001565 else
1566 /* we'll connect to the addr on the server */
1567 conn->addr.to = s->addr;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001568
Simon Horman4a741432013-02-23 15:35:38 +09001569 set_host_port(&conn->addr.to, check->port);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001570
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001571 /* It can return one of :
1572 * - SN_ERR_NONE if everything's OK
1573 * - SN_ERR_SRVTO if there are no more servers
1574 * - SN_ERR_SRVCL if the connection was refused by the server
1575 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1576 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1577 * - SN_ERR_INTERNAL for any other purely internal errors
1578 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001579 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001580 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001581 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001582 ret = SN_ERR_INTERNAL;
Simon Horman66183002013-02-23 10:16:43 +09001583 if (s->check_common.proto->connect)
Willy Tarreau57cd3e42013-10-24 22:01:26 +02001584 ret = s->check_common.proto->connect(conn, check->type, (check->type) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001585 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau57cd3e42013-10-24 22:01:26 +02001586 if (s->check.send_proxy) {
1587 conn->send_proxy_ofs = 1;
1588 conn->flags |= CO_FL_SEND_PROXY;
1589 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001590
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001591 switch (ret) {
1592 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001593 /* we allow up to min(inter, timeout.connect) for a connection
1594 * to establish but only when timeout.check is set
1595 * as it may be to short for a full check otherwise
1596 */
Simon Horman4a741432013-02-23 15:35:38 +09001597 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001598
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001599 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1600 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1601 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001602 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001603
1604 if (check->type)
1605 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1606
Willy Tarreau5a78f362012-11-23 12:47:05 +01001607 goto reschedule;
1608
1609 case SN_ERR_SRVTO: /* ETIMEDOUT */
1610 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman4a741432013-02-23 15:35:38 +09001611 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau5a78f362012-11-23 12:47:05 +01001612 break;
1613 case SN_ERR_PRXCOND:
1614 case SN_ERR_RESOURCE:
1615 case SN_ERR_INTERNAL:
Simon Horman4a741432013-02-23 15:35:38 +09001616 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001617 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001618 }
1619
Willy Tarreau5a78f362012-11-23 12:47:05 +01001620 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001621
Simon Hormancd5d7b62013-02-24 17:23:38 +09001622 check->state &= ~CHK_STATE_RUNNING;
Simon Hormand8583062013-11-25 10:46:33 +09001623 check_failed(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001624
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001625 /* we allow up to min(inter, timeout.connect) for a connection
1626 * to establish but only when timeout.check is set
1627 * as it may be to short for a full check otherwise
1628 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001629 while (tick_is_expired(t->expire, now_ms)) {
1630 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001631
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001632 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001633 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001634
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001635 if (s->proxy->timeout.check)
1636 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001637 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001638 }
1639 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001640 /* there was a test running.
1641 * First, let's check whether there was an uncaught error,
1642 * which can happen on connect timeout or error.
1643 */
Simon Horman28b5ffc2013-02-24 07:25:29 +09001644 if (s->check.result == SRV_CHK_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001645 /* good connection is enough for pure TCP check */
1646 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001647 if (check->use_ssl)
1648 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001649 else
Simon Horman4a741432013-02-23 15:35:38 +09001650 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001651 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001652 else if ((conn->flags & CO_FL_ERROR) || expired) {
1653 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001654 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001655 else
1656 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001657 }
1658
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001659 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001660 if (conn->xprt) {
1661 /* The check was aborted and the connection was not yet closed.
1662 * This can happen upon timeout, or when an external event such
1663 * as a failed response coupled with "observe layer7" caused the
1664 * server state to be suddenly changed.
1665 */
Willy Tarreauf79c8172013-10-21 16:30:56 +02001666 if ((conn->flags & CO_FL_CTRL_READY) && !(conn->flags & CO_FL_SOCK_RD_SH)) {
Willy Tarreau2d351b62013-12-05 02:36:25 +01001667 if (conn->flags & CO_FL_WAIT_RD || !conn->ctrl->drain || !conn->ctrl->drain(conn->t.sock.fd))
1668 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1669 (struct linger *) &nolinger, sizeof(struct linger));
1670 }
Willy Tarreauf79c8172013-10-21 16:30:56 +02001671 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001672 }
1673
Simon Hormand8583062013-11-25 10:46:33 +09001674 if (check->result & SRV_CHK_FAILED) /* a failure or timeout detected */
1675 check_failed(check);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001676 else { /* check was OK */
Willy Tarreau48494c02007-11-30 10:41:39 +01001677 /* we may have to add/remove this server from the LB group */
1678 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
Simon Horman4a741432013-02-23 15:35:38 +09001679 if ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE))
1680 set_server_enabled(check);
1681 else if (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE))
1682 set_server_disabled(check);
Willy Tarreau48494c02007-11-30 10:41:39 +01001683 }
1684
Simon Horman58c32972013-11-25 10:46:38 +09001685 if (check->health < check->rise + check->fall - 1) {
Simon Horman125d0992013-02-24 17:23:38 +09001686 check->health++; /* was bad, stays for a while */
Simon Horman4a741432013-02-23 15:35:38 +09001687 set_server_up(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001688 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001689 }
Simon Hormancd5d7b62013-02-24 17:23:38 +09001690 check->state &= ~CHK_STATE_RUNNING;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001691
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001692 rv = 0;
1693 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001694 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001695 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001696 }
Simon Horman4a741432013-02-23 15:35:38 +09001697 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001698 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001699
1700 reschedule:
1701 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09001702 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001703 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001704 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001705}
1706
Simon Horman5c942422013-11-25 10:46:32 +09001707static int start_check_task(struct check *check, int mininter,
1708 int nbcheck, int srvpos)
1709{
1710 struct task *t;
1711 /* task for the check */
1712 if ((t = task_new()) == NULL) {
1713 Alert("Starting [%s:%s] check: out of memory.\n",
1714 check->server->proxy->id, check->server->id);
1715 return 0;
1716 }
1717
1718 check->task = t;
1719 t->process = process_chk;
1720 t->context = check;
1721
1722 /* check this every ms */
1723 t->expire = tick_add(now_ms,
1724 MS_TO_TICKS(((mininter &&
1725 mininter >= srv_getinter(check)) ?
1726 mininter : srv_getinter(check)) * srvpos / nbcheck));
1727 check->start = now;
1728 task_queue(t);
1729
1730 return 1;
1731}
1732
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001733/*
1734 * Start health-check.
1735 * Returns 0 if OK, -1 if error, and prints the error in this case.
1736 */
1737int start_checks() {
1738
1739 struct proxy *px;
1740 struct server *s;
1741 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09001742 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001743
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001744 /* 1- count the checkers to run simultaneously.
1745 * We also determine the minimum interval among all of those which
1746 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1747 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001748 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001749 * too short an interval for all others.
1750 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001751 for (px = proxy; px; px = px->next) {
1752 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01001753 if (s->slowstart) {
1754 if ((t = task_new()) == NULL) {
1755 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1756 return -1;
1757 }
1758 /* We need a warmup task that will be called when the server
1759 * state switches from down to up.
1760 */
1761 s->warmup = t;
1762 t->process = server_warmup;
1763 t->context = s;
1764 t->expire = TICK_ETERNITY;
1765 }
1766
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001767 if (!(s->state & SRV_CHECKED))
1768 continue;
1769
Simon Horman4a741432013-02-23 15:35:38 +09001770 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1771 (!mininter || mininter > srv_getinter(&s->check)))
1772 mininter = srv_getinter(&s->check);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001773
Simon Horman4a741432013-02-23 15:35:38 +09001774 nbcheck++;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001775 }
1776 }
1777
Simon Horman4a741432013-02-23 15:35:38 +09001778 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001779 return 0;
1780
1781 srand((unsigned)time(NULL));
1782
1783 /*
1784 * 2- start them as far as possible from each others. For this, we will
1785 * start them after their interval set to the min interval divided by
1786 * the number of servers, weighted by the server's position in the list.
1787 */
1788 for (px = proxy; px; px = px->next) {
1789 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09001790 /* A task for the main check */
1791 if (s->state & SRV_CHECKED) {
1792 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1793 return -1;
1794 srvpos++;
1795 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001796
Simon Hormand60d6912013-11-25 10:46:36 +09001797 /* A task for a auxiliary agent check */
1798 if (s->state & SRV_AGENT_CHECKED) {
1799 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1800 return -1;
1801 }
1802 srvpos++;
1803 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001804 }
1805 }
1806 return 0;
1807}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001808
1809/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001810 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001811 * The buffer MUST be terminated by a null byte before calling this function.
1812 * Sets server status appropriately. The caller is responsible for ensuring
1813 * that the buffer contains at least 13 characters. If <done> is zero, we may
1814 * return 0 to indicate that data is required to decide of a match.
1815 */
1816static int httpchk_expect(struct server *s, int done)
1817{
1818 static char status_msg[] = "HTTP status check returned code <000>";
1819 char status_code[] = "000";
1820 char *contentptr;
1821 int crlf;
1822 int ret;
1823
1824 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1825 case PR_O2_EXP_STS:
1826 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001827 memcpy(status_code, s->check.bi->data + 9, 3);
1828 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001829
1830 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1831 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1832 else
1833 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1834
1835 /* we necessarily have the response, so there are no partial failures */
1836 if (s->proxy->options2 & PR_O2_EXP_INV)
1837 ret = !ret;
1838
Simon Horman4a741432013-02-23 15:35:38 +09001839 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01001840 break;
1841
1842 case PR_O2_EXP_STR:
1843 case PR_O2_EXP_RSTR:
1844 /* very simple response parser: ignore CR and only count consecutive LFs,
1845 * stop with contentptr pointing to first char after the double CRLF or
1846 * to '\0' if crlf < 2.
1847 */
1848 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001849 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001850 if (crlf >= 2)
1851 break;
1852 if (*contentptr == '\r')
1853 continue;
1854 else if (*contentptr == '\n')
1855 crlf++;
1856 else
1857 crlf = 0;
1858 }
1859
1860 /* Check that response contains a body... */
1861 if (crlf < 2) {
1862 if (!done)
1863 return 0;
1864
Simon Horman4a741432013-02-23 15:35:38 +09001865 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001866 "HTTP content check could not find a response body");
1867 return 1;
1868 }
1869
1870 /* Check that response body is not empty... */
1871 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001872 if (!done)
1873 return 0;
1874
Simon Horman4a741432013-02-23 15:35:38 +09001875 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001876 "HTTP content check found empty response body");
1877 return 1;
1878 }
1879
1880 /* Check the response content against the supplied string
1881 * or regex... */
1882 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1883 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1884 else
1885 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1886
1887 /* if we don't match, we may need to wait more */
1888 if (!ret && !done)
1889 return 0;
1890
1891 if (ret) {
1892 /* content matched */
1893 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001894 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001895 "HTTP check matched unwanted content");
1896 else
Simon Horman4a741432013-02-23 15:35:38 +09001897 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001898 "HTTP content check matched");
1899 }
1900 else {
1901 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001902 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001903 "HTTP check did not match unwanted content");
1904 else
Simon Horman4a741432013-02-23 15:35:38 +09001905 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001906 "HTTP content check did not match");
1907 }
1908 break;
1909 }
1910 return 1;
1911}
1912
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02001913/*
1914 * return the id of a step in a send/expect session
1915 */
1916static int tcpcheck_get_step_id(struct server *s)
1917{
1918 struct tcpcheck_rule *cur = NULL, *next = NULL;
1919 int i = 0;
1920
1921 cur = s->check.current_step;
1922
1923 /* no step => first step */
1924 if (cur == NULL)
1925 return 1;
1926
1927 /* increment i until current step */
1928 list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) {
1929 if (next->list.p == &cur->list)
1930 break;
1931 ++i;
1932 }
1933
1934 return i;
1935}
1936
1937static void tcpcheck_main(struct connection *conn)
1938{
1939 char *contentptr;
1940 unsigned int contentlen;
1941 struct list *head = NULL;
1942 struct tcpcheck_rule *cur = NULL;
1943 int done = 0, ret = 0;
1944
1945 struct check *check = conn->owner;
1946 struct server *s = check->server;
1947 struct task *t = check->task;
1948
1949 /* don't do anything until the connection is established */
1950 if (!(conn->flags & CO_FL_CONNECTED)) {
1951 /* update expire time, should be done by process_chk */
1952 /* we allow up to min(inter, timeout.connect) for a connection
1953 * to establish but only when timeout.check is set
1954 * as it may be to short for a full check otherwise
1955 */
1956 while (tick_is_expired(t->expire, now_ms)) {
1957 int t_con;
1958
1959 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1960 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1961
1962 if (s->proxy->timeout.check)
1963 t->expire = tick_first(t->expire, t_con);
1964 }
1965 return;
1966 }
1967
1968 /* here, we know that the connection is established */
1969 if (check->result & (SRV_CHK_FAILED | SRV_CHK_PASSED)) {
1970 goto out_end_tcpcheck;
1971 }
1972
1973 /* head is be the first element of the double chained list */
1974 head = &s->proxy->tcpcheck_rules;
1975
1976 /* no step means first step
1977 * initialisation */
1978 if (check->current_step == NULL) {
1979 check->bo->p = check->bo->data;
1980 check->bo->o = 0;
1981 check->bi->p = check->bi->data;
1982 check->bi->i = 0;
1983 cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
1984 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1985 if (s->proxy->timeout.check)
1986 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
1987 }
1988 /* keep on processing step */
1989 else {
1990 cur = check->current_step;
1991 }
1992
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01001993 if (conn->flags & CO_FL_HANDSHAKE)
1994 return;
1995
1996 /* It's only the rules which will enable send/recv */
1997 __conn_data_stop_both(conn);
1998
Willy Tarreauabca5b62013-12-06 14:19:25 +01001999 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002000 /* we have to try to flush the output buffer before reading, at the end,
2001 * or if we're about to send a string that does not fit in the remaining space.
2002 */
2003 if (check->bo->o &&
2004 (&cur->list == head ||
2005 check->current_step->action != TCPCHK_ACT_SEND ||
2006 check->current_step->string_len >= buffer_total_space(check->bo))) {
2007
2008 if ((conn->flags & CO_FL_WAIT_WR) ||
2009 conn->xprt->snd_buf(conn, check->bo, MSG_DONTWAIT | MSG_NOSIGNAL) <= 0) {
2010 if (conn->flags & CO_FL_ERROR) {
2011 chk_report_conn_err(conn, errno, 0);
2012 __conn_data_stop_both(conn);
2013 goto out_end_tcpcheck;
2014 }
2015 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002016 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002017 }
2018
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002019 /* did we reach the end ? If so, let's check that everything was sent */
2020 if (&cur->list == head) {
2021 if (check->bo->o)
2022 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002023 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002024 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002025
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002026 if (check->current_step->action == TCPCHK_ACT_SEND) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002027 /* reset the read buffer */
2028 if (*check->bi->data != '\0') {
2029 *check->bi->data = '\0';
2030 check->bi->i = 0;
2031 }
2032
2033 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2034 conn->flags |= CO_FL_ERROR;
2035 chk_report_conn_err(conn, 0, 0);
2036 goto out_end_tcpcheck;
2037 }
2038
Willy Tarreauabca5b62013-12-06 14:19:25 +01002039 if (check->current_step->string_len >= check->bo->size) {
2040 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2041 check->current_step->string_len, check->bo->size,
2042 tcpcheck_get_step_id(s));
2043 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2044 goto out_end_tcpcheck;
2045 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002046
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002047 /* do not try to send if there is no space */
2048 if (check->current_step->string_len >= buffer_total_space(check->bo))
2049 continue;
2050
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002051 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2052 *check->bo->p = '\0'; /* to make gdb output easier to read */
2053
Willy Tarreauabca5b62013-12-06 14:19:25 +01002054 /* go to next rule and try to send */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002055 cur = (struct tcpcheck_rule *)cur->list.n;
2056 check->current_step = cur;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002057 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002058 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002059 if (unlikely(check->result & SRV_CHK_FAILED))
2060 goto out_end_tcpcheck;
2061
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002062 if ((conn->flags & CO_FL_WAIT_RD) ||
2063 conn->xprt->rcv_buf(conn, check->bi, buffer_total_space(check->bi)) <= 0) {
2064 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2065 done = 1;
2066 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2067 /* Report network errors only if we got no other data. Otherwise
2068 * we'll let the upper layers decide whether the response is OK
2069 * or not. It is very common that an RST sent by the server is
2070 * reported as an error just after the last data chunk.
2071 */
2072 chk_report_conn_err(conn, errno, 0);
2073 goto out_end_tcpcheck;
2074 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002075 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002076 else
2077 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002078 }
2079
2080 /* Intermediate or complete response received.
2081 * Terminate string in check->bi->data buffer.
2082 */
2083 if (check->bi->i < check->bi->size) {
2084 check->bi->data[check->bi->i] = '\0';
2085 }
2086 else {
2087 check->bi->data[check->bi->i - 1] = '\0';
2088 done = 1; /* buffer full, don't wait for more data */
2089 }
2090
2091 contentptr = check->bi->data;
2092 contentlen = check->bi->i;
2093
2094 /* Check that response body is not empty... */
2095 if (*contentptr == '\0') {
2096 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002097 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002098
2099 /* empty response */
2100 chunk_printf(&trash, "TCPCHK got an empty response at step %d",
2101 tcpcheck_get_step_id(s));
2102 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2103
2104 goto out_end_tcpcheck;
2105 }
2106
2107 if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002108 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002109
Willy Tarreaua970c282013-12-06 12:47:19 +01002110 tcpcheck_expect:
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002111 if (cur->string != NULL)
2112 ret = my_memmem(contentptr, contentlen, cur->string, cur->string_len) != NULL;
2113 else if (cur->expect_regex != NULL)
2114 ret = regexec(cur->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
2115
2116 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002117 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002118
2119 /* matched */
2120 if (ret) {
2121 /* matched but we did not want to => ERROR */
2122 if (cur->inverse) {
2123 /* we were looking for a string */
2124 if (cur->string != NULL) {
2125 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
2126 cur->string, tcpcheck_get_step_id(s));
2127 }
2128 else {
2129 /* we were looking for a regex */
2130 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d",
2131 tcpcheck_get_step_id(s));
2132 }
2133 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2134 goto out_end_tcpcheck;
2135 }
2136 /* matched and was supposed to => OK, next step */
2137 else {
2138 cur = (struct tcpcheck_rule*)cur->list.n;
2139 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002140 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002141 goto tcpcheck_expect;
2142 __conn_data_stop_recv(conn);
2143 }
2144 }
2145 else {
2146 /* not matched */
2147 /* not matched and was not supposed to => OK, next step */
2148 if (cur->inverse) {
2149 cur = (struct tcpcheck_rule*)cur->list.n;
2150 check->current_step = cur;
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002151 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002152 goto tcpcheck_expect;
2153 __conn_data_stop_recv(conn);
2154 }
2155 /* not matched but was supposed to => ERROR */
2156 else {
2157 /* we were looking for a string */
2158 if (cur->string != NULL) {
2159 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
2160 cur->string, tcpcheck_get_step_id(s));
2161 }
2162 else {
2163 /* we were looking for a regex */
2164 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
2165 tcpcheck_get_step_id(s));
2166 }
2167 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2168 goto out_end_tcpcheck;
2169 }
2170 }
2171 } /* end expect */
2172 } /* end loop over double chained step list */
2173
2174 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2175 goto out_end_tcpcheck;
2176
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002177 out_need_io:
2178 if (check->bo->o)
2179 __conn_data_want_send(conn);
2180
2181 if (check->current_step->action == TCPCHK_ACT_EXPECT)
2182 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002183 return;
2184
2185 out_end_tcpcheck:
2186 /* collect possible new errors */
2187 if (conn->flags & CO_FL_ERROR)
2188 chk_report_conn_err(conn, 0, 0);
2189
2190 /* Close the connection... We absolutely want to perform a hard close
2191 * and reset the connection if some data are pending, otherwise we end
2192 * up with many TIME_WAITs and eat all the source port range quickly.
2193 * To avoid sending RSTs all the time, we first try to drain pending
2194 * data.
2195 */
2196 if (conn->xprt && conn->xprt->shutw)
2197 conn->xprt->shutw(conn, 0);
2198
2199 check->current_step = NULL;
2200
2201 if (check->result & SRV_CHK_FAILED)
2202 conn->flags |= CO_FL_ERROR;
2203
2204 __conn_data_stop_both(conn);
2205 task_wakeup(t, TASK_WOKEN_IO);
2206
2207 return;
2208}
2209
2210
Willy Tarreaubd741542010-03-16 18:46:54 +01002211/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002212 * Local variables:
2213 * c-indent-level: 8
2214 * c-basic-offset: 8
2215 * End:
2216 */