Coverage Report

Created: 2026-04-03 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/haproxy/src/check.c
Line
Count
Source
1
/*
2
 * Health-checks functions.
3
 *
4
 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5
 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6
 *
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
14
#include <assert.h>
15
#include <ctype.h>
16
#include <errno.h>
17
#include <stdarg.h>
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <time.h>
22
#include <unistd.h>
23
#include <sys/resource.h>
24
#include <sys/socket.h>
25
#include <sys/types.h>
26
#include <sys/wait.h>
27
#include <netinet/in.h>
28
#include <netinet/tcp.h>
29
#include <arpa/inet.h>
30
31
#include <haproxy/action.h>
32
#include <haproxy/api.h>
33
#include <haproxy/arg.h>
34
#include <haproxy/cfgparse.h>
35
#include <haproxy/check.h>
36
#include <haproxy/chunk.h>
37
#include <haproxy/counters-t.h>
38
#include <haproxy/dgram.h>
39
#include <haproxy/dynbuf.h>
40
#include <haproxy/extcheck.h>
41
#include <haproxy/fd.h>
42
#include <haproxy/global.h>
43
#include <haproxy/h1.h>
44
#include <haproxy/http.h>
45
#include <haproxy/http_htx.h>
46
#include <haproxy/htx.h>
47
#include <haproxy/istbuf.h>
48
#include <haproxy/list.h>
49
#include <haproxy/log.h>
50
#include <haproxy/mailers.h>
51
#include <haproxy/port_range.h>
52
#include <haproxy/proto_tcp.h>
53
#include <haproxy/protocol.h>
54
#include <haproxy/proxy.h>
55
#include <haproxy/queue.h>
56
#include <haproxy/regex.h>
57
#include <haproxy/resolvers.h>
58
#include <haproxy/sample.h>
59
#include <haproxy/server.h>
60
#include <haproxy/ssl_sock.h>
61
#include <haproxy/task.h>
62
#include <haproxy/tcpcheck.h>
63
#include <haproxy/thread.h>
64
#include <haproxy/time.h>
65
#include <haproxy/tools.h>
66
#include <haproxy/trace.h>
67
#include <haproxy/vars.h>
68
69
/* trace source and events */
70
static void check_trace(enum trace_level level, uint64_t mask,
71
      const struct trace_source *src,
72
      const struct ist where, const struct ist func,
73
      const void *a1, const void *a2, const void *a3, const void *a4);
74
75
/* The event representation is split like this :
76
 *   check  - check
77
 *
78
 * CHECK_EV_* macros are defined in <haproxy/check.h>
79
 */
