Coverage Report

Created: 2025-06-20 06:16

/src/haproxy/src/server.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Server management functions.
3
 *
4
 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5
 * Copyright 2007-2008 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 <sys/types.h>
15
#include <netinet/tcp.h>
16
#include <ctype.h>
17
#include <errno.h>
18
19
#include <import/ebmbtree.h>
20
21
#include <haproxy/api.h>
22
#include <haproxy/applet-t.h>
23
#include <haproxy/backend.h>
24
#include <haproxy/cfgparse.h>
25
#include <haproxy/check.h>
26
#include <haproxy/cli.h>
27
#include <haproxy/connection.h>
28
#include <haproxy/counters.h>
29
#include <haproxy/dict-t.h>
30
#include <haproxy/errors.h>
31
#include <haproxy/global.h>
32
#include <haproxy/guid.h>
33
#include <haproxy/log.h>
34
#include <haproxy/mailers.h>
35
#include <haproxy/namespace.h>
36
#include <haproxy/port_range.h>
37
#include <haproxy/protocol.h>
38
#include <haproxy/proxy.h>
39
#include <haproxy/queue.h>
40
#include <haproxy/quic_tp.h>
41
#include <haproxy/resolvers.h>
42
#include <haproxy/sample.h>
43
#include <haproxy/sc_strm.h>
44
#include <haproxy/server.h>
45
#include <haproxy/stats.h>
46
#include <haproxy/stconn.h>
47
#include <haproxy/stream.h>
48
#include <haproxy/stress.h>
49
#include <haproxy/task.h>
50
#include <haproxy/tcpcheck.h>
51
#include <haproxy/time.h>
52
#include <haproxy/tools.h>
53
#include <haproxy/xxhash.h>
54
#include <haproxy/event_hdl.h>
55
56
57
static void srv_update_status(struct server *s, int type, int cause);
58
static int srv_apply_lastaddr(struct server *srv, int *err_code);
59
static void srv_cleanup_connections(struct server *srv);
60
61
/* extra keywords used as value for other arguments. They are used as
62
 * suggestions for mistyped words.
63
 */
64
static const char *extra_kw_list[] = {
65
  "ipv4", "ipv6", "legacy", "octet-count",
66
  "fail-check", "sudden-death", "mark-down",
67
  NULL /* must be last */
68
};
69
70
/* List head of all known server keywords */
71
struct srv_kw_list srv_keywords = {
72
  .list = LIST_HEAD_INIT(srv_keywords.list)
73
};
74
75
__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
76
struct eb_root idle_conn_srv = EB_ROOT;
77
struct task *idle_conn_task __read_mostly = NULL;
78
struct mt_list servers_list = MT_LIST_HEAD_INIT(servers_list);
79
static struct task *server_atomic_sync_task = NULL;
80
static event_hdl_async_equeue server_atomic_sync_queue;
81
82
/* SERVER DELETE(n)->ADD global tracker:
83
 * This is meant to provide srv->rid (revision id) value.
84
 * Revision id allows to differentiate between a previously existing
85
 * deleted server and a new server reusing deleted server name/id.
86
 *
87
 * start value is 0 (even value)
88
 * LSB is used to specify that one or multiple srv delete in a row
89
 * were performed.
90
 * When adding a new server, increment by 1 if current
91
 * value is odd (odd = LSB set),
92
 * because adding a new server after one or
93
 * multiple deletions means we could potentially be reusing old names:
94
 * Increase the revision id to prevent mixups between old and new names.
95
 *
96
 * srv->rid is calculated from cnt even values only.
97
 * sizeof(srv_id_reuse_cnt) must be twice sizeof(srv->rid)
98
 *
99
 * Wraparound is expected and should not cause issues
100
 * (with current design we allow up to 4 billion unique revisions)
101
 *
102
 * Counter is only used under thread_isolate (cli_add/cli_del),
103
 * no need for atomic ops.
104
 */
105
static uint64_t srv_id_reuse_cnt = 0;
106
107
/* The server names dictionary */
108
struct dict server_key_dict = {
109
  .name = "server keys",
110
  .values = EB_ROOT_UNIQUE,
111
};
112
113
static const char *srv_adm_st_chg_cause_str[] = {
114
  [SRV_ADM_STCHGC_NONE] = "",
115
  [SRV_ADM_STCHGC_DNS_NOENT] = "entry removed from SRV record",
116
  [SRV_ADM_STCHGC_DNS_NOIP] = "No IP for server ",
117
  [SRV_ADM_STCHGC_DNS_NX] = "DNS NX status",
118
  [SRV_ADM_STCHGC_DNS_TIMEOUT] = "DNS timeout status",
119
  [SRV_ADM_STCHGC_DNS_REFUSED] = "DNS refused status",
120
  [SRV_ADM_STCHGC_DNS_UNSPEC] = "unspecified DNS error",
121
  [SRV_ADM_STCHGC_STATS_DISABLE] = "'disable' on stats page",
122
  [SRV_ADM_STCHGC_STATS_STOP] = "'stop' on stats page"
123
};
124
125
const char *srv_adm_st_chg_cause(enum srv_adm_st_chg_cause cause)
126
0
{
127
0
  return srv_adm_st_chg_cause_str[cause];
128
0
}
129
130
static const char *srv_op_st_chg_cause_str[] = {
131
  [SRV_OP_STCHGC_NONE] = "",
132
  [SRV_OP_STCHGC_HEALTH] = "",
133
  [SRV_OP_STCHGC_AGENT] = "",
134
  [SRV_OP_STCHGC_CLI] = "changed from CLI",
135
  [SRV_OP_STCHGC_LUA] = "changed from Lua script",
136
  [SRV_OP_STCHGC_STATS_WEB] = "changed from Web interface",
137
  [SRV_OP_STCHGC_STATEFILE] = "changed from server-state after a reload"
138
};
139
140
const char *srv_op_st_chg_cause(enum srv_op_st_chg_cause cause)
141
0
{
142
0
  return srv_op_st_chg_cause_str[cause];
143
0
}
144
145
int srv_downtime(const struct server *s)
146
0
{
147
0
  unsigned long last_change = COUNTERS_SHARED_LAST(s->proxy->be_counters.shared->tg, last_change);
148
149
0
  if ((s->cur_state != SRV_ST_STOPPED) || last_change >= ns_to_sec(now_ns))   // ignore negative time
150
0
    return s->down_time;
151
152
0
  return ns_to_sec(now_ns) - last_change + s->down_time;
153
0
}
154
155
int srv_getinter(const struct check *check)
156
0
{
157
0
  const struct server *s = check->server;
158
159
0
  if ((check->state & (CHK_ST_CONFIGURED|CHK_ST_FASTINTER)) == CHK_ST_CONFIGURED &&
160
0
      (check->health == check->rise + check->fall - 1))
161
0
    return check->inter;
162
163
0
  if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
164
0
    return (check->downinter)?(check->downinter):(check->inter);
165
166
0
  return (check->fastinter)?(check->fastinter):(check->inter);
167
0
}
168
169
/* Update server's addr:svc_port tuple in INET context
170
 *
171
 * Must be called under thread isolation to ensure consistent readings across
172
 * all threads (addr:svc_port might be read without srv lock being held).
173
 */
174
static void _srv_set_inetaddr_port(struct server *srv,
175
                                   const struct sockaddr_storage *addr,
176
                                   unsigned int svc_port, uint8_t mapped_port)
177
0
{
178
0
  ipcpy(addr, &srv->addr);
179
0
  srv->svc_port = svc_port;
180
0
  if (mapped_port)
181
0
    srv->flags |= SRV_F_MAPPORTS;
182
0
  else
183
0
    srv->flags &= ~SRV_F_MAPPORTS;
184
185
0
  if (srv->proxy->lbprm.update_server_eweight) {
186
    /* some balancers (chash in particular) may use the addr in their routing decisions */
187
0
    srv->proxy->lbprm.update_server_eweight(srv);
188
0
  }
189
190
0
  if (srv->log_target && srv->log_target->type == LOG_TARGET_DGRAM) {
191
    /* server is used as a log target, manually update log target addr for DGRAM */
192
0
    ipcpy(addr, srv->log_target->addr);
193
0
    set_host_port(srv->log_target->addr, svc_port);
194
0
  }
195
0
}
196
197
/* same as _srv_set_inetaddr_port() but only updates the addr part
198
 */
199
static void _srv_set_inetaddr(struct server *srv,
200
                              const struct sockaddr_storage *addr)
201
0
{
202
0
  _srv_set_inetaddr_port(srv, addr, srv->svc_port, !!(srv->flags & SRV_F_MAPPORTS));
203
0
}
204
205
/*
206
 * Function executed by server_atomic_sync_task to perform atomic updates on
207
 * compatible server struct members that are not guarded by any lock since
208
 * they are not supposed to change often and are subject to being used in
209
 * sensitive codepaths
210
 *
211
 * Some updates may require thread isolation: we start without isolation
212
 * but as soon as we encounter an event that requires isolation, we do so.
213
 * Once the event is processed, we keep the isolation until we've processed
214
 * the whole batch of events and leave isolation once we're done, as it would
215
 * be very costly to try to acquire isolation multiple times in a row.
216
 * The task will limit itself to a number of events per run to prevent
217
 * thread contention (see: "tune.events.max-events-at-once").
218
 *
219
 * TODO: if we find out that enforcing isolation is too costly, we may
220
 * consider adding thread_isolate_try_full(timeout) or equivalent to the
221
 * thread API so that we can do our best not to block harmless threads
222
 * for too long if one or multiple threads are still heavily busy. This
223
 * would mean that the task would be capable of rescheduling itself to
224
 * start again on the current event if it failed to acquire thread
225
 * isolation. This would also imply that the event_hdl API allows us
226
 * to check an event without popping it from the queue first (remove the
227
 * event once it is successfully processed).
228
 */
229
static void srv_set_addr_desc(struct server *s, int reattach);
230
static struct task *server_atomic_sync(struct task *task, void *context, unsigned int state)
231
0
{
232
0
  unsigned int remain = event_hdl_tune.max_events_at_once; // to limit max number of events per batch
233
0
  struct event_hdl_async_event *event;
234
235
0
  BUG_ON(remain == 0); // event_hdl_tune.max_events_at_once is expected to be > 0
236
237
  /* check for new server events that we care about */
238
0
  do {
239
0
    event = event_hdl_async_equeue_pop(&server_atomic_sync_queue);
240
0
    if (!event)
241
0
      break; /* no events in queue */
242
243
0
    if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) {
244
      /* ending event: no more events to come */
245
0
      event_hdl_async_free_event(event);
246
0
      task_destroy(task);
247
0
      task = NULL;
248
0
      break;
249
0
    }
250
251
    /* new event to process */
252
0
    if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_SERVER_INETADDR)) {
253
0
      struct sockaddr_storage new_addr;
254
0
      struct event_hdl_cb_data_server_inetaddr *data = event->data;
255
0
      struct proxy *px;
256
0
      struct server *srv;
257
258
      /* server ip:port changed, we must atomically update data members
259
       * to prevent invalid reads by other threads.
260
       */
261
262
      /*
263
       * this requires thread isolation, which is safe since we're the only
264
       * task working for the current subscription and we don't hold locks
265
       * or resources that other threads may depend on to complete a running
266
       * cycle. Note that we do this way because we assume that this event is
267
       * rather rare.
268
       */
269
0
      if (!thread_isolated())
270
0
        thread_isolate_full();
271
272
      /* check if related server still exists */
273
0
      px = proxy_find_by_id(data->server.safe.proxy_uuid, PR_CAP_BE, 0);
274
0
      if (!px)
275
0
        continue;
276
0
      srv = server_find_by_id_unique(px, data->server.safe.puid, data->server.safe.rid);
277
0
      if (!srv)
278
0
        continue;
279
280
      /* prepare new addr based on event cb data */
281
0
      memset(&new_addr, 0, sizeof(new_addr));
282
0
      new_addr.ss_family = data->safe.next.family;
283
0
      switch (new_addr.ss_family) {
284
0
        case AF_INET:
285
0
          ((struct sockaddr_in *)&new_addr)->sin_addr.s_addr =
286
0
            data->safe.next.addr.v4.s_addr;
287
0
          break;
288
0
        case AF_INET6:
289
0
          memcpy(&((struct sockaddr_in6 *)&new_addr)->sin6_addr,
290
0
                 &data->safe.next.addr.v6,
291
0
                 sizeof(struct in6_addr));
292
0
          break;
293
0
        case AF_UNSPEC:
294
          /* addr reset, nothing to do */
295
0
          break;
296
0
        default:
297
          /* should not happen */
298
0
          break;
299
0
      }
300
301
      /* apply new addr:port combination */
302
0
      _srv_set_inetaddr_port(srv, &new_addr,
303
0
                             data->safe.next.port.svc, data->safe.next.port.map);
304
305
      /* propagate the changes, force connection cleanup */
306
0
      if (new_addr.ss_family != AF_UNSPEC &&
307
0
          (srv->next_admin & SRV_ADMF_RMAINT)) {
308
        /* server was previously put under DNS maintenance due
309
         * to DNS error, but addr resolves again, so we must
310
         * put it out of maintenance
311
         */
312
0
        srv_clr_admin_flag(srv, SRV_ADMF_RMAINT);
313
314
        /* thanks to valid DNS resolution? */
315
0
        if (data->safe.updater.dns) {
316
0
          chunk_reset(&trash);
317
0
          chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", srv->proxy->id, srv->id);
318
0
          ha_warning("%s.\n", trash.area);
319
0
          send_log(srv->proxy, LOG_NOTICE, "%s.\n", trash.area);
320
0
        }
321
0
      }
322
0
      srv_cleanup_connections(srv);
323
0
      srv_set_dyncookie(srv);
324
0
      srv_set_addr_desc(srv, 1);
325
0
    }
326
0
    event_hdl_async_free_event(event);
327
0
  } while (--remain);
328
329
  /* some events possibly required thread_isolation:
330
   * now that we are done, we must leave thread isolation before
331
   * returning
332
   */
333
0
  if (thread_isolated())
334
0
    thread_release();
335
336
0
  if (!remain) {
337
    /* we stopped because we've already spent all our budget here,
338
     * and considering we possibly were under isolation, we cannot
339
           * keep blocking other threads any longer.
340
     *
341
     * Reschedule the task to finish where we left off if
342
     * there are remaining events in the queue.
343
     */
344
0
    BUG_ON(task == NULL); // ending event doesn't decrement remain
345
0
    if (!event_hdl_async_equeue_isempty(&server_atomic_sync_queue))
346
0
      task_wakeup(task, TASK_WOKEN_OTHER);
347
0
  }
348
349
0
  return task;
350
0
}
351
352
/* Try to start the atomic server sync task.
353
 *
354
 * Returns ERR_NONE on success and a combination of ERR_CODE on failure
355
 */
356
static int server_atomic_sync_start()
357
0
{
358
0
  struct event_hdl_sub_type subscriptions = EVENT_HDL_SUB_NONE;
359
360
0
  if (server_atomic_sync_task)
361
0
    return ERR_NONE; // nothing to do
362
0
  server_atomic_sync_task = task_new_anywhere();
363
0
  if (!server_atomic_sync_task)
364
0
    goto fail;
365
0
  server_atomic_sync_task->process = server_atomic_sync;
366
0
  event_hdl_async_equeue_init(&server_atomic_sync_queue);
367
368
  /* task created, now subscribe to relevant server events in the global list */
369
0
  subscriptions = event_hdl_sub_type_add(subscriptions, EVENT_HDL_SUB_SERVER_INETADDR);
370
0
  if (!event_hdl_subscribe(NULL, subscriptions,
371
0
                           EVENT_HDL_ASYNC_TASK(&server_atomic_sync_queue,
372
0
                                                server_atomic_sync_task,
373
0
                                                NULL,
374
0
                                                NULL)))
375
0
    goto fail;
376
377
378
0
  return ERR_NONE;
379
380
0
 fail:
381
0
  task_destroy(server_atomic_sync_task);
382
0
  server_atomic_sync_task = NULL;
383
0
  return ERR_ALERT | ERR_FATAL;
384
0
}
385
REGISTER_POST_CHECK(server_atomic_sync_start);
386
387
/* fill common server event data members struct
388
 * must be called with server lock or under thread isolate
389
 */
390
static inline void _srv_event_hdl_prepare(struct event_hdl_cb_data_server *cb_data,
391
                                          struct server *srv, uint8_t thread_isolate)
392
0
{
393
  /* safe data assignments */
394
0
  cb_data->safe.puid = srv->puid;
395
0
  cb_data->safe.rid = srv->rid;
396
0
  cb_data->safe.flags = srv->flags;
397
0
  snprintf(cb_data->safe.name, sizeof(cb_data->safe.name), "%s", srv->id);
398
0
  cb_data->safe.proxy_name[0] = '\0';
399
0
  cb_data->safe.proxy_uuid = -1; /* default value */
400
0
  if (srv->proxy) {
401
0
    cb_data->safe.proxy_uuid = srv->proxy->uuid;
402
0
    snprintf(cb_data->safe.proxy_name, sizeof(cb_data->safe.proxy_name), "%s", srv->proxy->id);
403
0
  }
404
  /* unsafe data assignments */
405
0
  cb_data->unsafe.ptr = srv;
406
0
  cb_data->unsafe.thread_isolate = thread_isolate;
407
0
  cb_data->unsafe.srv_lock = !thread_isolate;
408
0
}
409
410
/* take an event-check snapshot from a live check */
411
void _srv_event_hdl_prepare_checkres(struct event_hdl_cb_data_server_checkres *checkres,
412
                                     struct check *check)
413
0
{
414
0
  checkres->agent = !!(check->state & CHK_ST_AGENT);
415
0
  checkres->result = check->result;
416
0
  checkres->duration = check->duration;
417
0
  checkres->reason.status = check->status;
418
0
  checkres->reason.code = check->code;
419
0
  checkres->health.cur = check->health;
420
0
  checkres->health.rise = check->rise;
421
0
  checkres->health.fall = check->fall;
422
0
}
423
424
/* Prepare SERVER_STATE event
425
 *
426
 * This special event will contain extra hints related to the state change
427
 *
428
 * Must be called with server lock held
429
 */
430
void _srv_event_hdl_prepare_state(struct event_hdl_cb_data_server_state *cb_data,
431
                                  struct server *srv, int type, int cause,
432
                                  enum srv_state prev_state, int requeued)
433
0
{
434
  /* state event provides additional info about the server state change */
435
0
  cb_data->safe.type = type;
436
0
  cb_data->safe.new_state = srv->cur_state;
437
0
  cb_data->safe.old_state = prev_state;
438
0
  cb_data->safe.requeued = requeued;
439
0
  if (type) {
440
    /* administrative */
441
0
    cb_data->safe.adm_st_chg.cause = cause;
442
0
  }
443
0
  else {
444
    /* operational */
445
0
    cb_data->safe.op_st_chg.cause = cause;
446
0
    if (cause == SRV_OP_STCHGC_HEALTH || cause == SRV_OP_STCHGC_AGENT) {
447
0
      struct check *check = (cause == SRV_OP_STCHGC_HEALTH) ? &srv->check : &srv->agent;
448
449
      /* provide additional check-related state change result */
450
0
      _srv_event_hdl_prepare_checkres(&cb_data->safe.op_st_chg.check, check);
451
0
    }
452
0
  }
453
0
}
454
455
/* Prepare SERVER_INETADDR event, prev data is learned from the current
456
 * server settings.
457
 *
458
 * This special event will contain extra hints related to the addr change
459
 *
460
 * Must be called with the server lock held.
461
 */
462
static void _srv_event_hdl_prepare_inetaddr(struct event_hdl_cb_data_server_inetaddr *cb_data,
463
                                            struct server *srv,
464
                                            const struct server_inetaddr *next_inetaddr,
465
                                            struct server_inetaddr_updater updater)
466
0
{
467
0
  struct server_inetaddr prev_inetaddr;
468
469
0
  server_get_inetaddr(srv, &prev_inetaddr);
470
471
  /* only INET families are supported */
472
0
  BUG_ON((next_inetaddr->family != AF_UNSPEC &&
473
0
          next_inetaddr->family != AF_INET && next_inetaddr->family != AF_INET6));
474
475
  /* prev */
476
0
  cb_data->safe.prev = prev_inetaddr;
477
478
  /* next */
479
0
  cb_data->safe.next = *next_inetaddr;
480
481
  /* updater */
482
0
  cb_data->safe.updater = updater;
483
0
}
484
485
/* server event publishing helper: publish in both global and
486
 * server dedicated subscription list.
487
 */
488
#define _srv_event_hdl_publish(e, d, s)                                 \
489
0
        ({                                                              \
490
0
                /* publish in server dedicated sub list */              \
491
0
                event_hdl_publish(&s->e_subs, e, EVENT_HDL_CB_DATA(&d));\
492
0
                /* publish in global subscription list */               \
493
0
                event_hdl_publish(NULL, e, EVENT_HDL_CB_DATA(&d));      \
494
0
        })
495
496
/* General server event publishing:
497
 * Use this to publish EVENT_HDL_SUB_SERVER family type event
498
 * from srv facility.
499
 *
500
 * server ptr must be valid.
501
 * Must be called with srv lock or under thread_isolate.
502
 */
503
static void srv_event_hdl_publish(struct event_hdl_sub_type event,
504
                                  struct server *srv, uint8_t thread_isolate)
505
0
{
506
0
  struct event_hdl_cb_data_server cb_data;
507
508
  /* prepare event data */
509
0
  _srv_event_hdl_prepare(&cb_data, srv, thread_isolate);
510
0
  _srv_event_hdl_publish(event, cb_data, srv);
511
0
}
512
513
/* Publish SERVER_CHECK event
514
 *
515
 * This special event will contain extra hints related to the check itself
516
 *
517
 * Must be called with server lock held
518
 */
519
void srv_event_hdl_publish_check(struct server *srv, struct check *check)
520
0
{
521
0
  struct event_hdl_cb_data_server_check cb_data;
522
523
  /* check event provides additional info about the server check */
524
0
  _srv_event_hdl_prepare_checkres(&cb_data.safe.res, check);
525
526
0
  cb_data.unsafe.ptr = check;
527
528
  /* prepare event data (common server data) */
529
0
  _srv_event_hdl_prepare((struct event_hdl_cb_data_server *)&cb_data, srv, 0);
530
531
0
  _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_CHECK, cb_data, srv);
532
0
}
533
534
/*
535
 * Check that we did not get a hash collision.
536
 * Unlikely, but it can happen. The server's proxy must be at least
537
 * read-locked.
538
 */
539
static inline void srv_check_for_dup_dyncookie(struct server *s)
540
0
{
541
0
  struct proxy *p = s->proxy;
542
0
  struct server *tmpserv;
543
544
0
  for (tmpserv = p->srv; tmpserv != NULL;
545
0
      tmpserv = tmpserv->next) {
546
0
    if (tmpserv == s)
547
0
      continue;
548
0
    if (tmpserv->next_admin & SRV_ADMF_FMAINT)
549
0
      continue;
550
0
    if (tmpserv->cookie &&
551
0
        strcmp(tmpserv->cookie, s->cookie) == 0) {
552
0
      ha_warning("We generated two equal cookies for two different servers.\n"
553
0
           "Please change the secret key for '%s'.\n",
554
0
           s->proxy->id);
555
0
    }
556
0
  }
557
558
0
}
559
560
/*
561
 * Must be called with the server lock held, and will read-lock the proxy.
562
 */
563
void srv_set_dyncookie(struct server *s)
564
0
{
565
0
  struct proxy *p = s->proxy;
566
0
  char *tmpbuf;
567
0
  unsigned long long hash_value;
568
0
  size_t key_len;
569
0
  size_t buffer_len;
570
0
  int addr_len;
571
0
  int port;
572
573
0
  HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
574
575
0
  if ((s->flags & SRV_F_COOKIESET) ||
576
0
      !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
577
0
      s->proxy->dyncookie_key == NULL)
578
0
    goto out;
579
0
  key_len = strlen(p->dyncookie_key);
580
581
0
  if (s->addr.ss_family != AF_INET &&
582
0
      s->addr.ss_family != AF_INET6)
583
0
    goto out;
584
  /*
585
   * Buffer to calculate the cookie value.
586
   * The buffer contains the secret key + the server IP address
587
   * + the TCP port.
588
   */
589
0
  addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
590
  /*
591
   * The TCP port should use only 2 bytes, but is stored in
592
   * an unsigned int in struct server, so let's use 4, to be
593
   * on the safe side.
594
   */
595
0
  buffer_len = key_len + addr_len + 4;
596
0
  tmpbuf = trash.area;
597
0
  memcpy(tmpbuf, p->dyncookie_key, key_len);
598
0
  memcpy(&(tmpbuf[key_len]),
599
0
      s->addr.ss_family == AF_INET ?
600
0
      (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
601
0
      (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
602
0
      addr_len);
603
  /*
604
   * Make sure it's the same across all the load balancers,
605
   * no matter their endianness.
606
   */
607
0
  port = htonl(s->svc_port);
608
0
  memcpy(&tmpbuf[key_len + addr_len], &port, 4);
609
0
  hash_value = XXH64(tmpbuf, buffer_len, 0);
610
0
  memprintf(&s->cookie, "%016llx", hash_value);
611
0
  if (!s->cookie)
612
0
    goto out;
613
0
  s->cklen = 16;
614
615
  /* Don't bother checking if the dyncookie is duplicated if
616
   * the server is marked as "disabled", maybe it doesn't have
617
   * its real IP yet, but just a place holder.
618
   */
619
0
  if (!(s->next_admin & SRV_ADMF_FMAINT))
620
0
    srv_check_for_dup_dyncookie(s);
621
0
 out:
622
0
  HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
623
0
}
624
625
/* Returns true if it's possible to reuse an idle connection from server <srv>
626
 * for a websocket stream. This is the case if server is configured to use the
627
 * same protocol for both HTTP and websocket streams. This depends on the value
628
 * of "proto", "alpn" and "ws" keywords.
629
 */
630
int srv_check_reuse_ws(struct server *srv)
631
0
{
632
0
  if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
633
    /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
634
     * for mux selection.
635
     */
636
0
    const struct ist srv_mux = srv->mux_proto ?
637
0
                               srv->mux_proto->token : IST_NULL;
638
639
0
    switch (srv->ws) {
640
    /* "auto" means use the same protocol : reuse is possible. */
641
0
    case SRV_WS_AUTO:
642
0
      return 1;
643
644
    /* "h2" means use h2 for websocket : reuse is possible if
645
     * server mux is h2.
646
     */
647
0
    case SRV_WS_H2:
648
0
      if (srv->mux_proto && isteq(srv_mux, ist("h2")))
649
0
        return 1;
650
0
      break;
651
652
    /* "h1" means use h1 for websocket : reuse is possible if
653
     * server mux is h1.
654
     */
655
0
    case SRV_WS_H1:
656
0
      if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
657
0
        return 1;
658
0
      break;
659
0
    }
660
0
  }
661
0
  else {
662
    /* ALPN selection.
663
     * Based on the assumption that only "h2" and "http/1.1" token
664
     * are used on server ALPN.
665
     */
666
0
    const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
667
0
                                 srv->ssl_ctx.alpn_len);
668
669
0
    switch (srv->ws) {
670
0
    case SRV_WS_AUTO:
671
      /* for auto mode, consider reuse as possible if the
672
       * server uses a single protocol ALPN
673
       */
674
0
      if (!istchr(alpn, ','))
675
0
        return 1;
676
0
      break;
677
678
0
    case SRV_WS_H2:
679
0
      return isteq(alpn, ist("\x02h2"));
680
681
0
    case SRV_WS_H1:
682
0
      return isteq(alpn, ist("\x08http/1.1"));
683
0
    }
684
0
  }
685
686
0
  return 0;
687
0
}
688
689
/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
690
 * is a valid value indicating to use the fallback mux.
691
 */
692
const struct mux_ops *srv_get_ws_proto(struct server *srv)
693
0
{
694
0
  const struct mux_proto_list *mux = NULL;
695
696
0
  switch (srv->ws) {
697
0
  case SRV_WS_AUTO:
698
0
    mux = srv->mux_proto;
699
0
    break;
700
701
0
  case SRV_WS_H1:
702
0
    mux = get_mux_proto(ist("h1"));
703
0
    break;
704
705
0
  case SRV_WS_H2:
706
0
    mux = get_mux_proto(ist("h2"));
707
0
    break;
708
0
  }
709
710
0
  return mux ? mux->mux : NULL;
711
0
}
712
713
/*
714
 * Must be called with the server lock held. The server is first removed from
715
 * the proxy tree if it was already attached. If <reattach> is true, the server
716
 * will then be attached in the proxy tree. The proxy lock is held to
717
 * manipulate the tree.
718
 */
719
static void srv_set_addr_desc(struct server *s, int reattach)
720
0
{
721
0
  struct proxy *p = s->proxy;
722
0
  char *key;
723
724
  /* Risk of used_server_addr tree corruption if server is already deleted. */
725
0
  BUG_ON(s->flags & SRV_F_DELETED);
726
727
0
  key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
728
729
0
  if (s->addr_node.key) {
730
0
    if (key && strcmp(key, s->addr_node.key) == 0) {
731
0
      free(key);
732
0
      return;
733
0
    }
734
735
0
    HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
736
0
    ebpt_delete(&s->addr_node);
737
0
    HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
738
739
0
    free(s->addr_node.key);
740
0
  }
741
742
0
  s->addr_node.key = key;
743
744
0
  if (reattach) {
745
0
    if (s->addr_node.key) {
746
0
      HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
747
0
      ebis_insert(&p->used_server_addr, &s->addr_node);
748
0
      HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
749
0
    }
750
0
  }
751
0
}
752
753
/*
754
 * Registers the server keyword list <kwl> as a list of valid keywords for next
755
 * parsing sessions.
756
 */
757
void srv_register_keywords(struct srv_kw_list *kwl)
758
0
{
759
0
  LIST_APPEND(&srv_keywords.list, &kwl->list);
760
0
}
761
762
/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
763
 * keyword is found with a NULL ->parse() function, then an attempt is made to
764
 * find one with a valid ->parse() function. This way it is possible to declare
765
 * platform-dependant, known keywords as NULL, then only declare them as valid
766
 * if some options are met. Note that if the requested keyword contains an
767
 * opening parenthesis, everything from this point is ignored.
768
 */
769
struct srv_kw *srv_find_kw(const char *kw)
770
0
{
771
0
  int index;
772
0
  const char *kwend;
773
0
  struct srv_kw_list *kwl;
774
0
  struct srv_kw *ret = NULL;
775
776
0
  kwend = strchr(kw, '(');
777
0
  if (!kwend)
778
0
    kwend = kw + strlen(kw);
779
780
0
  list_for_each_entry(kwl, &srv_keywords.list, list) {
781
0
    for (index = 0; kwl->kw[index].kw != NULL; index++) {
782
0
      if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
783
0
          kwl->kw[index].kw[kwend-kw] == 0) {
784
0
        if (kwl->kw[index].parse)
785
0
          return &kwl->kw[index]; /* found it !*/
786
0
        else
787
0
          ret = &kwl->kw[index];  /* may be OK */
788
0
      }
789
0
    }
790
0
  }
791
0
  return ret;
792
0
}
793
794
/* Dumps all registered "server" keywords to the <out> string pointer. The
795
 * unsupported keywords are only dumped if their supported form was not
796
 * found.
797
 */
798
void srv_dump_kws(char **out)
799
0
{
800
0
  struct srv_kw_list *kwl;
801
0
  int index;
802
803
0
  if (!out)
804
0
    return;
805
806
0
  *out = NULL;
807
0
  list_for_each_entry(kwl, &srv_keywords.list, list) {
808
0
    for (index = 0; kwl->kw[index].kw != NULL; index++) {
809
0
      if (kwl->kw[index].parse ||
810
0
          srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
811
0
        memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
812
0
                  kwl->scope,
813
0
                  kwl->kw[index].kw,
814
0
                  kwl->kw[index].skip ? " <arg>" : "",
815
0
                  kwl->kw[index].default_ok ? " [dflt_ok]" : "",
816
0
                  kwl->kw[index].parse ? "" : " (not supported)");
817
0
      }
818
0
    }
819
0
  }
820
0
}
821
822
/* Try to find in srv_keyword the word that looks closest to <word> by counting
823
 * transitions between letters, digits and other characters. Will return the
824
 * best matching word if found, otherwise NULL. An optional array of extra
825
 * words to compare may be passed in <extra>, but it must then be terminated
826
 * by a NULL entry. If unused it may be NULL.
827
 */
828
static const char *srv_find_best_kw(const char *word)
829
0
{
830
0
  uint8_t word_sig[1024];
831
0
  uint8_t list_sig[1024];
832
0
  const struct srv_kw_list *kwl;
833
0
  const char *best_ptr = NULL;
834
0
  int dist, best_dist = INT_MAX;
835
0
  const char **extra;
836
0
  int index;
837
838
0
  make_word_fingerprint(word_sig, word);
839
0
  list_for_each_entry(kwl, &srv_keywords.list, list) {
840
0
    for (index = 0; kwl->kw[index].kw != NULL; index++) {
841
0
      make_word_fingerprint(list_sig, kwl->kw[index].kw);
842
0
      dist = word_fingerprint_distance(word_sig, list_sig);
843
0
      if (dist < best_dist) {
844
0
        best_dist = dist;
845
0
        best_ptr = kwl->kw[index].kw;
846
0
      }
847
0
    }
848
0
  }
849
850
0
  for (extra = extra_kw_list; *extra; extra++) {
851
0
    make_word_fingerprint(list_sig, *extra);
852
0
    dist = word_fingerprint_distance(word_sig, list_sig);
853
0
    if (dist < best_dist) {
854
0
      best_dist = dist;
855
0
      best_ptr = *extra;
856
0
    }
857
0
  }
858
859
0
  if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
860
0
    best_ptr = NULL;
861
862
0
  return best_ptr;
863
0
}
864
865
/* Parse the "backup" server keyword */
866
static int srv_parse_backup(char **args, int *cur_arg,
867
                            struct proxy *curproxy, struct server *newsrv, char **err)
868
0
{
869
0
  newsrv->flags |= SRV_F_BACKUP;
870
0
  return 0;
871
0
}
872
873
874
/* Parse the "cookie" server keyword */
875
static int srv_parse_cookie(char **args, int *cur_arg,
876
                            struct proxy *curproxy, struct server *newsrv, char **err)
877
0
{
878
0
  char *arg;
879
880
0
  arg = args[*cur_arg + 1];
881
0
  if (!*arg) {
882
0
    memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
883
0
    return ERR_ALERT | ERR_FATAL;
884
0
  }
885
886
0
  free(newsrv->cookie);
887
0
  newsrv->cookie = strdup(arg);
888
0
  newsrv->cklen = strlen(arg);
889
0
  newsrv->flags |= SRV_F_COOKIESET;
890
0
  return 0;
891
0
}
892
893
/* Parse the "disabled" server keyword */
894
static int srv_parse_disabled(char **args, int *cur_arg,
895
                              struct proxy *curproxy, struct server *newsrv, char **err)
896
0
{
897
0
  newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
898
0
  newsrv->next_state = SRV_ST_STOPPED;
899
0
  newsrv->check.state |= CHK_ST_PAUSED;
900
0
  newsrv->check.health = 0;
901
0
  return 0;
902
0
}
903
904
/* Parse the "enabled" server keyword */
905
static int srv_parse_enabled(char **args, int *cur_arg,
906
                             struct proxy *curproxy, struct server *newsrv, char **err)
907
0
{
908
0
  newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
909
0
  newsrv->next_state = SRV_ST_RUNNING;
910
0
  newsrv->check.state &= ~CHK_ST_PAUSED;
911
0
  newsrv->check.health = newsrv->check.rise;
912
0
  return 0;
913
0
}
914
915
/* Parse the "error-limit" server keyword */
916
static int srv_parse_error_limit(char **args, int *cur_arg,
917
                                 struct proxy *curproxy, struct server *newsrv, char **err)
918
0
{
919
0
  if (!*args[*cur_arg + 1]) {
920
0
    memprintf(err, "'%s' expects an integer argument.",
921
0
              args[*cur_arg]);
922
0
    return ERR_ALERT | ERR_FATAL;
923
0
  }
924
925
0
  newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
926
927
0
  if (newsrv->consecutive_errors_limit <= 0) {
928
0
    memprintf(err, "%s has to be > 0.",
929
0
              args[*cur_arg]);
930
0
    return ERR_ALERT | ERR_FATAL;
931
0
  }
932
933
0
  return 0;
934
0
}
935
936
/* Parse the "guid" keyword */
937
static int srv_parse_guid(char **args, int *cur_arg,
938
                        struct proxy *curproxy, struct server *newsrv, char **err)
