Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 2 | * Listener management functions. |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 4 | * Copyright 2000-2013 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <string.h> |
Willy Tarreau | 95ccdde | 2014-02-01 09:28:36 +0100 | [diff] [blame] | 17 | #include <unistd.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 19 | #include <haproxy/acl.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 20 | #include <haproxy/api.h> |
Willy Tarreau | 5d9ddc5 | 2021-10-06 19:54:09 +0200 | [diff] [blame] | 21 | #include <haproxy/activity.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 22 | #include <haproxy/cfgparse.h> |
Willy Tarreau | dbf7802 | 2021-10-06 09:05:08 +0200 | [diff] [blame] | 23 | #include <haproxy/cli-t.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 24 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 25 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 26 | #include <haproxy/fd.h> |
| 27 | #include <haproxy/freq_ctr.h> |
Amaury Denoyelle | 8ee9fc7 | 2023-10-25 15:32:28 +0200 | [diff] [blame] | 28 | #include <haproxy/frontend.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 29 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 30 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 31 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 32 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 33 | #include <haproxy/protocol.h> |
Willy Tarreau | 5958c43 | 2021-05-08 20:30:37 +0200 | [diff] [blame] | 34 | #include <haproxy/proxy.h> |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 35 | #include <haproxy/quic_tp.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 36 | #include <haproxy/sample.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 37 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 38 | #include <haproxy/task.h> |
Willy Tarreau | 9310f48 | 2021-10-06 16:18:40 +0200 | [diff] [blame] | 39 | #include <haproxy/ticks.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <haproxy/tools.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 42 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 43 | /* List head of all known bind keywords */ |
Willy Tarreau | ca1acd6 | 2022-03-29 15:02:44 +0200 | [diff] [blame] | 44 | struct bind_kw_list bind_keywords = { |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 45 | .list = LIST_HEAD_INIT(bind_keywords.list) |
| 46 | }; |
| 47 | |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 48 | /* list of the temporarily limited listeners because of lack of resource */ |
| 49 | static struct mt_list global_listener_queue = MT_LIST_HEAD_INIT(global_listener_queue); |
| 50 | static struct task *global_listener_queue_task; |
Willy Tarreau | 9615102 | 2023-05-11 13:51:31 +0200 | [diff] [blame] | 51 | /* number of times an accepted connection resulted in maxconn being reached */ |
| 52 | ullong maxconn_reached = 0; |
Willy Tarreau | 469fa47 | 2022-11-22 09:08:23 +0100 | [diff] [blame] | 53 | __decl_thread(static HA_RWLOCK_T global_listener_rwlock); |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 54 | |
William Dauchy | 3679d0c | 2021-02-14 23:22:55 +0100 | [diff] [blame] | 55 | /* listener status for stats */ |
| 56 | const char* li_status_st[LI_STATE_COUNT] = { |
| 57 | [LI_STATUS_WAITING] = "WAITING", |
| 58 | [LI_STATUS_OPEN] = "OPEN", |
| 59 | [LI_STATUS_FULL] = "FULL", |
| 60 | }; |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 61 | |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 62 | #if defined(USE_THREAD) |
| 63 | |
| 64 | struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))) = { }; |
| 65 | |
| 66 | /* dequeue and process a pending connection from the local accept queue (single |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 67 | * consumer). Returns the accepted connection or NULL if none was found. |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 68 | */ |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 69 | struct connection *accept_queue_pop_sc(struct accept_queue_ring *ring) |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 70 | { |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 71 | unsigned int pos, next; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 72 | struct connection *ptr; |
| 73 | struct connection **e; |
Willy Tarreau | e6f5ab5 | 2023-04-20 11:05:28 +0200 | [diff] [blame] | 74 | uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */ |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 75 | |
Willy Tarreau | e6f5ab5 | 2023-04-20 11:05:28 +0200 | [diff] [blame] | 76 | pos = idx >> 16; |
| 77 | if (pos == (uint16_t)idx) |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 78 | return NULL; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 79 | |
| 80 | next = pos + 1; |
| 81 | if (next >= ACCEPT_QUEUE_SIZE) |
| 82 | next = 0; |
| 83 | |
| 84 | e = &ring->entry[pos]; |
| 85 | |
| 86 | /* wait for the producer to update the listener's pointer */ |
| 87 | while (1) { |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 88 | ptr = *e; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 89 | __ha_barrier_load(); |
| 90 | if (ptr) |
| 91 | break; |
| 92 | pl_cpu_relax(); |
| 93 | } |
| 94 | |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 95 | /* release the entry */ |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 96 | *e = NULL; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 97 | |
| 98 | __ha_barrier_store(); |
Willy Tarreau | e6f5ab5 | 2023-04-20 11:05:28 +0200 | [diff] [blame] | 99 | do { |
| 100 | pos = (next << 16) | (idx & 0xffff); |
| 101 | } while (unlikely(!HA_ATOMIC_CAS(&ring->idx, &idx, pos) && __ha_cpu_relax())); |
| 102 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 103 | return ptr; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 107 | /* tries to push a new accepted connection <conn> into ring <ring>. Returns |
| 108 | * non-zero if it succeeds, or zero if the ring is full. Supports multiple |
| 109 | * producers. |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 110 | */ |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 111 | int accept_queue_push_mp(struct accept_queue_ring *ring, struct connection *conn) |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 112 | { |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 113 | unsigned int pos, next; |
Willy Tarreau | e6f5ab5 | 2023-04-20 11:05:28 +0200 | [diff] [blame] | 114 | uint32_t idx = _HA_ATOMIC_LOAD(&ring->idx); /* (head << 16) + tail */ |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 116 | do { |
Willy Tarreau | e6f5ab5 | 2023-04-20 11:05:28 +0200 | [diff] [blame] | 117 | pos = (uint16_t)idx; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 118 | next = pos + 1; |
| 119 | if (next >= ACCEPT_QUEUE_SIZE) |
| 120 | next = 0; |
Willy Tarreau | e6f5ab5 | 2023-04-20 11:05:28 +0200 | [diff] [blame] | 121 | if (next == (idx >> 16)) |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 122 | return 0; // ring full |
Willy Tarreau | e6f5ab5 | 2023-04-20 11:05:28 +0200 | [diff] [blame] | 123 | next |= (idx & 0xffff0000U); |
| 124 | } while (unlikely(!_HA_ATOMIC_CAS(&ring->idx, &idx, next) && __ha_cpu_relax())); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 125 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 126 | ring->entry[pos] = conn; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 127 | __ha_barrier_store(); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | |
Willy Tarreau | fb5401f | 2021-01-29 12:25:23 +0100 | [diff] [blame] | 131 | /* proceed with accepting new connections. Don't mark it static so that it appears |
| 132 | * in task dumps. |
| 133 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 134 | struct task *accept_queue_process(struct task *t, void *context, unsigned int state) |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 135 | { |
| 136 | struct accept_queue_ring *ring = context; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 137 | struct connection *conn; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 138 | struct listener *li; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 139 | unsigned int max_accept; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 140 | int ret; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 141 | |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 142 | /* if global.tune.maxaccept is -1, then max_accept is UINT_MAX. It |
| 143 | * is not really illimited, but it is probably enough. |
| 144 | */ |
Willy Tarreau | 6616132 | 2021-02-19 15:50:27 +0100 | [diff] [blame] | 145 | max_accept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 146 | for (; max_accept; max_accept--) { |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 147 | conn = accept_queue_pop_sc(ring); |
| 148 | if (!conn) |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 149 | break; |
| 150 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 151 | li = __objt_listener(conn->target); |
Willy Tarreau | fea8c19 | 2023-02-28 10:25:57 +0100 | [diff] [blame] | 152 | _HA_ATOMIC_INC(&li->thr_conn[ti->ltid]); |
Willy Tarreau | 3083615 | 2023-01-12 19:10:17 +0100 | [diff] [blame] | 153 | ret = li->bind_conf->accept(conn); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 154 | if (ret <= 0) { |
| 155 | /* connection was terminated by the application */ |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | /* increase the per-process number of cumulated sessions, this |
Willy Tarreau | 3083615 | 2023-01-12 19:10:17 +0100 | [diff] [blame] | 160 | * may only be done once l->bind_conf->accept() has accepted the |
| 161 | * connection. |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 162 | */ |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 163 | if (!(li->bind_conf->options & BC_O_UNLIMITED)) { |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 164 | HA_ATOMIC_UPDATE_MAX(&global.sps_max, |
| 165 | update_freq_ctr(&global.sess_per_sec, 1)); |
Ilya Shipitsin | 83f54b9 | 2023-04-26 21:05:12 +0200 | [diff] [blame] | 166 | if (li->bind_conf->options & BC_O_USE_SSL) { |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 167 | HA_ATOMIC_UPDATE_MAX(&global.ssl_max, |
| 168 | update_freq_ctr(&global.ssl_per_sec, 1)); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* ran out of budget ? Let's come here ASAP */ |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 174 | if (!max_accept) |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 175 | tasklet_wakeup(ring->tasklet); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 176 | |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 177 | return NULL; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Initializes the accept-queues. Returns 0 on success, otherwise ERR_* flags */ |
| 181 | static int accept_queue_init() |
| 182 | { |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 183 | struct tasklet *t; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 184 | int i; |
| 185 | |
| 186 | for (i = 0; i < global.nbthread; i++) { |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 187 | t = tasklet_new(); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 188 | if (!t) { |
| 189 | ha_alert("Out of memory while initializing accept queue for thread %d\n", i); |
| 190 | return ERR_FATAL|ERR_ABORT; |
| 191 | } |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 192 | t->tid = i; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 193 | t->process = accept_queue_process; |
| 194 | t->context = &accept_queue_rings[i]; |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 195 | accept_queue_rings[i].tasklet = t; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | REGISTER_CONFIG_POSTPARSER("multi-threaded accept queue", accept_queue_init); |
| 201 | |
Willy Tarreau | e01b08d | 2022-04-27 18:42:47 +0200 | [diff] [blame] | 202 | static void accept_queue_deinit() |
| 203 | { |
| 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < global.nbthread; i++) { |
Tim Duesterhus | b1ec21d | 2023-04-22 17:47:32 +0200 | [diff] [blame] | 207 | tasklet_free(accept_queue_rings[i].tasklet); |
Willy Tarreau | e01b08d | 2022-04-27 18:42:47 +0200 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | REGISTER_POST_DEINIT(accept_queue_deinit); |
| 212 | |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 213 | #endif // USE_THREAD |
| 214 | |
Willy Tarreau | 6a4d48b | 2023-04-21 10:46:45 +0200 | [diff] [blame] | 215 | /* Memory allocation and initialization of the per_thr field (one entry per |
| 216 | * bound thread). |
Amaury Denoyelle | f68b2cb | 2022-01-25 16:21:47 +0100 | [diff] [blame] | 217 | * Returns 0 if the field has been successfully initialized, -1 on failure. |
| 218 | */ |
| 219 | int li_init_per_thr(struct listener *li) |
| 220 | { |
Willy Tarreau | 6a4d48b | 2023-04-21 10:46:45 +0200 | [diff] [blame] | 221 | int nbthr = MIN(global.nbthread, MAX_THREADS_PER_GROUP); |
Amaury Denoyelle | f68b2cb | 2022-01-25 16:21:47 +0100 | [diff] [blame] | 222 | int i; |
| 223 | |
| 224 | /* allocate per-thread elements for listener */ |
Willy Tarreau | 6a4d48b | 2023-04-21 10:46:45 +0200 | [diff] [blame] | 225 | li->per_thr = calloc(nbthr, sizeof(*li->per_thr)); |
Amaury Denoyelle | f68b2cb | 2022-01-25 16:21:47 +0100 | [diff] [blame] | 226 | if (!li->per_thr) |
| 227 | return -1; |
| 228 | |
Willy Tarreau | 6a4d48b | 2023-04-21 10:46:45 +0200 | [diff] [blame] | 229 | for (i = 0; i < nbthr; ++i) { |
Amaury Denoyelle | f68b2cb | 2022-01-25 16:21:47 +0100 | [diff] [blame] | 230 | MT_LIST_INIT(&li->per_thr[i].quic_accept.list); |
| 231 | MT_LIST_INIT(&li->per_thr[i].quic_accept.conns); |
| 232 | |
| 233 | li->per_thr[i].li = li; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
William Dauchy | 3679d0c | 2021-02-14 23:22:55 +0100 | [diff] [blame] | 239 | /* helper to get listener status for stats */ |
| 240 | enum li_status get_li_status(struct listener *l) |
| 241 | { |
Willy Tarreau | 758c69d | 2023-01-12 18:59:37 +0100 | [diff] [blame] | 242 | if (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn) { |
William Dauchy | 3679d0c | 2021-02-14 23:22:55 +0100 | [diff] [blame] | 243 | if (l->state == LI_LIMITED) |
| 244 | return LI_STATUS_WAITING; |
| 245 | else |
| 246 | return LI_STATUS_OPEN; |
| 247 | } |
| 248 | return LI_STATUS_FULL; |
| 249 | } |
| 250 | |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 251 | /* adjust the listener's state and its proxy's listener counters if needed. |
| 252 | * It must be called under the listener's lock, but uses atomic ops to change |
| 253 | * the proxy's counters so that the proxy lock is not needed. |
| 254 | */ |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 255 | void listener_set_state(struct listener *l, enum li_state st) |
| 256 | { |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 257 | struct proxy *px = l->bind_conf->frontend; |
| 258 | |
| 259 | if (px) { |
| 260 | /* from state */ |
| 261 | switch (l->state) { |
| 262 | case LI_NEW: /* first call */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 263 | _HA_ATOMIC_INC(&px->li_all); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 264 | break; |
| 265 | case LI_INIT: |
| 266 | case LI_ASSIGNED: |
| 267 | break; |
| 268 | case LI_PAUSED: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 269 | _HA_ATOMIC_DEC(&px->li_paused); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 270 | break; |
| 271 | case LI_LISTEN: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 272 | _HA_ATOMIC_DEC(&px->li_bound); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 273 | break; |
| 274 | case LI_READY: |
| 275 | case LI_FULL: |
| 276 | case LI_LIMITED: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 277 | _HA_ATOMIC_DEC(&px->li_ready); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | |
| 281 | /* to state */ |
| 282 | switch (st) { |
| 283 | case LI_NEW: |
| 284 | case LI_INIT: |
| 285 | case LI_ASSIGNED: |
| 286 | break; |
| 287 | case LI_PAUSED: |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 288 | BUG_ON(l->rx.fd == -1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 289 | _HA_ATOMIC_INC(&px->li_paused); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 290 | break; |
| 291 | case LI_LISTEN: |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 292 | BUG_ON(l->rx.fd == -1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 293 | _HA_ATOMIC_INC(&px->li_bound); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 294 | break; |
| 295 | case LI_READY: |
| 296 | case LI_FULL: |
| 297 | case LI_LIMITED: |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 298 | BUG_ON(l->rx.fd == -1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 299 | _HA_ATOMIC_INC(&px->li_ready); |
Aurelien DARRAGON | 2370599 | 2023-02-14 08:51:14 +0100 | [diff] [blame] | 300 | l->flags |= LI_F_FINALIZED; |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | } |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 304 | l->state = st; |
| 305 | } |
| 306 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 307 | /* This function adds the specified listener's file descriptor to the polling |
| 308 | * lists if it is in the LI_LISTEN state. The listener enters LI_READY or |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 309 | * LI_FULL state depending on its number of connections. In daemon mode, we |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 310 | * also support binding only the relevant processes to their respective |
| 311 | * listeners. We don't do that in debug mode however. |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 312 | */ |
Willy Tarreau | 7834a3f | 2020-09-25 16:40:18 +0200 | [diff] [blame] | 313 | void enable_listener(struct listener *listener) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 314 | { |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 315 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | d6afb53 | 2020-10-09 10:35:40 +0200 | [diff] [blame] | 316 | |
| 317 | /* If this listener is supposed to be only in the master, close it in |
| 318 | * the workers. Conversely, if it's supposed to be only in the workers |
| 319 | * close it in the master. |
| 320 | */ |
Willy Tarreau | 18c20d2 | 2020-10-09 16:11:46 +0200 | [diff] [blame] | 321 | if (!!master != !!(listener->rx.flags & RX_F_MWORKER)) |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 322 | do_unbind_listener(listener); |
Willy Tarreau | d6afb53 | 2020-10-09 10:35:40 +0200 | [diff] [blame] | 323 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 324 | if (listener->state == LI_LISTEN) { |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 325 | BUG_ON(listener->rx.fd == -1); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 326 | if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) && |
Willy Tarreau | 72faef3 | 2021-06-15 08:36:30 +0200 | [diff] [blame] | 327 | (!!master != !!(listener->rx.flags & RX_F_MWORKER))) { |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 328 | /* we don't want to enable this listener and don't |
| 329 | * want any fd event to reach it. |
| 330 | */ |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 331 | do_unbind_listener(listener); |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 332 | } |
Willy Tarreau | 758c69d | 2023-01-12 18:59:37 +0100 | [diff] [blame] | 333 | else if (!listener->bind_conf->maxconn || listener->nbconn < listener->bind_conf->maxconn) { |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 334 | listener->rx.proto->enable(listener); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 335 | listener_set_state(listener, LI_READY); |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 336 | } |
| 337 | else { |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 338 | listener_set_state(listener, LI_FULL); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
Willy Tarreau | d6afb53 | 2020-10-09 10:35:40 +0200 | [diff] [blame] | 341 | |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 342 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 345 | /* |
Aurelien DARRAGON | 187396e | 2022-09-11 16:19:49 +0200 | [diff] [blame] | 346 | * This function completely stops a listener. |
| 347 | * The proxy's listeners count is updated and the proxy is |
| 348 | * disabled and woken up after the last one is gone. |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 349 | * It will need to operate under the proxy's lock, the protocol's lock and |
| 350 | * the listener's lock. The caller is responsible for indicating in lpx, |
| 351 | * lpr, lli whether the respective locks are already held (non-zero) or |
| 352 | * not (zero) so that the function picks the missing ones, in this order. |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 353 | */ |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 354 | void stop_listener(struct listener *l, int lpx, int lpr, int lli) |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 355 | { |
| 356 | struct proxy *px = l->bind_conf->frontend; |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 357 | |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 358 | if (l->bind_conf->options & BC_O_NOSTOP) { |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 359 | /* master-worker sockpairs are never closed but don't count as a |
| 360 | * job. |
| 361 | */ |
| 362 | return; |
| 363 | } |
| 364 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 365 | if (!lpx && px) |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 366 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 367 | |
| 368 | if (!lpr) |
| 369 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 370 | |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 371 | if (!lli) |
| 372 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 373 | |
| 374 | if (l->state > LI_INIT) { |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 375 | do_unbind_listener(l); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 376 | |
| 377 | if (l->state >= LI_ASSIGNED) |
| 378 | __delete_listener(l); |
| 379 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 380 | if (px) |
| 381 | proxy_cond_disable(px); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 384 | if (!lli) |
| 385 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 386 | |
| 387 | if (!lpr) |
| 388 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 389 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 390 | if (!lpx && px) |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 391 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 394 | /* This function adds the specified <listener> to the protocol <proto>. It |
| 395 | * does nothing if the protocol was already added. The listener's state is |
| 396 | * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners |
| 397 | * for the protocol is updated. This must be called with the proto lock held. |
| 398 | */ |
| 399 | void default_add_listener(struct protocol *proto, struct listener *listener) |
| 400 | { |
| 401 | if (listener->state != LI_INIT) |
| 402 | return; |
| 403 | listener_set_state(listener, LI_ASSIGNED); |
| 404 | listener->rx.proto = proto; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 405 | LIST_APPEND(&proto->receivers, &listener->rx.proto_list); |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 406 | proto->nb_receivers++; |
| 407 | } |
| 408 | |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 409 | /* default function called to suspend a listener: it simply passes the call to |
| 410 | * the underlying receiver. This is find for most socket-based protocols. This |
Aurelien DARRAGON | 7a15fa5 | 2023-02-07 11:23:38 +0100 | [diff] [blame] | 411 | * must be called under the listener's lock. It will return < 0 in case of |
| 412 | * failure, 0 if the listener was totally stopped, or > 0 if correctly paused.. |
| 413 | * If no receiver-level suspend is provided, the operation is assumed |
| 414 | * to succeed. |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 415 | */ |
| 416 | int default_suspend_listener(struct listener *l) |
| 417 | { |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 418 | if (!l->rx.proto->rx_suspend) |
| 419 | return 1; |
| 420 | |
Aurelien DARRAGON | 7a15fa5 | 2023-02-07 11:23:38 +0100 | [diff] [blame] | 421 | return l->rx.proto->rx_suspend(&l->rx); |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
| 425 | /* Tries to resume a suspended listener, and returns non-zero on success or |
| 426 | * zero on failure. On certain errors, an alert or a warning might be displayed. |
| 427 | * It must be called with the listener's lock held. Depending on the listener's |
| 428 | * state and protocol, a listen() call might be used to resume operations, or a |
| 429 | * call to the receiver's resume() function might be used as well. This is |
| 430 | * suitable as a default function for TCP and UDP. This must be called with the |
| 431 | * listener's lock held. |
| 432 | */ |
| 433 | int default_resume_listener(struct listener *l) |
| 434 | { |
| 435 | int ret = 1; |
| 436 | |
| 437 | if (l->state == LI_ASSIGNED) { |
| 438 | char msg[100]; |
Aurelien DARRAGON | 046a75e | 2023-02-07 12:17:20 +0100 | [diff] [blame] | 439 | char *errmsg; |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 440 | int err; |
| 441 | |
Aurelien DARRAGON | 046a75e | 2023-02-07 12:17:20 +0100 | [diff] [blame] | 442 | /* first, try to bind the receiver */ |
| 443 | err = l->rx.proto->fam->bind(&l->rx, &errmsg); |
| 444 | if (err != ERR_NONE) { |
| 445 | if (err & ERR_WARN) |
| 446 | ha_warning("Resuming listener: %s\n", errmsg); |
| 447 | else if (err & ERR_ALERT) |
| 448 | ha_alert("Resuming listener: %s\n", errmsg); |
| 449 | ha_free(&errmsg); |
| 450 | if (err & (ERR_FATAL | ERR_ABORT)) { |
| 451 | ret = 0; |
| 452 | goto end; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* then, try to listen: |
| 457 | * for now there's still always a listening function |
| 458 | * (same check performed in protocol_bind_all() |
| 459 | */ |
| 460 | BUG_ON(!l->rx.proto->listen); |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 461 | err = l->rx.proto->listen(l, msg, sizeof(msg)); |
| 462 | if (err & ERR_ALERT) |
| 463 | ha_alert("Resuming listener: %s\n", msg); |
| 464 | else if (err & ERR_WARN) |
| 465 | ha_warning("Resuming listener: %s\n", msg); |
| 466 | |
| 467 | if (err & (ERR_FATAL | ERR_ABORT)) { |
| 468 | ret = 0; |
| 469 | goto end; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (l->state < LI_PAUSED) { |
| 474 | ret = 0; |
| 475 | goto end; |
| 476 | } |
| 477 | |
| 478 | if (l->state == LI_PAUSED && l->rx.proto->rx_resume && |
| 479 | l->rx.proto->rx_resume(&l->rx) <= 0) |
| 480 | ret = 0; |
| 481 | end: |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 486 | /* This function tries to temporarily disable a listener, depending on the OS |
| 487 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 488 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 489 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 490 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 491 | * is disabled. It normally returns non-zero, unless an error is reported. |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 492 | * suspend() may totally stop a listener if it doesn't support the PAUSED |
| 493 | * state, in which case state will be set to ASSIGNED. |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 494 | * It will need to operate under the proxy's lock and the listener's lock. |
| 495 | * The caller is responsible for indicating in lpx, lli whether the respective |
| 496 | * locks are already held (non-zero) or not (zero) so that the function pick |
| 497 | * the missing ones, in this order. |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 498 | */ |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 499 | int suspend_listener(struct listener *l, int lpx, int lli) |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 500 | { |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 501 | struct proxy *px = l->bind_conf->frontend; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 502 | int ret = 1; |
| 503 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 504 | if (!lpx && px) |
Aurelien DARRAGON | 0013288 | 2022-09-09 15:32:57 +0200 | [diff] [blame] | 505 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock); |
| 506 | |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 507 | if (!lli) |
| 508 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 509 | |
Aurelien DARRAGON | 2370599 | 2023-02-14 08:51:14 +0100 | [diff] [blame] | 510 | if (!(l->flags & LI_F_FINALIZED) || l->state <= LI_PAUSED) |
Willy Tarreau | 9b3a932 | 2020-09-24 14:46:34 +0200 | [diff] [blame] | 511 | goto end; |
| 512 | |
Aurelien DARRAGON | 7a15fa5 | 2023-02-07 11:23:38 +0100 | [diff] [blame] | 513 | if (l->rx.proto->suspend) { |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 514 | ret = l->rx.proto->suspend(l); |
Aurelien DARRAGON | 7a15fa5 | 2023-02-07 11:23:38 +0100 | [diff] [blame] | 515 | /* if the suspend() fails, we don't want to change the |
| 516 | * current listener state |
| 517 | */ |
| 518 | if (ret < 0) |
| 519 | goto end; |
| 520 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 521 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 522 | MT_LIST_DELETE(&l->wait_queue); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 523 | |
Aurelien DARRAGON | 7a15fa5 | 2023-02-07 11:23:38 +0100 | [diff] [blame] | 524 | /* ret == 0 means that the suspend() has been turned into |
| 525 | * an unbind(), meaning the listener is now stopped (ie: ABNS), we need |
| 526 | * to report this state change properly |
| 527 | */ |
| 528 | listener_set_state(l, ((ret) ? LI_PAUSED : LI_ASSIGNED)); |
| 529 | |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 530 | if (px && !(l->flags & LI_F_SUSPENDED)) |
| 531 | px->li_suspended++; |
| 532 | l->flags |= LI_F_SUSPENDED; |
| 533 | |
Aurelien DARRAGON | 7a15fa5 | 2023-02-07 11:23:38 +0100 | [diff] [blame] | 534 | /* at this point, everything is under control, no error should be |
| 535 | * returned to calling function |
| 536 | */ |
| 537 | ret = 1; |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 538 | |
Aurelien DARRAGON | ca8a4b2 | 2023-02-07 12:36:27 +0100 | [diff] [blame] | 539 | if (px && !(px->flags & PR_FL_PAUSED) && !px->li_ready) { |
Aurelien DARRAGON | d46f437 | 2022-09-09 15:51:37 +0200 | [diff] [blame] | 540 | /* PROXY_LOCK is required */ |
| 541 | proxy_cond_pause(px); |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 542 | ha_warning("Paused %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 543 | send_log(px, LOG_WARNING, "Paused %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 544 | } |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 545 | end: |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 546 | if (!lli) |
| 547 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock); |
Aurelien DARRAGON | 0013288 | 2022-09-09 15:32:57 +0200 | [diff] [blame] | 548 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 549 | if (!lpx && px) |
Aurelien DARRAGON | 0013288 | 2022-09-09 15:32:57 +0200 | [diff] [blame] | 550 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock); |
| 551 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 552 | return ret; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 555 | /* This function tries to resume a temporarily disabled listener. Paused, full, |
| 556 | * limited and disabled listeners are handled, which means that this function |
| 557 | * may replace enable_listener(). The resulting state will either be LI_READY |
| 558 | * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket). |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 559 | * Listeners bound to a different process are not woken up unless we're in |
Willy Tarreau | af2fd58 | 2015-04-14 12:07:16 +0200 | [diff] [blame] | 560 | * foreground mode, and are ignored. If the listener was only in the assigned |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 561 | * state, it's totally rebound. This can happen if a suspend() has completely |
Willy Tarreau | af2fd58 | 2015-04-14 12:07:16 +0200 | [diff] [blame] | 562 | * stopped it. If the resume fails, 0 is returned and an error might be |
| 563 | * displayed. |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 564 | * It will need to operate under the proxy's lock and the listener's lock. |
| 565 | * The caller is responsible for indicating in lpx, lli whether the respective |
| 566 | * locks are already held (non-zero) or not (zero) so that the function pick |
| 567 | * the missing ones, in this order. |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 568 | */ |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 569 | int resume_listener(struct listener *l, int lpx, int lli) |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 570 | { |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 571 | struct proxy *px = l->bind_conf->frontend; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 572 | int ret = 1; |
| 573 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 574 | if (!lpx && px) |
Aurelien DARRAGON | 0013288 | 2022-09-09 15:32:57 +0200 | [diff] [blame] | 575 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock); |
| 576 | |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 577 | if (!lli) |
| 578 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 579 | |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 580 | /* check that another thread didn't to the job in parallel (e.g. at the |
| 581 | * end of listen_accept() while we'd come from dequeue_all_listeners(). |
| 582 | */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 583 | if (MT_LIST_INLIST(&l->wait_queue)) |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 584 | goto end; |
| 585 | |
Aurelien DARRAGON | 2370599 | 2023-02-14 08:51:14 +0100 | [diff] [blame] | 586 | if (!(l->flags & LI_F_FINALIZED) || l->state == LI_READY) |
Willy Tarreau | 5d7f9ce | 2020-09-24 18:54:11 +0200 | [diff] [blame] | 587 | goto end; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 588 | |
Aurelien DARRAGON | 3bb2a38 | 2023-02-07 13:26:14 +0100 | [diff] [blame] | 589 | if (l->rx.proto->resume) { |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 590 | ret = l->rx.proto->resume(l); |
Aurelien DARRAGON | 3bb2a38 | 2023-02-07 13:26:14 +0100 | [diff] [blame] | 591 | if (!ret) |
| 592 | goto end; /* failure to resume */ |
| 593 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 594 | |
Willy Tarreau | 758c69d | 2023-01-12 18:59:37 +0100 | [diff] [blame] | 595 | if (l->bind_conf->maxconn && l->nbconn >= l->bind_conf->maxconn) { |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 596 | l->rx.proto->disable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 597 | listener_set_state(l, LI_FULL); |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 598 | goto done; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 601 | l->rx.proto->enable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 602 | listener_set_state(l, LI_READY); |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 603 | |
| 604 | done: |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 605 | if (px && (l->flags & LI_F_SUSPENDED)) |
| 606 | px->li_suspended--; |
| 607 | l->flags &= ~LI_F_SUSPENDED; |
| 608 | |
Aurelien DARRAGON | ca8a4b2 | 2023-02-07 12:36:27 +0100 | [diff] [blame] | 609 | if (px && (px->flags & PR_FL_PAUSED) && !px->li_suspended) { |
Aurelien DARRAGON | d46f437 | 2022-09-09 15:51:37 +0200 | [diff] [blame] | 610 | /* PROXY_LOCK is required */ |
| 611 | proxy_cond_resume(px); |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 612 | ha_warning("Resumed %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 613 | send_log(px, LOG_WARNING, "Resumed %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 614 | } |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 615 | end: |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 616 | if (!lli) |
| 617 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock); |
Aurelien DARRAGON | 0013288 | 2022-09-09 15:32:57 +0200 | [diff] [blame] | 618 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 619 | if (!lpx && px) |
Aurelien DARRAGON | 0013288 | 2022-09-09 15:32:57 +0200 | [diff] [blame] | 620 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock); |
| 621 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 622 | return ret; |
| 623 | } |
| 624 | |
Aurelien DARRAGON | bcad7e6 | 2023-02-15 09:30:54 +0100 | [diff] [blame] | 625 | /* Same as resume_listener(), but will only work to resume from |
| 626 | * LI_FULL or LI_LIMITED states because we try to relax listeners that |
| 627 | * were temporarily restricted and not to resume inactive listeners that |
| 628 | * may have been paused or completely stopped in the meantime. |
| 629 | * Returns positive value for success and 0 for failure. |
| 630 | * It will need to operate under the proxy's lock and the listener's lock. |
| 631 | * The caller is responsible for indicating in lpx, lli whether the respective |
| 632 | * locks are already held (non-zero) or not (zero) so that the function pick |
| 633 | * the missing ones, in this order. |
| 634 | */ |
| 635 | int relax_listener(struct listener *l, int lpx, int lli) |
| 636 | { |
Christopher Faulet | 6844af6 | 2023-07-20 14:53:50 +0200 | [diff] [blame] | 637 | struct proxy *px = l->bind_conf->frontend; |
Aurelien DARRAGON | bcad7e6 | 2023-02-15 09:30:54 +0100 | [diff] [blame] | 638 | int ret = 1; |
| 639 | |
Christopher Faulet | 6844af6 | 2023-07-20 14:53:50 +0200 | [diff] [blame] | 640 | if (!lpx && px) |
| 641 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock); |
| 642 | |
Aurelien DARRAGON | bcad7e6 | 2023-02-15 09:30:54 +0100 | [diff] [blame] | 643 | if (!lli) |
| 644 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock); |
| 645 | |
| 646 | if (l->state != LI_FULL && l->state != LI_LIMITED) |
| 647 | goto end; /* listener may be suspended or even stopped */ |
Christopher Faulet | 6844af6 | 2023-07-20 14:53:50 +0200 | [diff] [blame] | 648 | ret = resume_listener(l, 1, 1); |
Aurelien DARRAGON | bcad7e6 | 2023-02-15 09:30:54 +0100 | [diff] [blame] | 649 | |
| 650 | end: |
| 651 | if (!lli) |
| 652 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 6844af6 | 2023-07-20 14:53:50 +0200 | [diff] [blame] | 653 | |
| 654 | if (!lpx && px) |
| 655 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock); |
| 656 | |
Aurelien DARRAGON | bcad7e6 | 2023-02-15 09:30:54 +0100 | [diff] [blame] | 657 | return ret; |
| 658 | } |
| 659 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 660 | /* Marks a ready listener as full so that the stream code tries to re-enable |
Aurelien DARRAGON | f5d9893 | 2023-02-06 17:19:58 +0100 | [diff] [blame] | 661 | * it upon next close() using relax_listener(). |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 662 | */ |
Christopher Faulet | 5580ba2 | 2017-08-28 15:29:20 +0200 | [diff] [blame] | 663 | static void listener_full(struct listener *l) |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 664 | { |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 665 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 666 | if (l->state >= LI_READY) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 667 | MT_LIST_DELETE(&l->wait_queue); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 668 | if (l->state != LI_FULL) { |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 669 | l->rx.proto->disable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 670 | listener_set_state(l, LI_FULL); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 671 | } |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 672 | } |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 673 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 676 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 677 | * resources are free again. It will be queued into the specified queue. |
| 678 | */ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 679 | static void limit_listener(struct listener *l, struct mt_list *list) |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 680 | { |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 681 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 682 | if (l->state == LI_READY) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 683 | MT_LIST_TRY_APPEND(list, &l->wait_queue); |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 684 | l->rx.proto->disable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 685 | listener_set_state(l, LI_LIMITED); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 686 | } |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 687 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 690 | /* Dequeues all listeners waiting for a resource the global wait queue */ |
| 691 | void dequeue_all_listeners() |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 692 | { |
Willy Tarreau | 01abd02 | 2019-02-28 10:27:18 +0100 | [diff] [blame] | 693 | struct listener *listener; |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 694 | |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 695 | while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) { |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 696 | /* This cannot fail because the listeners are by definition in |
Willy Tarreau | 01abd02 | 2019-02-28 10:27:18 +0100 | [diff] [blame] | 697 | * the LI_LIMITED state. |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 698 | */ |
Aurelien DARRAGON | f5d9893 | 2023-02-06 17:19:58 +0100 | [diff] [blame] | 699 | relax_listener(listener, 0, 0); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 703 | /* Dequeues all listeners waiting for a resource in proxy <px>'s queue */ |
| 704 | void dequeue_proxy_listeners(struct proxy *px) |
| 705 | { |
| 706 | struct listener *listener; |
| 707 | |
| 708 | while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) { |
| 709 | /* This cannot fail because the listeners are by definition in |
| 710 | * the LI_LIMITED state. |
| 711 | */ |
Aurelien DARRAGON | f5d9893 | 2023-02-06 17:19:58 +0100 | [diff] [blame] | 712 | relax_listener(listener, 0, 0); |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 716 | |
| 717 | /* default function used to unbind a listener. This is for use by standard |
| 718 | * protocols working on top of accepted sockets. The receiver's rx_unbind() |
| 719 | * will automatically be used after the listener is disabled if the socket is |
| 720 | * still bound. This must be used under the listener's lock. |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 721 | */ |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 722 | void default_unbind_listener(struct listener *listener) |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 723 | { |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 724 | if (listener->state <= LI_ASSIGNED) |
| 725 | goto out_close; |
| 726 | |
| 727 | if (listener->rx.fd == -1) { |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 728 | listener_set_state(listener, LI_ASSIGNED); |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 729 | goto out_close; |
| 730 | } |
| 731 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 732 | if (listener->state >= LI_READY) { |
| 733 | listener->rx.proto->disable(listener); |
| 734 | if (listener->rx.flags & RX_F_BOUND) |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 735 | listener_set_state(listener, LI_LISTEN); |
Willy Tarreau | b6607bf | 2020-09-23 16:24:23 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 738 | out_close: |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 739 | if (listener->rx.flags & RX_F_BOUND) |
| 740 | listener->rx.proto->rx_unbind(&listener->rx); |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* This function closes the listening socket for the specified listener, |
| 744 | * provided that it's already in a listening state. The protocol's unbind() |
| 745 | * is called to put the listener into LI_ASSIGNED or LI_LISTEN and handle |
| 746 | * the unbinding tasks. The listener enters then the LI_ASSIGNED state if |
| 747 | * the receiver is unbound. Must be called with the lock held. |
| 748 | */ |
| 749 | void do_unbind_listener(struct listener *listener) |
| 750 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 751 | MT_LIST_DELETE(&listener->wait_queue); |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 752 | |
| 753 | if (listener->rx.proto->unbind) |
| 754 | listener->rx.proto->unbind(listener); |
Willy Tarreau | 374e9af | 2020-10-09 15:47:17 +0200 | [diff] [blame] | 755 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 756 | /* we may have to downgrade the listener if the rx was closed */ |
| 757 | if (!(listener->rx.flags & RX_F_BOUND) && listener->state > LI_ASSIGNED) |
Willy Tarreau | 374e9af | 2020-10-09 15:47:17 +0200 | [diff] [blame] | 758 | listener_set_state(listener, LI_ASSIGNED); |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 761 | /* This function closes the listening socket for the specified listener, |
| 762 | * provided that it's already in a listening state. The listener enters the |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 763 | * LI_ASSIGNED state, except if the FD is not closed, in which case it may |
| 764 | * remain in LI_LISTEN. This function is intended to be used as a generic |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 765 | * function for standard protocols. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 766 | */ |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 767 | void unbind_listener(struct listener *listener) |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 768 | { |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 769 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 770 | do_unbind_listener(listener); |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 771 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 774 | /* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port |
| 775 | * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto |
Willy Tarreau | 9b3178d | 2020-09-16 17:58:55 +0200 | [diff] [blame] | 776 | * allocation). The address family is taken from ss->ss_family, and the protocol |
Willy Tarreau | d2fb99f | 2020-10-15 21:22:29 +0200 | [diff] [blame] | 777 | * passed in <proto> must be usable on this family. The protocol's default iocb |
| 778 | * is automatically preset as the receivers' iocb. The number of jobs and |
Willy Tarreau | 9b3178d | 2020-09-16 17:58:55 +0200 | [diff] [blame] | 779 | * listeners is automatically increased by the number of listeners created. It |
| 780 | * returns non-zero on success, zero on error with the error message set in <err>. |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 781 | */ |
| 782 | int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss, |
Willy Tarreau | 9b3178d | 2020-09-16 17:58:55 +0200 | [diff] [blame] | 783 | int portl, int porth, int fd, struct protocol *proto, char **err) |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 784 | { |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 785 | struct listener *l; |
| 786 | int port; |
| 787 | |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 788 | for (port = portl; port <= porth; port++) { |
| 789 | l = calloc(1, sizeof(*l)); |
| 790 | if (!l) { |
| 791 | memprintf(err, "out of memory"); |
| 792 | return 0; |
| 793 | } |
| 794 | l->obj_type = OBJ_TYPE_LISTENER; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 795 | LIST_APPEND(&bc->frontend->conf.listeners, &l->by_fe); |
| 796 | LIST_APPEND(&bc->listeners, &l->by_bind); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 797 | l->bind_conf = bc; |
Willy Tarreau | 0fce6bc | 2020-09-03 07:46:06 +0200 | [diff] [blame] | 798 | l->rx.settings = &bc->settings; |
Willy Tarreau | eef4542 | 2020-09-03 10:05:03 +0200 | [diff] [blame] | 799 | l->rx.owner = l; |
Willy Tarreau | d2fb99f | 2020-10-15 21:22:29 +0200 | [diff] [blame] | 800 | l->rx.iocb = proto->default_iocb; |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame] | 801 | l->rx.fd = fd; |
Willy Tarreau | 07400c5 | 2020-12-04 14:49:11 +0100 | [diff] [blame] | 802 | |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 803 | memcpy(&l->rx.addr, ss, sizeof(*ss)); |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 804 | if (proto->fam->set_port) |
| 805 | proto->fam->set_port(&l->rx.addr, port); |
Willy Tarreau | 07400c5 | 2020-12-04 14:49:11 +0100 | [diff] [blame] | 806 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 807 | MT_LIST_INIT(&l->wait_queue); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 808 | listener_set_state(l, LI_INIT); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 809 | |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 810 | proto->add(proto, l); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 811 | |
Willy Tarreau | 909c23b | 2020-09-15 13:50:58 +0200 | [diff] [blame] | 812 | if (fd != -1) |
Willy Tarreau | 43046fa | 2020-09-01 15:41:59 +0200 | [diff] [blame] | 813 | l->rx.flags |= RX_F_INHERITED; |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 814 | |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 815 | l->extra_counters = NULL; |
| 816 | |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 817 | HA_RWLOCK_INIT(&l->lock); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 818 | _HA_ATOMIC_INC(&jobs); |
| 819 | _HA_ATOMIC_INC(&listeners); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 820 | } |
| 821 | return 1; |
| 822 | } |
| 823 | |
Willy Tarreau | aae1810 | 2023-03-01 18:25:58 +0100 | [diff] [blame] | 824 | /* Optionally allocates a new shard info (if si == NULL) for receiver rx and |
| 825 | * assigns it to it, or attaches to an existing one. If the rx already had a |
| 826 | * shard_info, it is simply returned. It is illegal to call this function with |
| 827 | * an rx that's part of a group that is already attached. Attaching means the |
| 828 | * shard_info's thread count and group count are updated so the rx's group is |
| 829 | * added to the shard_info's group mask. The rx are added to the members in the |
| 830 | * attachment order, though it must not matter. It is meant for boot time setup |
| 831 | * and is not thread safe. NULL is returned on allocation failure. |
| 832 | */ |
| 833 | struct shard_info *shard_info_attach(struct receiver *rx, struct shard_info *si) |
| 834 | { |
| 835 | if (rx->shard_info) |
| 836 | return rx->shard_info; |
| 837 | |
| 838 | if (!si) { |
| 839 | si = calloc(1, sizeof(*si)); |
| 840 | if (!si) |
| 841 | return NULL; |
| 842 | |
| 843 | si->ref = rx; |
| 844 | } |
| 845 | |
| 846 | rx->shard_info = si; |
| 847 | BUG_ON (si->tgroup_mask & 1UL << (rx->bind_tgroup - 1)); |
| 848 | si->tgroup_mask |= 1UL << (rx->bind_tgroup - 1); |
| 849 | si->nbgroups = my_popcountl(si->tgroup_mask); |
| 850 | si->nbthreads += my_popcountl(rx->bind_thread); |
| 851 | si->members[si->nbgroups - 1] = rx; |
| 852 | return si; |
| 853 | } |
| 854 | |
| 855 | /* Detaches the rx from an optional shard_info it may be attached to. If so, |
| 856 | * the thread counts, group masks and refcounts are updated. The members list |
| 857 | * remains contiguous by replacing the current entry with the last one. The |
| 858 | * reference continues to point to the first receiver. If the group count |
| 859 | * reaches zero, the shard_info is automatically released. |
| 860 | */ |
| 861 | void shard_info_detach(struct receiver *rx) |
| 862 | { |
| 863 | struct shard_info *si = rx->shard_info; |
| 864 | uint gr; |
| 865 | |
| 866 | if (!si) |
| 867 | return; |
| 868 | |
| 869 | rx->shard_info = NULL; |
| 870 | |
| 871 | /* find the member slot this rx was attached to */ |
| 872 | for (gr = 0; gr < MAX_TGROUPS && si->members[gr] != rx; gr++) |
| 873 | ; |
| 874 | |
| 875 | BUG_ON(gr == MAX_TGROUPS); |
| 876 | |
| 877 | si->nbthreads -= my_popcountl(rx->bind_thread); |
| 878 | si->tgroup_mask &= ~(1UL << (rx->bind_tgroup - 1)); |
| 879 | si->nbgroups = my_popcountl(si->tgroup_mask); |
| 880 | |
| 881 | /* replace the member by the last one. If we removed the reference, we |
| 882 | * have to switch to another one. It's always the first entry so we can |
| 883 | * simply enforce it upon every removal. |
| 884 | */ |
| 885 | si->members[gr] = si->members[si->nbgroups]; |
| 886 | si->members[si->nbgroups] = NULL; |
| 887 | si->ref = si->members[0]; |
| 888 | |
| 889 | if (!si->nbgroups) |
| 890 | free(si); |
| 891 | } |
| 892 | |
Willy Tarreau | 59a877d | 2021-10-12 09:36:10 +0200 | [diff] [blame] | 893 | /* clones listener <src> and returns the new one. All dynamically allocated |
| 894 | * fields are reallocated (name for now). The new listener is inserted before |
| 895 | * the original one in the bind_conf and frontend lists. This allows it to be |
| 896 | * duplicated while iterating over the current list. The original listener must |
| 897 | * only be in the INIT or ASSIGNED states, and the new listener will only be |
| 898 | * placed into the INIT state. The counters are always set to NULL. Maxsock is |
Willy Tarreau | aae1810 | 2023-03-01 18:25:58 +0100 | [diff] [blame] | 899 | * updated. Returns NULL on allocation error. The shard_info is never taken so |
| 900 | * that the caller can decide what to do with it depending on how it intends to |
| 901 | * clone the listener. |
Willy Tarreau | 59a877d | 2021-10-12 09:36:10 +0200 | [diff] [blame] | 902 | */ |
| 903 | struct listener *clone_listener(struct listener *src) |
| 904 | { |
| 905 | struct listener *l; |
| 906 | |
| 907 | l = calloc(1, sizeof(*l)); |
| 908 | if (!l) |
| 909 | goto oom1; |
| 910 | memcpy(l, src, sizeof(*l)); |
| 911 | |
| 912 | if (l->name) { |
| 913 | l->name = strdup(l->name); |
| 914 | if (!l->name) |
| 915 | goto oom2; |
| 916 | } |
| 917 | |
| 918 | l->rx.owner = l; |
Willy Tarreau | aae1810 | 2023-03-01 18:25:58 +0100 | [diff] [blame] | 919 | l->rx.shard_info = NULL; |
Willy Tarreau | 59a877d | 2021-10-12 09:36:10 +0200 | [diff] [blame] | 920 | l->state = LI_INIT; |
| 921 | l->counters = NULL; |
| 922 | l->extra_counters = NULL; |
| 923 | |
| 924 | LIST_APPEND(&src->by_fe, &l->by_fe); |
| 925 | LIST_APPEND(&src->by_bind, &l->by_bind); |
| 926 | |
| 927 | MT_LIST_INIT(&l->wait_queue); |
| 928 | |
| 929 | l->rx.proto->add(l->rx.proto, l); |
| 930 | |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 931 | HA_RWLOCK_INIT(&l->lock); |
Willy Tarreau | 59a877d | 2021-10-12 09:36:10 +0200 | [diff] [blame] | 932 | _HA_ATOMIC_INC(&jobs); |
| 933 | _HA_ATOMIC_INC(&listeners); |
| 934 | global.maxsock++; |
| 935 | return l; |
| 936 | |
Willy Tarreau | 59a877d | 2021-10-12 09:36:10 +0200 | [diff] [blame] | 937 | oom2: |
| 938 | free(l); |
| 939 | oom1: |
Willy Tarreau | a146289 | 2021-10-16 14:45:29 +0200 | [diff] [blame] | 940 | return NULL; |
Willy Tarreau | 59a877d | 2021-10-12 09:36:10 +0200 | [diff] [blame] | 941 | } |
| 942 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 943 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 944 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
Willy Tarreau | 2cc5bae | 2017-09-15 08:18:11 +0200 | [diff] [blame] | 945 | * number of listeners is updated, as well as the global number of listeners |
| 946 | * and jobs. Note that the listener must have previously been unbound. This |
Willy Tarreau | b4c083f | 2020-10-07 15:36:16 +0200 | [diff] [blame] | 947 | * is a low-level function expected to be called with the proto_lock and the |
| 948 | * listener's lock held. |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 949 | */ |
Willy Tarreau | b4c083f | 2020-10-07 15:36:16 +0200 | [diff] [blame] | 950 | void __delete_listener(struct listener *listener) |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 951 | { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 952 | if (listener->state == LI_ASSIGNED) { |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 953 | listener_set_state(listener, LI_INIT); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 954 | LIST_DELETE(&listener->rx.proto_list); |
Willy Tarreau | aae1810 | 2023-03-01 18:25:58 +0100 | [diff] [blame] | 955 | shard_info_detach(&listener->rx); |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 956 | listener->rx.proto->nb_receivers--; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 957 | _HA_ATOMIC_DEC(&jobs); |
| 958 | _HA_ATOMIC_DEC(&listeners); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 959 | } |
Willy Tarreau | b4c083f | 2020-10-07 15:36:16 +0200 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* Delete a listener from its protocol's list of listeners (please check |
| 963 | * __delete_listener() above). The proto_lock and the listener's lock will |
| 964 | * be grabbed in this order. |
| 965 | */ |
| 966 | void delete_listener(struct listener *listener) |
| 967 | { |
| 968 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 969 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | b4c083f | 2020-10-07 15:36:16 +0200 | [diff] [blame] | 970 | __delete_listener(listener); |
Willy Tarreau | 08b6f96 | 2022-02-01 16:23:00 +0100 | [diff] [blame] | 971 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | 6ee9f8d | 2019-08-26 10:55:52 +0200 | [diff] [blame] | 972 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 973 | } |
| 974 | |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 975 | /* Returns a suitable value for a listener's backlog. It uses the listener's, |
| 976 | * otherwise the frontend's backlog, otherwise the listener's maxconn, |
| 977 | * otherwise the frontend's maxconn, otherwise 1024. |
| 978 | */ |
| 979 | int listener_backlog(const struct listener *l) |
| 980 | { |
Willy Tarreau | 1920f89 | 2023-01-12 18:55:13 +0100 | [diff] [blame] | 981 | if (l->bind_conf->backlog) |
| 982 | return l->bind_conf->backlog; |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 983 | |
| 984 | if (l->bind_conf->frontend->backlog) |
| 985 | return l->bind_conf->frontend->backlog; |
| 986 | |
Willy Tarreau | 758c69d | 2023-01-12 18:59:37 +0100 | [diff] [blame] | 987 | if (l->bind_conf->maxconn) |
| 988 | return l->bind_conf->maxconn; |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 989 | |
| 990 | if (l->bind_conf->frontend->maxconn) |
| 991 | return l->bind_conf->frontend->maxconn; |
| 992 | |
| 993 | return 1024; |
| 994 | } |
| 995 | |
Amaury Denoyelle | 331b8b1 | 2023-10-25 10:52:23 +0200 | [diff] [blame^] | 996 | /* Returns true if listener <l> must check maxconn limit prior to accept. */ |
| 997 | static inline int listener_uses_maxconn(const struct listener *l) |
| 998 | { |
| 999 | return !(l->bind_conf->options & (BC_O_UNLIMITED|BC_O_XPRT_MAXCONN)); |
| 1000 | } |
| 1001 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1002 | /* This function is called on a read event from a listening socket, corresponding |
| 1003 | * to an accept. It tries to accept as many connections as possible, and for each |
| 1004 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 1005 | */ |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 1006 | void listener_accept(struct listener *l) |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1007 | { |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 1008 | struct connection *cli_conn; |
Olivier Houchard | d16a9df | 2019-02-25 16:18:16 +0100 | [diff] [blame] | 1009 | struct proxy *p; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 1010 | unsigned int max_accept; |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1011 | int next_conn = 0; |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1012 | int next_feconn = 0; |
| 1013 | int next_actconn = 0; |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 1014 | int expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1015 | int ret; |
| 1016 | |
Olivier Houchard | d16a9df | 2019-02-25 16:18:16 +0100 | [diff] [blame] | 1017 | p = l->bind_conf->frontend; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 1018 | |
Willy Tarreau | 882f248 | 2023-01-12 18:52:23 +0100 | [diff] [blame] | 1019 | /* if l->bind_conf->maxaccept is -1, then max_accept is UINT_MAX. It is |
| 1020 | * not really illimited, but it is probably enough. |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 1021 | */ |
Willy Tarreau | 882f248 | 2023-01-12 18:52:23 +0100 | [diff] [blame] | 1022 | max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1023 | |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 1024 | if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.sps_lim) { |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1025 | int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0); |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1026 | |
| 1027 | if (unlikely(!max)) { |
| 1028 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1029 | expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0)); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1030 | goto limit_global; |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | if (max_accept > max) |
| 1034 | max_accept = max; |
| 1035 | } |
| 1036 | |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 1037 | if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.cps_lim) { |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1038 | int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); |
| 1039 | |
| 1040 | if (unlikely(!max)) { |
| 1041 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1042 | expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0)); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1043 | goto limit_global; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | if (max_accept > max) |
| 1047 | max_accept = max; |
| 1048 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1049 | #ifdef USE_OPENSSL |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 1050 | if (!(l->bind_conf->options & BC_O_UNLIMITED) && global.ssl_lim && |
Willy Tarreau | 11ba404 | 2022-05-20 15:56:32 +0200 | [diff] [blame] | 1051 | l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) { |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1052 | int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1053 | |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1054 | if (unlikely(!max)) { |
| 1055 | /* frontend accept rate limit was reached */ |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1056 | expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0)); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1057 | goto limit_global; |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | if (max_accept > max) |
| 1061 | max_accept = max; |
| 1062 | } |
| 1063 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1064 | if (p && p->fe_sps_lim) { |
| 1065 | int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); |
| 1066 | |
| 1067 | if (unlikely(!max)) { |
| 1068 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1069 | expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0)); |
| 1070 | goto limit_proxy; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (max_accept > max) |
| 1074 | max_accept = max; |
| 1075 | } |
| 1076 | |
| 1077 | /* Note: if we fail to allocate a connection because of configured |
| 1078 | * limits, we'll schedule a new attempt worst 1 second later in the |
| 1079 | * worst case. If we fail due to system limits or temporary resource |
| 1080 | * shortage, we try again 100ms later in the worst case. |
| 1081 | */ |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 1082 | for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1083 | unsigned int count; |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 1084 | int status; |
Willy Tarreau | 0aa5a5b | 2020-10-16 17:43:04 +0200 | [diff] [blame] | 1085 | __decl_thread(unsigned long mask); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1086 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1087 | /* pre-increase the number of connections without going too far. |
| 1088 | * We process the listener, then the proxy, then the process. |
| 1089 | * We know which ones to unroll based on the next_xxx value. |
| 1090 | */ |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1091 | do { |
| 1092 | count = l->nbconn; |
Willy Tarreau | 758c69d | 2023-01-12 18:59:37 +0100 | [diff] [blame] | 1093 | if (unlikely(l->bind_conf->maxconn && count >= l->bind_conf->maxconn)) { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1094 | /* the listener was marked full or another |
| 1095 | * thread is going to do it. |
| 1096 | */ |
| 1097 | next_conn = 0; |
Willy Tarreau | 93604ed | 2019-11-15 10:20:07 +0100 | [diff] [blame] | 1098 | listener_full(l); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1099 | goto end; |
| 1100 | } |
| 1101 | next_conn = count + 1; |
David Carlier | 5671662 | 2019-03-27 16:08:42 +0000 | [diff] [blame] | 1102 | } while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn)); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1103 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1104 | if (p) { |
| 1105 | do { |
| 1106 | count = p->feconn; |
Willy Tarreau | 93604ed | 2019-11-15 10:20:07 +0100 | [diff] [blame] | 1107 | if (unlikely(count >= p->maxconn)) { |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1108 | /* the frontend was marked full or another |
| 1109 | * thread is going to do it. |
| 1110 | */ |
| 1111 | next_feconn = 0; |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1112 | expire = TICK_ETERNITY; |
| 1113 | goto limit_proxy; |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1114 | } |
| 1115 | next_feconn = count + 1; |
Olivier Houchard | 64213e9 | 2019-03-08 18:52:57 +0100 | [diff] [blame] | 1116 | } while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn)); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Amaury Denoyelle | 331b8b1 | 2023-10-25 10:52:23 +0200 | [diff] [blame^] | 1119 | if (listener_uses_maxconn(l)) { |
Amaury Denoyelle | 8ee9fc7 | 2023-10-25 15:32:28 +0200 | [diff] [blame] | 1120 | next_actconn = increment_actconn(); |
| 1121 | if (!next_actconn) { |
| 1122 | /* the process was marked full or another |
| 1123 | * thread is going to do it. |
| 1124 | */ |
| 1125 | expire = tick_add(now_ms, 1000); /* try again in 1 second */ |
| 1126 | goto limit_global; |
| 1127 | } |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Willy Tarreau | fed93d3 | 2022-02-01 16:37:00 +0100 | [diff] [blame] | 1130 | /* be careful below, the listener might be shutting down in |
| 1131 | * another thread on error and we must not dereference its |
| 1132 | * FD without a bit of protection. |
| 1133 | */ |
| 1134 | cli_conn = NULL; |
| 1135 | status = CO_AC_PERMERR; |
| 1136 | |
| 1137 | HA_RWLOCK_RDLOCK(LISTENER_LOCK, &l->lock); |
| 1138 | if (l->rx.flags & RX_F_BOUND) |
| 1139 | cli_conn = l->rx.proto->accept_conn(l, &status); |
| 1140 | HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &l->lock); |
| 1141 | |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 1142 | if (!cli_conn) { |
| 1143 | switch (status) { |
| 1144 | case CO_AC_DONE: |
| 1145 | goto end; |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 1146 | |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 1147 | case CO_AC_RETRY: /* likely a signal */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1148 | _HA_ATOMIC_DEC(&l->nbconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1149 | if (p) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1150 | _HA_ATOMIC_DEC(&p->feconn); |
Amaury Denoyelle | 331b8b1 | 2023-10-25 10:52:23 +0200 | [diff] [blame^] | 1151 | if (listener_uses_maxconn(l)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1152 | _HA_ATOMIC_DEC(&actconn); |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 1153 | continue; |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 1154 | |
| 1155 | case CO_AC_YIELD: |
Willy Tarreau | 9207993 | 2019-12-10 09:30:05 +0100 | [diff] [blame] | 1156 | max_accept = 0; |
| 1157 | goto end; |
William Lallemand | d913800 | 2018-11-27 12:02:39 +0100 | [diff] [blame] | 1158 | |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 1159 | default: |
| 1160 | goto transient_error; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1164 | /* The connection was accepted, it must be counted as such */ |
| 1165 | if (l->counters) |
| 1166 | HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn); |
| 1167 | |
Willy Tarreau | d867934 | 2022-05-09 20:41:54 +0200 | [diff] [blame] | 1168 | if (p) { |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1169 | HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn); |
Willy Tarreau | d867934 | 2022-05-09 20:41:54 +0200 | [diff] [blame] | 1170 | proxy_inc_fe_conn_ctr(l, p); |
| 1171 | } |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1172 | |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 1173 | if (!(l->bind_conf->options & BC_O_UNLIMITED)) { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1174 | count = update_freq_ctr(&global.conn_per_sec, 1); |
| 1175 | HA_ATOMIC_UPDATE_MAX(&global.cps_max, count); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1178 | _HA_ATOMIC_INC(&activity[tid].accepted); |
Willy Tarreau | 64a9c05 | 2019-04-12 15:27:17 +0200 | [diff] [blame] | 1179 | |
Willy Tarreau | 9615102 | 2023-05-11 13:51:31 +0200 | [diff] [blame] | 1180 | /* count the number of times an accepted connection resulted in |
| 1181 | * maxconn being reached. |
| 1182 | */ |
| 1183 | if (unlikely(_HA_ATOMIC_LOAD(&actconn) + 1 >= global.maxconn)) |
| 1184 | _HA_ATOMIC_INC(&maxconn_reached); |
| 1185 | |
Willy Tarreau | 3083615 | 2023-01-12 19:10:17 +0100 | [diff] [blame] | 1186 | /* past this point, l->bind_conf->accept() will automatically decrement |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1187 | * l->nbconn, feconn and actconn once done. Setting next_*conn=0 |
| 1188 | * allows the error path not to rollback on nbconn. It's more |
| 1189 | * convenient than duplicating all exit labels. |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1190 | */ |
| 1191 | next_conn = 0; |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1192 | next_feconn = 0; |
| 1193 | next_actconn = 0; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1194 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 1195 | |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1196 | #if defined(USE_THREAD) |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1197 | if (!(global.tune.options & GTUNE_LISTENER_MQ_ANY) || stopping) |
| 1198 | goto local_accept; |
| 1199 | |
| 1200 | /* we want to perform thread rebalancing if the listener is |
| 1201 | * bound to more than one thread or if it's part of a shard |
| 1202 | * with more than one listener. |
| 1203 | */ |
Willy Tarreau | b2f38c1 | 2023-01-19 19:14:18 +0100 | [diff] [blame] | 1204 | mask = l->rx.bind_thread & _HA_ATOMIC_LOAD(&tg->threads_enabled); |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1205 | if (l->rx.shard_info || atleast2(mask)) { |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1206 | struct accept_queue_ring *ring; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1207 | struct listener *new_li; |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1208 | uint r1, r2, t, t1, t2; |
| 1209 | ulong n0, n1; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1210 | const struct tgroup_info *g1, *g2; |
| 1211 | ulong m1, m2; |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1212 | ulong *thr_idx_ptr; |
Willy Tarreau | fc630bd | 2019-03-04 19:57:34 +0100 | [diff] [blame] | 1213 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1214 | /* The principle is that we have two running indexes, |
| 1215 | * each visiting in turn all threads bound to this |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1216 | * listener's shard. The connection will be assigned to |
| 1217 | * the one with the least connections, and the other |
| 1218 | * one will be updated. This provides a good fairness |
| 1219 | * on short connections (round robin) and on long ones |
| 1220 | * (conn count), without ever missing any idle thread. |
| 1221 | * Each thread number is encoded as a combination of |
| 1222 | * times the receiver number and its local thread |
| 1223 | * number from 0 to MAX_THREADS_PER_GROUP - 1. The two |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1224 | * indexes are stored as 10/12 bit numbers in the thr_idx |
| 1225 | * array, since there are up to LONGBITS threads and |
| 1226 | * groups that can be represented. They are represented |
| 1227 | * like this: |
| 1228 | * 31:20 19:15 14:10 9:5 4:0 |
| 1229 | * 32b: [ counter | r2num | t2num | r1num | t1num ] |
| 1230 | * |
| 1231 | * 63:24 23:18 17:12 11:6 5:0 |
| 1232 | * 64b: [ counter | r2num | t2num | r1num | t1num ] |
| 1233 | * |
| 1234 | * The change counter is only used to avoid swapping too |
| 1235 | * old a value when the value loops back. |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1236 | * |
| 1237 | * In the loop below we have this for each index: |
| 1238 | * - n is the thread index |
| 1239 | * - r is the receiver number |
| 1240 | * - g is the receiver's thread group |
| 1241 | * - t is the thread number in this receiver |
| 1242 | * - m is the receiver's thread mask shifted by the thread number |
Willy Tarreau | fc630bd | 2019-03-04 19:57:34 +0100 | [diff] [blame] | 1243 | */ |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1244 | |
| 1245 | /* keep a copy for the final update. thr_idx is composite |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1246 | * and made of (n2<<16) + n1. |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1247 | */ |
Willy Tarreau | b657492 | 2023-03-29 17:02:17 +0200 | [diff] [blame] | 1248 | thr_idx_ptr = l->rx.shard_info ? &((struct listener *)(l->rx.shard_info->ref->owner))->thr_idx : &l->thr_idx; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1249 | while (1) { |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1250 | int q0, q1, q2; |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1251 | |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1252 | /* calculate r1/g1/t1 first (ascending idx) */ |
| 1253 | n0 = _HA_ATOMIC_LOAD(thr_idx_ptr); |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1254 | new_li = NULL; |
| 1255 | |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1256 | t1 = (uint)n0 & (LONGBITS - 1); |
| 1257 | r1 = ((uint)n0 / LONGBITS) & (LONGBITS - 1); |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1258 | |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1259 | while (1) { |
| 1260 | if (l->rx.shard_info) { |
| 1261 | /* multiple listeners, take the group into account */ |
| 1262 | if (r1 >= l->rx.shard_info->nbgroups) |
| 1263 | r1 = 0; |
| 1264 | |
| 1265 | g1 = &ha_tgroup_info[l->rx.shard_info->members[r1]->bind_tgroup - 1]; |
| 1266 | m1 = l->rx.shard_info->members[r1]->bind_thread; |
| 1267 | } else { |
| 1268 | /* single listener */ |
| 1269 | r1 = 0; |
| 1270 | g1 = tg; |
| 1271 | m1 = l->rx.bind_thread; |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1272 | } |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1273 | m1 &= _HA_ATOMIC_LOAD(&g1->threads_enabled); |
| 1274 | m1 >>= t1; |
| 1275 | |
| 1276 | /* find first existing thread */ |
| 1277 | if (unlikely(!(m1 & 1))) { |
| 1278 | m1 &= ~1UL; |
| 1279 | if (!m1) { |
| 1280 | /* no more threads here, switch to |
| 1281 | * first thread of next group. |
| 1282 | */ |
| 1283 | t1 = 0; |
| 1284 | if (l->rx.shard_info) |
| 1285 | r1++; |
| 1286 | /* loop again */ |
| 1287 | continue; |
| 1288 | } |
| 1289 | t1 += my_ffsl(m1) - 1; |
| 1290 | } |
| 1291 | /* done: r1 and t1 are OK */ |
| 1292 | break; |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1293 | } |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1294 | |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1295 | /* now r2/g2/t2 (descending idx) */ |
| 1296 | t2 = ((uint)n0 / LONGBITS / LONGBITS) & (LONGBITS - 1); |
| 1297 | r2 = ((uint)n0 / LONGBITS / LONGBITS / LONGBITS) & (LONGBITS - 1); |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1298 | |
Willy Tarreau | 84fe1f4 | 2023-04-20 15:40:38 +0200 | [diff] [blame] | 1299 | /* if running in round-robin mode ("fair"), we don't need |
| 1300 | * to go further. |
| 1301 | */ |
| 1302 | if ((global.tune.options & GTUNE_LISTENER_MQ_ANY) == GTUNE_LISTENER_MQ_FAIR) { |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1303 | t = g1->base + t1; |
| 1304 | if (l->rx.shard_info && t != tid) |
| 1305 | new_li = l->rx.shard_info->members[r1]->owner; |
Willy Tarreau | 84fe1f4 | 2023-04-20 15:40:38 +0200 | [diff] [blame] | 1306 | goto updt_t1; |
| 1307 | } |
| 1308 | |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1309 | while (1) { |
| 1310 | if (l->rx.shard_info) { |
| 1311 | /* multiple listeners, take the group into account */ |
| 1312 | if (r2 >= l->rx.shard_info->nbgroups) |
| 1313 | r2 = l->rx.shard_info->nbgroups - 1; |
Willy Tarreau | 85d0424 | 2019-04-16 18:09:13 +0200 | [diff] [blame] | 1314 | |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1315 | g2 = &ha_tgroup_info[l->rx.shard_info->members[r2]->bind_tgroup - 1]; |
| 1316 | m2 = l->rx.shard_info->members[r2]->bind_thread; |
| 1317 | } else { |
| 1318 | /* single listener */ |
| 1319 | r2 = 0; |
| 1320 | g2 = tg; |
| 1321 | m2 = l->rx.bind_thread; |
| 1322 | } |
| 1323 | m2 &= _HA_ATOMIC_LOAD(&g2->threads_enabled); |
| 1324 | m2 &= nbits(t2 + 1); |
| 1325 | |
| 1326 | /* find previous existing thread */ |
| 1327 | if (unlikely(!(m2 & (1UL << t2)) || (g1 == g2 && t1 == t2))) { |
| 1328 | /* highest bit not set or colliding threads, let's check |
| 1329 | * if we still have other threads available after this |
| 1330 | * one. |
| 1331 | */ |
| 1332 | m2 &= ~(1UL << t2); |
| 1333 | if (!m2) { |
| 1334 | /* no more threads here, switch to |
| 1335 | * last thread of previous group. |
| 1336 | */ |
| 1337 | t2 = MAX_THREADS_PER_GROUP - 1; |
| 1338 | if (l->rx.shard_info) |
| 1339 | r2--; |
| 1340 | /* loop again */ |
| 1341 | continue; |
| 1342 | } |
| 1343 | t2 = my_flsl(m2) - 1; |
| 1344 | } |
| 1345 | /* done: r2 and t2 are OK */ |
| 1346 | break; |
Willy Tarreau | 85d0424 | 2019-04-16 18:09:13 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Willy Tarreau | 77e3350 | 2023-04-19 17:19:28 +0200 | [diff] [blame] | 1349 | /* tests show that it's worth checking that other threads have not |
| 1350 | * already changed the index to save the rest of the calculation, |
| 1351 | * or we'd have to redo it anyway. |
| 1352 | */ |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1353 | if (n0 != _HA_ATOMIC_LOAD(thr_idx_ptr)) |
Willy Tarreau | 77e3350 | 2023-04-19 17:19:28 +0200 | [diff] [blame] | 1354 | continue; |
Willy Tarreau | 77e3350 | 2023-04-19 17:19:28 +0200 | [diff] [blame] | 1355 | |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1356 | /* here we have (r1,g1,t1) that designate the first receiver, its |
| 1357 | * thread group and local thread, and (r2,g2,t2) that designate |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1358 | * the second receiver, its thread group and local thread. We'll |
| 1359 | * also consider the local thread with q0. |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1360 | */ |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1361 | q0 = accept_queue_ring_len(&accept_queue_rings[tid]); |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1362 | q1 = accept_queue_ring_len(&accept_queue_rings[g1->base + t1]); |
| 1363 | q2 = accept_queue_ring_len(&accept_queue_rings[g2->base + t2]); |
| 1364 | |
| 1365 | /* add to this the currently active connections */ |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1366 | q0 += _HA_ATOMIC_LOAD(&l->thr_conn[ti->ltid]); |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1367 | if (l->rx.shard_info) { |
| 1368 | q1 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r1]->owner)->thr_conn[t1]); |
| 1369 | q2 += _HA_ATOMIC_LOAD(&((struct listener *)l->rx.shard_info->members[r2]->owner)->thr_conn[t2]); |
| 1370 | } else { |
| 1371 | q1 += _HA_ATOMIC_LOAD(&l->thr_conn[t1]); |
| 1372 | q2 += _HA_ATOMIC_LOAD(&l->thr_conn[t2]); |
| 1373 | } |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1374 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1375 | /* we have 3 possibilities now : |
| 1376 | * q1 < q2 : t1 is less loaded than t2, so we pick it |
| 1377 | * and update t2 (since t1 might still be |
| 1378 | * lower than another thread) |
| 1379 | * q1 > q2 : t2 is less loaded than t1, so we pick it |
| 1380 | * and update t1 (since t2 might still be |
| 1381 | * lower than another thread) |
| 1382 | * q1 = q2 : both are equally loaded, thus we pick t1 |
| 1383 | * and update t1 as it will become more loaded |
| 1384 | * than t2. |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1385 | * On top of that, if in the end the current thread appears |
| 1386 | * to be as good of a deal, we'll prefer it over a foreign |
| 1387 | * one as it will improve locality and avoid a migration. |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1388 | */ |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1389 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1390 | if (q1 - q2 < 0) { |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1391 | t = g1->base + t1; |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1392 | if (q0 <= q1) |
| 1393 | t = tid; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1394 | |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1395 | if (l->rx.shard_info && t != tid) |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1396 | new_li = l->rx.shard_info->members[r1]->owner; |
| 1397 | |
| 1398 | t2--; |
| 1399 | if (t2 >= MAX_THREADS_PER_GROUP) { |
| 1400 | if (l->rx.shard_info) |
| 1401 | r2--; |
| 1402 | t2 = MAX_THREADS_PER_GROUP - 1; |
| 1403 | } |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1404 | } |
| 1405 | else if (q1 - q2 > 0) { |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1406 | t = g2->base + t2; |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1407 | if (q0 <= q2) |
| 1408 | t = tid; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1409 | |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1410 | if (l->rx.shard_info && t != tid) |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1411 | new_li = l->rx.shard_info->members[r2]->owner; |
| 1412 | goto updt_t1; |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1413 | } |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1414 | else { // q1 == q2 |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1415 | t = g1->base + t1; |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1416 | if (q0 < q1) // local must be strictly better than both |
| 1417 | t = tid; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1418 | |
Willy Tarreau | 8adffaa | 2023-04-19 18:06:16 +0200 | [diff] [blame] | 1419 | if (l->rx.shard_info && t != tid) |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1420 | new_li = l->rx.shard_info->members[r1]->owner; |
Willy Tarreau | 84fe1f4 | 2023-04-20 15:40:38 +0200 | [diff] [blame] | 1421 | updt_t1: |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1422 | t1++; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1423 | if (t1 >= MAX_THREADS_PER_GROUP) { |
| 1424 | if (l->rx.shard_info) |
| 1425 | r1++; |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1426 | t1 = 0; |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1427 | } |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1428 | } |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1429 | |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1430 | /* The target thread number is in <t> now. Let's |
| 1431 | * compute the new index and try to update it. |
| 1432 | */ |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1433 | |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1434 | /* take previous counter and increment it */ |
| 1435 | n1 = n0 & -(ulong)(LONGBITS * LONGBITS * LONGBITS * LONGBITS); |
| 1436 | n1 += LONGBITS * LONGBITS * LONGBITS * LONGBITS; |
| 1437 | n1 += (((r2 * LONGBITS) + t2) * LONGBITS * LONGBITS); |
| 1438 | n1 += (r1 * LONGBITS) + t1; |
Willy Tarreau | b657492 | 2023-03-29 17:02:17 +0200 | [diff] [blame] | 1439 | if (likely(_HA_ATOMIC_CAS(thr_idx_ptr, &n0, n1))) |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1440 | break; |
Willy Tarreau | ff18504 | 2023-04-20 16:52:21 +0200 | [diff] [blame] | 1441 | |
| 1442 | /* bah we lost the race, try again */ |
| 1443 | __ha_cpu_relax(); |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1444 | } /* end of main while() loop */ |
| 1445 | |
| 1446 | /* we may need to update the listener in the connection |
| 1447 | * if we switched to another group. |
| 1448 | */ |
| 1449 | if (new_li) |
| 1450 | cli_conn->target = &new_li->obj_type; |
| 1451 | |
| 1452 | /* here we have the target thread number in <t> and we hold a |
| 1453 | * reservation in the target ring. |
| 1454 | */ |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1455 | |
Amaury Denoyelle | a66e043 | 2023-04-05 18:16:28 +0200 | [diff] [blame] | 1456 | if (l->rx.proto && l->rx.proto->set_affinity) { |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1457 | if (l->rx.proto->set_affinity(cli_conn, t)) { |
Amaury Denoyelle | a66e043 | 2023-04-05 18:16:28 +0200 | [diff] [blame] | 1458 | /* Failed migration, stay on the same thread. */ |
| 1459 | goto local_accept; |
| 1460 | } |
| 1461 | } |
| 1462 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1463 | /* We successfully selected the best thread "t" for this |
| 1464 | * connection. We use deferred accepts even if it's the |
| 1465 | * local thread because tests show that it's the best |
| 1466 | * performing model, likely due to better cache locality |
| 1467 | * when processing this loop. |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1468 | */ |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1469 | ring = &accept_queue_rings[t]; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 1470 | if (accept_queue_push_mp(ring, cli_conn)) { |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1471 | _HA_ATOMIC_INC(&activity[t].accq_pushed); |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 1472 | tasklet_wakeup(ring->tasklet); |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1473 | continue; |
| 1474 | } |
| 1475 | /* If the ring is full we do a synchronous accept on |
| 1476 | * the local thread here. |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1477 | */ |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1478 | _HA_ATOMIC_INC(&activity[t].accq_full); |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1479 | } |
| 1480 | #endif // USE_THREAD |
| 1481 | |
Amaury Denoyelle | 7f7713d | 2022-01-19 11:37:50 +0100 | [diff] [blame] | 1482 | local_accept: |
Willy Tarreau | 9d36060 | 2023-03-27 10:38:51 +0200 | [diff] [blame] | 1483 | /* restore the connection's listener in case we failed to migrate above */ |
| 1484 | cli_conn->target = &l->obj_type; |
Willy Tarreau | fea8c19 | 2023-02-28 10:25:57 +0100 | [diff] [blame] | 1485 | _HA_ATOMIC_INC(&l->thr_conn[ti->ltid]); |
Willy Tarreau | 3083615 | 2023-01-12 19:10:17 +0100 | [diff] [blame] | 1486 | ret = l->bind_conf->accept(cli_conn); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1487 | if (unlikely(ret <= 0)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1488 | /* The connection was closed by stream_accept(). Either |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1489 | * we just have to ignore it (ret == 0) or it's a critical |
| 1490 | * error due to a resource shortage, and we must stop the |
| 1491 | * listener (ret < 0). |
| 1492 | */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1493 | if (ret == 0) /* successful termination */ |
| 1494 | continue; |
| 1495 | |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 1496 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1499 | /* increase the per-process number of cumulated sessions, this |
Willy Tarreau | 3083615 | 2023-01-12 19:10:17 +0100 | [diff] [blame] | 1500 | * may only be done once l->bind_conf->accept() has accepted the |
| 1501 | * connection. |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1502 | */ |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 1503 | if (!(l->bind_conf->options & BC_O_UNLIMITED)) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1504 | count = update_freq_ctr(&global.sess_per_sec, 1); |
| 1505 | HA_ATOMIC_UPDATE_MAX(&global.sps_max, count); |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1506 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1507 | #ifdef USE_OPENSSL |
Willy Tarreau | 1714680 | 2023-01-12 19:58:42 +0100 | [diff] [blame] | 1508 | if (!(l->bind_conf->options & BC_O_UNLIMITED) && |
Willy Tarreau | 11ba404 | 2022-05-20 15:56:32 +0200 | [diff] [blame] | 1509 | l->bind_conf && l->bind_conf->options & BC_O_USE_SSL) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1510 | count = update_freq_ctr(&global.ssl_per_sec, 1); |
| 1511 | HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count); |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1512 | } |
| 1513 | #endif |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1514 | |
Willy Tarreau | bdcd325 | 2022-06-22 09:19:46 +0200 | [diff] [blame] | 1515 | _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_STUCK); // this thread is still running |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1516 | } /* end of for (max_accept--) */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1517 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1518 | end: |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1519 | if (next_conn) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1520 | _HA_ATOMIC_DEC(&l->nbconn); |
Willy Tarreau | 741b4d6 | 2019-02-25 15:02:04 +0100 | [diff] [blame] | 1521 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1522 | if (p && next_feconn) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1523 | _HA_ATOMIC_DEC(&p->feconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1524 | |
| 1525 | if (next_actconn) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1526 | _HA_ATOMIC_DEC(&actconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1527 | |
Willy Tarreau | 758c69d | 2023-01-12 18:59:37 +0100 | [diff] [blame] | 1528 | if ((l->state == LI_FULL && (!l->bind_conf->maxconn || l->nbconn < l->bind_conf->maxconn)) || |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 1529 | (l->state == LI_LIMITED && |
Willy Tarreau | cdcba11 | 2019-12-11 15:06:30 +0100 | [diff] [blame] | 1530 | ((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) && |
| 1531 | (!tick_isset(global_listener_queue_task->expire) || |
| 1532 | tick_is_expired(global_listener_queue_task->expire, now_ms))))) { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1533 | /* at least one thread has to this when quitting */ |
Aurelien DARRAGON | f5d9893 | 2023-02-06 17:19:58 +0100 | [diff] [blame] | 1534 | relax_listener(l, 0, 0); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1535 | |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 1536 | /* Dequeues all of the listeners waiting for a resource */ |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 1537 | dequeue_all_listeners(); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1538 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 1539 | if (p && !MT_LIST_ISEMPTY(&p->listener_queue) && |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1540 | (!p->fe_sps_lim || freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0) > 0)) |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 1541 | dequeue_proxy_listeners(p); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1542 | } |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1543 | return; |
| 1544 | |
| 1545 | transient_error: |
| 1546 | /* pause the listener for up to 100 ms */ |
| 1547 | expire = tick_add(now_ms, 100); |
| 1548 | |
Willy Tarreau | 258b351 | 2020-10-13 17:46:05 +0200 | [diff] [blame] | 1549 | /* This may be a shared socket that was paused by another process. |
| 1550 | * Let's put it to pause in this case. |
| 1551 | */ |
| 1552 | if (l->rx.proto && l->rx.proto->rx_listening(&l->rx) == 0) { |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 1553 | suspend_listener(l, 0, 0); |
Willy Tarreau | 258b351 | 2020-10-13 17:46:05 +0200 | [diff] [blame] | 1554 | goto end; |
| 1555 | } |
| 1556 | |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1557 | limit_global: |
| 1558 | /* (re-)queue the listener to the global queue and set it to expire no |
| 1559 | * later than <expire> ahead. The listener turns to LI_LIMITED. |
| 1560 | */ |
| 1561 | limit_listener(l, &global_listener_queue); |
Christopher Faulet | 13e86d9 | 2022-11-17 14:40:20 +0100 | [diff] [blame] | 1562 | HA_RWLOCK_RDLOCK(LISTENER_LOCK, &global_listener_rwlock); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1563 | task_schedule(global_listener_queue_task, expire); |
Christopher Faulet | 13e86d9 | 2022-11-17 14:40:20 +0100 | [diff] [blame] | 1564 | HA_RWLOCK_RDUNLOCK(LISTENER_LOCK, &global_listener_rwlock); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1565 | goto end; |
| 1566 | |
| 1567 | limit_proxy: |
| 1568 | /* (re-)queue the listener to the proxy's queue and set it to expire no |
| 1569 | * later than <expire> ahead. The listener turns to LI_LIMITED. |
| 1570 | */ |
| 1571 | limit_listener(l, &p->listener_queue); |
Willy Tarreau | eeea808 | 2020-01-08 19:15:07 +0100 | [diff] [blame] | 1572 | if (p->task && tick_isset(expire)) |
| 1573 | task_schedule(p->task, expire); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1574 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1577 | /* Notify the listener that a connection initiated from it was released. This |
| 1578 | * is used to keep the connection count consistent and to possibly re-open |
| 1579 | * listening when it was limited. |
| 1580 | */ |
| 1581 | void listener_release(struct listener *l) |
| 1582 | { |
| 1583 | struct proxy *fe = l->bind_conf->frontend; |
| 1584 | |
Amaury Denoyelle | 331b8b1 | 2023-10-25 10:52:23 +0200 | [diff] [blame^] | 1585 | if (listener_uses_maxconn(l)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1586 | _HA_ATOMIC_DEC(&actconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1587 | if (fe) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1588 | _HA_ATOMIC_DEC(&fe->feconn); |
| 1589 | _HA_ATOMIC_DEC(&l->nbconn); |
Willy Tarreau | fea8c19 | 2023-02-28 10:25:57 +0100 | [diff] [blame] | 1590 | _HA_ATOMIC_DEC(&l->thr_conn[ti->ltid]); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1591 | |
| 1592 | if (l->state == LI_FULL || l->state == LI_LIMITED) |
Aurelien DARRAGON | f5d9893 | 2023-02-06 17:19:58 +0100 | [diff] [blame] | 1593 | relax_listener(l, 0, 0); |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1594 | |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 1595 | /* Dequeues all of the listeners waiting for a resource */ |
| 1596 | dequeue_all_listeners(); |
| 1597 | |
Aurelien DARRAGON | a57786e | 2022-09-12 09:26:21 +0200 | [diff] [blame] | 1598 | if (fe && !MT_LIST_ISEMPTY(&fe->listener_queue) && |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1599 | (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0)) |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 1600 | dequeue_proxy_listeners(fe); |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1601 | } |
| 1602 | |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 1603 | /* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */ |
| 1604 | static int listener_queue_init() |
| 1605 | { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 1606 | global_listener_queue_task = task_new_anywhere(); |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1607 | if (!global_listener_queue_task) { |
| 1608 | ha_alert("Out of memory when initializing global listener queue\n"); |
| 1609 | return ERR_FATAL|ERR_ABORT; |
| 1610 | } |
| 1611 | /* very simple initialization, users will queue the task if needed */ |
| 1612 | global_listener_queue_task->context = NULL; /* not even a context! */ |
| 1613 | global_listener_queue_task->process = manage_global_listener_queue; |
Christopher Faulet | 13e86d9 | 2022-11-17 14:40:20 +0100 | [diff] [blame] | 1614 | HA_RWLOCK_INIT(&global_listener_rwlock); |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1615 | |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | static void listener_queue_deinit() |
| 1620 | { |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1621 | task_destroy(global_listener_queue_task); |
| 1622 | global_listener_queue_task = NULL; |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init); |
| 1626 | REGISTER_POST_DEINIT(listener_queue_deinit); |
| 1627 | |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1628 | |
| 1629 | /* This is the global management task for listeners. It enables listeners waiting |
| 1630 | * for global resources when there are enough free resource, or at least once in |
Willy Tarreau | d597ec2 | 2021-01-29 14:29:06 +0100 | [diff] [blame] | 1631 | * a while. It is designed to be called as a task. It's exported so that it's easy |
| 1632 | * to spot in "show tasks" or "show profiling". |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1633 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1634 | struct task *manage_global_listener_queue(struct task *t, void *context, unsigned int state) |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1635 | { |
| 1636 | /* If there are still too many concurrent connections, let's wait for |
| 1637 | * some of them to go away. We don't need to re-arm the timer because |
| 1638 | * each of them will scan the queue anyway. |
| 1639 | */ |
| 1640 | if (unlikely(actconn >= global.maxconn)) |
| 1641 | goto out; |
| 1642 | |
| 1643 | /* We should periodically try to enable listeners waiting for a global |
| 1644 | * resource here, because it is possible, though very unlikely, that |
| 1645 | * they have been blocked by a temporary lack of global resource such |
| 1646 | * as a file descriptor or memory and that the temporary condition has |
| 1647 | * disappeared. |
| 1648 | */ |
| 1649 | dequeue_all_listeners(); |
| 1650 | |
| 1651 | out: |
Christopher Faulet | 13e86d9 | 2022-11-17 14:40:20 +0100 | [diff] [blame] | 1652 | HA_RWLOCK_WRLOCK(LISTENER_LOCK, &global_listener_rwlock); |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1653 | t->expire = TICK_ETERNITY; |
Christopher Faulet | 13e86d9 | 2022-11-17 14:40:20 +0100 | [diff] [blame] | 1654 | HA_RWLOCK_WRUNLOCK(LISTENER_LOCK, &global_listener_rwlock); |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1655 | return t; |
| 1656 | } |
| 1657 | |
Willy Tarreau | f6a8444 | 2023-04-22 23:25:38 +0200 | [diff] [blame] | 1658 | /* Applies the thread mask, shards etc to the bind_conf. It normally returns 0 |
| 1659 | * otherwie the number of errors. Upon error it may set error codes (ERR_*) in |
| 1660 | * err_code. It is supposed to be called only once very late in the boot process |
| 1661 | * after the bind_conf's thread_set is fixed. The function may emit warnings and |
| 1662 | * alerts. Extra listeners may be created on the fly. |
| 1663 | */ |
| 1664 | int bind_complete_thread_setup(struct bind_conf *bind_conf, int *err_code) |
| 1665 | { |
| 1666 | struct proxy *fe = bind_conf->frontend; |
| 1667 | struct listener *li, *new_li, *ref; |
| 1668 | struct thread_set new_ts; |
| 1669 | int shard, shards, todo, done, grp, dups; |
| 1670 | ulong mask, gmask, bit; |
| 1671 | int cfgerr = 0; |
| 1672 | char *err; |
| 1673 | |
| 1674 | err = NULL; |
Willy Tarreau | c38499c | 2023-04-22 22:27:31 +0200 | [diff] [blame] | 1675 | if (thread_resolve_group_mask(&bind_conf->thread_set, 0, &err) < 0) { |
Willy Tarreau | a22db65 | 2023-04-22 23:52:17 +0200 | [diff] [blame] | 1676 | ha_alert("%s '%s': %s in 'bind %s' at [%s:%d].\n", |
| 1677 | proxy_type_str(fe), |
Willy Tarreau | f6a8444 | 2023-04-22 23:25:38 +0200 | [diff] [blame] | 1678 | fe->id, err, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1679 | free(err); |
| 1680 | cfgerr++; |
| 1681 | return cfgerr; |
| 1682 | } |
| 1683 | |
| 1684 | /* apply thread masks and groups to all receivers */ |
| 1685 | list_for_each_entry(li, &bind_conf->listeners, by_bind) { |
| 1686 | shards = bind_conf->settings.shards; |
| 1687 | todo = thread_set_count(&bind_conf->thread_set); |
| 1688 | |
| 1689 | /* special values: -1 = "by-thread", -2 = "by-group" */ |
Willy Tarreau | c1fbdd6 | 2023-04-22 11:38:55 +0200 | [diff] [blame] | 1690 | if (shards == -1) { |
Willy Tarreau | 8a5e6f4 | 2023-04-22 17:39:30 +0200 | [diff] [blame] | 1691 | if (protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED)) |
Willy Tarreau | c1fbdd6 | 2023-04-22 11:38:55 +0200 | [diff] [blame] | 1692 | shards = todo; |
| 1693 | else { |
| 1694 | if (fe != global.cli_fe) |
| 1695 | ha_diag_warning("[%s:%d]: Disabling per-thread sharding for listener in" |
| 1696 | " %s '%s' because SO_REUSEPORT is disabled\n", |
| 1697 | bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id); |
| 1698 | shards = 1; |
| 1699 | } |
| 1700 | } |
Willy Tarreau | f6a8444 | 2023-04-22 23:25:38 +0200 | [diff] [blame] | 1701 | else if (shards == -2) |
Willy Tarreau | 8a5e6f4 | 2023-04-22 17:39:30 +0200 | [diff] [blame] | 1702 | shards = protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED) ? my_popcountl(bind_conf->thread_set.grps) : 1; |
Willy Tarreau | f6a8444 | 2023-04-22 23:25:38 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* no more shards than total threads */ |
| 1705 | if (shards > todo) |
| 1706 | shards = todo; |
| 1707 | |
Willy Tarreau | c1fbdd6 | 2023-04-22 11:38:55 +0200 | [diff] [blame] | 1708 | /* We also need to check if an explicit shards count was set and cannot be honored */ |
Willy Tarreau | 8a5e6f4 | 2023-04-22 17:39:30 +0200 | [diff] [blame] | 1709 | if (shards > 1 && !protocol_supports_flag(li->rx.proto, PROTO_F_REUSEPORT_SUPPORTED)) { |
Willy Tarreau | c1fbdd6 | 2023-04-22 11:38:55 +0200 | [diff] [blame] | 1710 | ha_warning("[%s:%d]: Disabling sharding for listener in %s '%s' because SO_REUSEPORT is disabled\n", |
| 1711 | bind_conf->file, bind_conf->line, proxy_type_str(fe), fe->id); |
| 1712 | shards = 1; |
| 1713 | } |
| 1714 | |
Willy Tarreau | f6a8444 | 2023-04-22 23:25:38 +0200 | [diff] [blame] | 1715 | shard = done = grp = bit = mask = 0; |
| 1716 | new_li = li; |
| 1717 | |
| 1718 | while (shard < shards) { |
| 1719 | memset(&new_ts, 0, sizeof(new_ts)); |
| 1720 | while (grp < global.nbtgroups && done < todo) { |
| 1721 | /* enlarge mask to cover next bit of bind_thread till we |
| 1722 | * have enough bits for one shard. We restart from the |
| 1723 | * current grp+bit. |
| 1724 | */ |
| 1725 | |
| 1726 | /* first let's find the first non-empty group starting at <mask> */ |
| 1727 | if (!(bind_conf->thread_set.rel[grp] & ha_tgroup_info[grp].threads_enabled & ~mask)) { |
| 1728 | grp++; |
| 1729 | mask = 0; |
| 1730 | continue; |
| 1731 | } |
| 1732 | |
| 1733 | /* take next unassigned bit */ |
| 1734 | bit = (bind_conf->thread_set.rel[grp] & ~mask) & -(bind_conf->thread_set.rel[grp] & ~mask); |
| 1735 | new_ts.rel[grp] |= bit; |
| 1736 | mask |= bit; |
| 1737 | new_ts.grps |= 1UL << grp; |
| 1738 | |
| 1739 | done += shards; |
| 1740 | }; |
| 1741 | |
| 1742 | BUG_ON(!new_ts.grps); // no more bits left unassigned |
| 1743 | |
| 1744 | /* Create all required listeners for all bound groups. If more than one group is |
| 1745 | * needed, the first receiver serves as a reference, and subsequent ones point to |
| 1746 | * it. We already have a listener available in new_li() so we only allocate a new |
| 1747 | * one if we're not on the last one. We count the remaining groups by copying their |
| 1748 | * mask into <gmask> and dropping the lowest bit at the end of the loop until there |
| 1749 | * is no more. Ah yes, it's not pretty :-/ |
| 1750 | */ |
| 1751 | ref = new_li; |
| 1752 | gmask = new_ts.grps; |
| 1753 | for (dups = 0; gmask; dups++) { |
| 1754 | /* assign the first (and only) thread and group */ |
| 1755 | new_li->rx.bind_thread = thread_set_nth_tmask(&new_ts, dups); |
| 1756 | new_li->rx.bind_tgroup = thread_set_nth_group(&new_ts, dups); |
| 1757 | |
| 1758 | if (dups) { |
| 1759 | /* it has been allocated already in the previous round */ |
| 1760 | shard_info_attach(&new_li->rx, ref->rx.shard_info); |
| 1761 | new_li->rx.flags |= RX_F_MUST_DUP; |
| 1762 | } |
| 1763 | |
| 1764 | gmask &= gmask - 1; // drop lowest bit |
| 1765 | if (gmask) { |
| 1766 | /* yet another listener expected in this shard, let's |
| 1767 | * chain it. |
| 1768 | */ |
| 1769 | struct listener *tmp_li = clone_listener(new_li); |
| 1770 | |
| 1771 | if (!tmp_li) { |
| 1772 | ha_alert("Out of memory while trying to allocate extra listener for group %u of shard %d in %s %s\n", |
| 1773 | new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id); |
| 1774 | cfgerr++; |
| 1775 | *err_code |= ERR_FATAL | ERR_ALERT; |
| 1776 | return cfgerr; |
| 1777 | } |
| 1778 | |
| 1779 | /* if we're forced to create at least two listeners, we have to |
| 1780 | * allocate a shared shard_info that's linked to from the reference |
| 1781 | * and each other listener, so we'll create it here. |
| 1782 | */ |
| 1783 | if (!shard_info_attach(&ref->rx, NULL)) { |
| 1784 | ha_alert("Out of memory while trying to allocate shard_info for listener for group %u of shard %d in %s %s\n", |
| 1785 | new_li->rx.bind_tgroup, shard, proxy_type_str(fe), fe->id); |
| 1786 | cfgerr++; |
| 1787 | *err_code |= ERR_FATAL | ERR_ALERT; |
| 1788 | return cfgerr; |
| 1789 | } |
| 1790 | new_li = tmp_li; |
| 1791 | } |
| 1792 | } |
| 1793 | done -= todo; |
| 1794 | |
| 1795 | shard++; |
| 1796 | if (shard >= shards) |
| 1797 | break; |
| 1798 | |
| 1799 | /* create another listener for new shards */ |
| 1800 | new_li = clone_listener(li); |
| 1801 | if (!new_li) { |
| 1802 | ha_alert("Out of memory while trying to allocate extra listener for shard %d in %s %s\n", |
| 1803 | shard, proxy_type_str(fe), fe->id); |
| 1804 | cfgerr++; |
| 1805 | *err_code |= ERR_FATAL | ERR_ALERT; |
| 1806 | return cfgerr; |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | /* success */ |
| 1812 | return cfgerr; |
| 1813 | } |
| 1814 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 1815 | /* |
| 1816 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 1817 | * parsing sessions. |
| 1818 | */ |
| 1819 | void bind_register_keywords(struct bind_kw_list *kwl) |
| 1820 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1821 | LIST_APPEND(&bind_keywords.list, &kwl->list); |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. If the |
| 1825 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 1826 | * find one with a valid ->parse() function. This way it is possible to declare |
| 1827 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 1828 | * if some options are met. Note that if the requested keyword contains an |
| 1829 | * opening parenthesis, everything from this point is ignored. |
| 1830 | */ |
| 1831 | struct bind_kw *bind_find_kw(const char *kw) |
| 1832 | { |
| 1833 | int index; |
| 1834 | const char *kwend; |
| 1835 | struct bind_kw_list *kwl; |
| 1836 | struct bind_kw *ret = NULL; |
| 1837 | |
| 1838 | kwend = strchr(kw, '('); |
| 1839 | if (!kwend) |
| 1840 | kwend = kw + strlen(kw); |
| 1841 | |
| 1842 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 1843 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 1844 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 1845 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 1846 | if (kwl->kw[index].parse) |
| 1847 | return &kwl->kw[index]; /* found it !*/ |
| 1848 | else |
| 1849 | ret = &kwl->kw[index]; /* may be OK */ |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | return ret; |
| 1854 | } |
| 1855 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1856 | /* Dumps all registered "bind" keywords to the <out> string pointer. The |
| 1857 | * unsupported keywords are only dumped if their supported form was not |
| 1858 | * found. |
| 1859 | */ |
| 1860 | void bind_dump_kws(char **out) |
| 1861 | { |
| 1862 | struct bind_kw_list *kwl; |
| 1863 | int index; |
| 1864 | |
Christopher Faulet | 784063e | 2020-05-18 12:14:18 +0200 | [diff] [blame] | 1865 | if (!out) |
| 1866 | return; |
| 1867 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1868 | *out = NULL; |
| 1869 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 1870 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 1871 | if (kwl->kw[index].parse || |
| 1872 | bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1873 | memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "", |
| 1874 | kwl->scope, |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1875 | kwl->kw[index].kw, |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1876 | kwl->kw[index].skip ? " <arg>" : "", |
| 1877 | kwl->kw[index].parse ? "" : " (not supported)"); |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | |
Willy Tarreau | 433b05f | 2021-03-12 10:14:07 +0100 | [diff] [blame] | 1883 | /* Try to find in srv_keyword the word that looks closest to <word> by counting |
| 1884 | * transitions between letters, digits and other characters. Will return the |
| 1885 | * best matching word if found, otherwise NULL. |
| 1886 | */ |
| 1887 | const char *bind_find_best_kw(const char *word) |
| 1888 | { |
| 1889 | uint8_t word_sig[1024]; |
| 1890 | uint8_t list_sig[1024]; |
| 1891 | const struct bind_kw_list *kwl; |
| 1892 | const char *best_ptr = NULL; |
| 1893 | int dist, best_dist = INT_MAX; |
| 1894 | int index; |
| 1895 | |
| 1896 | make_word_fingerprint(word_sig, word); |
| 1897 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 1898 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 1899 | make_word_fingerprint(list_sig, kwl->kw[index].kw); |
| 1900 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 1901 | if (dist < best_dist) { |
| 1902 | best_dist = dist; |
| 1903 | best_ptr = kwl->kw[index].kw; |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr))) |
| 1909 | best_ptr = NULL; |
| 1910 | |
| 1911 | return best_ptr; |
| 1912 | } |
| 1913 | |
Willy Tarreau | dbf7802 | 2021-10-06 09:05:08 +0200 | [diff] [blame] | 1914 | /* allocate an bind_conf struct for a bind line, and chain it to the frontend <fe>. |
| 1915 | * If <arg> is not NULL, it is duplicated into ->arg to store useful config |
| 1916 | * information for error reporting. NULL is returned on error. |
| 1917 | */ |
| 1918 | struct bind_conf *bind_conf_alloc(struct proxy *fe, const char *file, |
| 1919 | int line, const char *arg, struct xprt_ops *xprt) |
| 1920 | { |
| 1921 | struct bind_conf *bind_conf = calloc(1, sizeof(*bind_conf)); |
| 1922 | |
| 1923 | if (!bind_conf) |
| 1924 | goto err; |
| 1925 | |
| 1926 | bind_conf->file = strdup(file); |
| 1927 | if (!bind_conf->file) |
| 1928 | goto err; |
| 1929 | bind_conf->line = line; |
| 1930 | if (arg) { |
| 1931 | bind_conf->arg = strdup(arg); |
| 1932 | if (!bind_conf->arg) |
| 1933 | goto err; |
| 1934 | } |
| 1935 | |
| 1936 | LIST_APPEND(&fe->conf.bind, &bind_conf->by_fe); |
| 1937 | bind_conf->settings.ux.uid = -1; |
| 1938 | bind_conf->settings.ux.gid = -1; |
| 1939 | bind_conf->settings.ux.mode = 0; |
Willy Tarreau | 7310164 | 2023-04-22 22:06:23 +0200 | [diff] [blame] | 1940 | bind_conf->settings.shards = global.tune.default_shards; |
Willy Tarreau | dbf7802 | 2021-10-06 09:05:08 +0200 | [diff] [blame] | 1941 | bind_conf->xprt = xprt; |
| 1942 | bind_conf->frontend = fe; |
Willy Tarreau | 7866e8e | 2023-01-12 18:39:42 +0100 | [diff] [blame] | 1943 | bind_conf->analysers = fe->fe_req_ana; |
Willy Tarreau | dbf7802 | 2021-10-06 09:05:08 +0200 | [diff] [blame] | 1944 | bind_conf->severity_output = CLI_SEVERITY_NONE; |
| 1945 | #ifdef USE_OPENSSL |
| 1946 | HA_RWLOCK_INIT(&bind_conf->sni_lock); |
| 1947 | bind_conf->sni_ctx = EB_ROOT; |
| 1948 | bind_conf->sni_w_ctx = EB_ROOT; |
| 1949 | #endif |
| 1950 | LIST_INIT(&bind_conf->listeners); |
| 1951 | return bind_conf; |
| 1952 | |
| 1953 | err: |
| 1954 | if (bind_conf) { |
| 1955 | ha_free(&bind_conf->file); |
| 1956 | ha_free(&bind_conf->arg); |
| 1957 | } |
| 1958 | ha_free(&bind_conf); |
| 1959 | return NULL; |
| 1960 | } |
| 1961 | |
| 1962 | const char *listener_state_str(const struct listener *l) |
| 1963 | { |
| 1964 | static const char *states[8] = { |
| 1965 | "NEW", "INI", "ASS", "PAU", "LIS", "RDY", "FUL", "LIM", |
| 1966 | }; |
| 1967 | unsigned int st = l->state; |
| 1968 | |
| 1969 | if (st >= sizeof(states) / sizeof(*states)) |
| 1970 | return "INVALID"; |
| 1971 | return states[st]; |
| 1972 | } |
| 1973 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1974 | /************************************************************************/ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 1975 | /* All supported sample and ACL keywords must be declared here. */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1976 | /************************************************************************/ |
| 1977 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1978 | /* set temp integer to the number of connexions to the same listening socket */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1979 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1980 | smp_fetch_dconn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1981 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1982 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1983 | smp->data.u.sint = smp->sess->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1984 | return 1; |
| 1985 | } |
| 1986 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1987 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1988 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1989 | smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1990 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1991 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1992 | smp->data.u.sint = smp->sess->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1993 | return 1; |
| 1994 | } |
Jerome Magnin | eb421b2 | 2020-03-27 22:08:40 +0100 | [diff] [blame] | 1995 | static int |
| 1996 | smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1997 | { |
| 1998 | smp->data.u.str.area = smp->sess->listener->name; |
| 1999 | if (!smp->data.u.str.area) |
| 2000 | return 0; |
| 2001 | |
| 2002 | smp->data.type = SMP_T_STR; |
| 2003 | smp->flags = SMP_F_CONST; |
| 2004 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
| 2005 | return 1; |
| 2006 | } |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 2007 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2008 | /* parse the "accept-proxy" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2009 | static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2010 | { |
Willy Tarreau | f1b4730 | 2023-01-12 19:48:50 +0100 | [diff] [blame] | 2011 | conf->options |= BC_O_ACC_PROXY; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2012 | return 0; |
| 2013 | } |
| 2014 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 2015 | /* parse the "accept-netscaler-cip" bind keyword */ |
| 2016 | static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2017 | { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 2018 | uint32_t val; |
| 2019 | |
| 2020 | if (!*args[cur_arg + 1]) { |
| 2021 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 2022 | return ERR_ALERT | ERR_FATAL; |
| 2023 | } |
| 2024 | |
| 2025 | val = atol(args[cur_arg + 1]); |
| 2026 | if (val <= 0) { |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 2027 | memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 2028 | return ERR_ALERT | ERR_FATAL; |
| 2029 | } |
| 2030 | |
Willy Tarreau | f1b4730 | 2023-01-12 19:48:50 +0100 | [diff] [blame] | 2031 | conf->options |= BC_O_ACC_CIP; |
| 2032 | conf->ns_cip_magic = val; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 2033 | return 0; |
| 2034 | } |
| 2035 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2036 | /* parse the "backlog" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2037 | static int bind_parse_backlog(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2038 | { |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2039 | int val; |
| 2040 | |
| 2041 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2042 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2043 | return ERR_ALERT | ERR_FATAL; |
| 2044 | } |
| 2045 | |
| 2046 | val = atol(args[cur_arg + 1]); |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 2047 | if (val < 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2048 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2049 | return ERR_ALERT | ERR_FATAL; |
| 2050 | } |
| 2051 | |
Willy Tarreau | 1920f89 | 2023-01-12 18:55:13 +0100 | [diff] [blame] | 2052 | conf->backlog = val; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | /* parse the "id" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2057 | static int bind_parse_id(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2058 | { |
| 2059 | struct eb32_node *node; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2060 | struct listener *l, *new; |
Thierry Fournier | e7fe8eb | 2016-02-26 08:45:58 +0100 | [diff] [blame] | 2061 | char *error; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2062 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2063 | if (conf->listeners.n != conf->listeners.p) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2064 | memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2065 | return ERR_ALERT | ERR_FATAL; |
| 2066 | } |
| 2067 | |
| 2068 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2069 | memprintf(err, "'%s' : expects an integer argument", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2070 | return ERR_ALERT | ERR_FATAL; |
| 2071 | } |
| 2072 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2073 | new = LIST_NEXT(&conf->listeners, struct listener *, by_bind); |
Thierry Fournier | e7fe8eb | 2016-02-26 08:45:58 +0100 | [diff] [blame] | 2074 | new->luid = strtol(args[cur_arg + 1], &error, 10); |
| 2075 | if (*error != '\0') { |
| 2076 | memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]); |
| 2077 | return ERR_ALERT | ERR_FATAL; |
| 2078 | } |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2079 | new->conf.id.key = new->luid; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2080 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2081 | if (new->luid <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2082 | memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2083 | return ERR_ALERT | ERR_FATAL; |
| 2084 | } |
| 2085 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2086 | node = eb32_lookup(&px->conf.used_listener_id, new->luid); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2087 | if (node) { |
| 2088 | l = container_of(node, struct listener, conf.id); |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2089 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')", |
| 2090 | args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line, |
| 2091 | l->bind_conf->arg); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2092 | return ERR_ALERT | ERR_FATAL; |
| 2093 | } |
| 2094 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2095 | eb32_insert(&px->conf.used_listener_id, &new->conf.id); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2096 | return 0; |
| 2097 | } |
| 2098 | |
Willy Tarreau | 3882d2a | 2022-05-20 15:41:45 +0200 | [diff] [blame] | 2099 | /* Complete a bind_conf by parsing the args after the address. <args> is the |
| 2100 | * arguments array, <cur_arg> is the first one to be considered. <section> is |
| 2101 | * the section name to report in error messages, and <file> and <linenum> are |
| 2102 | * the file name and line number respectively. Note that args[0..1] are used |
| 2103 | * in error messages to provide some context. The return value is an error |
| 2104 | * code, zero on success or an OR of ERR_{FATAL,ABORT,ALERT,WARN}. |
| 2105 | */ |
| 2106 | int bind_parse_args_list(struct bind_conf *bind_conf, char **args, int cur_arg, const char *section, const char *file, int linenum) |
| 2107 | { |
| 2108 | int err_code = 0; |
| 2109 | |
| 2110 | while (*(args[cur_arg])) { |
| 2111 | struct bind_kw *kw; |
| 2112 | const char *best; |
| 2113 | |
| 2114 | kw = bind_find_kw(args[cur_arg]); |
| 2115 | if (kw) { |
| 2116 | char *err = NULL; |
| 2117 | int code; |
| 2118 | |
| 2119 | if (!kw->parse) { |
| 2120 | ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : '%s' option is not implemented in this version (check build options).\n", |
| 2121 | file, linenum, args[0], args[1], section, args[cur_arg]); |
| 2122 | cur_arg += 1 + kw->skip ; |
| 2123 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2124 | goto out; |
| 2125 | } |
| 2126 | |
| 2127 | code = kw->parse(args, cur_arg, bind_conf->frontend, bind_conf, &err); |
| 2128 | err_code |= code; |
| 2129 | |
| 2130 | if (code) { |
| 2131 | if (err && *err) { |
| 2132 | indent_msg(&err, 2); |
| 2133 | if (((code & (ERR_WARN|ERR_ALERT)) == ERR_WARN)) |
| 2134 | ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err); |
| 2135 | else |
| 2136 | ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : %s\n", file, linenum, args[0], args[1], section, err); |
| 2137 | } |
| 2138 | else |
| 2139 | ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : error encountered while processing '%s'.\n", |
| 2140 | file, linenum, args[0], args[1], section, args[cur_arg]); |
| 2141 | if (code & ERR_FATAL) { |
| 2142 | free(err); |
| 2143 | cur_arg += 1 + kw->skip; |
| 2144 | goto out; |
| 2145 | } |
| 2146 | } |
| 2147 | free(err); |
| 2148 | cur_arg += 1 + kw->skip; |
| 2149 | continue; |
| 2150 | } |
| 2151 | |
| 2152 | best = bind_find_best_kw(args[cur_arg]); |
| 2153 | if (best) |
| 2154 | ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'; did you mean '%s' maybe ?\n", |
| 2155 | file, linenum, args[0], args[1], section, args[cur_arg], best); |
| 2156 | else |
| 2157 | ha_alert("parsing [%s:%d] : '%s %s' in section '%s': unknown keyword '%s'.\n", |
| 2158 | file, linenum, args[0], args[1], section, args[cur_arg]); |
| 2159 | |
| 2160 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2161 | goto out; |
| 2162 | } |
Willy Tarreau | 64306cc | 2022-05-20 16:20:52 +0200 | [diff] [blame] | 2163 | |
| 2164 | if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_SOCK_STREAM) || |
| 2165 | (bind_conf->options & (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_XPRT_DGRAM|BC_O_USE_XPRT_STREAM)) { |
| 2166 | ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : cannot mix datagram and stream protocols.\n", |
| 2167 | file, linenum, args[0], args[1], section); |
| 2168 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2169 | goto out; |
| 2170 | } |
| 2171 | |
Willy Tarreau | 78d0dcd | 2022-05-20 17:10:00 +0200 | [diff] [blame] | 2172 | /* The transport layer automatically switches to QUIC when QUIC is |
| 2173 | * selected, regardless of bind_conf settings. We then need to |
| 2174 | * initialize QUIC params. |
| 2175 | */ |
| 2176 | if ((bind_conf->options & (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) == (BC_O_USE_SOCK_DGRAM|BC_O_USE_XPRT_STREAM)) { |
| 2177 | #ifdef USE_QUIC |
| 2178 | bind_conf->xprt = xprt_get(XPRT_QUIC); |
Willy Tarreau | 287f32f | 2022-05-20 18:16:52 +0200 | [diff] [blame] | 2179 | if (!(bind_conf->options & BC_O_USE_SSL)) { |
| 2180 | bind_conf->options |= BC_O_USE_SSL; |
| 2181 | ha_warning("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol detected, enabling ssl. Use 'ssl' to shut this warning.\n", |
| 2182 | file, linenum, args[0], args[1], section); |
| 2183 | } |
Willy Tarreau | 78d0dcd | 2022-05-20 17:10:00 +0200 | [diff] [blame] | 2184 | quic_transport_params_init(&bind_conf->quic_params, 1); |
| 2185 | #else |
| 2186 | ha_alert("parsing [%s:%d] : '%s %s' in section '%s' : QUIC protocol selected but support not compiled in (check build options).\n", |
| 2187 | file, linenum, args[0], args[1], section); |
| 2188 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2189 | goto out; |
| 2190 | #endif |
| 2191 | } |
Willy Tarreau | 2071a99 | 2022-05-20 17:14:31 +0200 | [diff] [blame] | 2192 | else if (bind_conf->options & BC_O_USE_SSL) { |
| 2193 | bind_conf->xprt = xprt_get(XPRT_SSL); |
| 2194 | } |
Willy Tarreau | 78d0dcd | 2022-05-20 17:10:00 +0200 | [diff] [blame] | 2195 | |
Willy Tarreau | 3882d2a | 2022-05-20 15:41:45 +0200 | [diff] [blame] | 2196 | out: |
| 2197 | return err_code; |
| 2198 | } |
| 2199 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2200 | /* parse the "maxconn" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2201 | static int bind_parse_maxconn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2202 | { |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2203 | int val; |
| 2204 | |
| 2205 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2206 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2207 | return ERR_ALERT | ERR_FATAL; |
| 2208 | } |
| 2209 | |
| 2210 | val = atol(args[cur_arg + 1]); |
Willy Tarreau | a8cf66b | 2019-02-27 16:49:00 +0100 | [diff] [blame] | 2211 | if (val < 0) { |
| 2212 | memprintf(err, "'%s' : invalid value %d, must be >= 0", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2213 | return ERR_ALERT | ERR_FATAL; |
| 2214 | } |
| 2215 | |
Willy Tarreau | 758c69d | 2023-01-12 18:59:37 +0100 | [diff] [blame] | 2216 | conf->maxconn = val; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | /* parse the "name" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2221 | static int bind_parse_name(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2222 | { |
| 2223 | struct listener *l; |
| 2224 | |
| 2225 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2226 | memprintf(err, "'%s' : missing name", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2227 | return ERR_ALERT | ERR_FATAL; |
| 2228 | } |
| 2229 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2230 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2231 | l->name = strdup(args[cur_arg + 1]); |
| 2232 | |
| 2233 | return 0; |
| 2234 | } |
| 2235 | |
| 2236 | /* parse the "nice" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2237 | static int bind_parse_nice(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2238 | { |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2239 | int val; |
| 2240 | |
| 2241 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2242 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2243 | return ERR_ALERT | ERR_FATAL; |
| 2244 | } |
| 2245 | |
| 2246 | val = atol(args[cur_arg + 1]); |
| 2247 | if (val < -1024 || val > 1024) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2248 | memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2249 | return ERR_ALERT | ERR_FATAL; |
| 2250 | } |
| 2251 | |
Willy Tarreau | 7dbd418 | 2023-01-12 19:32:45 +0100 | [diff] [blame] | 2252 | conf->nice = val; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2253 | return 0; |
| 2254 | } |
| 2255 | |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 2256 | /* parse the "process" bind keyword */ |
| 2257 | static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2258 | { |
Willy Tarreau | acd6441 | 2022-07-15 17:16:01 +0200 | [diff] [blame] | 2259 | memprintf(err, "'process %s' on 'bind' lines is not supported anymore, please use 'thread' instead.", args[cur_arg+1]); |
| 2260 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 2261 | } |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2262 | |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 2263 | /* parse the "proto" bind keyword */ |
| 2264 | static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2265 | { |
| 2266 | struct ist proto; |
| 2267 | |
| 2268 | if (!*args[cur_arg + 1]) { |
| 2269 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 2270 | return ERR_ALERT | ERR_FATAL; |
| 2271 | } |
| 2272 | |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2273 | proto = ist(args[cur_arg + 1]); |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 2274 | conf->mux_proto = get_mux_proto(proto); |
| 2275 | if (!conf->mux_proto) { |
| 2276 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]); |
| 2277 | return ERR_ALERT | ERR_FATAL; |
| 2278 | } |
Willy Tarreau | c8cac04 | 2021-09-21 14:31:29 +0200 | [diff] [blame] | 2279 | return 0; |
| 2280 | } |
| 2281 | |
Willy Tarreau | a07635e | 2023-04-13 17:25:43 +0200 | [diff] [blame] | 2282 | /* parse the "shards" bind keyword. Takes an integer, "by-thread", or "by-group" */ |
Willy Tarreau | 6dfbef4 | 2021-10-12 15:23:03 +0200 | [diff] [blame] | 2283 | static int bind_parse_shards(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2284 | { |
| 2285 | int val; |
| 2286 | |
| 2287 | if (!*args[cur_arg + 1]) { |
| 2288 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 2289 | return ERR_ALERT | ERR_FATAL; |
| 2290 | } |
| 2291 | |
| 2292 | if (strcmp(args[cur_arg + 1], "by-thread") == 0) { |
Willy Tarreau | d30e82b | 2023-04-13 17:11:23 +0200 | [diff] [blame] | 2293 | val = -1; /* -1 = "by-thread", will be fixed in check_config_validity() */ |
Willy Tarreau | a07635e | 2023-04-13 17:25:43 +0200 | [diff] [blame] | 2294 | } else if (strcmp(args[cur_arg + 1], "by-group") == 0) { |
| 2295 | val = -2; /* -2 = "by-group", will be fixed in check_config_validity() */ |
Willy Tarreau | 6dfbef4 | 2021-10-12 15:23:03 +0200 | [diff] [blame] | 2296 | } else { |
| 2297 | val = atol(args[cur_arg + 1]); |
| 2298 | if (val < 1 || val > MAX_THREADS) { |
| 2299 | memprintf(err, "'%s' : invalid value %d, allowed range is %d..%d or 'by-thread'", args[cur_arg], val, 1, MAX_THREADS); |
| 2300 | return ERR_ALERT | ERR_FATAL; |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | conf->settings.shards = val; |
| 2305 | return 0; |
| 2306 | } |
| 2307 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 2308 | /* parse the "thread" bind keyword. This will replace any preset thread_set */ |
Willy Tarreau | c8cac04 | 2021-09-21 14:31:29 +0200 | [diff] [blame] | 2309 | static int bind_parse_thread(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2310 | { |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 2311 | /* note that the thread set is zeroed before first call, and we don't |
| 2312 | * want to reset it so that it remains possible to chain multiple |
| 2313 | * "thread" directives. |
| 2314 | */ |
| 2315 | if (parse_thread_set(args[cur_arg+1], &conf->thread_set, err) < 0) |
Willy Tarreau | c8cac04 | 2021-09-21 14:31:29 +0200 | [diff] [blame] | 2316 | return ERR_ALERT | ERR_FATAL; |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 2317 | return 0; |
| 2318 | } |
| 2319 | |
Willy Tarreau | 7310164 | 2023-04-22 22:06:23 +0200 | [diff] [blame] | 2320 | /* config parser for global "tune.listener.default-shards" */ |
| 2321 | static int cfg_parse_tune_listener_shards(char **args, int section_type, struct proxy *curpx, |
| 2322 | const struct proxy *defpx, const char *file, int line, |
| 2323 | char **err) |
| 2324 | { |
| 2325 | if (too_many_args(1, args, err, NULL)) |
| 2326 | return -1; |
| 2327 | |
| 2328 | if (strcmp(args[1], "by-thread") == 0) |
| 2329 | global.tune.default_shards = -1; |
| 2330 | else if (strcmp(args[1], "by-group") == 0) |
| 2331 | global.tune.default_shards = -2; |
| 2332 | else if (strcmp(args[1], "by-process") == 0) |
| 2333 | global.tune.default_shards = 1; |
| 2334 | else { |
| 2335 | memprintf(err, "'%s' expects either 'by-process', 'by-group', or 'by-thread' but got '%s'.", args[0], args[1]); |
| 2336 | return -1; |
| 2337 | } |
| 2338 | return 0; |
| 2339 | } |
| 2340 | |
Willy Tarreau | 84fe1f4 | 2023-04-20 15:40:38 +0200 | [diff] [blame] | 2341 | /* config parser for global "tune.listener.multi-queue", accepts "on", "fair" or "off" */ |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 2342 | static int cfg_parse_tune_listener_mq(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 2343 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 2344 | char **err) |
| 2345 | { |
| 2346 | if (too_many_args(1, args, err, NULL)) |
| 2347 | return -1; |
| 2348 | |
| 2349 | if (strcmp(args[1], "on") == 0) |
Willy Tarreau | 84fe1f4 | 2023-04-20 15:40:38 +0200 | [diff] [blame] | 2350 | global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_OPT; |
| 2351 | else if (strcmp(args[1], "fair") == 0) |
| 2352 | global.tune.options = (global.tune.options & ~GTUNE_LISTENER_MQ_ANY) | GTUNE_LISTENER_MQ_FAIR; |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 2353 | else if (strcmp(args[1], "off") == 0) |
Willy Tarreau | 84fe1f4 | 2023-04-20 15:40:38 +0200 | [diff] [blame] | 2354 | global.tune.options &= ~GTUNE_LISTENER_MQ_ANY; |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 2355 | else { |
Willy Tarreau | 84fe1f4 | 2023-04-20 15:40:38 +0200 | [diff] [blame] | 2356 | memprintf(err, "'%s' expects either 'on', 'fair', or 'off' but got '%s'.", args[0], args[1]); |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 2357 | return -1; |
| 2358 | } |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 2362 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2363 | * Please take care of keeping this list alphabetically sorted. |
| 2364 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2365 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2366 | { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, }, |
| 2367 | { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, }, |
Jerome Magnin | eb421b2 | 2020-03-27 22:08:40 +0100 | [diff] [blame] | 2368 | { "so_name", smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, }, |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 2369 | { /* END */ }, |
| 2370 | }}; |
| 2371 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2372 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 2373 | |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 2374 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2375 | * Please take care of keeping this list alphabetically sorted. |
| 2376 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2377 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 2378 | { /* END */ }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 2379 | }}; |
| 2380 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2381 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 2382 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2383 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2384 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 2385 | * all code contributors. |
| 2386 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 2387 | * the config parser can report an appropriate error when a known keyword was |
| 2388 | * not enabled. |
| 2389 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 2390 | static struct bind_kw_list bind_kws = { "ALL", { }, { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 2391 | { "accept-netscaler-cip", bind_parse_accept_netscaler_cip, 1 }, /* enable NetScaler Client IP insertion protocol */ |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2392 | { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */ |
| 2393 | { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */ |
| 2394 | { "id", bind_parse_id, 1 }, /* set id of listening socket */ |
| 2395 | { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */ |
| 2396 | { "name", bind_parse_name, 1 }, /* set name of listening socket */ |
| 2397 | { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */ |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 2398 | { "process", bind_parse_process, 1 }, /* set list of allowed process for this socket */ |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 2399 | { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */ |
Willy Tarreau | 6dfbef4 | 2021-10-12 15:23:03 +0200 | [diff] [blame] | 2400 | { "shards", bind_parse_shards, 1 }, /* set number of shards */ |
Willy Tarreau | c8cac04 | 2021-09-21 14:31:29 +0200 | [diff] [blame] | 2401 | { "thread", bind_parse_thread, 1 }, /* set list of allowed threads for this socket */ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 2402 | { /* END */ }, |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 2403 | }}; |
| 2404 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2405 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 2406 | |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 2407 | /* config keyword parsers */ |
| 2408 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 7310164 | 2023-04-22 22:06:23 +0200 | [diff] [blame] | 2409 | { CFG_GLOBAL, "tune.listener.default-shards", cfg_parse_tune_listener_shards }, |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 2410 | { CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq }, |
| 2411 | { 0, NULL, NULL } |
| 2412 | }}; |
| 2413 | |
| 2414 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 2415 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 2416 | /* |
| 2417 | * Local variables: |
| 2418 | * c-indent-level: 8 |
| 2419 | * c-basic-offset: 8 |
| 2420 | * End: |
| 2421 | */ |