80
static const struct trace_event check_trace_events[] = {
81
  { .mask = CHK_EV_TASK_WAKE,   .name = "task_wake",        .desc = "Check task woken up" },
82
  { .mask = CHK_EV_HCHK_START,  .name = "hchck_start",      .desc = "Health-check started" },
83
  { .mask = CHK_EV_HCHK_WAKE,   .name = "hchck_wake",       .desc = "Health-check woken up" },
84
  { .mask = CHK_EV_HCHK_RUN,    .name = "hchck_run",        .desc = "Health-check running" },
85
  { .mask = CHK_EV_HCHK_END,    .name = "hchck_end",        .desc = "Health-check terminated" },
86
  { .mask = CHK_EV_HCHK_SUCC,   .name = "hchck_succ",       .desc = "Health-check success" },
87
  { .mask = CHK_EV_HCHK_ERR,    .name = "hchck_err",        .desc = "Health-check failure" },
88
89
  { .mask = CHK_EV_TCPCHK_EVAL, .name = "tcp_check_eval",   .desc = "tcp-check rules evaluation" },
90
  { .mask = CHK_EV_TCPCHK_ERR,  .name = "tcp_check_err",    .desc = "tcp-check evaluation error" },
91
  { .mask = CHK_EV_TCPCHK_CONN, .name = "tcp_check_conn",   .desc = "tcp-check connection rule" },
92
  { .mask = CHK_EV_TCPCHK_SND,  .name = "tcp_check_send",   .desc = "tcp-check send rule" },
93
  { .mask = CHK_EV_TCPCHK_EXP,  .name = "tcp_check_expect", .desc = "tcp-check expect rule" },
94
  { .mask = CHK_EV_TCPCHK_ACT,  .name = "tcp_check_action", .desc = "tcp-check action rule" },
95
96
  { .mask = CHK_EV_RX_DATA,     .name = "rx_data",          .desc = "receipt of data" },
97
  { .mask = CHK_EV_RX_BLK,      .name = "rx_blk",           .desc = "receipt blocked" },
98
  { .mask = CHK_EV_RX_ERR,      .name = "rx_err",           .desc = "receipt error" },
99
100
  { .mask = CHK_EV_TX_DATA,     .name = "tx_data",          .desc = "transmission of data" },
101
  { .mask = CHK_EV_TX_BLK,      .name = "tx_blk",           .desc = "transmission blocked" },
102
  { .mask = CHK_EV_TX_ERR,      .name = "tx_err",           .desc = "transmission error" },
103
104
  {}
105
};
106
107
static const struct name_desc check_trace_lockon_args[4] = {
108
  /* arg1 */ { /* already used by the check */ },
109
  /* arg2 */ { },
110
  /* arg3 */ { },
111
  /* arg4 */ { }
112
};
113
114
static const struct name_desc check_trace_decoding[] = {
115
0
#define CHK_VERB_CLEAN    1
116
  { .name="clean",    .desc="only user-friendly stuff, generally suitable for level \"user\"" },
117
0
#define CHK_VERB_MINIMAL  2
118
  { .name="minimal",  .desc="report info on streams and connectors" },
119
#define CHK_VERB_SIMPLE   3
120
  { .name="simple",   .desc="add info on request and response channels" },
121
0
#define CHK_VERB_ADVANCED 4
122
  { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" },
123
0
#define CHK_VERB_COMPLETE 5
124
  { .name="complete", .desc="add info on channel's buffer" },
125
  { /* end */ }
126
};
127
128
struct trace_source trace_check = {
129
  .name = IST("check"),
130
  .desc = "Health-check",
131
  .arg_def = TRC_ARG1_CHK,  // TRACE()'s first argument is always a stream
132
  .default_cb = check_trace,
133
  .known_events = check_trace_events,
134
  .lockon_args = check_trace_lockon_args,
135
  .decoding = check_trace_decoding,
136
  .report_events = ~0,  // report everything by default
137
};
138
139
#define TRACE_SOURCE &trace_check
140
INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
141
142
143
/* Dummy frontend used to create all checks sessions. */
144
struct proxy checks_fe;
145
146
147
static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
148
0
{
149
0
  size_t block1, block2;
150
0
  int line, ptr, newptr;
151
152
0
  block1 = b_contig_data(buf, ofs);
153
0
  block2 = 0;
154
0
  if (block1 > len)
155
0
    block1 = len;
156
0
  block2 = len - block1;
157
158
0
  ofs = b_peek_ofs(buf, ofs);
159
160
0
  line = 0;
161
0
  ptr = ofs;
162
0
  while (ptr < ofs + block1) {
163
0
    newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
164
0
    if (newptr == ptr)
165
0
      break;
166
0
    ptr = newptr;
167
0
  }
168
169
0
  line = ptr = 0;
170
0
  while (ptr < block2) {
171
0
    newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
172
0
    if (newptr == ptr)
173
0
      break;
174
0
    ptr = newptr;
175
0
  }
176
0
}
177
178
/* trace source and events */
179
static void check_trace(enum trace_level level, uint64_t mask,
180
      const struct trace_source *src,
181
      const struct ist where, const struct ist func,
182
      const void *a1, const void *a2, const void *a3, const void *a4)
183
0
{
184
0
  const struct check *check = a1;
185
0
  const struct server *srv = (check ? check->server : NULL);
186
0
  const size_t        *val = a4;
187
0
  const char *res;
188
189
0
  if (!check || src->verbosity < CHK_VERB_CLEAN)
190
0
    return;
191
192
0
  BUG_ON(!srv);
193
0
  chunk_appendf(&trace_buf, " : [%c] SRV=%s",
194
0
          ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
195
0
          srv->id);
196
197
0
  chunk_appendf(&trace_buf, " status=%d/%d %s exp=%d",
198
0
          (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
199
0
          (check->health >= check->rise) ? check->fall : check->rise,
200
0
          (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN",
201
0
          (check->task->expire ? TICKS_TO_MS(check->task->expire - now_ms) : 0));
202
203
0
  switch (check->result) {
204
0
  case CHK_RES_NEUTRAL: res = "-";     break;
205
0
  case CHK_RES_FAILED:   res = "FAIL"; break;
206
0
  case CHK_RES_PASSED:   res = "PASS"; break;
207
0
  case CHK_RES_CONDPASS: res = "COND"; break;
208
0
  default:               res = "UNK";  break;
209
0
  }
210
211
0
  if (src->verbosity == CHK_VERB_CLEAN)
212
0
    return;
213
214
0
  chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
215
0
          get_check_status_info(check->status), check->status,
216
0
          res, check->result);
217
218
  /* Display the value to the 4th argument (level > STATE) */
219
0
  if (src->level > TRACE_LEVEL_STATE && val)
220
0
    chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
221
222
0
  chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
223
224
0
  if (src->verbosity == CHK_VERB_MINIMAL)
225
0
    return;
226
227
228
0
  if (check->sc) {
229
0
    struct connection *conn = sc_conn(check->sc);
230
231
0
    chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0);
232
0
    chunk_appendf(&trace_buf, " sc=%p(0x%08x)", check->sc, check->sc->flags);
233
0
  }
234
235
0
  if (mask & CHK_EV_TCPCHK) {
236
0
    const char *type;
237
238
0
    type = tcpcheck_ruleset_type_to_str(check->tcpcheck->rs);
239
0
    if (check->current_step)
240
0
      chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
241
0
    else
242
0
      chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
243
0
  }
244
245
  /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
246
0
  if (src->level > TRACE_LEVEL_USER) {
247
0
    const struct buffer *buf = NULL;
248
249
0
    chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
250
0
            (unsigned int)b_data(&check->bi), b_orig(&check->bi),
251
0
            (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
252
0
    chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
253
0
            (unsigned int)b_data(&check->bo), b_orig(&check->bo),
254
0
            (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
255
256
0
    if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
257
0
      buf = (b_is_null(&check->bi) ? NULL : &check->bi);
258
0
    else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
259
0
      buf = (b_is_null(&check->bo) ? NULL : &check->bo);
260
261
0
    if (buf) {
262
0
      if ((check->tcpcheck->rs->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
263
0
        int full = (src->verbosity == CHK_VERB_COMPLETE);
264
265
0
        chunk_memcat(&trace_buf, "\n\t", 2);
266
0
        htx_dump(&trace_buf, htxbuf(buf), full);
267
0
      }
268
0
      else {
269
0
        int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
270
271
0
        chunk_memcat(&trace_buf, "\n", 1);
272
0
        if (b_data(buf) > max) {
273
0
          check_trace_buf(buf, 0, max);
274
0
          chunk_memcat(&trace_buf, "  ...\n", 6);
275
0
        }
276
0
        else
277
0
          check_trace_buf(buf, 0, b_data(buf));
278
0
      }
279
280
0
    }
281
0
  }
282
283
0
}
284
285
286
/**************************************************************************/
287
/************************ Handle check results ****************************/
288
/**************************************************************************/
289
struct check_status {
290
  short result;     /* one of SRV_CHK_* */
291
  char *info;     /* human readable short info */
292
  char *desc;     /* long description */
293
};
294
295
struct analyze_status {
296
  char *desc;       /* description */
297
  unsigned char lr[HANA_OBS_SIZE];  /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
298
};
299
300
static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
301
  [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN,  "UNK",     "Unknown" },
302
  [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN,  "INI",     "Initializing" },
303
  [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
304
305
  /* Below we have finished checks */
306
  [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL,  "CHECKED", "No status change" },
307
  [HCHK_STATUS_HANA]  = { CHK_RES_FAILED,   "HANA",    "Health analyze" },
308
309
  [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED,   "SOCKERR", "Socket error" },
310
311
  [HCHK_STATUS_L4OK]  = { CHK_RES_PASSED,   "L4OK",    "Layer4 check passed" },
312
  [HCHK_STATUS_L4TOUT]  = { CHK_RES_FAILED,   "L4TOUT",  "Layer4 timeout" },
313
  [HCHK_STATUS_L4CON] = { CHK_RES_FAILED,   "L4CON",   "Layer4 connection problem" },
314
315
  [HCHK_STATUS_L6OK]  = { CHK_RES_PASSED,   "L6OK",    "Layer6 check passed" },
316
  [HCHK_STATUS_L6TOUT]  = { CHK_RES_FAILED,   "L6TOUT",  "Layer6 timeout" },
317
  [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED,   "L6RSP",   "Layer6 invalid response" },
318
319
  [HCHK_STATUS_L7TOUT]  = { CHK_RES_FAILED,   "L7TOUT",  "Layer7 timeout" },
320
  [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED,   "L7RSP",   "Layer7 invalid response" },
321
322
  [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
323
324
  [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED,   "L7OK",    "Layer7 check passed" },
325
  [HCHK_STATUS_L7OKCD]  = { CHK_RES_CONDPASS, "L7OKC",   "Layer7 check conditionally passed" },
326
  [HCHK_STATUS_L7STS] = { CHK_RES_FAILED,   "L7STS",   "Layer7 wrong status" },
327
328
  [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED,   "PROCERR",  "External check error" },
329
  [HCHK_STATUS_PROCTOUT]  = { CHK_RES_FAILED,   "PROCTOUT", "External check timeout" },
330
  [HCHK_STATUS_PROCOK]  = { CHK_RES_PASSED,   "PROCOK",   "External check passed" },
331
};
332
333
static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = {   /* 0: ignore, 1: error, 2: OK */
334
  [HANA_STATUS_UNKNOWN]   = { "Unknown",                         { 0, 0 }},
335
336
  [HANA_STATUS_L4_OK]   = { "L4 successful connection",        { 2, 0 }},
337
  [HANA_STATUS_L4_ERR]    = { "L4 unsuccessful connection",      { 1, 1 }},
338
339
  [HANA_STATUS_HTTP_OK]   = { "Correct http response",           { 0, 2 }},
340
  [HANA_STATUS_HTTP_STS]    = { "Wrong http response",             { 0, 1 }},
341
  [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
342
  [HANA_STATUS_HTTP_RSP]    = { "Invalid http response",           { 0, 1 }},
343
344
  [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)",               { 0, 1 }},
345
  [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)",             { 0, 1 }},
346
  [HANA_STATUS_HTTP_BROKEN_PIPE]  = { "Close from server (http)",        { 0, 1 }},
347
};
348
349
/* checks if <err> is a real error for errno or one that can be ignored, and
350
 * return 0 for these ones or <err> for real ones.
351
 */
352
static inline int unclean_errno(int err)
353
0
{
354
0
  if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS ||
355
0
      err == EISCONN || err == EALREADY)
356
0
    return 0;
357
0
  return err;
358
0
}
359
360
/* Converts check_status code to result code */
361
short get_check_status_result(short check_status)
362
0
{
363
0
  if (check_status < HCHK_STATUS_SIZE)
364
0
    return check_statuses[check_status].result;
365
0
  else
366
0
    return check_statuses[HCHK_STATUS_UNKNOWN].result;
367
0
}
368
369
/* Converts check_status code to description */
370
0
const char *get_check_status_description(short check_status) {
371
372
0
  const char *desc;
373
374
0
  if (check_status < HCHK_STATUS_SIZE)
375
0
    desc = check_statuses[check_status].desc;
376
0
  else
377
0
    desc = NULL;
378
379
0
  if (desc && *desc)
380
0
    return desc;
381
0
  else
382
0
    return check_statuses[HCHK_STATUS_UNKNOWN].desc;
383
0
}
384
385
/* Converts check_status code to short info */
386
const char *get_check_status_info(short check_status)
387
0
{
388
0
  const char *info;
389
390
0
  if (check_status < HCHK_STATUS_SIZE)
391
0
    info = check_statuses[check_status].info;
392
0
  else
393
0
    info = NULL;
394
395
0
  if (info && *info)
396
0
    return info;
397
0
  else
398
0
    return check_statuses[HCHK_STATUS_UNKNOWN].info;
399
0
}
400
401
/* Convert analyze_status to description */
402
0
const char *get_analyze_status(short analyze_status) {
403
404
0
  const char *desc;
405
406
0
  if (analyze_status < HANA_STATUS_SIZE)
407
0
    desc = analyze_statuses[analyze_status].desc;
408
0
  else
409
0
    desc = NULL;
410
411
0
  if (desc && *desc)
412
0
    return desc;
413
0
  else
414
0
    return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
415
0
}
416
417
/* append check info to buffer msg */
418
void check_append_info(struct buffer *msg, struct check *check)
419
0
{
420
0
  if (!check)
421
0
    return;
422
0
  chunk_appendf(msg, ", reason: %s", get_check_status_description(check->status));
423
424
0
  if (check->status >= HCHK_STATUS_L57DATA)
425
0
    chunk_appendf(msg, ", code: %d", check->code);
426
427
0
  if (check->desc[0]) {
428
0
    struct buffer src;
429
430
0
    chunk_appendf(msg, ", info: \"");
431
432
0
    chunk_initlen(&src, check->desc, 0, strlen(check->desc));
433
0
    chunk_asciiencode(msg, &src, '"');
434
435
0
    chunk_appendf(msg, "\"");
436
0
  }
437
438
0
  if (check->duration >= 0)
439
0
    chunk_appendf(msg, ", check duration: %ldms", check->duration);
440
0
}
441
442
/* Sets check->status, update check->duration and fill check->result with an
443
 * adequate CHK_RES_* value. The new check->health is computed based on the
444
 * result.
445
 *
446
 * Shows information in logs about failed health check if server is UP or
447
 * succeeded health checks if server is DOWN.
448
 */
449
void set_server_check_status(struct check *check, short status, const char *desc)
450
0
{
451
0
  struct server *s = check->server;
452
0
  short prev_status = check->status;
453
0
  int report = (status != prev_status) ? 1 : 0;
454
455
0
  TRACE_POINT(CHK_EV_HCHK_RUN, check);
456
457
0
  if (status == HCHK_STATUS_START) {
458
0
    check->result = CHK_RES_UNKNOWN;  /* no result yet */
459
0
    check->desc[0] = '\0';
460
0
    check->start = now_ns;
461
0
    return;
462
0
  }
463
464
0
  if (!check->status)
465
0
    return;
466
467
0
  if (desc && *desc) {
468
0
    strncpy(check->desc, desc, HCHK_DESC_LEN-1);
469
0
    check->desc[HCHK_DESC_LEN-1] = '\0';
470
0
  } else
471
0
    check->desc[0] = '\0';
472
473
0
  check->status = status;
474
0
  if (check_statuses[status].result)
475
0
    check->result = check_statuses[status].result;
476
477
0
  if (status == HCHK_STATUS_HANA)
478
0
    check->duration = -1;
479
0
  else if (check->start) {
480
    /* set_server_check_status() may be called more than once */
481
0
    check->duration = ns_to_ms(now_ns - check->start);
482
0
    check->start = 0;
483
0
  }
484
485
  /* no change is expected if no state change occurred */
486
0
  if (check->result == CHK_RES_NEUTRAL)
487
0
    return;
488
489
  /* If the check was really just sending a mail, it won't have an
490
   * associated server, so we're done now.
491
   */
492
0
  if (!s)
493
0
      return;
494
495
0
  switch (check->result) {
496
0
  case CHK_RES_FAILED:
497
    /* Failure to connect to the agent as a secondary check should not
498
     * cause the server to be marked down.
499
     */
500
0
    if ((!(check->state & CHK_ST_AGENT) ||
501
0
        (check->status >= HCHK_STATUS_L57DATA)) &&
502
0
        (check->health > 0)) {
503
0
      if (s->counters.shared.tg)
504
0
        _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->failed_checks);
505
0
      report = 1;
506
0
      check->health--;
507
0
      if (check->health < check->rise)
508
0
        check->health = 0;
509
0
    }
510
0
    break;
511
512
0
  case CHK_RES_PASSED:
513
0
  case CHK_RES_CONDPASS:
514
0
    if (check->health < check->rise + check->fall - 1) {
515
0
      report = 1;
516
0
      check->health++;
517
518
0
      if (check->health >= check->rise)
519
0
        check->health = check->rise + check->fall - 1; /* OK now */
520
0
    }
521
522
    /* clear consecutive_errors if observing is enabled */
523
0
    if (s->onerror)
524
0
      HA_ATOMIC_STORE(&s->consecutive_errors, 0);
525
0
    break;
526
527
0
  default:
528
0
    break;
529
0
  }
530
531
0
  if (report)
532
0
    srv_event_hdl_publish_check(s, check);
533
534
0
  if (s->proxy->options2 & PR_O2_LOGHCHKS && report) {
535
0
    chunk_printf(&trash,
536
0
                 "%s check for %sserver %s/%s %s%s",
537
0
           (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
538
0
                 s->flags & SRV_F_BACKUP ? "backup " : "",
539
0
                 s->proxy->id, s->id,
540
0
                 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
541
0
                 (check->result >= CHK_RES_PASSED)   ? "succeeded" : "failed");
542
543
0
    check_append_info(&trash, check);
544
545
0
    chunk_appendf(&trash, ", status: %d/%d %s",
546
0
                 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
547
0
                 (check->health >= check->rise) ? check->fall : check->rise,
548
0
           (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
549
550
0
    ha_warning("%s.\n", trash.area);
551
0
    send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
552
0
  }
553
0
}
554
555
static inline enum srv_op_st_chg_cause check_notify_cause(struct check *check)
556
0
{
557
0
  struct server *s = check->server;
558
559
  /* We only report a cause for the check if we did not do so previously */
560
0
  if (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS))
561
0
    return (check->state & CHK_ST_AGENT) ? SRV_OP_STCHGC_AGENT : SRV_OP_STCHGC_HEALTH;
562
0
  return SRV_OP_STCHGC_NONE;
563
0
}
564
565
/* Marks the check <check>'s server down if the current check is already failed
566
 * and the server is not down yet nor in maintenance.
567
 */
568
void check_notify_failure(struct check *check)
569
0
{
570
0
  struct server *s = check->server;
571
572
  /* The agent secondary check should only cause a server to be marked
573
   * as down if check->status is HCHK_STATUS_L7STS, which indicates
574
   * that the agent returned "fail", "stopped" or "down".
575
   * The implication here is that failure to connect to the agent
576
   * as a secondary check should not cause the server to be marked
577
   * down. */
578
0
  if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
579
0
    return;
580
581
0
  if (check->health > 0)
582
0
    return;
583
584
0
  TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
585
0
  srv_set_stopped(s, check_notify_cause(check));
586
0
}
587
588
/* Marks the check <check> as valid and tries to set its server up, provided
589
 * it isn't in maintenance, it is not tracking a down server and other checks
590
 * comply. The rule is simple : by default, a server is up, unless any of the
591
 * following conditions is true :
592
 *   - health check failed (check->health < rise)
593
 *   - agent check failed (agent->health < rise)
594
 *   - the server tracks a down server (track && track->state == STOPPED)
595
 * Note that if the server has a slowstart, it will switch to STARTING instead
596
 * of RUNNING. Also, only the health checks support the nolb mode, so the
597
 * agent's success may not take the server out of this mode.
598
 */
599
void check_notify_success(struct check *check)
600
0
{
601
0
  struct server *s = check->server;
602
603
0
  if (s->next_admin & SRV_ADMF_MAINT)
604
0
    return;
605
606
0
  if (s->track && s->track->next_state == SRV_ST_STOPPED)
607
0
    return;
608
609
0
  if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
610
0
    return;
611
612
0
  if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
613
0
    return;
614
615
0
  if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
616
0
    return;
617
618
0
  TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
619
0
  srv_set_running(s, check_notify_cause(check));
620
0
}
621
622
/* Marks the check <check> as valid and tries to set its server into stopping mode
623
 * if it was running or starting, and provided it isn't in maintenance and other
624
 * checks comply. The conditions for the server to be marked in stopping mode are
625
 * the same as for it to be turned up. Also, only the health checks support the
626
 * nolb mode.
627
 */
628
void check_notify_stopping(struct check *check)
629
0
{
630
0
  struct server *s = check->server;
631
632
0
  if (s->next_admin & SRV_ADMF_MAINT)
633
0
    return;
634
635
0
  if (check->state & CHK_ST_AGENT)
636
0
    return;
637
638
0
  if (s->track && s->track->next_state == SRV_ST_STOPPED)
639
0
    return;
640
641
0
  if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
642
0
    return;
643
644
0
  if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
645
0
    return;
646
647
0
  TRACE_STATE("health-check condionnaly succeeded, set server STOPPING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
648
0
  srv_set_stopping(s, check_notify_cause(check));
649
0
}
650
651
/* note: use health_adjust() only, which first checks that the observe mode is
652
 * enabled. This will take the server lock if needed.
653
 */
654
void __health_adjust(struct server *s, short status)
655
0
{
656
0
  int failed;
657
658
0
  if (s->observe >= HANA_OBS_SIZE)
659
0
    return;
660
661
0
  if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
662
0
    return;
663
664
0
  switch (analyze_statuses[status].lr[s->observe - 1]) {
665
0
    case 1:
666
0
      failed = 1;
667
0
      break;
668
669
0
    case 2:
670
0
      failed = 0;
671
0
      break;
672
673
0
    default:
674
0
      return;
675
0
  }
676
677
0
  if (!failed) {
678
    /* good: clear consecutive_errors */
679
0
    HA_ATOMIC_STORE(&s->consecutive_errors, 0);
680
0
    return;
681
0
  }
682
683
0
  if (HA_ATOMIC_ADD_FETCH(&s->consecutive_errors, 1) < s->consecutive_errors_limit)
684
0
    return;
685
686
0
  chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
687
0
               HA_ATOMIC_LOAD(&s->consecutive_errors), get_analyze_status(status));
688
689
0
  HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
690
691
  /* force fastinter for upcoming check
692
   * (does nothing if fastinter is not enabled)
693
   */
694
0
  s->check.state |= CHK_ST_FASTINTER;
695
696
0
  switch (s->onerror) {
697
0
    case HANA_ONERR_FASTINTER:
698
    /* force fastinter - nothing to do here as all modes force it */
699
0
      break;
700
701
0
    case HANA_ONERR_SUDDTH:
702
    /* simulate a pre-fatal failed health check */
703
0
      if (s->check.health > s->check.rise)
704
0
        s->check.health = s->check.rise + 1;
705
706
0
      __fallthrough;
707
708
0
    case HANA_ONERR_FAILCHK:
709
    /* simulate a failed health check */
710
0
      set_server_check_status(&s->check, HCHK_STATUS_HANA,
711
0
            trash.area);
712
0
      check_notify_failure(&s->check);
713
0
      break;
714
715
0
    case HANA_ONERR_MARKDWN:
716
    /* mark server down */
717
0
      s->check.health = s->check.rise;
718
0
      set_server_check_status(&s->check, HCHK_STATUS_HANA,
719
0
            trash.area);
720
0
      check_notify_failure(&s->check);
721
0
      break;
722
723
0
    default:
724
      /* write a warning? */
725
0
      break;
726
0
  }
727
728
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
729
730
0
  HA_ATOMIC_STORE(&s->consecutive_errors, 0);
731
0
  if (s->counters.shared.tg)
732
0
    _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->failed_hana);
733
734
0
  if (s->check.fastinter) {
735
    /* timer might need to be advanced, it might also already be
736
     * running in another thread. Let's just wake the task up, it
737
     * will automatically adjust its timer.
738
     */
739
0
    task_wakeup(s->check.task, TASK_WOKEN_MSG);
740
0
  }
741
0
}
742
743
/* Checks the connection. If an error has already been reported or the socket is
744
 * closed, keep errno intact as it is supposed to contain the valid error code.
745
 * If no error is reported, check the socket's error queue using getsockopt().
746
 * Warning, this must be done only once when returning from poll, and never
747
 * after an I/O error was attempted, otherwise the error queue might contain
748
 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
749
 * socket. Returns non-zero if an error was reported, zero if everything is
750
 * clean (including a properly closed socket).
751
 */
752
static int retrieve_errno_from_socket(struct connection *conn)
753
0
{
754
0
  int skerr;
755
0
  socklen_t lskerr = sizeof(skerr);
756
757
0
  if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
758
0
    return 1;
759
760
0
  if (!conn_ctrl_ready(conn))
761
0
    return 0;
762
763
0
  BUG_ON(conn->flags & CO_FL_FDLESS);
764
765
0
  if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
766
0
    errno = skerr;
767
768
0
  errno = unclean_errno(errno);
769
770
0
  if (!errno) {
771
    /* we could not retrieve an error, that does not mean there is
772
     * none. Just don't change anything and only report the prior
773
     * error if any.
774
     */
775
0
    if (conn->flags & CO_FL_ERROR)
776
0
      return 1;
777
0
    else
778
0
      return 0;
779
0
  }
780
781
0
  conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
782
0
  return 1;
783
0
}
784
785
/* Tries to collect as much information as possible on the connection status,
786
 * and adjust the server status accordingly. It may make use of <errno_bck>
787
 * if non-null when the caller is absolutely certain of its validity (eg:
788
 * checked just after a syscall). If the caller doesn't have a valid errno,
789
 * it can pass zero, and retrieve_errno_from_socket() will be called to try
790
 * to extract errno from the socket. If no error is reported, it will consider
791
 * the <expired> flag. This is intended to be used when a connection error was
792
 * reported in conn->flags or when a timeout was reported in <expired>. The
793
 * function takes care of not updating a server status which was already set.
794
 * All situations where at least one of <expired> or CO_FL_ERROR are set
795
 * produce a status.
796
 */
797
void chk_report_conn_err(struct check *check, int errno_bck, int expired)
798
0
{
799
0
  struct stconn *sc = check->sc;
800
0
  struct connection *conn = sc_conn(sc);
801
0
  const char *err_msg;
802
0
  struct buffer *chk;
803
0
  int step;
804
805
0
  if (check->result != CHK_RES_UNKNOWN) {
806
0
    return;
807
0
  }
808
809
0
  errno = unclean_errno(errno_bck);
810
0
  if (conn && errno)
811
0
    retrieve_errno_from_socket(conn);
812
813
0
  TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
814
815
  /* we'll try to build a meaningful error message depending on the
816
   * context of the error possibly present in conn->err_code, and the
817
   * socket error possibly collected above. This is useful to know the
818
   * exact step of the L6 layer (eg: SSL handshake).
819
   */
820
0
  chk = get_trash_chunk();
821
822
0
  if (check->type == PR_O2_TCPCHK_CHK &&
823
0
      (check->tcpcheck->rs->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
824
0
    step = tcpcheck_get_step_id(check, NULL);
825
0
    if (!step) {
826
0
      TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
827
0
      chunk_printf(chk, " at initial connection step of tcp-check");
828
0
    }
829
0
    else {
830
0
      chunk_printf(chk, " at step %d of tcp-check", step);
831
      /* we were looking for a string */
832
0
      if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
833
0
        if (check->current_step->connect.port)
834
0
          chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
835
0
        else
836
0
          chunk_appendf(chk, " (connect)");
837
0
        TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
838
0
      }
839
0
      else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
840
0
        struct tcpcheck_expect *expect = &check->current_step->expect;
841
842
0
        switch (expect->type) {
843
0
        case TCPCHK_EXPECT_STRING:
844
0
          chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
845
0
          break;
846
0
        case TCPCHK_EXPECT_BINARY:
847
0
          chunk_appendf(chk, " (expect binary '");
848
0
          dump_binary(chk, istptr(expect->data), (int)istlen(expect->data));
849
0
          chunk_appendf(chk, "')");
850
0
          break;
851
0
        case TCPCHK_EXPECT_STRING_REGEX:
852
0
          chunk_appendf(chk, " (expect regex)");
853
0
          break;
854
0
        case TCPCHK_EXPECT_BINARY_REGEX:
855
0
          chunk_appendf(chk, " (expect binary regex)");
856
0
          break;
857
0
        case TCPCHK_EXPECT_STRING_LF:
858
0
          chunk_appendf(chk, " (expect log-format string)");
859
0
          break;
860
0
        case TCPCHK_EXPECT_BINARY_LF:
861
0
          chunk_appendf(chk, " (expect log-format binary)");
862
0
          break;
863
0
        case TCPCHK_EXPECT_HTTP_STATUS:
864
0
          chunk_appendf(chk, " (expect HTTP status codes)");
865
0
          break;
866
0
        case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
867
0
          chunk_appendf(chk, " (expect HTTP status regex)");
868
0
          break;
869
0
        case TCPCHK_EXPECT_HTTP_HEADER:
870
0
          chunk_appendf(chk, " (expect HTTP header pattern)");
871
0
          break;
872
0
        case TCPCHK_EXPECT_HTTP_BODY:
873
0
          chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
874
0
          break;
875
0
        case TCPCHK_EXPECT_HTTP_BODY_REGEX:
876
0
          chunk_appendf(chk, " (expect HTTP body regex)");
877
0
          break;
878
0
        case TCPCHK_EXPECT_HTTP_BODY_LF:
879
0
          chunk_appendf(chk, " (expect log-format HTTP body)");
880
0
          break;
881
0
        case TCPCHK_EXPECT_CUSTOM:
882
0
          chunk_appendf(chk, " (expect custom function)");
883
0
          break;
884
0
        case TCPCHK_EXPECT_UNDEF:
885
0
          chunk_appendf(chk, " (undefined expect!)");
886
0
          break;
887
0
        }
888
0
        TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
889
0
      }
890
0
      else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
891
0
        chunk_appendf(chk, " (send)");
892
0
        TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
893
0
      }
894
895
0
      if (check->current_step && check->current_step->comment)
896
0
        chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
897
0
    }
898
0
  }
899
900
0
  if (conn && conn->err_code) {
901
0
    if (unclean_errno(errno))
902
0
      chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
903
0
             chk->area);
904
0
    else
905
0
      chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
906
0
             chk->area);
907
0
    err_msg = trash.area;
908
0
  }
909
0
  else {
910
0
    if (unclean_errno(errno)) {
911
0
      chunk_printf(&trash, "%s%s", strerror(errno),
912
0
             chk->area);
913
0
      err_msg = trash.area;
914
0
    }
915
0
    else {
916
0
      err_msg = chk->area;
917
0
    }
918
0
  }
919
920
0
  if (check->state & CHK_ST_PORT_MISS) {
921
    /* NOTE: this is reported after <fall> tries */
922
0
    set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
923
0
  }
924
925
0
  if (!conn || !conn->ctrl) {
926
    /* error before any connection attempt (connection allocation error or no control layer) */
927
0
    set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
928
0
  }
929
0
  else if (conn->flags & CO_FL_WAIT_L4_CONN) {
930
    /* L4 not established (yet) */
931
0
    if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))
932
0
      set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
933
0
    else if (expired)
934
0
      set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
935
936
    /*
937
     * might be due to a server IP change.
938
     * Let's trigger a DNS resolution if none are currently running.
939
     */
940
0
    if (check->server)
941
0
      resolv_trigger_resolution(check->server->resolv_requester);
942
943
0
  }