939
0
{
940
0
  const char *guid;
941
0
  char *guid_err = NULL;
942
943
0
  if (!*args[*cur_arg + 1]) {
944
0
    memprintf(err, "'%s' : expects an argument", args[*cur_arg]);
945
0
    return ERR_ALERT | ERR_FATAL;
946
0
  }
947
948
0
  guid = args[*cur_arg + 1];
949
0
  if (guid_insert(&newsrv->obj_type, guid, &guid_err)) {
950
0
    memprintf(err, "'%s': %s", args[*cur_arg], guid_err);
951
0
    ha_free(&guid_err);
952
0
    return ERR_ALERT | ERR_FATAL;
953
0
  }
954
955
0
  return 0;
956
0
}
957
958
/* Parse the "ws" keyword */
959
static int srv_parse_ws(char **args, int *cur_arg,
960
                        struct proxy *curproxy, struct server *newsrv, char **err)
961
0
{
962
0
  if (!args[*cur_arg + 1]) {
963
0
    memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
964
0
    return ERR_ALERT | ERR_FATAL;
965
0
  }
966
967
0
  if (strcmp(args[*cur_arg + 1], "h1") == 0) {
968
0
    newsrv->ws = SRV_WS_H1;
969
0
  }
970
0
  else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
971
0
    newsrv->ws = SRV_WS_H2;
972
0
  }
973
0
  else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
974
0
    newsrv->ws = SRV_WS_AUTO;
975
0
  }
976
0
  else {
977
0
    memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
978
0
    return ERR_ALERT | ERR_FATAL;
979
0
  }
980
981
982
0
  return 0;
983
0
}
984
985
/* Parse the "hash-key" server keyword */
986
static int srv_parse_hash_key(char **args, int *cur_arg,
987
            struct proxy *curproxy, struct server *newsrv, char **err)
988
0
{
989
0
  if (!args[*cur_arg + 1]) {
990
0
    memprintf(err, "'%s expects 'id', 'addr', or 'addr-port' value", args[*cur_arg]);
991
0
    return ERR_ALERT | ERR_FATAL;
992
0
  }
993
994
0
  if (strcmp(args[*cur_arg + 1], "id") == 0) {
995
0
    newsrv->hash_key = SRV_HASH_KEY_ID;
996
0
  }
997
0
  else if (strcmp(args[*cur_arg + 1], "addr") == 0) {
998
0
    newsrv->hash_key = SRV_HASH_KEY_ADDR;
999
0
  }
1000
0
  else if (strcmp(args[*cur_arg + 1], "addr-port") == 0) {
1001
0
    newsrv->hash_key = SRV_HASH_KEY_ADDR_PORT;
1002
0
  }
1003
0
  else {
1004
0
    memprintf(err, "'%s' has to be 'id', 'addr', or 'addr-port'", args[*cur_arg]);
1005
0
    return ERR_ALERT | ERR_FATAL;
1006
0
  }
1007
1008
0
  return 0;
1009
0
}
1010
1011
/* Parse the "idle-ping" server keyword */
1012
static int srv_parse_idle_ping(char **args, int *cur_arg,
1013
                               struct proxy *curproxy, struct server *newsrv, char **err)
1014
0
{
1015
0
  const char *res;
1016
0
  unsigned int value;
1017
1018
0
  if (!*(args[*cur_arg+1])) {
1019
0
    memprintf(err, "'%s' expects an argument.", args[*cur_arg]);
1020
0
    goto error;
1021
0
  }
1022
1023
0
  res = parse_time_err(args[*cur_arg+1], &value, TIME_UNIT_MS);
1024
0
  if (res == PARSE_TIME_OVER) {
1025
0
    memprintf(err, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1026
0
              args[*cur_arg+1], args[*cur_arg], newsrv->id);
1027
0
    goto error;
1028
0
  }
1029
0
  else if (res == PARSE_TIME_UNDER) {
1030
0
    memprintf(err, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1031
0
              args[*cur_arg+1], args[*cur_arg], newsrv->id);
1032
0
    goto error;
1033
0
  }
1034
0
  else if (res) {
1035
0
    memprintf(err, "unexpected character '%c' in '%s' argument of server %s.",
1036
0
              *res, args[*cur_arg], newsrv->id);
1037
0
    goto error;
1038
0
  }
1039
1040
0
  newsrv->idle_ping = value;
1041
1042
0
  return 0;
1043
1044
0
  error:
1045
0
  return ERR_ALERT | ERR_FATAL;
1046
0
}
1047
1048
/* Parse the "init-addr" server keyword */
1049
static int srv_parse_init_addr(char **args, int *cur_arg,
1050
                               struct proxy *curproxy, struct server *newsrv, char **err)
1051
0
{
1052
0
  char *p, *end;
1053
0
  int done;
1054
0
  struct sockaddr_storage sa;
1055
1056
0
  newsrv->init_addr_methods = 0;
1057
0
  memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1058
1059
0
  for (p = args[*cur_arg + 1]; *p; p = end) {
1060
    /* cut on next comma */
1061
0
    for (end = p; *end && *end != ','; end++);
1062
0
    if (*end)
1063
0
      *(end++) = 0;
1064
1065
0
    memset(&sa, 0, sizeof(sa));
1066
0
    if (strcmp(p, "libc") == 0) {
1067
0
      done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1068
0
    }
1069
0
    else if (strcmp(p, "last") == 0) {
1070
0
      done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1071
0
    }
1072
0
    else if (strcmp(p, "none") == 0) {
1073
0
      done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1074
0
    }
1075
0
    else if (str2ip2(p, &sa, 0)) {
1076
0
      if (is_addr(&newsrv->init_addr)) {
1077
0
        memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
1078
0
                  args[*cur_arg], p);
1079
0
        return ERR_ALERT | ERR_FATAL;
1080
0
      }
1081
0
      newsrv->init_addr = sa;
1082
0
      done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1083
0
    }
1084
0
    else {
1085
0
      memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
1086
0
                args[*cur_arg], p);
1087
0
      return ERR_ALERT | ERR_FATAL;
1088
0
    }
1089
0
    if (!done) {
1090
0
      memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
1091
0
                args[*cur_arg], p);
1092
0
      return ERR_ALERT | ERR_FATAL;
1093
0
    }
1094
0
  }
1095
1096
0
  return 0;
1097
0
}
1098
1099
/* Parse the "init-state" server keyword */
1100
static int srv_parse_init_state(char **args, int *cur_arg,
1101
                 struct proxy *curproxy, struct server *newsrv, char **err)
1102
0
{
1103
0
  if (strcmp(args[*cur_arg + 1], "fully-up") == 0)
1104
0
    newsrv->init_state= SRV_INIT_STATE_FULLY_UP;
1105
0
  else if (strcmp(args[*cur_arg + 1], "up") == 0)
1106
0
    newsrv->init_state = SRV_INIT_STATE_UP;
1107
0
  else if (strcmp(args[*cur_arg + 1], "down") == 0)
1108
0
    newsrv->init_state= SRV_INIT_STATE_DOWN;
1109
0
  else if (strcmp(args[*cur_arg + 1], "fully-down") == 0)
1110
0
    newsrv->init_state= SRV_INIT_STATE_FULLY_DOWN;
1111
0
  else {
1112
0
    memprintf(err, "'%s' expects one of 'fully-up', 'up', 'down', or 'fully-down' but got '%s'",
1113
0
          args[*cur_arg], args[*cur_arg + 1]);
1114
0
    return ERR_ALERT | ERR_FATAL;
1115
0
  }
1116
1117
0
  return 0;
1118
0
}
1119
1120
/* Parse the "log-bufsize" server keyword */
1121
static int srv_parse_log_bufsize(char **args, int *cur_arg,
1122
                                 struct proxy *curproxy, struct server *newsrv, char **err)
1123
0
{
1124
0
  if (!*args[*cur_arg + 1]) {
1125
0
    memprintf(err, "'%s' expects an integer argument.",
1126
0
              args[*cur_arg]);
1127
0
    return ERR_ALERT | ERR_FATAL;
1128
0
  }
1129
1130
0
  newsrv->log_bufsize = atoi(args[*cur_arg + 1]);
1131
1132
0
  if (newsrv->log_bufsize <= 0) {
1133
0
    memprintf(err, "%s has to be > 0.",
1134
0
              args[*cur_arg]);
1135
0
    return ERR_ALERT | ERR_FATAL;
1136
0
  }
1137
1138
0
  return 0;
1139
0
}
1140
1141
/* Parse the "log-proto" server keyword */
1142
static int srv_parse_log_proto(char **args, int *cur_arg,
1143
                               struct proxy *curproxy, struct server *newsrv, char **err)
1144
0
{
1145
0
  if (strcmp(args[*cur_arg + 1], "legacy") == 0)
1146
0
    newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
1147
0
  else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
1148
0
    newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
1149
0
  else {
1150
0
    memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
1151
0
              args[*cur_arg], args[*cur_arg + 1]);
1152
0
    return ERR_ALERT | ERR_FATAL;
1153
0
  }
1154
1155
0
  return 0;
1156
0
}
1157
1158
/* Parse the "maxconn" server keyword */
1159
static int srv_parse_maxconn(char **args, int *cur_arg,
1160
                             struct proxy *curproxy, struct server *newsrv, char **err)
1161
0
{
1162
0
  newsrv->maxconn = atol(args[*cur_arg + 1]);
1163
0
  return 0;
1164
0
}
1165
1166
/* Parse the "maxqueue" server keyword */
1167
static int srv_parse_maxqueue(char **args, int *cur_arg,
1168
                              struct proxy *curproxy, struct server *newsrv, char **err)
1169
0
{
1170
0
  newsrv->maxqueue = atol(args[*cur_arg + 1]);
1171
0
  return 0;
1172
0
}
1173
1174
/* Parse the "minconn" server keyword */
1175
static int srv_parse_minconn(char **args, int *cur_arg,
1176
                             struct proxy *curproxy, struct server *newsrv, char **err)
1177
0
{
1178
0
  newsrv->minconn = atol(args[*cur_arg + 1]);
1179
0
  return 0;
1180
0
}
1181
1182
static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
1183
0
{
1184
0
  char *arg;
1185
1186
0
  arg = args[*cur_arg + 1];
1187
0
  if (!*arg) {
1188
0
    memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
1189
0
    return ERR_ALERT | ERR_FATAL;
1190
0
  }
1191
0
  newsrv->max_reuse = atoi(arg);
1192
1193
0
  return 0;
1194
0
}
1195
1196
static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
1197
0
{
1198
0
  const char *res;
1199
0
  char *arg;
1200
0
  unsigned int time;
1201
1202
0
  arg = args[*cur_arg + 1];
1203
0
  if (!*arg) {
1204
0
    memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
1205
0
    return ERR_ALERT | ERR_FATAL;
1206
0
  }
1207
0
  res = parse_time_err(arg, &time, TIME_UNIT_MS);
1208
0
  if (res == PARSE_TIME_OVER) {
1209
0
    memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1210
0
        args[*cur_arg+1], args[*cur_arg]);
1211
0
    return ERR_ALERT | ERR_FATAL;
1212
0
  }
1213
0
  else if (res == PARSE_TIME_UNDER) {
1214
0
    memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1215
0
        args[*cur_arg+1], args[*cur_arg]);
1216
0
    return ERR_ALERT | ERR_FATAL;
1217
0
  }
1218
0
  else if (res) {
1219
0
    memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
1220
0
        *res, args[*cur_arg]);
1221
0
    return ERR_ALERT | ERR_FATAL;
1222
0
  }
1223
0
  newsrv->pool_purge_delay = time;
1224
1225
0
  return 0;
1226
0
}
1227
1228
static int srv_parse_pool_conn_name(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
1229
0
{
1230
0
  char *arg;
1231
1232
0
  arg = args[*cur_arg + 1];
1233
0
  if (!*arg) {
1234
0
    memprintf(err, "'%s' expects <value> as argument", args[*cur_arg]);
1235
0
    return ERR_ALERT | ERR_FATAL;
1236
0
  }
1237
1238
0
  ha_free(&newsrv->pool_conn_name);
1239
0
  newsrv->pool_conn_name = strdup(arg);
1240
0
  if (!newsrv->pool_conn_name) {
1241
0
    memprintf(err, "'%s' : out of memory", args[*cur_arg]);
1242
0
    return ERR_ALERT | ERR_FATAL;
1243
0
  }
1244
1245
0
  return 0;
1246
0
}
1247
1248
static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
1249
0
{
1250
0
  char *arg;
1251
1252
0
  arg = args[*cur_arg + 1];
1253
0
  if (!*arg) {
1254
0
    memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
1255
0
    return ERR_ALERT | ERR_FATAL;
1256
0
  }
1257
1258
0
  newsrv->low_idle_conns = atoi(arg);
1259
0
  return 0;
1260
0
}
1261
1262
static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
1263
0
{
1264
0
  char *arg;
1265
1266
0
  arg = args[*cur_arg + 1];
1267
0
  if (!*arg) {
1268
0
    memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
1269
0
    return ERR_ALERT | ERR_FATAL;
1270
0
  }
1271
1272
0
  newsrv->max_idle_conns = atoi(arg);
1273
0
  if ((int)newsrv->max_idle_conns < -1) {
1274
0
    memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
1275
0
    return ERR_ALERT | ERR_FATAL;
1276
0
  }
1277
1278
0
  return 0;
1279
0
}
1280
1281
/* parse the "id" server keyword */
1282
static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
1283
0
{
1284
0
  struct eb32_node *node;
1285
1286
0
  if (!*args[*cur_arg + 1]) {
1287
0
    memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
1288
0
    return ERR_ALERT | ERR_FATAL;
1289
0
  }
1290
1291
0
  newsrv->puid = atol(args[*cur_arg + 1]);
1292
0
  newsrv->conf.id.key = newsrv->puid;
1293
1294
0
  if (newsrv->puid <= 0) {
1295
0
    memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
1296
0
    return ERR_ALERT | ERR_FATAL;
1297
0
  }
1298
1299
0
  node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
1300
0
  if (node) {
1301
0
    struct server *target = container_of(node, struct server, conf.id);
1302
0
    memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
1303
0
              args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
1304
0
              target->id);
1305
0
    return ERR_ALERT | ERR_FATAL;
1306
0
  }
1307
1308
0
  newsrv->flags |= SRV_F_FORCED_ID;
1309
0
  return 0;
1310
0
}
1311
1312
/* Parse the "namespace" server keyword */
1313
static int srv_parse_namespace(char **args, int *cur_arg,
1314
                               struct proxy *curproxy, struct server *newsrv, char **err)
1315
0
{
1316
#ifdef USE_NS
1317
  char *arg;
1318
1319
  arg = args[*cur_arg + 1];
1320
  if (!*arg) {
1321
    memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
1322
    return ERR_ALERT | ERR_FATAL;
1323
  }
1324
1325
  if (strcmp(arg, "*") == 0) {
1326
    /* Use the namespace associated with the connection (if present). */
1327
    newsrv->flags |= SRV_F_USE_NS_FROM_PP;
1328
    global.last_checks |= LSTCHK_SYSADM;
1329
    return 0;
1330
  }
1331
1332
  /*
1333
   * As this parser may be called several times for the same 'default-server'
1334
   * object, or for a new 'server' instance deriving from a 'default-server'
1335
   * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
1336
   */
1337
  newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
1338
1339
  newsrv->netns = netns_store_lookup(arg, strlen(arg));
1340
  if (!newsrv->netns)
1341
    newsrv->netns = netns_store_insert(arg);
1342
1343
  if (!newsrv->netns) {
1344
    memprintf(err, "Cannot open namespace '%s'", arg);
1345
    return ERR_ALERT | ERR_FATAL;
1346
  }
1347
  global.last_checks |= LSTCHK_SYSADM;
1348
1349
  return 0;
1350
#else
1351
0
  memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
1352
0
  return ERR_ALERT | ERR_FATAL;
1353
0
#endif
1354
0
}
1355
1356
/* Parse the "no-backup" server keyword */
1357
static int srv_parse_no_backup(char **args, int *cur_arg,
1358
                               struct proxy *curproxy, struct server *newsrv, char **err)
1359
0
{
1360
0
  newsrv->flags &= ~SRV_F_BACKUP;
1361
0
  return 0;
1362
0
}
1363
1364
1365
/* Disable server PROXY protocol flags. */
1366
static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
1367
0
{
1368
0
  srv->pp_opts &= ~flags;
1369
0
  return 0;
1370
0
}
1371
1372
/* Parse the "no-send-proxy" server keyword */
1373
static int srv_parse_no_send_proxy(char **args, int *cur_arg,
1374
                                   struct proxy *curproxy, struct server *newsrv, char **err)
1375
0
{
1376
0
  return srv_disable_pp_flags(newsrv, SRV_PP_V1);
1377
0
}
1378
1379
/* Parse the "no-send-proxy-v2" server keyword */
1380
static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
1381
                                      struct proxy *curproxy, struct server *newsrv, char **err)
1382
0
{
1383
0
  return srv_disable_pp_flags(newsrv, SRV_PP_V2);
1384
0
}
1385
1386
/* Parse the "shard" server keyword */
1387
static int srv_parse_shard(char **args, int *cur_arg,
1388
                           struct proxy *curproxy, struct server *newsrv, char **err)
1389
0
{
1390
0
  newsrv->shard = atol(args[*cur_arg + 1]);
1391
0
  return 0;
1392
0
}
1393
1394
/* Parse the "no-tfo" server keyword */
1395
static int srv_parse_no_tfo(char **args, int *cur_arg,
1396
                            struct proxy *curproxy, struct server *newsrv, char **err)
1397
0
{
1398
0
  newsrv->flags &= ~SRV_F_FASTOPEN;
1399
0
  return 0;
1400
0
}
1401
1402
/* Parse the "non-stick" server keyword */
1403
static int srv_parse_non_stick(char **args, int *cur_arg,
1404
                               struct proxy *curproxy, struct server *newsrv, char **err)
1405
0
{
1406
0
  newsrv->flags |= SRV_F_NON_STICK;
1407
0
  return 0;
1408
0
}
1409
1410
/* Enable server PROXY protocol flags. */
1411
static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
1412
0
{
1413
0
  srv->pp_opts |= flags;
1414
0
  return 0;
1415
0
}
1416
/* parse the "proto" server keyword */
1417
static int srv_parse_proto(char **args, int *cur_arg,
1418
         struct proxy *px, struct server *newsrv, char **err)
1419
0
{
1420
0
  struct ist proto;
1421
1422
0
  if (!*args[*cur_arg + 1]) {
1423
0
    memprintf(err, "'%s' : missing value", args[*cur_arg]);
1424
0
    return ERR_ALERT | ERR_FATAL;
1425
0
  }
1426
0
  proto = ist(args[*cur_arg + 1]);
1427
0
  newsrv->mux_proto = get_mux_proto(proto);
1428
0
  if (!newsrv->mux_proto) {
1429
0
    memprintf(err, "'%s' :  unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
1430
0
    return ERR_ALERT | ERR_FATAL;
1431
0
  }
1432
0
  return 0;
1433
0
}
1434
1435
/* parse the "proxy-v2-options" */
1436
static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
1437
              struct proxy *px, struct server *newsrv, char **err)
1438
0
{
1439
0
  char *p, *n;
1440
0
  for (p = args[*cur_arg+1]; p; p = n) {
1441
0
    n = strchr(p, ',');
1442
0
    if (n)
1443
0
      *n++ = '\0';
1444
0
    if (strcmp(p, "ssl") == 0) {
1445
0
      newsrv->pp_opts |= SRV_PP_V2_SSL;
1446
0
    } else if (strcmp(p, "cert-cn") == 0) {
1447
0
      newsrv->pp_opts |= SRV_PP_V2_SSL;
1448
0
      newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
1449
0
    } else if (strcmp(p, "cert-key") == 0) {
1450
0
      newsrv->pp_opts |= SRV_PP_V2_SSL;
1451
0
      newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
1452
0
    } else if (strcmp(p, "cert-sig") == 0) {
1453
0
      newsrv->pp_opts |= SRV_PP_V2_SSL;
1454
0
      newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
1455
0
    } else if (strcmp(p, "ssl-cipher") == 0) {
1456
0
      newsrv->pp_opts |= SRV_PP_V2_SSL;
1457
0
      newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
1458
0
    } else if (strcmp(p, "authority") == 0) {
1459
0
      newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
1460
0
    } else if (strcmp(p, "crc32c") == 0) {
1461
0
      newsrv->pp_opts |= SRV_PP_V2_CRC32C;
1462
0
    } else if (strcmp(p, "unique-id") == 0) {
1463
0
      newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
1464
0
    } else
1465
0
      goto fail;
1466
0
  }
1467
0
  return 0;
1468
0
 fail:
1469
0
  if (err)
1470
0
    memprintf(err, "'%s' : proxy v2 option not implemented", p);
1471
0
  return ERR_ALERT | ERR_FATAL;
1472
0
}
1473
1474
/* Parse the "observe" server keyword */
1475
static int srv_parse_observe(char **args, int *cur_arg,
1476
                             struct proxy *curproxy, struct server *newsrv, char **err)
1477
0
{
1478
0
  char *arg;
1479
1480
0
  arg = args[*cur_arg + 1];
1481
0
  if (!*arg) {
1482
0
    memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
1483
0
    return ERR_ALERT | ERR_FATAL;
1484
0
  }
1485
1486
0
  if (strcmp(arg, "none") == 0) {
1487
0
    newsrv->observe = HANA_OBS_NONE;
1488
0
  }
1489
0
  else if (strcmp(arg, "layer4") == 0) {
1490
0
    newsrv->observe = HANA_OBS_LAYER4;
1491
0
  }
1492
0
  else if (strcmp(arg, "layer7") == 0) {
1493
0
    if (curproxy->mode != PR_MODE_HTTP) {
1494
0
      memprintf(err, "'%s' can only be used in http proxies.\n", arg);
1495
0
      return ERR_ALERT;
1496
0
    }
1497
0
    newsrv->observe = HANA_OBS_LAYER7;
1498
0
  }
1499
0
  else {
1500
0
    memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
1501
0
                   "but got '%s'\n", args[*cur_arg], arg);
1502
0
    return ERR_ALERT | ERR_FATAL;
1503
0
  }
1504
1505
0
  return 0;
1506
0
}
1507
1508
/* Parse the "on-error" server keyword */
1509
static int srv_parse_on_error(char **args, int *cur_arg,
1510
                              struct proxy *curproxy, struct server *newsrv, char **err)
1511
0
{
1512
0
  if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
1513
0
    newsrv->onerror = HANA_ONERR_FASTINTER;
1514
0
  else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
1515
0
    newsrv->onerror = HANA_ONERR_FAILCHK;
1516
0
  else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
1517
0
    newsrv->onerror = HANA_ONERR_SUDDTH;
1518
0
  else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
1519
0
    newsrv->onerror = HANA_ONERR_MARKDWN;
1520
0
  else {
1521
0
    memprintf(err, "'%s' expects one of 'fastinter', "
1522
0
              "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1523
0
              args[*cur_arg], args[*cur_arg + 1]);
1524
0
    return ERR_ALERT | ERR_FATAL;
1525
0
  }
1526
1527
0
  return 0;
1528
0
}
1529
1530
/* Parse the "on-marked-down" server keyword */
1531
static int srv_parse_on_marked_down(char **args, int *cur_arg,
1532
                                    struct proxy *curproxy, struct server *newsrv, char **err)
1533
0
{
1534
0
  if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1535
0
    newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1536
0
  else {
1537
0
    memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1538
0
              args[*cur_arg], args[*cur_arg + 1]);
1539
0
    return ERR_ALERT | ERR_FATAL;
1540
0
  }
1541
1542
0
  return 0;
1543
0
}
1544
1545
/* Parse the "on-marked-up" server keyword */
1546
static int srv_parse_on_marked_up(char **args, int *cur_arg,
1547
                                  struct proxy *curproxy, struct server *newsrv, char **err)
1548
0
{
1549
0
  if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1550
0
    newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1551
0
  else {
1552
0
    memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1553
0
              args[*cur_arg], args[*cur_arg + 1]);
1554
0
    return ERR_ALERT | ERR_FATAL;
1555
0
  }
1556
1557
0
  return 0;
1558
0
}
1559
1560
/* Parse the "redir" server keyword */
1561
static int srv_parse_redir(char **args, int *cur_arg,
1562
                           struct proxy *curproxy, struct server *newsrv, char **err)
1563
0
{
1564
0
  char *arg;
1565
1566
0
  arg = args[*cur_arg + 1];
1567
0
  if (!*arg) {
1568
0
    memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1569
0
    return ERR_ALERT | ERR_FATAL;
1570
0
  }
1571
1572
0
  free(newsrv->rdr_pfx);
1573
0
  newsrv->rdr_pfx = strdup(arg);
1574
0
  newsrv->rdr_len = strlen(arg);
1575
1576
0
  return 0;
1577
0
}
1578
1579
/* Parse the "resolvers" server keyword */
1580
static int srv_parse_resolvers(char **args, int *cur_arg,
1581
                           struct proxy *curproxy, struct server *newsrv, char **err)
1582
0
{
1583
0
  free(newsrv->resolvers_id);
1584
0
  newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1585
0
  return 0;
1586
0
}
1587
1588
/* Parse the "resolve-net" server keyword */
1589
static int srv_parse_resolve_net(char **args, int *cur_arg,
1590
                                 struct proxy *curproxy, struct server *newsrv, char **err)
1591
0
{
1592
0
  char *p, *e;
1593
0
  unsigned char mask;
1594
0
  struct resolv_options *opt;
1595
1596
0
  if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1597
0
    memprintf(err, "'%s' expects a list of networks.",
1598
0
              args[*cur_arg]);
1599
0
    return ERR_ALERT | ERR_FATAL;
1600
0
  }
1601
1602
0
  opt = &newsrv->resolv_opts;
1603
1604
  /* Split arguments by comma, and convert it from ipv4 or ipv6
1605
   * string network in in_addr or in6_addr.
1606
   */
1607
0
  p = args[*cur_arg + 1];
1608
0
  e = p;
1609
0
  while (*p != '\0') {
1610
    /* If no room available, return error. */
1611
0
    if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1612
0
      memprintf(err, "'%s' exceed %d networks.",
1613
0
                args[*cur_arg], SRV_MAX_PREF_NET);
1614
0
      return ERR_ALERT | ERR_FATAL;
1615
0
    }
1616
    /* look for end or comma. */
1617
0
    while (*e != ',' && *e != '\0')
1618
0
      e++;
1619
0
    if (*e == ',') {
1620
0
      *e = '\0';
1621
0
      e++;
1622
0
    }
1623
0
    if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1624
0
                      &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1625
      /* Try to convert input string from ipv4 or ipv6 network. */
1626
0
      opt->pref_net[opt->pref_net_nb].family = AF_INET;
1627
0
    } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1628
0
                         &mask)) {
1629
      /* Try to convert input string from ipv6 network. */
1630
0
      len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1631
0
      opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1632
0
    } else {
1633
      /* All network conversions fail, return error. */
1634
0
      memprintf(err, "'%s' invalid network '%s'.",
1635
0
                args[*cur_arg], p);
1636
0
      return ERR_ALERT | ERR_FATAL;
1637
0
    }
1638
0
    opt->pref_net_nb++;
1639
0
    p = e;
1640
0
  }
1641
1642
0
  return 0;
1643
0
}
1644
1645
/* Parse the "resolve-opts" server keyword */
1646
static int srv_parse_resolve_opts(char **args, int *cur_arg,
1647
                                  struct proxy *curproxy, struct server *newsrv, char **err)
1648
0
{
1649
0
  char *p, *end;
1650
1651
0
  for (p = args[*cur_arg + 1]; *p; p = end) {
1652
    /* cut on next comma */
1653
0
    for (end = p; *end && *end != ','; end++);
1654
0
    if (*end)
1655
0
      *(end++) = 0;
1656
1657
0
    if (strcmp(p, "allow-dup-ip") == 0) {
1658
0
      newsrv->resolv_opts.accept_duplicate_ip = 1;
1659
0
    }
1660
0
    else if (strcmp(p, "ignore-weight") == 0) {
1661
0
      newsrv->resolv_opts.ignore_weight = 1;
1662
0
    }
1663
0
    else if (strcmp(p, "prevent-dup-ip") == 0) {
1664
0
      newsrv->resolv_opts.accept_duplicate_ip = 0;
1665
0
    }
1666
0
    else {
1667
0
      memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1668
0
                args[*cur_arg], p);
1669
0
      return ERR_ALERT | ERR_FATAL;
1670
0
    }
1671
0
  }
1672
1673
0
  return 0;
1674
0
}
1675
1676
/* Parse the "resolve-prefer" server keyword */
1677
static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1678
                                    struct proxy *curproxy, struct server *newsrv, char **err)
1679
0
{
1680
0
  if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1681
0
    newsrv->resolv_opts.family_prio = AF_INET;
1682
0
  else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1683
0
    newsrv->resolv_opts.family_prio = AF_INET6;
1684
0
  else {
1685
0
    memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1686
0
              args[*cur_arg]);
1687
0
    return ERR_ALERT | ERR_FATAL;
1688
0
  }
1689
1690
0
  return 0;
1691
0
}
1692
1693
/* Parse the "send-proxy" server keyword */
1694
static int srv_parse_send_proxy(char **args, int *cur_arg,
1695
                                struct proxy *curproxy, struct server *newsrv, char **err)
1696
0
{
1697
0
  return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1698
0
}
1699
1700
/* Parse the "send-proxy-v2" server keyword */
1701
static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1702
                                   struct proxy *curproxy, struct server *newsrv, char **err)
1703
0
{
1704
0
  return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1705
0
}
1706
1707
/* Parse the "set-proxy-v2-tlv-fmt" server keyword */
1708
static int srv_parse_set_proxy_v2_tlv_fmt(char **args, int *cur_arg,
1709
                                   struct proxy *px, struct server *newsrv, char **err)
1710
0
{
1711
0
  char *error = NULL, *cmd = NULL;
1712
0
  unsigned int  tlv_type = 0;
1713
0
  struct srv_pp_tlv_list *srv_tlv = NULL;
1714
1715
0
  cmd = args[*cur_arg];
1716
0
  if (!*cmd) {
1717
0
    memprintf(err, "'%s' : could not read set-proxy-v2-tlv-fmt command", args[*cur_arg]);
1718
0
    goto fail;
1719
0
  }
1720
1721
0
  cmd += strlen("set-proxy-v2-tlv-fmt");
1722
1723
0
  if (*cmd == '(') {
1724
0
    cmd++; /* skip the '(' */
1725
0
    errno = 0;
1726
0
    tlv_type = strtoul(cmd, &error, 0); /* convert TLV ID */
1727
0
    if (unlikely((cmd == error) || (errno != 0))) {
1728
0
      memprintf(err, "'%s' : could not convert TLV ID", args[*cur_arg]);
1729
0
      goto fail;
1730
0
    }
1731
0
    if (errno == EINVAL) {
1732
0
      memprintf(err, "'%s' : could not find a valid number for the TLV ID", args[*cur_arg]);
1733
0
      goto fail;
1734
0
    }
1735
0
    if (*error != ')') {
1736
0
      memprintf(err, "'%s' : expects set-proxy-v2-tlv(<TLV ID>)", args[*cur_arg]);
1737
0
      goto fail;
1738
0
    }
1739
0
    if (tlv_type > 0xFF) {
1740
0
      memprintf(err, "'%s' : the maximum allowed TLV ID is %d", args[*cur_arg], 0xFF);
1741
0
      goto fail;
1742
0
    }
1743
0
  }
1744
1745
0
  srv_tlv = malloc(sizeof(*srv_tlv));
1746
0
  if (unlikely(!srv_tlv)) {
1747
0
    memprintf(err, "'%s' : failed to parse allocate TLV entry", args[*cur_arg]);
1748
0
    goto fail;
1749
0
  }
1750
0
  srv_tlv->type = tlv_type;
1751
0
  lf_expr_init(&srv_tlv->fmt);
1752
0
  srv_tlv->fmt_string = strdup(args[*cur_arg + 1]);
1753
0
  if (unlikely(!srv_tlv->fmt_string)) {
1754
0
    memprintf(err, "'%s' : failed to save format string for parsing", args[*cur_arg]);
1755
0
    goto fail;
1756
0
  }
1757
1758
0
  LIST_APPEND(&newsrv->pp_tlvs, &srv_tlv->list);
1759
1760
0
  (*cur_arg)++;
1761
1762
0
  return 0;
1763
1764
0
 fail:
1765
0
  free(srv_tlv);
1766
0
  errno = 0;
1767
0
  return ERR_ALERT | ERR_FATAL;
1768
0
}
1769
1770
/* Parse the "slowstart" server keyword */
1771
static int srv_parse_slowstart(char **args, int *cur_arg,
1772
                               struct proxy *curproxy, struct server *newsrv, char **err)
1773
0
{
1774
  /* slowstart is stored in seconds */
1775
0
  unsigned int val;
1776
0
  const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1777
1778
0
  if (time_err == PARSE_TIME_OVER) {
1779
0
    memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1780
0
              args[*cur_arg+1], args[*cur_arg], newsrv->id);
1781
0
    return ERR_ALERT | ERR_FATAL;
1782
0
  }
1783
0
  else if (time_err == PARSE_TIME_UNDER) {
1784
0
    memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1785
0
              args[*cur_arg+1], args[*cur_arg], newsrv->id);
1786
0
    return ERR_ALERT | ERR_FATAL;
1787
0
  }
1788
0
  else if (time_err) {
1789
0
    memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1790
0
              *time_err, newsrv->id);
1791
0
    return ERR_ALERT | ERR_FATAL;
1792
0
  }
1793
0
  newsrv->slowstart = (val + 999) / 1000;
1794
1795
0
  return 0;
1796
0
}
1797
1798
/* Parse the "source" server keyword */
1799
static int srv_parse_source(char **args, int *cur_arg,
1800
                            struct proxy *curproxy, struct server *newsrv, char **err)
1801
0
{
1802
0
  char *errmsg;
1803
0
  int port_low, port_high;
1804
0
  struct sockaddr_storage *sk;
1805
1806
0
  errmsg = NULL;
1807
1808
0
  if (!*args[*cur_arg + 1]) {
1809
0
    memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1810
0
                   "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1811
0
    goto err;
1812
0
  }
1813
1814
  /* 'sk' is statically allocated (no need to be freed). */
1815
0
  sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, NULL,
1816
0
                    &errmsg, NULL, NULL, NULL,
1817
0
              PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM | PA_O_CONNECT);
1818
0
  if (!sk) {
1819
0
    memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1820
0
    goto err;
1821
0
  }
1822
1823
0
  newsrv->conn_src.opts |= CO_SRC_BIND;
1824
0
  newsrv->conn_src.source_addr = *sk;
1825
1826
0
  if (port_low != port_high) {
1827
0
    int i;
1828
1829
0
    newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
1830
0
    if (!newsrv->conn_src.sport_range) {
1831
0
      ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1832
0
      goto err;
1833
0
    }
1834
0
    for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1835
0
      newsrv->conn_src.sport_range->ports[i] = port_low + i;
1836
0
  }
1837
1838
0
  *cur_arg += 2;
1839
0
  while (*(args[*cur_arg])) {
1840
0
    if (strcmp(args[*cur_arg], "usesrc") == 0) {  /* address to use outside */
1841
0
#if defined(CONFIG_HAP_TRANSPARENT)
1842
0
      if (!*args[*cur_arg + 1]) {
1843
0
        ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1844
0
           "or 'hdr_ip(name,#)' as argument.\n");
1845
0
        goto err;
1846
0
      }
1847
0
      if (strcmp(args[*cur_arg + 1], "client") == 0) {
1848
0
        newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1849
0
        newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1850
0
      }
1851
0
      else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
1852
0
        newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1853
0
        newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1854
0
      }
1855
0
      else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1856
0
        char *name, *end;
1857
1858
0
        name = args[*cur_arg + 1] + 7;
1859
0
        while (isspace((unsigned char)*name))
1860
0
          name++;
1861
1862
0
        end = name;
1863
0
        while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
1864
0
          end++;
1865
1866
0
        newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1867
0
        newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1868
0
        free(newsrv->conn_src.bind_hdr_name);
1869
0
        newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
1870
0
        if (!newsrv->conn_src.bind_hdr_name) {
1871
0
          ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1872
0
          goto err;
1873
0
        }
1874
0
        newsrv->conn_src.bind_hdr_len = end - name;
