blob: f1072cdb75fb37b899e578951e8667c8ca4a5c83 [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>
Simon Hormana2b9dad2013-02-12 10:45:54 +090028#include <stdbool.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
33#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020034#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020040#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090041#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/fd.h>
43#include <proto/log.h>
44#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020045#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010046#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010047#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010048#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020049#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090051#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010052#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/task.h>
54
Willy Tarreaubd741542010-03-16 18:46:54 +010055static int httpchk_expect(struct server *s, int done);
56
Simon Horman63a4a822012-03-19 07:24:41 +090057static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020058 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
59 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
60 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020061
Willy Tarreaud3aac702012-11-23 11:32:12 +010062 [HCHK_STATUS_HANA] = { SRV_CHK_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010063
Willy Tarreaud3aac702012-11-23 11:32:12 +010064 [HCHK_STATUS_SOCKERR] = { SRV_CHK_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020065
Willy Tarreaud3aac702012-11-23 11:32:12 +010066 [HCHK_STATUS_L4OK] = { SRV_CHK_PASSED, "L4OK", "Layer4 check passed" },
67 [HCHK_STATUS_L4TOUT] = { SRV_CHK_FAILED, "L4TOUT", "Layer4 timeout" },
68 [HCHK_STATUS_L4CON] = { SRV_CHK_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020069
Willy Tarreaud3aac702012-11-23 11:32:12 +010070 [HCHK_STATUS_L6OK] = { SRV_CHK_PASSED, "L6OK", "Layer6 check passed" },
71 [HCHK_STATUS_L6TOUT] = { SRV_CHK_FAILED, "L6TOUT", "Layer6 timeout" },
72 [HCHK_STATUS_L6RSP] = { SRV_CHK_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020073
Willy Tarreaud3aac702012-11-23 11:32:12 +010074 [HCHK_STATUS_L7TOUT] = { SRV_CHK_FAILED, "L7TOUT", "Layer7 timeout" },
75 [HCHK_STATUS_L7RSP] = { SRV_CHK_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020077 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020078
Willy Tarreaud3aac702012-11-23 11:32:12 +010079 [HCHK_STATUS_L7OKD] = { SRV_CHK_PASSED, "L7OK", "Layer7 check passed" },
80 [HCHK_STATUS_L7OKCD] = { SRV_CHK_PASSED | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
81 [HCHK_STATUS_L7STS] = { SRV_CHK_FAILED, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020082};
83
Simon Horman63a4a822012-03-19 07:24:41 +090084static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010085 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
86
87 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
88 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
89
90 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
91 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
92 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
93 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
94
95 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
96 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
97 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
98};
99
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200100/*
101 * Convert check_status code to description
102 */
103const char *get_check_status_description(short check_status) {
104
105 const char *desc;
106
107 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200108 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200109 else
110 desc = NULL;
111
112 if (desc && *desc)
113 return desc;
114 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200115 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200116}
117
118/*
119 * Convert check_status code to short info
120 */
121const char *get_check_status_info(short check_status) {
122
123 const char *info;
124
125 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200126 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200127 else
128 info = NULL;
129
130 if (info && *info)
131 return info;
132 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200133 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200134}
135
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100136const char *get_analyze_status(short analyze_status) {
137
138 const char *desc;
139
140 if (analyze_status < HANA_STATUS_SIZE)
141 desc = analyze_statuses[analyze_status].desc;
142 else
143 desc = NULL;
144
145 if (desc && *desc)
146 return desc;
147 else
148 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
149}
150
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200151#define SSP_O_HCHK 0x0002
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200152
153static void server_status_printf(struct chunk *msg, struct server *s, unsigned options, int xferred) {
154
Willy Tarreau44267702011-10-28 15:35:33 +0200155 if (s->track)
Willy Tarreau77804732012-10-29 16:14:26 +0100156 chunk_appendf(msg, " via %s/%s",
Willy Tarreau44267702011-10-28 15:35:33 +0200157 s->track->proxy->id, s->track->id);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200158
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200159 if (options & SSP_O_HCHK) {
Willy Tarreau77804732012-10-29 16:14:26 +0100160 chunk_appendf(msg, ", reason: %s", get_check_status_description(s->check.status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200161
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200162 if (s->check.status >= HCHK_STATUS_L57DATA)
Willy Tarreau77804732012-10-29 16:14:26 +0100163 chunk_appendf(msg, ", code: %d", s->check.code);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200164
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200165 if (*s->check.desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200166 struct chunk src;
167
Willy Tarreau77804732012-10-29 16:14:26 +0100168 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200169
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200170 chunk_initlen(&src, s->check.desc, 0, strlen(s->check.desc));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200171 chunk_asciiencode(msg, &src, '"');
172
Willy Tarreau77804732012-10-29 16:14:26 +0100173 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200174 }
175
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200176 if (s->check.duration >= 0)
Willy Tarreau77804732012-10-29 16:14:26 +0100177 chunk_appendf(msg, ", check duration: %ldms", s->check.duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200178 }
179
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200180 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100181 if (!(s->state & SRV_RUNNING))
Willy Tarreau77804732012-10-29 16:14:26 +0100182 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100183 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200184 s->proxy->srv_act, s->proxy->srv_bck,
185 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
186 s->cur_sess, xferred, s->nbpend);
187 else
Willy Tarreau77804732012-10-29 16:14:26 +0100188 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100189 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200190 s->proxy->srv_act, s->proxy->srv_bck,
191 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
192 xferred, s->nbpend);
193 }
194}
195
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200196/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200197 * Set s->check.status, update s->check.duration and fill s->result with
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200198 * an adequate SRV_CHK_* value.
199 *
200 * Show information in logs about failed health check if server is UP
201 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200202 */
Simon Horman007f2a22013-02-12 10:45:49 +0900203static void set_server_check_status(struct server *s, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100204{
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200205 if (status == HCHK_STATUS_START) {
206 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200207 s->check.desc[0] = '\0';
208 s->check.start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200209 return;
210 }
211
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200212 if (!s->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) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200216 strncpy(s->check.desc, desc, HCHK_DESC_LEN-1);
217 s->check.desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200218 } else
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200219 s->check.desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200220
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200221 s->check.status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200222 if (check_statuses[status].result)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100223 s->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)
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200226 s->check.duration = -1;
227 else if (!tv_iszero(&s->check.start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200228 /* set_server_check_status() may be called more than once */
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200229 s->check.duration = tv_ms_elapsed(&s->check.start, &now);
230 tv_zero(&s->check.start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200231 }
232
233 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
Willy Tarreaud3aac702012-11-23 11:32:12 +0100234 (((s->health != 0) && (s->result & SRV_CHK_FAILED)) ||
235 ((s->health != s->rise + s->fall - 1) && (s->result & SRV_CHK_PASSED)) ||
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200236 ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE)) ||
237 (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE)))) {
238
239 int health, rise, fall, state;
240
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100241 chunk_reset(&trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200242
243 /* FIXME begin: calculate local version of the health/rise/fall/state */
244 health = s->health;
245 rise = s->rise;
246 fall = s->fall;
247 state = s->state;
248
Willy Tarreaud3aac702012-11-23 11:32:12 +0100249 if (s->result & SRV_CHK_FAILED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200250 if (health > rise) {
251 health--; /* still good */
252 } else {
253 if (health == rise)
254 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
255
256 health = 0;
257 }
258 }
259
Willy Tarreaud3aac702012-11-23 11:32:12 +0100260 if (s->result & SRV_CHK_PASSED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200261 if (health < rise + fall - 1) {
262 health++; /* was bad, stays for a while */
263
264 if (health == rise)
265 state |= SRV_RUNNING;
266
267 if (health >= rise)
268 health = rise + fall - 1; /* OK now */
269 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100270
271 /* clear consecutive_errors if observing is enabled */
272 if (s->onerror)
273 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200274 }
275 /* FIXME end: calculate local version of the health/rise/fall/state */
276
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100277 chunk_appendf(&trash,
278 "Health check for %sserver %s/%s %s%s",
279 s->state & SRV_BACKUP ? "backup " : "",
280 s->proxy->id, s->id,
281 (s->result & SRV_CHK_DISABLE)?"conditionally ":"",
Willy Tarreaud3aac702012-11-23 11:32:12 +0100282 (s->result & SRV_CHK_PASSED)?"succeeded":"failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200283
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100284 server_status_printf(&trash, s, SSP_O_HCHK, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100286 chunk_appendf(&trash, ", status: %d/%d %s",
287 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
288 (state & SRV_RUNNING) ? (fall) : (rise),
289 (state & SRV_RUNNING)?"UP":"DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200290
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100291 Warning("%s.\n", trash.str);
292 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200293 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200294}
295
Willy Tarreau48494c02007-11-30 10:41:39 +0100296/* sends a log message when a backend goes down, and also sets last
297 * change date.
298 */
299static void set_backend_down(struct proxy *be)
300{
301 be->last_change = now.tv_sec;
302 be->down_trans++;
303
304 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
305 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
306}
307
308/* Redistribute pending connections when a server goes down. The number of
309 * connections redistributed is returned.
310 */
311static int redistribute_pending(struct server *s)
312{
313 struct pendconn *pc, *pc_bck, *pc_end;
314 int xferred = 0;
315
316 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
317 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100318 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
319 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100320 /* The REDISP option was specified. We will ignore
321 * cookie and force to balance or use the dispatcher.
322 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100323
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100324 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100325 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100326
Willy Tarreau48494c02007-11-30 10:41:39 +0100327 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200328 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100329 xferred++;
330 }
331 }
332 return xferred;
333}
334
335/* Check for pending connections at the backend, and assign some of them to
336 * the server coming up. The server's weight is checked before being assigned
337 * connections it may not be able to handle. The total number of transferred
338 * connections is returned.
339 */
340static int check_for_pending(struct server *s)
341{
342 int xferred;
343
344 if (!s->eweight)
345 return 0;
346
347 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
348 struct session *sess;
349 struct pendconn *p;
350
351 p = pendconn_from_px(s->proxy);
352 if (!p)
353 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100354 p->sess->target = &s->obj_type;
Willy Tarreau48494c02007-11-30 10:41:39 +0100355 sess = p->sess;
356 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200357 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100358 }
359 return xferred;
360}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700362/* Shutdown all connections of a server. The caller must pass a termination
363 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
364 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900365 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700366static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900367{
368 struct session *session, *session_bck;
369
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200370 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
371 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700372 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900373}
374
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700375/* Shutdown all connections of all backup servers of a proxy. The caller must
376 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
377 * the reason for the shutdown.
378 */
379static void shutdown_backup_sessions(struct proxy *px, int why)
380{
381 struct server *srv;
382
383 for (srv = px->srv; srv != NULL; srv = srv->next)
384 if (srv->state & SRV_BACKUP)
385 shutdown_sessions(srv, why);
386}
387
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388/* Sets server <s> down, notifies by all available means, recounts the
389 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200390 * possible to other servers. It automatically recomputes the number of
391 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 */
Cyril Bontécd19e512010-01-31 22:34:03 +0100393void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100395 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396 int xferred;
397
Cyril Bontécd19e512010-01-31 22:34:03 +0100398 if (s->state & SRV_MAINTAIN) {
399 s->health = s->rise;
400 }
401
Willy Tarreau44267702011-10-28 15:35:33 +0200402 if (s->health == s->rise || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100403 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200404 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200405
406 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100407 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200408 if (s->proxy->lbprm.set_server_status_down)
409 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900411 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700412 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900413
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 /* we might have sessions queued on this server and waiting for
415 * a connection. Those which are redispatchable will be queued
416 * to another server or to the proxy itself.
417 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100418 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100419
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100420 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100421
Cyril Bontécd19e512010-01-31 22:34:03 +0100422 if (s->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100423 chunk_appendf(&trash,
424 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
425 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100426 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100427 chunk_appendf(&trash,
428 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
429 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100430
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100431 server_status_printf(&trash, s,
432 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? SSP_O_HCHK : 0),
433 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100434 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100435 Warning("%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200436
Willy Tarreau48494c02007-11-30 10:41:39 +0100437 /* we don't send an alert if the server was previously paused */
438 if (srv_was_paused)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100439 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreau48494c02007-11-30 10:41:39 +0100440 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100441 send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200442
Willy Tarreaud64d2252010-10-17 17:16:42 +0200443 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100444 set_backend_down(s->proxy);
445
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200446 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100447
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100448 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100449 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100450 if (! (srv->state & SRV_MAINTAIN))
451 /* Only notify tracking servers that are not already in maintenance. */
452 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100454
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 s->health = 0; /* failure */
456}
457
Cyril Bontécd19e512010-01-31 22:34:03 +0100458void set_server_up(struct server *s) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100459
460 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100461 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100462 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100463
Cyril Bontécd19e512010-01-31 22:34:03 +0100464 if (s->state & SRV_MAINTAIN) {
465 s->health = s->rise;
466 }
467
Willy Tarreau44267702011-10-28 15:35:33 +0200468 if (s->health == s->rise || s->track) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100469 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
470 if (s->proxy->last_change < now.tv_sec) // ignore negative times
471 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
472 s->proxy->last_change = now.tv_sec;
473 }
474
475 if (s->last_change < now.tv_sec) // ignore negative times
476 s->down_time += now.tv_sec - s->last_change;
477
478 s->last_change = now.tv_sec;
479 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100480 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100481
482 if (s->slowstart > 0) {
483 s->state |= SRV_WARMINGUP;
484 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
485 /* For dynamic algorithms, start at the first step of the weight,
486 * without multiplying by BE_WEIGHT_SCALE.
487 */
488 s->eweight = s->uweight;
489 if (s->proxy->lbprm.update_server_eweight)
490 s->proxy->lbprm.update_server_eweight(s);
491 }
Willy Tarreau2e993902011-10-31 11:53:20 +0100492 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100493 }
Willy Tarreau9580d162012-05-19 19:07:40 +0200494 if (s->proxy->lbprm.set_server_status_up)
495 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100496
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700497 /* If the server is set with "on-marked-up shutdown-backup-sessions",
498 * and it's not a backup server and its effective weight is > 0,
499 * then it can accept new connections, so we shut down all sessions
500 * on all backup servers.
501 */
502 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
503 !(s->state & SRV_BACKUP) && s->eweight)
504 shutdown_backup_sessions(s->proxy, SN_ERR_UP);
505
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100506 /* check if we can handle some connections queued at the proxy. We
507 * will take as many as we can handle.
508 */
509 xferred = check_for_pending(s);
510
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100511 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100512
Willy Tarreau45446782012-03-09 17:16:09 +0100513 if (old_state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100514 chunk_appendf(&trash,
515 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
516 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100517 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100518 chunk_appendf(&trash,
519 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
520 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100521
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100522 server_status_printf(&trash, s,
523 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? SSP_O_HCHK : 0),
524 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100525 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100526
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100527 Warning("%s.\n", trash.str);
528 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100529
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100530 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100531 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100532 if (! (srv->state & SRV_MAINTAIN))
533 /* Only notify tracking servers if they're not in maintenance. */
534 set_server_up(srv);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100535 }
536
537 if (s->health >= s->rise)
538 s->health = s->rise + s->fall - 1; /* OK now */
539
540}
541
542static void set_server_disabled(struct server *s) {
543
544 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100545 int xferred;
546
547 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200548 if (s->proxy->lbprm.set_server_status_down)
549 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100550
551 /* we might have sessions queued on this server and waiting for
552 * a connection. Those which are redispatchable will be queued
553 * to another server or to the proxy itself.
554 */
555 xferred = redistribute_pending(s);
556
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100557 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100558
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100559 chunk_appendf(&trash,
560 "Load-balancing on %sServer %s/%s is disabled",
561 s->state & SRV_BACKUP ? "Backup " : "",
562 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100563
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100564 server_status_printf(&trash, s,
565 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? SSP_O_HCHK : 0),
566 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100567
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100568 Warning("%s.\n", trash.str);
569 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100570
571 if (!s->proxy->srv_bck && !s->proxy->srv_act)
572 set_backend_down(s->proxy);
573
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100574 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100575 for(srv = s->tracknext; srv; srv = srv->tracknext)
576 set_server_disabled(srv);
577}
578
579static void set_server_enabled(struct server *s) {
580
581 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100582 int xferred;
583
584 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200585 if (s->proxy->lbprm.set_server_status_up)
586 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100587
588 /* check if we can handle some connections queued at the proxy. We
589 * will take as many as we can handle.
590 */
591 xferred = check_for_pending(s);
592
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100593 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100594
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100595 chunk_appendf(&trash,
596 "Load-balancing on %sServer %s/%s is enabled again",
597 s->state & SRV_BACKUP ? "Backup " : "",
598 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100599
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100600 server_status_printf(&trash, s,
601 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? SSP_O_HCHK : 0),
602 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100603
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100604 Warning("%s.\n", trash.str);
605 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100606
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100607 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100608 for(srv = s->tracknext; srv; srv = srv->tracknext)
609 set_server_enabled(srv);
610}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100612void health_adjust(struct server *s, short status)
613{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100614 int failed;
615 int expire;
616
617 /* return now if observing nor health check is not enabled */
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200618 if (!s->observe || !s->check.task)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100619 return;
620
621 if (s->observe >= HANA_OBS_SIZE)
622 return;
623
Willy Tarreaubb956662013-01-24 00:37:39 +0100624 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100625 return;
626
627 switch (analyze_statuses[status].lr[s->observe - 1]) {
628 case 1:
629 failed = 1;
630 break;
631
632 case 2:
633 failed = 0;
634 break;
635
636 default:
637 return;
638 }
639
640 if (!failed) {
641 /* good: clear consecutive_errors */
642 s->consecutive_errors = 0;
643 return;
644 }
645
646 s->consecutive_errors++;
647
648 if (s->consecutive_errors < s->consecutive_errors_limit)
649 return;
650
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100651 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
652 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100653
654 switch (s->onerror) {
655 case HANA_ONERR_FASTINTER:
656 /* force fastinter - nothing to do here as all modes force it */
657 break;
658
659 case HANA_ONERR_SUDDTH:
660 /* simulate a pre-fatal failed health check */
661 if (s->health > s->rise)
662 s->health = s->rise + 1;
663
664 /* no break - fall through */
665
666 case HANA_ONERR_FAILCHK:
667 /* simulate a failed health check */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100668 set_server_check_status(s, HCHK_STATUS_HANA, trash.str);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100669
670 if (s->health > s->rise) {
671 s->health--; /* still good */
672 s->counters.failed_checks++;
673 }
674 else
675 set_server_down(s);
676
677 break;
678
679 case HANA_ONERR_MARKDWN:
680 /* mark server down */
681 s->health = s->rise;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100682 set_server_check_status(s, HCHK_STATUS_HANA, trash.str);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100683 set_server_down(s);
684
685 break;
686
687 default:
688 /* write a warning? */
689 break;
690 }
691
692 s->consecutive_errors = 0;
693 s->counters.failed_hana++;
694
695 if (s->fastinter) {
696 expire = tick_add(now_ms, MS_TO_TICKS(s->fastinter));
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200697 if (s->check.task->expire > expire)
698 s->check.task->expire = expire;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100699 }
700}
701
Willy Tarreauef781042010-01-27 11:53:01 +0100702static int httpchk_build_status_header(struct server *s, char *buffer)
703{
704 int sv_state;
705 int ratio;
706 int hlen = 0;
707 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
708 "UP %d/%d", "UP",
709 "NOLB %d/%d", "NOLB",
710 "no check" };
711
712 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
713 hlen += 24;
714
715 if (!(s->state & SRV_CHECKED))
716 sv_state = 6; /* should obviously never happen */
717 else if (s->state & SRV_RUNNING) {
718 if (s->health == s->rise + s->fall - 1)
719 sv_state = 3; /* UP */
720 else
721 sv_state = 2; /* going down */
722
723 if (s->state & SRV_GOINGDOWN)
724 sv_state += 2;
725 } else {
726 if (s->health)
727 sv_state = 1; /* going up */
728 else
729 sv_state = 0; /* DOWN */
730 }
731
732 hlen += sprintf(buffer + hlen,
733 srv_hlt_st[sv_state],
734 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
735 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
736
737 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
738 s->proxy->id, s->id,
739 global.node,
740 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
741 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
742 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
743 s->nbpend);
744
745 if ((s->state & SRV_WARMINGUP) &&
746 now.tv_sec < s->last_change + s->slowstart &&
747 now.tv_sec >= s->last_change) {
748 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
749 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
750 }
751
752 buffer[hlen++] = '\r';
753 buffer[hlen++] = '\n';
754
755 return hlen;
756}
757
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758/*
759 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200760 * the connection acknowledgement. If the proxy requires L7 health-checks,
761 * it sends the request. In other cases, it calls set_server_check_status()
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200762 * to set s->check.status, s->check.duration and s->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200764static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765{
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200766 struct server *s = conn->owner;
767 int fd = conn->t.sock.fd;
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200768 struct task *t = s->check.task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200769
Willy Tarreau5a78f362012-11-23 12:47:05 +0100770 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200771 conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200773 if (unlikely(conn->flags & CO_FL_ERROR)) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100774 int skerr, err = errno;
775 socklen_t lskerr = sizeof(skerr);
776
777 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
778 err = skerr;
779
780 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200781 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200782 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200783
Willy Tarreau5a78f362012-11-23 12:47:05 +0100784 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_WR))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200785 return;
Willy Tarreau83749182007-04-15 20:56:27 +0200786
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200787 /* here, we know that the connection is established */
Willy Tarreaud3aac702012-11-23 11:32:12 +0100788 if (!(s->result & SRV_CHK_FAILED)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200790 if (s->check.bo->o) {
Willy Tarreau392e9392012-10-05 14:54:30 +0200791 conn->xprt->snd_buf(conn, s->check.bo, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauf1503172012-09-28 19:39:36 +0200792 if (conn->flags & CO_FL_ERROR) {
793 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
794 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200795 }
Willy Tarreau5a78f362012-11-23 12:47:05 +0100796 if (s->check.bo->o) {
797 goto out_incomplete;
798 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200800
Willy Tarreau5a78f362012-11-23 12:47:05 +0100801 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
802 if (s->proxy->timeout.check) {
803 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
804 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 }
Willy Tarreau5a78f362012-11-23 12:47:05 +0100806 goto out_nowake;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 }
Willy Tarreau83749182007-04-15 20:56:27 +0200808 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200809 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200810 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200811 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreau5a78f362012-11-23 12:47:05 +0100812 out_incomplete:
Willy Tarreau3267d362012-08-17 23:53:56 +0200813 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200814 out_error:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200815 conn->flags |= CO_FL_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200816 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817}
818
819
820/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200821 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200822 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200823 * set_server_check_status() to update s->check.status, s->check.duration
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200824 * and s->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200825
826 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
827 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
828 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
829 * response to an SSL HELLO (the principle is that this is enough to
830 * distinguish between an SSL server and a pure TCP relay). All other cases will
831 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
832 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200834static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835{
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200836 struct server *s = conn->owner;
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200837 struct task *t = s->check.task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200838 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100839 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200840 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200841
Willy Tarreaud3aac702012-11-23 11:32:12 +0100842 if (unlikely((s->result & SRV_CHK_FAILED) || (conn->flags & CO_FL_ERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200843 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreaud3aac702012-11-23 11:32:12 +0100844 if (!(s->result & SRV_CHK_FAILED))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200845 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200846
Willy Tarreau83749182007-04-15 20:56:27 +0200847 goto out_wakeup;
848 }
849
Willy Tarreau5a78f362012-11-23 12:47:05 +0100850 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_RD))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200851 return;
852
Willy Tarreau83749182007-04-15 20:56:27 +0200853 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
854 * but the connection was closed on the remote end. Fortunately, recv still
855 * works correctly and we don't need to do the getsockopt() on linux.
856 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000857
858 /* Set buffer to point to the end of the data already read, and check
859 * that there is free space remaining. If the buffer is full, proceed
860 * with running the checks without attempting another socket read.
861 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000862
Willy Tarreau03938182010-03-17 21:52:07 +0100863 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000864
Willy Tarreau392e9392012-10-05 14:54:30 +0200865 conn->xprt->rcv_buf(conn, s->check.bi, s->check.bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200866 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100867 done = 1;
Willy Tarreauf1503172012-09-28 19:39:36 +0200868 if ((conn->flags & CO_FL_ERROR) && !s->check.bi->i) {
869 /* Report network errors only if we got no other data. Otherwise
870 * we'll let the upper layers decide whether the response is OK
871 * or not. It is very common that an RST sent by the server is
872 * reported as an error just after the last data chunk.
873 */
Willy Tarreaud3aac702012-11-23 11:32:12 +0100874 if (!(s->result & SRV_CHK_FAILED))
Willy Tarreauc1a07962010-03-16 20:55:43 +0100875 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
876 goto out_wakeup;
877 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878 }
879
Willy Tarreau03938182010-03-17 21:52:07 +0100880 /* Intermediate or complete response received.
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200881 * Terminate string in check.bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100882 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200883 if (s->check.bi->i < s->check.bi->size)
884 s->check.bi->data[s->check.bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100885 else {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200886 s->check.bi->data[s->check.bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100887 done = 1; /* buffer full, don't wait for more data */
888 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200889
Nick Chalk57b1bf72010-03-16 15:50:46 +0000890 /* Run the checks... */
Willy Tarreau1620ec32011-08-06 17:05:02 +0200891 switch (s->proxy->options2 & PR_O2_CHK_ANY) {
892 case PR_O2_HTTP_CHK:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200893 if (!done && s->check.bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100894 goto wait_more_data;
895
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100896 /* Check if the server speaks HTTP 1.X */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200897 if ((s->check.bi->i < strlen("HTTP/1.0 000\r")) ||
898 (memcmp(s->check.bi->data, "HTTP/1.", 7) != 0 ||
899 (*(s->check.bi->data + 12) != ' ' && *(s->check.bi->data + 12) != '\r')) ||
900 !isdigit((unsigned char) *(s->check.bi->data + 9)) || !isdigit((unsigned char) *(s->check.bi->data + 10)) ||
901 !isdigit((unsigned char) *(s->check.bi->data + 11))) {
902 cut_crlf(s->check.bi->data);
903 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check.bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200904
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100905 goto out_wakeup;
906 }
907
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200908 s->check.code = str2uic(s->check.bi->data + 9);
909 desc = ltrim(s->check.bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000910
Willy Tarreaubd741542010-03-16 18:46:54 +0100911 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200912 (s->state & SRV_RUNNING) && (s->check.code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000913 /* 404 may be accepted as "stopping" only if the server was up */
914 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200915 set_server_check_status(s, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000916 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100917 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
918 /* Run content verification check... We know we have at least 13 chars */
919 if (!httpchk_expect(s, done))
920 goto wait_more_data;
921 }
922 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200923 else if (*(s->check.bi->data + 9) == '2' || *(s->check.bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100924 cut_crlf(desc);
925 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
926 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000927 else {
928 cut_crlf(desc);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200929 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000930 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200931 break;
932
933 case PR_O2_SSL3_CHK:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200934 if (!done && s->check.bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100935 goto wait_more_data;
936
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100937 /* Check for SSLv3 alert or handshake */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200938 if ((s->check.bi->i >= 5) && (*s->check.bi->data == 0x15 || *s->check.bi->data == 0x16))
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200939 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200940 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200941 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200942 break;
943
944 case PR_O2_SMTP_CHK:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200945 if (!done && s->check.bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100946 goto wait_more_data;
947
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200948 /* Check if the server speaks SMTP */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200949 if ((s->check.bi->i < strlen("000\r")) ||
950 (*(s->check.bi->data + 3) != ' ' && *(s->check.bi->data + 3) != '\r') ||
951 !isdigit((unsigned char) *s->check.bi->data) || !isdigit((unsigned char) *(s->check.bi->data + 1)) ||
952 !isdigit((unsigned char) *(s->check.bi->data + 2))) {
953 cut_crlf(s->check.bi->data);
954 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check.bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200955
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200956 goto out_wakeup;
957 }
958
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200959 s->check.code = str2uic(s->check.bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200960
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200961 desc = ltrim(s->check.bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200962 cut_crlf(desc);
963
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100964 /* Check for SMTP code 2xx (should be 250) */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +0200965 if (*s->check.bi->data == '2')
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200966 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200967 else
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200968 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200969 break;
970
Simon Hormana2b9dad2013-02-12 10:45:54 +0900971 case PR_O2_LB_AGENT_CHK: {
972 short status = HCHK_STATUS_L7RSP;
973 const char *desc = "Unknown feedback string";
974 const char *down_cmd = NULL;
975
976 if (!done)
977 goto wait_more_data;
978
979 cut_crlf(s->check.bi->data);
980
981 if (strchr(s->check.bi->data, '%')) {
982 desc = server_parse_weight_change_request(s, s->check.bi->data);
983 if (!desc) {
984 status = HCHK_STATUS_L7OKD;
985 desc = s->check.bi->data;
986 }
987 } else if (!strcasecmp(s->check.bi->data, "drain")) {
988 desc = server_parse_weight_change_request(s, "0%");
989 if (!desc) {
990 desc = "drain";
991 status = HCHK_STATUS_L7OKD;
992 }
993 } else if (!strncasecmp(s->check.bi->data, "down", strlen("down"))) {
994 down_cmd = "down";
995 } else if (!strncasecmp(s->check.bi->data, "stopped", strlen("stopped"))) {
996 down_cmd = "stopped";
997 } else if (!strncasecmp(s->check.bi->data, "fail", strlen("fail"))) {
998 down_cmd = "fail";
999 }
1000
1001 if (down_cmd) {
1002 const char *end = s->check.bi->data + strlen(down_cmd);
1003 /*
1004 * The command keyword must terminated the string or
1005 * be followed by a blank.
1006 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001007 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001008 status = HCHK_STATUS_L7STS;
1009 /* Skip over leading blanks */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001010 while (end[0] != '\0' && (end[0] == ' ' || end[0] == '\t'))
Simon Hormana2b9dad2013-02-12 10:45:54 +09001011 end++;
1012 desc = end;
1013 }
1014 }
1015
1016 set_server_check_status(s, status, desc);
1017 break;
1018 }
1019
Willy Tarreau1620ec32011-08-06 17:05:02 +02001020 case PR_O2_PGSQL_CHK:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001021 if (!done && s->check.bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001022 goto wait_more_data;
1023
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001024 if (s->check.bi->data[0] == 'R') {
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001025 set_server_check_status(s, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
1026 }
1027 else {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001028 if ((s->check.bi->data[0] == 'E') && (s->check.bi->data[5]!=0) && (s->check.bi->data[6]!=0))
1029 desc = &s->check.bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001030 else
1031 desc = "PostgreSQL unknown error";
1032
1033 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1034 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001035 break;
1036
1037 case PR_O2_REDIS_CHK:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001038 if (!done && s->check.bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001039 goto wait_more_data;
1040
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001041 if (strcmp(s->check.bi->data, "+PONG\r\n") == 0) {
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001042 set_server_check_status(s, HCHK_STATUS_L7OKD, "Redis server is ok");
1043 }
1044 else {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001045 set_server_check_status(s, HCHK_STATUS_L7STS, s->check.bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001046 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001047 break;
1048
1049 case PR_O2_MYSQL_CHK:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001050 if (!done && s->check.bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001051 goto wait_more_data;
1052
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001053 if (s->proxy->check_len == 0) { // old mode
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001054 if (*(s->check.bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001055 /* We set the MySQL Version in description for information purpose
1056 * FIXME : it can be cool to use MySQL Version for other purpose,
1057 * like mark as down old MySQL server.
1058 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001059 if (s->check.bi->i > 51) {
1060 desc = ltrim(s->check.bi->data + 5, ' ');
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001061 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1062 }
1063 else {
1064 if (!done)
1065 goto wait_more_data;
1066 /* it seems we have a OK packet but without a valid length,
1067 * it must be a protocol error
1068 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001069 set_server_check_status(s, HCHK_STATUS_L7RSP, s->check.bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001070 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001071 }
1072 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001073 /* An error message is attached in the Error packet */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001074 desc = ltrim(s->check.bi->data + 7, ' ');
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001075 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1076 }
1077 } else {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001078 unsigned int first_packet_len = ((unsigned int) *s->check.bi->data) +
1079 (((unsigned int) *(s->check.bi->data + 1)) << 8) +
1080 (((unsigned int) *(s->check.bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001081
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001082 if (s->check.bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001083 /* MySQL Error packet always begin with field_count = 0xff */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001084 if (*(s->check.bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001085 /* We have only one MySQL packet and it is a Handshake Initialization packet
1086 * but we need to have a second packet to know if it is alright
1087 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001088 if (!done && s->check.bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001089 goto wait_more_data;
1090 }
1091 else {
1092 /* We have only one packet and it is an Error packet,
1093 * an error message is attached, so we can display it
1094 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001095 desc = &s->check.bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001096 //Warning("onlyoneERR: %s\n", desc);
1097 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1098 }
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001099 } else if (s->check.bi->i > first_packet_len + 4) {
1100 unsigned int second_packet_len = ((unsigned int) *(s->check.bi->data + first_packet_len + 4)) +
1101 (((unsigned int) *(s->check.bi->data + first_packet_len + 5)) << 8) +
1102 (((unsigned int) *(s->check.bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001103
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001104 if (s->check.bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001105 /* We have 2 packets and that's good */
1106 /* Check if the second packet is a MySQL Error packet or not */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001107 if (*(s->check.bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001108 /* No error packet */
1109 /* We set the MySQL Version in description for information purpose */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001110 desc = &s->check.bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001111 //Warning("2packetOK: %s\n", desc);
1112 set_server_check_status(s, HCHK_STATUS_L7OKD, desc);
1113 }
1114 else {
1115 /* An error message is attached in the Error packet
1116 * so we can display it ! :)
1117 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001118 desc = &s->check.bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001119 //Warning("2packetERR: %s\n", desc);
1120 set_server_check_status(s, HCHK_STATUS_L7STS, desc);
1121 }
1122 }
1123 }
1124 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001125 if (!done)
1126 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001127 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001128 * it must be a protocol error
1129 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001130 desc = &s->check.bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001131 //Warning("protoerr: %s\n", desc);
1132 set_server_check_status(s, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001133 }
1134 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001135 break;
1136
1137 case PR_O2_LDAP_CHK:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001138 if (!done && s->check.bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001139 goto wait_more_data;
1140
1141 /* Check if the server speaks LDAP (ASN.1/BER)
1142 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1143 * http://tools.ietf.org/html/rfc4511
1144 */
1145
1146 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1147 * LDAPMessage: 0x30: SEQUENCE
1148 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001149 if ((s->check.bi->i < 14) || (*(s->check.bi->data) != '\x30')) {
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001150 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1151 }
1152 else {
1153 /* size of LDAPMessage */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001154 msglen = (*(s->check.bi->data + 1) & 0x80) ? (*(s->check.bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001155
1156 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1157 * messageID: 0x02 0x01 0x01: INTEGER 1
1158 * protocolOp: 0x61: bindResponse
1159 */
1160 if ((msglen > 2) ||
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001161 (memcmp(s->check.bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001162 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1163
1164 goto out_wakeup;
1165 }
1166
1167 /* size of bindResponse */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001168 msglen += (*(s->check.bi->data + msglen + 6) & 0x80) ? (*(s->check.bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001169
1170 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1171 * ldapResult: 0x0a 0x01: ENUMERATION
1172 */
1173 if ((msglen > 4) ||
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001174 (memcmp(s->check.bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001175 set_server_check_status(s, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
1176
1177 goto out_wakeup;
1178 }
1179
1180 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1181 * resultCode
1182 */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001183 s->check.code = *(s->check.bi->data + msglen + 9);
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001184 if (s->check.code) {
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001185 set_server_check_status(s, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
1186 } else {
1187 set_server_check_status(s, HCHK_STATUS_L7OKD, "Success");
1188 }
1189 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001190 break;
1191
1192 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001193 /* other checks are valid if the connection succeeded anyway */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001194 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001195 break;
1196 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001197
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001198 out_wakeup:
Willy Tarreaud3aac702012-11-23 11:32:12 +01001199 if (s->result & SRV_CHK_FAILED)
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001200 conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001201
Nick Chalk57b1bf72010-03-16 15:50:46 +00001202 /* Reset the check buffer... */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001203 *s->check.bi->data = '\0';
1204 s->check.bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001205
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001206 /* Close the connection... We absolutely want to perform a hard close
1207 * and reset the connection if some data are pending, otherwise we end
1208 * up with many TIME_WAITs and eat all the source port range quickly.
1209 * To avoid sending RSTs all the time, we first try to drain pending
1210 * data.
1211 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001212 if (conn->xprt && conn->xprt->shutw)
1213 conn->xprt->shutw(conn, 0);
Willy Tarreaucfd97c62012-11-23 17:35:59 +01001214 if (conn->ctrl) {
1215 if (!(conn->flags & CO_FL_WAIT_RD))
1216 recv(conn->t.sock.fd, trash.str, trash.size, MSG_NOSIGNAL|MSG_DONTWAIT);
1217 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1218 (struct linger *) &nolinger, sizeof(struct linger));
1219 }
Willy Tarreaua522f802012-11-23 08:56:35 +01001220 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001221 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001222 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001223
1224 wait_more_data:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001225 __conn_data_poll_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001226}
1227
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001228/*
1229 * This function is used only for server health-checks. It handles connection
1230 * status updates including errors. If necessary, it wakes the check task up.
1231 * It always returns 0.
1232 */
1233static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001234{
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001235 struct server *s = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001236
Willy Tarreau6c560da2012-11-24 11:14:45 +01001237 if (unlikely(conn->flags & CO_FL_ERROR)) {
1238 /* Note that we might as well have been woken up by a handshake handler */
Willy Tarreauc5c61fc2012-12-30 01:44:24 +01001239 if (s->result == SRV_CHK_UNKNOWN)
1240 s->result |= SRV_CHK_FAILED;
Willy Tarreau6c560da2012-11-24 11:14:45 +01001241 __conn_data_stop_both(conn);
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001242 task_wakeup(s->check.task, TASK_WOKEN_IO);
Willy Tarreau6c560da2012-11-24 11:14:45 +01001243 }
Willy Tarreau20bea422012-07-06 12:00:49 +02001244
Willy Tarreau2b199c92012-11-23 17:32:21 +01001245 if (s->result & (SRV_CHK_FAILED|SRV_CHK_PASSED))
1246 conn_full_close(conn);
Willy Tarreau3267d362012-08-17 23:53:56 +02001247 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001248}
1249
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001250struct data_cb check_conn_cb = {
1251 .recv = event_srv_chk_r,
1252 .send = event_srv_chk_w,
1253 .wake = wake_srv_chk,
1254};
1255
Willy Tarreaubaaee002006-06-26 02:48:02 +02001256/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001257 * updates the server's weight during a warmup stage. Once the final weight is
1258 * reached, the task automatically stops. Note that any server status change
1259 * must have updated s->last_change accordingly.
1260 */
1261static struct task *server_warmup(struct task *t)
1262{
1263 struct server *s = t->context;
1264
1265 /* by default, plan on stopping the task */
1266 t->expire = TICK_ETERNITY;
1267 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1268 return t;
1269
1270 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1271 /* go to full throttle if the slowstart interval is reached */
1272 s->state &= ~SRV_WARMINGUP;
1273 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1274 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1275 if (s->proxy->lbprm.update_server_eweight)
1276 s->proxy->lbprm.update_server_eweight(s);
1277 }
1278 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1279 /* for dynamic algorithms, let's slowly update the weight */
1280 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1281 s->slowstart - 1) / s->slowstart;
1282 s->eweight *= s->uweight;
1283 if (s->proxy->lbprm.update_server_eweight)
1284 s->proxy->lbprm.update_server_eweight(s);
1285 }
1286 /* Note that static algorithms are already running at full throttle */
1287
1288 /* probably that we can refill this server with a bit more connections */
1289 check_for_pending(s);
1290
1291 /* get back there in 1 second or 1/20th of the slowstart interval,
1292 * whichever is greater, resulting in small 5% steps.
1293 */
1294 if (s->state & SRV_WARMINGUP)
1295 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1296 return t;
1297}
1298
1299/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300 * manages a server health-check. Returns
1301 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1302 */
Simon Horman63a4a822012-03-19 07:24:41 +09001303static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001304{
1305 struct server *s = t->context;
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001306 struct connection *conn = s->check.conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001307 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001308 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001309 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001310
Willy Tarreau5a78f362012-11-23 12:47:05 +01001311 if (!(s->state & SRV_CHK_RUNNING)) {
1312 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001313 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001314 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315
1316 /* we don't send any health-checks when the proxy is stopped or when
1317 * the server should not be checked.
1318 */
Willy Tarreau5a78f362012-11-23 12:47:05 +01001319 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN))
1320 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001321
1322 /* we'll initiate a new check */
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001323 set_server_check_status(s, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001324
Willy Tarreau5a78f362012-11-23 12:47:05 +01001325 s->state |= SRV_CHK_RUNNING;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001326 s->check.bi->p = s->check.bi->data;
1327 s->check.bi->i = 0;
1328 s->check.bo->p = s->check.bo->data;
1329 s->check.bo->o = 0;
1330
1331 /* prepare the check buffer */
1332 if (s->proxy->options2 & PR_O2_CHK_ANY) {
1333 bo_putblk(s->check.bo, s->proxy->check_req, s->proxy->check_len);
1334
1335 /* we want to check if this host replies to HTTP or SSLv3 requests
1336 * so we'll send the request, and won't wake the checker up now.
1337 */
1338 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK) {
1339 /* SSL requires that we put Unix time in the request */
1340 int gmt_time = htonl(date.tv_sec);
1341 memcpy(s->check.bo->data + 11, &gmt_time, 4);
1342 }
1343 else if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK) {
1344 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001345 bo_putblk(s->check.bo, trash.str, httpchk_build_status_header(s, trash.str));
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001346 bo_putstr(s->check.bo, "\r\n");
1347 *s->check.bo->p = '\0'; /* to make gdb output easier to read */
1348 }
1349 }
1350
1351 /* prepare a new connection */
Willy Tarreaub63b5962012-11-23 16:22:08 +01001352 conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01001353 conn->err_code = CO_ER_NONE;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001354 conn->target = &s->obj_type;
Willy Tarreauf4288ee2012-09-28 18:13:10 +02001355 conn_prepare(conn, &check_conn_cb, s->check.proto, s->check.xprt, s);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001356
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001357 /* no client address */
1358 clear_addr(&conn->addr.from);
1359
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001360 if (is_addr(&s->check.addr))
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001361 /* we'll connect to the check addr specified on the server */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001362 conn->addr.to = s->check.addr;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001363 else
1364 /* we'll connect to the addr on the server */
1365 conn->addr.to = s->addr;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001366
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001367 set_host_port(&conn->addr.to, s->check.port);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001368
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001369 /* It can return one of :
1370 * - SN_ERR_NONE if everything's OK
1371 * - SN_ERR_SRVTO if there are no more servers
1372 * - SN_ERR_SRVCL if the connection was refused by the server
1373 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1374 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1375 * - SN_ERR_INTERNAL for any other purely internal errors
1376 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001377 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001378 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001379 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001380 ret = SN_ERR_INTERNAL;
1381 if (s->check.proto->connect)
1382 ret = s->check.proto->connect(conn, s->proxy->options2 & PR_O2_CHK_ANY,
1383 s->check.send_proxy ? 1 : (s->proxy->options2 & PR_O2_CHK_ANY) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001384 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau6c16adc2012-10-05 00:04:16 +02001385 if (s->check.send_proxy)
1386 conn->flags |= CO_FL_LOCAL_SPROXY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001387
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001388 switch (ret) {
1389 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001390 /* we allow up to min(inter, timeout.connect) for a connection
1391 * to establish but only when timeout.check is set
1392 * as it may be to short for a full check otherwise
1393 */
1394 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001395
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001396 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1397 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1398 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399 }
Willy Tarreaudb3b4a22012-11-23 16:32:33 +01001400 conn_data_poll_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau5a78f362012-11-23 12:47:05 +01001401 goto reschedule;
1402
1403 case SN_ERR_SRVTO: /* ETIMEDOUT */
1404 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
1405 set_server_check_status(s, HCHK_STATUS_L4CON, strerror(errno));
1406 break;
1407 case SN_ERR_PRXCOND:
1408 case SN_ERR_RESOURCE:
1409 case SN_ERR_INTERNAL:
1410 set_server_check_status(s, HCHK_STATUS_SOCKERR, NULL);
1411 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412 }
1413
Willy Tarreau5a78f362012-11-23 12:47:05 +01001414 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001415
Willy Tarreau5a78f362012-11-23 12:47:05 +01001416 s->state &= ~SRV_CHK_RUNNING;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417 if (s->health > s->rise) {
1418 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001419 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420 }
1421 else
1422 set_server_down(s);
1423
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001424 /* we allow up to min(inter, timeout.connect) for a connection
1425 * to establish but only when timeout.check is set
1426 * as it may be to short for a full check otherwise
1427 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001428 while (tick_is_expired(t->expire, now_ms)) {
1429 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001430
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001431 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1432 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001433
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001434 if (s->proxy->timeout.check)
1435 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001436 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001437 }
1438 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001439 /* there was a test running.
1440 * First, let's check whether there was an uncaught error,
1441 * which can happen on connect timeout or error.
1442 */
1443 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001444 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
1445 /* L4 not established (yet) */
Willy Tarreauf1503172012-09-28 19:39:36 +02001446 if (conn->flags & CO_FL_ERROR)
1447 set_server_check_status(s, HCHK_STATUS_L4CON, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001448 else if (expired)
Willy Tarreauf1503172012-09-28 19:39:36 +02001449 set_server_check_status(s, HCHK_STATUS_L4TOUT, NULL);
1450 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001451 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001452 /* L6 not established (yet) */
Willy Tarreauf1503172012-09-28 19:39:36 +02001453 if (conn->flags & CO_FL_ERROR)
1454 set_server_check_status(s, HCHK_STATUS_L6RSP, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001455 else if (expired)
1456 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
1457 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001458 else if (!(s->proxy->options2 & PR_O2_CHK_ANY)) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001459 /* good connection is enough for pure TCP check */
1460 if (s->check.use_ssl)
1461 set_server_check_status(s, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001462 else
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001463 set_server_check_status(s, HCHK_STATUS_L4OK, NULL);
1464 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001465 else if (expired) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001466 /* connection established but expired check */
1467 if ((s->proxy->options2 & PR_O2_CHK_ANY) == PR_O2_SSL3_CHK)
Willy Tarreauf1503172012-09-28 19:39:36 +02001468 set_server_check_status(s, HCHK_STATUS_L6TOUT, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001469 else /* HTTP, SMTP, ... */
1470 set_server_check_status(s, HCHK_STATUS_L7TOUT, NULL);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001471
Willy Tarreauf1503172012-09-28 19:39:36 +02001472 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001473 else
1474 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001475 }
1476
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001477 /* check complete or aborted */
1478
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001479 if (conn->xprt) {
1480 /* The check was aborted and the connection was not yet closed.
1481 * This can happen upon timeout, or when an external event such
1482 * as a failed response coupled with "observe layer7" caused the
1483 * server state to be suddenly changed.
1484 */
1485 if (conn->ctrl)
1486 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1487 (struct linger *) &nolinger, sizeof(struct linger));
1488 conn_full_close(conn);
1489 }
1490
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001491 if (s->result & SRV_CHK_FAILED) { /* a failure or timeout detected */
1492 if (s->health > s->rise) {
1493 s->health--; /* still good */
1494 s->counters.failed_checks++;
1495 }
1496 else
1497 set_server_down(s);
1498 }
1499 else { /* check was OK */
Willy Tarreau48494c02007-11-30 10:41:39 +01001500 /* we may have to add/remove this server from the LB group */
1501 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001502 if ((s->state & SRV_GOINGDOWN) && !(s->result & SRV_CHK_DISABLE))
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001503 set_server_enabled(s);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001504 else if (!(s->state & SRV_GOINGDOWN) && (s->result & SRV_CHK_DISABLE))
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001505 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +01001506 }
1507
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001508 if (s->health < s->rise + s->fall - 1) {
1509 s->health++; /* was bad, stays for a while */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001510 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001511 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001512 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001513 s->state &= ~SRV_CHK_RUNNING;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001514
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001515 rv = 0;
1516 if (global.spread_checks > 0) {
1517 rv = srv_getinter(s) * global.spread_checks / 100;
1518 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001519 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001520 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001521 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001522
1523 reschedule:
1524 while (tick_is_expired(t->expire, now_ms))
1525 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001526 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001527 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001528}
1529
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001530/*
1531 * Start health-check.
1532 * Returns 0 if OK, -1 if error, and prints the error in this case.
1533 */
1534int start_checks() {
1535
1536 struct proxy *px;
1537 struct server *s;
1538 struct task *t;
1539 int nbchk=0, mininter=0, srvpos=0;
1540
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001541 /* 1- count the checkers to run simultaneously.
1542 * We also determine the minimum interval among all of those which
1543 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1544 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001545 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001546 * too short an interval for all others.
1547 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001548 for (px = proxy; px; px = px->next) {
1549 for (s = px->srv; s; s = s->next) {
1550 if (!(s->state & SRV_CHECKED))
1551 continue;
1552
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001553 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
1554 (!mininter || mininter > srv_getinter(s)))
1555 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001556
1557 nbchk++;
1558 }
1559 }
1560
1561 if (!nbchk)
1562 return 0;
1563
1564 srand((unsigned)time(NULL));
1565
1566 /*
1567 * 2- start them as far as possible from each others. For this, we will
1568 * start them after their interval set to the min interval divided by
1569 * the number of servers, weighted by the server's position in the list.
1570 */
1571 for (px = proxy; px; px = px->next) {
1572 for (s = px->srv; s; s = s->next) {
Willy Tarreau2e993902011-10-31 11:53:20 +01001573 if (s->slowstart) {
1574 if ((t = task_new()) == NULL) {
1575 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1576 return -1;
1577 }
1578 /* We need a warmup task that will be called when the server
1579 * state switches from down to up.
1580 */
1581 s->warmup = t;
1582 t->process = server_warmup;
1583 t->context = s;
1584 t->expire = TICK_ETERNITY;
1585 }
1586
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001587 if (!(s->state & SRV_CHECKED))
1588 continue;
1589
Willy Tarreau2e993902011-10-31 11:53:20 +01001590 /* one task for the checks */
Willy Tarreaua4613182009-03-21 18:13:21 +01001591 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001592 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1593 return -1;
1594 }
1595
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001596 s->check.task = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001597 t->process = process_chk;
1598 t->context = s;
1599
1600 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001601 t->expire = tick_add(now_ms,
1602 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
1603 mininter : srv_getinter(s)) * srvpos / nbchk));
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001604 s->check.start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001605 task_queue(t);
1606
1607 srvpos++;
1608 }
1609 }
1610 return 0;
1611}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001612
1613/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001614 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001615 * The buffer MUST be terminated by a null byte before calling this function.
1616 * Sets server status appropriately. The caller is responsible for ensuring
1617 * that the buffer contains at least 13 characters. If <done> is zero, we may
1618 * return 0 to indicate that data is required to decide of a match.
1619 */
1620static int httpchk_expect(struct server *s, int done)
1621{
1622 static char status_msg[] = "HTTP status check returned code <000>";
1623 char status_code[] = "000";
1624 char *contentptr;
1625 int crlf;
1626 int ret;
1627
1628 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1629 case PR_O2_EXP_STS:
1630 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001631 memcpy(status_code, s->check.bi->data + 9, 3);
1632 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001633
1634 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1635 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1636 else
1637 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1638
1639 /* we necessarily have the response, so there are no partial failures */
1640 if (s->proxy->options2 & PR_O2_EXP_INV)
1641 ret = !ret;
1642
1643 set_server_check_status(s, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
1644 break;
1645
1646 case PR_O2_EXP_STR:
1647 case PR_O2_EXP_RSTR:
1648 /* very simple response parser: ignore CR and only count consecutive LFs,
1649 * stop with contentptr pointing to first char after the double CRLF or
1650 * to '\0' if crlf < 2.
1651 */
1652 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001653 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001654 if (crlf >= 2)
1655 break;
1656 if (*contentptr == '\r')
1657 continue;
1658 else if (*contentptr == '\n')
1659 crlf++;
1660 else
1661 crlf = 0;
1662 }
1663
1664 /* Check that response contains a body... */
1665 if (crlf < 2) {
1666 if (!done)
1667 return 0;
1668
1669 set_server_check_status(s, HCHK_STATUS_L7RSP,
1670 "HTTP content check could not find a response body");
1671 return 1;
1672 }
1673
1674 /* Check that response body is not empty... */
1675 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001676 if (!done)
1677 return 0;
1678
Willy Tarreaubd741542010-03-16 18:46:54 +01001679 set_server_check_status(s, HCHK_STATUS_L7RSP,
1680 "HTTP content check found empty response body");
1681 return 1;
1682 }
1683
1684 /* Check the response content against the supplied string
1685 * or regex... */
1686 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1687 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1688 else
1689 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1690
1691 /* if we don't match, we may need to wait more */
1692 if (!ret && !done)
1693 return 0;
1694
1695 if (ret) {
1696 /* content matched */
1697 if (s->proxy->options2 & PR_O2_EXP_INV)
1698 set_server_check_status(s, HCHK_STATUS_L7RSP,
1699 "HTTP check matched unwanted content");
1700 else
1701 set_server_check_status(s, HCHK_STATUS_L7OKD,
1702 "HTTP content check matched");
1703 }
1704 else {
1705 if (s->proxy->options2 & PR_O2_EXP_INV)
1706 set_server_check_status(s, HCHK_STATUS_L7OKD,
1707 "HTTP check did not match unwanted content");
1708 else
1709 set_server_check_status(s, HCHK_STATUS_L7RSP,
1710 "HTTP content check did not match");
1711 }
1712 break;
1713 }
1714 return 1;
1715}
1716
1717/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001718 * Local variables:
1719 * c-indent-level: 8
1720 * c-basic-offset: 8
1721 * End:
1722 */