944
0
  else if (conn->flags & CO_FL_WAIT_L6_CONN) {
945
    /* L6 not established (yet) */
946
0
    if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))
947
0
      set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
948
0
    else if (expired)
949
0
      set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
950
0
  }
951
0
  else if (conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR)) {
952
    /* I/O error after connection was established and before we could diagnose */
953
0
    set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
954
0
  }
955
0
  else if (expired) {
956
0
    enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
957
958
    /* connection established but expired check */
959
0
    if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
960
0
        check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
961
0
      tout = check->current_step->expect.tout_status;
962
0
    set_server_check_status(check, tout, err_msg);
963
0
  }
964
965
0
  if (check->result == CHK_RES_UNKNOWN) {
966
    /* No other reason found, report a socket error (may be an internal or a ressournce error) */
967
0
    set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
968
0
  }
969
970
0
  TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
971
0
  return;
972
0
}
973
974
975
/* Builds the server state header used by HTTP health-checks */
976
int httpchk_build_status_header(struct server *s, struct buffer *buf)
977
0
{
978
0
  int sv_state;
979
0
  int ratio;
980
0
  char addr[46];
981
0
  char port[6];
982
0
  const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
983
0
              "UP %d/%d", "UP",
984
0
              "NOLB %d/%d", "NOLB",
985
0
              "no check" };
986
0
  unsigned long last_change = s->last_change;
987
988
0
  if (!(s->check.state & CHK_ST_ENABLED))
989
0
    sv_state = 6;
990
0
  else if (s->cur_state != SRV_ST_STOPPED) {
991
0
    if (s->check.health == s->check.rise + s->check.fall - 1)
992
0
      sv_state = 3; /* UP */
993
0
    else
994
0
      sv_state = 2; /* going down */
995
996
0
    if (s->cur_state == SRV_ST_STOPPING)
997
0
      sv_state += 2;
998
0
  } else {
999
0
    if (s->check.health)
1000
0
      sv_state = 1; /* going up */
1001
0
    else
1002
0
      sv_state = 0; /* DOWN */
1003
0
  }