1875
0
        memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1876
0
        newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1877
0
        newsrv->conn_src.bind_hdr_occ = -1;
1878
1879
        /* now look for an occurrence number */
1880
0
        while (isspace((unsigned char)*end))
1881
0
          end++;
1882
0
        if (*end == ',') {
1883
0
          end++;
1884
0
          name = end;
1885
0
          if (*end == '-')
1886
0
            end++;
1887
0
          while (isdigit((unsigned char)*end))
1888
0
            end++;
1889
0
          newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1890
0
        }
1891
1892
0
        if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
1893
0
          ha_alert("usesrc hdr_ip(name,num) does not support negative"
1894
0
             " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
1895
0
          goto err;
1896
0
        }
1897
0
      }
1898
0
      else {
1899
0
        struct sockaddr_storage *sk;
1900
0
        int port1, port2;
1901
1902
        /* 'sk' is statically allocated (no need to be freed). */
1903
0
        sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL, NULL,
1904
0
                          &errmsg, NULL, NULL, NULL,
1905
0
                          PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
1906
0
        if (!sk) {
1907
0
          ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1908
0
          goto err;
1909
0
        }
1910
1911
0
        newsrv->conn_src.tproxy_addr = *sk;
1912
0
        newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1913
0
      }
1914
0
      global.last_checks |= LSTCHK_NETADM;
1915
0
      *cur_arg += 2;
1916
0
      continue;
1917
#else /* no TPROXY support */
1918
      ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
1919
      goto err;
1920
#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1921
0
    } /* "usesrc" */
1922
1923
0
    if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
1924
0
#ifdef SO_BINDTODEVICE
1925
0
      if (!*args[*cur_arg + 1]) {
1926
0
        ha_alert("'%s' : missing interface name.\n", args[0]);
1927
0
        goto err;
1928
0
      }
1929
0
      free(newsrv->conn_src.iface_name);
1930
0
      newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1931
0
      newsrv->conn_src.iface_len  = strlen(newsrv->conn_src.iface_name);
1932
0
      global.last_checks |= LSTCHK_NETADM;
1933
#else
1934
      ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
1935
      goto err;
1936
#endif
1937
0
      *cur_arg += 2;
1938
0
      continue;
1939
0
    }
1940
    /* this keyword in not an option of "source" */
1941
0
    break;
1942
0
  } /* while */
1943
1944
0
  return 0;
1945
1946
0
 err:
1947
0
  free(errmsg);
1948
0
  return ERR_ALERT | ERR_FATAL;
1949
0
}
1950
1951
/* Parse the "stick" server keyword */
1952
static int srv_parse_stick(char **args, int *cur_arg,
1953
                           struct proxy *curproxy, struct server *newsrv, char **err)
1954
0
{
1955
0
  newsrv->flags &= ~SRV_F_NON_STICK;
1956
0
  return 0;
1957
0
}
1958
1959
/* Parse the "track" server keyword */
1960
static int srv_parse_track(char **args, int *cur_arg,
1961
                           struct proxy *curproxy, struct server *newsrv, char **err)
1962
0
{
1963
0
  char *arg;
1964
1965
0
  arg = args[*cur_arg + 1];
1966
0
  if (!*arg) {
1967
0
    memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1968
0
    return ERR_ALERT | ERR_FATAL;
1969
0
  }
1970
1971
0
  free(newsrv->trackit);
1972
0
  newsrv->trackit = strdup(arg);
1973
1974
0
  return 0;
1975
0
}
1976
1977
/* Parse the "socks4" server keyword */
1978
static int srv_parse_socks4(char **args, int *cur_arg,
1979
                            struct proxy *curproxy, struct server *newsrv, char **err)
1980
0
{
1981
0
  char *errmsg;
1982
0
  int port_low, port_high;
1983
0
  struct sockaddr_storage *sk;
1984
1985
0
  errmsg = NULL;
1986
1987
0
  if (!*args[*cur_arg + 1]) {
1988
0
    memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1989
0
    goto err;
1990
0
  }
1991
1992
  /* 'sk' is statically allocated (no need to be freed). */
1993
0
  sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, NULL,
1994
0
                    &errmsg, NULL, NULL, NULL,
1995
0
                    PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
1996
0
  if (!sk) {
1997
0
    memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1998
0
    goto err;
1999
0
  }
2000
2001
0
  newsrv->flags |= SRV_F_SOCKS4_PROXY;
2002
0
  newsrv->socks4_addr = *sk;
2003
2004
0
  return 0;
2005
2006
0
 err:
2007
0
  free(errmsg);
2008
0
  return ERR_ALERT | ERR_FATAL;
2009
0
}
2010
2011
2012
/* parse the "tfo" server keyword */
2013
static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2014
0
{
2015
0
  newsrv->flags |= SRV_F_FASTOPEN;
2016
0
  return 0;
2017
0
}
2018
2019
/* parse the "usesrc" server keyword */
2020
static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2021
0
{
2022
0
  memprintf(err, "'%s' only allowed after a '%s' statement.",
2023
0
            "usesrc", "source");
2024
0
  return ERR_ALERT | ERR_FATAL;
2025
0
}
2026
2027
/* parse the "weight" server keyword */
2028
static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2029
0
{
2030
0
  int w;
2031
2032
0
  w = atol(args[*cur_arg + 1]);
2033
0
  if (w < 0 || w > SRV_UWGHT_MAX) {
2034
0
    memprintf(err, "weight of server %s is not within 0 and %d (%d).",
2035
0
              newsrv->id, SRV_UWGHT_MAX, w);
2036
0
    return ERR_ALERT | ERR_FATAL;
2037
0
  }
2038
0
  newsrv->uweight = newsrv->iweight = w;
2039
2040
0
  return 0;
2041
0
}
2042
2043
static int srv_parse_strict_maxconn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2044
0
{
2045
2046
0
  newsrv->flags |= SRV_F_STRICT_MAXCONN;
2047
2048
0
  return 0;
2049
0
}
2050
/* Returns 1 if the server has streams pointing to it, and 0 otherwise. */
2051
static int srv_has_streams(struct server *srv)
2052
0
{
2053
0
  return !!_HA_ATOMIC_LOAD(&srv->served);
2054
0
}
2055
2056
/* Shutdown all connections of a server. The caller must pass a termination
2057
 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
2058
 * shutdown.
2059
 *
2060
 * Must be called with the server lock held.
2061
 */
2062
void srv_shutdown_streams(struct server *srv, int why)
2063
0
{
2064
0
  struct stream *stream;
2065
0
  struct mt_list back;
2066
0
  int thr;
2067
2068
0
  for (thr = 0; thr < global.nbthread; thr++)
2069
0
    MT_LIST_FOR_EACH_ENTRY_LOCKED(stream, &srv->per_thr[thr].streams, by_srv, back)
2070
0
      if (stream->srv_conn == srv)
2071
0
        stream_shutdown(stream, why);
2072
2073
  /* also kill the possibly pending streams in the queue */
2074
0
  pendconn_redistribute(srv);
2075
0
}
2076
2077
/* Shutdown all connections of all backup servers of a proxy. The caller must
2078
 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
2079
 * the reason for the shutdown.
2080
 *
2081
 * Must be called with the server lock held.
2082
 */
2083
void srv_shutdown_backup_streams(struct proxy *px, int why)
2084
0
{
2085
0
  struct server *srv;
2086
2087
0
  for (srv = px->srv; srv != NULL; srv = srv->next)
2088
0
    if (srv->flags & SRV_F_BACKUP)
2089
0
      srv_shutdown_streams(srv, why);
2090
0
}
2091
2092
static void srv_append_op_chg_cause(struct buffer *msg, struct server *s, enum srv_op_st_chg_cause cause)
2093
0
{
2094
0
  switch (cause) {
2095
0
    case SRV_OP_STCHGC_NONE:
2096
0
      break; /* do nothing */
2097
0
    case SRV_OP_STCHGC_HEALTH:
2098
0
      check_append_info(msg, &s->check);
2099
0
      break;
2100
0
    case SRV_OP_STCHGC_AGENT:
2101
0
      check_append_info(msg, &s->agent);
2102
0
      break;
2103
0
    default:
2104
0
      chunk_appendf(msg, ", %s", srv_op_st_chg_cause(cause));
2105
0
      break;
2106
0
  }
2107
0
}
2108
2109
static void srv_append_adm_chg_cause(struct buffer *msg, struct server *s, enum srv_adm_st_chg_cause cause)
2110
0
{
2111
0
  if (cause)
2112
0
    chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(cause));
2113
0
}
2114
2115
/* Appends some information to a message string related to a server tracking
2116
 * or requeued connections info.
2117
 *
2118
 * If <forced> is null and the server tracks another one, a "via"
2119
 * If <xferred> is non-negative, some information about requeued sessions are
2120
 * provided.
2121
 *
2122
 * Must be called with the server lock held.
2123
 */
2124
static void srv_append_more(struct buffer *msg, struct server *s,
2125
                            int xferred, int forced)
2126
0
{
2127
0
  if (!forced && s->track) {
2128
0
    chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
2129
0
  }
2130
2131
0
  if (xferred >= 0) {
2132
0
    if (s->next_state == SRV_ST_STOPPED)
2133
0
      chunk_appendf(msg, ". %d active and %d backup servers left.%s"
2134
0
        " %d sessions active, %d requeued, %d remaining in queue",
2135
0
        s->proxy->srv_act, s->proxy->srv_bck,
2136
0
        (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
2137
0
        s->cur_sess, xferred, s->queueslength);
2138
0
    else
2139
0
      chunk_appendf(msg, ". %d active and %d backup servers online.%s"
2140
0
        " %d sessions requeued, %d total in queue",
2141
0
        s->proxy->srv_act, s->proxy->srv_bck,
2142
0
        (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
2143
0
        xferred, s->queueslength);
2144
0
  }
2145
0
}
2146
2147
/* Marks server <s> down, regardless of its checks' statuses. The server
2148
 * transfers queued streams whenever possible to other servers at a sync
2149
 * point. Maintenance servers are ignored.
2150
 *
2151
 * Must be called with the server lock held.
2152
 */
2153
void srv_set_stopped(struct server *s, enum srv_op_st_chg_cause cause)
2154
0
{
2155
0
  struct server *srv;
2156
2157
0
  if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
2158
0
    return;
2159
2160
0
  s->next_state = SRV_ST_STOPPED;
2161
2162
  /* propagate changes */
2163
0
  srv_update_status(s, 0, cause);
2164
2165
0
  for (srv = s->trackers; srv; srv = srv->tracknext) {
2166
0
    HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
2167
0
    srv_set_stopped(srv, SRV_OP_STCHGC_NONE);
2168
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2169
0
  }
2170
0
}
2171
2172
/* Marks server <s> up regardless of its checks' statuses and provided it isn't
2173
 * in maintenance. The server tries to grab requests from the proxy at a sync
2174
 * point. Maintenance servers are ignored.
2175
 *
2176
 * Must be called with the server lock held.
2177
 */
2178
void srv_set_running(struct server *s, enum srv_op_st_chg_cause cause)
2179
0
{
2180
0
  struct server *srv;
2181
2182
0
  if (s->cur_admin & SRV_ADMF_MAINT)
2183
0
    return;
2184
2185
0
  if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
2186
0
    return;
2187
2188
0
  s->next_state = SRV_ST_STARTING;
2189
2190
0
  if (s->slowstart <= 0)
2191
0
    s->next_state = SRV_ST_RUNNING;
2192
2193
  /* propagate changes */
2194
0
  srv_update_status(s, 0, cause);
2195
2196
0
  for (srv = s->trackers; srv; srv = srv->tracknext) {
2197
0
    HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
2198
0
    srv_set_running(srv, SRV_OP_STCHGC_NONE);
2199
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2200
0
  }
2201
0
}
2202
2203
/* Marks server <s> stopping regardless of its checks' statuses and provided it
2204
 * isn't in maintenance. The server tries to redispatch pending requests
2205
 * to the proxy. Maintenance servers are ignored.
2206
 *
2207
 * Must be called with the server lock held.
2208
 */
2209
void srv_set_stopping(struct server *s, enum srv_op_st_chg_cause cause)
2210
0
{
2211
0
  struct server *srv;
2212
2213
0
  if (s->cur_admin & SRV_ADMF_MAINT)
2214
0
    return;
2215
2216
0
  if (s->next_state == SRV_ST_STOPPING)
2217
0
    return;
2218
2219
0
  s->next_state = SRV_ST_STOPPING;
2220
2221
  /* propagate changes */
2222
0
  srv_update_status(s, 0, cause);
2223
2224
0
  for (srv = s->trackers; srv; srv = srv->tracknext) {
2225
0
    HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
2226
0
    srv_set_stopping(srv, SRV_OP_STCHGC_NONE);
2227
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2228
0
  }
2229
0
}
2230
2231
/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
2232
 * enforce either maint mode or drain mode. It is not allowed to set more than
2233
 * one flag at once. The equivalent "inherited" flag is propagated to all
2234
 * tracking servers. Maintenance mode disables health checks (but not agent
2235
 * checks). When either the flag is already set or no flag is passed, nothing
2236
 * is done. If <cause> is non-null, it will be displayed at the end of the log
2237
 * lines to justify the state change.
2238
 *
2239
 * Must be called with the server lock held.
2240
 */
2241
void srv_set_admin_flag(struct server *s, enum srv_admin mode, enum srv_adm_st_chg_cause cause)
2242
0
{
2243
0
  struct server *srv;
2244
2245
0
  if (!mode)
2246
0
    return;
2247
2248
  /* stop going down as soon as we meet a server already in the same state */
2249
0
  if (s->next_admin & mode)
2250
0
    return;
2251
2252
0
  s->next_admin |= mode;
2253
2254
  /* propagate changes */
2255
0
  srv_update_status(s, 1, cause);
2256
2257
  /* stop going down if the equivalent flag was already present (forced or inherited) */
2258
0
  if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
2259
0
      ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
2260
0
    return;
2261
2262
  /* compute the inherited flag to propagate */
2263
0
  if (mode & SRV_ADMF_MAINT)
2264
0
    mode = SRV_ADMF_IMAINT;
2265
0
  else if (mode & SRV_ADMF_DRAIN)
2266
0
    mode = SRV_ADMF_IDRAIN;
2267
2268
0
  for (srv = s->trackers; srv; srv = srv->tracknext) {
2269
0
    HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
2270
0
    srv_set_admin_flag(srv, mode, cause);
2271
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2272
0
  }
2273
0
}
2274
2275
/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
2276
 * stop enforcing either maint mode or drain mode. It is not allowed to set more
2277
 * than one flag at once. The equivalent "inherited" flag is propagated to all
2278
 * tracking servers. Leaving maintenance mode re-enables health checks. When
2279
 * either the flag is already cleared or no flag is passed, nothing is done.
2280
 *
2281
 * Must be called with the server lock held.
2282
 */
2283
void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
2284
0
{
2285
0
  struct server *srv;
2286
2287
0
  if (!mode)
2288
0
    return;
2289
2290
  /* stop going down as soon as we see the flag is not there anymore */
2291
0
  if (!(s->next_admin & mode))
2292
0
    return;
2293
2294
0
  s->next_admin &= ~mode;
2295
2296
  /* propagate changes */
2297
0
  srv_update_status(s, 1, SRV_ADM_STCHGC_NONE);
2298
2299
  /* stop going down if the equivalent flag is still present (forced or inherited) */
2300
0
  if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
2301
0
      ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
2302
0
    return;
2303
2304
0
  if (mode & SRV_ADMF_MAINT)
2305
0
    mode = SRV_ADMF_IMAINT;
2306
0
  else if (mode & SRV_ADMF_DRAIN)
2307
0
    mode = SRV_ADMF_IDRAIN;
2308
2309
0
  for (srv = s->trackers; srv; srv = srv->tracknext) {
2310
0
    HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
2311
0
    srv_clr_admin_flag(srv, mode);
2312
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2313
0
  }
2314
0
}
2315
2316
/* principle: propagate maint and drain to tracking servers. This is useful
2317
 * upon startup so that inherited states are correct.
2318
 */
2319
static void srv_propagate_admin_state(struct server *srv)
2320
0
{
2321
0
  struct server *srv2;
2322
2323
0
  if (!srv->trackers)
2324
0
    return;
2325
2326
0
  for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
2327
0
    HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
2328
0
    if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
2329
0
      srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE);
2330
2331
0
    if (srv->next_admin & SRV_ADMF_DRAIN)
2332
0
      srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE);
2333
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
2334
0
  }
2335
0
}
2336
2337
/* Compute and propagate the admin states for all servers in proxy <px>.
2338
 * Only servers *not* tracking another one are considered, because other
2339
 * ones will be handled when the server they track is visited.
2340
 */
2341
void srv_compute_all_admin_states(struct proxy *px)
2342
0
{
2343
0
  struct server *srv;
2344
2345
0
  for (srv = px->srv; srv; srv = srv->next) {
2346
0
    if (srv->track)
2347
0
      continue;
2348
0
    srv_propagate_admin_state(srv);
2349
0
  }
2350
0
}
2351
2352
/* Note: must not be declared <const> as its list will be overwritten.
2353
 *
2354
 ***   P L E A S E   R E A D   B E L O W   B E F O R E   T O U C H I N G  !!! ***
2355
 *
2356
 * Some mistakes are commonly repeated when touching this table, so please
2357
 * read the following rules before changing / adding an entry, and better
2358
 * ask on the mailing list in case of doubt.
2359
 *
2360
 *  - this list is alphabetically ordered, doing so helps all code contributors
2361
 *    spot how to name a keyword, which helps users thanks to a form of naming
2362
 *    consistency. Please insert new entries at the right position so as not
2363
 *    to break alphabetical ordering. If in doubt, sorting the lines in your
2364
 *    editor should not change anything (or should fix your addition).
2365
 *
2366
 *  - the fields for each entry in the array are, from left to right:
2367
 *      - the keyword itself (a string, all characters lower case, no special
2368
 *        chars, no space/dot/underscore, use-dash-to-delimit-multiple-words)
2369
 *      - the parsing function (edit or copy one close to your needs, parsers
2370
 *        can easily support multiple keywords if adapted to check args[0]).
2371
 *      - the number of arguments the keyword takes. Please do not add new
2372
 *        keywords taking other than exactly 1 argument, they're hard to adapt
2373
 *        to for external parsers. The special value -1 indicates a variable
2374
 *        number, used by "source" only. Never do this.
2375
 *      - whether or not the keyword is supported on default-server lines
2376
 *        (0 = not supported, 1 = supported). Please do not add unsupported
2377
 *        keywords without a prior discussion with maintainers on the list,
2378
 *        as usually it hides a deeper problem.
2379
 *      - whether or not the keyword is supported for dynamic servers added at
2380
 *        run time on the CLI (0 = not supported, 1 = supported). Please do not
2381
 *        add unsupported keywords without a prior discussion with maintainers
2382
 *        on the list, as usually it hides a deeper problem.
2383
 *
2384
 *  - please also add a short comment reminding what the keyword does.
2385
 *
2386
 *  - please test your changes with default-server and dynamic servers on the
2387
 *    CLI (see "add server" in the management guide).
2388
 *
2389
 ***   P L E A S E   R E A D   A B O V E   B E F O R E   T O U C H I N G  !!! ***
2390
 *
2391
 * Optional keywords are also declared with a NULL ->parse() function so that
2392
 * the config parser can report an appropriate error when a known keyword was
2393
 * not enabled.
2394
 */
2395
static struct srv_kw_list srv_kws = { "ALL", { }, {
2396
/*  { "keyword",              parsing_function,            args, def, dyn }, */
2397
  { "backup",               srv_parse_backup,               0,  1,  1 }, /* Flag as backup server */
2398
  { "cookie",               srv_parse_cookie,               1,  1,  1 }, /* Assign a cookie to the server */
2399
  { "disabled",             srv_parse_disabled,             0,  1,  1 }, /* Start the server in 'disabled' state */
2400
  { "enabled",              srv_parse_enabled,              0,  1,  0 }, /* Start the server in 'enabled' state */
2401
  { "error-limit",          srv_parse_error_limit,          1,  1,  1 }, /* Configure the consecutive count of check failures to consider a server on error */
2402
  { "guid",                 srv_parse_guid,                 1,  0,  1 }, /* Set global unique ID of the server */
2403
  { "ws",                   srv_parse_ws,                   1,  1,  1 }, /* websocket protocol */
2404
  { "hash-key",             srv_parse_hash_key,             1,  1,  1 }, /* Configure how chash keys are computed */
2405
  { "id",                   srv_parse_id,                   1,  0,  1 }, /* set id# of server */
2406
  { "idle-ping",            srv_parse_idle_ping,            1,  1,  1 }, /* Activate idle ping if mux support it */
2407
  { "init-addr",            srv_parse_init_addr,            1,  1,  0 }, /* */
2408
  { "init-state",           srv_parse_init_state,           1,  1,  1 }, /* Set the initial state of the server */
2409
  { "log-bufsize",          srv_parse_log_bufsize,          1,  1,  0 }, /* Set the ring bufsize for log server (only for log backends) */
2410
  { "log-proto",            srv_parse_log_proto,            1,  1,  0 }, /* Set the protocol for event messages, only relevant in a log or ring section */
2411
  { "maxconn",              srv_parse_maxconn,              1,  1,  1 }, /* Set the max number of concurrent connection */
2412
  { "maxqueue",             srv_parse_maxqueue,             1,  1,  1 }, /* Set the max number of connection to put in queue */
2413
  { "max-reuse",            srv_parse_max_reuse,            1,  1,  0 }, /* Set the max number of requests on a connection, -1 means unlimited */
2414
  { "minconn",              srv_parse_minconn,              1,  1,  1 }, /* Enable a dynamic maxconn limit */
2415
  { "namespace",            srv_parse_namespace,            1,  1,  0 }, /* Namespace the server socket belongs to (if supported) */
2416
  { "no-backup",            srv_parse_no_backup,            0,  1,  1 }, /* Flag as non-backup server */
2417
  { "no-send-proxy",        srv_parse_no_send_proxy,        0,  1,  1 }, /* Disable use of PROXY V1 protocol */
2418
  { "no-send-proxy-v2",     srv_parse_no_send_proxy_v2,     0,  1,  1 }, /* Disable use of PROXY V2 protocol */
2419
  { "no-tfo",               srv_parse_no_tfo,               0,  1,  1 }, /* Disable use of TCP Fast Open */
2420
  { "non-stick",            srv_parse_non_stick,            0,  1,  0 }, /* Disable stick-table persistence */
2421
  { "observe",              srv_parse_observe,              1,  1,  1 }, /* Enables health adjusting based on observing communication with the server */
2422
  { "on-error",             srv_parse_on_error,             1,  1,  1 }, /* Configure the action on check failure */
2423
  { "on-marked-down",       srv_parse_on_marked_down,       1,  1,  1 }, /* Configure the action when a server is marked down */
2424
  { "on-marked-up",         srv_parse_on_marked_up,         1,  1,  1 }, /* Configure the action when a server is marked up */
2425
  { "pool-conn-name",       srv_parse_pool_conn_name,       1,  1,  1 }, /* Define expression to identify connections in idle pool */
2426
  { "pool-low-conn",        srv_parse_pool_low_conn,        1,  1,  1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */
2427
  { "pool-max-conn",        srv_parse_pool_max_conn,        1,  1,  1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
2428
  { "pool-purge-delay",     srv_parse_pool_purge_delay,     1,  1,  1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
2429
  { "proto",                srv_parse_proto,                1,  1,  1 }, /* Set the proto to use for all outgoing connections */
2430
  { "proxy-v2-options",     srv_parse_proxy_v2_options,     1,  1,  1 }, /* options for send-proxy-v2 */
2431
  { "redir",                srv_parse_redir,                1,  1,  0 }, /* Enable redirection mode */
2432
  { "resolve-net",          srv_parse_resolve_net,          1,  1,  0 }, /* Set the preferred network range for name resolution */
2433
  { "resolve-opts",         srv_parse_resolve_opts,         1,  1,  0 }, /* Set options for name resolution */
2434
  { "resolve-prefer",       srv_parse_resolve_prefer,       1,  1,  0 }, /* Set the preferred family for name resolution */
2435
  { "resolvers",            srv_parse_resolvers,            1,  1,  0 }, /* Configure the resolver to use for name resolution */
2436
  { "send-proxy",           srv_parse_send_proxy,           0,  1,  1 }, /* Enforce use of PROXY V1 protocol */
2437
  { "send-proxy-v2",        srv_parse_send_proxy_v2,        0,  1,  1 }, /* Enforce use of PROXY V2 protocol */
2438
  { "set-proxy-v2-tlv-fmt", srv_parse_set_proxy_v2_tlv_fmt, 0,  1,  1 }, /* Set TLV of PROXY V2 protocol */
2439
  { "shard",                srv_parse_shard,                1,  1,  1 }, /* Server shard (only in peers protocol context) */
2440
  { "slowstart",            srv_parse_slowstart,            1,  1,  1 }, /* Set the warm-up timer for a previously failed server */
2441
  { "source",               srv_parse_source,              -1,  1,  1 }, /* Set the source address to be used to connect to the server */
2442
  { "stick",                srv_parse_stick,                0,  1,  0 }, /* Enable stick-table persistence */
2443
  { "strict-maxconn",       srv_parse_strict_maxconn,       0,  1,  1 }, /* Strictly enforces maxconn */
2444
  { "tfo",                  srv_parse_tfo,                  0,  1,  1 }, /* enable TCP Fast Open of server */
2445
  { "track",                srv_parse_track,                1,  1,  1 }, /* Set the current state of the server, tracking another one */
2446
  { "socks4",               srv_parse_socks4,               1,  1,  0 }, /* Set the socks4 proxy of the server*/
2447
  { "usesrc",               srv_parse_usesrc,               0,  1,  1 }, /* safe-guard against usesrc without preceding <source> keyword */
2448
  { "weight",               srv_parse_weight,               1,  1,  1 }, /* Set the load-balancing weight */
2449
  { NULL, NULL, 0 },
2450
}};
2451
2452
INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
2453
2454
/* Recomputes the server's eweight based on its state, uweight, the current time,
2455
 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
2456
 * state is automatically disabled if the time is elapsed. If <must_update> is
2457
 * not zero, the update will be propagated immediately.
2458
 *
2459
 * Must be called with the server lock held.
2460
 */
2461
void server_recalc_eweight(struct server *sv, int must_update)
2462
0
{
2463
0
  unsigned long last_change = COUNTERS_SHARED_LAST(sv->proxy->be_counters.shared->tg, last_change);
2464
0
  struct proxy *px = sv->proxy;
2465
0
  unsigned w;
2466
2467
0
  if (ns_to_sec(now_ns) < last_change || ns_to_sec(now_ns) >= last_change + sv->slowstart) {
2468
    /* go to full throttle if the slowstart interval is reached unless server is currently down */
2469
0
    if ((sv->cur_state != SRV_ST_STOPPED) && (sv->next_state == SRV_ST_STARTING))
2470
0
      sv->next_state = SRV_ST_RUNNING;
2471
0
  }
2472
2473
  /* We must take care of not pushing the server to full throttle during slow starts.
2474
   * It must also start immediately, at least at the minimal step when leaving maintenance.
2475
   */
2476
0
  if ((sv->cur_state == SRV_ST_STOPPED) && (sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
2477
0
    w = 1;
2478
0
  else if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
2479
0
    w = (px->lbprm.wdiv * (ns_to_sec(now_ns) - last_change) + sv->slowstart) / sv->slowstart;
2480
0
  else
2481
0
    w = px->lbprm.wdiv;
2482
2483
0
  sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
2484
2485
  /* propagate changes only if needed (i.e. not recursively) */
2486
0
  if (must_update)
2487
0
    srv_update_status(sv, 0, SRV_OP_STCHGC_NONE);
2488
0
}
2489
2490
/* requeuing tasklet used to asynchronously queue the server into its tree in
2491
 * case of extreme contention. It is woken up by the code that failed to grab
2492
 * an important lock.
2493
 */
2494
struct task *server_requeue(struct task *t, void *context, unsigned int state)
2495
0
{
2496
0
  struct server *srv = context;
2497
2498
  /* let's call the LB's requeue function. If it fails, it will itself
2499
   * wake us up.
2500
   */
2501
0
  if (srv->proxy->lbprm.server_requeue)
2502
0
    srv->proxy->lbprm.server_requeue(srv);
2503
0
  return t;
2504
0
}
2505
2506
/*
2507
 * Parses weight_str and configures sv accordingly.
2508
 * Returns NULL on success, error message string otherwise.
2509
 *
2510
 * Must be called with the server lock held.
2511
 */
2512
const char *server_parse_weight_change_request(struct server *sv,
2513
                 const char *weight_str)
2514
0
{
2515
0
  struct proxy *px;
2516
0
  long int w;
2517
0
  char *end;
2518
2519
0
  px = sv->proxy;
2520
2521
  /* if the weight is terminated with '%', it is set relative to
2522
   * the initial weight, otherwise it is absolute.
2523
   */
2524
0
  if (!*weight_str)
2525
0
    return "Require <weight> or <weight%>.\n";
2526
2527
0
  w = strtol(weight_str, &end, 10);
2528
0
  if (end == weight_str)
2529
0
    return "Empty weight string empty or preceded by garbage\n";
2530
0
  else if (end[0] == '%' && end[1] == '\0') {
2531
0
    if (w < 0)
2532
0
      return "Relative weight must be positive.\n";
2533
    /* Avoid integer overflow */
2534
0
    if (w > 25600)
2535
0
      w = 25600;
2536
0
    w = sv->iweight * w / 100;
2537
0
    if (w > 256)
2538
0
      w = 256;
2539
0
  }
2540
0
  else if (w < 0 || w > 256)
2541
0
    return "Absolute weight can only be between 0 and 256 inclusive.\n";
2542
0
  else if (end[0] != '\0')
2543
0
    return "Trailing garbage in weight string\n";
2544
2545
0
  if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
2546
0
    return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
2547
2548
0
  sv->uweight = w;
2549
0
  server_recalc_eweight(sv, 1);
2550
2551
0
  return NULL;
2552
0
}
2553
2554
/*
2555
 * Must be called with the server lock held.
2556
 */
2557
const char *server_parse_maxconn_change_request(struct server *sv,
2558
                                                const char *maxconn_str)
2559
0
{
2560
0
  long int v;
2561
0
  char *end;
2562
2563
0
  if (!*maxconn_str)
2564
0
    return "Require <maxconn>.\n";
2565
2566
0
  v = strtol(maxconn_str, &end, 10);
2567
0
  if (end == maxconn_str)
2568
0
    return "maxconn string empty or preceded by garbage\n";
2569
0
  else if (end[0] != '\0')
2570
0
    return "Trailing garbage in maxconn string\n";
2571
2572
0
  if (sv->maxconn == sv->minconn) { // static maxconn
2573
0
    sv->maxconn = sv->minconn = v;
2574
0
  } else { // dynamic maxconn
2575
0
    sv->maxconn = v;
2576
0
  }
2577
2578
0
  if (may_dequeue_tasks(sv, sv->proxy))
2579
0
    process_srv_queue(sv);
2580
2581
0
  return NULL;
2582
0
}
2583
2584
/* Interpret <expr> as sample expression. This function is reserved for
2585
 * internal server allocation. On parsing use parse_srv_expr() for extra sample
2586
 * check validity.
2587
 *
2588
 * Returns the allocated sample on success or NULL on error.
2589
 */
2590
struct sample_expr *_parse_srv_expr(char *expr, struct arg_list *args_px,
2591
                                    const char *file, int linenum, char **err)
2592
0
{
2593
0
  int idx;
2594
0
  const char *args[] = {
2595
0
    expr,
2596
0
    NULL,
2597
0
  };
2598
2599
0
  idx = 0;
2600
0
  args_px->ctx = ARGC_SRV;
2601
2602
0
  return sample_parse_expr((char **)args, &idx, file, linenum, err, args_px, NULL);
2603
0
}
2604
2605
/* Interpret <str> if not empty as a sample expression and store it into <out>.
2606
 * Contrary to _parse_srv_expr(), fetch scope validity is checked to ensure it
2607
 * is valid on a server line context. It also updates <px> HTTP mode
2608
 * requirement depending on fetch method used.
2609
 *
2610
 * Returns 0 on success else non zero.
2611
 */
2612
static int parse_srv_expr(char *str, struct sample_expr **out, struct proxy *px,
2613
                          char **err)
2614
0
{
2615
0
  struct sample_expr *expr;
2616
2617
0
  if (!str)
2618
0
    return 0;
2619
2620
0
  expr = _parse_srv_expr(str, &px->conf.args, px->conf.file, px->conf.line, err);
2621
0
  if (!expr)
2622
0
    return ERR_ALERT | ERR_FATAL;
2623
2624
0
  if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2625
0
    memprintf(err, "fetch method '%s' extracts information from '%s', "
2626
0
              "none of which is available here.",
2627
0
              str, sample_src_names(expr->fetch->use));
2628
0
    return ERR_ALERT | ERR_FATAL;
2629
0
  }
2630
2631
0
  px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2632
0
  release_sample_expr(*out);
2633
0
  *out = expr;
2634
2635
0
  return 0;
2636
0
}
2637
2638
static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, int err_code, char **err)
2639
0
{
2640
0
  char *msg = "error encountered while processing ";
2641
0
  char *quote = "'";
2642
0
  char *token = args[cur_arg];
2643
2644
0
  if (err && *err) {
2645
0
    indent_msg(err, 2);
2646
0
    msg = *err;
2647
0
    quote = "";
2648
0
    token = "";
2649
0
  }
2650
2651
0
  if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
2652
0
    ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
2653
0
  else
2654
0
    ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
2655
0
}
2656
2657
static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
2658
0
{
2659
0
  int range_sz;
2660
2661
0
  range_sz = src->conn_src.sport_range->size;
2662
0
  if (range_sz > 0) {
2663
0
    srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2664
0
    if (srv->conn_src.sport_range != NULL) {
2665
0
      int i;
2666
2667
0
      for (i = 0; i < range_sz; i++) {
2668
0
        srv->conn_src.sport_range->ports[i] =
2669
0
          src->conn_src.sport_range->ports[i];
2670
0
      }
2671
0
    }
2672
0
  }
2673
0
}
2674
2675
/*
2676
 * Copy <src> server connection source settings to <srv> server everything needed.
2677
 */
2678
static void srv_conn_src_cpy(struct server *srv, const struct server *src)
2679
0
{
2680
0
  srv->conn_src.opts = src->conn_src.opts;
2681
0
  srv->conn_src.source_addr = src->conn_src.source_addr;
2682
2683
  /* Source port range copy. */
2684
0
  if (src->conn_src.sport_range != NULL)
2685
0
    srv_conn_src_sport_range_cpy(srv, src);
2686
2687
0
#ifdef CONFIG_HAP_TRANSPARENT
2688
0
  if (src->conn_src.bind_hdr_name != NULL) {
2689
0
    srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2690
0
    srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2691
0
  }
2692
0
  srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2693
0
  srv->conn_src.tproxy_addr  = src->conn_src.tproxy_addr;
2694
0
#endif
2695
0
  if (src->conn_src.iface_name != NULL) {
2696
0
    srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2697
0
    srv->conn_src.iface_len = src->conn_src.iface_len;
2698
0
  }
2699
0
}
2700
2701
/*
2702
 * Copy <src> server SSL settings to <srv> server allocating
2703
 * everything needed.
2704
 */
2705
#if defined(USE_OPENSSL)
2706
static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
2707
{
2708
  /* <src> is the current proxy's default server and SSL is enabled */
2709
  BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2710
2711
  if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2712
    srv->flags |= SRV_F_DEFSRV_USE_SSL;
2713
2714
  if (src->ssl_ctx.ca_file != NULL)
2715
    srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2716
  if (src->ssl_ctx.crl_file != NULL)
2717
    srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
2718
  if (src->ssl_ctx.client_crt != NULL)
2719
    srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
2720
2721
  srv->ssl_ctx.verify = src->ssl_ctx.verify;
2722
2723
2724
  if (src->ssl_ctx.verify_host != NULL)
2725
    srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2726
  if (src->ssl_ctx.ciphers != NULL)
2727
    srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
2728
  if (src->ssl_ctx.options)
2729
    srv->ssl_ctx.options = src->ssl_ctx.options;
2730
  if (src->ssl_ctx.methods.flags)
2731
    srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2732
  if (src->ssl_ctx.methods.min)
2733
    srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2734
  if (src->ssl_ctx.methods.max)
2735
    srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2736
2737
  if (src->ssl_ctx.ciphersuites != NULL)
2738
    srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
2739
  if (src->sni_expr != NULL)
2740
    srv->sni_expr = strdup(src->sni_expr);
2741
2742
  if (src->ssl_ctx.alpn_str) {
2743
    srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2744
    if (srv->ssl_ctx.alpn_str) {
2745
      memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2746
          src->ssl_ctx.alpn_len);
2747
      srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2748
    }
2749
  }
2750
2751
  if (src->ssl_ctx.npn_str) {
2752
    srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2753
    if (srv->ssl_ctx.npn_str) {
2754
      memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2755
          src->ssl_ctx.npn_len);
2756
      srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2757
    }
2758
  }
2759
}
2760
2761
/* Activate ssl on server <s>.
2762
 * do nothing if there is no change to apply
2763
 *
2764
 * Must be called with the server lock held.
2765
 */
2766
void srv_set_ssl(struct server *s, int use_ssl)
2767
{
2768
  if (s->use_ssl == use_ssl)
2769
    return;
2770
2771
  s->use_ssl = use_ssl;
2772
  if (s->use_ssl)
2773
    s->xprt = xprt_get(XPRT_SSL);
2774
  else
2775
    s->xprt = xprt_get(XPRT_RAW);
2776
}
2777
2778
#endif /* USE_OPENSSL */
2779
2780
/*
2781
 * Prepare <srv> for hostname resolution.
2782
 * May be safely called with a default server as <src> argument (without hostname).
2783
 * Returns -1 in case of any allocation failure, 0 if not.
2784
 */
2785
int srv_prepare_for_resolution(struct server *srv, const char *hostname)
2786
0
{
2787
0
  char *hostname_dn;
2788
0
  int   hostname_len, hostname_dn_len;
2789
2790
0
  if (!hostname)
2791
0
    return 0;
2792
2793
0
  hostname_len    = strlen(hostname);
2794
0
  hostname_dn     = trash.area;
2795
0
  hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
2796
0
                                           hostname_dn, trash.size);
2797
0
  if (hostname_dn_len == -1)
2798
0
    goto err;
2799
2800
2801
0
  free(srv->hostname);
2802
0
  free(srv->hostname_dn);
2803
0
  srv->hostname        = strdup(hostname);
2804
0
  srv->hostname_dn     = strdup(hostname_dn);
2805
0
  srv->hostname_dn_len = hostname_dn_len;
2806
0
  if (!srv->hostname || !srv->hostname_dn)
2807
0
    goto err;
2808
2809
0
  return 0;
2810
2811
0
 err:
2812
0
  ha_free(&srv->hostname);
2813
0
  ha_free(&srv->hostname_dn);
2814
0
  return -1;
2815
0
}
2816
2817
/* Initialize default values for <srv>. Used both for dynamic servers and
2818
 * default servers. The latter are not initialized via new_server(), hence this
2819
 * function purpose. For static servers, srv_settings_cpy() is used instead
2820
 * reusing their default server instance.
2821
 */
