blob: a2ff23795e00996e14285a1e5bec3a10316f0bad [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);
55
Simon Horman63a4a822012-03-19 07:24:41 +090056static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020057 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
58 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
59 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020060
Willy Tarreaud3aac702012-11-23 11:32:12 +010061 [HCHK_STATUS_HANA] = { SRV_CHK_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010062
Willy Tarreaud3aac702012-11-23 11:32:12 +010063 [HCHK_STATUS_SOCKERR] = { SRV_CHK_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020064
Willy Tarreaud3aac702012-11-23 11:32:12 +010065 [HCHK_STATUS_L4OK] = { SRV_CHK_PASSED, "L4OK", "Layer4 check passed" },
66 [HCHK_STATUS_L4TOUT] = { SRV_CHK_FAILED, "L4TOUT", "Layer4 timeout" },
67 [HCHK_STATUS_L4CON] = { SRV_CHK_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020068
Willy Tarreaud3aac702012-11-23 11:32:12 +010069 [HCHK_STATUS_L6OK] = { SRV_CHK_PASSED, "L6OK", "Layer6 check passed" },
70 [HCHK_STATUS_L6TOUT] = { SRV_CHK_FAILED, "L6TOUT", "Layer6 timeout" },
71 [HCHK_STATUS_L6RSP] = { SRV_CHK_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072
Willy Tarreaud3aac702012-11-23 11:32:12 +010073 [HCHK_STATUS_L7TOUT] = { SRV_CHK_FAILED, "L7TOUT", "Layer7 timeout" },
74 [HCHK_STATUS_L7RSP] = { SRV_CHK_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020076 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020077
Willy Tarreaud3aac702012-11-23 11:32:12 +010078 [HCHK_STATUS_L7OKD] = { SRV_CHK_PASSED, "L7OK", "Layer7 check passed" },
79 [HCHK_STATUS_L7OKCD] = { SRV_CHK_PASSED | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
80 [HCHK_STATUS_L7STS] = { SRV_CHK_FAILED, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020081};
82
Simon Horman63a4a822012-03-19 07:24:41 +090083static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010084 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
85
86 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
87 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
88
89 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
90 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
91 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
92 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
93
94 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
95 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
96 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
97};
98
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099/*
100 * Convert check_status code to description
101 */
102const char *get_check_status_description(short check_status) {
103
104 const char *desc;
105
106 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200107 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200108 else
109 desc = NULL;
110
111 if (desc && *desc)
112 return desc;
113 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200114 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200115}
116
117/*
118 * Convert check_status code to short info
119 */
120const char *get_check_status_info(short check_status) {
121
122 const char *info;
123
124 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200125 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200126 else
127 info = NULL;
128
129 if (info && *info)
130 return info;
131 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200132 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200133}
134
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100135const char *get_analyze_status(short analyze_status) {
136
137 const char *desc;
138
139 if (analyze_status < HANA_STATUS_SIZE)
140 desc = analyze_statuses[analyze_status].desc;
141 else
142 desc = NULL;
143
144 if (desc && *desc)
145 return desc;
146 else
147 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
148}
149
Simon Horman4a741432013-02-23 15:35:38 +0900150static void server_status_printf(struct chunk *msg, struct server *s, struct check *check, int xferred) {
Willy Tarreau44267702011-10-28 15:35:33 +0200151 if (s->track)
Willy Tarreau77804732012-10-29 16:14:26 +0100152 chunk_appendf(msg, " via %s/%s",
Willy Tarreau44267702011-10-28 15:35:33 +0200153 s->track->proxy->id, s->track->id);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200154
Simon Horman4a741432013-02-23 15:35:38 +0900155 if (check) {
156 chunk_appendf(msg, ", reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200157
Simon Horman4a741432013-02-23 15:35:38 +0900158 if (check->status >= HCHK_STATUS_L57DATA)
159 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200160
Simon Horman4a741432013-02-23 15:35:38 +0900161 if (*check->desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200162 struct chunk src;
163
Willy Tarreau77804732012-10-29 16:14:26 +0100164 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200165
Simon Horman4a741432013-02-23 15:35:38 +0900166 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200167 chunk_asciiencode(msg, &src, '"');
168
Willy Tarreau77804732012-10-29 16:14:26 +0100169 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200170 }
171
Simon Horman4a741432013-02-23 15:35:38 +0900172 if (check->duration >= 0)
173 chunk_appendf(msg, ", check duration: %ldms", check->duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200174 }
175
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200176 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100177 if (!(s->state & SRV_RUNNING))
Willy Tarreau77804732012-10-29 16:14:26 +0100178 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100179 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200180 s->proxy->srv_act, s->proxy->srv_bck,
181 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
182 s->cur_sess, xferred, s->nbpend);
183 else
Willy Tarreau77804732012-10-29 16:14:26 +0100184 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100185 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200186 s->proxy->srv_act, s->proxy->srv_bck,
187 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
188 xferred, s->nbpend);
189 }
190}
191
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200192/*
Simon Horman4a741432013-02-23 15:35:38 +0900193 * Set check->status, update check->duration and fill check->result with
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200194 * an adequate SRV_CHK_* value.
195 *
196 * Show information in logs about failed health check if server is UP
197 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200198 */
Simon Horman4a741432013-02-23 15:35:38 +0900199static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100200{
Simon Horman4a741432013-02-23 15:35:38 +0900201 struct server *s = check->server;
202
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200203 if (status == HCHK_STATUS_START) {
Simon Horman4a741432013-02-23 15:35:38 +0900204 check->result = SRV_CHK_UNKNOWN; /* no result yet */
205 check->desc[0] = '\0';
206 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200207 return;
208 }
209
Simon Horman4a741432013-02-23 15:35:38 +0900210 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200211 return;
212
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200213 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900214 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
215 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200216 } else
Simon Horman4a741432013-02-23 15:35:38 +0900217 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200218
Simon Horman4a741432013-02-23 15:35:38 +0900219 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200220 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900221 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200222
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100223 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900224 check->duration = -1;
225 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200226 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900227 check->duration = tv_ms_elapsed(&check->start, &now);
228 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200229 }
230
231 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
Simon Horman4a741432013-02-23 15:35:38 +0900232 (((s->health != 0) && (check->result & SRV_CHK_FAILED)) ||
233 ((s->health != s->rise + s->fall - 1) && (check->result & SRV_CHK_PASSED)) ||
234 ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE)) ||
235 (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE)))) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200236
237 int health, rise, fall, state;
238
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100239 chunk_reset(&trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200240
241 /* FIXME begin: calculate local version of the health/rise/fall/state */
242 health = s->health;
243 rise = s->rise;
244 fall = s->fall;
245 state = s->state;
246
Simon Horman4a741432013-02-23 15:35:38 +0900247 if (check->result & SRV_CHK_FAILED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200248 if (health > rise) {
249 health--; /* still good */
250 } else {
251 if (health == rise)
252 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
253
254 health = 0;
255 }
256 }
257
Simon Horman4a741432013-02-23 15:35:38 +0900258 if (check->result & SRV_CHK_PASSED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200259 if (health < rise + fall - 1) {
260 health++; /* was bad, stays for a while */
261
262 if (health == rise)
263 state |= SRV_RUNNING;
264
265 if (health >= rise)
266 health = rise + fall - 1; /* OK now */
267 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100268
269 /* clear consecutive_errors if observing is enabled */
270 if (s->onerror)
271 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200272 }
273 /* FIXME end: calculate local version of the health/rise/fall/state */
274
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100275 chunk_appendf(&trash,
276 "Health check for %sserver %s/%s %s%s",
277 s->state & SRV_BACKUP ? "backup " : "",
278 s->proxy->id, s->id,
Simon Horman4a741432013-02-23 15:35:38 +0900279 (check->result & SRV_CHK_DISABLE)?"conditionally ":"",
280 (check->result & SRV_CHK_PASSED)?"succeeded":"failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200281
Simon Horman4a741432013-02-23 15:35:38 +0900282 server_status_printf(&trash, s, check, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200283
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100284 chunk_appendf(&trash, ", status: %d/%d %s",
285 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
286 (state & SRV_RUNNING) ? (fall) : (rise),
287 (state & SRV_RUNNING)?"UP":"DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200288
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100289 Warning("%s.\n", trash.str);
290 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200291 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200292}
293
Willy Tarreau48494c02007-11-30 10:41:39 +0100294/* sends a log message when a backend goes down, and also sets last
295 * change date.
296 */
297static void set_backend_down(struct proxy *be)
298{
299 be->last_change = now.tv_sec;
300 be->down_trans++;
301
302 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
303 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
304}
305
306/* Redistribute pending connections when a server goes down. The number of
307 * connections redistributed is returned.
308 */
309static int redistribute_pending(struct server *s)
310{
311 struct pendconn *pc, *pc_bck, *pc_end;
312 int xferred = 0;
313
314 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
315 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100316 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
317 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100318 /* The REDISP option was specified. We will ignore
319 * cookie and force to balance or use the dispatcher.
320 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100321
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100322 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100323 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100324
Willy Tarreau48494c02007-11-30 10:41:39 +0100325 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200326 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100327 xferred++;
328 }
329 }
330 return xferred;
331}
332
333/* Check for pending connections at the backend, and assign some of them to
334 * the server coming up. The server's weight is checked before being assigned
335 * connections it may not be able to handle. The total number of transferred
336 * connections is returned.
337 */
338static int check_for_pending(struct server *s)
339{
340 int xferred;
341
342 if (!s->eweight)
343 return 0;
344
345 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
346 struct session *sess;
347 struct pendconn *p;
348
349 p = pendconn_from_px(s->proxy);
350 if (!p)
351 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100352 p->sess->target = &s->obj_type;
Willy Tarreau48494c02007-11-30 10:41:39 +0100353 sess = p->sess;
354 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200355 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100356 }
357 return xferred;
358}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700360/* Shutdown all connections of a server. The caller must pass a termination
361 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
362 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900363 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700364static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900365{
366 struct session *session, *session_bck;
367
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200368 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
369 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700370 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900371}
372
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700373/* Shutdown all connections of all backup servers of a proxy. The caller must
374 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
375 * the reason for the shutdown.
376 */
377static void shutdown_backup_sessions(struct proxy *px, int why)
378{
379 struct server *srv;
380
381 for (srv = px->srv; srv != NULL; srv = srv->next)
382 if (srv->state & SRV_BACKUP)
383 shutdown_sessions(srv, why);
384}
385
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386/* Sets server <s> down, notifies by all available means, recounts the
387 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200388 * possible to other servers. It automatically recomputes the number of
389 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390 */
Simon Horman4a741432013-02-23 15:35:38 +0900391void set_server_down(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392{
Simon Horman4a741432013-02-23 15:35:38 +0900393 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100394 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 int xferred;
396
Cyril Bontécd19e512010-01-31 22:34:03 +0100397 if (s->state & SRV_MAINTAIN) {
398 s->health = s->rise;
399 }
400
Willy Tarreau44267702011-10-28 15:35:33 +0200401 if (s->health == s->rise || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100402 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200403 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200404
405 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100406 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200407 if (s->proxy->lbprm.set_server_status_down)
408 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900410 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700411 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900412
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413 /* we might have sessions queued on this server and waiting for
414 * a connection. Those which are redispatchable will be queued
415 * to another server or to the proxy itself.
416 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100417 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100418
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100419 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100420
Cyril Bontécd19e512010-01-31 22:34:03 +0100421 if (s->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100422 chunk_appendf(&trash,
423 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
424 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100425 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100426 chunk_appendf(&trash,
427 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
428 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100429
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100430 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900431 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : 0),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100432 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100433 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100434 Warning("%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200435
Willy Tarreau48494c02007-11-30 10:41:39 +0100436 /* we don't send an alert if the server was previously paused */
437 if (srv_was_paused)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100438 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreau48494c02007-11-30 10:41:39 +0100439 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100440 send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200441
Willy Tarreaud64d2252010-10-17 17:16:42 +0200442 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100443 set_backend_down(s->proxy);
444
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200445 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100446
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100447 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100448 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100449 if (! (srv->state & SRV_MAINTAIN))
450 /* Only notify tracking servers that are not already in maintenance. */
Simon Horman4a741432013-02-23 15:35:38 +0900451 set_server_down(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100453
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454 s->health = 0; /* failure */
455}
456
Simon Horman4a741432013-02-23 15:35:38 +0900457void set_server_up(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100458
Simon Horman4a741432013-02-23 15:35:38 +0900459 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100460 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,
Simon Horman4a741432013-02-23 15:35:38 +0900523 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100524 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. */
Simon Horman4a741432013-02-23 15:35:38 +0900534 set_server_up(check);
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
Simon Horman4a741432013-02-23 15:35:38 +0900542static void set_server_disabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100543
Simon Horman4a741432013-02-23 15:35:38 +0900544 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100545 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100546 int xferred;
547
548 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200549 if (s->proxy->lbprm.set_server_status_down)
550 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100551
552 /* we might have sessions queued on this server and waiting for
553 * a connection. Those which are redispatchable will be queued
554 * to another server or to the proxy itself.
555 */
556 xferred = redistribute_pending(s);
557
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100558 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100559
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100560 chunk_appendf(&trash,
561 "Load-balancing on %sServer %s/%s is disabled",
562 s->state & SRV_BACKUP ? "Backup " : "",
563 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100564
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100565 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900566 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100567 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100568
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100569 Warning("%s.\n", trash.str);
570 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100571
572 if (!s->proxy->srv_bck && !s->proxy->srv_act)
573 set_backend_down(s->proxy);
574
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100575 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100576 for(srv = s->tracknext; srv; srv = srv->tracknext)
Simon Horman4a741432013-02-23 15:35:38 +0900577 set_server_disabled(check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100578}
579
Simon Horman4a741432013-02-23 15:35:38 +0900580static void set_server_enabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100581
Simon Horman4a741432013-02-23 15:35:38 +0900582 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100583 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100584 int xferred;
585
586 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200587 if (s->proxy->lbprm.set_server_status_up)
588 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100589
590 /* check if we can handle some connections queued at the proxy. We
591 * will take as many as we can handle.
592 */
593 xferred = check_for_pending(s);
594
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100595 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100596
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100597 chunk_appendf(&trash,
598 "Load-balancing on %sServer %s/%s is enabled again",
599 s->state & SRV_BACKUP ? "Backup " : "",
600 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100601
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100602 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900603 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100604 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100605
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100606 Warning("%s.\n", trash.str);
607 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100608
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100609 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100610 for(srv = s->tracknext; srv; srv = srv->tracknext)
Simon Horman4a741432013-02-23 15:35:38 +0900611 set_server_enabled(check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100612}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100614void health_adjust(struct server *s, short status)
615{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100616 int failed;
617 int expire;
618
619 /* return now if observing nor health check is not enabled */
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200620 if (!s->observe || !s->check.task)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100621 return;
622
623 if (s->observe >= HANA_OBS_SIZE)
624 return;
625
Willy Tarreaubb956662013-01-24 00:37:39 +0100626 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100627 return;
628
629 switch (analyze_statuses[status].lr[s->observe - 1]) {
630 case 1:
631 failed = 1;
632 break;
633
634 case 2:
635 failed = 0;
636 break;
637
638 default:
639 return;
640 }
641
642 if (!failed) {
643 /* good: clear consecutive_errors */
644 s->consecutive_errors = 0;
645 return;
646 }
647
648 s->consecutive_errors++;
649
650 if (s->consecutive_errors < s->consecutive_errors_limit)
651 return;
652
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100653 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
654 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100655
656 switch (s->onerror) {
657 case HANA_ONERR_FASTINTER:
658 /* force fastinter - nothing to do here as all modes force it */
659 break;
660
661 case HANA_ONERR_SUDDTH:
662 /* simulate a pre-fatal failed health check */
663 if (s->health > s->rise)
664 s->health = s->rise + 1;
665
666 /* no break - fall through */
667
668 case HANA_ONERR_FAILCHK:
669 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900670 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100671
672 if (s->health > s->rise) {
673 s->health--; /* still good */
674 s->counters.failed_checks++;
675 }
676 else
Simon Horman4a741432013-02-23 15:35:38 +0900677 set_server_down(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100678
679 break;
680
681 case HANA_ONERR_MARKDWN:
682 /* mark server down */
683 s->health = s->rise;
Simon Horman4a741432013-02-23 15:35:38 +0900684 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
685 set_server_down(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100686
687 break;
688
689 default:
690 /* write a warning? */
691 break;
692 }
693
694 s->consecutive_errors = 0;
695 s->counters.failed_hana++;
696
Simon Horman66183002013-02-23 10:16:43 +0900697 if (s->check.fastinter) {
698 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300699 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200700 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300701 /* requeue check task with new expire */
702 task_queue(s->check.task);
703 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100704 }
705}
706
Willy Tarreauef781042010-01-27 11:53:01 +0100707static int httpchk_build_status_header(struct server *s, char *buffer)
708{
709 int sv_state;
710 int ratio;
711 int hlen = 0;
712 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
713 "UP %d/%d", "UP",
714 "NOLB %d/%d", "NOLB",
715 "no check" };
716
717 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
718 hlen += 24;
719
720 if (!(s->state & SRV_CHECKED))
721 sv_state = 6; /* should obviously never happen */
722 else if (s->state & SRV_RUNNING) {
723 if (s->health == s->rise + s->fall - 1)
724 sv_state = 3; /* UP */
725 else
726 sv_state = 2; /* going down */
727
728 if (s->state & SRV_GOINGDOWN)
729 sv_state += 2;
730 } else {
731 if (s->health)
732 sv_state = 1; /* going up */
733 else
734 sv_state = 0; /* DOWN */
735 }
736
737 hlen += sprintf(buffer + hlen,
738 srv_hlt_st[sv_state],
739 (s->state & SRV_RUNNING) ? (s->health - s->rise + 1) : (s->health),
740 (s->state & SRV_RUNNING) ? (s->fall) : (s->rise));
741
742 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
743 s->proxy->id, s->id,
744 global.node,
745 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
746 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
747 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
748 s->nbpend);
749
750 if ((s->state & SRV_WARMINGUP) &&
751 now.tv_sec < s->last_change + s->slowstart &&
752 now.tv_sec >= s->last_change) {
753 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
754 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
755 }
756
757 buffer[hlen++] = '\r';
758 buffer[hlen++] = '\n';
759
760 return hlen;
761}
762
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763/*
764 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200765 * the connection acknowledgement. If the proxy requires L7 health-checks,
766 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900767 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200769static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770{
Simon Horman4a741432013-02-23 15:35:38 +0900771 struct check *check = conn->owner;
772 struct server *s = check->server;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200773 int fd = conn->t.sock.fd;
Simon Horman4a741432013-02-23 15:35:38 +0900774 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200775
Willy Tarreau5a78f362012-11-23 12:47:05 +0100776 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200777 conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200779 if (unlikely(conn->flags & CO_FL_ERROR)) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100780 int skerr, err = errno;
781 socklen_t lskerr = sizeof(skerr);
782
783 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
784 err = skerr;
785
Simon Horman4a741432013-02-23 15:35:38 +0900786 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200787 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200788 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200789
Willy Tarreau5a78f362012-11-23 12:47:05 +0100790 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_WR))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200791 return;
Willy Tarreau83749182007-04-15 20:56:27 +0200792
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200793 /* here, we know that the connection is established */
Simon Horman4a741432013-02-23 15:35:38 +0900794 if (!(check->result & SRV_CHK_FAILED)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Simon Horman4a741432013-02-23 15:35:38 +0900796 if (check->bo->o) {
797 conn->xprt->snd_buf(conn, check->bo, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauf1503172012-09-28 19:39:36 +0200798 if (conn->flags & CO_FL_ERROR) {
Simon Horman4a741432013-02-23 15:35:38 +0900799 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreauf1503172012-09-28 19:39:36 +0200800 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200801 }
Simon Horman4a741432013-02-23 15:35:38 +0900802 if (check->bo->o) {
Willy Tarreau5a78f362012-11-23 12:47:05 +0100803 goto out_incomplete;
804 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200806
Willy Tarreau5a78f362012-11-23 12:47:05 +0100807 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
808 if (s->proxy->timeout.check) {
809 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
810 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 }
Willy Tarreau5a78f362012-11-23 12:47:05 +0100812 goto out_nowake;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813 }
Willy Tarreau83749182007-04-15 20:56:27 +0200814 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200815 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200816 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200817 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreau5a78f362012-11-23 12:47:05 +0100818 out_incomplete:
Willy Tarreau3267d362012-08-17 23:53:56 +0200819 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200820 out_error:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200821 conn->flags |= CO_FL_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200822 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823}
824
825
826/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200827 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200828 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900829 * set_server_check_status() to update check->status, check->duration
830 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200831
832 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
833 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
834 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
835 * response to an SSL HELLO (the principle is that this is enough to
836 * distinguish between an SSL server and a pure TCP relay). All other cases will
837 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
838 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200840static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841{
Simon Horman4a741432013-02-23 15:35:38 +0900842 struct check *check = conn->owner;
843 struct server *s = check->server;
844 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200845 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100846 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200847 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200848
Simon Horman4a741432013-02-23 15:35:38 +0900849 if (unlikely((check->result & SRV_CHK_FAILED) || (conn->flags & CO_FL_ERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200850 /* in case of TCP only, this tells us if the connection failed */
Simon Horman4a741432013-02-23 15:35:38 +0900851 if (!(check->result & SRV_CHK_FAILED))
852 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreau83749182007-04-15 20:56:27 +0200853 goto out_wakeup;
854 }
855
Willy Tarreau5a78f362012-11-23 12:47:05 +0100856 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_RD))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200857 return;
858
Willy Tarreau83749182007-04-15 20:56:27 +0200859 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
860 * but the connection was closed on the remote end. Fortunately, recv still
861 * works correctly and we don't need to do the getsockopt() on linux.
862 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000863
864 /* Set buffer to point to the end of the data already read, and check
865 * that there is free space remaining. If the buffer is full, proceed
866 * with running the checks without attempting another socket read.
867 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000868
Willy Tarreau03938182010-03-17 21:52:07 +0100869 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000870
Simon Horman4a741432013-02-23 15:35:38 +0900871 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200872 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100873 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900874 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200875 /* Report network errors only if we got no other data. Otherwise
876 * we'll let the upper layers decide whether the response is OK
877 * or not. It is very common that an RST sent by the server is
878 * reported as an error just after the last data chunk.
879 */
Simon Horman4a741432013-02-23 15:35:38 +0900880 if (!(check->result & SRV_CHK_FAILED))
881 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100882 goto out_wakeup;
883 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200884 }
885
Willy Tarreau03938182010-03-17 21:52:07 +0100886 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900887 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100888 */
Simon Horman4a741432013-02-23 15:35:38 +0900889 if (check->bi->i < check->bi->size)
890 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100891 else {
Simon Horman4a741432013-02-23 15:35:38 +0900892 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100893 done = 1; /* buffer full, don't wait for more data */
894 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200895
Nick Chalk57b1bf72010-03-16 15:50:46 +0000896 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900897 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200898 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900899 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100900 goto wait_more_data;
901
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100902 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900903 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
904 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
905 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
906 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
907 !isdigit((unsigned char) *(check->bi->data + 11))) {
908 cut_crlf(check->bi->data);
909 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200910
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100911 goto out_wakeup;
912 }
913
Simon Horman4a741432013-02-23 15:35:38 +0900914 check->code = str2uic(check->bi->data + 9);
915 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000916
Willy Tarreaubd741542010-03-16 18:46:54 +0100917 if ((s->proxy->options & PR_O_DISABLE404) &&
Simon Horman4a741432013-02-23 15:35:38 +0900918 (s->state & SRV_RUNNING) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000919 /* 404 may be accepted as "stopping" only if the server was up */
920 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900921 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000922 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100923 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
924 /* Run content verification check... We know we have at least 13 chars */
925 if (!httpchk_expect(s, done))
926 goto wait_more_data;
927 }
928 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900929 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100930 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900931 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100932 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000933 else {
934 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900935 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000936 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200937 break;
938
939 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900940 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100941 goto wait_more_data;
942
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100943 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900944 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
945 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200946 else
Simon Horman4a741432013-02-23 15:35:38 +0900947 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200948 break;
949
950 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900951 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100952 goto wait_more_data;
953
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200954 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900955 if ((check->bi->i < strlen("000\r")) ||
956 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
957 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
958 !isdigit((unsigned char) *(check->bi->data + 2))) {
959 cut_crlf(check->bi->data);
960 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200961
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200962 goto out_wakeup;
963 }
964
Simon Horman4a741432013-02-23 15:35:38 +0900965 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200966
Simon Horman4a741432013-02-23 15:35:38 +0900967 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200968 cut_crlf(desc);
969
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100970 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900971 if (*check->bi->data == '2')
972 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200973 else
Simon Horman4a741432013-02-23 15:35:38 +0900974 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200975 break;
976
Simon Hormana2b9dad2013-02-12 10:45:54 +0900977 case PR_O2_LB_AGENT_CHK: {
978 short status = HCHK_STATUS_L7RSP;
979 const char *desc = "Unknown feedback string";
980 const char *down_cmd = NULL;
981
982 if (!done)
983 goto wait_more_data;
984
Simon Horman4a741432013-02-23 15:35:38 +0900985 cut_crlf(check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +0900986
Simon Horman4a741432013-02-23 15:35:38 +0900987 if (strchr(check->bi->data, '%')) {
988 desc = server_parse_weight_change_request(s, check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +0900989 if (!desc) {
990 status = HCHK_STATUS_L7OKD;
Simon Horman4a741432013-02-23 15:35:38 +0900991 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900992 }
Simon Horman4a741432013-02-23 15:35:38 +0900993 } else if (!strcasecmp(check->bi->data, "drain")) {
Simon Hormana2b9dad2013-02-12 10:45:54 +0900994 desc = server_parse_weight_change_request(s, "0%");
995 if (!desc) {
996 desc = "drain";
997 status = HCHK_STATUS_L7OKD;
998 }
Simon Horman4a741432013-02-23 15:35:38 +0900999 } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001000 down_cmd = "down";
Simon Horman4a741432013-02-23 15:35:38 +09001001 } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001002 down_cmd = "stopped";
Simon Horman4a741432013-02-23 15:35:38 +09001003 } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001004 down_cmd = "fail";
1005 }
1006
1007 if (down_cmd) {
Simon Horman4a741432013-02-23 15:35:38 +09001008 const char *end = check->bi->data + strlen(down_cmd);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001009 /*
1010 * The command keyword must terminated the string or
1011 * be followed by a blank.
1012 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001013 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001014 status = HCHK_STATUS_L7STS;
1015 /* Skip over leading blanks */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001016 while (end[0] != '\0' && (end[0] == ' ' || end[0] == '\t'))
Simon Hormana2b9dad2013-02-12 10:45:54 +09001017 end++;
1018 desc = end;
1019 }
1020 }
1021
Simon Horman4a741432013-02-23 15:35:38 +09001022 set_server_check_status(check, status, desc);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001023 break;
1024 }
1025
Willy Tarreau1620ec32011-08-06 17:05:02 +02001026 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001027 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001028 goto wait_more_data;
1029
Simon Horman4a741432013-02-23 15:35:38 +09001030 if (check->bi->data[0] == 'R') {
1031 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001032 }
1033 else {
Simon Horman4a741432013-02-23 15:35:38 +09001034 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1035 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001036 else
1037 desc = "PostgreSQL unknown error";
1038
Simon Horman4a741432013-02-23 15:35:38 +09001039 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001040 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001041 break;
1042
1043 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001044 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001045 goto wait_more_data;
1046
Simon Horman4a741432013-02-23 15:35:38 +09001047 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1048 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001049 }
1050 else {
Simon Horman4a741432013-02-23 15:35:38 +09001051 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001052 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001053 break;
1054
1055 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001056 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001057 goto wait_more_data;
1058
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001059 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001060 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001061 /* We set the MySQL Version in description for information purpose
1062 * FIXME : it can be cool to use MySQL Version for other purpose,
1063 * like mark as down old MySQL server.
1064 */
Simon Horman4a741432013-02-23 15:35:38 +09001065 if (check->bi->i > 51) {
1066 desc = ltrim(check->bi->data + 5, ' ');
1067 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001068 }
1069 else {
1070 if (!done)
1071 goto wait_more_data;
1072 /* it seems we have a OK packet but without a valid length,
1073 * it must be a protocol error
1074 */
Simon Horman4a741432013-02-23 15:35:38 +09001075 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001076 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001077 }
1078 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001079 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001080 desc = ltrim(check->bi->data + 7, ' ');
1081 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001082 }
1083 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001084 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1085 (((unsigned int) *(check->bi->data + 1)) << 8) +
1086 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001087
Simon Horman4a741432013-02-23 15:35:38 +09001088 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001089 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001090 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001091 /* We have only one MySQL packet and it is a Handshake Initialization packet
1092 * but we need to have a second packet to know if it is alright
1093 */
Simon Horman4a741432013-02-23 15:35:38 +09001094 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001095 goto wait_more_data;
1096 }
1097 else {
1098 /* We have only one packet and it is an Error packet,
1099 * an error message is attached, so we can display it
1100 */
Simon Horman4a741432013-02-23 15:35:38 +09001101 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001102 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001103 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001104 }
Simon Horman4a741432013-02-23 15:35:38 +09001105 } else if (check->bi->i > first_packet_len + 4) {
1106 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1107 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1108 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001109
Simon Horman4a741432013-02-23 15:35:38 +09001110 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001111 /* We have 2 packets and that's good */
1112 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001113 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001114 /* No error packet */
1115 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001116 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001117 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001118 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001119 }
1120 else {
1121 /* An error message is attached in the Error packet
1122 * so we can display it ! :)
1123 */
Simon Horman4a741432013-02-23 15:35:38 +09001124 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001125 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001126 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001127 }
1128 }
1129 }
1130 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001131 if (!done)
1132 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001133 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001134 * it must be a protocol error
1135 */
Simon Horman4a741432013-02-23 15:35:38 +09001136 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001137 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001138 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001139 }
1140 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001141 break;
1142
1143 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001144 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001145 goto wait_more_data;
1146
1147 /* Check if the server speaks LDAP (ASN.1/BER)
1148 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1149 * http://tools.ietf.org/html/rfc4511
1150 */
1151
1152 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1153 * LDAPMessage: 0x30: SEQUENCE
1154 */
Simon Horman4a741432013-02-23 15:35:38 +09001155 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1156 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001157 }
1158 else {
1159 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001160 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001161
1162 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1163 * messageID: 0x02 0x01 0x01: INTEGER 1
1164 * protocolOp: 0x61: bindResponse
1165 */
1166 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001167 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1168 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001169
1170 goto out_wakeup;
1171 }
1172
1173 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001174 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001175
1176 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1177 * ldapResult: 0x0a 0x01: ENUMERATION
1178 */
1179 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001180 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1181 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001182
1183 goto out_wakeup;
1184 }
1185
1186 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1187 * resultCode
1188 */
Simon Horman4a741432013-02-23 15:35:38 +09001189 check->code = *(check->bi->data + msglen + 9);
1190 if (check->code) {
1191 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 +02001192 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001193 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001194 }
1195 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001196 break;
1197
1198 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001199 /* other checks are valid if the connection succeeded anyway */
Simon Horman4a741432013-02-23 15:35:38 +09001200 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001201 break;
1202 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001203
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001204 out_wakeup:
Simon Horman4a741432013-02-23 15:35:38 +09001205 if (check->result & SRV_CHK_FAILED)
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001206 conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001207
Nick Chalk57b1bf72010-03-16 15:50:46 +00001208 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001209 *check->bi->data = '\0';
1210 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001211
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001212 /* Close the connection... We absolutely want to perform a hard close
1213 * and reset the connection if some data are pending, otherwise we end
1214 * up with many TIME_WAITs and eat all the source port range quickly.
1215 * To avoid sending RSTs all the time, we first try to drain pending
1216 * data.
1217 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001218 if (conn->xprt && conn->xprt->shutw)
1219 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001220
1221 if (conn->ctrl && !(conn->flags & CO_FL_SOCK_RD_SH)) {
1222 if (conn->flags & CO_FL_WAIT_RD || !conn->ctrl->drain || !conn->ctrl->drain(conn->t.sock.fd))
1223 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1224 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreaucfd97c62012-11-23 17:35:59 +01001225 }
Willy Tarreaua522f802012-11-23 08:56:35 +01001226 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001227 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001228 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001229
1230 wait_more_data:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001231 __conn_data_poll_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001232}
1233
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001234/*
1235 * This function is used only for server health-checks. It handles connection
1236 * status updates including errors. If necessary, it wakes the check task up.
1237 * It always returns 0.
1238 */
1239static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001240{
Simon Horman4a741432013-02-23 15:35:38 +09001241 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001242
Willy Tarreau6c560da2012-11-24 11:14:45 +01001243 if (unlikely(conn->flags & CO_FL_ERROR)) {
1244 /* Note that we might as well have been woken up by a handshake handler */
Simon Horman4a741432013-02-23 15:35:38 +09001245 if (check->result == SRV_CHK_UNKNOWN)
1246 check->result |= SRV_CHK_FAILED;
Willy Tarreau6c560da2012-11-24 11:14:45 +01001247 __conn_data_stop_both(conn);
Simon Horman4a741432013-02-23 15:35:38 +09001248 task_wakeup(check->task, TASK_WOKEN_IO);
Willy Tarreau6c560da2012-11-24 11:14:45 +01001249 }
Willy Tarreau20bea422012-07-06 12:00:49 +02001250
Simon Horman4a741432013-02-23 15:35:38 +09001251 if (check->result & (SRV_CHK_FAILED|SRV_CHK_PASSED))
Willy Tarreau2b199c92012-11-23 17:32:21 +01001252 conn_full_close(conn);
Willy Tarreau3267d362012-08-17 23:53:56 +02001253 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001254}
1255
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001256struct data_cb check_conn_cb = {
1257 .recv = event_srv_chk_r,
1258 .send = event_srv_chk_w,
1259 .wake = wake_srv_chk,
1260};
1261
Willy Tarreaubaaee002006-06-26 02:48:02 +02001262/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001263 * updates the server's weight during a warmup stage. Once the final weight is
1264 * reached, the task automatically stops. Note that any server status change
1265 * must have updated s->last_change accordingly.
1266 */
1267static struct task *server_warmup(struct task *t)
1268{
1269 struct server *s = t->context;
1270
1271 /* by default, plan on stopping the task */
1272 t->expire = TICK_ETERNITY;
1273 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1274 return t;
1275
1276 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
1277 /* go to full throttle if the slowstart interval is reached */
1278 s->state &= ~SRV_WARMINGUP;
1279 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
1280 s->eweight = s->uweight * BE_WEIGHT_SCALE;
1281 if (s->proxy->lbprm.update_server_eweight)
1282 s->proxy->lbprm.update_server_eweight(s);
1283 }
1284 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
1285 /* for dynamic algorithms, let's slowly update the weight */
1286 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
1287 s->slowstart - 1) / s->slowstart;
1288 s->eweight *= s->uweight;
1289 if (s->proxy->lbprm.update_server_eweight)
1290 s->proxy->lbprm.update_server_eweight(s);
1291 }
1292 /* Note that static algorithms are already running at full throttle */
1293
1294 /* probably that we can refill this server with a bit more connections */
1295 check_for_pending(s);
1296
1297 /* get back there in 1 second or 1/20th of the slowstart interval,
1298 * whichever is greater, resulting in small 5% steps.
1299 */
1300 if (s->state & SRV_WARMINGUP)
1301 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1302 return t;
1303}
1304
1305/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306 * manages a server health-check. Returns
1307 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1308 */
Simon Horman63a4a822012-03-19 07:24:41 +09001309static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001310{
Simon Horman4a741432013-02-23 15:35:38 +09001311 struct check *check = t->context;
1312 struct server *s = check->server;
1313 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001314 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001315 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001316 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317
Willy Tarreau5a78f362012-11-23 12:47:05 +01001318 if (!(s->state & SRV_CHK_RUNNING)) {
1319 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001320 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001321 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001322
1323 /* we don't send any health-checks when the proxy is stopped or when
1324 * the server should not be checked.
1325 */
Willy Tarreau5a78f362012-11-23 12:47:05 +01001326 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED || (s->state & SRV_MAINTAIN))
1327 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001328
1329 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001330 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001331
Willy Tarreau5a78f362012-11-23 12:47:05 +01001332 s->state |= SRV_CHK_RUNNING;
Simon Horman4a741432013-02-23 15:35:38 +09001333 check->bi->p = check->bi->data;
1334 check->bi->i = 0;
1335 check->bo->p = check->bo->data;
1336 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001337
1338 /* prepare the check buffer */
Simon Horman4a741432013-02-23 15:35:38 +09001339 if (check->type) {
1340 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001341
1342 /* we want to check if this host replies to HTTP or SSLv3 requests
1343 * so we'll send the request, and won't wake the checker up now.
1344 */
Simon Horman4a741432013-02-23 15:35:38 +09001345 if ((check->type) == PR_O2_SSL3_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001346 /* SSL requires that we put Unix time in the request */
1347 int gmt_time = htonl(date.tv_sec);
Simon Horman4a741432013-02-23 15:35:38 +09001348 memcpy(check->bo->data + 11, &gmt_time, 4);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001349 }
Simon Horman4a741432013-02-23 15:35:38 +09001350 else if ((check->type) == PR_O2_HTTP_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001351 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Simon Horman4a741432013-02-23 15:35:38 +09001352 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str));
1353 bo_putstr(check->bo, "\r\n");
1354 *check->bo->p = '\0'; /* to make gdb output easier to read */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001355 }
1356 }
1357
1358 /* prepare a new connection */
Willy Tarreaub63b5962012-11-23 16:22:08 +01001359 conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01001360 conn->err_code = CO_ER_NONE;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001361 conn->target = &s->obj_type;
Simon Horman4a741432013-02-23 15:35:38 +09001362 conn_prepare(conn, &check_conn_cb, s->check_common.proto, s->check_common.xprt, check);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001363
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001364 /* no client address */
1365 clear_addr(&conn->addr.from);
1366
Simon Horman66183002013-02-23 10:16:43 +09001367 if (is_addr(&s->check_common.addr))
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001368 /* we'll connect to the check addr specified on the server */
Simon Horman66183002013-02-23 10:16:43 +09001369 conn->addr.to = s->check_common.addr;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001370 else
1371 /* we'll connect to the addr on the server */
1372 conn->addr.to = s->addr;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001373
Simon Horman4a741432013-02-23 15:35:38 +09001374 set_host_port(&conn->addr.to, check->port);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001375
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001376 /* It can return one of :
1377 * - SN_ERR_NONE if everything's OK
1378 * - SN_ERR_SRVTO if there are no more servers
1379 * - SN_ERR_SRVCL if the connection was refused by the server
1380 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1381 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1382 * - SN_ERR_INTERNAL for any other purely internal errors
1383 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001384 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001385 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001386 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001387 ret = SN_ERR_INTERNAL;
Simon Horman66183002013-02-23 10:16:43 +09001388 if (s->check_common.proto->connect)
Simon Horman4a741432013-02-23 15:35:38 +09001389 ret = s->check_common.proto->connect(conn, check->type,
1390 s->check.send_proxy ? 1 : (check->type) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001391 conn->flags |= CO_FL_WAKE_DATA;
Simon Horman4a741432013-02-23 15:35:38 +09001392 if (check->send_proxy)
Willy Tarreau6c16adc2012-10-05 00:04:16 +02001393 conn->flags |= CO_FL_LOCAL_SPROXY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001395 switch (ret) {
1396 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001397 /* we allow up to min(inter, timeout.connect) for a connection
1398 * to establish but only when timeout.check is set
1399 * as it may be to short for a full check otherwise
1400 */
Simon Horman4a741432013-02-23 15:35:38 +09001401 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001402
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001403 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1404 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1405 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001406 }
Willy Tarreaudb3b4a22012-11-23 16:32:33 +01001407 conn_data_poll_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau5a78f362012-11-23 12:47:05 +01001408 goto reschedule;
1409
1410 case SN_ERR_SRVTO: /* ETIMEDOUT */
1411 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman4a741432013-02-23 15:35:38 +09001412 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau5a78f362012-11-23 12:47:05 +01001413 break;
1414 case SN_ERR_PRXCOND:
1415 case SN_ERR_RESOURCE:
1416 case SN_ERR_INTERNAL:
Simon Horman4a741432013-02-23 15:35:38 +09001417 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001418 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419 }
1420
Willy Tarreau5a78f362012-11-23 12:47:05 +01001421 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001422
Willy Tarreau5a78f362012-11-23 12:47:05 +01001423 s->state &= ~SRV_CHK_RUNNING;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424 if (s->health > s->rise) {
1425 s->health--; /* still good */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001426 s->counters.failed_checks++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001427 }
1428 else
Simon Horman4a741432013-02-23 15:35:38 +09001429 set_server_down(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001430
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001431 /* we allow up to min(inter, timeout.connect) for a connection
1432 * to establish but only when timeout.check is set
1433 * as it may be to short for a full check otherwise
1434 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001435 while (tick_is_expired(t->expire, now_ms)) {
1436 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001437
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001438 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001439 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001440
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001441 if (s->proxy->timeout.check)
1442 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001443 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001444 }
1445 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001446 /* there was a test running.
1447 * First, let's check whether there was an uncaught error,
1448 * which can happen on connect timeout or error.
1449 */
Simon Horman28b5ffc2013-02-24 07:25:29 +09001450 if (s->check.result == SRV_CHK_UNKNOWN) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001451 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
1452 /* L4 not established (yet) */
Willy Tarreauf1503172012-09-28 19:39:36 +02001453 if (conn->flags & CO_FL_ERROR)
Simon Horman4a741432013-02-23 15:35:38 +09001454 set_server_check_status(check, HCHK_STATUS_L4CON, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001455 else if (expired)
Simon Horman4a741432013-02-23 15:35:38 +09001456 set_server_check_status(check, HCHK_STATUS_L4TOUT, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001457 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001458 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001459 /* L6 not established (yet) */
Willy Tarreauf1503172012-09-28 19:39:36 +02001460 if (conn->flags & CO_FL_ERROR)
Simon Horman4a741432013-02-23 15:35:38 +09001461 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001462 else if (expired)
Simon Horman4a741432013-02-23 15:35:38 +09001463 set_server_check_status(check, HCHK_STATUS_L6TOUT, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001464 }
Simon Horman4a741432013-02-23 15:35:38 +09001465 else if (!check->type) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001466 /* good connection is enough for pure TCP check */
Simon Horman4a741432013-02-23 15:35:38 +09001467 if (check->use_ssl)
1468 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001469 else
Simon Horman4a741432013-02-23 15:35:38 +09001470 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001471 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001472 else if (expired) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001473 /* connection established but expired check */
Simon Horman4a741432013-02-23 15:35:38 +09001474 if (check->type == PR_O2_SSL3_CHK)
1475 set_server_check_status(check, HCHK_STATUS_L6TOUT, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001476 else /* HTTP, SMTP, ... */
Simon Horman4a741432013-02-23 15:35:38 +09001477 set_server_check_status(check, HCHK_STATUS_L7TOUT, NULL);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001478
Willy Tarreauf1503172012-09-28 19:39:36 +02001479 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001480 else
1481 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001482 }
1483
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001484 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001485 if (conn->xprt) {
1486 /* The check was aborted and the connection was not yet closed.
1487 * This can happen upon timeout, or when an external event such
1488 * as a failed response coupled with "observe layer7" caused the
1489 * server state to be suddenly changed.
1490 */
1491 if (conn->ctrl)
1492 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1493 (struct linger *) &nolinger, sizeof(struct linger));
1494 conn_full_close(conn);
1495 }
1496
Simon Horman28b5ffc2013-02-24 07:25:29 +09001497 if (s->check.result & SRV_CHK_FAILED) { /* a failure or timeout detected */
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001498 if (s->health > s->rise) {
1499 s->health--; /* still good */
1500 s->counters.failed_checks++;
1501 }
1502 else
Simon Horman4a741432013-02-23 15:35:38 +09001503 set_server_down(check);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001504 }
1505 else { /* check was OK */
Willy Tarreau48494c02007-11-30 10:41:39 +01001506 /* we may have to add/remove this server from the LB group */
1507 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
Simon Horman4a741432013-02-23 15:35:38 +09001508 if ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE))
1509 set_server_enabled(check);
1510 else if (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE))
1511 set_server_disabled(check);
Willy Tarreau48494c02007-11-30 10:41:39 +01001512 }
1513
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001514 if (s->health < s->rise + s->fall - 1) {
1515 s->health++; /* was bad, stays for a while */
Simon Horman4a741432013-02-23 15:35:38 +09001516 set_server_up(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001517 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001518 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001519 s->state &= ~SRV_CHK_RUNNING;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001520
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001521 rv = 0;
1522 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001523 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001524 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001525 }
Simon Horman4a741432013-02-23 15:35:38 +09001526 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001527 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001528
1529 reschedule:
1530 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09001531 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001532 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001533 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001534}
1535
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001536/*
1537 * Start health-check.
1538 * Returns 0 if OK, -1 if error, and prints the error in this case.
1539 */
1540int start_checks() {
1541
1542 struct proxy *px;
1543 struct server *s;
1544 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09001545 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001546
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001547 /* 1- count the checkers to run simultaneously.
1548 * We also determine the minimum interval among all of those which
1549 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1550 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001551 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001552 * too short an interval for all others.
1553 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001554 for (px = proxy; px; px = px->next) {
1555 for (s = px->srv; s; s = s->next) {
1556 if (!(s->state & SRV_CHECKED))
1557 continue;
1558
Simon Horman4a741432013-02-23 15:35:38 +09001559 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1560 (!mininter || mininter > srv_getinter(&s->check)))
1561 mininter = srv_getinter(&s->check);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001562
Simon Horman4a741432013-02-23 15:35:38 +09001563 nbcheck++;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001564 }
1565 }
1566
Simon Horman4a741432013-02-23 15:35:38 +09001567 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001568 return 0;
1569
1570 srand((unsigned)time(NULL));
1571
1572 /*
1573 * 2- start them as far as possible from each others. For this, we will
1574 * start them after their interval set to the min interval divided by
1575 * the number of servers, weighted by the server's position in the list.
1576 */
1577 for (px = proxy; px; px = px->next) {
1578 for (s = px->srv; s; s = s->next) {
Willy Tarreau2e993902011-10-31 11:53:20 +01001579 if (s->slowstart) {
1580 if ((t = task_new()) == NULL) {
1581 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1582 return -1;
1583 }
1584 /* We need a warmup task that will be called when the server
1585 * state switches from down to up.
1586 */
1587 s->warmup = t;
1588 t->process = server_warmup;
1589 t->context = s;
1590 t->expire = TICK_ETERNITY;
1591 }
1592
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001593 if (!(s->state & SRV_CHECKED))
1594 continue;
1595
Willy Tarreau2e993902011-10-31 11:53:20 +01001596 /* one task for the checks */
Willy Tarreaua4613182009-03-21 18:13:21 +01001597 if ((t = task_new()) == NULL) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001598 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1599 return -1;
1600 }
1601
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001602 s->check.task = t;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001603 t->process = process_chk;
Simon Horman4a741432013-02-23 15:35:38 +09001604 t->context = &s->check;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001605
1606 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001607 t->expire = tick_add(now_ms,
Simon Horman4a741432013-02-23 15:35:38 +09001608 MS_TO_TICKS(((mininter &&
1609 mininter >= srv_getinter(&s->check)) ?
1610 mininter : srv_getinter(&s->check)) * srvpos / nbcheck));
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001611 s->check.start = now;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001612 task_queue(t);
1613
1614 srvpos++;
1615 }
1616 }
1617 return 0;
1618}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001619
1620/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001621 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001622 * The buffer MUST be terminated by a null byte before calling this function.
1623 * Sets server status appropriately. The caller is responsible for ensuring
1624 * that the buffer contains at least 13 characters. If <done> is zero, we may
1625 * return 0 to indicate that data is required to decide of a match.
1626 */
1627static int httpchk_expect(struct server *s, int done)
1628{
1629 static char status_msg[] = "HTTP status check returned code <000>";
1630 char status_code[] = "000";
1631 char *contentptr;
1632 int crlf;
1633 int ret;
1634
1635 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1636 case PR_O2_EXP_STS:
1637 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001638 memcpy(status_code, s->check.bi->data + 9, 3);
1639 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001640
1641 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1642 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1643 else
1644 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1645
1646 /* we necessarily have the response, so there are no partial failures */
1647 if (s->proxy->options2 & PR_O2_EXP_INV)
1648 ret = !ret;
1649
Simon Horman4a741432013-02-23 15:35:38 +09001650 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01001651 break;
1652
1653 case PR_O2_EXP_STR:
1654 case PR_O2_EXP_RSTR:
1655 /* very simple response parser: ignore CR and only count consecutive LFs,
1656 * stop with contentptr pointing to first char after the double CRLF or
1657 * to '\0' if crlf < 2.
1658 */
1659 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001660 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001661 if (crlf >= 2)
1662 break;
1663 if (*contentptr == '\r')
1664 continue;
1665 else if (*contentptr == '\n')
1666 crlf++;
1667 else
1668 crlf = 0;
1669 }
1670
1671 /* Check that response contains a body... */
1672 if (crlf < 2) {
1673 if (!done)
1674 return 0;
1675
Simon Horman4a741432013-02-23 15:35:38 +09001676 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001677 "HTTP content check could not find a response body");
1678 return 1;
1679 }
1680
1681 /* Check that response body is not empty... */
1682 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001683 if (!done)
1684 return 0;
1685
Simon Horman4a741432013-02-23 15:35:38 +09001686 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001687 "HTTP content check found empty response body");
1688 return 1;
1689 }
1690
1691 /* Check the response content against the supplied string
1692 * or regex... */
1693 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1694 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1695 else
1696 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1697
1698 /* if we don't match, we may need to wait more */
1699 if (!ret && !done)
1700 return 0;
1701
1702 if (ret) {
1703 /* content matched */
1704 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001705 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001706 "HTTP check matched unwanted content");
1707 else
Simon Horman4a741432013-02-23 15:35:38 +09001708 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001709 "HTTP content check matched");
1710 }
1711 else {
1712 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001713 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001714 "HTTP check did not match unwanted content");
1715 else
Simon Horman4a741432013-02-23 15:35:38 +09001716 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001717 "HTTP content check did not match");
1718 }
1719 break;
1720 }
1721 return 1;
1722}
1723
1724/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001725 * Local variables:
1726 * c-indent-level: 8
1727 * c-basic-offset: 8
1728 * End:
1729 */