1004
1005
0
  chunk_appendf(buf, srv_hlt_st[sv_state],
1006
0
          (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
1007
0
          (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
1008
1009
0
  addr_to_str(&s->addr, addr, sizeof(addr));
1010
0
  if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1011
0
    snprintf(port, sizeof(port), "%u", s->svc_port);
1012
0
  else
1013
0
    *port = 0;
1014
1015
0
  chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
1016
0
          addr, port, s->proxy->id, s->id,
1017
0
          global.node,
1018
0
          (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
1019
0
          (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
1020
0
          s->cur_sess, s->proxy->beconn - s->proxy->queueslength,
1021
0
          s->queueslength);
1022
1023
0
  if ((s->cur_state == SRV_ST_STARTING) &&
1024
0
      ns_to_sec(now_ns) < last_change + s->slowstart &&
1025
0
      ns_to_sec(now_ns) >= last_change) {
1026
0
    ratio = MAX(1, 100 * (ns_to_sec(now_ns) - last_change) / s->slowstart);
1027
0
    chunk_appendf(buf, "; throttle=%d%%", ratio);
1028
0
  }
1029
1030
0
  return b_data(buf);
1031
0
}
1032
1033
/**************************************************************************/
1034
/***************** Health-checks based on connections *********************/
1035
/**************************************************************************/
1036
/* This function handles connection status updates including errors. If
1037
 * necessary, it wakes the check task up.
1038
 */
1039
struct task *srv_chk_io_cb(struct task *t, void *ctx, unsigned int state)
1040
0
{
1041
0
  struct stconn *sc = ctx;
1042
0
  struct connection *conn;
1043
0
  struct check *check = __sc_check(sc);
1044
0
  int ret = 0;
1045
1046
0
  BUG_ON(!check->server);
1047
1048
0
  TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
1049
0
  if (check->result != CHK_RES_UNKNOWN)
1050
0
    goto end;
1051
1052
0
  HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1053
1054
  /* we may have to make progress on the TCP checks */
1055
0
  ret = tcpcheck_main(check);
1056
1057
0
  sc = check->sc;
1058
0
  conn = sc_conn(sc);
1059
1060
0
  if (unlikely(!conn || conn->flags & CO_FL_ERROR || sc_ep_test(sc, SE_FL_ERROR))) {
1061
    /* We may get error reports bypassing the I/O handlers, typically
1062
     * the case when sending a pure TCP check which fails, then the I/O
1063
     * handlers above are not called. This is completely handled by the
1064
     * main processing task so let's simply wake it up. If we get here,
1065
     * we expect errno to still be valid.
1066
     */
1067
0
    TRACE_ERROR("report connection error", CHK_EV_HCHK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1068
0
    chk_report_conn_err(check, errno, 0);
1069
0
    task_wakeup(check->task, TASK_WOKEN_IO);
1070
0
  }
1071
1072
0
  if (check->result != CHK_RES_UNKNOWN || ret == -1) {
1073
    /* Check complete or aborted. Wake the check task up to be sure
1074
     * the result is handled ASAP. */
1075
0
    ret = -1;
1076
0
    task_wakeup(check->task, TASK_WOKEN_IO);
1077
0
  }
1078
0
  else {
1079
    /* Check in progress. Queue it to eventually handle timeout
1080
     * update */
1081
0
    task_queue(check->task);
1082
0
  }
1083
1084
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1085
1086
0
  end:
1087
0
  TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
1088
0
  return t;
1089
0
}
1090
1091
/* returns <0, 0, >0 if check thread 1 is respectively less loaded than,
1092
 * equally as, or more loaded than thread 2. This is made to decide on
1093
 * migrations so a margin is applied in either direction. For ease of
1094
 * remembering the direction, consider this returns load1 - load2.
1095
 */
1096
static inline int check_thread_cmp_load(int thr1, int thr2)
1097
0
{
1098
0
  uint t1_load = _HA_ATOMIC_LOAD(&ha_thread_ctx[thr1].rq_total);
1099
0
  uint t1_act  = _HA_ATOMIC_LOAD(&ha_thread_ctx[thr1].active_checks);
1100
0
  uint t2_load = _HA_ATOMIC_LOAD(&ha_thread_ctx[thr2].rq_total);
1101
0
  uint t2_act  = _HA_ATOMIC_LOAD(&ha_thread_ctx[thr2].active_checks);
1102
1103
  /* twice as more active checks is a significant difference */
1104
0
  if (t1_act * 2 < t2_act)
1105
0
    return -1;
1106
1107
0
  if (t2_act * 2 < t1_act)
1108
0
    return 1;
1109
1110
  /* twice as more rqload with more checks is also a significant
1111
   * difference.
1112
   */
1113
0
  if (t1_act <= t2_act && t1_load * 2 < t2_load)
1114
0
    return -1;
1115
1116
0
  if (t2_act <= t1_act && t2_load * 2 < t1_load)
1117
0
    return 1;
1118
1119
  /* otherwise they're roughly equal */
1120
0
  return 0;
1121
0
}
1122
1123
/* returns <0, 0, >0 if check thread 1's active checks count is respectively
1124
 * higher than, equal, or lower than thread 2's. This is made to decide on
1125
 * forced migrations upon overload, so only a very little margin is applied
1126
 * here (~1%). For ease of remembering the direction, consider this returns
1127
 * active1 - active2.
1128
 */
1129
static inline int check_thread_cmp_active(int thr1, int thr2)
1130
0
{
1131
0
  uint t1_act  = _HA_ATOMIC_LOAD(&ha_thread_ctx[thr1].active_checks);
1132
0
  uint t2_act  = _HA_ATOMIC_LOAD(&ha_thread_ctx[thr2].active_checks);
1133
1134
0
  if (t1_act * 128 >= t2_act * 129)
1135
0
    return 1;
1136
0
  if (t2_act * 128 >= t1_act * 129)
1137
0
    return -1;
1138
0
  return 0;
1139
0
}
1140
1141
1142
/* manages a server health-check that uses a connection. Returns
1143
 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1144
 *
1145
 * Please do NOT place any return statement in this function and only leave
1146
 * via the out_unlock label.
1147
 */
1148
struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
1149
0
{
1150
0
  struct check *check = context;
1151
0
  struct proxy *proxy = check->proxy;
1152
0
  struct stconn *sc;
1153
0
  struct connection *conn;
1154
0
  int rv;
1155
0
  int expired = tick_is_expired(t->expire, now_ms);
1156
1157
0
  TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1158
1159
0
  if (check->state & CHK_ST_SLEEPING) {
1160
    /* This check just restarted. It's still time to verify if
1161
     * we're on an overloaded thread or if a more suitable one is
1162
     * available. This helps spread the load over the available
1163
     * threads, without migrating too often. For this we'll check
1164
     * our load, and pick a random thread, check if it has less
1165
     * than half of the current thread's load, and if so we'll
1166
     * bounce the task there. It's possible because it's not yet
1167
     * tied to the current thread. The other thread will not bounce
1168
     * the task again because we're setting CHK_ST_READY indicating
1169
     * a migration.
1170
     */
1171
0
    uint run_checks = _HA_ATOMIC_LOAD(&th_ctx->running_checks);
1172
0
    uint my_load = HA_ATOMIC_LOAD(&th_ctx->rq_total);
1173
0
    uint attempts = MIN(global.nbthread, 3);
1174
1175
0
    if (check->state & CHK_ST_READY) {
1176
      /* check was migrated, active already counted */
1177
0
      activity[tid].check_adopted++;
1178
0
    }
1179
0
    else {
1180
      /* first wakeup, let's check if another thread is less loaded
1181
       * than this one in order to smooth the load. If the current
1182
       * thread is not yet overloaded, we attempt an opportunistic
1183
       * migration to another thread that is not full and that is
1184
       * significantly less loaded. And if the current thread is
1185
       * already overloaded, we attempt a forced migration to a
1186
       * thread with less active checks. We try at most 3 random
1187
       * other thread.
1188
       */
1189
0
      while (attempts-- > 0 &&
1190
0
             (!LIST_ISEMPTY(&th_ctx->queued_checks) || my_load >= 3) &&
1191
0
             _HA_ATOMIC_LOAD(&th_ctx->active_checks) >= 3) {
1192
0
        uint new_tid  = statistical_prng_range(global.nbthread);
1193
1194
0
        if (new_tid == tid)
1195
0
          continue;
1196
1197
0
        ALREADY_CHECKED(new_tid);
1198
1199
0
        if (check_thread_cmp_active(tid, new_tid) > 0 &&
1200
0
            (run_checks >= global.tune.max_checks_per_thread ||
1201
0
             check_thread_cmp_load(tid, new_tid) > 0)) {
1202
          /* Found one. Let's migrate the task over there. We have to
1203
           * remove it from the WQ first and kill its expire time
1204
           * otherwise the scheduler will reinsert it and trigger a
1205
           * BUG_ON() as we're not allowed to call task_queue() for a
1206
           * foreign thread. The recipient will restore the expiration.
1207
           */
1208
0
          check->state |= CHK_ST_READY;
1209
0
          HA_ATOMIC_INC(&ha_thread_ctx[new_tid].active_checks);
1210
0
          task_unlink_wq(t);
1211
0
          t->expire = TICK_ETERNITY;
1212
0
          task_set_thread(t, new_tid);
1213
0
          task_wakeup(t, TASK_WOKEN_MSG);
1214
0
          TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
1215
0
          return t;
1216
0
        }
1217
0
      }
1218
      /* check just woke up, count it as active */
1219
0
      _HA_ATOMIC_INC(&th_ctx->active_checks);
1220
0
    }
1221
1222
    /* OK we're keeping it so this check is ours now */
1223
0
    task_set_thread(t, tid);
1224
0
    check->state &= ~CHK_ST_SLEEPING;
1225
1226
    /* if we just woke up and the thread is full of running, or
1227
     * already has others waiting, we might have to wait in queue
1228
     * (for health checks only). This means !SLEEPING && !READY.
1229
     */
1230
0
    if (check->server &&
1231
0
        (!LIST_ISEMPTY(&th_ctx->queued_checks) ||
1232
0
         (global.tune.max_checks_per_thread &&
1233
0
          _HA_ATOMIC_LOAD(&th_ctx->running_checks) >= global.tune.max_checks_per_thread))) {
1234
0
      TRACE_DEVEL("health-check queued", CHK_EV_TASK_WAKE, check);
1235
0
      t->expire = TICK_ETERNITY;
1236
0
      LIST_APPEND(&th_ctx->queued_checks, &check->check_queue);
1237
1238
      /* reset fastinter flag (if set) so that srv_getinter()
1239
       * only returns fastinter if server health is degraded
1240
       */
1241
0
      check->state &= ~CHK_ST_FASTINTER;
1242
0
      goto out_leave;
1243
0
    }
1244
1245
    /* OK let's run, now we cannot roll back anymore */
1246
0
    check->state |= CHK_ST_READY;
1247
0
    activity[tid].check_started++;
1248
0
    _HA_ATOMIC_INC(&th_ctx->running_checks);
1249
0
  }
1250
1251
  /* at this point, CHK_ST_SLEEPING = 0 and CHK_ST_READY = 1*/
1252
1253
0
  if (check->server)
1254
0
    HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1255
1256
0
  if (!(check->state & (CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC))) {
1257
    /* This task might have bounced from another overloaded thread, it
1258
     * needs an expiration timer that was supposed to be now, but that
1259
     * was erased during the bounce.
1260
     */
1261
0
    if (!tick_isset(t->expire)) {
1262
0
      t->expire = tick_add(now_ms, 0);
1263
0
      expired = 0;
1264
0
    }
1265
0
  }
1266
1267
0
  if (unlikely(check->state & CHK_ST_PURGE)) {
1268
0
    TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1269
0
  }
1270
0
  else if (!(check->state & (CHK_ST_INPROGRESS))) {
1271
    /* no check currently running, but we might have been woken up
1272
     * before the timer's expiration to update it according to a
1273
     * new state (e.g. fastinter), in which case we'll reprogram
1274
     * the new timer.
1275
     */
1276
0
    if (!tick_is_expired(t->expire, now_ms)) { /* woke up too early */
1277
0
      if (check->server) {
1278
0
        int new_exp = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check)));
1279
1280
0
        if (tick_is_expired(new_exp, t->expire)) {
1281
0
          TRACE_STATE("health-check was advanced", CHK_EV_TASK_WAKE, check);
1282
0
          goto update_timer;
1283
0
        }
1284
0
      }
1285
1286
0
      TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
1287
0
      goto out_unlock;
1288
0
    }
1289
1290
    /* we don't send any health-checks when the proxy is
1291
     * stopped, the server should not be checked or the check
1292
     * is disabled.
1293
     */
1294
0
    if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1295
0
        (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
1296
0
      TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
1297
0
      goto reschedule;
1298
0
    }
1299
1300
    /* we'll initiate a new check */
1301
0
    set_server_check_status(check, HCHK_STATUS_START, NULL);
1302
1303
0
    check->state |= CHK_ST_INPROGRESS;
1304
0
    TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
1305
1306
0
    check->current_step = NULL;
1307
1308
0
    check->sc = sc_new_from_check(check);
1309
0
    if (!check->sc) {
1310
0
      set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1311
0
      goto end;
1312
0
    }
1313
0
    tcpcheck_main(check);
1314
0
    expired = 0;
1315
0
  }
1316
1317
  /* there was a test running.
1318
   * First, let's check whether there was an uncaught error,
1319
   * which can happen on connect timeout or error.
1320
   */
1321
0
  if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
1322
0
    sc = check->sc;
1323
0
    conn = sc_conn(sc);
1324
1325
    /* Here the connection must be defined. Otherwise the
1326
     * error would have already been detected
1327
     */
1328
0
    if ((conn && ((conn->flags & CO_FL_ERROR) || sc_ep_test(sc, SE_FL_ERROR))) || expired) {
1329
0
      TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1330
0
      chk_report_conn_err(check, 0, expired);
1331
0
    }
1332
0
    else {
1333
0
      if (check->state & CHK_ST_CLOSE_CONN) {
1334
0
        TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1335
0
        check->state &= ~CHK_ST_CLOSE_CONN;
1336
0
        if (!sc_reset_endp(check->sc)) {
1337
          /* error will be handled by tcpcheck_main().
1338
           * On success, remove all flags except SE_FL_DETACHED
1339
           */
1340
0
          sc_ep_clr(check->sc, ~SE_FL_DETACHED);
1341
0
        }
1342
0
        tcpcheck_main(check);
1343
0
      }
1344
0
      if (check->result == CHK_RES_UNKNOWN) {
1345
0
        TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1346
0
        goto out_unlock; /* timeout not reached, wait again */
1347
0
      }
1348
0
    }
1349
0
  }
1350
1351
  /* check complete or aborted */
1352
0
  TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
1353
1354
  /* check->sc may be NULL when the healthcheck is purged */
1355
0
  check->current_step = NULL;
1356
0
  sc = check->sc;
1357
0
  conn = (sc ? sc_conn(sc) : NULL);
1358
1359
0
  if (conn && conn->xprt) {
1360
    /* The check was aborted and the connection was not yet closed.
1361
     * This can happen upon timeout, or when an external event such
1362
     * as a failed response coupled with "observe layer7" caused the
1363
     * server state to be suddenly changed.
1364
     */
1365
0
    se_shutdown(sc->sedesc, SE_SHR_DRAIN|SE_SHW_SILENT);
1366
0
  }
1367
1368
0
  if (sc) {
1369
0
    sc_destroy(sc);
1370
0
    check->sc = NULL;
1371
0
  }
1372
1373
0
  if (check->sess != NULL) {
1374
0
    vars_prune(&check->vars, check->sess, NULL);
1375
0
    session_free(check->sess);
1376
0
    check->sess = NULL;
1377
0
  }
1378
1379
0
  end:
1380
0
  if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
1381
0
    if (check->result == CHK_RES_FAILED) {
1382
      /* a failure or timeout detected */
1383
0
      TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1384
0
      check_notify_failure(check);
1385
0
    }
1386
0
    else if (check->result == CHK_RES_CONDPASS) {
1387
      /* check is OK but asks for stopping mode */
1388
0
      TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1389
0
      check_notify_stopping(check);
1390
0
    }
1391
0
    else if (check->result == CHK_RES_PASSED) {
1392
      /* a success was detected */
1393
0
      TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1394
0
      check_notify_success(check);
1395
0
    }
1396
0
  }