2822
void srv_settings_init(struct server *srv)
2823
0
{
2824
0
  srv->check.inter = DEF_CHKINTR;
2825
0
  srv->check.fastinter = 0;
2826
0
  srv->check.downinter = 0;
2827
0
  srv->check.rise = DEF_RISETIME;
2828
0
  srv->check.fall = DEF_FALLTIME;
2829
0
  srv->check.port = 0;
2830
2831
0
  srv->agent.inter = DEF_CHKINTR;
2832
0
  srv->agent.fastinter = 0;
2833
0
  srv->agent.downinter = 0;
2834
0
  srv->agent.rise = DEF_AGENT_RISETIME;
2835
0
  srv->agent.fall = DEF_AGENT_FALLTIME;
2836
0
  srv->agent.port = 0;
2837
2838
0
  srv->init_state = SRV_INIT_STATE_UP;
2839
2840
0
  srv->maxqueue = 0;
2841
0
  srv->minconn = 0;
2842
0
  srv->maxconn = 0;
2843
2844
0
  srv->max_reuse = -1;
2845
0
  srv->max_idle_conns = -1;
2846
0
  srv->pool_purge_delay = 5000;
2847
2848
0
  srv->slowstart = 0;
2849
2850
0
  srv->onerror = DEF_HANA_ONERR;
2851
0
  srv->consecutive_errors_limit = DEF_HANA_ERRLIMIT;
2852
2853
0
  srv->uweight = srv->iweight = 1;
2854
2855
0
  LIST_INIT(&srv->pp_tlvs);
2856
0
}
2857
2858
/*
2859
 * Copy <src> server settings to <srv> server allocating
2860
 * everything needed.
2861
 * This function is not supposed to be called at any time, but only
2862
 * during server settings parsing or during server allocations from
2863
 * a server template, and just after having calloc()'ed a new server.
2864
 * So, <src> may only be a default server (when parsing server settings)
2865
 * or a server template (during server allocations from a server template).
2866
 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2867
 * 0 if not).
2868
 */
2869
void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
2870
0
{
2871
0
  struct srv_pp_tlv_list *srv_tlv = NULL, *new_srv_tlv = NULL;
2872
2873
  /* Connection source settings copy */
2874
0
  srv_conn_src_cpy(srv, src);
2875
2876
0
  if (srv_tmpl) {
2877
0
    srv->addr = src->addr;
2878
0
    srv->addr_type = src->addr_type;
2879
0
    srv->svc_port = src->svc_port;
2880
0
  }
2881
2882
0
  srv->pp_opts = src->pp_opts;
2883
0
  if (src->rdr_pfx != NULL) {
2884
0
    srv->rdr_pfx = strdup(src->rdr_pfx);
2885
0
    srv->rdr_len = src->rdr_len;
2886
0
  }
2887
0
  if (src->cookie != NULL) {
2888
0
    srv->cookie = strdup(src->cookie);
2889
0
    srv->cklen  = src->cklen;
2890
0
  }
2891
0
  srv->use_ssl                  = src->use_ssl;
2892
0
  srv->check.addr               = src->check.addr;
2893
0
  srv->agent.addr               = src->agent.addr;
2894
0
  srv->check.use_ssl            = src->check.use_ssl;
2895
0
  srv->check.port               = src->check.port;
2896
0
  srv->check.sni                = src->check.sni;
2897
0
  srv->check.alpn_str           = src->check.alpn_str;
2898
0
  srv->check.alpn_len           = src->check.alpn_len;
2899
0
  if (!(srv->flags & SRV_F_RHTTP))
2900
0
    srv->check.reuse_pool = src->check.reuse_pool;
2901
0
  if (src->check.pool_conn_name)
2902
0
    srv->check.pool_conn_name = strdup(src->check.pool_conn_name);
2903
  /* Note: 'flags' field has potentially been already initialized. */
2904
0
  srv->flags                   |= src->flags;
2905
0
  srv->do_check                 = src->do_check;
2906
0
  srv->do_agent                 = src->do_agent;
2907
0
  srv->check.inter              = src->check.inter;
2908
0
  srv->check.fastinter          = src->check.fastinter;
2909
0
  srv->check.downinter          = src->check.downinter;
2910
0
  srv->agent.use_ssl            = src->agent.use_ssl;
2911
0
  srv->agent.port               = src->agent.port;
2912
2913
0
  if (src->agent.tcpcheck_rules) {
2914
0
    srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2915
0
    if (srv->agent.tcpcheck_rules) {
2916
0
      srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2917
0
      srv->agent.tcpcheck_rules->list  = src->agent.tcpcheck_rules->list;
2918
0
      LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2919
0
      dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2920
0
            &src->agent.tcpcheck_rules->preset_vars);
2921
0
    }
2922
0
  }
2923
2924
0
  srv->agent.inter              = src->agent.inter;
2925
0
  srv->agent.fastinter          = src->agent.fastinter;
2926
0
  srv->agent.downinter          = src->agent.downinter;
2927
0
  srv->maxqueue                 = src->maxqueue;
2928
0
  srv->ws                       = src->ws;
2929
0
  srv->minconn                  = src->minconn;
2930
0
  srv->maxconn                  = src->maxconn;
2931
0
  srv->slowstart                = src->slowstart;
2932
0
  srv->hash_key                 = src->hash_key;
2933
0
  srv->observe                  = src->observe;
2934
0
  srv->onerror                  = src->onerror;
2935
0
  srv->onmarkeddown             = src->onmarkeddown;
2936
0
  srv->onmarkedup               = src->onmarkedup;
2937
0
  if (src->trackit != NULL)
2938
0
    srv->trackit = strdup(src->trackit);
2939
0
  srv->consecutive_errors_limit = src->consecutive_errors_limit;
2940
0
  srv->uweight = srv->iweight   = src->iweight;
2941
2942
0
  srv->check.send_proxy         = src->check.send_proxy;
2943
  /* health: up, but will fall down at first failure */
2944
0
  srv->check.rise = srv->check.health = src->check.rise;
2945
0
  srv->check.fall               = src->check.fall;
2946
2947
  /* Here we check if 'disabled' is the default server state */
2948
0
  if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2949
0
    srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2950
0
    srv->next_state        = SRV_ST_STOPPED;
2951
0
    srv->check.state |= CHK_ST_PAUSED;
2952
0
    srv->check.health = 0;
2953
0
  }
2954
2955
  /* health: up but will fall down at first failure */
2956
0
  srv->agent.rise = srv->agent.health = src->agent.rise;
2957
0
  srv->agent.fall               = src->agent.fall;
2958
2959
0
  if (src->resolvers_id != NULL)
2960
0
    srv->resolvers_id = strdup(src->resolvers_id);
2961
0
  srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2962
0
  srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2963
0
  srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2964
0
  if (srv->resolv_opts.family_prio == AF_UNSPEC)
2965
0
    srv->resolv_opts.family_prio = AF_INET6;
2966
0
  memcpy(srv->resolv_opts.pref_net,
2967
0
         src->resolv_opts.pref_net,
2968
0
         sizeof srv->resolv_opts.pref_net);
2969
0
  srv->resolv_opts.pref_net_nb  = src->resolv_opts.pref_net_nb;
2970
2971
0
  srv->init_addr_methods        = src->init_addr_methods;
2972
0
  srv->init_addr                = src->init_addr;
2973
2974
0
  srv->init_state               = src->init_state;
2975
#if defined(USE_OPENSSL)
2976
  srv_ssl_settings_cpy(srv, src);
2977
#endif
2978
0
#ifdef TCP_USER_TIMEOUT
2979
0
  srv->tcp_ut = src->tcp_ut;
2980
0
#endif
2981
0
  srv->mux_proto = src->mux_proto;
2982
0
  if (srv->pool_conn_name)
2983
0
    srv->pool_conn_name = strdup(srv->pool_conn_name);
2984
0
  srv->pool_purge_delay = src->pool_purge_delay;
2985
0
  srv->low_idle_conns = src->low_idle_conns;
2986
0
  srv->max_idle_conns = src->max_idle_conns;
2987
0
  srv->max_reuse = src->max_reuse;
2988
2989
0
  if (srv_tmpl)
2990
0
    srv->srvrq = src->srvrq;
2991
2992
0
  srv->netns                    = src->netns;
2993
0
  srv->check.via_socks4         = src->check.via_socks4;
2994
0
  srv->socks4_addr              = src->socks4_addr;
2995
0
  srv->log_bufsize              = src->log_bufsize;
2996
2997
0
  LIST_INIT(&srv->pp_tlvs);
2998
2999
0
  list_for_each_entry(srv_tlv, &src->pp_tlvs, list) {
3000
0
    new_srv_tlv = malloc(sizeof(*new_srv_tlv));
3001
0
    if (unlikely(!new_srv_tlv)) {
3002
0
      break;
3003
0
    }
3004
0
    new_srv_tlv->fmt_string = strdup(srv_tlv->fmt_string);
3005
0
    if (unlikely(!new_srv_tlv->fmt_string)) {
3006
0
      free(new_srv_tlv);
3007
0
      break;
3008
0
    }
3009
0
    new_srv_tlv->type = srv_tlv->type;
3010
0
    lf_expr_init(&new_srv_tlv->fmt);
3011
0
    if (srv_tmpl) {
3012
0
      if (new_srv_tlv->fmt_string && unlikely(!parse_logformat_string(new_srv_tlv->fmt_string,
3013
0
          srv->proxy, &new_srv_tlv->fmt, 0, SMP_VAL_BE_SRV_CON, NULL))) {
3014
0
        free(new_srv_tlv->fmt_string);
3015
0
        free(new_srv_tlv);
3016
0
        break;
3017
0
      }
3018
0
    }
3019
0
    LIST_APPEND(&srv->pp_tlvs, &new_srv_tlv->list);
3020
0
  }
3021
0
}
3022
3023
/* allocate a server, attachs it to the global servers_list
3024
 * and adds it to <proxy> server list. Before deleting the server with
3025
 * srv_drop(), srv_detach() must be called to remove it from the parent
3026
 * proxy list
3027
 *
3028
 * Returns the server on success, otherwise NULL.
3029
 */
3030
struct server *new_server(struct proxy *proxy)
3031
0
{
3032
0
  struct server *srv;
3033
3034
0
  srv = calloc(1, sizeof *srv);
3035
0
  if (!srv)
3036
0
    return NULL;
3037
3038
0
  srv_take(srv);
3039
3040
0
  srv->obj_type = OBJ_TYPE_SERVER;
3041
0
  srv->proxy = proxy;
3042
0
  MT_LIST_APPEND(&servers_list, &srv->global_list);
3043
0
  LIST_INIT(&srv->srv_rec_item);
3044
0
  LIST_INIT(&srv->ip_rec_item);
3045
0
  LIST_INIT(&srv->pp_tlvs);
3046
0
  event_hdl_sub_list_init(&srv->e_subs);
3047
0
  srv->rid = 0; /* rid defaults to 0 */
3048
3049
0
  srv->next_state = SRV_ST_RUNNING; /* early server setup */
3050
3051
0
  srv->check.obj_type = OBJ_TYPE_CHECK;
3052
0
  srv->check.status = HCHK_STATUS_INI;
3053
0
  srv->check.server = srv;
3054
0
  srv->check.proxy = proxy;
3055
0
  srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
3056
3057
0
  srv->agent.obj_type = OBJ_TYPE_CHECK;
3058
0
  srv->agent.status = HCHK_STATUS_INI;
3059
0
  srv->agent.server = srv;
3060
0
  srv->agent.proxy = proxy;
3061
0
  srv->xprt  = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
3062
3063
0
  MT_LIST_INIT(&srv->sess_conns);
3064
3065
0
  guid_init(&srv->guid);
3066
0
  MT_LIST_INIT(&srv->watcher_list);
3067
3068
0
  srv->extra_counters = NULL;
3069
#ifdef USE_OPENSSL
3070
  HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
3071
#endif
3072
3073
  // add server to proxy list:
3074
  /* TODO use a double-linked list for px->srv */
3075
0
  if (!(proxy->flags & PR_FL_CHECKED) || !proxy->srv) {
3076
    /* they are linked backwards first during parsing
3077
     * This will be restablished after parsing.
3078
     */
3079
0
    srv->next = proxy->srv;
3080
0
    proxy->srv = srv;
3081
0
  }
3082
0
  else {
3083
0
    struct server *sv = proxy->srv;
3084
3085
    // runtime, add the server at the end of the list
3086
0
    while (sv && sv->next)
3087
0
      sv = sv->next;
3088
0
    sv->next = srv;
3089
0
  }
3090
3091
  /* please don't put default server settings here, they are set in
3092
   * proxy_preset_defaults().
3093
   */
3094
0
  return srv;
3095
0
}
3096
3097
/* Increment the server refcount. */
3098
void srv_take(struct server *srv)
3099
0
{
3100
0
  HA_ATOMIC_INC(&srv->refcount);
3101
0
}
3102
3103
/* deallocate common server parameters (may be used by default-servers) */
3104
void srv_free_params(struct server *srv)
3105
0
{
3106
0
  struct srv_pp_tlv_list *srv_tlv = NULL;
3107
3108
0
  free(srv->cookie);
3109
0
  free(srv->rdr_pfx);
3110
0
  free(srv->hostname);
3111
0
  free(srv->hostname_dn);
3112
0
  free((char*)srv->conf.file);
3113
0
  free(srv->per_thr);
3114
0
  free(srv->per_tgrp);
3115
0
  free(srv->curr_idle_thr);
3116
0
  free(srv->pool_conn_name);
3117
0
  release_sample_expr(srv->pool_conn_name_expr);
3118
0
  free(srv->resolvers_id);
3119
0
  free(srv->addr_node.key);
3120
0
  free(srv->lb_nodes);
3121
0
  counters_be_shared_drop(srv->counters.shared);
3122
0
  if (srv->log_target) {
3123
0
    deinit_log_target(srv->log_target);
3124
0
    free(srv->log_target);
3125
0
  }
3126
0
  free(srv->tmpl_info.prefix);
3127
3128
0
  if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
3129
0
    xprt_get(XPRT_SSL)->destroy_srv(srv);
3130
3131
0
  while (!LIST_ISEMPTY(&srv->pp_tlvs)) {
3132
0
    srv_tlv = LIST_ELEM(srv->pp_tlvs.n, struct srv_pp_tlv_list *, list);
3133
0
    LIST_DEL_INIT(&srv_tlv->list);
3134
0
    lf_expr_deinit(&srv_tlv->fmt);
3135
0
    ha_free(&srv_tlv->fmt_string);
3136
0
    ha_free(&srv_tlv);
3137
0
  }
3138
0
}
3139
3140
/* Deallocate a server <srv> and its member. <srv> must be allocated. For
3141
 * dynamic servers, its refcount is decremented first. The free operations are
3142
 * conducted only if the refcount is nul.
3143
 *
3144
 * A general rule is to assume that proxy may already be freed, so cleanup checks
3145
 * must not depend on the proxy
3146
 *
3147
 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
3148
 * when calling srv_drop on the list of servers.
3149
 */
3150
struct server *srv_drop(struct server *srv)
3151
0
{
3152
0
  struct server *next = NULL;
3153
3154
0
  if (!srv)
3155
0
    goto end;
3156
3157
0
  next = srv->next;
3158
3159
  /* For dynamic servers, decrement the reference counter. Only free the
3160
   * server when reaching zero.
3161
   */
3162
0
  if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
3163
0
    goto end;
3164
3165
  /* This BUG_ON() is invalid for now as server released on deinit will
3166
   * trigger it as they are not properly removed from their tree.
3167
   */
3168
  //BUG_ON(srv->addr_node.node.leaf_p ||
3169
  //       srv->idle_node.node.leaf_p ||
3170
  //       srv->conf.id.node.leaf_p ||
3171
  //       srv->conf.name.node.leaf_p);
3172
3173
0
  guid_remove(&srv->guid);
3174
3175
0
  if (srv->requeue_tasklet)
3176
0
    tasklet_kill(srv->requeue_tasklet);
3177
0
  task_destroy(srv->warmup);
3178
0
  task_destroy(srv->srvrq_check);
3179
3180
0
  free(srv->id);
3181
0
  srv_free_params(srv);
3182
3183
0
  HA_SPIN_DESTROY(&srv->lock);
3184
3185
0
  MT_LIST_DELETE(&srv->global_list);
3186
0
  event_hdl_sub_list_destroy(&srv->e_subs);
3187
3188
0
  EXTRA_COUNTERS_FREE(srv->extra_counters);
3189
3190
0
  ha_free(&srv);
3191
3192
0
 end:
3193
0
  return next;
3194
0
}
3195
3196
/* Remove a server <srv> from a tracking list if <srv> is tracking another
3197
 * server. No special care is taken if <srv> is tracked itself by another one :
3198
 * this situation should be avoided by the caller.
3199
 *
3200
 * Not thread-safe.
3201
 */
3202
static void release_server_track(struct server *srv)
3203
0
{
3204
0
  struct server *strack = srv->track;
3205
0
  struct server **base;
3206
3207
0
  if (!strack)
3208
0
    return;
3209
3210
0
  for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
3211
0
    if (*base == srv) {
3212
0
      *base = srv->tracknext;
3213
0
      return;
3214
0
    }
3215
0
  }
3216
3217
  /* srv not found on the tracking list, this should never happen */
3218
0
  BUG_ON(!*base);
3219
0
}
3220
3221
/*
3222
 * Parse as much as possible such a range string argument: low[-high]
3223
 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
3224
 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
3225
 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
3226
 * Returns 0 if succeeded, -1 if not.
3227
 */
3228
static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
3229
                                 int *nb_low, int *nb_high)
3230
0
{
3231
0
  char *nb_high_arg;
3232
3233
0
  *nb_high = 0;
3234
0
  chunk_printf(&trash, "%s", arg);
3235
0
  *nb_low = atoi(trash.area);
3236
3237
0
  if ((nb_high_arg = strchr(trash.area, '-'))) {
3238
0
    *nb_high_arg++ = '\0';
3239
0
    *nb_high = atoi(nb_high_arg);
3240
0
  }
3241
0
  else {
3242
0
    *nb_high += *nb_low;
3243
0
    *nb_low = 1;
3244
0
  }
3245
3246
0
  if (*nb_low < 0 || *nb_high < *nb_low)
3247
0
    return -1;
3248
3249
0
  return 0;
3250
0
}
3251
3252
/* Parse as much as possible such a range string argument: low[-high]
3253
 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
3254
 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
3255
 *
3256
 * This function is first intended to be used through parse_server to
3257
 * initialize a new server on startup.
3258
 *
3259
 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
3260
 * Returns 0 if succeeded, -1 if not.
3261
 */
3262
static inline void _srv_parse_set_id_from_prefix(struct server *srv,
3263
                                                 const char *prefix, int nb)
3264
0
{
3265
0
  chunk_printf(&trash, "%s%d", prefix, nb);
3266
0
  free(srv->id);
3267
0
  srv->id = strdup(trash.area);
3268
0
}
3269
3270
/* Initialize as much as possible servers from <srv> server template.
3271
 * Note that a server template is a special server with
3272
 * a few different parameters than a server which has
3273
 * been parsed mostly the same way as a server.
3274
 *
3275
 * This function is first intended to be used through parse_server to
3276
 * initialize a new server on startup.
3277
 *
3278
 * Returns the number of servers successfully allocated,
3279
 * 'srv' template included.
3280
 */
3281
static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
3282
0
{
3283
0
  int i;
3284
0
  struct server *newsrv;
3285
3286
  /* Set the first server's ID. */
3287
0
  _srv_parse_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
3288
0
  srv->conf.name.key = srv->id;
3289
0
  ebis_insert(&curproxy->conf.used_server_name, &srv->conf.name);
3290
3291
  /* then create other servers from this one */
3292
0
  for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
3293
0
    newsrv = new_server(px);
3294
0
    if (!newsrv)
3295
0
      goto err;
3296
3297
0
    newsrv->conf.file = strdup(srv->conf.file);
3298
0
    newsrv->conf.line = srv->conf.line;
3299
3300
0
    srv_settings_cpy(newsrv, srv, 1);
3301
0
    srv_prepare_for_resolution(newsrv, srv->hostname);
3302
3303
    /* Use sni as fallback if pool_conn_name isn't set */
3304
0
    if (!newsrv->pool_conn_name && newsrv->sni_expr) {
3305
0
      newsrv->pool_conn_name = strdup(newsrv->sni_expr);
3306
0
      if (!newsrv->pool_conn_name)
3307
0
        goto err;
3308
0
    }
3309
3310
0
    if (newsrv->pool_conn_name) {
3311
0
      newsrv->pool_conn_name_expr = _parse_srv_expr(srv->pool_conn_name, &px->conf.args, NULL, 0, NULL);
3312
0
      if (!newsrv->pool_conn_name_expr)
3313
0
        goto err;
3314
0
    }
3315
3316
0
    if (newsrv->sni_expr) {
3317
0
      newsrv->ssl_ctx.sni = _parse_srv_expr(srv->sni_expr, &px->conf.args, NULL, 0, NULL);
3318
0
      if (!newsrv->ssl_ctx.sni)
3319
0
        goto err;
3320
0
    }
3321
3322
    /* append to list of servers available to receive an hostname */
3323
0
    if (newsrv->srvrq)
3324
0
      LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
3325
3326
    /* Set this new server ID. */
3327
0
    _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
3328
3329
0
    newsrv->conf.name.key = newsrv->id;
3330
0
    ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
3331
0
  }
3332
3333
0
  return i - srv->tmpl_info.nb_low;
3334
3335
0
 err:
3336
0
  if (newsrv)  {
3337
0
    release_sample_expr(newsrv->ssl_ctx.sni);
3338
0
    free_check(&newsrv->agent);
3339
0
    free_check(&newsrv->check);
3340
0
    MT_LIST_DELETE(&newsrv->global_list);
3341
0
  }
3342
0
  free(newsrv);
3343
0
  return i - srv->tmpl_info.nb_low;
3344
0
}
3345
3346
/* Ensure server config will work with effective proxy mode
3347
 *
3348
 * This function is expected to be called after _srv_parse_init() initialization
3349
 * but only when the effective server's proxy mode is known, which is not always
3350
 * the case during parsing time, in which case the function will be called during
3351
 * postparsing thanks to the srv_init() below.
3352
 *
3353
 * Returns ERR_NONE on success else a combination or ERR_CODE.
3354
 */
3355
static int _srv_check_proxy_mode(struct server *srv, char postparse)
3356
0
{
3357
0
  int err_code = ERR_NONE;
3358
3359
0
  if (postparse && !(srv->proxy->cap & PR_CAP_LB))
3360
0
    return ERR_NONE; /* nothing to do, the check was already performed during parsing */
3361
3362
0
  if (srv->conf.file)
3363
0
    set_usermsgs_ctx(srv->conf.file, srv->conf.line, NULL);
3364
3365
0
  if (!srv->proxy) {
3366
    /* proxy mode not known, cannot perform checks (ie: defaults section) */
3367
0
    goto out;
3368
0
  }
3369
3370
0
  if (srv->proxy->mode == PR_MODE_SYSLOG) {
3371
    /* log backend server (belongs to proxy with mode log enabled):
3372
     * perform some compatibility checks
3373
     */
3374
3375
    /* supported address family types are:
3376
     *   - ipv4
3377
     *   - ipv6
3378
     * (UNSPEC is supported because it means it will be resolved later)
3379
     */
3380
0
    if (srv->addr.ss_family != AF_UNSPEC &&
3381
0
        srv->addr.ss_family != AF_INET && srv->addr.ss_family != AF_INET6) {
3382
0
      ha_alert("log server address family not supported for log backend server.\n");
3383
0
      err_code |= ERR_ALERT | ERR_FATAL;
3384
0
      goto out;
3385
0
    }
3386
3387
    /* only @tcp or @udp address forms (or equivalent) are supported */
3388
0
    if (!(srv->addr_type.xprt_type == PROTO_TYPE_DGRAM && srv->addr_type.proto_type == PROTO_TYPE_DGRAM) &&
3389
0
        !(srv->addr_type.xprt_type == PROTO_TYPE_STREAM && srv->addr_type.proto_type == PROTO_TYPE_STREAM)) {
3390
0
      ha_alert("log server address type not supported for log backend server.\n");
3391
0
      err_code |= ERR_ALERT | ERR_FATAL;
3392
0
    }
3393
0
  }
3394
0
  else {
3395
    /* for all other proxy modes: only TCP expected as srv's transport type for now */
3396
0
    if (srv->addr_type.xprt_type != PROTO_TYPE_STREAM) {
3397
0
      ha_alert("unsupported transport for server address in '%s' backend.\n", proxy_mode_str(srv->proxy->mode));
3398
0
      err_code |= ERR_ALERT | ERR_FATAL;
3399
0
    }
3400
0
  }
3401
0
 out:
3402
0
  if (srv->conf.file)
3403
0
    reset_usermsgs_ctx();
3404
3405
0
  return err_code;
3406
0
}
3407
/* Finish initializing the server after parsing
3408
 *
3409
 * We must be careful that checks / postinits performed within this function
3410
 * don't depend or conflict with other postcheck functions that are registered
3411
 * using REGISTER_POST_SERVER_CHECK() hook.
3412
 *
3413
 * Returns ERR_NONE on success else a combination or ERR_CODE.
3414
 */
3415
static int init_srv_requeue(struct server *srv);
3416
static int init_srv_slowstart(struct server *srv);
3417
static int srv_init_per_thr(struct server *srv);
3418
int srv_init(struct server *srv)
3419
0
{
3420
0
  int err_code = ERR_NONE;
3421
3422
0
  if (srv->flags & SRV_F_CHECKED)
3423
0
    return ERR_NONE; // nothing to do
3424
3425
0
  err_code |= _srv_check_proxy_mode(srv, 1);
3426
3427
0
  if (err_code & ERR_CODE)
3428
0
    goto out;
3429
3430
0
  srv->counters.shared = counters_be_shared_get(&srv->guid);
3431
0
  if (!srv->counters.shared) {
3432
0
    ha_alert("memory error while setting up shared counters for %s/%s server\n", srv->proxy->id, srv->id);
3433
0
    err_code |= ERR_ALERT | ERR_FATAL;
3434
0
    goto out;
3435
0
  }
3436
3437
0
  if (srv->flags & SRV_F_DYNAMIC) {
3438
    /* A dynamic server is disabled on startup */
3439
0
    srv->next_admin = SRV_ADMF_FMAINT;
3440
0
    srv->next_state = SRV_ST_STOPPED;
3441
0
    server_recalc_eweight(srv, 0); // relies on srv counters
3442
0
    srv_lb_commit_status(srv);
3443
0
  }
3444
3445
0
  err_code |= init_srv_requeue(srv);
3446
3447
0
  if (err_code & ERR_CODE)
3448
0
    goto out;
3449
3450
0
  err_code |= init_srv_slowstart(srv);
3451
3452
0
  if (err_code & ERR_CODE)
3453
0
    goto out;
3454
3455
0
  if (srv_init_per_thr(srv) == -1) {
3456
0
    ha_alert("error during per-thread init for %s/%s server\n", srv->proxy->id, srv->id);
3457
0
    err_code |= ERR_ALERT | ERR_FATAL;
3458
0
    goto out;
3459
0
  }
3460
3461
  /* initialize idle conns lists */
3462
0
  if (srv->max_idle_conns != 0) {
3463
0
    srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
3464
0
    if (!srv->curr_idle_thr) {
3465
0
      ha_alert("memory error during idle conn list init for %s/%s server\n",
3466
0
               srv->proxy->id, srv->id);
3467
0
      err_code |= ERR_ALERT | ERR_FATAL;
3468
0
      goto out;
3469
0
    }
3470
0
  }
3471
3472
0
 out:
3473
0
  if (!(err_code & ERR_CODE))
3474
0
    srv->flags |= SRV_F_CHECKED;
3475
0
  return err_code;
3476
0
}
3477
REGISTER_POST_SERVER_CHECK(srv_init);
3478
3479
/* Allocate a new server pointed by <srv> and try to parse the first arguments
3480
 * in <args> as an address for a server or an address-range for a template or
3481
 * nothing for a default-server. <cur_arg> is incremented to the next argument.
3482
 *
3483
 * This function is first intended to be used through parse_server to
3484
 * initialize a new server on startup.
3485
 *
3486
 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
3487
 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
3488
 * <srv> will be set to NULL.
3489
 */
3490
static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
3491
                           struct proxy *curproxy,
3492
                           int parse_flags)
3493
0
{
3494
0
  struct server *newsrv = NULL;
3495
0
  const char *err = NULL;
3496
0
  int err_code = 0;
3497
0
  char *fqdn = NULL;
3498
0
  int alt_proto = 0;
3499
0
  int tmpl_range_low = 0, tmpl_range_high = 0;
3500
0
  char *errmsg = NULL;
3501
3502
0
  *srv = NULL;
3503
3504
  /* There is no mandatory first arguments for default server. */
3505
0
  if (parse_flags & SRV_PARSE_PARSE_ADDR) {
3506
0
    if (parse_flags & SRV_PARSE_TEMPLATE) {
3507
0
      if (!*args[3]) {
3508
        /* 'server-template' line number of argument check. */
3509
0
        ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
3510
0
                 args[0]);
3511
0
        err_code |= ERR_ALERT | ERR_FATAL;
3512
0
        goto out;
3513
0
      }
3514
3515
0
      err = invalid_prefix_char(args[1]);
3516
0
    }
3517
0
    else {
3518
0
      if (!*args[2]) {
3519
        /* 'server' line number of argument check. */
3520
0
        ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
3521
0
                 args[0]);
3522
0
        err_code |= ERR_ALERT | ERR_FATAL;
3523
0
        goto out;
3524
0
      }
3525
3526
0
      err = invalid_char(args[1]);
3527
0
    }
3528
3529
0
    if (err) {
3530
0
      ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
3531
0
               *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
3532
0
      err_code |= ERR_ALERT | ERR_FATAL;
3533
0
      goto out;
3534
0
    }
3535
0
  }
3536
3537
0
  *cur_arg = 2;
3538
0
  if (parse_flags & SRV_PARSE_TEMPLATE) {
3539
    /* Parse server-template <nb | range> arg. */
3540
0
    if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
3541
0
      ha_alert("Wrong %s number or range arg '%s'.\n",
3542
0
               args[0], args[*cur_arg]);
3543
0
      err_code |= ERR_ALERT | ERR_FATAL;
3544
0
      goto out;
3545
0
    }
3546
0
    (*cur_arg)++;
3547
0
  }
3548
3549
0
  if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
3550
0
    struct sockaddr_storage *sk;
3551
0
    int port1, port2, port;
3552
3553
0
    *srv = newsrv = new_server(curproxy);
3554
0
    if (!newsrv) {
3555
0
      ha_alert("out of memory.\n");
3556
0
      err_code |= ERR_ALERT | ERR_ABORT;
3557
0
      goto out;
3558
0
    }
3559
0
    register_parsing_obj(&newsrv->obj_type);
3560
3561
0
    if (parse_flags & SRV_PARSE_TEMPLATE) {
3562
0
      newsrv->tmpl_info.nb_low = tmpl_range_low;
3563
0
      newsrv->tmpl_info.nb_high = tmpl_range_high;
3564
0
    }
3565
3566
0
    if (parse_flags & SRV_PARSE_DYNAMIC)
3567
0
      newsrv->flags |= SRV_F_DYNAMIC;
3568
3569
    /* Note: for a server template, its id is its prefix.
3570
     * This is a temporary id which will be used for server allocations to come
3571
     * after parsing.
3572
     */
3573
0
    if (!(parse_flags & SRV_PARSE_TEMPLATE))
3574
0
      newsrv->id = strdup(args[1]);
3575
0
    else
3576
0
      newsrv->tmpl_info.prefix = strdup(args[1]);
3577
3578
    /* several ways to check the port component :
3579
     *  - IP    => port=+0, relative (IPv4 only)
3580
     *  - IP:   => port=+0, relative
3581
     *  - IP:N  => port=N, absolute
3582
     *  - IP:+N => port=+N, relative
3583
     *  - IP:-N => port=-N, relative
3584
     */
3585
0
    if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
3586
0
      goto skip_addr;
3587
3588
0
    sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL, &newsrv->addr_type,
3589
0
                      &errmsg, NULL, &fqdn, &alt_proto,
3590
0
                      (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK |
3591
0
          (parse_flags & SRV_PARSE_IN_PEER_SECTION ? PA_O_PORT_MAND : PA_O_PORT_OFS) |
3592
0
          PA_O_STREAM | PA_O_DGRAM | PA_O_XPRT);
3593
3594
0
    if (!sk) {
3595
0
      ha_alert("%s\n", errmsg);
3596
0
      err_code |= ERR_ALERT | ERR_FATAL;
3597
0
      ha_free(&errmsg);
3598
0
      goto out;
3599
0
    }
3600
3601
#ifdef USE_QUIC
3602
    if (srv_is_quic(newsrv)) {
3603
      if (!experimental_directives_allowed) {
3604
        ha_alert("QUIC is experimental for server '%s',"
3605
                 " must be allowed via a global 'expose-experimental-directives'\n",
3606
                 newsrv->id);
3607
        err_code |= ERR_ALERT | ERR_FATAL;
3608
        goto out;
3609
      }
3610
3611
      newsrv->xprt = xprt_get(XPRT_QUIC);
3612
      quic_transport_params_init(&newsrv->quic_params, 0);
3613
    }
3614
#endif
3615
3616
0
    if (!port1 || !port2) {
3617
0
      if (sk->ss_family != AF_CUST_RHTTP_SRV) {
3618
        /* no port specified, +offset, -offset */
3619
0
        newsrv->flags |= SRV_F_MAPPORTS;
3620
0
      }
3621
0
      else {
3622
0
        newsrv->flags |= SRV_F_RHTTP;
3623
        /* Automatically activate check-reuse-pool for rhttp@ servers. */
3624
0
        newsrv->check.reuse_pool = 1;
3625
0
      }
3626
0
    }
