blob: eb8ce81c165362392ee3200a9cccadf55ab589e1 [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
Simon Horman2f1f9552013-11-25 10:46:37 +0900231 /* Failure to connect to the agent as a secondary check should not
232 * cause the server to be marked down. So only log status changes
233 * for HCHK_STATUS_* statuses */
234 if (check == &s->agent && check->status < HCHK_STATUS_L7TOUT)
235 return;
236
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
Simon Horman125d0992013-02-24 17:23:38 +0900238 (((check->health != 0) && (check->result & SRV_CHK_FAILED)) ||
Simon Horman58c32972013-11-25 10:46:38 +0900239 ((check->health != check->rise + check->fall - 1) && (check->result & SRV_CHK_PASSED)) ||
Simon Horman4a741432013-02-23 15:35:38 +0900240 ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE)) ||
241 (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE)))) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200242
243 int health, rise, fall, state;
244
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100245 chunk_reset(&trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200246
247 /* FIXME begin: calculate local version of the health/rise/fall/state */
Simon Horman125d0992013-02-24 17:23:38 +0900248 health = check->health;
Simon Horman58c32972013-11-25 10:46:38 +0900249 rise = check->rise;
250 fall = check->fall;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200251 state = s->state;
252
Simon Horman4a741432013-02-23 15:35:38 +0900253 if (check->result & SRV_CHK_FAILED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200254 if (health > rise) {
255 health--; /* still good */
256 } else {
257 if (health == rise)
258 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
259
260 health = 0;
261 }
262 }
263
Simon Horman4a741432013-02-23 15:35:38 +0900264 if (check->result & SRV_CHK_PASSED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200265 if (health < rise + fall - 1) {
266 health++; /* was bad, stays for a while */
267
268 if (health == rise)
269 state |= SRV_RUNNING;
270
271 if (health >= rise)
272 health = rise + fall - 1; /* OK now */
273 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100274
275 /* clear consecutive_errors if observing is enabled */
276 if (s->onerror)
277 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200278 }
279 /* FIXME end: calculate local version of the health/rise/fall/state */
280
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100281 chunk_appendf(&trash,
282 "Health check for %sserver %s/%s %s%s",
283 s->state & SRV_BACKUP ? "backup " : "",
284 s->proxy->id, s->id,
Simon Horman4a741432013-02-23 15:35:38 +0900285 (check->result & SRV_CHK_DISABLE)?"conditionally ":"",
286 (check->result & SRV_CHK_PASSED)?"succeeded":"failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200287
Simon Horman4a741432013-02-23 15:35:38 +0900288 server_status_printf(&trash, s, check, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200289
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100290 chunk_appendf(&trash, ", status: %d/%d %s",
291 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
292 (state & SRV_RUNNING) ? (fall) : (rise),
293 (state & SRV_RUNNING)?"UP":"DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200294
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100295 Warning("%s.\n", trash.str);
296 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200297 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200298}
299
Willy Tarreau48494c02007-11-30 10:41:39 +0100300/* sends a log message when a backend goes down, and also sets last
301 * change date.
302 */
303static void set_backend_down(struct proxy *be)
304{
305 be->last_change = now.tv_sec;
306 be->down_trans++;
307
308 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
309 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
310}
311
312/* Redistribute pending connections when a server goes down. The number of
313 * connections redistributed is returned.
314 */
315static int redistribute_pending(struct server *s)
316{
317 struct pendconn *pc, *pc_bck, *pc_end;
318 int xferred = 0;
319
320 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
321 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100322 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
323 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100324 /* The REDISP option was specified. We will ignore
325 * cookie and force to balance or use the dispatcher.
326 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100327
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100328 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100329 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100330
Willy Tarreau48494c02007-11-30 10:41:39 +0100331 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200332 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100333 xferred++;
334 }
335 }
336 return xferred;
337}
338
339/* Check for pending connections at the backend, and assign some of them to
340 * the server coming up. The server's weight is checked before being assigned
341 * connections it may not be able to handle. The total number of transferred
342 * connections is returned.
343 */
344static int check_for_pending(struct server *s)
345{
346 int xferred;
347
348 if (!s->eweight)
349 return 0;
350
351 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
352 struct session *sess;
353 struct pendconn *p;
354
355 p = pendconn_from_px(s->proxy);
356 if (!p)
357 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100358 p->sess->target = &s->obj_type;
Willy Tarreau48494c02007-11-30 10:41:39 +0100359 sess = p->sess;
360 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200361 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100362 }
363 return xferred;
364}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700366/* Shutdown all connections of a server. The caller must pass a termination
367 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
368 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900369 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700370static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900371{
372 struct session *session, *session_bck;
373
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200374 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
375 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700376 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900377}
378
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700379/* Shutdown all connections of all backup servers of a proxy. The caller must
380 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
381 * the reason for the shutdown.
382 */
383static void shutdown_backup_sessions(struct proxy *px, int why)
384{
385 struct server *srv;
386
387 for (srv = px->srv; srv != NULL; srv = srv->next)
388 if (srv->state & SRV_BACKUP)
389 shutdown_sessions(srv, why);
390}
391
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392/* Sets server <s> down, notifies by all available means, recounts the
393 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200394 * possible to other servers. It automatically recomputes the number of
395 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396 */
Simon Horman4a741432013-02-23 15:35:38 +0900397void set_server_down(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398{
Simon Horman4a741432013-02-23 15:35:38 +0900399 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100400 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401 int xferred;
402
Cyril Bontécd19e512010-01-31 22:34:03 +0100403 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900404 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100405 }
406
Simon Horman58c32972013-11-25 10:46:38 +0900407 if ((s->state & SRV_RUNNING && check->health == check->rise) || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100408 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200409 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200410
411 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100412 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200413 if (s->proxy->lbprm.set_server_status_down)
414 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900416 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700417 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900418
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419 /* we might have sessions queued on this server and waiting for
420 * a connection. Those which are redispatchable will be queued
421 * to another server or to the proxy itself.
422 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100423 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100424
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100425 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100426
Cyril Bontécd19e512010-01-31 22:34:03 +0100427 if (s->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100428 chunk_appendf(&trash,
429 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
430 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100431 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100432 chunk_appendf(&trash,
433 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
434 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100435
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100436 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900437 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : 0),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100438 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100439 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100440 Warning("%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200441
Willy Tarreau48494c02007-11-30 10:41:39 +0100442 /* we don't send an alert if the server was previously paused */
443 if (srv_was_paused)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100444 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreau48494c02007-11-30 10:41:39 +0100445 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100446 send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200447
Willy Tarreaud64d2252010-10-17 17:16:42 +0200448 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100449 set_backend_down(s->proxy);
450
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200451 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100452
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100453 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100454 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100455 if (! (srv->state & SRV_MAINTAIN))
456 /* Only notify tracking servers that are not already in maintenance. */
Simon Horman4a741432013-02-23 15:35:38 +0900457 set_server_down(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100459
Simon Horman125d0992013-02-24 17:23:38 +0900460 check->health = 0; /* failure */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461}
462
Simon Horman4a741432013-02-23 15:35:38 +0900463void set_server_up(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100464
Simon Horman4a741432013-02-23 15:35:38 +0900465 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100466 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100467 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100468 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100469
Cyril Bontécd19e512010-01-31 22:34:03 +0100470 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900471 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100472 }
473
Simon Horman58c32972013-11-25 10:46:38 +0900474 if ((s->check.health >= s->check.rise && s->agent.health >= s->agent.rise &&
475 check->health == check->rise) || s->track) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100476 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
477 if (s->proxy->last_change < now.tv_sec) // ignore negative times
478 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
479 s->proxy->last_change = now.tv_sec;
480 }
481
482 if (s->last_change < now.tv_sec) // ignore negative times
483 s->down_time += now.tv_sec - s->last_change;
484
485 s->last_change = now.tv_sec;
486 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100487 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100488
489 if (s->slowstart > 0) {
490 s->state |= SRV_WARMINGUP;
Willy Tarreau2e993902011-10-31 11:53:20 +0100491 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100492 }
Willy Tarreau004e0452013-11-21 11:22:01 +0100493
494 server_recalc_eweight(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100495
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700496 /* If the server is set with "on-marked-up shutdown-backup-sessions",
497 * and it's not a backup server and its effective weight is > 0,
498 * then it can accept new connections, so we shut down all sessions
499 * on all backup servers.
500 */
501 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
502 !(s->state & SRV_BACKUP) && s->eweight)
503 shutdown_backup_sessions(s->proxy, SN_ERR_UP);
504
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100505 /* check if we can handle some connections queued at the proxy. We
506 * will take as many as we can handle.
507 */
508 xferred = check_for_pending(s);
509
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100510 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100511
Willy Tarreau45446782012-03-09 17:16:09 +0100512 if (old_state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100513 chunk_appendf(&trash,
514 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
515 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100516 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100517 chunk_appendf(&trash,
518 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
519 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100520
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100521 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900522 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100523 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100524 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100525
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100526 Warning("%s.\n", trash.str);
527 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100528
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100529 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100530 for(srv = s->tracknext; srv; srv = srv->tracknext)
Cyril Bontécd19e512010-01-31 22:34:03 +0100531 if (! (srv->state & SRV_MAINTAIN))
532 /* Only notify tracking servers if they're not in maintenance. */
Simon Horman4a741432013-02-23 15:35:38 +0900533 set_server_up(check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100534 }
535
Simon Horman58c32972013-11-25 10:46:38 +0900536 if (check->health >= check->rise)
537 check->health = check->rise + check->fall - 1; /* OK now */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100538
539}
540
Simon Horman4a741432013-02-23 15:35:38 +0900541static void set_server_disabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100542
Simon Horman4a741432013-02-23 15:35:38 +0900543 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100544 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100545 int xferred;
546
547 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200548 if (s->proxy->lbprm.set_server_status_down)
549 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100550
551 /* we might have sessions queued on this server and waiting for
552 * a connection. Those which are redispatchable will be queued
553 * to another server or to the proxy itself.
554 */
555 xferred = redistribute_pending(s);
556
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100557 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100558
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100559 chunk_appendf(&trash,
560 "Load-balancing on %sServer %s/%s is disabled",
561 s->state & SRV_BACKUP ? "Backup " : "",
562 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100563
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100564 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900565 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100566 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100567
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100568 Warning("%s.\n", trash.str);
569 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100570
571 if (!s->proxy->srv_bck && !s->proxy->srv_act)
572 set_backend_down(s->proxy);
573
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100574 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100575 for(srv = s->tracknext; srv; srv = srv->tracknext)
Simon Horman4a741432013-02-23 15:35:38 +0900576 set_server_disabled(check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100577}
578
Simon Horman4a741432013-02-23 15:35:38 +0900579static void set_server_enabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100580
Simon Horman4a741432013-02-23 15:35:38 +0900581 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100582 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100583 int xferred;
584
585 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200586 if (s->proxy->lbprm.set_server_status_up)
587 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100588
589 /* check if we can handle some connections queued at the proxy. We
590 * will take as many as we can handle.
591 */
592 xferred = check_for_pending(s);
593
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100594 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100595
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100596 chunk_appendf(&trash,
597 "Load-balancing on %sServer %s/%s is enabled again",
598 s->state & SRV_BACKUP ? "Backup " : "",
599 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100600
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100601 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900602 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100603 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100604
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100605 Warning("%s.\n", trash.str);
606 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100607
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100608 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100609 for(srv = s->tracknext; srv; srv = srv->tracknext)
Simon Horman4a741432013-02-23 15:35:38 +0900610 set_server_enabled(check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100611}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612
Simon Hormand8583062013-11-25 10:46:33 +0900613static void check_failed(struct check *check)
614{
615 struct server *s = check->server;
616
Simon Horman2f1f9552013-11-25 10:46:37 +0900617 /* The agent secondary check should only cause a server to be marked
618 * as down if check->status is HCHK_STATUS_L7STS, which indicates
619 * that the agent returned "fail", "stopped" or "down".
620 * The implication here is that failure to connect to the agent
621 * as a secondary check should not cause the server to be marked
622 * down. */
623 if (check == &s->agent && check->status != HCHK_STATUS_L7STS)
624 return;
625
Simon Horman58c32972013-11-25 10:46:38 +0900626 if (check->health > check->rise) {
Simon Hormand8583062013-11-25 10:46:33 +0900627 check->health--; /* still good */
628 s->counters.failed_checks++;
629 }
630 else
631 set_server_down(check);
632}
633
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100634void health_adjust(struct server *s, short status)
635{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100636 int failed;
637 int expire;
638
639 /* return now if observing nor health check is not enabled */
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200640 if (!s->observe || !s->check.task)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100641 return;
642
643 if (s->observe >= HANA_OBS_SIZE)
644 return;
645
Willy Tarreaubb956662013-01-24 00:37:39 +0100646 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100647 return;
648
649 switch (analyze_statuses[status].lr[s->observe - 1]) {
650 case 1:
651 failed = 1;
652 break;
653
654 case 2:
655 failed = 0;
656 break;
657
658 default:
659 return;
660 }
661
662 if (!failed) {
663 /* good: clear consecutive_errors */
664 s->consecutive_errors = 0;
665 return;
666 }
667
668 s->consecutive_errors++;
669
670 if (s->consecutive_errors < s->consecutive_errors_limit)
671 return;
672
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100673 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
674 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100675
676 switch (s->onerror) {
677 case HANA_ONERR_FASTINTER:
678 /* force fastinter - nothing to do here as all modes force it */
679 break;
680
681 case HANA_ONERR_SUDDTH:
682 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900683 if (s->check.health > s->check.rise)
684 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100685
686 /* no break - fall through */
687
688 case HANA_ONERR_FAILCHK:
689 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900690 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Simon Hormand8583062013-11-25 10:46:33 +0900691 check_failed(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100692
693 break;
694
695 case HANA_ONERR_MARKDWN:
696 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900697 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900698 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
699 set_server_down(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100700
701 break;
702
703 default:
704 /* write a warning? */
705 break;
706 }
707
708 s->consecutive_errors = 0;
709 s->counters.failed_hana++;
710
Simon Horman66183002013-02-23 10:16:43 +0900711 if (s->check.fastinter) {
712 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300713 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200714 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300715 /* requeue check task with new expire */
716 task_queue(s->check.task);
717 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100718 }
719}
720
Willy Tarreauef781042010-01-27 11:53:01 +0100721static int httpchk_build_status_header(struct server *s, char *buffer)
722{
723 int sv_state;
724 int ratio;
725 int hlen = 0;
726 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
727 "UP %d/%d", "UP",
728 "NOLB %d/%d", "NOLB",
729 "no check" };
730
731 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
732 hlen += 24;
733
734 if (!(s->state & SRV_CHECKED))
735 sv_state = 6; /* should obviously never happen */
736 else if (s->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +0900737 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100738 sv_state = 3; /* UP */
739 else
740 sv_state = 2; /* going down */
741
742 if (s->state & SRV_GOINGDOWN)
743 sv_state += 2;
744 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900745 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100746 sv_state = 1; /* going up */
747 else
748 sv_state = 0; /* DOWN */
749 }
750
751 hlen += sprintf(buffer + hlen,
752 srv_hlt_st[sv_state],
Simon Horman58c32972013-11-25 10:46:38 +0900753 (s->state & SRV_RUNNING) ? (s->check.health - s->check.rise + 1) : (s->check.health),
754 (s->state & SRV_RUNNING) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100755
756 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
757 s->proxy->id, s->id,
758 global.node,
759 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
760 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
761 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
762 s->nbpend);
763
764 if ((s->state & SRV_WARMINGUP) &&
765 now.tv_sec < s->last_change + s->slowstart &&
766 now.tv_sec >= s->last_change) {
767 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
768 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
769 }
770
771 buffer[hlen++] = '\r';
772 buffer[hlen++] = '\n';
773
774 return hlen;
775}
776
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777/*
778 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200779 * the connection acknowledgement. If the proxy requires L7 health-checks,
780 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900781 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200783static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784{
Simon Horman4a741432013-02-23 15:35:38 +0900785 struct check *check = conn->owner;
786 struct server *s = check->server;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200787 int fd = conn->t.sock.fd;
Simon Horman4a741432013-02-23 15:35:38 +0900788 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200789
Willy Tarreau5a78f362012-11-23 12:47:05 +0100790 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200791 conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200793 if (unlikely(conn->flags & CO_FL_ERROR)) {
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100794 int skerr, err = errno;
795 socklen_t lskerr = sizeof(skerr);
796
797 if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr)
798 err = skerr;
799
Simon Horman4a741432013-02-23 15:35:38 +0900800 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(err));
Willy Tarreau6996e152007-04-30 14:37:43 +0200801 goto out_error;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200802 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200803
Willy Tarreau5a78f362012-11-23 12:47:05 +0100804 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_WR))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200805 return;
Willy Tarreau83749182007-04-15 20:56:27 +0200806
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200807 /* here, we know that the connection is established */
Simon Horman4a741432013-02-23 15:35:38 +0900808 if (!(check->result & SRV_CHK_FAILED)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Simon Horman4a741432013-02-23 15:35:38 +0900810 if (check->bo->o) {
811 conn->xprt->snd_buf(conn, check->bo, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauf1503172012-09-28 19:39:36 +0200812 if (conn->flags & CO_FL_ERROR) {
Simon Horman4a741432013-02-23 15:35:38 +0900813 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreauf1503172012-09-28 19:39:36 +0200814 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200815 }
Simon Horman4a741432013-02-23 15:35:38 +0900816 if (check->bo->o) {
Willy Tarreau5a78f362012-11-23 12:47:05 +0100817 goto out_incomplete;
818 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200820
Willy Tarreau5a78f362012-11-23 12:47:05 +0100821 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
822 if (s->proxy->timeout.check) {
823 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
824 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 }
Willy Tarreau5a78f362012-11-23 12:47:05 +0100826 goto out_nowake;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 }
Willy Tarreau83749182007-04-15 20:56:27 +0200828 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200829 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200830 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200831 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreau5a78f362012-11-23 12:47:05 +0100832 out_incomplete:
Willy Tarreau3267d362012-08-17 23:53:56 +0200833 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200834 out_error:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200835 conn->flags |= CO_FL_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200836 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200837}
838
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200840 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200841 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900842 * set_server_check_status() to update check->status, check->duration
843 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200844
845 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
846 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
847 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
848 * response to an SSL HELLO (the principle is that this is enough to
849 * distinguish between an SSL server and a pure TCP relay). All other cases will
850 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
851 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200853static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854{
Simon Horman4a741432013-02-23 15:35:38 +0900855 struct check *check = conn->owner;
856 struct server *s = check->server;
857 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200858 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100859 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200860 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200861
Simon Horman4a741432013-02-23 15:35:38 +0900862 if (unlikely((check->result & SRV_CHK_FAILED) || (conn->flags & CO_FL_ERROR))) {
Willy Tarreau83749182007-04-15 20:56:27 +0200863 /* in case of TCP only, this tells us if the connection failed */
Simon Horman4a741432013-02-23 15:35:38 +0900864 if (!(check->result & SRV_CHK_FAILED))
865 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreau83749182007-04-15 20:56:27 +0200866 goto out_wakeup;
867 }
868
Willy Tarreau5a78f362012-11-23 12:47:05 +0100869 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_RD))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200870 return;
871
Willy Tarreau83749182007-04-15 20:56:27 +0200872 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
873 * but the connection was closed on the remote end. Fortunately, recv still
874 * works correctly and we don't need to do the getsockopt() on linux.
875 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000876
877 /* Set buffer to point to the end of the data already read, and check
878 * that there is free space remaining. If the buffer is full, proceed
879 * with running the checks without attempting another socket read.
880 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000881
Willy Tarreau03938182010-03-17 21:52:07 +0100882 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000883
Simon Horman4a741432013-02-23 15:35:38 +0900884 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200885 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100886 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900887 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200888 /* Report network errors only if we got no other data. Otherwise
889 * we'll let the upper layers decide whether the response is OK
890 * or not. It is very common that an RST sent by the server is
891 * reported as an error just after the last data chunk.
892 */
Simon Horman4a741432013-02-23 15:35:38 +0900893 if (!(check->result & SRV_CHK_FAILED))
894 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100895 goto out_wakeup;
896 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 }
898
Willy Tarreau03938182010-03-17 21:52:07 +0100899 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900900 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100901 */
Simon Horman4a741432013-02-23 15:35:38 +0900902 if (check->bi->i < check->bi->size)
903 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100904 else {
Simon Horman4a741432013-02-23 15:35:38 +0900905 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100906 done = 1; /* buffer full, don't wait for more data */
907 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200908
Nick Chalk57b1bf72010-03-16 15:50:46 +0000909 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900910 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200911 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900912 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100913 goto wait_more_data;
914
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100915 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900916 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
917 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
918 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
919 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
920 !isdigit((unsigned char) *(check->bi->data + 11))) {
921 cut_crlf(check->bi->data);
922 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200923
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100924 goto out_wakeup;
925 }
926
Simon Horman4a741432013-02-23 15:35:38 +0900927 check->code = str2uic(check->bi->data + 9);
928 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000929
Willy Tarreaubd741542010-03-16 18:46:54 +0100930 if ((s->proxy->options & PR_O_DISABLE404) &&
Simon Horman4a741432013-02-23 15:35:38 +0900931 (s->state & SRV_RUNNING) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000932 /* 404 may be accepted as "stopping" only if the server was up */
933 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900934 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000935 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100936 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
937 /* Run content verification check... We know we have at least 13 chars */
938 if (!httpchk_expect(s, done))
939 goto wait_more_data;
940 }
941 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900942 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100943 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900944 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100945 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000946 else {
947 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900948 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000949 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200950 break;
951
952 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900953 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100954 goto wait_more_data;
955
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100956 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900957 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
958 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200959 else
Simon Horman4a741432013-02-23 15:35:38 +0900960 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200961 break;
962
963 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900964 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100965 goto wait_more_data;
966
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200967 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900968 if ((check->bi->i < strlen("000\r")) ||
969 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
970 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
971 !isdigit((unsigned char) *(check->bi->data + 2))) {
972 cut_crlf(check->bi->data);
973 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200974
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200975 goto out_wakeup;
976 }
977
Simon Horman4a741432013-02-23 15:35:38 +0900978 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200979
Simon Horman4a741432013-02-23 15:35:38 +0900980 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200981 cut_crlf(desc);
982
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100983 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900984 if (*check->bi->data == '2')
985 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200986 else
Simon Horman4a741432013-02-23 15:35:38 +0900987 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200988 break;
989
Simon Hormana2b9dad2013-02-12 10:45:54 +0900990 case PR_O2_LB_AGENT_CHK: {
991 short status = HCHK_STATUS_L7RSP;
992 const char *desc = "Unknown feedback string";
993 const char *down_cmd = NULL;
Simon Horman671b6f02013-11-25 10:46:39 +0900994 int disabled;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900995
996 if (!done)
997 goto wait_more_data;
998
Simon Horman4a741432013-02-23 15:35:38 +0900999 cut_crlf(check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001000
Simon Horman671b6f02013-11-25 10:46:39 +09001001 /*
1002 * The agent may have been disabled after a check was
1003 * initialised. If so, ignore weight changes and drain
1004 * settings from the agent. Note that the setting is
1005 * always present in the state of the agent the server,
1006 * regardless of if the agent is being run as a primary or
1007 * secondary check. That is, regardless of if the check
1008 * parameter of this function is the agent or check field
1009 * of the server.
1010 */
1011 disabled = check->server->agent.state & CHK_STATE_DISABLED;
1012
Simon Horman4a741432013-02-23 15:35:38 +09001013 if (strchr(check->bi->data, '%')) {
Simon Horman671b6f02013-11-25 10:46:39 +09001014 if (disabled)
1015 break;
Simon Horman4a741432013-02-23 15:35:38 +09001016 desc = server_parse_weight_change_request(s, check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001017 if (!desc) {
1018 status = HCHK_STATUS_L7OKD;
Simon Horman4a741432013-02-23 15:35:38 +09001019 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001020 }
Simon Horman4a741432013-02-23 15:35:38 +09001021 } else if (!strcasecmp(check->bi->data, "drain")) {
Simon Horman671b6f02013-11-25 10:46:39 +09001022 if (disabled)
1023 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001024 desc = server_parse_weight_change_request(s, "0%");
1025 if (!desc) {
1026 desc = "drain";
1027 status = HCHK_STATUS_L7OKD;
1028 }
Simon Horman4a741432013-02-23 15:35:38 +09001029 } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001030 down_cmd = "down";
Simon Horman4a741432013-02-23 15:35:38 +09001031 } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001032 down_cmd = "stopped";
Simon Horman4a741432013-02-23 15:35:38 +09001033 } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001034 down_cmd = "fail";
1035 }
1036
1037 if (down_cmd) {
Simon Horman4a741432013-02-23 15:35:38 +09001038 const char *end = check->bi->data + strlen(down_cmd);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001039 /*
1040 * The command keyword must terminated the string or
1041 * be followed by a blank.
1042 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001043 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001044 status = HCHK_STATUS_L7STS;
Simon Horman80fefae2013-11-25 10:46:34 +09001045 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001046 }
1047 }
1048
Simon Horman4a741432013-02-23 15:35:38 +09001049 set_server_check_status(check, status, desc);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001050 break;
1051 }
1052
Willy Tarreau1620ec32011-08-06 17:05:02 +02001053 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001054 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001055 goto wait_more_data;
1056
Simon Horman4a741432013-02-23 15:35:38 +09001057 if (check->bi->data[0] == 'R') {
1058 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001059 }
1060 else {
Simon Horman4a741432013-02-23 15:35:38 +09001061 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1062 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001063 else
1064 desc = "PostgreSQL unknown error";
1065
Simon Horman4a741432013-02-23 15:35:38 +09001066 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001067 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001068 break;
1069
1070 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001071 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001072 goto wait_more_data;
1073
Simon Horman4a741432013-02-23 15:35:38 +09001074 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1075 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001076 }
1077 else {
Simon Horman4a741432013-02-23 15:35:38 +09001078 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001079 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001080 break;
1081
1082 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001083 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001084 goto wait_more_data;
1085
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001086 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001087 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001088 /* We set the MySQL Version in description for information purpose
1089 * FIXME : it can be cool to use MySQL Version for other purpose,
1090 * like mark as down old MySQL server.
1091 */
Simon Horman4a741432013-02-23 15:35:38 +09001092 if (check->bi->i > 51) {
1093 desc = ltrim(check->bi->data + 5, ' ');
1094 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001095 }
1096 else {
1097 if (!done)
1098 goto wait_more_data;
1099 /* it seems we have a OK packet but without a valid length,
1100 * it must be a protocol error
1101 */
Simon Horman4a741432013-02-23 15:35:38 +09001102 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001103 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001104 }
1105 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001106 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001107 desc = ltrim(check->bi->data + 7, ' ');
1108 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001109 }
1110 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001111 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1112 (((unsigned int) *(check->bi->data + 1)) << 8) +
1113 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001114
Simon Horman4a741432013-02-23 15:35:38 +09001115 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001116 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001117 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001118 /* We have only one MySQL packet and it is a Handshake Initialization packet
1119 * but we need to have a second packet to know if it is alright
1120 */
Simon Horman4a741432013-02-23 15:35:38 +09001121 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001122 goto wait_more_data;
1123 }
1124 else {
1125 /* We have only one packet and it is an Error packet,
1126 * an error message is attached, so we can display it
1127 */
Simon Horman4a741432013-02-23 15:35:38 +09001128 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001129 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001130 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001131 }
Simon Horman4a741432013-02-23 15:35:38 +09001132 } else if (check->bi->i > first_packet_len + 4) {
1133 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1134 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1135 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001136
Simon Horman4a741432013-02-23 15:35:38 +09001137 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001138 /* We have 2 packets and that's good */
1139 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001140 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001141 /* No error packet */
1142 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001143 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001144 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001145 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001146 }
1147 else {
1148 /* An error message is attached in the Error packet
1149 * so we can display it ! :)
1150 */
Simon Horman4a741432013-02-23 15:35:38 +09001151 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001152 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001153 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001154 }
1155 }
1156 }
1157 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001158 if (!done)
1159 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001160 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001161 * it must be a protocol error
1162 */
Simon Horman4a741432013-02-23 15:35:38 +09001163 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001164 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001165 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001166 }
1167 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001168 break;
1169
1170 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001171 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001172 goto wait_more_data;
1173
1174 /* Check if the server speaks LDAP (ASN.1/BER)
1175 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1176 * http://tools.ietf.org/html/rfc4511
1177 */
1178
1179 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1180 * LDAPMessage: 0x30: SEQUENCE
1181 */
Simon Horman4a741432013-02-23 15:35:38 +09001182 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1183 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001184 }
1185 else {
1186 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001187 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001188
1189 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1190 * messageID: 0x02 0x01 0x01: INTEGER 1
1191 * protocolOp: 0x61: bindResponse
1192 */
1193 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001194 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1195 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001196
1197 goto out_wakeup;
1198 }
1199
1200 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001201 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001202
1203 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1204 * ldapResult: 0x0a 0x01: ENUMERATION
1205 */
1206 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001207 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1208 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001209
1210 goto out_wakeup;
1211 }
1212
1213 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1214 * resultCode
1215 */
Simon Horman4a741432013-02-23 15:35:38 +09001216 check->code = *(check->bi->data + msglen + 9);
1217 if (check->code) {
1218 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 +02001219 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001220 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001221 }
1222 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001223 break;
1224
1225 default:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001226 /* other checks are valid if the connection succeeded anyway */
Simon Horman4a741432013-02-23 15:35:38 +09001227 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001228 break;
1229 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001230
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001231 out_wakeup:
Simon Horman4a741432013-02-23 15:35:38 +09001232 if (check->result & SRV_CHK_FAILED)
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001233 conn->flags |= CO_FL_ERROR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001234
Nick Chalk57b1bf72010-03-16 15:50:46 +00001235 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001236 *check->bi->data = '\0';
1237 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001238
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001239 /* Close the connection... We absolutely want to perform a hard close
1240 * and reset the connection if some data are pending, otherwise we end
1241 * up with many TIME_WAITs and eat all the source port range quickly.
1242 * To avoid sending RSTs all the time, we first try to drain pending
1243 * data.
1244 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001245 if (conn->xprt && conn->xprt->shutw)
1246 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001247
1248 if (conn->ctrl && !(conn->flags & CO_FL_SOCK_RD_SH)) {
1249 if (conn->flags & CO_FL_WAIT_RD || !conn->ctrl->drain || !conn->ctrl->drain(conn->t.sock.fd))
1250 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1251 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreaucfd97c62012-11-23 17:35:59 +01001252 }
Willy Tarreaua522f802012-11-23 08:56:35 +01001253 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001254 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001255 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001256
1257 wait_more_data:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001258 __conn_data_poll_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001259}
1260
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001261/*
1262 * This function is used only for server health-checks. It handles connection
1263 * status updates including errors. If necessary, it wakes the check task up.
1264 * It always returns 0.
1265 */
1266static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001267{
Simon Horman4a741432013-02-23 15:35:38 +09001268 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001269
Willy Tarreau6c560da2012-11-24 11:14:45 +01001270 if (unlikely(conn->flags & CO_FL_ERROR)) {
1271 /* Note that we might as well have been woken up by a handshake handler */
Simon Horman4a741432013-02-23 15:35:38 +09001272 if (check->result == SRV_CHK_UNKNOWN)
1273 check->result |= SRV_CHK_FAILED;
Willy Tarreau6c560da2012-11-24 11:14:45 +01001274 __conn_data_stop_both(conn);
Simon Horman4a741432013-02-23 15:35:38 +09001275 task_wakeup(check->task, TASK_WOKEN_IO);
Willy Tarreau6c560da2012-11-24 11:14:45 +01001276 }
Willy Tarreau20bea422012-07-06 12:00:49 +02001277
Simon Horman4a741432013-02-23 15:35:38 +09001278 if (check->result & (SRV_CHK_FAILED|SRV_CHK_PASSED))
Willy Tarreau2b199c92012-11-23 17:32:21 +01001279 conn_full_close(conn);
Willy Tarreau3267d362012-08-17 23:53:56 +02001280 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001281}
1282
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001283struct data_cb check_conn_cb = {
1284 .recv = event_srv_chk_r,
1285 .send = event_srv_chk_w,
1286 .wake = wake_srv_chk,
1287};
1288
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001290 * updates the server's weight during a warmup stage. Once the final weight is
1291 * reached, the task automatically stops. Note that any server status change
1292 * must have updated s->last_change accordingly.
1293 */
1294static struct task *server_warmup(struct task *t)
1295{
1296 struct server *s = t->context;
1297
1298 /* by default, plan on stopping the task */
1299 t->expire = TICK_ETERNITY;
1300 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1301 return t;
1302
Willy Tarreau004e0452013-11-21 11:22:01 +01001303 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001304
1305 /* probably that we can refill this server with a bit more connections */
1306 check_for_pending(s);
1307
1308 /* get back there in 1 second or 1/20th of the slowstart interval,
1309 * whichever is greater, resulting in small 5% steps.
1310 */
1311 if (s->state & SRV_WARMINGUP)
1312 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1313 return t;
1314}
1315
1316/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317 * manages a server health-check. Returns
1318 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1319 */
Simon Horman63a4a822012-03-19 07:24:41 +09001320static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001321{
Simon Horman4a741432013-02-23 15:35:38 +09001322 struct check *check = t->context;
1323 struct server *s = check->server;
1324 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001325 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001326 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001327 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001328
Simon Hormancd5d7b62013-02-24 17:23:38 +09001329 if (!(check->state & CHK_STATE_RUNNING)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001330 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001331 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001332 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001333
Simon Horman671b6f02013-11-25 10:46:39 +09001334 /* we don't send any health-checks when the proxy is
1335 * stopped, the server should not be checked or the check
1336 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001337 */
Simon Horman671b6f02013-11-25 10:46:39 +09001338 if (!(s->state & SRV_CHECKED) ||
1339 s->proxy->state == PR_STSTOPPED ||
1340 (s->state & SRV_MAINTAIN) ||
1341 (check->state & CHK_STATE_DISABLED))
Willy Tarreau5a78f362012-11-23 12:47:05 +01001342 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001343
1344 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001345 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001346
Simon Hormancd5d7b62013-02-24 17:23:38 +09001347 check->state |= CHK_STATE_RUNNING;
Simon Horman4a741432013-02-23 15:35:38 +09001348 check->bi->p = check->bi->data;
1349 check->bi->i = 0;
1350 check->bo->p = check->bo->data;
1351 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001352
Simon Hormand60d6912013-11-25 10:46:36 +09001353 /* prepare the check buffer
1354 * This should not be used if check is the secondary agent check
1355 * of a server as s->proxy->check_req will relate to the
1356 * configuration of the primary check */
1357 if (check->type && check != &s->agent) {
Simon Horman4a741432013-02-23 15:35:38 +09001358 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001359
1360 /* we want to check if this host replies to HTTP or SSLv3 requests
1361 * so we'll send the request, and won't wake the checker up now.
1362 */
Simon Horman4a741432013-02-23 15:35:38 +09001363 if ((check->type) == PR_O2_SSL3_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001364 /* SSL requires that we put Unix time in the request */
1365 int gmt_time = htonl(date.tv_sec);
Simon Horman4a741432013-02-23 15:35:38 +09001366 memcpy(check->bo->data + 11, &gmt_time, 4);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001367 }
Simon Horman4a741432013-02-23 15:35:38 +09001368 else if ((check->type) == PR_O2_HTTP_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001369 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Simon Horman4a741432013-02-23 15:35:38 +09001370 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str));
1371 bo_putstr(check->bo, "\r\n");
1372 *check->bo->p = '\0'; /* to make gdb output easier to read */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001373 }
1374 }
1375
1376 /* prepare a new connection */
Willy Tarreaub63b5962012-11-23 16:22:08 +01001377 conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01001378 conn->err_code = CO_ER_NONE;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001379 conn->target = &s->obj_type;
Simon Horman4a741432013-02-23 15:35:38 +09001380 conn_prepare(conn, &check_conn_cb, s->check_common.proto, s->check_common.xprt, check);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001381
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001382 /* no client address */
1383 clear_addr(&conn->addr.from);
1384
Simon Horman66183002013-02-23 10:16:43 +09001385 if (is_addr(&s->check_common.addr))
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001386 /* we'll connect to the check addr specified on the server */
Simon Horman66183002013-02-23 10:16:43 +09001387 conn->addr.to = s->check_common.addr;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001388 else
1389 /* we'll connect to the addr on the server */
1390 conn->addr.to = s->addr;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001391
Simon Horman4a741432013-02-23 15:35:38 +09001392 set_host_port(&conn->addr.to, check->port);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001393
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001394 /* It can return one of :
1395 * - SN_ERR_NONE if everything's OK
1396 * - SN_ERR_SRVTO if there are no more servers
1397 * - SN_ERR_SRVCL if the connection was refused by the server
1398 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1399 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1400 * - SN_ERR_INTERNAL for any other purely internal errors
1401 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001402 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001403 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001404 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001405 ret = SN_ERR_INTERNAL;
Simon Horman66183002013-02-23 10:16:43 +09001406 if (s->check_common.proto->connect)
Simon Horman4a741432013-02-23 15:35:38 +09001407 ret = s->check_common.proto->connect(conn, check->type,
1408 s->check.send_proxy ? 1 : (check->type) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001409 conn->flags |= CO_FL_WAKE_DATA;
Simon Horman4a741432013-02-23 15:35:38 +09001410 if (check->send_proxy)
Willy Tarreau6c16adc2012-10-05 00:04:16 +02001411 conn->flags |= CO_FL_LOCAL_SPROXY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001413 switch (ret) {
1414 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001415 /* we allow up to min(inter, timeout.connect) for a connection
1416 * to establish but only when timeout.check is set
1417 * as it may be to short for a full check otherwise
1418 */
Simon Horman4a741432013-02-23 15:35:38 +09001419 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001420
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001421 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1422 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1423 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424 }
Willy Tarreaudb3b4a22012-11-23 16:32:33 +01001425 conn_data_poll_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau5a78f362012-11-23 12:47:05 +01001426 goto reschedule;
1427
1428 case SN_ERR_SRVTO: /* ETIMEDOUT */
1429 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman4a741432013-02-23 15:35:38 +09001430 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau5a78f362012-11-23 12:47:05 +01001431 break;
1432 case SN_ERR_PRXCOND:
1433 case SN_ERR_RESOURCE:
1434 case SN_ERR_INTERNAL:
Simon Horman4a741432013-02-23 15:35:38 +09001435 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001436 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001437 }
1438
Willy Tarreau5a78f362012-11-23 12:47:05 +01001439 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001440
Simon Hormancd5d7b62013-02-24 17:23:38 +09001441 check->state &= ~CHK_STATE_RUNNING;
Simon Hormand8583062013-11-25 10:46:33 +09001442 check_failed(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001443
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001444 /* we allow up to min(inter, timeout.connect) for a connection
1445 * to establish but only when timeout.check is set
1446 * as it may be to short for a full check otherwise
1447 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001448 while (tick_is_expired(t->expire, now_ms)) {
1449 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001450
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001451 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001452 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001453
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001454 if (s->proxy->timeout.check)
1455 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001456 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001457 }
1458 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001459 /* there was a test running.
1460 * First, let's check whether there was an uncaught error,
1461 * which can happen on connect timeout or error.
1462 */
Simon Horman28b5ffc2013-02-24 07:25:29 +09001463 if (s->check.result == SRV_CHK_UNKNOWN) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001464 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
1465 /* L4 not established (yet) */
Willy Tarreauf1503172012-09-28 19:39:36 +02001466 if (conn->flags & CO_FL_ERROR)
Simon Horman4a741432013-02-23 15:35:38 +09001467 set_server_check_status(check, HCHK_STATUS_L4CON, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001468 else if (expired)
Simon Horman4a741432013-02-23 15:35:38 +09001469 set_server_check_status(check, HCHK_STATUS_L4TOUT, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001470 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001471 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001472 /* L6 not established (yet) */
Willy Tarreauf1503172012-09-28 19:39:36 +02001473 if (conn->flags & CO_FL_ERROR)
Simon Horman4a741432013-02-23 15:35:38 +09001474 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001475 else if (expired)
Simon Horman4a741432013-02-23 15:35:38 +09001476 set_server_check_status(check, HCHK_STATUS_L6TOUT, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001477 }
Simon Horman4a741432013-02-23 15:35:38 +09001478 else if (!check->type) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001479 /* good connection is enough for pure TCP check */
Simon Horman4a741432013-02-23 15:35:38 +09001480 if (check->use_ssl)
1481 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001482 else
Simon Horman4a741432013-02-23 15:35:38 +09001483 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001484 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001485 else if (expired) {
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001486 /* connection established but expired check */
Simon Horman4a741432013-02-23 15:35:38 +09001487 if (check->type == PR_O2_SSL3_CHK)
1488 set_server_check_status(check, HCHK_STATUS_L6TOUT, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001489 else /* HTTP, SMTP, ... */
Simon Horman4a741432013-02-23 15:35:38 +09001490 set_server_check_status(check, HCHK_STATUS_L7TOUT, NULL);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001491
Willy Tarreauf1503172012-09-28 19:39:36 +02001492 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001493 else
1494 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001495 }
1496
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001497 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001498 if (conn->xprt) {
1499 /* The check was aborted and the connection was not yet closed.
1500 * This can happen upon timeout, or when an external event such
1501 * as a failed response coupled with "observe layer7" caused the
1502 * server state to be suddenly changed.
1503 */
1504 if (conn->ctrl)
1505 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1506 (struct linger *) &nolinger, sizeof(struct linger));
1507 conn_full_close(conn);
1508 }
1509
Simon Hormand8583062013-11-25 10:46:33 +09001510 if (check->result & SRV_CHK_FAILED) /* a failure or timeout detected */
1511 check_failed(check);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001512 else { /* check was OK */
Willy Tarreau48494c02007-11-30 10:41:39 +01001513 /* we may have to add/remove this server from the LB group */
1514 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
Simon Horman4a741432013-02-23 15:35:38 +09001515 if ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE))
1516 set_server_enabled(check);
1517 else if (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE))
1518 set_server_disabled(check);
Willy Tarreau48494c02007-11-30 10:41:39 +01001519 }
1520
Simon Horman58c32972013-11-25 10:46:38 +09001521 if (check->health < check->rise + check->fall - 1) {
Simon Horman125d0992013-02-24 17:23:38 +09001522 check->health++; /* was bad, stays for a while */
Simon Horman4a741432013-02-23 15:35:38 +09001523 set_server_up(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001524 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001525 }
Simon Hormancd5d7b62013-02-24 17:23:38 +09001526 check->state &= ~CHK_STATE_RUNNING;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001527
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001528 rv = 0;
1529 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001530 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001531 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001532 }
Simon Horman4a741432013-02-23 15:35:38 +09001533 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001534 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001535
1536 reschedule:
1537 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09001538 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001539 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001540 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001541}
1542
Simon Horman5c942422013-11-25 10:46:32 +09001543static int start_check_task(struct check *check, int mininter,
1544 int nbcheck, int srvpos)
1545{
1546 struct task *t;
1547 /* task for the check */
1548 if ((t = task_new()) == NULL) {
1549 Alert("Starting [%s:%s] check: out of memory.\n",
1550 check->server->proxy->id, check->server->id);
1551 return 0;
1552 }
1553
1554 check->task = t;
1555 t->process = process_chk;
1556 t->context = check;
1557
1558 /* check this every ms */
1559 t->expire = tick_add(now_ms,
1560 MS_TO_TICKS(((mininter &&
1561 mininter >= srv_getinter(check)) ?
1562 mininter : srv_getinter(check)) * srvpos / nbcheck));
1563 check->start = now;
1564 task_queue(t);
1565
1566 return 1;
1567}
1568
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001569/*
1570 * Start health-check.
1571 * Returns 0 if OK, -1 if error, and prints the error in this case.
1572 */
1573int start_checks() {
1574
1575 struct proxy *px;
1576 struct server *s;
1577 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09001578 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001579
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001580 /* 1- count the checkers to run simultaneously.
1581 * We also determine the minimum interval among all of those which
1582 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1583 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001584 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001585 * too short an interval for all others.
1586 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001587 for (px = proxy; px; px = px->next) {
1588 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01001589 if (s->slowstart) {
1590 if ((t = task_new()) == NULL) {
1591 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1592 return -1;
1593 }
1594 /* We need a warmup task that will be called when the server
1595 * state switches from down to up.
1596 */
1597 s->warmup = t;
1598 t->process = server_warmup;
1599 t->context = s;
1600 t->expire = TICK_ETERNITY;
1601 }
1602
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001603 if (!(s->state & SRV_CHECKED))
1604 continue;
1605
Simon Horman4a741432013-02-23 15:35:38 +09001606 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1607 (!mininter || mininter > srv_getinter(&s->check)))
1608 mininter = srv_getinter(&s->check);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001609
Simon Horman4a741432013-02-23 15:35:38 +09001610 nbcheck++;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001611 }
1612 }
1613
Simon Horman4a741432013-02-23 15:35:38 +09001614 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001615 return 0;
1616
1617 srand((unsigned)time(NULL));
1618
1619 /*
1620 * 2- start them as far as possible from each others. For this, we will
1621 * start them after their interval set to the min interval divided by
1622 * the number of servers, weighted by the server's position in the list.
1623 */
1624 for (px = proxy; px; px = px->next) {
1625 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09001626 /* A task for the main check */
1627 if (s->state & SRV_CHECKED) {
1628 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1629 return -1;
1630 srvpos++;
1631 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001632
Simon Hormand60d6912013-11-25 10:46:36 +09001633 /* A task for a auxiliary agent check */
1634 if (s->state & SRV_AGENT_CHECKED) {
1635 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1636 return -1;
1637 }
1638 srvpos++;
1639 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001640 }
1641 }
1642 return 0;
1643}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001644
1645/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001646 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001647 * The buffer MUST be terminated by a null byte before calling this function.
1648 * Sets server status appropriately. The caller is responsible for ensuring
1649 * that the buffer contains at least 13 characters. If <done> is zero, we may
1650 * return 0 to indicate that data is required to decide of a match.
1651 */
1652static int httpchk_expect(struct server *s, int done)
1653{
1654 static char status_msg[] = "HTTP status check returned code <000>";
1655 char status_code[] = "000";
1656 char *contentptr;
1657 int crlf;
1658 int ret;
1659
1660 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1661 case PR_O2_EXP_STS:
1662 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001663 memcpy(status_code, s->check.bi->data + 9, 3);
1664 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001665
1666 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1667 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1668 else
1669 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1670
1671 /* we necessarily have the response, so there are no partial failures */
1672 if (s->proxy->options2 & PR_O2_EXP_INV)
1673 ret = !ret;
1674
Simon Horman4a741432013-02-23 15:35:38 +09001675 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01001676 break;
1677
1678 case PR_O2_EXP_STR:
1679 case PR_O2_EXP_RSTR:
1680 /* very simple response parser: ignore CR and only count consecutive LFs,
1681 * stop with contentptr pointing to first char after the double CRLF or
1682 * to '\0' if crlf < 2.
1683 */
1684 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001685 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001686 if (crlf >= 2)
1687 break;
1688 if (*contentptr == '\r')
1689 continue;
1690 else if (*contentptr == '\n')
1691 crlf++;
1692 else
1693 crlf = 0;
1694 }
1695
1696 /* Check that response contains a body... */
1697 if (crlf < 2) {
1698 if (!done)
1699 return 0;
1700
Simon Horman4a741432013-02-23 15:35:38 +09001701 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001702 "HTTP content check could not find a response body");
1703 return 1;
1704 }
1705
1706 /* Check that response body is not empty... */
1707 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001708 if (!done)
1709 return 0;
1710
Simon Horman4a741432013-02-23 15:35:38 +09001711 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001712 "HTTP content check found empty response body");
1713 return 1;
1714 }
1715
1716 /* Check the response content against the supplied string
1717 * or regex... */
1718 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1719 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1720 else
1721 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1722
1723 /* if we don't match, we may need to wait more */
1724 if (!ret && !done)
1725 return 0;
1726
1727 if (ret) {
1728 /* content matched */
1729 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001730 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001731 "HTTP check matched unwanted content");
1732 else
Simon Horman4a741432013-02-23 15:35:38 +09001733 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001734 "HTTP content check matched");
1735 }
1736 else {
1737 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001738 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001739 "HTTP check did not match unwanted content");
1740 else
Simon Horman4a741432013-02-23 15:35:38 +09001741 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001742 "HTTP content check did not match");
1743 }
1744 break;
1745 }
1746 return 1;
1747}
1748
1749/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001750 * Local variables:
1751 * c-indent-level: 8
1752 * c-basic-offset: 8
1753 * End:
1754 */