1397
1398
0
  b_dequeue(&check->buf_wait);
1399
1400
0
  check_release_buf(check, &check->bi);
1401
0
  check_release_buf(check, &check->bo);
1402
0
  _HA_ATOMIC_DEC(&th_ctx->running_checks);
1403
0
  _HA_ATOMIC_DEC(&th_ctx->active_checks);
1404
0
  check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
1405
0
  check->state &= ~CHK_ST_READY;
1406
0
  check->state |= CHK_ST_SLEEPING;
1407
1408
0
 update_timer:
1409
  /* when going to sleep, we need to check if other checks are waiting
1410
   * for a slot. If so we pick them out of the queue and wake them up.
1411
   */
1412
0
  if (check->server && (check->state & CHK_ST_SLEEPING)) {
1413
0
    if (!LIST_ISEMPTY(&th_ctx->queued_checks) &&
1414
0
        _HA_ATOMIC_LOAD(&th_ctx->running_checks) < global.tune.max_checks_per_thread) {
1415
0
      struct check *next_chk = LIST_ELEM(th_ctx->queued_checks.n, struct check *, check_queue);
1416
1417
      /* wake up pending task */
1418
0
      LIST_DEL_INIT(&next_chk->check_queue);
1419
1420
0
      activity[tid].check_started++;
1421
0
      _HA_ATOMIC_INC(&th_ctx->running_checks);
1422
0
      next_chk->state |= CHK_ST_READY;
1423
      /* now running */
1424
0
      task_wakeup(next_chk->task, TASK_WOKEN_RES);
1425
0
    }
1426
0
  }
1427
1428
0
  if (check->server) {
1429
0
    rv = 0;
1430
0
    if (global.spread_checks > 0) {
1431
0
      rv = srv_getinter(check) * global.spread_checks / 100;
1432
0
      rv -= (int) (2 * rv * (statistical_prng() / 4294967295.0));
1433
0
    }
1434
0
    t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1435
    /* reset fastinter flag (if set) so that srv_getinter()
1436
     * only returns fastinter if server health is degraded
1437
     */
1438
0
    check->state &= ~CHK_ST_FASTINTER;
1439
0
  }
1440
1441
0
 reschedule:
1442
0
  if (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1443
0
    t->expire = TICK_ETERNITY;
1444
0
  else {
1445
0
    while (tick_is_expired(t->expire, now_ms))
1446
0
      t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1447
0
  }
1448
1449
0
 out_unlock:
1450
0
  if (check->server)
1451
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1452
1453
0
 out_leave:
1454
0
  TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
1455
1456
  /* Free the check if set to PURGE. After this, the check instance may be
1457
   * freed via the srv_drop invocation, so it must not be accessed after
1458
   * this point.
1459
   */
1460
0
  if (unlikely(check->state & CHK_ST_PURGE)) {
1461
0
    free_check(check);
1462
0
    if (check->server)
1463
0
      srv_drop(check->server);
1464
1465
0
    t = NULL;
1466
0
  }
1467
1468
0
  return t;
1469
0
}
1470
1471
1472
/**************************************************************************/
1473
/************************** Init/deinit checks ****************************/
1474
/**************************************************************************/
1475
/*
1476
 * Tries to grab a buffer and to re-enables processing on check <target>. The
1477
 * check flags are used to figure what buffer was requested. It returns 1 if the
1478
 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1479
 * impossible to wake up and we prefer to be woken up later.
1480
 */
1481
int check_buf_available(void *target)
1482
0
{
1483
0
  struct check *check = target;
1484
1485
0
  BUG_ON(!check->sc);
1486
1487
0
  if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi, DB_CHANNEL)) {
1488
0
    TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
1489
0
    check->state &= ~CHK_ST_IN_ALLOC;
1490
0
    tasklet_wakeup(check->sc->wait_event.tasklet);
1491
0
    return 1;
1492
0
  }
1493
0
  if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo, DB_CHANNEL)) {
1494
0
    TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
1495
0
    check->state &= ~CHK_ST_OUT_ALLOC;
1496
0
    tasklet_wakeup(check->sc->wait_event.tasklet);
1497
0
    return 1;
1498
0
  }
1499
1500
0
  return 0;
1501
0
}
1502
1503
/*
1504
 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
1505
 */
1506
struct buffer *check_get_buf(struct check *check, struct buffer *bptr, unsigned int small_buffer)
1507
0
{
1508
0
  struct buffer *buf = NULL;
1509
1510
0
  if (small_buffer == 0 || (buf = b_alloc_small(bptr)) == NULL) {
1511
0
    if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
1512
0
        unlikely((buf = b_alloc(bptr, DB_CHANNEL)) == NULL)) {
1513
0
      b_queue(DB_CHANNEL, &check->buf_wait, check, check_buf_available);
1514
0
    }
1515
0
  }
1516
0
  return buf;
1517
0
}
1518
1519
/*
1520
 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1521
 * wait queue.
1522
 */
1523
void check_release_buf(struct check *check, struct buffer *bptr)
1524
0
{
1525
0
  if (bptr->size) {
1526
0
    int defbuf = b_is_default(bptr);
1527
1528
0
    b_free(bptr);
1529
0
    if (defbuf)
1530
0
      offer_buffers(check->buf_wait.target, 1);
1531
0
  }
1532
0
}
1533
1534
const char *init_check(struct check *check, int type)
1535
0
{
1536
0
  check->type = type;
1537
1538
0
  check->bi = BUF_NULL;
1539
0
  check->bo = BUF_NULL;
1540
0
  LIST_INIT(&check->buf_wait.list);
1541
0
  LIST_INIT(&check->check_queue);
1542
0
  return NULL;
1543
0
}
1544
1545
/* Liberates the resources allocated for a check.
1546
 *
1547
 * This function must only be run by the thread owning the check.
1548
 */
1549
void free_check(struct check *check)
1550
0
{
1551
  /* For agent-check, free the rules / vars from the server. This is not
1552
   * done for health-check : the proxy is the owner of the rules / vars
1553
   * in this case.
1554
   */
1555
0
  if (check->state & CHK_ST_AGENT || check->tcpcheck->healthcheck) {
1556
0
    free_tcpcheck_vars(&check->tcpcheck->preset_vars);
1557
0
    ha_free(&check->tcpcheck->healthcheck);
1558
0
    ha_free(&check->tcpcheck);
1559
0
  }
1560
1561
0
  ha_free(&check->pool_conn_name);
1562
0
  ha_free(&check->sni);
1563
0
  ha_free(&check->alpn_str);
1564
0
  task_destroy(check->task);
1565
1566
0
  check_release_buf(check, &check->bi);
1567
0
  check_release_buf(check, &check->bo);
1568
0
  if (check->sc) {
1569
0
    sc_destroy(check->sc);
1570
0
    check->sc = NULL;
1571
0
  }
1572
0
}
1573
1574
/* This function must be used in order to free a started check. The check will
1575
 * be scheduled for a next execution in order to properly close and free all
1576
 * check elements.
1577
 *
1578
 * Non thread-safe.
1579
 */
1580
void check_purge(struct check *check)
1581
0
{
1582
0
  check->state |= CHK_ST_PURGE;
1583
0
  task_wakeup(check->task, TASK_WOKEN_OTHER);
1584
0
}
1585
1586
/* manages a server health-check. Returns the time the task accepts to wait, or
1587
 * TIME_ETERNITY for infinity.
1588
 */
1589
struct task *process_chk(struct task *t, void *context, unsigned int state)
1590
0
{
1591
0
  struct check *check = context;
1592
1593
0
  if (check->type == PR_O2_EXT_CHK)
1594
0
    return process_chk_proc(t, context, state);
1595
0
  return process_chk_conn(t, context, state);
1596
1597
0
}
1598
1599
1600
int start_check_task(struct check *check, int mininter,
1601
          int nbcheck, int srvpos)
1602
0
{
1603
0
  struct task *t;
1604
1605
  /* task for the check. Process-based checks exclusively run on thread 1. */
1606
0
  if (check->type == PR_O2_EXT_CHK)
1607
0
    t = task_new_on(0);
1608
0
  else
1609
0
    t = task_new_anywhere();
1610
1611
0
  if (!t)
1612
0
    goto fail_alloc_task;
1613
1614
0
  check->task = t;
1615
0
  t->process = process_chk;
1616
0
  t->context = check;
1617
1618
0
  if (mininter < srv_getinter(check))
1619
0
    mininter = srv_getinter(check);
1620
1621
0
  if (global.spread_checks > 0) {
1622
0
    int rnd;
1623
1624
0
    rnd  = srv_getinter(check) * global.spread_checks / 100;
1625
0
    rnd -= (int) (2 * rnd * (ha_random32() / 4294967295.0));
1626
0
    mininter += rnd;
1627
0
  }
1628
1629
0
  if (global.max_spread_checks && mininter > global.max_spread_checks)
1630
0
    mininter = global.max_spread_checks;
1631
1632
  /* check this every ms */
1633
0
  t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1634
0
  check->start = now_ns;
1635
0
  task_queue(t);
1636
1637
0
  return 1;
1638
1639
0
  fail_alloc_task:
1640
0
  ha_alert("Starting [%s:%s] check: out of memory.\n",
1641
0
     check->server->proxy->id, check->server->id);
1642
0
  return 0;
1643
0
}
1644
1645
/*
1646
 * Start health-check.
1647
 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1648
 */
1649
static int start_checks()
1650
0
{
1651
0
  struct proxy *px;
1652
0
  struct server *s;
1653
0
  char *errmsg = NULL;
1654
0
  int nbcheck=0, mininter=0, srvpos=0;
1655
1656
  /* 0- init the dummy frontend used to create all checks sessions */
1657
0
  if (!setup_new_proxy(&checks_fe, "CHECKS-FE", PR_CAP_FE | PR_CAP_BE | PR_CAP_INT, &errmsg)) {
1658
0
    ha_alert("error during checks frontend creation: %s\n", errmsg);
1659
0
    ha_free(&errmsg);
1660
0
    return ERR_ALERT | ERR_FATAL;
1661
0
  }
1662
0
        checks_fe.mode = PR_MODE_TCP;
1663
0
  checks_fe.maxconn = 0;
1664
0
  checks_fe.conn_retries = CONN_RETRIES;
1665
0
  checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1666
0
  checks_fe.timeout.client = TICK_ETERNITY;
1667
1668
  /* 1- count the checkers to run simultaneously.
1669
   * We also determine the minimum interval among all of those which
1670
   * have an interval larger than SRV_CHK_INTER_THRES. This interval
1671
   * will be used to spread their start-up date. Those which have
1672
   * a shorter interval will start independently and will not dictate
1673
   * too short an interval for all others.
1674
   */
1675
0
  for (px = proxies_list; px; px = px->next) {
1676
0
    for (s = px->srv; s; s = s->next) {
1677
0
      if ((px->options2 & PR_O2_USE_SBUF_CHECK) &&
1678
0
          (s->check.tcpcheck->rs->flags & TCPCHK_RULES_MAY_USE_SBUF))
1679
0
        s->check.state |= CHK_ST_USE_SMALL_BUFF;
1680
1681
0
      if (s->check.state & CHK_ST_CONFIGURED) {
1682
0
        nbcheck++;
1683
0
        if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1684
0
            (!mininter || mininter > srv_getinter(&s->check)))
1685
0
          mininter = srv_getinter(&s->check);
1686
0
      }
1687
1688
0
      if (s->agent.state & CHK_ST_CONFIGURED) {
1689
0
        nbcheck++;
1690
0
        if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1691
0
            (!mininter || mininter > srv_getinter(&s->agent)))
1692
0
          mininter = srv_getinter(&s->agent);
1693
0
      }
1694
0
    }