3627
3628
    /* save hostname and create associated name resolution */
3629
0
    if (fqdn) {
3630
0
      if (fqdn[0] == '_') { /* SRV record */
3631
        /* Check if a SRV request already exists, and if not, create it */
3632
0
        if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
3633
0
          newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
3634
0
        if (newsrv->srvrq == NULL) {
3635
0
          err_code |= ERR_ALERT | ERR_FATAL;
3636
0
          goto out;
3637
0
        }
3638
0
        LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
3639
0
      }
3640
0
      else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
3641
0
        ha_alert("Can't create DNS resolution for server '%s'\n",
3642
0
                 newsrv->id);
3643
0
        err_code |= ERR_ALERT | ERR_FATAL;
3644
0
        goto out;
3645
0
      }
3646
0
    }
3647
3648
0
    newsrv->addr = *sk;
3649
0
    newsrv->svc_port = port;
3650
0
    newsrv->alt_proto = alt_proto;
3651
    /*
3652
     * we don't need to lock the server here, because
3653
     * we are in the process of initializing.
3654
     *
3655
     * Note that the server is not attached into the proxy tree if
3656
     * this is a dynamic server.
3657
     */
3658
0
    srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
3659
3660
0
    if (!newsrv->srvrq && !newsrv->hostname &&
3661
0
        !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
3662
0
      ha_alert("Unknown protocol family %d '%s'\n",
3663
0
               newsrv->addr.ss_family, args[*cur_arg]);
3664
0
      err_code |= ERR_ALERT | ERR_FATAL;
3665
0
      goto out;
3666
0
    }
3667
3668
0
    (*cur_arg)++;
3669
0
 skip_addr:
3670
0
    if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
3671
      /* Copy default server settings to new server */
3672
0
      srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
3673
0
    } else
3674
0
      srv_settings_init(newsrv);
3675
0
    HA_SPIN_INIT(&newsrv->lock);
3676
0
  }
3677
0
  else {
3678
0
    *srv = newsrv = &curproxy->defsrv;
3679
0
    *cur_arg = 1;
3680
0
  }
3681
3682
0
  free(fqdn);
3683
0
  if (!(curproxy->cap & PR_CAP_LB)) {
3684
    /* No need to wait for effective proxy mode, it is already known:
3685
     * Only general purpose user-declared proxies ("listen", "frontend", "backend")
3686
     * offer the possibility to configure the mode of the proxy. Hopefully for us,
3687
     * they have the PR_CAP_LB set.
3688
     */
3689
0
    return _srv_check_proxy_mode(newsrv, 0);
3690
0
  }
3691
0
  return 0;
3692
3693
0
out:
3694
0
  free(fqdn);
3695
0
  return err_code;
3696
0
}
3697
3698
/* Parse the server keyword in <args>.
3699
 * <cur_arg> is incremented beyond the keyword optional value. Note that this
3700
 * might not be the case if an error is reported.
3701
 *
3702
 * This function is first intended to be used through parse_server to
3703
 * initialize a new server on startup.
3704
 *
3705
 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
3706
 * interrupted.
3707
 */
3708
static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
3709
                         struct proxy *curproxy,
3710
                         int parse_flags)
3711
0
{
3712
0
  int err_code = 0;
3713
0
  struct srv_kw *kw;
3714
0
  const char *best;
3715
0
  char *errmsg = NULL;
3716
3717
0
  kw = srv_find_kw(args[*cur_arg]);
3718
0
  if (!kw) {
3719
0
    best = srv_find_best_kw(args[*cur_arg]);
3720
0
    if (best)
3721
0
      ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
3722
0
               args[*cur_arg], best,
3723
0
         (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
3724
0
         " Hint: no address was expected for this server.");
3725
0
    else
3726
0
      ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
3727
0
         (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
3728
0
         " Hint: no address was expected for this server.");
3729
3730
0
    return ERR_ALERT | ERR_FATAL;
3731
0
  }
3732
3733
0
  if (!kw->parse) {
3734
0
    ha_alert("'%s' option is not implemented in this version (check build options)\n",
3735
0
             args[*cur_arg]);
3736
0
    err_code = ERR_ALERT | ERR_FATAL;
3737
0
    goto out;
3738
0
  }
3739
3740
0
  if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
3741
0
    ha_alert("'%s' option is not accepted in default-server sections\n",
3742
0
             args[*cur_arg]);
3743
0
    err_code = ERR_ALERT;
3744
0
    goto out;
3745
0
  }
3746
0
  else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
3747
0
    ha_alert("'%s' option is not accepted for dynamic server\n",
3748
0
             args[*cur_arg]);
3749
0
    err_code |= ERR_ALERT;
3750
0
    goto out;
3751
0
  }
3752
3753
0
  err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
3754
0
  if (err_code) {
3755
0
    display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
3756
0
    free(errmsg);
3757
0
  }
3758
3759
0
out:
3760
0
  if (kw->skip != -1)
3761
0
    *cur_arg += 1 + kw->skip;
3762
3763
0
  return err_code;
3764
0
}
3765
3766
/* Server initializations finalization.
3767
 * Initialize health check, agent check, SNI expression and outgoing TLVs if enabled.
3768
 * Must not be called for a default server instance.
3769
 *
3770
 * This function is first intended to be used through parse_server to
3771
 * initialize a new server on startup.
3772
 */
3773
static int _srv_parse_finalize(char **args, int cur_arg,
3774
                               struct server *srv, struct proxy *px,
3775
                               int parse_flags)
3776
0
{
3777
0
  int ret;
3778
0
  char *errmsg = NULL;
3779
0
  struct srv_pp_tlv_list *srv_tlv = NULL;
3780
3781
0
  if (srv->do_check && srv->trackit) {
3782
0
    ha_alert("unable to enable checks and tracking at the same time!\n");
3783
0
    return ERR_ALERT | ERR_FATAL;
3784
0
  }
3785
3786
0
  if (srv->do_agent && !srv->agent.port) {
3787
0
    ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
3788
0
             srv->id);
3789
0
    return ERR_ALERT | ERR_FATAL;
3790
0
  }
3791
3792
0
  if ((ret = parse_srv_expr(srv->sni_expr, &srv->ssl_ctx.sni, px, &errmsg))) {
3793
0
    if (errmsg) {
3794
0
      ha_alert("error detected while parsing sni expression : %s.\n", errmsg);
3795
0
      free(errmsg);
3796
0
    }
3797
0
    return ret;
3798
0
  }
3799
3800
  /* Use sni as fallback if pool_conn_name isn't set */
3801
0
  if (!srv->pool_conn_name && srv->sni_expr) {
3802
0
    srv->pool_conn_name = strdup(srv->sni_expr);
3803
0
    if (!srv->pool_conn_name) {
3804
0
      ha_alert("out of memory\n");
3805
0
      return ERR_ALERT | ERR_FATAL;
3806
0
    }
3807
0
  }
3808
3809
0
  if ((ret = parse_srv_expr(srv->pool_conn_name, &srv->pool_conn_name_expr,
3810
0
                            px, &errmsg))) {
3811
0
    if (errmsg) {
3812
0
      ha_alert("error detected while parsing pool-conn-name expression : %s.\n", errmsg);
3813
0
      free(errmsg);
3814
0
    }
3815
0
    return ret;
3816
0
  }
3817
3818
  /* A dynamic server is disabled on startup. It must not be counted as
3819
   * an active backend entry.
3820
   */
3821
0
  if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
3822
0
    if (srv->flags & SRV_F_BACKUP)
3823
0
      px->srv_bck++;
3824
0
    else
3825
0
      px->srv_act++;
3826
0
  }
3827
3828
0
  list_for_each_entry(srv_tlv, &srv->pp_tlvs, list) {
3829
0
    if (srv_tlv->fmt_string && unlikely(!parse_logformat_string(srv_tlv->fmt_string,
3830
0
      srv->proxy, &srv_tlv->fmt, 0, SMP_VAL_BE_SRV_CON, &errmsg))) {
3831
0
      if (errmsg) {
3832
0
        ha_alert("%s\n", errmsg);
3833
0
        free(errmsg);
3834
0
      }
3835
0
      return ERR_ALERT | ERR_FATAL;
3836
0
    }
3837
0
  }
3838
3839
#ifdef USE_QUIC
3840
  if (srv_is_quic(srv)) {
3841
    if (!srv->use_ssl) {
3842
      ha_alert("QUIC protocol detected without explicit SSL requirement. Use 'ssl' to fix this.\n");
3843
      return ERR_ALERT | ERR_FATAL;
3844
    }
3845
  }
3846
#endif
3847
3848
0
  srv_lb_commit_status(srv);
3849
3850
0
  return 0;
3851
0
}
3852
3853
int parse_server(const char *file, int linenum, char **args,
3854
                 struct proxy *curproxy, const struct proxy *defproxy,
3855
                 int parse_flags)
3856
0
{
3857
0
  struct server *newsrv = NULL;
3858
0
  int err_code = 0;
3859
3860
0
  int cur_arg;
3861
3862
0
  set_usermsgs_ctx(file, linenum, NULL);
3863
3864
0
  if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
3865
0
    ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
3866
0
    err_code |= ERR_ALERT | ERR_FATAL;
3867
0
    goto out;
3868
0
  }
3869
0
  else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
3870
0
    err_code |= ERR_ALERT | ERR_FATAL;
3871
0
    goto out;
3872
0
  }
3873
3874
0
  if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
3875
0
      (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
3876
0
    if (!*args[2])
3877
0
      goto out;
3878
0
  }
3879
3880
0
  err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
3881
0
                             parse_flags);
3882
3883
0
  if (err_code & ERR_CODE)
3884
0
    goto out;
3885
3886
0
  if (!newsrv->conf.file) // note: do it only once for default-server
3887
0
    newsrv->conf.file = strdup(file);
3888
0
  newsrv->conf.line = linenum;
3889
3890
0
  while (*args[cur_arg]) {
3891
0
    err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
3892
0
                             parse_flags);
3893
0
    if (err_code & ERR_FATAL)
3894
0
      goto out;
3895
0
  }
3896
3897
0
  if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
3898
0
    err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
3899
0
    if (err_code & ERR_FATAL)
3900
0
      goto out;
3901
0
  }
3902
3903
0
  if (parse_flags & SRV_PARSE_TEMPLATE) {
3904
0
    _srv_parse_tmpl_init(newsrv, curproxy);
3905
0
  }
3906
0
  else if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
3907
0
    newsrv->conf.name.key = newsrv->id;
3908
0
    ebis_insert(&curproxy->conf.used_server_name, &newsrv->conf.name);
3909
0
  }
3910
3911
  /* If the server id is fixed, insert it in the proxy used_id tree.
3912
   * This is needed to detect a later duplicate id via srv_parse_id.
3913
   *
3914
   * If no is specified, a dynamic one is generated in
3915
   * check_config_validity.
3916
   */
3917
0
  if (newsrv->flags & SRV_F_FORCED_ID)
3918
0
    eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3919
3920
0
  HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
3921
0
                       "configured with weight of 0 will never be selected by load balancing algorithms\n");
3922
3923
0
  reset_usermsgs_ctx();
3924
0
  return 0;
3925
3926
0
 out:
3927
0
  reset_usermsgs_ctx();
3928
0
  return err_code;
3929
0
}
3930
3931
/* Returns a pointer to the first server matching either id <id>.
3932
 * NULL is returned if no match is found.
3933
 * the lookup is performed in the backend <bk>
3934
 */
3935
struct server *server_find_by_id(struct proxy *bk, int id)
3936
0
{
3937
0
  struct eb32_node *eb32;
3938
0
  struct server *curserver;
3939
3940
0
  if (!bk || (id ==0))
3941
0
    return NULL;
3942
3943
  /* <bk> has no backend capabilities, so it can't have a server */
3944
0
  if (!(bk->cap & PR_CAP_BE))
3945
0
    return NULL;
3946
3947
0
  curserver = NULL;
3948
3949
0
  eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3950
0
  if (eb32)
3951
0
    curserver = container_of(eb32, struct server, conf.id);
3952
3953
0
  return curserver;
3954
0
}
3955
3956
/*
3957
 * This function finds a server with matching "<puid> x <rid>" within
3958
 * selected backend <bk>.
3959
 * Using the combination of proxy-uid + revision id ensures that the function
3960
 * will either return the server we're expecting or NULL if it has been removed
3961
 * from the proxy (<id> is unique within the list, but it is not true over the
3962
 * process lifetime as new servers may reuse the id of a previously deleted
3963
 * server).
3964
 */
3965
struct server *server_find_by_id_unique(struct proxy *bk, int id, uint32_t rid)
3966
0
{
3967
0
  struct server *curserver;
3968
3969
0
  curserver = server_find_by_id(bk, id);
3970
0
  if (!curserver || curserver->rid != rid)
3971
0
    return NULL;
3972
0
  return curserver;
3973
0
}
3974
3975
/* Returns a pointer to the first server matching either name <name>, or id
3976
 * if <name> starts with a '#'. NULL is returned if no match is found.
3977
 * the lookup is performed in the backend <bk>
3978
 */
3979
struct server *server_find_by_name(struct proxy *bk, const char *name)
3980
0
{
3981
0
  struct server *curserver;
3982
3983
0
  if (!bk || !name)
3984
0
    return NULL;
3985
3986
  /* <bk> has no backend capabilities, so it can't have a server */
3987
0
  if (!(bk->cap & PR_CAP_BE))
3988
0
    return NULL;
3989
3990
0
  curserver = NULL;
3991
0
  if (*name == '#') {
3992
0
    curserver = server_find_by_id(bk, atoi(name + 1));
3993
0
  }
3994
0
  else {
3995
0
    struct ebpt_node *node;
3996
3997
0
    node = ebis_lookup(&bk->conf.used_server_name, name);
3998
0
    if (node)
3999
0
      curserver = container_of(node, struct server, conf.name);
4000
0
  }
4001
4002
0
  return curserver;
4003
0
}
4004
4005
/*
4006
 * This function finds a server with matching "<name> x <rid>" within
4007
 * selected backend <bk>.
4008
 * Using the combination of name + revision id ensures that the function
4009
 * will either return the server we're expecting or NULL if it has been removed
4010
 * from the proxy. For this we assume that <name> is unique within the list,
4011
 * which is the case in most setups, but in rare cases the user may have
4012
 * enforced duplicate server names in the initial config (ie: if he intends to
4013
 * use numerical IDs for identification instead). In this particular case, the
4014
 * function will not work as expected so server_find_by_id_unique() should be
4015
 * used to match a unique server instead.
4016
 *
4017
 * Just like server_find_by_id_unique(), if a server is deleted and a new server
4018
 * reuses the same name, the rid check will prevent the function from returning
4019
 * a different server from the one we were expecting to match against at a given
4020
 * time.
4021
 */
4022
struct server *server_find_by_name_unique(struct proxy *bk, const char *name, uint32_t rid)
4023
0
{
4024
0
  struct server *curserver;
4025
4026
0
  curserver = server_find_by_name(bk, name);
4027
0
  if (!curserver || curserver->rid != rid)
4028
0
    return NULL;
4029
0
  return curserver;
4030
0
}
4031
4032
struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
4033
0
{
4034
0
  struct server *byname;
4035
0
  struct server *byid;
4036
4037
0
  if (!name && !id)
4038
0
    return NULL;
4039
4040
0
  if (diff)
4041
0
    *diff = 0;
4042
4043
0
  byname = byid = NULL;
4044
4045
0
  if (name) {
4046
0
    byname = server_find_by_name(bk, name);
4047
0
    if (byname && (!id || byname->puid == id))
4048
0
      return byname;
4049
0
  }
4050
4051
  /* remaining possibilities :
4052
   *  - name not set
4053
   *  - name set but not found
4054
   *  - name found but ID doesn't match
4055
   */
4056
0
  if (id) {
4057
0
    byid = server_find_by_id(bk, id);
4058
0
    if (byid) {
4059
0
      if (byname) {
4060
        /* use id only if forced by configuration */
4061
0
        if (byid->flags & SRV_F_FORCED_ID) {
4062
0
          if (diff)
4063
0
            *diff |= 2;
4064
0
          return byid;
4065
0
        }
4066
0
        else {
4067
0
          if (diff)
4068
0
            *diff |= 1;
4069
0
          return byname;
4070
0
        }
4071
0
      }
4072
4073
      /* remaining possibilities:
4074
       *   - name not set
4075
       *   - name set but not found
4076
       */
4077
0
      if (name && diff)
4078
0
        *diff |= 2;
4079
0
      return byid;
4080
0
    }
4081
4082
    /* id bot found */
4083
0
    if (byname) {
4084
0
      if (diff)
4085
0
        *diff |= 1;
4086
0
      return byname;
4087
0
    }
4088
0
  }
4089
4090
0
  return NULL;
4091
0
}
4092
4093
/* This functions retrieves server's addr and port to fill
4094
 * <inetaddr> struct passed as argument.
4095
 *
4096
 * This may only be used under inet context.
4097
 */
4098
void server_get_inetaddr(struct server *s, struct server_inetaddr *inetaddr)
4099
0
{
4100
0
  struct sockaddr_storage *addr = &s->addr;
4101
0
  unsigned int port = s->svc_port;
4102
0
  uint8_t mapports = !!(s->flags & SRV_F_MAPPORTS);
4103
4104
  /* only INET families are supported */
4105
0
  BUG_ON((addr->ss_family != AF_UNSPEC &&
4106
0
          addr->ss_family != AF_INET && addr->ss_family != AF_INET6));
4107
4108
0
  inetaddr->family = addr->ss_family;
4109
0
  memset(&inetaddr->addr, 0, sizeof(inetaddr->addr));
4110
4111
0
  if (addr->ss_family == AF_INET)
4112
0
    inetaddr->addr.v4 =
4113
0
      ((struct sockaddr_in *)addr)->sin_addr;
4114
0
  else if (addr->ss_family == AF_INET6)
4115
0
    inetaddr->addr.v6 =
4116
0
      ((struct sockaddr_in6 *)addr)->sin6_addr;
4117
4118
0
  inetaddr->port.svc = port;
4119
0
  inetaddr->port.map = mapports;
4120
0
}
4121
4122
/* get human readable name for server_inetaddr_updater .by struct member
4123
 */
4124
const char *server_inetaddr_updater_by_to_str(enum server_inetaddr_updater_by by)
4125
0
{
4126
0
  switch (by) {
4127
0
    case SERVER_INETADDR_UPDATER_BY_CLI:
4128
0
      return "stats socket command";
4129
0
    case SERVER_INETADDR_UPDATER_BY_LUA:
4130
0
      return "Lua script";
4131
0
    case SERVER_INETADDR_UPDATER_BY_DNS_AR:
4132
0
      return "DNS additional record";
4133
0
    case SERVER_INETADDR_UPDATER_BY_DNS_CACHE:
4134
0
      return "DNS cache";
4135
0
    case SERVER_INETADDR_UPDATER_BY_DNS_RESOLVER:
4136
0
      return "DNS resolver";
4137
0
    default:
4138
      /* unknown, don't mention updater */
4139
0
      break;
4140
0
  }
4141
0
  return NULL;
4142
0
}
4143
4144
/* append inetaddr updater info to chunk <out>
4145
 */
4146
static void _srv_append_inetaddr_updater_info(struct buffer *out,
4147
                                              struct server *s,
4148
                                              struct server_inetaddr_updater updater)
4149
0
{
4150
0
  switch (updater.by) {
4151
0
    case SERVER_INETADDR_UPDATER_BY_DNS_RESOLVER:
4152
      /* we need to report the resolver/nameserver id which is
4153
       * responsible for the update
4154
       */
4155
0
      {
4156
0
        struct resolvers *r = s->resolvers;
4157
0
        struct dns_nameserver *ns;
4158
4159
        /* we already know that the update comes from the
4160
         * resolver section linked to the server, but we
4161
         * need to find out which nameserver handled the dns
4162
         * query
4163
         */
4164
0
        BUG_ON(!r);
4165
0
        ns = find_nameserver_by_resolvers_and_id(r, updater.u.dns_resolver.ns_id);
4166
0
        BUG_ON(!ns);
4167
0
        chunk_appendf(out, " by '%s/%s'", r->id, ns->id);
4168
0
      }
4169
0
      break;
4170
0
    default:
4171
0
      {
4172
0
        const char *by_name;
4173
4174
0
        by_name = server_inetaddr_updater_by_to_str(updater.by);
4175
0
        if (by_name)
4176
0
          chunk_appendf(out, " by '%s'", by_name);
4177
0
      }
4178
0
      break;
4179
0
  }
4180
0
}
4181
4182
/* server_set_inetaddr() helper */
4183
static void _addr_to_str(int family, const void *addr, char *addr_str, size_t len)
4184
0
{
4185
0
  memset(addr_str, 0, len);
4186
0
  switch (family) {
4187
0
    case AF_INET:
4188
0
    case AF_INET6:
4189
0
      inet_ntop(family, addr, addr_str, len);
4190
0
      break;
4191
0
    default:
4192
0
      strlcpy2(addr_str, "(none)", len);
4193
0
      break;
4194
0
  }
4195
0
}
4196
/* server_set_inetaddr() helper */
4197
static int _inetaddr_addr_cmp(const struct server_inetaddr *inetaddr, const struct sockaddr_storage *addr)
4198
0
{
4199
0
  struct in_addr *v4;
4200
0
  struct in6_addr *v6;
4201
4202
0
  if (inetaddr->family != addr->ss_family)
4203
0
    return 1;
4204
4205
0
  if (inetaddr->family == AF_INET) {
4206
0
    v4 = &((struct sockaddr_in *)addr)->sin_addr;
4207
0
    if (memcmp(&inetaddr->addr.v4, v4, sizeof(struct in_addr)))
4208
0
      return 1;
4209
0
  }
4210
0
  else if (inetaddr->family == AF_INET6) {
4211
0
    v6 = &((struct sockaddr_in6 *)addr)->sin6_addr;
4212
0
    if (memcmp(&inetaddr->addr.v6, v6, sizeof(struct in6_addr)))
4213
0
      return 1;
4214
0
  }
4215
4216
0
  return 0; // both inetaddr storage are equivalent
4217
0
}
4218
4219
/* This function sets a server's addr and port in inet context based on new
4220
 * inetaddr input
4221
 *
4222
 * The function first does the following, in that order:
4223
 * - checks if an update is required (new IP or port is different than current
4224
 * one)
4225
 * - check the update is allowed:
4226
 *  - allow all changes if no CHECKS are configured
4227
 *  - if CHECK is configured:
4228
 *   - if switch to port map (SRV_F_MAPPORTS), ensure health check have their
4229
 *     own ports
4230
 *  - applies required changes to both ADDR and PORT if both 'required' and
4231
 *    'allowed' conditions are met.
4232
 *
4233
 * Caller can pass <msg> buffer so that it gets some information about the
4234
 * operation. It may as well provide <updater> so that messages mention that
4235
 * the update was performed on the behalf of it.
4236
 *
4237
 * <inetaddr> family may be set to UNSPEC to reset server's addr
4238
 *
4239
 * Caller must set <inetaddr>->port.map to 1 if <inetaddr>->port.svc must be
4240
 * handled as an offset
4241
 *
4242
 * The function returns 1 if an update was performed and 0 if nothing was
4243
 * changed.
4244
 */
4245
int server_set_inetaddr(struct server *s,
4246
                        const struct server_inetaddr *inetaddr,
4247
                        struct server_inetaddr_updater updater, struct buffer *msg)
4248
0
{
4249
0
  union {
4250
0
    struct event_hdl_cb_data_server_inetaddr addr;
4251
0
    struct event_hdl_cb_data_server common;
4252
0
  } cb_data;
4253
0
  char addr_str[INET6_ADDRSTRLEN];
4254
0
  uint16_t current_port;
4255
0
  uint8_t ip_change = 0;
4256
0
  uint8_t port_change = 0;
4257
0
  int ret = 0;
4258
4259
  /* only INET families are supported */
4260
0
  BUG_ON((inetaddr->family != AF_UNSPEC &&
4261
0
          inetaddr->family != AF_INET && inetaddr->family != AF_INET6) ||
4262
0
         (s->addr.ss_family != AF_UNSPEC &&
4263
0
          s->addr.ss_family != AF_INET && s->addr.ss_family != AF_INET6));
4264
4265
  /* ignore if no change */
4266
0
  if (!_inetaddr_addr_cmp(inetaddr, &s->addr))
4267
0
    goto port;
4268
4269
0
  ip_change = 1;
4270
4271
  /* update report for caller */
4272
0
  if (msg) {
4273
0
    void *from_ptr = NULL;
4274
4275
0
    if (s->addr.ss_family == AF_INET)
4276
0
      from_ptr = &((struct sockaddr_in *)&s->addr)->sin_addr;
4277
0
    else if (s->addr.ss_family == AF_INET6)
4278
0
      from_ptr = &((struct sockaddr_in6 *)&s->addr)->sin6_addr;
4279
4280
0
    _addr_to_str(s->addr.ss_family, from_ptr, addr_str, sizeof(addr_str));
4281
0
    chunk_printf(msg, "IP changed from '%s'", addr_str);
4282
0
    _addr_to_str(inetaddr->family, &inetaddr->addr, addr_str, sizeof(addr_str));
4283
0
    chunk_appendf(msg, " to '%s'", addr_str);
4284
0
  }
4285
4286
0
  if (inetaddr->family == AF_UNSPEC)
4287
0
    goto out; // ignore port information when unsetting addr
4288
4289
0
 port:
4290
4291
  /* collection data currently setup */
4292
0
  current_port = s->svc_port;
4293
4294
  /* check if caller triggers a port mapped or offset */
4295
0
  if (inetaddr->port.map) {
4296
    /* check if server currently uses port map */
4297
0
    if (!(s->flags & SRV_F_MAPPORTS)) {
4298
      /* we're switching from a fixed port to a SRV_F_MAPPORTS
4299
       * (mapped) port, prevent PORT change if check is enabled
4300
       * and it doesn't have it's dedicated port while switching
4301
       * to port mapping
4302
       */
4303
0
      if ((s->check.state & CHK_ST_ENABLED) && !s->check.port) {
4304
0
        if (msg) {
4305
0
          if (ip_change)
4306
0
            chunk_appendf(msg, ", ");
4307
0
          chunk_appendf(msg, "can't change <port> to port map because it is incompatible with current health check port configuration (use 'port' statement from the 'server' directive).");
4308
0
        }
4309
0
        goto out;
4310
0
      }
4311
      /* switch from fixed port to port map mandatorily triggers
4312
       * a port change
4313
       */
4314
0
      port_change = 1;
4315
0
    }
4316
    /* else we're already using port maps */
4317
0
    else {
4318
0
      port_change = current_port != inetaddr->port.svc;
4319
0
    }
4320
0
  }
4321
  /* fixed port */
4322
0
  else {
4323
0
    if ((s->flags & SRV_F_MAPPORTS))
4324
0
      port_change = 1; // changing from mapped to fixed
4325
0
    else
4326
0
      port_change = current_port != inetaddr->port.svc;
4327
0
  }
4328
4329
  /* update response message about PORT change */
4330
0
  if (port_change && msg) {
4331
0
    if (ip_change)
4332
0
      chunk_appendf(msg, ", ");
4333
4334
0
    chunk_appendf(msg, "port changed from '");
4335
0
    if (s->flags & SRV_F_MAPPORTS)
4336
0
      chunk_appendf(msg, "+");
4337
4338
0
    chunk_appendf(msg, "%d' to '", s->svc_port);
4339
0
    if (inetaddr->port.map)
4340
0
      chunk_appendf(msg, "+");
4341
0
    chunk_appendf(msg, "%d'", inetaddr->port.svc);
4342
0
  }
4343
4344
0
 out:
4345
0
  if (ip_change || port_change) {
4346
0
    _srv_event_hdl_prepare(&cb_data.common, s, 0);
4347
0
    _srv_event_hdl_prepare_inetaddr(&cb_data.addr, s,
4348
0
                                    inetaddr,
4349
0
                                    updater);
4350
4351
    /* server_atomic_sync_task will apply the changes for us */
4352
0
    _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_INETADDR, cb_data, s);
4353
4354
0
    ret = 1;
4355
0
  }
4356
4357
0
  if (ret && msg && updater.by != SERVER_INETADDR_UPDATER_BY_NONE)
4358
0
    _srv_append_inetaddr_updater_info(msg, s, updater);
4359
0
  return ret;
4360
0
}
4361
4362
/* Sets new server's addr and/or svc_port, then send a log and report a
4363
 * warning on stderr if something has changed.
4364
 *
4365
 * Returns 1 if something has changed, 0 otherwise.
4366
 * see server_set_inetaddr() for more information.
4367
 */
4368
int server_set_inetaddr_warn(struct server *s,
4369
                             const struct server_inetaddr *inetaddr,
4370
                             struct server_inetaddr_updater updater)
4371
0
{
4372
0
  struct buffer *msg = get_trash_chunk();
4373
0
  int ret;
4374
4375
0
  chunk_reset(msg);
4376
4377
0
  ret = server_set_inetaddr(s, inetaddr, updater, msg);
4378
0
  if (msg->data) {
4379
    /* write the buffer on stderr */
4380
0
    ha_warning("%s/%s: %s.\n", s->proxy->id, s->id, msg->area);
4381
4382
    /* send a log */
4383
0
    send_log(s->proxy, LOG_NOTICE, "%s/%s: %s.\n", s->proxy->id, s->id, msg->area);
4384
0
  }
4385
0
  return ret;
4386
0
}
4387
4388
/*
4389
 * update a server's current IP address.
4390
 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
4391
 * ip is in network format.
4392
 * updater is a string which contains an information about the requester of the update.
4393
 * updater is used if not NULL.
4394
 *
4395
 * A log line and a stderr warning message is generated based on server's backend options.
4396
 *
4397
 * Must be called with the server lock held.
4398
 */
4399
int srv_update_addr(struct server *s, void *ip, int ip_sin_family, struct server_inetaddr_updater updater)
4400
0
{
4401
0
  struct server_inetaddr inetaddr;
4402
4403
0
  server_get_inetaddr(s, &inetaddr);
4404
0
  BUG_ON(ip_sin_family != AF_INET && ip_sin_family != AF_INET6);
4405
4406
  /* save the new IP family */
4407
0
  inetaddr.family = ip_sin_family;
4408
  /* save the new IP address */
4409
0
  switch (ip_sin_family) {
4410
0
  case AF_INET:
4411
0
    memcpy(&inetaddr.addr.v4, ip, 4);
4412
0
    break;
4413
0
  case AF_INET6:
4414
0
    memcpy(&inetaddr.addr.v6, ip, 16);
4415
0
    break;
4416
0
  };
4417
4418
0
  server_set_inetaddr_warn(s, &inetaddr, updater);
4419
4420
0
  return 0;
4421
0
}
4422
4423
/* update agent health check address and port
4424
 * addr can be ip4/ip6 or a hostname
4425
 * if one error occurs, don't apply anything
4426
 * must be called with the server lock held.
4427
 */
4428
const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
4429
0
{
4430
0
  struct sockaddr_storage sk;
4431
0
  struct buffer *msg;
4432
0
  int new_port;
4433
4434
0
  msg = get_trash_chunk();
4435
0
  chunk_reset(msg);
4436
4437
0
  if (!(s->agent.state & CHK_ST_ENABLED)) {
4438
0
    chunk_strcat(msg, "agent checks are not enabled on this server");
4439
0
    goto out;
4440
0
  }
4441
0
  if (addr) {
4442
0
    memset(&sk, 0, sizeof(struct sockaddr_storage));
4443
0
    if (str2ip(addr, &sk) == NULL) {
4444
0
      chunk_appendf(msg, "invalid addr '%s'", addr);
4445
0
      goto out;
4446
0
    }
4447
0
  }
4448
0
  if (port) {
4449
0
    if (strl2irc(port, strlen(port), &new_port) != 0) {
4450
0
      chunk_appendf(msg, "provided port is not an integer");
4451
0
      goto out;
4452
0
    }
4453
0
    if (new_port < 0 || new_port > 65535) {
4454
0
      chunk_appendf(msg, "provided port is invalid");
4455
0
      goto out;
4456
0
    }
4457
0
  }
4458
0
out:
4459
0
  if (msg->data)
4460
0
    return msg->area;
4461
0
  else {
4462
0
    if (addr)
4463
0
      set_srv_agent_addr(s, &sk);
4464
0
    if (port)
4465
0
      set_srv_agent_port(s, new_port);
4466
0
  }
4467
0
  return NULL;
4468
0
}
4469
4470
/* update server health check address and port
4471
 * addr must be ip4 or ip6, it won't be resolved
4472
 * if one error occurs, don't apply anything
4473
 * must be called with the server lock held.
4474
 */
4475
const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
4476
0
{
4477
0
  struct sockaddr_storage sk;
4478
0
  struct buffer *msg;
4479
0
  int new_port;
4480
4481
0
  msg = get_trash_chunk();
4482
0
  chunk_reset(msg);
4483
4484
0
  if (!(s->check.state & CHK_ST_ENABLED)) {
4485
0
    chunk_strcat(msg, "health checks are not enabled on this server");
4486
0
    goto out;
4487
0
  }
4488
0
  if (addr) {
4489
0
    memset(&sk, 0, sizeof(struct sockaddr_storage));
4490
0
    if (str2ip2(addr, &sk, 0) == NULL) {
4491
0
      chunk_appendf(msg, "invalid addr '%s'", addr);
4492
0
      goto out;
4493
0
    }
4494
0
  }
4495
0
  if (port) {
4496
0
    if (strl2irc(port, strlen(port), &new_port) != 0) {
4497
0
      chunk_appendf(msg, "provided port is not an integer");
4498
0
      goto out;
4499
0
    }
4500
0
    if (new_port < 0 || new_port > 65535) {
4501
0
      chunk_appendf(msg, "provided port is invalid");
4502
0
      goto out;
4503
0
    }
4504
    /* prevent the update of port to 0 if MAPPORTS are in use */
4505
0
    if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
4506
0
      chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
4507
0
      goto out;
4508
0
    }
4509
0
  }
4510
0
out:
4511
0
  if (msg->data)
4512
0
    return msg->area;
4513
0
  else {
4514
0
    if (addr)
4515
0
      s->check.addr = sk;
4516
0
    if (port)
4517
0
      s->check.port = new_port;
4518
0
  }
4519
0
  return NULL;
4520
0
}
4521
4522
/*
4523
 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
4524
 *
4525
 * Caller can pass its info through <updater> to get it integrated in the response
4526
 * message returned by the function.
4527
 *
4528
 * The function first does the following, in that order:
4529
 * - checks that don't switch from/to a family other than AF_INET and AF_INET6
4530
 * - validates the new addr and/or port
4531
 * - calls server_set_inetaddr() to check and apply the change
4532
 *
4533
 * Must be called with the server lock held.
4534
 */
4535
const char *srv_update_addr_port(struct server *s, const char *addr, const char *port,
4536
                                 struct server_inetaddr_updater updater)
4537
0
{
4538
0
  struct sockaddr_storage sa;
4539
0
  struct server_inetaddr inetaddr;
4540
0
  struct buffer *msg;
4541
0
  int ret;
4542
4543
0
  msg = get_trash_chunk();
4544
0
  chunk_reset(msg);
4545
4546
  /* even a simple port change is not supported outside of inet context, because
4547
   * s->svc_port is only relevant under inet context
4548
  */
4549
0
  if ((s->addr.ss_family != AF_INET) && (s->addr.ss_family != AF_INET6)) {
4550
0
    chunk_printf(msg, "Update for the current server address family is only supported through configuration file.");
4551
0
    goto out;
4552
0
  }
4553
4554
0
  server_get_inetaddr(s, &inetaddr);
4555
4556
0
  if (addr) {
4557
0
    memset(&sa, 0, sizeof(struct sockaddr_storage));
4558
0
    if (str2ip2(addr, &sa, 0) == NULL) {
4559
0
      chunk_printf(msg, "Invalid addr '%s'", addr);
4560
0
      goto out;
4561
0
    }
4562
4563
    /* changes are allowed on AF_INET* families only */
4564
0
    if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
4565
0
      chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
4566
0
      goto out;
4567
0
    }
4568
4569
0
    inetaddr.family = sa.ss_family;
4570
0
    switch (inetaddr.family) {
4571
0
      case AF_INET:
4572
0
        inetaddr.addr.v4 = ((struct sockaddr_in *)&sa)->sin_addr;
4573
0
      break;
4574
0
      case AF_INET6:
4575
0
        inetaddr.addr.v6 = ((struct sockaddr_in6 *)&sa)->sin6_addr;
4576
0
      break;
4577
0
    }
4578
0
  }
4579
4580
0
  if (port) {
4581
0
    uint16_t new_port;
4582
0
    char sign = '\0';
4583
0
    char *endptr;
4584
4585
0
    sign = *port;
4586
4587
0
    errno = 0;
4588
0
    new_port = strtol(port, &endptr, 10);
4589
0
    if ((errno != 0) || (port == endptr)) {
4590
0
      chunk_appendf(msg, "problem converting port '%s' to an int", port);
4591
0
      goto out;
4592
0
    }
4593
4594
    /* check if caller triggers a port mapped or offset */
4595
0
    if (sign == '-' || sign == '+')
4596
0
      inetaddr.port.map = 1;
4597
0
    else
4598
0
      inetaddr.port.map = 0;
4599
4600
0
    inetaddr.port.svc = new_port;
4601
4602
    /* note: negative offset was converted to positive offset
4603
     * (new_port is unsigned) to prevent later conversions errors
4604
     * since svc_port is handled as an unsigned int all along the
4605
     * chain. Unfortunately this is a one-way operation so the user
4606
     * could be surprised to see a negative offset reported using
4607
     * its equivalent positive offset in the generated message
4608
     * (-X = +(65535 - (X-1))), but thanks to proper wraparound it
4609
     * will be interpreted as a negative offset during port
4610
     * remapping so it will work as expected.
4611
     */
4612
0
  }
4613
4614
0
  ret = server_set_inetaddr(s, &inetaddr, updater, msg);
4615
0
  if (!ret)
4616
0
    chunk_printf(msg, "nothing changed");
4617
4618
0
 out:
4619
0
  return msg->area;
4620
0
}
4621
4622
/*
4623
 * put the server in maintenance because of failing SRV resolution
4624
 * returns:
4625
 *  0 if server was put under maintenance
4626
 *  1 if server status has not changed
4627
 *
4628
 * Must be called with the server lock held.
4629
 */
4630
int srvrq_set_srv_down(struct server *s)
4631
0
{
4632
0
  if (!s->srvrq)
4633
0
    return 1;
4634
4635
0
  if (s->next_admin & SRV_ADMF_RMAINT)
4636
0
    return 1;
4637
4638
0
  srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOENT);
4639
0
  return 0;
4640
0
}
4641
4642
/*
4643
 * put server under maintenance as a result of name resolution
4644
 * returns:
4645
 *  0 if server was put under maintenance
4646
 *  1 if server status has not changed
4647
 *
4648
 * Must be called with the server lock held.
4649
 */
4650
int snr_set_srv_down(struct server *s)
4651
0
{
4652
0
  struct resolvers  *resolvers  = s->resolvers;
4653
0
  struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
4654
0
  int exp;
4655
4656
  /* server already under maintenance */
4657
0
  if (s->next_admin & SRV_ADMF_RMAINT)
4658
0
    goto out;
4659
4660
  /* If resolution is NULL we're dealing with SRV records Additional records */
4661
0
  if (resolution == NULL)
4662
0
    return srvrq_set_srv_down(s);
4663
4664
0
  switch (resolution->status) {
4665
0
    case RSLV_STATUS_NONE:
4666
      /* status when HAProxy has just (re)started.
4667
       * Nothing to do, since the task is already automatically started */
4668
0
      goto out;
4669
4670
0
    case RSLV_STATUS_VALID:
4671
      /*
4672
       * valid resolution but no usable server address
4673
       */
4674
0
      srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOIP);
4675
0
      return 0;
4676
4677
0
    case RSLV_STATUS_NX:
4678
      /* stop server if resolution is NX for a long enough period */
4679
0
      exp = tick_add(resolution->last_valid, resolvers->hold.nx);
4680
0
      if (!tick_is_expired(exp, now_ms))
4681
0
        goto out; // not yet expired
4682
4683
0
      srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NX);
4684
0
      return 0;
4685
4686
0
    case RSLV_STATUS_TIMEOUT:
4687
      /* stop server if resolution is TIMEOUT for a long enough period */
4688
0
      exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
4689
0
      if (!tick_is_expired(exp, now_ms))
4690
0
        goto out; // not yet expired
4691
4692
0
      srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_TIMEOUT);
4693
0
      return 0;
4694
4695
0
    case RSLV_STATUS_REFUSED:
4696
      /* stop server if resolution is REFUSED for a long enough period */
4697
0
      exp = tick_add(resolution->last_valid, resolvers->hold.refused);
4698
0
      if (!tick_is_expired(exp, now_ms))
4699
0
        goto out; // not yet expired
4700
4701
0
      srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_REFUSED);
4702
0
      return 0;
4703
4704
0
    default:
4705
      /* stop server if resolution failed for a long enough period */
4706
0
      exp = tick_add(resolution->last_valid, resolvers->hold.other);
4707
0
      if (!tick_is_expired(exp, now_ms))
4708
0
        goto out; // not yet expired
4709
4710
0
      srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_UNSPEC);
4711
0
      return 0;
4712
0
  }
4713
4714
0
 out:
4715
0
  return 1;
4716
0
}
4717
4718
/*
4719
 * Server Name Resolution valid response callback
4720
 * It expects:
4721
 *  - <nameserver>: the name server which answered the valid response
4722
 *  - <response>: buffer containing a valid DNS response
4723
 *  - <response_len>: size of <response>
4724
 * It performs the following actions:
4725
 *  - ignore response if current ip found and server family not met
4726
 *  - update with first new ip found if family is met and current IP is not found
4727
 * returns:
4728
 *  0 on error
4729
 *  1 when no error or safe ignore
4730
 *
4731
 * Must be called with server lock held
4732
 */
4733
int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
4734
0
{
4735
0
  struct server *s = NULL;
4736
0
  struct resolv_resolution *resolution = NULL;
4737
0
  void *serverip, *firstip;
4738
0
  short server_sin_family, firstip_sin_family;
4739
0
  int ret;
4740
0
  int has_no_ip = 0;
4741
4742
0
  s = objt_server(requester->owner);
4743
0
  if (!s)
4744
0
    return 1;
4745
4746
0
  if (s->srvrq) {
4747
    /* If DNS resolution is disabled ignore it.
4748
     * This is the case if the server was associated to
4749
     * a SRV record and this record is now expired.
4750
     */
4751
0
    if (s->flags & SRV_F_NO_RESOLUTION)
4752
0
      return 1;
4753
0
  }
4754
4755
0
  resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
4756
0
  if (!resolution)
4757
0
    return 1;
4758
4759
  /* initializing variables */
4760
0
  firstip = NULL;   /* pointer to the first valid response found */
4761
        /* it will be used as the new IP if a change is required */
4762
0
  firstip_sin_family = AF_UNSPEC;
4763
0
  serverip = NULL;  /* current server IP address */
4764
4765
  /* initializing server IP pointer */
4766
0
  server_sin_family = s->addr.ss_family;
4767
0
  switch (server_sin_family) {
4768
0
    case AF_INET:
4769
0
      serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4770
0
      break;
4771
4772
0
    case AF_INET6:
4773
0
      serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4774
0
      break;
4775
4776
0
    case AF_UNSPEC:
4777
0
      break;
4778
4779
0
    default:
4780
0
      goto invalid;
4781
0
  }
4782
4783
0
  ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
4784
0
                                    serverip, server_sin_family, &firstip,
4785
0
                                    &firstip_sin_family, s);
4786
4787
0
  switch (ret) {
4788
0
    case RSLV_UPD_NO:
4789
0
      goto update_status;
4790
4791
0
    case RSLV_UPD_SRVIP_NOT_FOUND:
4792
0
      goto save_ip;
4793
4794
0
    case RSLV_UPD_NO_IP_FOUND:
4795
0
      has_no_ip = 1;
4796
0
      goto update_status;
4797
4798
0
    default:
4799
0
      has_no_ip = 1;
4800
0
      goto invalid;
4801
4802
0
  }
4803
4804
0
 save_ip:
4805
0
  if (counters) {
4806
0
    counters->app.resolver.update++;
4807
    /* save the first ip we found */
4808
0
    srv_update_addr(s, firstip, firstip_sin_family,
4809
0
                    SERVER_INETADDR_UPDATER_DNS_RESOLVER(counters->ns_puid));
4810
0
  }
4811
0
  else
4812
0
    srv_update_addr(s, firstip, firstip_sin_family, SERVER_INETADDR_UPDATER_DNS_CACHE);
4813
4814
0
 update_status:
4815
0
  if (has_no_ip && !snr_set_srv_down(s)) {
4816
0
    struct server_inetaddr srv_addr;
4817
4818
    /* unset server's addr, keep port */
4819
0
    server_get_inetaddr(s, &srv_addr);
4820
0
    srv_addr.family = AF_UNSPEC;
4821
0
    memset(&srv_addr.addr, 0, sizeof(srv_addr.addr));
4822
0
    server_set_inetaddr(s, &srv_addr, SERVER_INETADDR_UPDATER_NONE, NULL);
4823
0
  }
4824
0
  return 1;
4825
4826
0
 invalid:
4827
0
  if (counters) {
4828
0
    counters->app.resolver.invalid++;
4829
0
    goto update_status;
4830
0
  }
4831
0
  if (has_no_ip && !snr_set_srv_down(s)) {
4832
0
    struct server_inetaddr srv_addr;
4833
4834
    /* unset server's addr, keep port */
4835
0
    server_get_inetaddr(s, &srv_addr);
4836
0
    srv_addr.family = AF_UNSPEC;
4837
0
    memset(&srv_addr.addr, 0, sizeof(srv_addr.addr));
4838
0
    server_set_inetaddr(s, &srv_addr, SERVER_INETADDR_UPDATER_NONE, NULL);
4839
0
  }
4840
0
  return 0;
4841
0
}
4842
4843
/*
4844
 * SRV record error management callback
4845
 * returns:
4846
 *  0 if we can trash answser items.
4847
 *  1 when safely ignored and we must kept answer items
4848
 *
4849
 * Grabs the server's lock.
4850
 */
4851
int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
4852
0
{
4853
0
  struct resolv_srvrq *srvrq;
4854
0
  struct resolv_resolution *res;
4855
0
  struct resolvers *resolvers;
4856
0
  int exp;
4857
4858
  /* SRV records */
4859
0
  srvrq = objt_resolv_srvrq(requester->owner);
4860
0
  if (!srvrq)
4861
0
    return 0;
4862
4863
0
  resolvers = srvrq->resolvers;
4864
0
  res = requester->resolution;
4865
4866
0
  switch (res->status) {
4867
4868
0
    case RSLV_STATUS_NX:
4869
      /* stop server if resolution is NX for a long enough period */
4870
0
      exp = tick_add(res->last_valid, resolvers->hold.nx);
4871
0
      if (!tick_is_expired(exp, now_ms))
4872
0
        return 1;
4873
0
      break;
4874
4875
0
    case RSLV_STATUS_TIMEOUT:
4876
      /* stop server if resolution is TIMEOUT for a long enough period */
4877
0
      exp = tick_add(res->last_valid, resolvers->hold.timeout);
4878
0
      if (!tick_is_expired(exp, now_ms))
4879
0
        return 1;
4880
0
      break;
4881
4882
0
    case RSLV_STATUS_REFUSED:
4883
      /* stop server if resolution is REFUSED for a long enough period */
4884
0
      exp = tick_add(res->last_valid, resolvers->hold.refused);
4885
0
      if (!tick_is_expired(exp, now_ms))
4886
0
        return 1;
4887
0
      break;
4888
4889
0
    default:
4890
      /* stop server if resolution failed for a long enough period */
4891
0
      exp = tick_add(res->last_valid, resolvers->hold.other);
4892
0
      if (!tick_is_expired(exp, now_ms))
4893
0
        return 1;
4894
0
  }
4895
4896
  /* Remove any associated server ref */
4897
0
  resolv_detach_from_resolution_answer_items(res,  requester);
4898
4899
0
  return 0;
4900
0
}
4901
4902
/*
4903
 * Server Name Resolution error management callback
4904
 * returns:
4905
 *  0 if we can trash answser items.
4906
 *  1 when safely ignored and we must kept answer items
4907
 *
4908
 * Grabs the server's lock.
4909
 */
4910
int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
4911
0
{
4912
0
  struct server *s;
4913
4914
0
  s = objt_server(requester->owner);
4915
0
  if (!s)
4916
0
    return 0;
4917
4918
0
  HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4919
0
  if (!snr_set_srv_down(s)) {
4920
0
    struct server_inetaddr srv_addr;
4921
4922
    /* unset server's addr, keep port */
4923
0
    server_get_inetaddr(s, &srv_addr);
4924
0
    srv_addr.family = AF_UNSPEC;
4925
0
    memset(&srv_addr.addr, 0, sizeof(srv_addr.addr));
4926
0
    server_set_inetaddr(s, &srv_addr, SERVER_INETADDR_UPDATER_NONE, NULL);
4927
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4928
0
    resolv_detach_from_resolution_answer_items(requester->resolution, requester);
4929
0
    return 0;
4930
0
  }
4931
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4932
4933
0
  return 1;
4934
0
}
4935
4936
/*
4937
 * Function to check if <ip> is already affected to a server in the backend
4938
 * which owns <srv> and is up.
4939
 * It returns a pointer to the first server found or NULL if <ip> is not yet
4940
 * assigned.
4941
 *
4942
 * Must be called with server lock held
4943
 */
4944
struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4945
0
{
4946
0
  struct server *tmpsrv;
4947
0
  struct proxy *be;
4948
4949
0
  if (!srv)
4950
0
    return NULL;
4951
4952
0
  be = srv->proxy;
4953
0
  for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
4954
    /* we found the current server is the same, ignore it */
4955
0
    if (srv == tmpsrv)
4956
0
      continue;
4957
4958
    /* We want to compare the IP in the record with the IP of the servers in the
4959
     * same backend, only if:
4960
     *   * DNS resolution is enabled on the server
4961
     *   * the hostname used for the resolution by our server is the same than the
4962
     *     one used for the server found in the backend
4963
     *   * the server found in the backend is not our current server
4964
     */
4965
0
    HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
4966
0
    if ((tmpsrv->hostname_dn == NULL) ||
4967
0
        (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4968
0
        (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
4969
0
        (srv->puid == tmpsrv->puid)) {
4970
0
      HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
4971
0
      continue;
4972
0
    }
4973
4974
    /* If the server has been taken down, don't consider it */
4975
0
    if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
4976
0
      HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
4977
0
      continue;
4978
0
    }
4979
4980
    /* At this point, we have 2 different servers using the same DNS hostname
4981
     * for their respective resolution.
4982
     */
4983
0
    if (*ip_family == tmpsrv->addr.ss_family &&
4984
0
        ((tmpsrv->addr.ss_family == AF_INET &&
4985
0
          memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4986
0
         (tmpsrv->addr.ss_family == AF_INET6 &&
4987
0
          memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
4988
0
      HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
4989
0
      return tmpsrv;
4990
0
    }
4991
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
4992
0
  }
4993
4994
4995
0
  return NULL;
4996
0
}
4997
4998
/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4999
 * resolver. This is suited for initial address configuration. Returns 0 on
5000
 * success otherwise a non-zero error code. In case of error, *err_code, if
5001
 * not NULL, is filled up.
5002
 */
5003
int srv_set_addr_via_libc(struct server *srv, int *err_code)
5004
0
{
5005
0
  struct sockaddr_storage new_addr;
5006
5007
0
  memset(&new_addr, 0, sizeof(new_addr));
5008
5009
  /* Use the preferred family, if configured */
5010
0
  new_addr.ss_family = srv->addr.ss_family;
5011
0
  if (str2ip2(srv->hostname, &new_addr, 1) == NULL) {
5012
0
    if (err_code)
5013
0
      *err_code |= ERR_WARN;
5014
0
    return 1;
5015
0
  }
5016
0
  _srv_set_inetaddr(srv, &new_addr);
5017
0
  return 0;
5018
0
}
5019
5020
/* Set the server's FDQN (->hostname) from <hostname>.
5021
 * Returns -1 if failed, 0 if not.
5022
 *
5023
 * Must be called with the server lock held.
5024
 */
5025
int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
5026
0
{
5027
0
  struct resolv_resolution *resolution;
5028
0
  char                  *hostname_dn;
5029
0
  int                    hostname_len, hostname_dn_len;
5030
5031
  /* Note that the server lock is already held. */
5032
0
  if (!srv->resolvers)
5033
0
    return -1;
5034
5035
0
  if (!resolv_locked)
5036
0
    HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
5037
  /* run time DNS/SRV resolution was not active for this server
5038
   * and we can't enable it at run time for now.
5039
   */
5040
0
  if (!srv->resolv_requester && !srv->srvrq)
5041
0
    goto err;
5042
5043
0
  chunk_reset(&trash);
5044
0
  hostname_len    = strlen(hostname);
5045
0
  hostname_dn     = trash.area;
5046
0
  hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
5047
0
                                           hostname_dn, trash.size);
5048
0
  if (hostname_dn_len == -1)
5049
0
    goto err;
5050
5051
0
  resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
5052
0
  if (resolution &&
5053
0
      resolution->hostname_dn &&
5054
0
      resolution->hostname_dn_len == hostname_dn_len &&
5055
0
      strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
5056
0
    goto end;
5057
5058
0
  resolv_unlink_resolution(srv->resolv_requester);
5059
5060
0
  free(srv->hostname);
5061
0
  free(srv->hostname_dn);
5062
0
  srv->hostname        = strdup(hostname);
5063
0
  srv->hostname_dn     = strdup(hostname_dn);
5064
0
  srv->hostname_dn_len = hostname_dn_len;
5065
0
  if (!srv->hostname || !srv->hostname_dn)
5066
0
    goto err;
5067
5068
0
  if (srv->flags & SRV_F_NO_RESOLUTION)
5069
0
    goto end;
5070
5071
0
  if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
5072
0
    goto err;
5073
5074
0
  end:
5075
0
  if (!resolv_locked)
5076
0
    HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
5077
0
  return 0;
5078
5079
0
  err:
5080
0
  if (!resolv_locked)
5081
0
    HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
5082
0
  return -1;
5083
0
}
5084
5085
/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
5086
 * from the state file. This is suited for initial address configuration.
5087
 * Returns 0 on success otherwise a non-zero error code. In case of error,
5088
 * *err_code, if not NULL, is filled up.
5089
 */
5090
static int srv_apply_lastaddr(struct server *srv, int *err_code)
5091
0
{
5092
0
  struct sockaddr_storage new_addr;
5093
5094
0
  memset(&new_addr, 0, sizeof(new_addr));
5095
5096
  /* Use the preferred family, if configured */
5097
0
  new_addr.ss_family = srv->addr.ss_family;
5098
0
  if (!str2ip2(srv->lastaddr, &new_addr, 0)) {
5099
0
    if (err_code)
5100
0
      *err_code |= ERR_WARN;
5101
0
    return 1;
5102
0
  }
5103
0
  _srv_set_inetaddr(srv, &new_addr);
5104
0
  return 0;
5105
0
}
5106
5107
/* returns 0 if no error, otherwise a combination of ERR_* flags */
5108
static int srv_iterate_initaddr(struct server *srv)
5109
0
{
5110
0
  char *name = srv->hostname;
5111
0
  int return_code = 0;
5112
0
  int err_code;
5113
0
  unsigned int methods;
5114
5115
  /* If no addr and no hostname set, get the name from the DNS SRV request */
5116
0
  if (!name && srv->srvrq)
5117
0
    name = srv->srvrq->name;
5118
5119
0
  methods = srv->init_addr_methods;
5120
0
  if (!methods) {
5121
    /* otherwise default to "last,libc" */
5122
0
    srv_append_initaddr(&methods, SRV_IADDR_LAST);
5123
0
    srv_append_initaddr(&methods, SRV_IADDR_LIBC);
5124
0
    if (srv->resolvers_id) {
5125
      /* dns resolution is configured, add "none" to not fail on startup */
5126
0
      srv_append_initaddr(&methods, SRV_IADDR_NONE);
5127
0
    }
5128
0
  }
5129
5130
  /* "-dr" : always append "none" so that server addresses resolution
5131
   * failures are silently ignored, this is convenient to validate some
5132
   * configs out of their environment.
5133
   */
5134
0
  if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
5135
0
    srv_append_initaddr(&methods, SRV_IADDR_NONE);
5136
5137
0
  while (methods) {
5138
0
    err_code = 0;
5139
0
    switch (srv_get_next_initaddr(&methods)) {
5140
0
    case SRV_IADDR_LAST:
5141
0
      if (!srv->lastaddr)
5142
0
        continue;
5143
0
      if (srv_apply_lastaddr(srv, &err_code) == 0)
5144
0
        goto out;
5145
0
      return_code |= err_code;
5146
0
      break;
5147
5148
0
    case SRV_IADDR_LIBC:
5149
0
      if (!srv->hostname)
5150
0
        continue;
5151
0
      if (srv_set_addr_via_libc(srv, &err_code) == 0)
5152
0
        goto out;
5153
0
      return_code |= err_code;
5154
0
      break;
5155
5156
0
    case SRV_IADDR_NONE:
5157
0
      srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE);
5158
0
      if (return_code) {
5159
0
        ha_notice("could not resolve address '%s', disabling server.\n",
5160
0
            name);
5161
0
      }
5162
0
      return return_code;
5163
5164
0
    case SRV_IADDR_IP:
5165
0
      _srv_set_inetaddr(srv, &srv->init_addr);
5166
0
      if (return_code) {
5167
0
        ha_notice("could not resolve address '%s', falling back to configured address.\n",
5168
0
            name);
5169
0
      }
5170
0
      goto out;
5171
5172
0
    default: /* unhandled method */
5173
0
      break;
5174
0
    }
5175
0
  }
5176
5177
0
  if (!return_code)
5178
0
    ha_alert("no method found to resolve address '%s'.\n", name);
5179
0
  else
5180
0
    ha_alert("could not resolve address '%s'.\n", name);
5181
5182
0
  return_code |= ERR_ALERT | ERR_FATAL;
5183
0
  return return_code;
5184
0
out:
5185
0
  srv_set_dyncookie(srv);
5186
0
  srv_set_addr_desc(srv, 1);
5187
0
  return return_code;
5188
0
}
5189
5190
/*
5191
 * This function parses all backends and all servers within each backend
5192
 * and performs servers' addr resolution based on information provided by:
5193
 *   - configuration file
5194
 *   - server-state file (states provided by an 'old' haproxy process)
5195
 *
5196
 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
5197
 */
5198
int srv_init_addr(void)
5199
0
{
5200
0
  struct proxy *curproxy;
5201
0
  int return_code = 0;
5202
5203
0
  curproxy = proxies_list;
5204
0
  while (curproxy) {
5205
0
    struct server *srv;
5206
5207
    /* servers are in backend only */
5208
0
    if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
5209
0
      goto srv_init_addr_next;
5210
5211
0
    for (srv = curproxy->srv; srv; srv = srv->next) {
5212
0
      set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
5213
0
      if (srv->hostname || srv->srvrq)
5214
0
        return_code |= srv_iterate_initaddr(srv);
5215
0
      reset_usermsgs_ctx();
5216
0
    }
5217
5218
0
 srv_init_addr_next:
5219
0
    curproxy = curproxy->next;
5220
0
  }
5221
5222
0
  return return_code;
5223
0
}
5224
5225
/*
5226
 * Must be called with the server lock held.
5227
 */
5228
const char *srv_update_fqdn(struct server *server, const char *fqdn, const char *updater, int resolv_locked)
5229
0
{
5230
5231
0
  struct buffer *msg;
5232
5233
0
  msg = get_trash_chunk();
5234
0
  chunk_reset(msg);
5235
5236
0
  if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
5237
0
    chunk_appendf(msg, "no need to change the FDQN");
5238
0
    goto out;
5239
0
  }
5240
5241
0
  if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
5242
0
    chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
5243
0
    goto out;
5244
0
  }
5245
5246
0
  chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
5247
0
                server->proxy->id, server->id, server->hostname, fqdn);
5248
5249
0
  if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
5250
0
    chunk_reset(msg);
5251
0
    chunk_appendf(msg, "could not update %s/%s FQDN",
5252
0
                  server->proxy->id, server->id);
5253
0
    goto out;
5254
0
  }
5255
5256
  /* Flag as FQDN changed (e.g.: set from stats socket or resolvers) */
5257
0
  server->next_admin |= SRV_ADMF_FQDN_CHANGED;
5258
5259
0
 out:
5260
0
  if (updater)
5261
0
    chunk_appendf(msg, " by '%s'", updater);
5262
0
  chunk_appendf(msg, "\n");
5263
5264
0
  return msg->area;
5265
0
}
5266
5267
5268
/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
5269
 * and returns the pointer to the server. Otherwise, display adequate error messages
5270
 * on the CLI, sets the CLI's state to CLI_ST_PRINT and returns NULL. This is only
5271
 * used for CLI commands requiring a server name.
5272
 * Important: the <arg> is modified to remove the '/'.
5273
 */
5274
struct server *cli_find_server(struct appctx *appctx, char *arg)
5275
0
{
5276
0
  struct proxy *px;
5277
0
  struct server *sv;
5278
0
  struct ist be_name, sv_name = ist(arg);
5279
5280
0
  be_name = istsplit(&sv_name, '/');
5281
0
  if (!istlen(sv_name)) {
5282
0
    cli_err(appctx, "Require 'backend/server'.\n");
5283
0
    return NULL;
5284
0
  }
5285
5286
0
  if (!(px = proxy_be_by_name(ist0(be_name)))) {
5287
0
    cli_err(appctx, "No such backend.\n");
5288
0
    return NULL;
5289
0
  }
5290
0
  if (!(sv = server_find_by_name(px, ist0(sv_name)))) {
5291
0
    cli_err(appctx, "No such server.\n");
5292
0
    return NULL;
5293
0
  }
5294
5295
0
  if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
5296
0
    cli_err(appctx, "Proxy is disabled.\n");
5297
0
    return NULL;
5298
0
  }
5299
5300
0
  return sv;
5301
0
}
5302
5303
5304
/* grabs the server lock */
5305
static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
5306
0
{
5307
0
  struct server *sv;
5308
0
  const char *warning;
5309
5310
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5311
0
    return 1;
5312
5313
0
  sv = cli_find_server(appctx, args[2]);
5314
0
  if (!sv)
5315
0
    return 1;
5316
5317
0
  if (strcmp(args[3], "weight") == 0) {
5318
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5319
0
    warning = server_parse_weight_change_request(sv, args[4]);
5320
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5321
0
    if (warning)
5322
0
      cli_err(appctx, warning);
5323
0
  }
5324
0
  else if (strcmp(args[3], "state") == 0) {
5325
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5326
0
    if (strcmp(args[4], "ready") == 0)
5327
0
      srv_adm_set_ready(sv);
5328
0
    else if (strcmp(args[4], "drain") == 0)
5329
0
      srv_adm_set_drain(sv);
5330
0
    else if (strcmp(args[4], "maint") == 0)
5331
0
      srv_adm_set_maint(sv);
5332
0
    else
5333
0
      cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
5334
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5335
0
  }
5336
0
  else if (strcmp(args[3], "health") == 0) {
5337
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5338
0
    if (sv->track)
5339
0
      cli_err(appctx, "cannot change health on a tracking server.\n");
5340
0
    else if (strcmp(args[4], "up") == 0) {
5341
0
      sv->check.health = sv->check.rise + sv->check.fall - 1;
5342
0
      srv_set_running(sv, SRV_OP_STCHGC_CLI);
5343
0
    }
5344
0
    else if (strcmp(args[4], "stopping") == 0) {
5345
0
      sv->check.health = sv->check.rise + sv->check.fall - 1;
5346
0
      srv_set_stopping(sv, SRV_OP_STCHGC_CLI);
5347
0
    }
5348
0
    else if (strcmp(args[4], "down") == 0) {
5349
0
      sv->check.health = 0;
5350
0
      srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
5351
0
    }
5352
0
    else
5353
0
      cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
5354
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5355
0
  }
5356
0
  else if (strcmp(args[3], "agent") == 0) {
5357
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5358
0
    if (!(sv->agent.state & CHK_ST_ENABLED))
5359
0
      cli_err(appctx, "agent checks are not enabled on this server.\n");
5360
0
    else if (strcmp(args[4], "up") == 0) {
5361
0
      sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5362
0
      srv_set_running(sv, SRV_OP_STCHGC_CLI);
5363
0
    }
5364
0
    else if (strcmp(args[4], "down") == 0) {
5365
0
      sv->agent.health = 0;
5366
0
      srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
5367
0
    }
5368
0
    else
5369
0
      cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
5370
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5371
0
  }
5372
0
  else if (strcmp(args[3], "agent-addr") == 0) {
5373
0
    char *addr = NULL;
5374
0
    char *port = NULL;
5375
0
    if (strlen(args[4]) == 0) {
5376
0
      cli_err(appctx, "set server <b>/<s> agent-addr requires"
5377
0
          " an address and optionally a port.\n");
5378
0
      goto out;
5379
0
    }
5380
0
    addr = args[4];
5381
0
    if (strcmp(args[5], "port") == 0)
5382
0
      port = args[6];
5383
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5384
0
    warning = srv_update_agent_addr_port(sv, addr, port);
5385
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5386
0
    if (warning)
5387
0
      cli_msg(appctx, LOG_WARNING, warning);
5388
0
  }
5389
0
  else if (strcmp(args[3], "agent-port") == 0) {
5390
0
    char *port = NULL;
5391
0
    if (strlen(args[4]) == 0) {
5392
0
      cli_err(appctx, "set server <b>/<s> agent-port requires"
5393
0
          " a port.\n");
5394
0
      goto out;
5395
0
    }
5396
0
    port = args[4];
5397
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5398
0
    warning = srv_update_agent_addr_port(sv, NULL, port);
5399
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5400
0
    if (warning)
5401
0
      cli_msg(appctx, LOG_WARNING, warning);
5402
0
  }
5403
0
  else if (strcmp(args[3], "agent-send") == 0) {
5404
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5405
0
    if (!(sv->agent.state & CHK_ST_ENABLED))
5406
0
      cli_err(appctx, "agent checks are not enabled on this server.\n");
5407
0
    else {
5408
0
      if (!set_srv_agent_send(sv, args[4]))
5409
0
        cli_err(appctx, "cannot allocate memory for new string.\n");
5410
0
    }
5411
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5412
0
  }
5413
0
  else if (strcmp(args[3], "check-addr") == 0) {
5414
0
    char *addr = NULL;
5415
0
    char *port = NULL;
5416
0
    if (strlen(args[4]) == 0) {
5417
0
      cli_err(appctx, "set server <b>/<s> check-addr requires"
5418
0
          " an address and optionally a port.\n");
5419
0
      goto out;
5420
0
    }
5421
0
    addr = args[4];
5422
0
    if (strcmp(args[5], "port") == 0)
5423
0
      port = args[6];
5424
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5425
0
    warning = srv_update_check_addr_port(sv, addr, port);
5426
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5427
0
    if (warning)
5428
0
      cli_msg(appctx, LOG_WARNING, warning);
5429
0
  }
5430
0
  else if (strcmp(args[3], "check-port") == 0) {
5431
0
    char *port = NULL;
5432
0
    if (strlen(args[4]) == 0) {
5433
0
      cli_err(appctx, "set server <b>/<s> check-port requires"
5434
0
          " a port.\n");
5435
0
      goto out;
5436
0
    }
5437
0
    port = args[4];
5438
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5439
0
    warning = srv_update_check_addr_port(sv, NULL, port);
5440
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5441
0
    if (warning)
5442
0
      cli_msg(appctx, LOG_WARNING, warning);
5443
0
  }
5444
0
  else if (strcmp(args[3], "addr") == 0) {
5445
0
    char *addr = NULL;
5446
0
    char *port = NULL;
5447
0
    if (strlen(args[4]) == 0) {
5448
0
      cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
5449
0
      goto out;
5450
0
    }
5451
0
    else {
5452
0
      addr = args[4];
5453
0
    }
5454
0
    if (strcmp(args[5], "port") == 0) {
5455
0
      port = args[6];
5456
0
    }
5457
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5458
0
    warning = srv_update_addr_port(sv, addr, port, SERVER_INETADDR_UPDATER_CLI);
5459
0
    if (warning)
5460
0
      cli_msg(appctx, LOG_WARNING, warning);
5461
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5462
0
  }
5463
0
  else if (strcmp(args[3], "fqdn") == 0) {
5464
0
    if (!*args[4]) {
5465
0
      cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
5466
0
      goto out;
5467
0
    }
5468
0
    if (!sv->resolvers) {
5469
0
      cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
5470
0
      goto out;
5471
0
    }
5472
0
    if (sv->srvrq) {
5473
0
      cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
5474
0
      goto out;
5475
0
    }
5476
0
    HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
5477
0
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5478
    /* ensure runtime resolver will process this new fqdn */
5479
0
    if (sv->flags & SRV_F_NO_RESOLUTION) {
5480
0
      sv->flags &= ~SRV_F_NO_RESOLUTION;
5481
0
    }
5482
0
    warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
5483
0
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5484
0
    HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
5485
0
    if (warning)
5486
0
      cli_msg(appctx, LOG_WARNING, warning);
5487
0
  }
5488
0
  else if (strcmp(args[3], "ssl") == 0) {
5489
#ifdef USE_OPENSSL
5490
    if (sv->flags & SRV_F_DYNAMIC) {
5491
      cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
5492
      goto out;
5493
    }
5494
5495
    if (sv->ssl_ctx.ctx == NULL) {
5496
      cli_err(appctx, "'set server <srv> ssl' cannot be set. "
5497
          " default-server should define ssl settings\n");
5498
      goto out;
5499
    }
5500
5501
    HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5502
    if (strcmp(args[4], "on") == 0) {
5503
      srv_set_ssl(sv, 1);
5504
    } else if (strcmp(args[4], "off") == 0) {
5505
      srv_set_ssl(sv, 0);
5506
    } else {
5507
      HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5508
      cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
5509
      goto out;
5510
    }
5511
    srv_cleanup_connections(sv);
5512
    HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5513
    cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
5514
#else
5515
0
    cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
5516
0
#endif
5517
0
  } else {
5518
0
    cli_err(appctx,
5519
0
      "usage: set server <backend>/<server> "
5520
0
      "addr | agent | agent-addr | agent-port | agent-send | "
5521
0
      "check-addr | check-port | fqdn | health | ssl | "
5522
0
      "state | weight\n");
5523
0
  }
5524
0
 out:
5525
0
  return 1;
5526
0
}
5527
5528
static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
5529
0
{
5530
0
  struct proxy *be;
5531
0
  struct server *sv;
5532
0
  struct ist be_name, sv_name = ist(args[2]);
5533
5534
0
  be_name = istsplit(&sv_name, '/');
5535
0
  if (!istlen(sv_name))
5536
0
    return cli_err(appctx, "Require 'backend/server'.\n");
5537
5538
0
  if (!(be = proxy_be_by_name(ist0(be_name))))
5539
0
    return cli_err(appctx, "No such backend.\n");
5540
0
  if (!(sv = server_find_by_name(be, ist0(sv_name))))
5541
0
    return cli_err(appctx, "No such server.\n");
5542
5543
  /* return server's effective weight at the moment */
5544
0
  snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
5545
0
     sv->iweight);
5546
0
  if (applet_putstr(appctx, trash.area) == -1)
5547
0
    return 0;
5548
0
  return 1;
5549
0
}
5550
5551
/* Parse a "set weight" command.
5552
 *
5553
 * Grabs the server lock.
5554
 */
5555
static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
5556
0
{
5557
0
  struct server *sv;
5558
0
  const char *warning;
5559
5560
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5561
0
    return 1;
5562
5563
0
  sv = cli_find_server(appctx, args[2]);
5564
0
  if (!sv)
5565
0
    return 1;
5566
5567
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5568
5569
0
  warning = server_parse_weight_change_request(sv, args[3]);
5570
0
  if (warning)
5571
0
    cli_err(appctx, warning);
5572
5573
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5574
5575
0
  return 1;
5576
0
}
5577
5578
/* parse a "set maxconn server" command. It always returns 1.
5579
 *
5580
 * Grabs the server lock.
5581
 */
5582
static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
5583
0
{
5584
0
  struct server *sv;
5585
0
  const char *warning;
5586
5587
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5588
0
    return 1;
5589
5590
0
  sv = cli_find_server(appctx, args[3]);
5591
0
  if (!sv)
5592
0
    return 1;
5593
5594
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5595
5596
0
  warning = server_parse_maxconn_change_request(sv, args[4]);
5597
0
  if (warning)
5598
0
    cli_err(appctx, warning);
5599
5600
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5601
5602
0
  return 1;
5603
0
}
5604
5605
/* parse a "disable agent" command. It always returns 1.
5606
 *
5607
 * Grabs the server lock.
5608
 */