1695
0
  }
1696
1697
0
  if (!nbcheck)
1698
0
    return ERR_NONE;
1699
1700
0
  srand((unsigned)time(NULL));
1701
1702
  /* 2- start them as far as possible from each other. For this, we will
1703
   * start them after their interval is set to the min interval divided
1704
   * by the number of servers, weighted by the server's position in the
1705
   * list.
1706
   */
1707
0
  for (px = proxies_list; px; px = px->next) {
1708
0
    if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1709
0
      if (init_pid_list()) {
1710
0
        ha_alert("Starting [%s] check: out of memory.\n", px->id);
1711
0
        return ERR_ALERT | ERR_FATAL;
1712
0
      }
1713
0
    }
1714
1715
0
    for (s = px->srv; s; s = s->next) {
1716
      /* A task for the main check */
1717
0
      if (s->check.state & CHK_ST_CONFIGURED) {
1718
0
        if (s->check.type == PR_O2_EXT_CHK) {
1719
0
          if (!prepare_external_check(&s->check))
1720
0
            return ERR_ALERT | ERR_FATAL;
1721
0
        }
1722
0
        if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1723
0
          return ERR_ALERT | ERR_FATAL;
1724
0
        srvpos++;
1725
0
      }
1726
1727
      /* A task for a auxiliary agent check */
1728
0
      if (s->agent.state & CHK_ST_CONFIGURED) {
1729
0
        if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1730
0
          return ERR_ALERT | ERR_FATAL;
1731
0
        }
1732
0
        srvpos++;
1733
0
      }
1734
0
    }
1735
0
  }
1736
0
  return ERR_NONE;
1737
0
}
1738
1739
/* called during deinit */
1740
static void clear_checks()
1741
0
{
1742
0
  if (checks_fe.id)
1743
0
    deinit_proxy(&checks_fe);
1744
0
}
1745
1746
/*
1747
 * Return value:
1748
 *   the port to be used for the health check
1749
 *   0 in case no port could be found for the check
1750
 */
1751
static int srv_check_healthcheck_port(struct check *chk)
1752
0
{
1753
0
  int i = 0;
1754
0
  struct server *srv = NULL;
1755
1756
0
  srv = chk->server;
1757
1758
  /* by default, we use the health check port configured */
1759
0
  if (chk->port > 0)
1760
0
    return chk->port;
1761
1762
  /* try to get the port from check_core.addr if check.port not set */
1763
0
  i = get_host_port(&chk->addr);
1764
0
  if (i > 0)
1765
0
    return i;
1766
1767
  /* try to get the port from server address */
1768
  /* prevent MAPPORTS from working at this point, since checks could
1769
   * not be performed in such case (MAPPORTS impose a relative ports
1770
   * based on live traffic)
1771
   */
1772
0
  if (srv->flags & SRV_F_MAPPORTS)
1773
0
    return 0;
1774
1775
0
  i = srv->svc_port; /* by default */
1776
0
  if (i > 0)
1777
0
    return i;
1778
1779
0
  return 0;
1780
0
}
1781
1782
/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1783
 * if an error occurred.
1784
 */
1785
int init_srv_check(struct server *srv)
1786
0
{
1787
0
  const char *err;
1788
0
  struct tcpcheck_rule *r;
1789
0
  int ret = ERR_NONE;
1790
0
  int check_type;
1791
1792
0
  if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
1793
0
    goto out;
1794
1795
0
  check_type = srv->check.tcpcheck->rs->flags & TCPCHK_RULES_PROTO_CHK;
1796
1797
0
  if (!(srv->flags & SRV_F_DYNAMIC)) {
1798
    /* If neither a port nor an addr was specified and no check
1799
     * transport layer is forced, then the transport layer used by
1800
     * the checks is the same as for the production traffic.
1801
     * Otherwise we use raw_sock by default, unless one is
1802
     * specified.
1803
     */
1804
0
    if (!srv->check.port && !is_addr(&srv->check.addr)) {
1805
      /*
1806
       * If any setting is set for the check, then we can't
1807
       * assume we'll use the same XPRT as the server, the
1808
       * server may be QUIC, but we want a TCP check.
1809
       */
1810
0
      if (!srv->check.use_ssl && srv->use_ssl != -1 &&
1811
0
          !srv->check.via_socks4 && !srv->check.send_proxy &&
1812
0
          (!srv->check.alpn_len || (srv->check.alpn_len == srv->ssl_ctx.alpn_len && !strncmp(srv->check.alpn_str, srv->ssl_ctx.alpn_str, srv->check.alpn_len))) &&
1813
0
          (!srv->check.mux_proto || srv->check.mux_proto != srv->mux_proto))
1814
0
        srv->check.xprt = srv->xprt;
1815
0
      else if (srv->check.use_ssl == 1)
1816
0
        srv->check.xprt = xprt_get(XPRT_SSL);
1817
0
      srv->check.send_proxy |= (srv->pp_opts);
1818
0
    }
1819
0
    else if (srv->check.use_ssl == 1)
1820
0
      srv->check.xprt = xprt_get(XPRT_SSL);
1821
0
  }
1822
0
  else {
1823
    /* For dynamic servers, check-ssl and check-send-proxy must be
1824
     * explicitly defined even if the check port was not
1825
     * overridden.
1826
     */
1827
0
    if (srv->check.use_ssl == 1)
1828
0
      srv->check.xprt = xprt_get(XPRT_SSL);
1829
0
  }
1830
1831
  /* Inherit the mux protocol from the server if not already defined for
1832
   * the check
1833
   */
1834
0
  if (srv->mux_proto && !srv->check.mux_proto &&
1835
0
      ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1836
0
       (srv->mux_proto->mode == PROTO_MODE_SPOP && check_type == TCPCHK_RULES_SPOP_CHK) ||
1837
0
       (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
1838
0
    srv->check.mux_proto = srv->mux_proto;
1839
0
  }
1840
  /* test that check proto is valid if explicitly defined */
1841
0
  else if (srv->check.mux_proto &&
1842
0
           ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1843
0
      (srv->check.mux_proto->mode == PROTO_MODE_SPOP && check_type != TCPCHK_RULES_SPOP_CHK) ||
1844
0
            (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1845
0
    ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1846
0
             proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1847
0
    ret |= ERR_ALERT | ERR_FATAL;
1848
0
    goto out;
1849
0
  }
1850
1851
  /* validate <srv> server health-check settings */
1852
1853
0
  if (srv_is_quic(srv)) {
1854
0
    if (srv->check.mux_proto && srv->check.mux_proto != get_mux_proto(ist("quic"))) {
1855
0
      ha_alert("config: %s '%s': QUIC server '%s' uses an incompatible MUX protocol for checks.\n",
1856
0
               proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1857
0
      ret |= ERR_ALERT | ERR_FATAL;
1858
0
      goto out;
1859
0
    }
1860
1861
0
    if (srv->check.use_ssl < 0) {
1862
0
      ha_alert("config: %s '%s': SSL is mandatory for checks on QUIC server '%s'.\n",
1863
0
               proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1864
0
      ret |= ERR_ALERT | ERR_FATAL;
1865
0
    }
1866
1867
0
    if (srv->check.send_proxy) {
1868
0
      ha_alert("config: %s '%s': cannot use PROXY protocol for checks on QUIC server '%s'.\n",
1869
0
               proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1870
0
      ret |= ERR_ALERT | ERR_FATAL;
1871
0
    }
1872
0
  }
1873
0
  else {
1874
0
    if (srv->check.mux_proto && srv->check.mux_proto == get_mux_proto(ist("quic"))) {
1875
0
      ha_alert("config: %s '%s': QUIC checks on non-QUIC server '%s' is not yet supported.\n",
1876
0
               proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1877
0
      ret |= ERR_ALERT | ERR_FATAL;
1878
0
      goto out;
1879
0
    }
1880
0
  }
1881
1882
  /* We need at least a service port, a check port or the first tcp-check
1883
   * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1884
   */
1885
0
  if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1886
0
      (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1887
0
    goto init;
1888
1889
0
  if (LIST_ISEMPTY(&srv->check.tcpcheck->rs->rules)) {
1890
0
    ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1891
0
       proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1892
0
    ret |= ERR_ALERT | ERR_ABORT;
1893
0
    goto out;
1894
0
  }
1895
1896
  /* search the first action (connect / send / expect) in the list */
1897
0
  r = get_first_tcpcheck_rule(srv->check.tcpcheck->rs);
1898
0
  if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1899
0
    ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1900
0
       "nor tcp_check rule 'connect' with port information.\n",
1901
0
       proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1902
0
    ret |= ERR_ALERT | ERR_ABORT;
1903
0
    goto out;
1904
0
  }
1905
1906
  /* scan the tcp-check ruleset to ensure a port has been configured */
1907
0
  list_for_each_entry(r, &srv->check.tcpcheck->rs->rules, list) {
1908
0
    if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
1909
0
      ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1910
0
         "and a tcp_check rule 'connect' with no port information.\n",
1911
0
         proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1912
0
      ret |= ERR_ALERT | ERR_ABORT;
1913
0
      goto out;
1914
0
    }
1915
0
  }
1916
1917
0
  init:
1918
0
  err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1919
0
  if (err) {
1920
0
    ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1921
0
       proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1922
0
    ret |= ERR_ALERT | ERR_ABORT;
1923
0
    goto out;
1924
0
  }
1925
0
  srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_SLEEPING;
1926
0
  srv_take(srv);
1927
1928
  /* Only increment maxsock for servers from the configuration. Dynamic
1929
   * servers at the moment are not taken into account for the estimation
1930
   * of the resources limits.
1931
   */
1932
0
  if (global.mode & MODE_STARTING)
1933
0
    global.maxsock++;
1934
1935
0
  out:
1936
0
  return ret;
1937
0
}
1938
1939
/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1940
 * if an error occurred.
1941
 */
1942
int init_srv_agent_check(struct server *srv)
1943
0
{
1944
0
  struct tcpcheck_rule *chk;
1945
0
  const char *err;
1946
0
  int ret = ERR_NONE;
1947
1948
0
  if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
1949
0
    goto out;
1950
1951
  /* If there is no connect rule preceding all send / expect rules, an
1952
   * implicit one is inserted before all others.
1953
   */
1954
0
  chk = get_first_tcpcheck_rule(srv->agent.tcpcheck->rs);
1955
0
  if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1956
0
    chk = calloc(1, sizeof(*chk));
1957
0
    if (!chk) {
1958
0
      ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
1959
0
         " to agent-check for server '%s' (out of memory).\n",
1960
0
         proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1961
0
      ret |= ERR_ALERT | ERR_FATAL;
1962
0
      goto out;
1963
0
    }
1964
0
    chk->action = TCPCHK_ACT_CONNECT;
1965
0
    chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
1966
0
    LIST_INSERT(&srv->agent.tcpcheck->rs->rules, &chk->list);
1967
0
  }
1968
1969
  /* <chk> is always defined here and it is a CONNECT action. If there is
1970
   * a preset variable, it means there is an agent string defined and data
1971
   * will be sent after the connect.
1972
   */
1973
0
  if (!LIST_ISEMPTY(&srv->agent.tcpcheck->preset_vars))
1974
0
    chk->connect.options |= TCPCHK_OPT_HAS_DATA;
1975
1976
1977
0
  err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1978
0
  if (err) {
1979
0
    ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1980
0
       proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1981
0
    ret |= ERR_ALERT | ERR_ABORT;
1982
0
    goto out;
1983
0
  }
1984
1985
0
  if (!srv->agent.inter)
1986
0
    srv->agent.inter = srv->check.inter;
1987
1988
0
  srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_SLEEPING | CHK_ST_AGENT;
1989
0
  srv_take(srv);
1990
1991
  /* Only increment maxsock for servers from the configuration. Dynamic
1992
   * servers at the moment are not taken into account for the estimation
1993
   * of the resources limits.
1994
   */
1995
0
  if (global.mode & MODE_STARTING)
1996
0
    global.maxsock++;
1997
1998
0
  out:
1999
0
  return ret;
2000
0
}
2001
2002
static void deinit_srv_check(struct server *srv)
2003
0
{
2004
0
  if (srv->check.state & CHK_ST_CONFIGURED) {
2005
0
    free_check(&srv->check);
2006
    /* it is safe to drop now since the main server reference is still held by the proxy */
2007
0
    srv_drop(srv);
2008
0
  }
2009
0
  srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
2010
0
  srv->do_check = 0;
2011
0
}
2012
2013
2014
static void deinit_srv_agent_check(struct server *srv)
2015
0
{
2016
0
  if (srv->agent.state & CHK_ST_CONFIGURED) {
2017
0
    free_check(&srv->agent);
2018
    /* it is safe to drop now since the main server reference is still held by the proxy */
2019
0
    srv_drop(srv);
2020
0
  }
2021
2022
0
  srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
2023
0
  srv->do_agent = 0;
2024
0
}
2025
2026
REGISTER_POST_SERVER_CHECK(init_srv_check);
2027
REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
2028
REGISTER_POST_CHECK(start_checks);
2029
2030
REGISTER_SERVER_DEINIT(deinit_srv_check);
2031
REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
2032
REGISTER_POST_DEINIT(clear_checks);
2033
2034
/* perform minimal initializations */
2035
static void init_checks()
2036
0
{
2037
0
  int i;
2038
2039
0
  for (i = 0; i < MAX_THREADS; i++)
2040
0
    LIST_INIT(&ha_thread_ctx[i].queued_checks);
2041
0
}
2042
2043
INITCALL0(STG_PREPARE, init_checks);
2044
2045
/**************************************************************************/
2046
/************************** Check sample fetches **************************/
2047
/**************************************************************************/
2048
2049
static struct sample_fetch_kw_list smp_kws = {ILH, {
2050
  { /* END */ },
2051
}};
2052
2053
INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
2054
2055
2056
/**************************************************************************/
2057
/************************ Check's parsing functions ***********************/
2058
/**************************************************************************/
2059
/* Parse the "addr" server keyword */
2060
static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2061
        char **errmsg)
2062
0
{
2063
0
  struct sockaddr_storage *sk;
2064
0
  struct protocol *proto;
2065
0
  int port1, port2, err_code = 0;
2066
2067
2068
0
  if (!*args[*cur_arg+1]) {
2069
0
    memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
2070
0
    goto error;
2071
0
  }
2072
2073
0
  sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, &proto, NULL, errmsg, NULL, NULL, NULL,
2074
0
                    PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
2075
0
  if (!sk) {
2076
0
    memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
2077
0
    goto error;
2078
0
  }
2079
2080
0
  srv->check.addr = *sk;
2081
0
  srv->check.proto = proto;
2082
  /* if agentaddr was never set, we can use addr */
2083
0
  if (!(srv->flags & SRV_F_AGENTADDR))
2084
0
    srv->agent.addr = *sk;
2085
2086
0
  out:
2087
0
  return err_code;
2088
2089
0
 error:
2090
0
  err_code |= ERR_ALERT | ERR_FATAL;
2091
0
  goto out;
2092
0
}
2093
2094
/* Parse the "agent-addr" server keyword */
2095
static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2096
        char **errmsg)
2097
0
{
2098
0
  struct sockaddr_storage sk;
2099
0
  int err_code = 0;
2100
2101
0
  if (!*(args[*cur_arg+1])) {
2102
0
    memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
2103
0
    goto error;
2104
0
  }
2105
0
  memset(&sk, 0, sizeof(sk));
2106
0
  if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
2107
0
    memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
2108
0
    goto error;
2109
0
  }
2110
0
  set_srv_agent_addr(srv, &sk);
2111
  /* Agent currently only uses TCP */
2112
0
  if (sk.ss_family == AF_INET)
2113
0
    srv->agent.proto = &proto_tcpv4;
2114
0
  else
2115
0
    srv->agent.proto = &proto_tcpv6;
2116
0
  out:
2117
0
  return err_code;
2118
2119
0
  error:
2120
0
  err_code |= ERR_ALERT | ERR_FATAL;
2121
0
  goto out;
2122
0
}
2123
2124
/* Parse the "agent-check" server keyword */
2125
static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2126
         char **errmsg)