5609
static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
5610
0
{
5611
0
  struct server *sv;
5612
5613
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5614
0
    return 1;
5615
5616
0
  sv = cli_find_server(appctx, args[2]);
5617
0
  if (!sv)
5618
0
    return 1;
5619
5620
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5621
0
  sv->agent.state &= ~CHK_ST_ENABLED;
5622
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5623
0
  return 1;
5624
0
}
5625
5626
/* parse a "disable health" command. It always returns 1.
5627
 *
5628
 * Grabs the server lock.
5629
 */
5630
static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
5631
0
{
5632
0
  struct server *sv;
5633
5634
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5635
0
    return 1;
5636
5637
0
  sv = cli_find_server(appctx, args[2]);
5638
0
  if (!sv)
5639
0
    return 1;
5640
5641
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5642
0
  sv->check.state &= ~CHK_ST_ENABLED;
5643
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5644
0
  return 1;
5645
0
}
5646
5647
/* parse a "disable server" command. It always returns 1.
5648
 *
5649
 * Grabs the server lock.
5650
 */
5651
static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
5652
0
{
5653
0
  struct server *sv;
5654
5655
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5656
0
    return 1;
5657
5658
0
  sv = cli_find_server(appctx, args[2]);
5659
0
  if (!sv)
5660
0
    return 1;
5661
5662
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5663
0
  srv_adm_set_maint(sv);
5664
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5665
0
  return 1;
5666
0
}
5667
5668
/* parse a "enable agent" command. It always returns 1.
5669
 *
5670
 * Grabs the server lock.
5671
 */
5672
static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
5673
0
{
5674
0
  struct server *sv;
5675
5676
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5677
0
    return 1;
5678
5679
0
  sv = cli_find_server(appctx, args[2]);
5680
0
  if (!sv)
5681
0
    return 1;
5682
5683
0
  if (!(sv->agent.state & CHK_ST_CONFIGURED))
5684
0
    return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
5685
5686
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5687
0
  sv->agent.state |= CHK_ST_ENABLED;
5688
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5689
0
  return 1;
5690
0
}
5691
5692
/* parse a "enable health" command. It always returns 1.
5693
 *
5694
 * Grabs the server lock.
5695
 */
5696
static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
5697
0
{
5698
0
  struct server *sv;
5699
5700
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5701
0
    return 1;
5702
5703
0
  sv = cli_find_server(appctx, args[2]);
5704
0
  if (!sv)
5705
0
    return 1;
5706
5707
0
  if (!(sv->check.state & CHK_ST_CONFIGURED))
5708
0
    return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
5709
5710
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5711
0
  sv->check.state |= CHK_ST_ENABLED;
5712
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5713
0
  return 1;
5714
0
}
5715
5716
/* parse a "enable server" command. It always returns 1.
5717
 *
5718
 * Grabs the server lock.
5719
 */
5720
static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
5721
0
{
5722
0
  struct server *sv;
5723
5724
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5725
0
    return 1;
5726
5727
0
  sv = cli_find_server(appctx, args[2]);
5728
0
  if (!sv)
5729
0
    return 1;
5730
5731
0
  HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
5732
0
  srv_adm_set_ready(sv);
5733
0
  if (!(sv->flags & SRV_F_COOKIESET)
5734
0
      && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
5735
0
      sv->cookie)
5736
0
    srv_check_for_dup_dyncookie(sv);
5737
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
5738
0
  return 1;
5739
0
}
5740
5741
/* Allocates data structure related to load balancing for the server <sv>. It
5742
 * is only required for dynamic servers.
5743
 *
5744
 * At the moment, the server lock is not used as this function is only called
5745
 * for a dynamic server not yet registered.
5746
 *
5747
 * Returns 1 on success, 0 on allocation failure.
5748
 */
5749
static int srv_alloc_lb(struct server *sv, struct proxy *be)
5750
0
{
5751
0
  int node;
5752
5753
0
  sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
5754
0
                &be->lbprm.chash.bck : &be->lbprm.chash.act;
5755
0
  sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
5756
0
  sv->lb_nodes_now = 0;
5757
5758
0
  if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
5759
0
      ((be->lbprm.algo & (BE_LB_KIND | BE_LB_HASH_TYPE)) == (BE_LB_KIND_HI | BE_LB_HASH_CONS))) {
5760
0
    sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
5761
5762
0
    if (!sv->lb_nodes)
5763
0
      return 0;
5764
5765
0
    for (node = 0; node < sv->lb_nodes_tot; node++) {
5766
0
      sv->lb_nodes[node].server = sv;
5767
0
      sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
5768
0
    }
5769
0
  }
5770
5771
0
  return 1;
5772
0
}
5773
5774
/* updates the server's weight during a warmup stage. Once the final weight is
5775
 * reached, the task automatically stops. Note that any server status change
5776
 * must have updated s->counters.last_change accordingly.
5777
 */
5778
static struct task *server_warmup(struct task *t, void *context, unsigned int state)
5779
0
{
5780
0
  struct server *s = context;
5781
5782
  /* by default, plan on stopping the task */
5783
0
  t->expire = TICK_ETERNITY;
5784
0
  if ((s->next_admin & SRV_ADMF_MAINT) ||
5785
0
      (s->next_state != SRV_ST_STARTING))
5786
0
    return t;
5787
5788
0
  HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
5789
5790
  /* recalculate the weights and update the state */
5791
0
  server_recalc_eweight(s, 1);
5792
5793
  /* probably that we can refill this server with a bit more connections */
5794
0
  process_srv_queue(s);
5795
5796
0
  HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
5797
5798
  /* get back there in 1 second or 1/20th of the slowstart interval,
5799
   * whichever is greater, resulting in small 5% steps.
5800
   */
5801
0
  if (s->next_state == SRV_ST_STARTING)
5802
0
    t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
5803
0
  return t;
5804
0
}
5805
5806
/* Allocate the slowstart task if the server is configured with a slowstart
5807
 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
5808
 *
5809
 * Returns 0 on success else non-zero.
5810
 */
5811
static int init_srv_slowstart(struct server *srv)
5812
0
{
5813
0
  struct task *t;
5814
5815
0
  if (srv->slowstart) {
5816
0
    if ((t = task_new_anywhere()) == NULL) {
5817
0
      ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
5818
0
      return ERR_ALERT | ERR_FATAL;
5819
0
    }
5820
5821
    /* We need a warmup task that will be called when the server
5822
     * state switches from down to up.
5823
     */
5824
0
    srv->warmup = t;
5825
0
    t->process = server_warmup;
5826
0
    t->context = srv;
5827
5828
    /* server can be in this state only because of */
5829
0
    if (srv->next_state == SRV_ST_STARTING) {
5830
0
      task_schedule(srv->warmup,
5831
0
                    tick_add(now_ms,
5832
0
                             MS_TO_TICKS(MAX(1000, (ns_to_sec(now_ns) - COUNTERS_SHARED_LAST(srv->proxy->be_counters.shared->tg, last_change))) / 20)));
5833
0
    }
5834
0
  }
5835
5836
0
  return ERR_NONE;
5837
0
}
5838
5839
/* allocate the tasklet that's meant to permit a server */
5840
static int init_srv_requeue(struct server *srv)
5841
0
{
5842
0
  struct tasklet *t;
5843
5844
0
  if ((t = tasklet_new()) == NULL) {
5845
0
    ha_alert("Cannot allocate a server requeuing tasklet for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
5846
0
    return ERR_ALERT | ERR_FATAL;
5847
0
  }
5848
5849
0
  srv->requeue_tasklet = t;
5850
0
  t->process = server_requeue;
5851
0
  t->context = srv;
5852
0
  return ERR_NONE;
5853
0
}
5854
5855
/* Memory allocation and initialization of the per_thr field.
5856
 * Returns 0 if the field has been successfully initialized, -1 on failure.
5857
 */
5858
static int srv_init_per_thr(struct server *srv)
5859
0
{
5860
0
  int i;
5861
5862
0
  srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
5863
0
  srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
5864
0
  if (!srv->per_thr || !srv->per_tgrp)
5865
0
    return -1;
5866
5867
0
  for (i = 0; i < global.nbthread; i++) {
5868
0
    srv->per_thr[i].idle_conns = EB_ROOT;
5869
0
    srv->per_thr[i].safe_conns = EB_ROOT;
5870
0
    srv->per_thr[i].avail_conns = EB_ROOT;
5871
0
    MT_LIST_INIT(&srv->per_thr[i].streams);
5872
5873
0
    LIST_INIT(&srv->per_thr[i].idle_conn_list);
5874
0
  }
5875
5876
0
  for (i = 0; i < global.nbtgroups; i++) {
5877
0
    srv->per_tgrp[i].server = srv;
5878
0
    queue_init(&srv->per_tgrp[i].queue, srv->proxy, srv);
5879
0
  }
5880
5881
0
  return 0;
5882
0
}
5883
5884
/* Distinguish between "add server" default usage or one of its sub-commands. */
5885
enum add_srv_mode {
5886
  ADD_SRV_MODE_DEF,  /* default mode, IO handler should be skipped by parser. */
5887
  ADD_SRV_MODE_HELP, /* help mode to list supported keywords */
5888
};
5889
5890
/* Context for "add server" CLI. */
5891
struct add_srv_ctx {
5892
  enum add_srv_mode mode;
5893
  void *obj1;
5894
  void *obj2;
5895
};
5896
5897
/* Handler for "add server" command. Should be reserved to extra sub-commands
5898
 * such as "help".
5899
 */
5900
int cli_io_handler_add_server(struct appctx *appctx)
5901
0
{
5902
0
  struct add_srv_ctx *ctx = appctx->svcctx;
5903
0
  struct srv_kw_list *kwl = ctx->obj1;
5904
0
  struct srv_kw *kw;
5905
5906
0
  switch (ctx->mode) {
5907
0
  case ADD_SRV_MODE_HELP:
5908
0
    if (!kwl) {
5909
      /* first invocation */
5910
0
      if (applet_putstr(appctx, "List of keywords supported for dynamic server:\n") < 0)
5911
0
        return cli_err(appctx, "output error");
5912
5913
0
      kwl = LIST_NEXT(&srv_keywords.list, struct srv_kw_list *, list);
5914
0
      ctx->obj1 = kwl;
5915
0
      ctx->obj2 = kwl->kw;
5916
0
    }
5917
5918
0
    while (kwl != &srv_keywords) {
5919
0
      for (kw = ctx->obj2; kw->kw; ++kw) {
5920
0
        if (!kw->dynamic_ok)
5921
0
          continue;
5922
5923
0
        ctx->obj2 = kw;
5924
0
        chunk_reset(&trash);
5925
0
        chunk_printf(&trash, "%s\n", kw->kw);
5926
0
        if (STRESS_RUN1(applet_putchk_stress(appctx, &trash) == -1,
5927
0
                        applet_putchk(appctx, &trash) == -1)) {
5928
0
          goto full;
5929
0
        }
5930
0
      }
5931
5932
0
      kwl = LIST_NEXT(&kwl->list, struct srv_kw_list *, list);
5933
0
      ctx->obj1 = kwl;
5934
0
      ctx->obj2 = kwl->kw;
5935
0
    }
5936
0
    break;
5937
5938
0
  case ADD_SRV_MODE_DEF:
5939
    /* Add srv parser must return 1 to prevent I/O handler execution in default mode. */
5940
0
    ABORT_NOW();
5941
0
    break;
5942
0
  }
5943
5944
0
  return 1;
5945
5946
0
 full:
5947
0
  return 0;
5948
0
}
5949
5950
/* Parse a "add server" command.
5951
 *
5952
 * Returns 1 to skip I/O handler processing, unless a sub-command is executed.
5953
 */
5954
static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
5955
0
{
5956
0
  struct add_srv_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
5957
0
  struct proxy *be;
5958
0
  struct server *srv;
5959
0
  char *be_name, *sv_name;
5960
0
  int errcode, argc;
5961
0
  int next_id;
5962
0
  const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
5963
5964
0
  usermsgs_clr("CLI");
5965
5966
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5967
0
    return 1;
5968
5969
0
  ++args;
5970
5971
0
  if (strcmp(args[1], "help") == 0) {
5972
0
    ctx->mode = ADD_SRV_MODE_HELP;
5973
0
    ctx->obj2 = ctx->obj1 = NULL;
5974
0
    return 0;
5975
0
  }
5976
5977
0
  ctx->mode = ADD_SRV_MODE_DEF;
5978
0
  sv_name = be_name = args[1];
5979
  /* split backend/server arg */
5980
0
  while (*sv_name && *(++sv_name)) {
5981
0
    if (*sv_name == '/') {
5982
0
      *sv_name = '\0';
5983
0
      ++sv_name;
5984
0
      break;
5985
0
    }
5986
0
  }
5987
5988
0
  if (!*sv_name)
5989
0
    return cli_err(appctx, "Require 'backend/server'.\n");
5990
5991
0
  be = proxy_be_by_name(be_name);
5992
0
  if (!be)
5993
0
    return cli_err(appctx, "No such backend.\n");
5994
5995
0
  if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
5996
0
    cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.\n");
5997
0
    return 1;
5998
0
  }
5999
6000
0
  if (be->mode == PR_MODE_SYSLOG) {
6001
0
    cli_err(appctx," Dynamic servers cannot be used with log backends.\n");
6002
0
    return 1;
6003
0
  }
6004
6005
  /* At this point, some operations might not be thread-safe anymore. This
6006
   * might be the case for parsing handlers which were designed to run
6007
   * only at the starting stage on single-thread mode.
6008
   *
6009
   * Activate thread isolation to ensure thread-safety.
6010
   */
6011
0
  thread_isolate();
6012
6013
  /*
6014
   * If a server with the same name is found, reject the new one.
6015
   */
6016
0
  if (server_find_by_name(be, sv_name)) {
6017
0
    thread_release();
6018
0
    cli_err(appctx, "Already exists a server with the same name in backend.\n");
6019
0
    return 1;
6020
0
  }
6021
6022
0
  args[1] = sv_name;
6023
0
  errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
6024
0
  if (errcode)
6025
0
    goto out;
6026
6027
0
  while (*args[argc]) {
6028
0
    errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
6029
6030
0
    if (errcode)
6031
0
      goto out;
6032
0
  }
6033
6034
0
  errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
6035
0
  if (errcode)
6036
0
    goto out;
6037
6038
  /* A dynamic server does not currently support resolution.
6039
   *
6040
   * Initialize it explicitly to the "none" method to ensure no
6041
   * resolution will ever be executed.
6042
   */
6043
0
  srv->init_addr_methods = SRV_IADDR_NONE;
6044
6045
0
  if (srv->mux_proto) {
6046
0
    int proto_mode = conn_pr_mode_to_proto_mode(be->mode);
6047
0
    const struct mux_proto_list *mux_ent;
6048
6049
0
    mux_ent = conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, proto_mode);
6050
6051
0
    if (!mux_ent || !isteq(mux_ent->token, srv->mux_proto->token)) {
6052
0
      ha_alert("MUX protocol is not usable for server.\n");
6053
0
      goto out;
6054
0
    }
6055
0
  }
6056
6057
0
  if (!srv_alloc_lb(srv, be)) {
6058
0
    ha_alert("Failed to initialize load-balancing data.\n");
6059
0
    goto out;
6060
0
  }
6061
6062
0
  if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
6063
0
                                              COUNTERS_SV,
6064
0
                                              STATS_PX_CAP_SRV)) {
6065
0
    ha_alert("failed to allocate extra counters for server.\n");
6066
0
    goto out;
6067
0
  }
6068
6069
  /* ensure minconn/maxconn consistency */
6070
0
  srv_minmax_conn_apply(srv);
6071
6072
0
  if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
6073
0
      srv->check.use_ssl == 1) {
6074
0
    if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
6075
0
      if (xprt_get(XPRT_SSL)->prepare_srv(srv))
6076
0
        goto out;
6077
0
    }
6078
0
  }
6079
6080
0
  if (srv->trackit) {
6081
0
    if (srv_apply_track(srv, be))
6082
0
      goto out;
6083
0
  }
6084
6085
  /* Init check/agent if configured. The check is manually disabled
6086
   * because a dynamic server is started in a disable state. It must be
6087
   * manually activated via a "enable health/agent" command.
6088
   */
6089
0
  if (srv->do_check) {
6090
0
    if (init_srv_check(srv))
6091
0
      goto out;
6092
6093
0
    srv->check.state &= ~CHK_ST_ENABLED;
6094
0
  }
6095
6096
0
  if (srv->do_agent) {
6097
0
    if (init_srv_agent_check(srv))
6098
0
      goto out;
6099
6100
0
    srv->agent.state &= ~CHK_ST_ENABLED;
6101
0
  }
6102
6103
0
  errcode = srv_init(srv);
6104
0
  if (errcode)
6105
0
    goto out;
6106
6107
  /* generate the server id if not manually specified */
6108
0
  if (!srv->puid) {
6109
0
    next_id = get_next_id(&be->conf.used_server_id, 1);
6110
0
    if (!next_id) {
6111
0
      ha_alert("Cannot attach server : no id left in proxy\n");
6112
0
      goto out;
6113
0
    }
6114
6115
0
    srv->conf.id.key = srv->puid = next_id;
6116
0
  }
6117
0
  srv->conf.name.key = srv->id;
6118
6119
  /* insert the server in the backend trees */
6120
0
  eb32_insert(&be->conf.used_server_id, &srv->conf.id);
6121
0
  ebis_insert(&be->conf.used_server_name, &srv->conf.name);
6122
  /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
6123
0
  if (srv->addr_node.key)
6124
0
    ebis_insert(&be->used_server_addr, &srv->addr_node);
6125
6126
  /* check if LSB bit (odd bit) is set for reuse_cnt */
6127
0
  if (srv_id_reuse_cnt & 1) {
6128
    /* cnt must be increased */
6129
0
    srv_id_reuse_cnt++;
6130
0
  }
6131
  /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
6132
   * save some space
6133
   * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
6134
   */
6135
0
  srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
6136
6137
  /* generate new server's dynamic cookie if enabled on backend */
6138
0
  if (be->ck_opts & PR_CK_DYNAMIC) {
6139
0
    srv_set_dyncookie(srv);
6140
0
  }
6141
6142
  /* adding server cannot fail when we reach this:
6143
   * publishing EVENT_HDL_SUB_SERVER_ADD
6144
   */
6145
0
  srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
6146
6147
0
  thread_release();
6148
6149
  /* Start the check task. The server must be fully initialized.
6150
   *
6151
   * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
6152
   * need to randomly spread the task interval for dynamic servers.
6153
   */
6154
0
  if (srv->check.state & CHK_ST_CONFIGURED) {
6155
0
    if (!start_check_task(&srv->check, 0, 1, 1))
6156
0
      ha_alert("System might be unstable, consider to execute a reload\n");
6157
0
  }
6158
0
  if (srv->agent.state & CHK_ST_CONFIGURED) {
6159
0
    if (!start_check_task(&srv->agent, 0, 1, 1))
6160
0
      ha_alert("System might be unstable, consider to execute a reload\n");
6161
0
  }
6162
6163
0
  if (srv->cklen && be->mode != PR_MODE_HTTP)
6164
0
    ha_warning("Ignoring cookie as HTTP mode is disabled.\n");
6165
6166
0
  ha_notice("New server registered.\n");
6167
0
  return cli_umsg(appctx, LOG_INFO);
6168
6169
0
out:
6170
0
  if (srv) {
6171
0
    if (srv->track)
6172
0
      release_server_track(srv);
6173
6174
0
    if (srv->check.state & CHK_ST_CONFIGURED) {
6175
0
      free_check(&srv->check);
6176
0
      srv_drop(srv);
6177
0
    }
6178
0
    if (srv->agent.state & CHK_ST_CONFIGURED) {
6179
0
      free_check(&srv->agent);
6180
0
      srv_drop(srv);
6181
0
    }
6182
6183
    /* remove the server from the proxy linked list */
6184
0
    srv_detach(srv);
6185
0
  }
6186
6187
0
  thread_release();
6188
6189
0
  if (!usermsgs_empty())
6190
0
    cli_umsgerr(appctx);
6191
6192
0
  if (srv)
6193
0
    srv_drop(srv);
6194
6195
0
  return 1;
6196
0
}
6197
6198
/* Check if the server <bename>/<svname> exists and is ready for being deleted.
6199
 * This means that the server is in maintenance with no streams attached to it,
6200
 * no queue and no used idle conns. This is not supposed to change during all
6201
 * the maintenance phase (except for force-persist etc, which are not covered).
6202
 * Both <bename> and <svname> must be valid strings. If pb/ps are not null,
6203
 * upon success, the pointer to the backend and server respectively will be put
6204
 * there. If pm is not null, a pointer to an error/success message is returned
6205
 * there (possibly NULL if nothing to say). Returned values:
6206
 *  >0 if OK
6207
 *   0 if not yet (should wait if it can)
6208
 *  <0 if not possible
6209
 */
6210
int srv_check_for_deletion(const char *bename, const char *svname, struct proxy **pb, struct server **ps, const char **pm)
6211
0
{
6212
0
  struct server *srv = NULL;
6213
0
  struct proxy *be = NULL;
6214
0
  const char *msg = NULL;
6215
0
  int ret;
6216
6217
  /* First, unrecoverable errors */
6218
0
  ret = -1;
6219
6220
0
  if (!(be = proxy_be_by_name(bename))) {
6221
0
    msg = "No such backend.";
6222
0
    goto leave;
6223
0
  }
6224
6225
0
  if (!(srv = server_find_by_name(be, svname))) {
6226
0
    msg = "No such server.";
6227
0
    goto leave;
6228
0
  }
6229
6230
0
  if (srv->flags & SRV_F_NON_PURGEABLE) {
6231
0
    msg = "This server cannot be removed at runtime due to other configuration elements pointing to it.";
6232
0
    goto leave;
6233
0
  }
6234
6235
  /* Only servers in maintenance can be deleted. This ensures that the
6236
   * server is not present anymore in the lb structures (through
6237
   * lbprm.set_server_status_down).
6238
   */
6239
0
  if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
6240
0
    msg = "Only servers in maintenance mode can be deleted.";
6241
0
    goto leave;
6242
0
  }
6243
6244
  /* Second, conditions that may change over time */
6245
0
  ret = 0;
6246
6247
  /* Ensure that there is no active/pending connection on the server. */
6248
0
  if (_HA_ATOMIC_LOAD(&srv->curr_used_conns) ||
6249
0
      _HA_ATOMIC_LOAD(&srv->queueslength) || srv_has_streams(srv)) {
6250
0
    msg = "Server still has connections attached to it, cannot remove it.";
6251
0
    goto leave;
6252
0
  }
6253
6254
  /* OK, let's go */
6255
0
  ret = 1;
6256
0
leave:
6257
0
  if (pb)
6258
0
    *pb = be;
6259
0
  if (ps)
6260
0
    *ps = srv;
6261
0
  if (pm)
6262
0
    *pm = msg;
6263
0
  return ret;
6264
0
}
6265
6266
/* Parse a "del server" command
6267
 * Returns 0 if the server has been successfully initialized, 1 on failure.
6268
 */
6269
static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
6270
0
{
6271
0
  struct proxy *be;
6272
0
  struct server *srv;
6273
0
  struct ist be_name, sv_name;
6274
0
  struct mt_list back;
6275
0
  struct sess_priv_conns *sess_conns = NULL;
6276
0
  struct watcher *srv_watch;
6277
0
  const char *msg;
6278
0
  int ret, i;
6279
6280
0
  if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
6281
0
    return 1;
6282
6283
0
  ++args;
6284
6285
  /* The proxy servers list is currently not protected by a lock so this
6286
   * requires thread isolation. In addition, any place referencing the
6287
   * server about to be deleted would be unsafe after our operation, so
6288
   * we must be certain to be alone so that no other thread has even
6289
   * started to grab a temporary reference to this server.
6290
   */
6291
0
  thread_isolate_full();
6292
6293
0
  sv_name = ist(args[1]);
6294
0
  be_name = istsplit(&sv_name, '/');
6295
0
  if (!istlen(sv_name)) {
6296
0
    cli_err(appctx, "Require 'backend/server'.\n");
6297
0
    goto out;
6298
0
  }
6299
6300
0
  ret = srv_check_for_deletion(ist0(be_name), ist0(sv_name), &be, &srv, &msg);
6301
0
  if (ret <= 0) {
6302
    /* failure (recoverable or not) */
6303
0
    cli_err(appctx, msg);
6304
0
    goto out;
6305
0
  }
6306
6307
  /* Close idle connections attached to this server. */
6308
0
  for (i = tid;;) {
6309
0
    struct list *list = &srv->per_thr[i].idle_conn_list;
6310
0
    struct connection *conn;
6311
6312
0
    while (!LIST_ISEMPTY(list)) {
6313
0
      conn = LIST_ELEM(list->n, struct connection *, idle_list);
6314
0
      if (i != tid) {
6315
0
        if (conn->mux && conn->mux->takeover)
6316
0
          conn->mux->takeover(conn, i, 1);
6317
0
        else if (conn->xprt && conn->xprt->takeover)
6318
0
          conn->xprt->takeover(conn, conn->ctx, i, 1);
6319
0
      }
6320
0
      conn_release(conn);
6321
0
    }
6322
6323
    /* Also remove all purgeable conns as some of them may still
6324
     * reference the currently deleted server.
6325
     */
6326
0
    while ((conn = MT_LIST_POP(&idle_conns[i].toremove_conns,
6327
0
                               struct connection *, toremove_list))) {
6328
0
      conn_release(conn);
6329
0
    }
6330
6331
0
    if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6332
0
      break;
6333
0
  }
6334
6335
  /* All idle connections should be removed now. */
6336
0
  BUG_ON(srv->curr_idle_conns);
6337
6338
  /* Close idle private connections attached to this server. */
6339
0
  MT_LIST_FOR_EACH_ENTRY_LOCKED(sess_conns, &srv->sess_conns, srv_el, back) {
6340
0
    struct connection *conn, *conn_back;
6341
0
    list_for_each_entry_safe(conn, conn_back, &sess_conns->conn_list, sess_el) {
6342
6343
      /* Only idle connections should be present if srv_check_for_deletion() is true. */
6344
0
      BUG_ON(!(conn->flags & CO_FL_SESS_IDLE));
6345
6346
0
      LIST_DEL_INIT(&conn->sess_el);
6347
0
      conn->owner = NULL;
6348
0
      conn->flags &= ~CO_FL_SESS_IDLE;
6349
0
      if (sess_conns->tid != tid) {
6350
0
        if (conn->mux && conn->mux->takeover)
6351
0
          conn->mux->takeover(conn, sess_conns->tid, 1);
6352
0
        else if (conn->xprt && conn->xprt->takeover)
6353
0
          conn->xprt->takeover(conn, conn->ctx, sess_conns->tid, 1);
6354
0
      }
6355
0
      conn_release(conn);
6356
0
    }
6357
6358
0
    LIST_DELETE(&sess_conns->sess_el);
6359
0
    pool_free(pool_head_sess_priv_conns, sess_conns);
6360
0
    sess_conns = NULL;
6361
0
  }
6362
6363
  /* removing cannot fail anymore when we reach this:
6364
   * publishing EVENT_HDL_SUB_SERVER_DEL
6365
   */
6366
0
  srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
6367
6368
  /* remove srv from tracking list */
6369
0
  if (srv->track)
6370
0
    release_server_track(srv);
6371
6372
  /* stop the check task if running */
6373
0
  if (srv->check.state & CHK_ST_CONFIGURED)
6374
0
    check_purge(&srv->check);
6375
0
  if (srv->agent.state & CHK_ST_CONFIGURED)
6376
0
    check_purge(&srv->agent);
6377
6378
0
  if (srv->proxy->lbprm.server_deinit)
6379
0
    srv->proxy->lbprm.server_deinit(srv);
6380
6381
0
  while (!MT_LIST_ISEMPTY(&srv->watcher_list)) {
6382
0
    srv_watch = MT_LIST_NEXT(&srv->watcher_list, struct watcher *, el);
6383
0
    BUG_ON(srv->next && srv->next->flags & SRV_F_DELETED);
6384
0
    watcher_next(srv_watch, srv->next);
6385
0
  }
6386
6387
  /* detach the server from the proxy linked list
6388
   * The proxy servers list is currently not protected by a lock, so this
6389
   * requires thread_isolate/release.
6390
   */
6391
0
  srv_detach(srv);
6392
6393
  /* remove srv from addr_node tree */
6394
0
  eb32_delete(&srv->conf.id);
6395
0
  ebpt_delete(&srv->conf.name);
6396
0
  ebpt_delete(&srv->addr_node);
6397
6398
  /* remove srv from idle_node tree for idle conn cleanup */
6399
0
  eb32_delete(&srv->idle_node);
6400
6401
  /* flag the server as deleted
6402
   * (despite the server being removed from primary server list,
6403
   * one could still access the server data from a valid ptr)
6404
   * Deleted flag helps detecting when a server is in transient removal
6405
   * state.
6406
   * ie: removed from the list but not yet freed/purged from memory.
6407
   */
6408
0
  srv->flags |= SRV_F_DELETED;
6409
6410
  /* set LSB bit (odd bit) for reuse_cnt */
6411
0
  srv_id_reuse_cnt |= 1;
6412
6413
0
  thread_release();
6414
6415
0
  ha_notice("Server deleted.\n");
6416
0
  srv_drop(srv);
6417
6418
0
  cli_msg(appctx, LOG_INFO, "Server deleted.\n");
6419
0
  return 0;
6420
6421
0
out:
6422
0
  thread_release();
6423
0
  return 1;
6424
0
}
6425
6426
/* register cli keywords */
6427
static struct cli_kw_list cli_kws = {{ },{
6428
  { { "disable", "agent",  NULL },         "disable agent                           : disable agent checks",                                        cli_parse_disable_agent, NULL },
6429
  { { "disable", "health",  NULL },        "disable health                          : disable health checks",                                       cli_parse_disable_health, NULL },
6430
  { { "disable", "server",  NULL },        "disable server (DEPRECATED)             : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL },
6431
  { { "enable", "agent",  NULL },          "enable agent                            : enable agent checks",                                         cli_parse_enable_agent, NULL },
6432
  { { "enable", "health",  NULL },         "enable health                           : enable health checks",                                        cli_parse_enable_health, NULL },
6433
  { { "enable", "server",  NULL },         "enable server  (DEPRECATED)             : enable a disabled server (use 'set server' instead)",         cli_parse_enable_server, NULL },
6434
  { { "set", "maxconn", "server",  NULL }, "set maxconn server <bk>/<srv>           : change a server's maxconn setting",                           cli_parse_set_maxconn_server, NULL },
6435
  { { "set", "server", NULL },             "set server <bk>/<srv> [opts]            : change a server's state, weight, address or ssl",             cli_parse_set_server },
6436
  { { "get", "weight", NULL },             "get weight <bk>/<srv>                   : report a server's current weight",                            cli_parse_get_weight },
6437
  { { "set", "weight", NULL },             "set weight <bk>/<srv>  (DEPRECATED)     : change a server's weight (use 'set server' instead)",         cli_parse_set_weight },
6438
  { { "add", "server", NULL },             "add server <bk>/<srv>                   : create a new server",                                         cli_parse_add_server, cli_io_handler_add_server },
6439
  { { "del", "server", NULL },             "del server <bk>/<srv>                   : remove a dynamically added server",                           cli_parse_delete_server, NULL },
6440
  {{},}
6441
}};
6442
6443
INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
6444
6445
/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
6446
 * field is used to retrieve the identifier of the tracked server, either with
6447
 * the format "proxy/server" or just "server". <curproxy> must point to the
6448
 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
6449
 * to find the tracked server.
6450
 *
6451
 * Returns 0 if the server track has been activated else non-zero.
6452
 *
6453
 * Not thread-safe.
6454
 */
6455
int srv_apply_track(struct server *srv, struct proxy *curproxy)
6456
0
{
6457
0
  struct proxy *px;
6458
0
  struct server *strack, *loop;
6459
0
  char *pname, *sname;
6460
6461
0
  if (!srv->trackit)
6462
0
    return 1;
6463
6464
0
  pname = srv->trackit;
6465
0
  sname = strrchr(pname, '/');
6466
6467
0
  if (sname) {
6468
0
    *sname++ = '\0';
6469
0
  }
6470
0
  else {
6471
0
    sname = pname;
6472
0
    pname = NULL;
6473
0
  }
6474
6475
0
  if (pname) {
6476
0
    px = proxy_be_by_name(pname);
6477
0
    if (!px) {
6478
0
      ha_alert("unable to find required proxy '%s' for tracking.\n",
6479
0
               pname);
6480
0
      return 1;
6481
0
    }
6482
0
  }
6483
0
  else {
6484
0
    px = curproxy;
6485
0
  }
6486
6487
0
  strack = findserver(px, sname);
6488
0
  if (!strack) {
6489
0
    ha_alert("unable to find required server '%s' for tracking.\n",
6490
0
             sname);
6491
0
    return 1;
6492
0
  }
6493
6494
0
  if (strack->flags & SRV_F_DYNAMIC) {
6495
0
    ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
6496
0
             px->id, strack->id);
6497
0
    return 1;
6498
0
  }
6499
6500
0
  if (!strack->do_check && !strack->do_agent && !strack->track &&
6501
0
      !strack->trackit) {
6502
0
    ha_alert("unable to use %s/%s for "
6503
0
             "tracking as it does not have any check nor agent enabled.\n",
6504
0
             px->id, strack->id);
6505
0
    return 1;
6506
0
  }
6507
6508
0
  for (loop = strack->track; loop && loop != srv; loop = loop->track)
6509
0
    ;
6510
6511
0
  if (srv == strack || loop) {
6512
0
    ha_alert("unable to track %s/%s as it "
6513
0
             "belongs to a tracking chain looping back to %s/%s.\n",
6514
0
             px->id, strack->id, px->id,
6515
0
             srv == strack ? strack->id : loop->id);
6516
0
    return 1;
6517
0
  }
6518
6519
0
  if (curproxy != px &&
6520
0
      (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
6521
0
    ha_alert("unable to use %s/%s for"
6522
0
             "tracking: disable-on-404 option inconsistency.\n",
6523
0
             px->id, strack->id);
6524
0
    return 1;
6525
0
  }
6526
6527
0
  srv->track = strack;
6528
0
  srv->tracknext = strack->trackers;
6529
0
  strack->trackers = srv;
6530
0
  strack->flags |= SRV_F_NON_PURGEABLE;
6531
6532
0
  ha_free(&srv->trackit);
6533
6534
0
  return 0;
6535
0
}
6536
6537
/* This function propagates srv state change to lb algorithms */
6538
static void srv_lb_propagate(struct server *s)
6539
0
{
6540
0
  struct proxy *px = s->proxy;
6541
6542
0
  if (px->lbprm.update_server_eweight)
6543
0
    px->lbprm.update_server_eweight(s);
6544
0
  else if (srv_willbe_usable(s)) {
6545
0
    if (px->lbprm.set_server_status_up)
6546
0
      px->lbprm.set_server_status_up(s);
6547
0
  }
6548
0
  else {
6549
0
    if (px->lbprm.set_server_status_down)
6550
0
      px->lbprm.set_server_status_down(s);
6551
0
  }
6552
0
}
6553
6554
/* directly update server state based on an operational change
6555
 * (compare current and next state to know which transition to apply)
6556
 *
6557
 * The function returns the number of requeued sessions (either taken by
6558
 * the server or redispatched to others servers) due to the server state
6559
 * change.
6560
 */
6561
static int _srv_update_status_op(struct server *s, enum srv_op_st_chg_cause cause)
6562
0
{
6563
0
  struct buffer *tmptrash = NULL;
6564
0
  int log_level;
6565
0
  int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
6566
0
  int xferred = 0;
6567
6568
0
  if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
6569
0
    srv_lb_propagate(s);
6570
6571
0
    if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
6572
0
      srv_shutdown_streams(s, SF_ERR_DOWN);
6573
6574
    /* we might have streams queued on this server and waiting for
6575
     * a connection. Those which are redispatchable will be queued
6576
     * to another server or to the proxy itself.
6577
     */
6578
0
    xferred = pendconn_redistribute(s);
6579
6580
0
    tmptrash = alloc_trash_chunk();
6581
0
    if (tmptrash) {
6582
0
      chunk_printf(tmptrash,
6583
0
                   "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
6584
0
                   s->proxy->id, s->id);
6585
6586
0
      srv_append_op_chg_cause(tmptrash, s, cause);
6587
0
      srv_append_more(tmptrash, s, xferred, 0);
6588
6589
0
      ha_warning("%s.\n", tmptrash->area);
6590
6591
      /* we don't send an alert if the server was previously paused */
6592
0
      log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
6593
0
      send_log(s->proxy, log_level, "%s.\n",
6594
0
         tmptrash->area);
6595
0
      send_email_alert(s, log_level, "%s",
6596
0
           tmptrash->area);
6597
0
      free_trash_chunk(tmptrash);
6598
0
    }
6599
0
  }
6600
0
  else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
6601
0
    srv_lb_propagate(s);
6602
6603
    /* we might have streams queued on this server and waiting for
6604
     * a connection. Those which are redispatchable will be queued
6605
     * to another server or to the proxy itself.
6606
     */
6607
0
    xferred = pendconn_redistribute(s);
6608
6609
0
    tmptrash = alloc_trash_chunk();
6610
0
    if (tmptrash) {
6611
0
      chunk_printf(tmptrash,
6612
0
                   "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
6613
0
                   s->proxy->id, s->id);
6614
6615
0
      srv_append_op_chg_cause(tmptrash, s, cause);
6616
0
      srv_append_more(tmptrash, s, xferred, 0);
6617
6618
0
      ha_warning("%s.\n", tmptrash->area);
6619
0
      send_log(s->proxy, LOG_NOTICE, "%s.\n",
6620
0
         tmptrash->area);
6621
0
      free_trash_chunk(tmptrash);
6622
0
    }
6623
0
  }
6624
0
  else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
6625
0
     || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
6626
6627
0
    if (s->next_state == SRV_ST_STARTING && s->warmup)
6628
0
      task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
6629
6630
0
    server_recalc_eweight(s, 0);
6631
    /* now propagate the status change to any LB algorithms */
6632
0
    srv_lb_propagate(s);
6633
6634
    /* If the server is set with "on-marked-up shutdown-backup-sessions",
6635
     * and it's not a backup server and its effective weight is > 0,
6636
     * then it can accept new connections, so we shut down all streams
6637
     * on all backup servers.
6638
     */
6639
0
    if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
6640
0
        !(s->flags & SRV_F_BACKUP) && s->next_eweight)
6641
0
      srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
6642
6643
    /* check if we can handle some connections queued.
6644
     * We will take as many as we can handle.
6645
     */
6646
0
    xferred = process_srv_queue(s);
6647
6648
0
    tmptrash = alloc_trash_chunk();
6649
0
    if (tmptrash) {
6650
0
      chunk_printf(tmptrash,
6651
0
                   "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
6652
0
                   s->proxy->id, s->id);
6653
6654
0
      srv_append_op_chg_cause(tmptrash, s, cause);
6655
0
      srv_append_more(tmptrash, s, xferred, 0);
6656
6657
0
      ha_warning("%s.\n", tmptrash->area);
6658
0
      send_log(s->proxy, LOG_NOTICE, "%s.\n",
6659
0
         tmptrash->area);
6660
0
      send_email_alert(s, LOG_NOTICE, "%s",
6661
0
           tmptrash->area);
6662
0
      free_trash_chunk(tmptrash);
6663
0
    }
6664
0
  }
6665
0
  else if (s->cur_eweight != s->next_eweight) {
6666
    /* now propagate the status change to any LB algorithms */
6667
0
    srv_lb_propagate(s);
6668
0
  }
6669
0
  return xferred;
6670
0
}
6671
6672
/* deduct and update server state from an administrative change
6673
 * (use current and next admin to deduct the administrative transition that
6674
 *  may result in server state update)
6675
 *
6676
 * The function returns the number of requeued sessions (either taken by
6677
 * the server or redispatched to others servers) due to the server state
6678
 * change.
6679
 */
6680
static int _srv_update_status_adm(struct server *s, enum srv_adm_st_chg_cause cause)
6681
0
{
6682
0
  struct buffer *tmptrash = NULL;
6683
0
  int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
6684
0
  int xferred = 0;
6685
6686
  /* Maintenance must also disable health checks */
6687
0
  if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
6688
0
    if (s->check.state & CHK_ST_ENABLED) {
6689
0
      s->check.state |= CHK_ST_PAUSED;
6690
0
      s->check.health = 0;
6691
0
    }
6692
6693
0
    if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
6694
0
      tmptrash = alloc_trash_chunk();
6695
0
      if (tmptrash) {
6696
0
        chunk_printf(tmptrash,
6697
0
            "%sServer %s/%s was DOWN and now enters maintenance",
6698
0
            s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
6699
0
        srv_append_adm_chg_cause(tmptrash, s, cause);
6700
0
        srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT));
6701
6702
0
        if (!(global.mode & MODE_STARTING)) {
6703
0
          ha_warning("%s.\n", tmptrash->area);
6704
0
          send_log(s->proxy, LOG_NOTICE, "%s.\n",
6705
0
             tmptrash->area);
6706
0
        }
6707
0
        free_trash_chunk(tmptrash);
6708
0
      }
6709
0
    }
6710
0
    else { /* server was still running */
6711
0
      s->check.health = 0; /* failure */
6712
6713
0
      s->next_state = SRV_ST_STOPPED;
6714
0
      srv_lb_propagate(s);
6715
6716
0
      if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
6717
0
        srv_shutdown_streams(s, SF_ERR_DOWN);
6718
6719
      /* force connection cleanup on the given server */
6720
0
      srv_cleanup_connections(s);
6721
      /* we might have streams queued on this server and waiting for
6722
       * a connection. Those which are redispatchable will be queued
6723
       * to another server or to the proxy itself.
6724
       */
6725
0
      xferred = pendconn_redistribute(s);
6726
6727
0
      tmptrash = alloc_trash_chunk();
6728
0
      if (tmptrash) {
6729
0
        chunk_printf(tmptrash,
6730
0
                     "%sServer %s/%s is going DOWN for maintenance",
6731
0
                     s->flags & SRV_F_BACKUP ? "Backup " : "",
6732
0
                     s->proxy->id, s->id);
6733
0
        srv_append_adm_chg_cause(tmptrash, s, cause);
6734
0
        srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT));
6735
6736
0
        if (!(global.mode & MODE_STARTING)) {
6737
0
          ha_warning("%s.\n", tmptrash->area);
6738
0
          send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
6739
0
             tmptrash->area);
6740
0
        }
6741
0
        free_trash_chunk(tmptrash);
6742
0
      }
6743
0
    }
6744
0
  }
6745
0
  else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
6746
    /* OK here we're leaving maintenance, we have many things to check,
6747
     * because the server might possibly be coming back up depending on
6748
     * its state. In practice, leaving maintenance means that we should
6749
     * immediately turn to UP (more or less the slowstart) under the
6750
     * following conditions :
6751
     *   - server is neither checked nor tracked
6752
     *   - server tracks another server which is not checked
6753
     *   - server tracks another server which is already up
6754
     * Which sums up as something simpler :
6755
     * "either the tracking server is up or the server's checks are disabled
6756
     * or up". Otherwise we only re-enable health checks. There's a special
6757
     * case associated to the stopping state which can be inherited. Note
6758
     * that the server might still be in drain mode, which is naturally dealt
6759
     * with by the lower level functions.
6760
     */
6761
0
    if (s->check.state & CHK_ST_ENABLED) {
6762
0
      s->check.state &= ~CHK_ST_PAUSED;
6763
0
      if(s->init_state == SRV_INIT_STATE_FULLY_UP) {
6764
0
        s->check.health = s->check.rise + s->check.fall - 1; /* initially UP, when all checks fail to bring server DOWN */
6765
0
      }
6766
0
      else if(s->init_state == SRV_INIT_STATE_DOWN) {
6767
0
        s->check.health = s->check.rise - 1; /* initially DOWN, when one check is successful bring server UP */
6768
0
      }
6769
0
      else if(s->init_state == SRV_INIT_STATE_FULLY_DOWN) {
6770
0
        s->check.health = 0; /* initially DOWN, when all checks are successful bring server UP */
6771
0
      } else {
6772
0
        s->check.health = s->check.rise; /* initially UP, when one check fails check brings server DOWN */
6773
0
      }
6774
0
    }
6775
6776
0
    if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
6777
0
        (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
6778
0
        (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
6779
0
      if (s->track && s->track->next_state == SRV_ST_STOPPING) {
6780
0
        s->next_state = SRV_ST_STOPPING;
6781
0
      }
6782
0
      else {
6783
0
        s->next_state = SRV_ST_STARTING;
6784
0
        if (s->slowstart > 0) {
6785
0
          if (s->warmup)
6786
0
            task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
6787
0
        }
6788
0
        else
6789
0
          s->next_state = SRV_ST_RUNNING;
6790
0
      }
6791
6792
0
    }
6793
6794
0
    tmptrash = alloc_trash_chunk();
6795
0
    if (tmptrash) {
6796
0
      if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
6797
0
        chunk_printf(tmptrash,
6798
0
               "%sServer %s/%s is %s/%s (leaving forced maintenance)",
6799
0
               s->flags & SRV_F_BACKUP ? "Backup " : "",
6800
0
               s->proxy->id, s->id,
6801
0
               (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
6802
0
               (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
6803
0
      }
6804
0
      if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
6805
0
        chunk_printf(tmptrash,
6806
0
               "%sServer %s/%s ('%s') is %s/%s (resolves again)",
6807
0
               s->flags & SRV_F_BACKUP ? "Backup " : "",
6808
0
               s->proxy->id, s->id, s->hostname,
6809
0
               (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
6810
0
               (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
6811
0
      }
6812
0
      if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
6813
0
        chunk_printf(tmptrash,
6814
0
               "%sServer %s/%s is %s/%s (leaving maintenance)",
6815
0
               s->flags & SRV_F_BACKUP ? "Backup " : "",
6816
0
               s->proxy->id, s->id,
6817
0
               (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
6818
0
               (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
6819
0
      }
6820
0
      ha_warning("%s.\n", tmptrash->area);
6821
0
      send_log(s->proxy, LOG_NOTICE, "%s.\n",
6822
0
         tmptrash->area);
6823
0
      free_trash_chunk(tmptrash);
6824
0
    }
6825
6826
0
    server_recalc_eweight(s, 0);
6827
    /* now propagate the status change to any LB algorithms */
6828
0
    srv_lb_propagate(s);
6829
6830
    /* If the server is set with "on-marked-up shutdown-backup-sessions",
6831
     * and it's not a backup server and its effective weight is > 0,
6832
     * then it can accept new connections, so we shut down all streams
6833
     * on all backup servers.
6834
     */
6835
0
    if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
6836
0
        !(s->flags & SRV_F_BACKUP) && s->next_eweight)
6837
0
      srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
6838
6839
    /* check if we can handle some connections queued.
6840
     * We will take as many as we can handle.
6841
     */
6842
0
    xferred = process_srv_queue(s);
6843
0
  }
6844
0
  else if (s->next_admin & SRV_ADMF_MAINT) {
6845
    /* remaining in maintenance mode, let's inform precisely about the
6846
     * situation.
6847
     */
6848
0
    if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
6849
0
      tmptrash = alloc_trash_chunk();
6850
0
      if (tmptrash) {
6851
0
        chunk_printf(tmptrash,
6852
0
                     "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
6853
0
                     s->flags & SRV_F_BACKUP ? "Backup " : "",
6854
0
                     s->proxy->id, s->id);
6855
6856
0
        if (s->track) /* normally it's mandatory here */
6857
0
          chunk_appendf(tmptrash, " via %s/%s",
6858
0
                      s->track->proxy->id, s->track->id);
6859
0
        ha_warning("%s.\n", tmptrash->area);
6860
0
        send_log(s->proxy, LOG_NOTICE, "%s.\n",
6861
0
           tmptrash->area);
6862
0
        free_trash_chunk(tmptrash);
6863
0
      }
6864
0
    }
6865
0
    if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
6866
0
      tmptrash = alloc_trash_chunk();
6867
0
      if (tmptrash) {
6868
0
        chunk_printf(tmptrash,
6869
0
                     "%sServer %s/%s ('%s') resolves again but remains in maintenance",
6870
0
                     s->flags & SRV_F_BACKUP ? "Backup " : "",
6871
0
                     s->proxy->id, s->id, s->hostname);
6872
6873
0
        if (s->track) /* normally it's mandatory here */
6874
0
          chunk_appendf(tmptrash, " via %s/%s",
6875
0
                      s->track->proxy->id, s->track->id);
6876
0
        ha_warning("%s.\n", tmptrash->area);
6877
0
        send_log(s->proxy, LOG_NOTICE, "%s.\n",
6878
0
           tmptrash->area);
6879
0
        free_trash_chunk(tmptrash);
6880
0
      }
6881
0
    }
6882
0
    else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
6883
0
      tmptrash = alloc_trash_chunk();
6884
0
      if (tmptrash) {
6885
0
        chunk_printf(tmptrash,
6886
0
                     "%sServer %s/%s remains in forced maintenance",
6887
0
                     s->flags & SRV_F_BACKUP ? "Backup " : "",
6888
0
                     s->proxy->id, s->id);
6889
0
        ha_warning("%s.\n", tmptrash->area);
6890
0
        send_log(s->proxy, LOG_NOTICE, "%s.\n",
6891
0
           tmptrash->area);
6892
0
        free_trash_chunk(tmptrash);
6893
0
      }
6894
0
    }
6895
    /* don't report anything when leaving drain mode and remaining in maintenance */
6896
0
  }
6897
6898
0
  if (!(s->next_admin & SRV_ADMF_MAINT)) {
6899
0
    if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
6900
      /* drain state is applied only if not yet in maint */
6901
6902
0
      srv_lb_propagate(s);
6903
6904
      /* we might have streams queued on this server and waiting for
6905
       * a connection. Those which are redispatchable will be queued
6906
       * to another server or to the proxy itself.
6907
       */
6908
0
      xferred = pendconn_redistribute(s);
6909
6910
0
      tmptrash = alloc_trash_chunk();
6911
0
      if (tmptrash) {
6912
0
        chunk_printf(tmptrash, "%sServer %s/%s enters drain state",
6913
0
               s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
6914
0
        srv_append_adm_chg_cause(tmptrash, s, cause);
6915
0
        srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
6916
6917
0
        if (!(global.mode & MODE_STARTING)) {
6918
0
          ha_warning("%s.\n", tmptrash->area);
6919
0
          send_log(s->proxy, LOG_NOTICE, "%s.\n",
6920
0
             tmptrash->area);
6921
0
          send_email_alert(s, LOG_NOTICE, "%s",
6922
0
               tmptrash->area);
6923
0
        }
6924
0
        free_trash_chunk(tmptrash);
6925
0
      }
6926
0
    }
6927
0
    else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
6928
      /* OK completely leaving drain mode */
6929
0
      server_recalc_eweight(s, 0);
6930
6931
0
      tmptrash = alloc_trash_chunk();
6932
0
      if (tmptrash) {
6933
0
        if (s->cur_admin & SRV_ADMF_FDRAIN) {
6934
0
          chunk_printf(tmptrash,
6935
0
                 "%sServer %s/%s is %s (leaving forced drain)",
6936
0
                 s->flags & SRV_F_BACKUP ? "Backup " : "",
6937
0
                       s->proxy->id, s->id,
6938
0
                       (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
6939
0
        }
6940
0
        else {
6941
0
          chunk_printf(tmptrash,
6942
0
                       "%sServer %s/%s is %s (leaving drain)",
6943
0
                       s->flags & SRV_F_BACKUP ? "Backup " : "",
6944
0
                 s->proxy->id, s->id,
6945
0
                 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
6946
0
          if (s->track) /* normally it's mandatory here */
6947
0
            chunk_appendf(tmptrash, " via %s/%s",
6948
0
          s->track->proxy->id, s->track->id);
6949
0
        }
6950
6951
0
        ha_warning("%s.\n", tmptrash->area);
6952
0
        send_log(s->proxy, LOG_NOTICE, "%s.\n",
6953
0
           tmptrash->area);
6954
0
        free_trash_chunk(tmptrash);
6955
0
      }
6956
6957
      /* now propagate the status change to any LB algorithms */
6958
0
      srv_lb_propagate(s);
6959
0
    }
6960
0
    else if ((s->next_admin & SRV_ADMF_DRAIN)) {
6961
      /* remaining in drain mode after removing one of its flags */
6962
6963
0
      tmptrash = alloc_trash_chunk();
6964
0
      if (tmptrash) {
6965
0
        if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
6966
0
          chunk_printf(tmptrash,
6967
0
                       "%sServer %s/%s remains in drain mode",
6968
0
                       s->flags & SRV_F_BACKUP ? "Backup " : "",
6969
0
                       s->proxy->id, s->id);
6970
6971
0
          if (s->track) /* normally it's mandatory here */
6972
0
            chunk_appendf(tmptrash, " via %s/%s",
6973
0
                        s->track->proxy->id, s->track->id);
6974
0
        }
6975
0
        else {
6976
0
          chunk_printf(tmptrash,
6977
0
                       "%sServer %s/%s remains in forced drain mode",
6978
0
                       s->flags & SRV_F_BACKUP ? "Backup " : "",
6979
0
                       s->proxy->id, s->id);
6980
0
        }
6981
0
        ha_warning("%s.\n", tmptrash->area);
6982
0
        send_log(s->proxy, LOG_NOTICE, "%s.\n",
6983
0
           tmptrash->area);
6984
0
        free_trash_chunk(tmptrash);
6985
0
      }
6986
0
    }
6987
0
  }
6988
0
  return xferred;
6989
0
}
6990
6991
/*
6992
 * This function applies server's status changes.
6993
 *
6994
 * Must be called with the server lock held. This may also be called at init
6995
 * time as the result of parsing the state file, in which case no lock will be
6996
 * held, and the server's warmup task can be null.
6997
 * <type> should be 0 for operational and 1 for administrative
6998
 * <cause> must be srv_op_st_chg_cause enum for operational and
6999
 * srv_adm_st_chg_cause enum for administrative
7000
 */
7001
static void srv_update_status(struct server *s, int type, int cause)
7002
0
{
7003
0
  int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
7004
0
  enum srv_state srv_prev_state = s->cur_state;
7005
0
  union {
7006
0
    struct event_hdl_cb_data_server_state state;
7007
0
    struct event_hdl_cb_data_server_admin admin;
7008
0
    struct event_hdl_cb_data_server common;
7009
0
  } cb_data;
7010
0
  int requeued;
7011
7012
  /* prepare common server event data */
7013
0
  _srv_event_hdl_prepare(&cb_data.common, s, 0);
7014
7015
0
  if (type) {
7016
0
    cb_data.admin.safe.cause = cause;
7017
0
    cb_data.admin.safe.old_admin = s->cur_admin;
7018
0
    cb_data.admin.safe.new_admin = s->next_admin;
7019
0
    requeued = _srv_update_status_adm(s, cause);
7020
0
    cb_data.admin.safe.requeued = requeued;
7021
    /* publish admin change */
7022
0
    _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADMIN, cb_data.admin, s);
7023
0
  }
7024
0
  else
7025
0
    requeued = _srv_update_status_op(s, cause);
7026
7027
  /* explicitly commit state changes (even if it was already applied implicitly
7028
   * by some lb state change function), so we don't miss anything
7029
   */
7030
0
  srv_lb_commit_status(s);
7031
7032
  /* check if server stats must be updated due the the server state change */
7033
0
  if (srv_prev_state != s->cur_state) {
7034
0
    if (srv_prev_state == SRV_ST_STOPPED) {
7035
0
      unsigned long last_change = COUNTERS_SHARED_LAST(s->proxy->be_counters.shared->tg, last_change);
7036
7037
      /* server was down and no longer is */
7038
0
      if (last_change < ns_to_sec(now_ns))                        // ignore negative times
7039
0
        s->down_time += ns_to_sec(now_ns) - last_change;
7040
0
      _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, cb_data.common, s);
7041
0
    }
7042
0
    else if (s->cur_state == SRV_ST_STOPPED) {
7043
      /* server was up and is currently down */
7044
0
      HA_ATOMIC_INC(&s->counters.shared->tg[tgid - 1]->down_trans);
7045
0
      _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, cb_data.common, s);
7046
0
    }
7047
7048
    /*
7049
     * If the server is no longer running, let's not pretend
7050
     * it can handle requests.
7051
     */
7052
0
    if (s->cur_state != SRV_ST_RUNNING && s->proxy->ready_srv == s)
7053
0
      HA_ATOMIC_STORE(&s->proxy->ready_srv, NULL);
7054
7055
0
    HA_ATOMIC_STORE(&s->counters.shared->tg[tgid - 1]->last_change, ns_to_sec(now_ns));
7056
7057
    /* publish the state change */
7058
0
    _srv_event_hdl_prepare_state(&cb_data.state,
7059
0
                                 s, type, cause, srv_prev_state, requeued);
7060
0
    _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_STATE, cb_data.state, s);
7061
0
  }
7062
7063
  /* check if backend stats must be updated due to the server state change */
7064
0
  if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
7065
0
    set_backend_down(s->proxy); /* backend going down */
7066
0
  else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) {
7067
0
    unsigned long last_change = COUNTERS_SHARED_LAST(s->proxy->be_counters.shared->tg, last_change);
7068
7069
    /* backend was down and is back up again:
7070
     * no helper function, updating last_change and backend downtime stats
7071
     */
7072
0
    if (last_change < ns_to_sec(now_ns))         // ignore negative times
7073
0
      s->proxy->down_time += ns_to_sec(now_ns) - last_change;
7074
0
    HA_ATOMIC_STORE(&s->proxy->be_counters.shared->tg[tgid - 1]->last_change, ns_to_sec(now_ns));
7075
0
  }
7076
0
}
7077
7078
struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
7079
0
{
7080
0
  struct connection *conn;
7081
7082
0
  while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
7083
0
                                 struct connection *, toremove_list)) != NULL) {
7084
0
    conn->mux->destroy(conn->ctx);
7085
0
  }
7086
7087
0
  return task;
7088
0
}
7089
7090
/* Move <toremove_nb> count connections from <list> storage to <toremove_list>
7091
 * list storage. -1 means moving all of them.
7092
 *
7093
 * Returns the number of connections moved.
7094
 *
7095
 * Must be called with idle_conns_lock held.
7096
 */
7097
static int srv_migrate_conns_to_remove(struct list *list, struct mt_list *toremove_list, int toremove_nb)
7098
0
{
7099
0
  struct connection *conn;
7100
0
  int i = 0;
7101
7102
0
  while (!LIST_ISEMPTY(list)) {
7103
0
    if (toremove_nb != -1 && i >= toremove_nb)
7104
0
      break;
7105
7106
0
    conn = LIST_ELEM(list->n, struct connection *, idle_list);
7107
0
    conn_delete_from_tree(conn);
7108
0
    MT_LIST_APPEND(toremove_list, &conn->toremove_list);
7109
0
    i++;
7110
0
  }
7111
7112
0
  return i;
7113
0
}
7114
/* cleanup connections for a given server
7115
 * might be useful when going on forced maintenance or live changing ip/port
7116
 */
7117
static void srv_cleanup_connections(struct server *srv)
7118
0
{
7119
0
  int did_remove;
7120
0
  int i;
7121
7122
  /* nothing to do if pool-max-conn is null */
7123
0
  if (!srv->max_idle_conns)
7124
0
    return;
7125
7126
  /* check all threads starting with ours */
7127
0
  for (i = tid;;) {
7128
0
    did_remove = 0;
7129
0
    HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
7130
0
    if (srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conn_list, &idle_conns[i].toremove_conns, -1) > 0)
7131
0
      did_remove = 1;
7132
0
    HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
7133
0
    if (did_remove)
7134
0
      task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
7135
7136
0
    if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
7137
0
      break;
7138
0
  }
7139
0
}
7140
7141
/* removes an idle conn after updating the server idle conns counters */
7142
void srv_release_conn(struct server *srv, struct connection *conn)
7143
0
{
7144
0
  if (conn->flags & CO_FL_LIST_MASK) {
7145
    /* The connection is currently in the server's idle list, so tell it
7146
     * there's one less connection available in that list.
7147
     */
7148
0
    _HA_ATOMIC_DEC(&srv->curr_idle_conns);
7149
0
    _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
7150
0
    _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
7151
0
  }
7152
0
  else {
7153
    /* The connection is not private and not in any server's idle
7154
     * list, so decrement the current number of used connections
7155
     */
7156
0
    _HA_ATOMIC_DEC(&srv->curr_used_conns);
7157
0
  }
7158
7159
  /* Remove the connection from any tree (safe, idle or available) */
7160
0
  if (conn->hash_node) {
7161
0
    HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
7162
0
    conn_delete_from_tree(conn);
7163
0
    conn->flags &= ~CO_FL_LIST_MASK;
7164
0
    HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
7165
0
  }
7166
0
}
7167
7168
/* retrieve a connection from its <hash> in <tree>
7169
 * returns NULL if no connection found
7170
 */
7171
struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
7172
0
{
7173
0
  struct eb64_node *node = NULL;
7174
0
  struct connection *conn = NULL;
7175
0
  struct conn_hash_node *hash_node = NULL;
7176
7177
0
  node = eb64_lookup(tree, hash);
7178
0
  if (node) {
7179
0
    hash_node = ebmb_entry(node, struct conn_hash_node, node);
7180
0
    conn = hash_node->conn;
7181
0
  }
7182
7183
0
  return conn;
7184
0
}
7185
7186
/* retrieve the next connection sharing the same hash as <conn>
7187
 * returns NULL if no connection found
7188
 */
7189
struct connection *srv_lookup_conn_next(struct connection *conn)
7190
0
{
7191
0
  struct eb64_node *node = NULL;
7192
0
  struct connection *next_conn = NULL;
7193
0
  struct conn_hash_node *hash_node = NULL;
7194
7195
0
  node = eb64_next_dup(&conn->hash_node->node);
7196
0
  if (node) {
7197
0
    hash_node = eb64_entry(node, struct conn_hash_node, node);
7198
0
    next_conn = hash_node->conn;
7199
0
  }
7200
7201
0
  return next_conn;
7202
0
}
7203
7204
/* Add <conn> in <srv> idle trees. Set <is_safe> if connection is deemed safe
7205
 * for reuse.
7206
 *
7207
 * This function is a simple wrapper for tree insert. It should only be used
7208
 * for internal usage or when removing briefly the connection to avoid takeover
7209
 * on it before reinserting it with this function. In other context, prefer to
7210
 * use the full feature srv_add_to_idle_list().
7211
 *
7212
 * Must be called with idle_conns_lock.
7213
 */
7214
void _srv_add_idle(struct server *srv, struct connection *conn, int is_safe)
7215
0
{
7216
0
  struct eb_root *tree = is_safe ? &srv->per_thr[tid].safe_conns :
7217
0
                                   &srv->per_thr[tid].idle_conns;
7218
7219
  /* first insert in idle or safe tree. */
7220
0
  eb64_insert(tree, &conn->hash_node->node);
7221
7222
  /* insert in list sorted by connection usage. */
7223
0
  LIST_APPEND(&srv->per_thr[tid].idle_conn_list, &conn->idle_list);
7224
0
}
7225
7226
/* This adds an idle connection to the server's list if the connection is
7227
 * reusable, not held by any owner anymore, but still has available streams.
7228
 */
7229
int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
7230
0
{
7231
  /* we try to keep the connection in the server's idle list
7232
   * if we don't have too many FD in use, and if the number of
7233
   * idle+current conns is lower than what was observed before
7234
   * last purge, or if we already don't have idle conns for the
7235
   * current thread and we don't exceed last count by global.nbthread.
7236
   */
7237
0
  if (!(conn->flags & CO_FL_PRIVATE) &&
7238
0
      srv && srv->pool_purge_delay > 0 &&
7239
0
      ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
7240
0
      ha_used_fds < global.tune.pool_high_count &&
7241
0
      (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
7242
0
      ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
7243
0
        (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
7244
0
       (ha_used_fds < global.tune.pool_low_count &&
7245
0
        (srv->curr_used_conns + srv->curr_idle_conns <=
7246
0
         MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns ||
7247
0
         (conn->flags & CO_FL_REVERSED)))) &&
7248
0
      !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
7249
0
    int retadd;
7250
7251
0
    retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
7252
0
    if (retadd > srv->max_idle_conns) {
7253
0
      _HA_ATOMIC_DEC(&srv->curr_idle_conns);
7254
0
      return 0;
7255
0
    }
7256
0
    _HA_ATOMIC_DEC(&srv->curr_used_conns);
7257
7258
0
    HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
7259
0
    conn_delete_from_tree(conn);
7260
7261
0
    if (is_safe) {
7262
0
      conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
7263
0
      _srv_add_idle(srv, conn, 1);
7264
0
      _HA_ATOMIC_INC(&srv->curr_safe_nb);
7265
0
    } else {
7266
0
      conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
7267
0
      _srv_add_idle(srv, conn, 0);
7268
0
      _HA_ATOMIC_INC(&srv->curr_idle_nb);
7269
0
    }
7270
0
    HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
7271
0
    _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
7272
7273
0
    __ha_barrier_full();
7274
0
    if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
7275
0
      HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
7276
0
      if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
7277
0
        srv->idle_node.key = tick_add(srv->pool_purge_delay,
7278
0
                                      now_ms);
7279
0
        eb32_insert(&idle_conn_srv, &srv->idle_node);
7280
0
        if (!task_in_wq(idle_conn_task) && !
7281
0
            task_in_rq(idle_conn_task)) {
7282
0
          task_schedule(idle_conn_task,
7283
0
                        srv->idle_node.key);
7284
0
        }
7285
7286
0
      }
7287
0
      HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
7288
0
    }
7289
0
    return 1;
7290
0
  }
7291
0
  return 0;
7292
0
}
7293
7294
/* Insert <conn> connection in <srv> server available list. This is reserved
7295
 * for backend connection currently in used with usable streams left.
7296
 */
7297
void srv_add_to_avail_list(struct server *srv, struct connection *conn)
7298
0
{
7299
  /* connection cannot be in idle list if used as an avail idle conn. */
7300
0
  BUG_ON(LIST_INLIST(&conn->idle_list));
7301
0
  eb64_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node);
7302
0
}
7303
7304
struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
7305
0
{
7306
0
  struct server *srv;
7307
0
  struct eb32_node *eb;
7308
0
  int i;
7309
0
  unsigned int next_wakeup;
7310
7311
0
  next_wakeup = TICK_ETERNITY;
7312
0
  HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
7313
0
  while (1) {
7314
0
    int exceed_conns;
7315
0
    int to_kill;
7316
0
    int curr_idle;
7317
7318
0
    eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
7319
0
    if (!eb) {
7320
      /* we might have reached the end of the tree, typically because
7321
       * <now_ms> is in the first half and we're first scanning the last
7322
      * half. Let's loop back to the beginning of the tree now.
7323
      */
7324
7325
0
      eb = eb32_first(&idle_conn_srv);
7326
0
      if (likely(!eb))
7327
0
        break;
7328
0
    }
7329
0
    if (tick_is_lt(now_ms, eb->key)) {
7330
      /* timer not expired yet, revisit it later */
7331
0
      next_wakeup = eb->key;
7332
0
      break;
7333
0
    }
7334
0
    srv = eb32_entry(eb, struct server, idle_node);
7335
7336
    /* Calculate how many idle connections we want to kill :
7337
     * we want to remove half the difference between the total
7338
     * of established connections (used or idle) and the max
7339
     * number of used connections.
7340
     */
7341
0
    curr_idle = srv->curr_idle_conns;
7342
0
    if (curr_idle == 0)
7343
0
      goto remove;
7344
0
    exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
7345
0
    exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
7346
7347
0
    srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
7348
0
    if (srv->est_need_conns < srv->max_used_conns)
7349
0
      srv->est_need_conns = srv->max_used_conns;
7350
7351
0
    HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
7352
7353
0
    if (exceed_conns <= 0)
7354
0
      goto remove;
7355
7356
    /* check all threads starting with ours */
7357
0
    for (i = tid;;) {
7358
0
      int max_conn;
7359
0
      int j;
7360
0
      int did_remove = 0;
7361
7362
0
      max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
7363
0
                 curr_idle + 1;
7364
7365
0
      HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
7366
0
      j = srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conn_list, &idle_conns[i].toremove_conns, max_conn);
7367
0
      if (j > 0)
7368
0
        did_remove = 1;
7369
0
      HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
7370
7371
0
      if (did_remove)
7372
0
        task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
7373
7374
0
      if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
7375
0
        break;
7376
0
    }
7377
0
remove:
7378
0
    eb32_delete(&srv->idle_node);
7379
7380
0
    if (srv->curr_idle_conns) {
7381
      /* There are still more idle connections, add the
7382
       * server back in the tree.
7383
       */
7384
0
      srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
7385
0
      eb32_insert(&idle_conn_srv, &srv->idle_node);
7386
0
      next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
7387
0
    }
7388
0
  }
7389
0
  HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
7390
7391
0
  task->expire = next_wakeup;
7392
0
  return task;
7393
0
}
7394
7395
/* Close remaining idle connections. This functions is designed to be run on
7396
 * process shutdown. This guarantees a proper socket shutdown to avoid
7397
 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
7398
 * bypassed.
7399
 *
7400
 * This function is not thread-safe so it must only be called via a global
7401
 * deinit function.
7402
 */
7403
static void srv_close_idle_conns(struct server *srv)
7404
0
{
7405
0
  struct eb_root **cleaned_tree;
7406
0
  int i;
7407
7408
0
  for (i = 0; i < global.nbthread; ++i) {
7409
0
    struct eb_root *conn_trees[] = {
7410
0
      &srv->per_thr[i].idle_conns,
7411
0
      &srv->per_thr[i].safe_conns,
7412
0
      &srv->per_thr[i].avail_conns,
7413
0
      NULL
7414
0
    };
7415
7416
0
    for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
7417
0
      while (!eb_is_empty(*cleaned_tree)) {
7418
0
        struct ebmb_node *node = ebmb_first(*cleaned_tree);
7419
0
        struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
7420
0
        struct connection *conn = conn_hash_node->conn;
7421
7422
0
        if (conn->ctrl->ctrl_close)
7423
0
          conn->ctrl->ctrl_close(conn);
7424
0
        conn_delete_from_tree(conn);
7425
0
      }
7426
0
    }
7427
0
  }
7428
0
}
7429
7430
REGISTER_SERVER_DEINIT(srv_close_idle_conns);
7431
7432
/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
7433
static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
7434
                                      const struct proxy *defpx, const char *file, int line,
7435
                                      char **err)
7436
0
{
7437
0
  if (too_many_args(1, args, err, NULL))
7438
0
    return -1;
7439
7440
0
  if (strcmp(args[1], "on") == 0)
7441
0
    global.tune.options |= GTUNE_IDLE_POOL_SHARED;
7442
0
  else if (strcmp(args[1], "off") == 0)
7443
0
    global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
7444
0
  else {
7445
0
    memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
7446
0
    return -1;
7447
0
  }
7448
0
  return 0;
7449
0
}
7450
7451
/* config parser for global "tune.pool-{low,high}-fd-ratio" */
7452
static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
7453
                                   const struct proxy *defpx, const char *file, int line,
7454
                                   char **err)
7455
0
{
7456
0
  int arg = -1;
7457
7458
0
  if (too_many_args(1, args, err, NULL))
7459
0
    return -1;
7460
7461
0
  if (*(args[1]) != 0)
7462
0
    arg = atoi(args[1]);
7463
7464
0
  if (arg < 0 || arg > 100) {
7465
0
    memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
7466
0
    return -1;
7467
0
  }
7468
7469
0
  if (args[0][10] == 'h')
7470
0
    global.tune.pool_high_ratio = arg;
7471
0
  else
7472
0
    global.tune.pool_low_ratio = arg;
7473
0
  return 0;
7474
0
}
7475
7476
/* config keyword parsers */
7477
static struct cfg_kw_list cfg_kws = {ILH, {
7478
  { CFG_GLOBAL, "tune.idle-pool.shared",       cfg_parse_idle_pool_shared },
7479
  { CFG_GLOBAL, "tune.pool-high-fd-ratio",     cfg_parse_pool_fd_ratio },
7480
  { CFG_GLOBAL, "tune.pool-low-fd-ratio",      cfg_parse_pool_fd_ratio },
7481
  { 0, NULL, NULL }
7482
}};
7483
7484
INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7485
7486
/*
7487
 * Local variables:
7488
 *  c-indent-level: 8
7489
 *  c-basic-offset: 8
7490
 * End:
7491
 */