2127
0
{
2128
0
  struct tcpcheck_ruleset *rs = NULL;
2129
0
  struct tcpcheck *tc = srv->agent.tcpcheck;
2130
0
  struct tcpcheck_rule *chk;
2131
0
  int err_code = 0;
2132
2133
0
  if (srv->do_agent)
2134
0
    goto out;
2135
2136
0
  if (!(curpx->cap & PR_CAP_BE)) {
2137
0
    memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2138
0
        args[*cur_arg], proxy_type_str(curpx), curpx->id);
2139
0
    return ERR_WARN;
2140
0
  }
2141
2142
0
  if (!tc) {
2143
0
    tc = calloc(1, sizeof(*tc));
2144
0
    if (!tc) {
2145
0
      memprintf(errmsg, "out of memory.");
2146
0
      goto error;
2147
0
    }
2148
0
    LIST_INIT(&tc->preset_vars);
2149
0
    srv->agent.tcpcheck = tc;
2150
0
  }
2151
2152
0
  rs = find_tcpcheck_ruleset("*agent-check");
2153
0
  if (rs)
2154
0
    goto ruleset_found;
2155
2156
0
  rs = create_tcpcheck_ruleset("*agent-check");
2157
0
  if (rs == NULL) {
2158
0
    memprintf(errmsg, "out of memory.");
2159
0
    goto error;
2160
0
  }
2161
2162
0
  chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
2163
0
          1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
2164
0
  if (!chk) {
2165
0
    memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
2166
0
    goto error;
2167
0
  }
2168
0
  chk->index = 0;
2169
0
  LIST_APPEND(&rs->rules, &chk->list);
2170
2171
0
  chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
2172
0
                        1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
2173
0
            srv->conf.file, srv->conf.line, errmsg);
2174
0
  if (!chk) {
2175
0
    memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
2176
0
    goto error;
2177
0
  }
2178
0
  chk->expect.custom = tcpcheck_agent_expect_reply;
2179
0
  chk->index = 1;
2180
0
  LIST_APPEND(&rs->rules, &chk->list);
2181
2182
0
  ruleset_found:
2183
0
  tc->rs = rs;
2184
0
  tc->flags &= ~TCPCHK_FL_UNUSED_RS;
2185
0
  rs->flags |= TCPCHK_RULES_AGENT_CHK;
2186
0
  srv->do_agent = 1;
2187
2188
0
  out:
2189
0
  return err_code;
2190
2191
0
  error:
2192
0
  deinit_srv_agent_check(srv);
2193
0
  free_tcpcheck_ruleset(rs);
2194
0
  err_code |= ERR_ALERT | ERR_FATAL;
2195
0
  goto out;
2196
0
}
2197
2198
/* Parse the "agent-inter" server keyword */
2199
static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2200
         char **errmsg)
2201
0
{
2202
0
  const char *err = NULL;
2203
0
  unsigned int delay;
2204
0
  int err_code = 0;
2205
2206
0
  if (!*(args[*cur_arg+1])) {
2207
0
    memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2208
0
    goto error;
2209
0
  }
2210
2211
0
  err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2212
0
  if (err == PARSE_TIME_OVER) {
2213
0
    memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2214
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2215
0
    goto error;
2216
0
  }
2217
0
  else if (err == PARSE_TIME_UNDER) {
2218
0
    memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2219
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2220
0
    goto error;
2221
0
  }
2222
0
  else if (err) {
2223
0
    memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2224
0
        *err, srv->id);
2225
0
    goto error;
2226
0
  }
2227
0
  if (delay <= 0) {
2228
0
    memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2229
0
        delay, args[*cur_arg], srv->id);
2230
0
    goto error;
2231
0
  }
2232
0
  srv->agent.inter = delay;
2233
2234
0
  if (warn_if_lower(args[*cur_arg+1], 100)) {
2235
0
    memprintf(errmsg, "'%s %u' in server '%s' is suspiciously small for a value in milliseconds. Please use an explicit unit ('%ums') if that was the intent",
2236
0
              args[*cur_arg], delay, srv->id, delay);
2237
0
    err_code |= ERR_WARN;
2238
0
  }
2239
2240
0
  out:
2241
0
  return err_code;
2242
2243
0
  error:
2244
0
  err_code |= ERR_ALERT | ERR_FATAL;
2245
0
  goto out;
2246
0
}
2247
2248
/* Parse the "agent-port" server keyword */
2249
static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2250
        char **errmsg)
2251
0
{
2252
0
  int err_code = 0;
2253
2254
0
  if (!*(args[*cur_arg+1])) {
2255
0
    memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2256
0
    goto error;
2257
0
  }
2258
2259
  /* Only increment maxsock for servers from the configuration. Dynamic
2260
   * servers at the moment are not taken into account for the estimation
2261
   * of the resources limits.
2262
   */
2263
0
  if (global.mode & MODE_STARTING)
2264
0
    global.maxsock++;
2265
2266
0
  set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
2267
2268
0
  out:
2269
0
  return err_code;
2270
2271
0
  error:
2272
0
  err_code |= ERR_ALERT | ERR_FATAL;
2273
0
  goto out;
2274
0
}
2275
2276
int set_srv_agent_send(struct server *srv, const char *send)
2277
0
{
2278
0
  struct tcpcheck *tc = srv->agent.tcpcheck;
2279
0
  struct tcpcheck_var *var = NULL;
2280
0
  char *str;
2281
2282
0
  str = strdup(send);
2283
0
  var = create_tcpcheck_var(ist("check.agent_string"));
2284
0
  if (str == NULL || var == NULL)
2285
0
    goto error;
2286
2287
0
  free_tcpcheck_vars(&tc->preset_vars);
2288
2289
0
  var->data.type = SMP_T_STR;
2290
0
  var->data.u.str.area = str;
2291
0
  var->data.u.str.data = strlen(str);
2292
0
  LIST_INIT(&var->list);
2293
0
  LIST_APPEND(&tc->preset_vars, &var->list);
2294
2295
0
  return 1;
2296
2297
0
  error:
2298
0
  free(str);
2299
0
  free(var);
2300
0
  return 0;
2301
0
}
2302
2303
/* Parse the "agent-send" server keyword */
2304
static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2305
        char **errmsg)
2306
0
{
2307
0
  struct tcpcheck *tc = srv->agent.tcpcheck;
2308
0
  int err_code = 0;
2309
2310
0
  if (!*(args[*cur_arg+1])) {
2311
0
    memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2312
0
    goto error;
2313
0
  }
2314
2315
0
  if (!tc) {
2316
0
    tc = calloc(1, sizeof(*tc));
2317
0
    if (!tc) {
2318
0
      memprintf(errmsg, "out of memory.");
2319
0
      goto error;
2320
0
    }
2321
0
    LIST_INIT(&tc->preset_vars);
2322
0
    srv->agent.tcpcheck = tc;
2323
0
  }
2324
2325
0
  if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
2326
0
    memprintf(errmsg, "out of memory.");
2327
0
    goto error;
2328
0
  }
2329
2330
0
  out:
2331
0
  return err_code;
2332
2333
0
  error:
2334
0
  deinit_srv_agent_check(srv);
2335
0
  err_code |= ERR_ALERT | ERR_FATAL;
2336
0
  goto out;
2337
0
}
2338
2339
/* Parse the "no-agent-send" server keyword */
2340
static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2341
            char **errmsg)
2342
0
{
2343
0
  deinit_srv_agent_check(srv);
2344
0
  return 0;
2345
0
}
2346
2347
/* Parse the "check" server keyword */
2348
static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2349
         char **errmsg)
2350
0
{
2351
0
  if (!(curpx->cap & PR_CAP_BE)) {
2352
0
    memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2353
0
        args[*cur_arg], proxy_type_str(curpx), curpx->id);
2354
0
    return ERR_WARN;
2355
0
  }
2356
2357
0
  srv->do_check = 1;
2358
0
  return 0;
2359
0
}
2360
2361
/* Parse the "check-send-proxy" server keyword */
2362
static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2363
              char **errmsg)
2364
0
{
2365
0
  srv->check.send_proxy = 1;
2366
0
  return 0;
2367
0
}
2368
2369
/* Parse the "check-via-socks4" server keyword */
2370
static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2371
              char **errmsg)
2372
0
{
2373
0
  srv->check.via_socks4 = 1;
2374
0
  return 0;
2375
0
}
2376
2377
/* Parse the "no-check" server keyword */
2378
static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2379
            char **errmsg)
2380
0
{
2381
0
  deinit_srv_check(srv);
2382
0
  return 0;
2383
0
}
2384
2385
/* Parse the "no-check-reuse-pool" server keyword */
2386
static int srv_parse_no_check_reuse_pool(char **args, int *cur_arg,
2387
                                         struct proxy *curpx, struct server *srv,
2388
                                         char **errmsg)
2389
0
{
2390
0
  srv->check.reuse_pool = 0;
2391
0
  return 0;
2392
0
}
2393
2394
/* Parse the "no-check-send-proxy" server keyword */
2395
static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2396
           char **errmsg)
2397
0
{
2398
0
  srv->check.send_proxy = 0;
2399
0
  return 0;
2400
0
}
2401
2402
/* parse the "check-pool-conn-name" server keyword */
2403
static int srv_parse_check_pool_conn_name(char **args, int *cur_arg,
2404
                                          struct proxy *px,
2405
                                          struct server *newsrv, char **err)
2406
0
{
2407
0
  int err_code = 0;
2408
2409
0
  if (!*args[*cur_arg + 1]) {
2410
0
    memprintf(err, "'%s' : missing value", args[*cur_arg]);
2411
0
    goto error;
2412
0
  }
2413
2414
0
  ha_free(&newsrv->check.pool_conn_name);
2415
0
  newsrv->check.pool_conn_name = strdup(args[*cur_arg + 1]);
2416
0
  if (!newsrv->check.pool_conn_name) {
2417
0
    memprintf(err, "'%s' : out of memory", args[*cur_arg]);
2418
0
    return ERR_ALERT | ERR_FATAL;
2419
0
  }
2420
2421
0
  out:
2422
0
  return err_code;
2423
2424
0
  error:
2425
0
  err_code |= ERR_ALERT | ERR_FATAL;
2426
0
  goto out;
2427
0
}
2428
2429
2430
/* parse the "check-proto" server keyword */
2431
static int srv_parse_check_proto(char **args, int *cur_arg,
2432
         struct proxy *px, struct server *newsrv, char **err)
2433
0
{
2434
0
  int err_code = 0;
2435
2436
0
  if (!*args[*cur_arg + 1]) {
2437
0
    memprintf(err, "'%s' : missing value", args[*cur_arg]);
2438
0
    goto error;
2439
0
  }
2440
0
  newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
2441
0
  if (!newsrv->check.mux_proto) {
2442
0
    memprintf(err, "'%s' :  unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2443
0
    goto error;
2444
0
  }
2445
2446
0
  out:
2447
0
  return err_code;
2448
2449
0
  error:
2450
0
  err_code |= ERR_ALERT | ERR_FATAL;
2451
0
  goto out;
2452
0
}
2453
2454
/* Parse the "check-reuse-pool" server keyword */
2455
static int srv_parse_check_reuse_pool(char **args, int *cur_arg,
2456
                                      struct proxy *curpx, struct server *srv,
2457
                                      char **errmsg)
2458
0
{
2459
0
  srv->check.reuse_pool = 1;
2460
0
  return 0;
2461
0
}
2462
2463
2464
/* Parse the "rise" server keyword */
2465
static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2466
        char **errmsg)
2467
0
{
2468
0
  int err_code = 0;
2469
2470
0
  if (!*args[*cur_arg + 1]) {
2471
0
    memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2472
0
    goto error;
2473
0
  }
2474
2475
0
  srv->check.rise = atol(args[*cur_arg+1]);
2476
0
  if (srv->check.rise <= 0) {
2477
0
    memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2478
0
    goto error;
2479
0
  }
2480
2481
0
  if (srv->check.health)
2482
0
    srv->check.health = srv->check.rise;
2483
2484
0
  out:
2485
0
  return err_code;
2486
2487
0
  error:
2488
0
  deinit_srv_agent_check(srv);
2489
0
  err_code |= ERR_ALERT | ERR_FATAL;
2490
0
  goto out;
2491
0
}
2492
2493
/* Parse the "fall" server keyword */
2494
static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2495
        char **errmsg)
2496
0
{
2497
0
  int err_code = 0;
2498
2499
0
  if (!*args[*cur_arg + 1]) {
2500
0
    memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2501
0
    goto error;
2502
0
  }
2503
2504
0
  srv->check.fall = atol(args[*cur_arg+1]);
2505
0
  if (srv->check.fall <= 0) {
2506
0
    memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2507
0
    goto error;
2508
0
  }
2509
2510
0
  out:
2511
0
  return err_code;
2512
2513
0
  error:
2514
0
  deinit_srv_agent_check(srv);
2515
0
  err_code |= ERR_ALERT | ERR_FATAL;
2516
0
  goto out;
2517
0
}
2518
2519
/* Parse the "inter" server keyword */
2520
static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2521
         char **errmsg)
2522
0
{
2523
0
  const char *err = NULL;
2524
0
  unsigned int delay;
2525
0
  int err_code = 0;
2526
2527
0
  if (!*(args[*cur_arg+1])) {
2528
0
    memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2529
0
    goto error;
2530
0
  }
2531
2532
0
  err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2533
0
  if (err == PARSE_TIME_OVER) {
2534
0
    memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2535
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2536
0
    goto error;
2537
0
  }
2538
0
  else if (err == PARSE_TIME_UNDER) {
2539
0
    memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2540
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2541
0
    goto error;
2542
0
  }
2543
0
  else if (err) {
2544
0
    memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2545
0
        *err, srv->id);
2546
0
    goto error;
2547
0
  }
2548
0
  if (delay <= 0) {
2549
0
    memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2550
0
        delay, args[*cur_arg], srv->id);
2551
0
    goto error;
2552
0
  }
2553
0
  srv->check.inter = delay;
2554
2555
0
  if (warn_if_lower(args[*cur_arg+1], 100)) {
2556
0
    memprintf(errmsg, "'%s %u' in server '%s' is suspiciously small for a value in milliseconds. Please use an explicit unit ('%ums') if that was the intent",
2557
0
              args[*cur_arg], delay, srv->id, delay);
2558
0
    err_code |= ERR_WARN;
2559
0
  }
2560
2561
0
  out:
2562
0
  return err_code;
2563
2564
0
  error:
2565
0
  err_code |= ERR_ALERT | ERR_FATAL;
2566
0
  goto out;
2567
0
}
2568
2569
2570
/* Parse the "fastinter" server keyword */
2571
static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2572
             char **errmsg)
2573
0
{
2574
0
  const char *err = NULL;
2575
0
  unsigned int delay;
2576
0
  int err_code = 0;
2577
2578
0
  if (!*(args[*cur_arg+1])) {
2579
0
    memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2580
0
    goto error;
2581
0
  }
2582
2583
0
  err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2584
0
  if (err == PARSE_TIME_OVER) {
2585
0
    memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2586
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2587
0
    goto error;
2588
0
  }
2589
0
  else if (err == PARSE_TIME_UNDER) {
2590
0
    memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2591
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2592
0
    goto error;
2593
0
  }
2594
0
  else if (err) {
2595
0
    memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2596
0
        *err, srv->id);
2597
0
    goto error;
2598
0
  }
2599
0
  if (delay <= 0) {
2600
0
    memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2601
0
        delay, args[*cur_arg], srv->id);
2602
0
    goto error;
2603
0
  }
2604
0
  srv->check.fastinter = delay;
2605
2606
0
  if (warn_if_lower(args[*cur_arg+1], 100)) {
2607
0
    memprintf(errmsg, "'%s %u' in server '%s' is suspiciously small for a value in milliseconds. Please use an explicit unit ('%ums') if that was the intent",
2608
0
              args[*cur_arg], delay, srv->id, delay);
2609
0
    err_code |= ERR_WARN;
2610
0
  }
2611
2612
0
  out:
2613
0
  return err_code;
2614
2615
0
  error:
2616
0
  err_code |= ERR_ALERT | ERR_FATAL;
2617
0
  goto out;
2618
0
}
2619
2620
2621
/* Parse the "downinter" server keyword */
2622
static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2623
             char **errmsg)
2624
0
{
2625
0
  const char *err = NULL;
2626
0
  unsigned int delay;
2627
0
  int err_code = 0;
2628
2629
0
  if (!*(args[*cur_arg+1])) {
2630
0
    memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2631
0
    goto error;
2632
0
  }
2633
2634
0
  err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2635
0
  if (err == PARSE_TIME_OVER) {
2636
0
    memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2637
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2638
0
    goto error;
2639
0
  }
2640
0
  else if (err == PARSE_TIME_UNDER) {
2641
0
    memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2642
0
        args[*cur_arg+1], args[*cur_arg], srv->id);
2643
0
    goto error;
2644
0
  }
2645
0
  else if (err) {
2646
0
    memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2647
0
        *err, srv->id);
2648
0
    goto error;
2649
0
  }
2650
0
  if (delay <= 0) {
2651
0
    memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2652
0
        delay, args[*cur_arg], srv->id);
2653
0
    goto error;
2654
0
  }
2655
0
  srv->check.downinter = delay;
2656
2657
0
  if (warn_if_lower(args[*cur_arg+1], 100)) {
2658
0
    memprintf(errmsg, "'%s %u' in server '%s' is suspiciously small for a value in milliseconds. Please use an explicit unit ('%ums') if that was the intent",
2659
0
              args[*cur_arg], delay, srv->id, delay);
2660
0
    err_code |= ERR_WARN;
2661
0
  }
2662
2663
0
  out:
2664
0
  return err_code;
2665
2666
0
  error:
2667
0
  err_code |= ERR_ALERT | ERR_FATAL;
2668
0
  goto out;
2669
0
}
2670
2671
/* Parse the "port" server keyword */
2672
static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2673
        char **errmsg)
2674
0
{
2675
0
  int err_code = 0;
2676
2677
0
  if (!*(args[*cur_arg+1])) {
2678
0
    memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2679
0
    goto error;
2680
0
  }
2681
2682
  /* Only increment maxsock for servers from the configuration. Dynamic
2683
   * servers at the moment are not taken into account for the estimation
2684
   * of the resources limits.
2685
   */
2686
0
  if (global.mode & MODE_STARTING)
2687
0
    global.maxsock++;
2688
2689
0
  srv->check.port = atol(args[*cur_arg+1]);
2690
  /* if agentport was never set, we can use port */
2691
0
  if (!(srv->flags & SRV_F_AGENTPORT))
2692
0
    srv->agent.port = srv->check.port;
2693
2694
0
  out:
2695
0
  return err_code;
2696
2697
0
  error:
2698
0
  err_code |= ERR_ALERT | ERR_FATAL;
2699
0
  goto out;
2700
0
}
2701
2702
/* config parser for global "tune.max-checks-per-thread" */
2703
static int check_parse_global_max_checks(char **args, int section_type, struct proxy *curpx,
2704
                                       const struct proxy *defpx, const char *file, int line,
2705
                                       char **err)
2706
0
{
2707
0
  if (too_many_args(1, args, err, NULL))
2708
0
    return -1;
2709
0
  global.tune.max_checks_per_thread = atoi(args[1]);
2710
0
  return 0;
2711
0
}
2712
2713
/* register "global" section keywords */
2714
static struct cfg_kw_list chk_cfg_kws = {ILH, {
2715
  { CFG_GLOBAL, "tune.max-checks-per-thread", check_parse_global_max_checks },
2716
  { 0, NULL, NULL }
2717
}};
2718
2719
INITCALL1(STG_REGISTER, cfg_register_keywords, &chk_cfg_kws);
2720
2721
/* register "server" line keywords */
2722
static struct srv_kw_list srv_kws = { "CHK", { }, {
2723
  { "addr",                srv_parse_addr,                1,  1,  1 }, /* IP address to send health to or to probe from agent-check */
2724
  { "agent-addr",          srv_parse_agent_addr,          1,  1,  1 }, /* Enable an auxiliary agent check */
2725
  { "agent-check",         srv_parse_agent_check,         0,  1,  1 }, /* Enable agent checks */
2726
  { "agent-inter",         srv_parse_agent_inter,         1,  1,  1 }, /* Set the interval between two agent checks */
2727
  { "agent-port",          srv_parse_agent_port,          1,  1,  1 }, /* Set the TCP port used for agent checks. */
2728
  { "agent-send",          srv_parse_agent_send,          1,  1,  1 }, /* Set string to send to agent. */
2729
  { "check",               srv_parse_check,               0,  1,  1 }, /* Enable health checks */
2730
  { "check-pool-conn-name", srv_parse_check_pool_conn_name, 1, 1, 1 }, /* */
2731
  { "check-proto",         srv_parse_check_proto,         1,  1,  1 }, /* Set the mux protocol for health checks  */
2732
  { "check-reuse-pool",    srv_parse_check_reuse_pool,    0,  1,  1 }, /* Allows to reuse idle connections for checks */
2733
  { "check-send-proxy",    srv_parse_check_send_proxy,    0,  1,  1 }, /* Enable PROXY protocol for health checks */
2734
  { "check-via-socks4",    srv_parse_check_via_socks4,    0,  1,  1 }, /* Enable socks4 proxy for health checks */
2735
  { "no-agent-check",      srv_parse_no_agent_check,      0,  1,  0 }, /* Do not enable any auxiliary agent check */
2736
  { "no-check",            srv_parse_no_check,            0,  1,  0 }, /* Disable health checks */
2737
  { "no-check-reuse-pool", srv_parse_no_check_reuse_pool, 0,  1,  0 }, /* Disable PROXY protocol for health checks */
2738
  { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0,  1,  0 }, /* Disable PROXY protocol for health checks */
2739
  { "rise",                srv_parse_check_rise,          1,  1,  1 }, /* Set rise value for health checks */
2740
  { "fall",                srv_parse_check_fall,          1,  1,  1 }, /* Set fall value for health checks */
2741
  { "inter",               srv_parse_check_inter,         1,  1,  1 }, /* Set inter value for health checks */
2742
  { "fastinter",           srv_parse_check_fastinter,     1,  1,  1 }, /* Set fastinter value for health checks */
2743
  { "downinter",           srv_parse_check_downinter,     1,  1,  1 }, /* Set downinter value for health checks */
2744
  { "port",                srv_parse_check_port,          1,  1,  1 }, /* Set the TCP port used for health checks. */
2745
  { NULL, NULL, 0 },
2746
}};
2747
2748
INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
2749
2750
/*
2751
 * Local variables:
2752
 *  c-indent-level: 8
2753
 *  c-basic-offset: 8
2754
 * End:
2755
 */