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> |
| 18 | #include <fcntl.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 20 | #include <haproxy/acl.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 21 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 22 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 23 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 24 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 25 | #include <haproxy/fd.h> |
| 26 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 27 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 28 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 29 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/protocol.h> |
Willy Tarreau | 5958c43 | 2021-05-08 20:30:37 +0200 | [diff] [blame] | 32 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 33 | #include <haproxy/sample.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 34 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 35 | #include <haproxy/task.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 36 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/tools.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 39 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 40 | /* List head of all known bind keywords */ |
| 41 | static struct bind_kw_list bind_keywords = { |
| 42 | .list = LIST_HEAD_INIT(bind_keywords.list) |
| 43 | }; |
| 44 | |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 45 | /* list of the temporarily limited listeners because of lack of resource */ |
| 46 | static struct mt_list global_listener_queue = MT_LIST_HEAD_INIT(global_listener_queue); |
| 47 | static struct task *global_listener_queue_task; |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 48 | |
William Dauchy | 3679d0c | 2021-02-14 23:22:55 +0100 | [diff] [blame] | 49 | /* listener status for stats */ |
| 50 | const char* li_status_st[LI_STATE_COUNT] = { |
| 51 | [LI_STATUS_WAITING] = "WAITING", |
| 52 | [LI_STATUS_OPEN] = "OPEN", |
| 53 | [LI_STATUS_FULL] = "FULL", |
| 54 | }; |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 56 | #if defined(USE_THREAD) |
| 57 | |
| 58 | struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))) = { }; |
| 59 | |
| 60 | /* dequeue and process a pending connection from the local accept queue (single |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 61 | * consumer). Returns the accepted connection or NULL if none was found. |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 62 | */ |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 63 | struct connection *accept_queue_pop_sc(struct accept_queue_ring *ring) |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 64 | { |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 65 | unsigned int pos, next; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 66 | struct connection *ptr; |
| 67 | struct connection **e; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 68 | |
| 69 | pos = ring->head; |
| 70 | |
| 71 | if (pos == ring->tail) |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 72 | return NULL; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 73 | |
| 74 | next = pos + 1; |
| 75 | if (next >= ACCEPT_QUEUE_SIZE) |
| 76 | next = 0; |
| 77 | |
| 78 | e = &ring->entry[pos]; |
| 79 | |
| 80 | /* wait for the producer to update the listener's pointer */ |
| 81 | while (1) { |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 82 | ptr = *e; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 83 | __ha_barrier_load(); |
| 84 | if (ptr) |
| 85 | break; |
| 86 | pl_cpu_relax(); |
| 87 | } |
| 88 | |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 89 | /* release the entry */ |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 90 | *e = NULL; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 91 | |
| 92 | __ha_barrier_store(); |
| 93 | ring->head = next; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 94 | return ptr; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 98 | /* tries to push a new accepted connection <conn> into ring <ring>. Returns |
| 99 | * non-zero if it succeeds, or zero if the ring is full. Supports multiple |
| 100 | * producers. |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 101 | */ |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 102 | 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] | 103 | { |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 104 | unsigned int pos, next; |
| 105 | |
| 106 | pos = ring->tail; |
| 107 | do { |
| 108 | next = pos + 1; |
| 109 | if (next >= ACCEPT_QUEUE_SIZE) |
| 110 | next = 0; |
| 111 | if (next == ring->head) |
| 112 | return 0; // ring full |
Olivier Houchard | 64213e9 | 2019-03-08 18:52:57 +0100 | [diff] [blame] | 113 | } while (unlikely(!_HA_ATOMIC_CAS(&ring->tail, &pos, next))); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 114 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 115 | ring->entry[pos] = conn; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 116 | __ha_barrier_store(); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 117 | return 1; |
| 118 | } |
| 119 | |
Willy Tarreau | fb5401f | 2021-01-29 12:25:23 +0100 | [diff] [blame] | 120 | /* proceed with accepting new connections. Don't mark it static so that it appears |
| 121 | * in task dumps. |
| 122 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 123 | 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] | 124 | { |
| 125 | struct accept_queue_ring *ring = context; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 126 | struct connection *conn; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 127 | struct listener *li; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 128 | unsigned int max_accept; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 129 | int ret; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 130 | |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 131 | /* if global.tune.maxaccept is -1, then max_accept is UINT_MAX. It |
| 132 | * is not really illimited, but it is probably enough. |
| 133 | */ |
Willy Tarreau | 6616132 | 2021-02-19 15:50:27 +0100 | [diff] [blame] | 134 | max_accept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 135 | for (; max_accept; max_accept--) { |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 136 | conn = accept_queue_pop_sc(ring); |
| 137 | if (!conn) |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 138 | break; |
| 139 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 140 | li = __objt_listener(conn->target); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 141 | _HA_ATOMIC_INC(&li->thr_conn[tid]); |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 142 | ret = li->accept(conn); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 143 | if (ret <= 0) { |
| 144 | /* connection was terminated by the application */ |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | /* increase the per-process number of cumulated sessions, this |
| 149 | * may only be done once l->accept() has accepted the connection. |
| 150 | */ |
| 151 | if (!(li->options & LI_O_UNLIMITED)) { |
| 152 | HA_ATOMIC_UPDATE_MAX(&global.sps_max, |
| 153 | update_freq_ctr(&global.sess_per_sec, 1)); |
| 154 | if (li->bind_conf && li->bind_conf->is_ssl) { |
| 155 | HA_ATOMIC_UPDATE_MAX(&global.ssl_max, |
| 156 | update_freq_ctr(&global.ssl_per_sec, 1)); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* ran out of budget ? Let's come here ASAP */ |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 162 | if (!max_accept) |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 163 | tasklet_wakeup(ring->tasklet); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 164 | |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 165 | return NULL; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* Initializes the accept-queues. Returns 0 on success, otherwise ERR_* flags */ |
| 169 | static int accept_queue_init() |
| 170 | { |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 171 | struct tasklet *t; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 172 | int i; |
| 173 | |
| 174 | for (i = 0; i < global.nbthread; i++) { |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 175 | t = tasklet_new(); |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 176 | if (!t) { |
| 177 | ha_alert("Out of memory while initializing accept queue for thread %d\n", i); |
| 178 | return ERR_FATAL|ERR_ABORT; |
| 179 | } |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 180 | t->tid = i; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 181 | t->process = accept_queue_process; |
| 182 | t->context = &accept_queue_rings[i]; |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 183 | accept_queue_rings[i].tasklet = t; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | REGISTER_CONFIG_POSTPARSER("multi-threaded accept queue", accept_queue_init); |
| 189 | |
| 190 | #endif // USE_THREAD |
| 191 | |
William Dauchy | 3679d0c | 2021-02-14 23:22:55 +0100 | [diff] [blame] | 192 | /* helper to get listener status for stats */ |
| 193 | enum li_status get_li_status(struct listener *l) |
| 194 | { |
| 195 | if (!l->maxconn || l->nbconn < l->maxconn) { |
| 196 | if (l->state == LI_LIMITED) |
| 197 | return LI_STATUS_WAITING; |
| 198 | else |
| 199 | return LI_STATUS_OPEN; |
| 200 | } |
| 201 | return LI_STATUS_FULL; |
| 202 | } |
| 203 | |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 204 | /* adjust the listener's state and its proxy's listener counters if needed. |
| 205 | * It must be called under the listener's lock, but uses atomic ops to change |
| 206 | * the proxy's counters so that the proxy lock is not needed. |
| 207 | */ |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 208 | void listener_set_state(struct listener *l, enum li_state st) |
| 209 | { |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 210 | struct proxy *px = l->bind_conf->frontend; |
| 211 | |
| 212 | if (px) { |
| 213 | /* from state */ |
| 214 | switch (l->state) { |
| 215 | case LI_NEW: /* first call */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 216 | _HA_ATOMIC_INC(&px->li_all); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 217 | break; |
| 218 | case LI_INIT: |
| 219 | case LI_ASSIGNED: |
| 220 | break; |
| 221 | case LI_PAUSED: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 222 | _HA_ATOMIC_DEC(&px->li_paused); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 223 | break; |
| 224 | case LI_LISTEN: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 225 | _HA_ATOMIC_DEC(&px->li_bound); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 226 | break; |
| 227 | case LI_READY: |
| 228 | case LI_FULL: |
| 229 | case LI_LIMITED: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 230 | _HA_ATOMIC_DEC(&px->li_ready); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | |
| 234 | /* to state */ |
| 235 | switch (st) { |
| 236 | case LI_NEW: |
| 237 | case LI_INIT: |
| 238 | case LI_ASSIGNED: |
| 239 | break; |
| 240 | case LI_PAUSED: |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 241 | BUG_ON(l->rx.fd == -1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 242 | _HA_ATOMIC_INC(&px->li_paused); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 243 | break; |
| 244 | case LI_LISTEN: |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 245 | BUG_ON(l->rx.fd == -1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 246 | _HA_ATOMIC_INC(&px->li_bound); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 247 | break; |
| 248 | case LI_READY: |
| 249 | case LI_FULL: |
| 250 | case LI_LIMITED: |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 251 | BUG_ON(l->rx.fd == -1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 252 | _HA_ATOMIC_INC(&px->li_ready); |
Willy Tarreau | efc0eec | 2020-09-24 07:27:06 +0200 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | } |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 256 | l->state = st; |
| 257 | } |
| 258 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 259 | /* This function adds the specified listener's file descriptor to the polling |
| 260 | * 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] | 261 | * 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] | 262 | * also support binding only the relevant processes to their respective |
| 263 | * listeners. We don't do that in debug mode however. |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 264 | */ |
Willy Tarreau | 7834a3f | 2020-09-25 16:40:18 +0200 | [diff] [blame] | 265 | void enable_listener(struct listener *listener) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 266 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 267 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | d6afb53 | 2020-10-09 10:35:40 +0200 | [diff] [blame] | 268 | |
| 269 | /* If this listener is supposed to be only in the master, close it in |
| 270 | * the workers. Conversely, if it's supposed to be only in the workers |
| 271 | * close it in the master. |
| 272 | */ |
Willy Tarreau | 18c20d2 | 2020-10-09 16:11:46 +0200 | [diff] [blame] | 273 | if (!!master != !!(listener->rx.flags & RX_F_MWORKER)) |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 274 | do_unbind_listener(listener); |
Willy Tarreau | d6afb53 | 2020-10-09 10:35:40 +0200 | [diff] [blame] | 275 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 276 | if (listener->state == LI_LISTEN) { |
Willy Tarreau | 95a3460 | 2020-10-08 15:32:21 +0200 | [diff] [blame] | 277 | BUG_ON(listener->rx.fd == -1); |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 278 | if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) && |
Willy Tarreau | 72faef3 | 2021-06-15 08:36:30 +0200 | [diff] [blame] | 279 | (!!master != !!(listener->rx.flags & RX_F_MWORKER))) { |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 280 | /* we don't want to enable this listener and don't |
| 281 | * want any fd event to reach it. |
| 282 | */ |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 283 | do_unbind_listener(listener); |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 284 | } |
Willy Tarreau | a8cf66b | 2019-02-27 16:49:00 +0100 | [diff] [blame] | 285 | else if (!listener->maxconn || listener->nbconn < listener->maxconn) { |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 286 | listener->rx.proto->enable(listener); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 287 | listener_set_state(listener, LI_READY); |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 288 | } |
| 289 | else { |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 290 | listener_set_state(listener, LI_FULL); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
Willy Tarreau | d6afb53 | 2020-10-09 10:35:40 +0200 | [diff] [blame] | 293 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 294 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 297 | /* |
| 298 | * This function completely stops a listener. It will need to operate under the |
| 299 | * proxy's lock, the protocol's lock, and the listener's lock. The caller is |
| 300 | * responsible for indicating in lpx, lpr, lli whether the respective locks are |
| 301 | * already held (non-zero) or not (zero) so that the function picks the missing |
| 302 | * ones, in this order. The proxy's listeners count is updated and the proxy is |
| 303 | * disabled and woken up after the last one is gone. |
| 304 | */ |
| 305 | void stop_listener(struct listener *l, int lpx, int lpr, int lli) |
| 306 | { |
| 307 | struct proxy *px = l->bind_conf->frontend; |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 308 | |
| 309 | if (l->options & LI_O_NOSTOP) { |
| 310 | /* master-worker sockpairs are never closed but don't count as a |
| 311 | * job. |
| 312 | */ |
| 313 | return; |
| 314 | } |
| 315 | |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 316 | if (!lpx) |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 317 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 318 | |
| 319 | if (!lpr) |
| 320 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 321 | |
| 322 | if (!lli) |
| 323 | HA_SPIN_LOCK(LISTENER_LOCK, &l->lock); |
| 324 | |
| 325 | if (l->state > LI_INIT) { |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 326 | do_unbind_listener(l); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 327 | |
| 328 | if (l->state >= LI_ASSIGNED) |
| 329 | __delete_listener(l); |
| 330 | |
Willy Tarreau | acde152 | 2020-10-07 16:31:39 +0200 | [diff] [blame] | 331 | proxy_cond_disable(px); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (!lli) |
| 335 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
| 336 | |
| 337 | if (!lpr) |
| 338 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 339 | |
| 340 | if (!lpx) |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 341 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock); |
Willy Tarreau | caa7df1 | 2020-10-07 15:58:50 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 344 | /* This function adds the specified <listener> to the protocol <proto>. It |
| 345 | * does nothing if the protocol was already added. The listener's state is |
| 346 | * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners |
| 347 | * for the protocol is updated. This must be called with the proto lock held. |
| 348 | */ |
| 349 | void default_add_listener(struct protocol *proto, struct listener *listener) |
| 350 | { |
| 351 | if (listener->state != LI_INIT) |
| 352 | return; |
| 353 | listener_set_state(listener, LI_ASSIGNED); |
| 354 | listener->rx.proto = proto; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 355 | LIST_APPEND(&proto->receivers, &listener->rx.proto_list); |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 356 | proto->nb_receivers++; |
| 357 | } |
| 358 | |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 359 | /* default function called to suspend a listener: it simply passes the call to |
| 360 | * the underlying receiver. This is find for most socket-based protocols. This |
| 361 | * must be called under the listener's lock. It will return non-zero on success, |
| 362 | * 0 on failure. If no receiver-level suspend is provided, the operation is |
| 363 | * assumed to succeed. |
| 364 | */ |
| 365 | int default_suspend_listener(struct listener *l) |
| 366 | { |
| 367 | int ret = 1; |
| 368 | |
| 369 | if (!l->rx.proto->rx_suspend) |
| 370 | return 1; |
| 371 | |
| 372 | ret = l->rx.proto->rx_suspend(&l->rx); |
| 373 | return ret > 0 ? ret : 0; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /* Tries to resume a suspended listener, and returns non-zero on success or |
| 378 | * zero on failure. On certain errors, an alert or a warning might be displayed. |
| 379 | * It must be called with the listener's lock held. Depending on the listener's |
| 380 | * state and protocol, a listen() call might be used to resume operations, or a |
| 381 | * call to the receiver's resume() function might be used as well. This is |
| 382 | * suitable as a default function for TCP and UDP. This must be called with the |
| 383 | * listener's lock held. |
| 384 | */ |
| 385 | int default_resume_listener(struct listener *l) |
| 386 | { |
| 387 | int ret = 1; |
| 388 | |
| 389 | if (l->state == LI_ASSIGNED) { |
| 390 | char msg[100]; |
| 391 | int err; |
| 392 | |
| 393 | err = l->rx.proto->listen(l, msg, sizeof(msg)); |
| 394 | if (err & ERR_ALERT) |
| 395 | ha_alert("Resuming listener: %s\n", msg); |
| 396 | else if (err & ERR_WARN) |
| 397 | ha_warning("Resuming listener: %s\n", msg); |
| 398 | |
| 399 | if (err & (ERR_FATAL | ERR_ABORT)) { |
| 400 | ret = 0; |
| 401 | goto end; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (l->state < LI_PAUSED) { |
| 406 | ret = 0; |
| 407 | goto end; |
| 408 | } |
| 409 | |
| 410 | if (l->state == LI_PAUSED && l->rx.proto->rx_resume && |
| 411 | l->rx.proto->rx_resume(&l->rx) <= 0) |
| 412 | ret = 0; |
| 413 | end: |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 418 | /* This function tries to temporarily disable a listener, depending on the OS |
| 419 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 420 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 421 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 422 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 423 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 424 | */ |
| 425 | int pause_listener(struct listener *l) |
| 426 | { |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 427 | struct proxy *px = l->bind_conf->frontend; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 428 | int ret = 1; |
| 429 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 430 | HA_SPIN_LOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 431 | |
Willy Tarreau | 9b3a932 | 2020-09-24 14:46:34 +0200 | [diff] [blame] | 432 | if (l->state <= LI_PAUSED) |
| 433 | goto end; |
| 434 | |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 435 | if (l->rx.proto->suspend) |
| 436 | ret = l->rx.proto->suspend(l); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 437 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 438 | MT_LIST_DELETE(&l->wait_queue); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 439 | |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 440 | listener_set_state(l, LI_PAUSED); |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 441 | |
| 442 | if (px && !px->li_ready) { |
| 443 | ha_warning("Paused %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 444 | send_log(px, LOG_WARNING, "Paused %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 445 | } |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 446 | end: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 447 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 448 | return ret; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 451 | /* This function tries to resume a temporarily disabled listener. Paused, full, |
| 452 | * limited and disabled listeners are handled, which means that this function |
| 453 | * may replace enable_listener(). The resulting state will either be LI_READY |
| 454 | * 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] | 455 | * 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] | 456 | * foreground mode, and are ignored. If the listener was only in the assigned |
| 457 | * state, it's totally rebound. This can happen if a pause() has completely |
| 458 | * stopped it. If the resume fails, 0 is returned and an error might be |
| 459 | * displayed. |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 460 | */ |
Willy Tarreau | 01abd02 | 2019-02-28 10:27:18 +0100 | [diff] [blame] | 461 | int resume_listener(struct listener *l) |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 462 | { |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 463 | struct proxy *px = l->bind_conf->frontend; |
| 464 | int was_paused = px && px->li_paused; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 465 | int ret = 1; |
| 466 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 467 | HA_SPIN_LOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 468 | |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 469 | /* check that another thread didn't to the job in parallel (e.g. at the |
| 470 | * end of listen_accept() while we'd come from dequeue_all_listeners(). |
| 471 | */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 472 | if (MT_LIST_INLIST(&l->wait_queue)) |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 473 | goto end; |
| 474 | |
Willy Tarreau | 5d7f9ce | 2020-09-24 18:54:11 +0200 | [diff] [blame] | 475 | if (l->state == LI_READY) |
| 476 | goto end; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 477 | |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 478 | if (l->rx.proto->resume) |
| 479 | ret = l->rx.proto->resume(l); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 480 | |
Willy Tarreau | a8cf66b | 2019-02-27 16:49:00 +0100 | [diff] [blame] | 481 | if (l->maxconn && l->nbconn >= l->maxconn) { |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 482 | l->rx.proto->disable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 483 | listener_set_state(l, LI_FULL); |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 484 | goto done; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 487 | l->rx.proto->enable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 488 | listener_set_state(l, LI_READY); |
Willy Tarreau | 58651b4 | 2020-09-24 16:03:29 +0200 | [diff] [blame] | 489 | |
| 490 | done: |
| 491 | if (was_paused && !px->li_paused) { |
| 492 | ha_warning("Resumed %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 493 | send_log(px, LOG_WARNING, "Resumed %s %s.\n", proxy_cap_str(px->cap), px->id); |
| 494 | } |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 495 | end: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 496 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 497 | return ret; |
| 498 | } |
| 499 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 500 | /* Marks a ready listener as full so that the stream code tries to re-enable |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 501 | * it upon next close() using resume_listener(). |
| 502 | */ |
Christopher Faulet | 5580ba2 | 2017-08-28 15:29:20 +0200 | [diff] [blame] | 503 | static void listener_full(struct listener *l) |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 504 | { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 505 | HA_SPIN_LOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 506 | if (l->state >= LI_READY) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 507 | MT_LIST_DELETE(&l->wait_queue); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 508 | if (l->state != LI_FULL) { |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 509 | l->rx.proto->disable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 510 | listener_set_state(l, LI_FULL); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 511 | } |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 512 | } |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 513 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 516 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 517 | * resources are free again. It will be queued into the specified queue. |
| 518 | */ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 519 | static void limit_listener(struct listener *l, struct mt_list *list) |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 520 | { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 521 | HA_SPIN_LOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 522 | if (l->state == LI_READY) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 523 | MT_LIST_TRY_APPEND(list, &l->wait_queue); |
Willy Tarreau | 4b51f42 | 2020-09-25 20:32:28 +0200 | [diff] [blame] | 524 | l->rx.proto->disable(l); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 525 | listener_set_state(l, LI_LIMITED); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 526 | } |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 527 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 530 | /* Dequeues all listeners waiting for a resource the global wait queue */ |
| 531 | void dequeue_all_listeners() |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 532 | { |
Willy Tarreau | 01abd02 | 2019-02-28 10:27:18 +0100 | [diff] [blame] | 533 | struct listener *listener; |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 534 | |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 535 | while ((listener = MT_LIST_POP(&global_listener_queue, struct listener *, wait_queue))) { |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 536 | /* This cannot fail because the listeners are by definition in |
Willy Tarreau | 01abd02 | 2019-02-28 10:27:18 +0100 | [diff] [blame] | 537 | * the LI_LIMITED state. |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 538 | */ |
Willy Tarreau | 01abd02 | 2019-02-28 10:27:18 +0100 | [diff] [blame] | 539 | resume_listener(listener); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 543 | /* Dequeues all listeners waiting for a resource in proxy <px>'s queue */ |
| 544 | void dequeue_proxy_listeners(struct proxy *px) |
| 545 | { |
| 546 | struct listener *listener; |
| 547 | |
| 548 | while ((listener = MT_LIST_POP(&px->listener_queue, struct listener *, wait_queue))) { |
| 549 | /* This cannot fail because the listeners are by definition in |
| 550 | * the LI_LIMITED state. |
| 551 | */ |
| 552 | resume_listener(listener); |
| 553 | } |
| 554 | } |
| 555 | |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 556 | |
| 557 | /* default function used to unbind a listener. This is for use by standard |
| 558 | * protocols working on top of accepted sockets. The receiver's rx_unbind() |
| 559 | * will automatically be used after the listener is disabled if the socket is |
| 560 | * still bound. This must be used under the listener's lock. |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 561 | */ |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 562 | void default_unbind_listener(struct listener *listener) |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 563 | { |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 564 | if (listener->state <= LI_ASSIGNED) |
| 565 | goto out_close; |
| 566 | |
| 567 | if (listener->rx.fd == -1) { |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 568 | listener_set_state(listener, LI_ASSIGNED); |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 569 | goto out_close; |
| 570 | } |
| 571 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 572 | if (listener->state >= LI_READY) { |
| 573 | listener->rx.proto->disable(listener); |
| 574 | if (listener->rx.flags & RX_F_BOUND) |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 575 | listener_set_state(listener, LI_LISTEN); |
Willy Tarreau | b6607bf | 2020-09-23 16:24:23 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Willy Tarreau | 87acd4e | 2020-10-08 15:36:46 +0200 | [diff] [blame] | 578 | out_close: |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 579 | if (listener->rx.flags & RX_F_BOUND) |
| 580 | listener->rx.proto->rx_unbind(&listener->rx); |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* This function closes the listening socket for the specified listener, |
| 584 | * provided that it's already in a listening state. The protocol's unbind() |
| 585 | * is called to put the listener into LI_ASSIGNED or LI_LISTEN and handle |
| 586 | * the unbinding tasks. The listener enters then the LI_ASSIGNED state if |
| 587 | * the receiver is unbound. Must be called with the lock held. |
| 588 | */ |
| 589 | void do_unbind_listener(struct listener *listener) |
| 590 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 591 | MT_LIST_DELETE(&listener->wait_queue); |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 592 | |
| 593 | if (listener->rx.proto->unbind) |
| 594 | listener->rx.proto->unbind(listener); |
Willy Tarreau | 374e9af | 2020-10-09 15:47:17 +0200 | [diff] [blame] | 595 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 596 | /* we may have to downgrade the listener if the rx was closed */ |
| 597 | if (!(listener->rx.flags & RX_F_BOUND) && listener->state > LI_ASSIGNED) |
Willy Tarreau | 374e9af | 2020-10-09 15:47:17 +0200 | [diff] [blame] | 598 | listener_set_state(listener, LI_ASSIGNED); |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 599 | } |
| 600 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 601 | /* This function closes the listening socket for the specified listener, |
| 602 | * 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] | 603 | * LI_ASSIGNED state, except if the FD is not closed, in which case it may |
| 604 | * 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] | 605 | * function for standard protocols. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 606 | */ |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 607 | void unbind_listener(struct listener *listener) |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 608 | { |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 609 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 610 | do_unbind_listener(listener); |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 611 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 614 | /* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port |
| 615 | * 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] | 616 | * 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] | 617 | * passed in <proto> must be usable on this family. The protocol's default iocb |
| 618 | * is automatically preset as the receivers' iocb. The number of jobs and |
Willy Tarreau | 9b3178d | 2020-09-16 17:58:55 +0200 | [diff] [blame] | 619 | * listeners is automatically increased by the number of listeners created. It |
| 620 | * 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] | 621 | */ |
| 622 | int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss, |
Willy Tarreau | 9b3178d | 2020-09-16 17:58:55 +0200 | [diff] [blame] | 623 | int portl, int porth, int fd, struct protocol *proto, char **err) |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 624 | { |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 625 | struct listener *l; |
| 626 | int port; |
| 627 | |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 628 | for (port = portl; port <= porth; port++) { |
| 629 | l = calloc(1, sizeof(*l)); |
| 630 | if (!l) { |
| 631 | memprintf(err, "out of memory"); |
| 632 | return 0; |
| 633 | } |
| 634 | l->obj_type = OBJ_TYPE_LISTENER; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 635 | LIST_APPEND(&bc->frontend->conf.listeners, &l->by_fe); |
| 636 | LIST_APPEND(&bc->listeners, &l->by_bind); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 637 | l->bind_conf = bc; |
Willy Tarreau | 0fce6bc | 2020-09-03 07:46:06 +0200 | [diff] [blame] | 638 | l->rx.settings = &bc->settings; |
Willy Tarreau | eef4542 | 2020-09-03 10:05:03 +0200 | [diff] [blame] | 639 | l->rx.owner = l; |
Willy Tarreau | d2fb99f | 2020-10-15 21:22:29 +0200 | [diff] [blame] | 640 | l->rx.iocb = proto->default_iocb; |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame] | 641 | l->rx.fd = fd; |
Willy Tarreau | 07400c5 | 2020-12-04 14:49:11 +0100 | [diff] [blame] | 642 | |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 643 | memcpy(&l->rx.addr, ss, sizeof(*ss)); |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 644 | if (proto->fam->set_port) |
| 645 | proto->fam->set_port(&l->rx.addr, port); |
Willy Tarreau | 07400c5 | 2020-12-04 14:49:11 +0100 | [diff] [blame] | 646 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 647 | MT_LIST_INIT(&l->wait_queue); |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 648 | listener_set_state(l, LI_INIT); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 649 | |
Willy Tarreau | d1f250f | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 650 | proto->add(proto, l); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 651 | |
Willy Tarreau | 909c23b | 2020-09-15 13:50:58 +0200 | [diff] [blame] | 652 | if (fd != -1) |
Willy Tarreau | 43046fa | 2020-09-01 15:41:59 +0200 | [diff] [blame] | 653 | l->rx.flags |= RX_F_INHERITED; |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 654 | |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 655 | l->extra_counters = NULL; |
| 656 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 657 | HA_SPIN_INIT(&l->lock); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 658 | _HA_ATOMIC_INC(&jobs); |
| 659 | _HA_ATOMIC_INC(&listeners); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 660 | } |
| 661 | return 1; |
| 662 | } |
| 663 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 664 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 665 | * 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] | 666 | * number of listeners is updated, as well as the global number of listeners |
| 667 | * and jobs. Note that the listener must have previously been unbound. This |
Willy Tarreau | b4c083f | 2020-10-07 15:36:16 +0200 | [diff] [blame] | 668 | * is a low-level function expected to be called with the proto_lock and the |
| 669 | * listener's lock held. |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 670 | */ |
Willy Tarreau | b4c083f | 2020-10-07 15:36:16 +0200 | [diff] [blame] | 671 | void __delete_listener(struct listener *listener) |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 672 | { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 673 | if (listener->state == LI_ASSIGNED) { |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 674 | listener_set_state(listener, LI_INIT); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 675 | LIST_DELETE(&listener->rx.proto_list); |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 676 | listener->rx.proto->nb_receivers--; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 677 | _HA_ATOMIC_DEC(&jobs); |
| 678 | _HA_ATOMIC_DEC(&listeners); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 679 | } |
Willy Tarreau | b4c083f | 2020-10-07 15:36:16 +0200 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* Delete a listener from its protocol's list of listeners (please check |
| 683 | * __delete_listener() above). The proto_lock and the listener's lock will |
| 684 | * be grabbed in this order. |
| 685 | */ |
| 686 | void delete_listener(struct listener *listener) |
| 687 | { |
| 688 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 689 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
| 690 | __delete_listener(listener); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 691 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | 6ee9f8d | 2019-08-26 10:55:52 +0200 | [diff] [blame] | 692 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 695 | /* Returns a suitable value for a listener's backlog. It uses the listener's, |
| 696 | * otherwise the frontend's backlog, otherwise the listener's maxconn, |
| 697 | * otherwise the frontend's maxconn, otherwise 1024. |
| 698 | */ |
| 699 | int listener_backlog(const struct listener *l) |
| 700 | { |
| 701 | if (l->backlog) |
| 702 | return l->backlog; |
| 703 | |
| 704 | if (l->bind_conf->frontend->backlog) |
| 705 | return l->bind_conf->frontend->backlog; |
| 706 | |
| 707 | if (l->maxconn) |
| 708 | return l->maxconn; |
| 709 | |
| 710 | if (l->bind_conf->frontend->maxconn) |
| 711 | return l->bind_conf->frontend->maxconn; |
| 712 | |
| 713 | return 1024; |
| 714 | } |
| 715 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 716 | /* This function is called on a read event from a listening socket, corresponding |
| 717 | * to an accept. It tries to accept as many connections as possible, and for each |
| 718 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 719 | */ |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 720 | void listener_accept(struct listener *l) |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 721 | { |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 722 | struct connection *cli_conn; |
Olivier Houchard | d16a9df | 2019-02-25 16:18:16 +0100 | [diff] [blame] | 723 | struct proxy *p; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 724 | unsigned int max_accept; |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 725 | int next_conn = 0; |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 726 | int next_feconn = 0; |
| 727 | int next_actconn = 0; |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 728 | int expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 729 | int ret; |
| 730 | |
Olivier Houchard | d16a9df | 2019-02-25 16:18:16 +0100 | [diff] [blame] | 731 | p = l->bind_conf->frontend; |
Christopher Faulet | 102854c | 2019-04-30 12:17:13 +0200 | [diff] [blame] | 732 | |
| 733 | /* if l->maxaccept is -1, then max_accept is UINT_MAX. It is not really |
| 734 | * illimited, but it is probably enough. |
| 735 | */ |
Olivier Houchard | d16a9df | 2019-02-25 16:18:16 +0100 | [diff] [blame] | 736 | max_accept = l->maxaccept ? l->maxaccept : 1; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 737 | |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 738 | if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) { |
| 739 | 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] | 740 | |
| 741 | if (unlikely(!max)) { |
| 742 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 743 | 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] | 744 | goto limit_global; |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | if (max_accept > max) |
| 748 | max_accept = max; |
| 749 | } |
| 750 | |
| 751 | if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) { |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 752 | int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); |
| 753 | |
| 754 | if (unlikely(!max)) { |
| 755 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 756 | 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] | 757 | goto limit_global; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | if (max_accept > max) |
| 761 | max_accept = max; |
| 762 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 763 | #ifdef USE_OPENSSL |
| 764 | if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) { |
| 765 | 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] | 766 | |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 767 | if (unlikely(!max)) { |
| 768 | /* frontend accept rate limit was reached */ |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 769 | 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] | 770 | goto limit_global; |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | if (max_accept > max) |
| 774 | max_accept = max; |
| 775 | } |
| 776 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 777 | if (p && p->fe_sps_lim) { |
| 778 | int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); |
| 779 | |
| 780 | if (unlikely(!max)) { |
| 781 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 782 | expire = tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0)); |
| 783 | goto limit_proxy; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (max_accept > max) |
| 787 | max_accept = max; |
| 788 | } |
| 789 | |
| 790 | /* Note: if we fail to allocate a connection because of configured |
| 791 | * limits, we'll schedule a new attempt worst 1 second later in the |
| 792 | * worst case. If we fail due to system limits or temporary resource |
| 793 | * shortage, we try again 100ms later in the worst case. |
| 794 | */ |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 795 | for (; max_accept; next_conn = next_feconn = next_actconn = 0, max_accept--) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 796 | unsigned int count; |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 797 | int status; |
Willy Tarreau | 0aa5a5b | 2020-10-16 17:43:04 +0200 | [diff] [blame] | 798 | __decl_thread(unsigned long mask); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 799 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 800 | /* pre-increase the number of connections without going too far. |
| 801 | * We process the listener, then the proxy, then the process. |
| 802 | * We know which ones to unroll based on the next_xxx value. |
| 803 | */ |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 804 | do { |
| 805 | count = l->nbconn; |
Willy Tarreau | 93604ed | 2019-11-15 10:20:07 +0100 | [diff] [blame] | 806 | if (unlikely(l->maxconn && count >= l->maxconn)) { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 807 | /* the listener was marked full or another |
| 808 | * thread is going to do it. |
| 809 | */ |
| 810 | next_conn = 0; |
Willy Tarreau | 93604ed | 2019-11-15 10:20:07 +0100 | [diff] [blame] | 811 | listener_full(l); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 812 | goto end; |
| 813 | } |
| 814 | next_conn = count + 1; |
David Carlier | 5671662 | 2019-03-27 16:08:42 +0000 | [diff] [blame] | 815 | } while (!_HA_ATOMIC_CAS(&l->nbconn, (int *)(&count), next_conn)); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 816 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 817 | if (p) { |
| 818 | do { |
| 819 | count = p->feconn; |
Willy Tarreau | 93604ed | 2019-11-15 10:20:07 +0100 | [diff] [blame] | 820 | if (unlikely(count >= p->maxconn)) { |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 821 | /* the frontend was marked full or another |
| 822 | * thread is going to do it. |
| 823 | */ |
| 824 | next_feconn = 0; |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 825 | expire = TICK_ETERNITY; |
| 826 | goto limit_proxy; |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 827 | } |
| 828 | next_feconn = count + 1; |
Olivier Houchard | 64213e9 | 2019-03-08 18:52:57 +0100 | [diff] [blame] | 829 | } while (!_HA_ATOMIC_CAS(&p->feconn, &count, next_feconn)); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 832 | if (!(l->options & LI_O_UNLIMITED)) { |
| 833 | do { |
| 834 | count = actconn; |
Willy Tarreau | 93604ed | 2019-11-15 10:20:07 +0100 | [diff] [blame] | 835 | if (unlikely(count >= global.maxconn)) { |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 836 | /* the process was marked full or another |
| 837 | * thread is going to do it. |
| 838 | */ |
| 839 | next_actconn = 0; |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 840 | expire = tick_add(now_ms, 1000); /* try again in 1 second */ |
| 841 | goto limit_global; |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 842 | } |
| 843 | next_actconn = count + 1; |
David Carlier | 5671662 | 2019-03-27 16:08:42 +0000 | [diff] [blame] | 844 | } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn)); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 847 | cli_conn = l->rx.proto->accept_conn(l, &status); |
| 848 | if (!cli_conn) { |
| 849 | switch (status) { |
| 850 | case CO_AC_DONE: |
| 851 | goto end; |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 852 | |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 853 | case CO_AC_RETRY: /* likely a signal */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 854 | _HA_ATOMIC_DEC(&l->nbconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 855 | if (p) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 856 | _HA_ATOMIC_DEC(&p->feconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 857 | if (!(l->options & LI_O_UNLIMITED)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 858 | _HA_ATOMIC_DEC(&actconn); |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 859 | continue; |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 860 | |
| 861 | case CO_AC_YIELD: |
Willy Tarreau | 9207993 | 2019-12-10 09:30:05 +0100 | [diff] [blame] | 862 | max_accept = 0; |
| 863 | goto end; |
William Lallemand | d913800 | 2018-11-27 12:02:39 +0100 | [diff] [blame] | 864 | |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 865 | default: |
| 866 | goto transient_error; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 870 | /* The connection was accepted, it must be counted as such */ |
| 871 | if (l->counters) |
| 872 | HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, next_conn); |
| 873 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 874 | if (p) |
| 875 | HA_ATOMIC_UPDATE_MAX(&p->fe_counters.conn_max, next_feconn); |
| 876 | |
| 877 | proxy_inc_fe_conn_ctr(l, p); |
| 878 | |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 879 | if (!(l->options & LI_O_UNLIMITED)) { |
| 880 | count = update_freq_ctr(&global.conn_per_sec, 1); |
| 881 | HA_ATOMIC_UPDATE_MAX(&global.cps_max, count); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 884 | _HA_ATOMIC_INC(&activity[tid].accepted); |
Willy Tarreau | 64a9c05 | 2019-04-12 15:27:17 +0200 | [diff] [blame] | 885 | |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 886 | if (unlikely(cli_conn->handle.fd >= global.maxsock)) { |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 887 | send_log(p, LOG_EMERG, |
| 888 | "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", |
| 889 | p->id); |
Willy Tarreau | 9378bbe | 2020-10-15 10:09:31 +0200 | [diff] [blame] | 890 | close(cli_conn->handle.fd); |
William Dauchy | 835712a | 2020-10-18 18:37:43 +0200 | [diff] [blame] | 891 | conn_free(cli_conn); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 892 | expire = tick_add(now_ms, 1000); /* try again in 1 second */ |
| 893 | goto limit_global; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 894 | } |
| 895 | |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 896 | /* past this point, l->accept() will automatically decrement |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 897 | * l->nbconn, feconn and actconn once done. Setting next_*conn=0 |
| 898 | * allows the error path not to rollback on nbconn. It's more |
| 899 | * convenient than duplicating all exit labels. |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 900 | */ |
| 901 | next_conn = 0; |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 902 | next_feconn = 0; |
| 903 | next_actconn = 0; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 904 | |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 905 | |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 906 | #if defined(USE_THREAD) |
Willy Tarreau | 818a92e | 2020-09-03 07:50:19 +0200 | [diff] [blame] | 907 | mask = thread_mask(l->rx.settings->bind_thread) & all_threads_mask; |
Willy Tarreau | a7da5e8 | 2020-03-12 17:33:29 +0100 | [diff] [blame] | 908 | if (atleast2(mask) && (global.tune.options & GTUNE_LISTENER_MQ) && !stopping) { |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 909 | struct accept_queue_ring *ring; |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 910 | unsigned int t, t0, t1, t2; |
Willy Tarreau | fc630bd | 2019-03-04 19:57:34 +0100 | [diff] [blame] | 911 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 912 | /* The principle is that we have two running indexes, |
| 913 | * each visiting in turn all threads bound to this |
| 914 | * listener. The connection will be assigned to the one |
| 915 | * with the least connections, and the other one will |
| 916 | * be updated. This provides a good fairness on short |
Willy Tarreau | fc630bd | 2019-03-04 19:57:34 +0100 | [diff] [blame] | 917 | * connections (round robin) and on long ones (conn |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 918 | * count), without ever missing any idle thread. |
Willy Tarreau | fc630bd | 2019-03-04 19:57:34 +0100 | [diff] [blame] | 919 | */ |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 920 | |
| 921 | /* keep a copy for the final update. thr_idx is composite |
| 922 | * and made of (t2<<16) + t1. |
| 923 | */ |
Willy Tarreau | 0cf3317 | 2019-03-06 15:26:33 +0100 | [diff] [blame] | 924 | t0 = l->thr_idx; |
Willy Tarreau | fc630bd | 2019-03-04 19:57:34 +0100 | [diff] [blame] | 925 | do { |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 926 | unsigned long m1, m2; |
| 927 | int q1, q2; |
| 928 | |
| 929 | t2 = t1 = t0; |
| 930 | t2 >>= 16; |
| 931 | t1 &= 0xFFFF; |
| 932 | |
| 933 | /* t1 walks low to high bits ; |
| 934 | * t2 walks high to low. |
| 935 | */ |
| 936 | m1 = mask >> t1; |
| 937 | m2 = mask & (t2 ? nbits(t2 + 1) : ~0UL); |
| 938 | |
Willy Tarreau | 85d0424 | 2019-04-16 18:09:13 +0200 | [diff] [blame] | 939 | if (unlikely(!(m1 & 1))) { |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 940 | m1 &= ~1UL; |
| 941 | if (!m1) { |
| 942 | m1 = mask; |
| 943 | t1 = 0; |
| 944 | } |
| 945 | t1 += my_ffsl(m1) - 1; |
| 946 | } |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 947 | |
Willy Tarreau | 85d0424 | 2019-04-16 18:09:13 +0200 | [diff] [blame] | 948 | if (unlikely(!(m2 & (1UL << t2)) || t1 == t2)) { |
| 949 | /* highest bit not set */ |
| 950 | if (!m2) |
| 951 | m2 = mask; |
| 952 | |
| 953 | t2 = my_flsl(m2) - 1; |
| 954 | } |
| 955 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 956 | /* now we have two distinct thread IDs belonging to the mask */ |
| 957 | q1 = accept_queue_rings[t1].tail - accept_queue_rings[t1].head + ACCEPT_QUEUE_SIZE; |
| 958 | if (q1 >= ACCEPT_QUEUE_SIZE) |
| 959 | q1 -= ACCEPT_QUEUE_SIZE; |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 960 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 961 | q2 = accept_queue_rings[t2].tail - accept_queue_rings[t2].head + ACCEPT_QUEUE_SIZE; |
| 962 | if (q2 >= ACCEPT_QUEUE_SIZE) |
| 963 | q2 -= ACCEPT_QUEUE_SIZE; |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 964 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 965 | /* we have 3 possibilities now : |
| 966 | * q1 < q2 : t1 is less loaded than t2, so we pick it |
| 967 | * and update t2 (since t1 might still be |
| 968 | * lower than another thread) |
| 969 | * q1 > q2 : t2 is less loaded than t1, so we pick it |
| 970 | * and update t1 (since t2 might still be |
| 971 | * lower than another thread) |
| 972 | * q1 = q2 : both are equally loaded, thus we pick t1 |
| 973 | * and update t1 as it will become more loaded |
| 974 | * than t2. |
| 975 | */ |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 976 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 977 | q1 += l->thr_conn[t1]; |
| 978 | q2 += l->thr_conn[t2]; |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 979 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 980 | if (q1 - q2 < 0) { |
| 981 | t = t1; |
| 982 | t2 = t2 ? t2 - 1 : LONGBITS - 1; |
| 983 | } |
| 984 | else if (q1 - q2 > 0) { |
| 985 | t = t2; |
| 986 | t1++; |
| 987 | if (t1 >= LONGBITS) |
| 988 | t1 = 0; |
| 989 | } |
| 990 | else { |
| 991 | t = t1; |
| 992 | t1++; |
| 993 | if (t1 >= LONGBITS) |
| 994 | t1 = 0; |
| 995 | } |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 996 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 997 | /* new value for thr_idx */ |
| 998 | t1 += (t2 << 16); |
Olivier Houchard | 64213e9 | 2019-03-08 18:52:57 +0100 | [diff] [blame] | 999 | } while (unlikely(!_HA_ATOMIC_CAS(&l->thr_idx, &t0, t1))); |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1000 | |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1001 | /* We successfully selected the best thread "t" for this |
| 1002 | * connection. We use deferred accepts even if it's the |
| 1003 | * local thread because tests show that it's the best |
| 1004 | * performing model, likely due to better cache locality |
| 1005 | * when processing this loop. |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1006 | */ |
Willy Tarreau | 0fe703b | 2019-03-05 08:46:28 +0100 | [diff] [blame] | 1007 | ring = &accept_queue_rings[t]; |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 1008 | if (accept_queue_push_mp(ring, cli_conn)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1009 | _HA_ATOMIC_INC(&activity[t].accq_pushed); |
Willy Tarreau | 2bd65a7 | 2019-09-24 06:55:18 +0200 | [diff] [blame] | 1010 | tasklet_wakeup(ring->tasklet); |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1011 | continue; |
| 1012 | } |
| 1013 | /* If the ring is full we do a synchronous accept on |
| 1014 | * the local thread here. |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1015 | */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1016 | _HA_ATOMIC_INC(&activity[t].accq_full); |
Willy Tarreau | e0e9c48 | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 1017 | } |
| 1018 | #endif // USE_THREAD |
| 1019 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1020 | _HA_ATOMIC_INC(&l->thr_conn[tid]); |
Willy Tarreau | 83efc32 | 2020-10-14 17:37:17 +0200 | [diff] [blame] | 1021 | ret = l->accept(cli_conn); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1022 | if (unlikely(ret <= 0)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1023 | /* The connection was closed by stream_accept(). Either |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1024 | * we just have to ignore it (ret == 0) or it's a critical |
| 1025 | * error due to a resource shortage, and we must stop the |
| 1026 | * listener (ret < 0). |
| 1027 | */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1028 | if (ret == 0) /* successful termination */ |
| 1029 | continue; |
| 1030 | |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 1031 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1034 | /* increase the per-process number of cumulated sessions, this |
| 1035 | * may only be done once l->accept() has accepted the connection. |
| 1036 | */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1037 | if (!(l->options & LI_O_UNLIMITED)) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1038 | count = update_freq_ctr(&global.sess_per_sec, 1); |
| 1039 | HA_ATOMIC_UPDATE_MAX(&global.sps_max, count); |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1040 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1041 | #ifdef USE_OPENSSL |
| 1042 | if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1043 | count = update_freq_ctr(&global.ssl_per_sec, 1); |
| 1044 | HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count); |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 1045 | } |
| 1046 | #endif |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 1047 | |
Willy Tarreau | 8d2c98b | 2020-05-01 09:51:11 +0200 | [diff] [blame] | 1048 | ti->flags &= ~TI_FL_STUCK; // this thread is still running |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1049 | } /* end of for (max_accept--) */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1050 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1051 | end: |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1052 | if (next_conn) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1053 | _HA_ATOMIC_DEC(&l->nbconn); |
Willy Tarreau | 741b4d6 | 2019-02-25 15:02:04 +0100 | [diff] [blame] | 1054 | |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1055 | if (p && next_feconn) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1056 | _HA_ATOMIC_DEC(&p->feconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1057 | |
| 1058 | if (next_actconn) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1059 | _HA_ATOMIC_DEC(&actconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1060 | |
Willy Tarreau | a8cf66b | 2019-02-27 16:49:00 +0100 | [diff] [blame] | 1061 | if ((l->state == LI_FULL && (!l->maxconn || l->nbconn < l->maxconn)) || |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 1062 | (l->state == LI_LIMITED && |
Willy Tarreau | cdcba11 | 2019-12-11 15:06:30 +0100 | [diff] [blame] | 1063 | ((!p || p->feconn < p->maxconn) && (actconn < global.maxconn) && |
| 1064 | (!tick_isset(global_listener_queue_task->expire) || |
| 1065 | tick_is_expired(global_listener_queue_task->expire, now_ms))))) { |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1066 | /* at least one thread has to this when quitting */ |
| 1067 | resume_listener(l); |
| 1068 | |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 1069 | /* Dequeues all of the listeners waiting for a resource */ |
Willy Tarreau | 241797a | 2019-12-10 14:10:52 +0100 | [diff] [blame] | 1070 | dequeue_all_listeners(); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1071 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 1072 | if (p && !MT_LIST_ISEMPTY(&p->listener_queue) && |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1073 | (!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] | 1074 | dequeue_proxy_listeners(p); |
Willy Tarreau | 3f0d02b | 2019-02-25 19:23:37 +0100 | [diff] [blame] | 1075 | } |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1076 | return; |
| 1077 | |
| 1078 | transient_error: |
| 1079 | /* pause the listener for up to 100 ms */ |
| 1080 | expire = tick_add(now_ms, 100); |
| 1081 | |
Willy Tarreau | 258b351 | 2020-10-13 17:46:05 +0200 | [diff] [blame] | 1082 | /* This may be a shared socket that was paused by another process. |
| 1083 | * Let's put it to pause in this case. |
| 1084 | */ |
| 1085 | if (l->rx.proto && l->rx.proto->rx_listening(&l->rx) == 0) { |
| 1086 | pause_listener(l); |
| 1087 | goto end; |
| 1088 | } |
| 1089 | |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1090 | limit_global: |
| 1091 | /* (re-)queue the listener to the global queue and set it to expire no |
| 1092 | * later than <expire> ahead. The listener turns to LI_LIMITED. |
| 1093 | */ |
| 1094 | limit_listener(l, &global_listener_queue); |
| 1095 | task_schedule(global_listener_queue_task, expire); |
| 1096 | goto end; |
| 1097 | |
| 1098 | limit_proxy: |
| 1099 | /* (re-)queue the listener to the proxy's queue and set it to expire no |
| 1100 | * later than <expire> ahead. The listener turns to LI_LIMITED. |
| 1101 | */ |
| 1102 | limit_listener(l, &p->listener_queue); |
Willy Tarreau | eeea808 | 2020-01-08 19:15:07 +0100 | [diff] [blame] | 1103 | if (p->task && tick_isset(expire)) |
| 1104 | task_schedule(p->task, expire); |
Willy Tarreau | 0591bf7 | 2019-12-10 12:01:21 +0100 | [diff] [blame] | 1105 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1108 | /* Notify the listener that a connection initiated from it was released. This |
| 1109 | * is used to keep the connection count consistent and to possibly re-open |
| 1110 | * listening when it was limited. |
| 1111 | */ |
| 1112 | void listener_release(struct listener *l) |
| 1113 | { |
| 1114 | struct proxy *fe = l->bind_conf->frontend; |
| 1115 | |
| 1116 | if (!(l->options & LI_O_UNLIMITED)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1117 | _HA_ATOMIC_DEC(&actconn); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1118 | if (fe) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1119 | _HA_ATOMIC_DEC(&fe->feconn); |
| 1120 | _HA_ATOMIC_DEC(&l->nbconn); |
| 1121 | _HA_ATOMIC_DEC(&l->thr_conn[tid]); |
Willy Tarreau | 82c9789 | 2019-02-27 19:32:32 +0100 | [diff] [blame] | 1122 | |
| 1123 | if (l->state == LI_FULL || l->state == LI_LIMITED) |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1124 | resume_listener(l); |
| 1125 | |
Willy Tarreau | 02757d0 | 2021-01-28 18:07:24 +0100 | [diff] [blame] | 1126 | /* Dequeues all of the listeners waiting for a resource */ |
| 1127 | dequeue_all_listeners(); |
| 1128 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 1129 | if (!MT_LIST_ISEMPTY(&fe->listener_queue) && |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1130 | (!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] | 1131 | dequeue_proxy_listeners(fe); |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 1134 | /* Initializes the listener queues. Returns 0 on success, otherwise ERR_* flags */ |
| 1135 | static int listener_queue_init() |
| 1136 | { |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1137 | global_listener_queue_task = task_new(MAX_THREADS_MASK); |
| 1138 | if (!global_listener_queue_task) { |
| 1139 | ha_alert("Out of memory when initializing global listener queue\n"); |
| 1140 | return ERR_FATAL|ERR_ABORT; |
| 1141 | } |
| 1142 | /* very simple initialization, users will queue the task if needed */ |
| 1143 | global_listener_queue_task->context = NULL; /* not even a context! */ |
| 1144 | global_listener_queue_task->process = manage_global_listener_queue; |
| 1145 | |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | static void listener_queue_deinit() |
| 1150 | { |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1151 | task_destroy(global_listener_queue_task); |
| 1152 | global_listener_queue_task = NULL; |
Willy Tarreau | f2cb169 | 2019-07-11 10:08:31 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | REGISTER_CONFIG_POSTPARSER("multi-threaded listener queue", listener_queue_init); |
| 1156 | REGISTER_POST_DEINIT(listener_queue_deinit); |
| 1157 | |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1158 | |
| 1159 | /* This is the global management task for listeners. It enables listeners waiting |
| 1160 | * 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] | 1161 | * a while. It is designed to be called as a task. It's exported so that it's easy |
| 1162 | * to spot in "show tasks" or "show profiling". |
Willy Tarreau | a1d97f8 | 2019-12-10 11:18:41 +0100 | [diff] [blame] | 1163 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1164 | 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] | 1165 | { |
| 1166 | /* If there are still too many concurrent connections, let's wait for |
| 1167 | * some of them to go away. We don't need to re-arm the timer because |
| 1168 | * each of them will scan the queue anyway. |
| 1169 | */ |
| 1170 | if (unlikely(actconn >= global.maxconn)) |
| 1171 | goto out; |
| 1172 | |
| 1173 | /* We should periodically try to enable listeners waiting for a global |
| 1174 | * resource here, because it is possible, though very unlikely, that |
| 1175 | * they have been blocked by a temporary lack of global resource such |
| 1176 | * as a file descriptor or memory and that the temporary condition has |
| 1177 | * disappeared. |
| 1178 | */ |
| 1179 | dequeue_all_listeners(); |
| 1180 | |
| 1181 | out: |
| 1182 | t->expire = TICK_ETERNITY; |
| 1183 | task_queue(t); |
| 1184 | return t; |
| 1185 | } |
| 1186 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 1187 | /* |
| 1188 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 1189 | * parsing sessions. |
| 1190 | */ |
| 1191 | void bind_register_keywords(struct bind_kw_list *kwl) |
| 1192 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1193 | LIST_APPEND(&bind_keywords.list, &kwl->list); |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. If the |
| 1197 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 1198 | * find one with a valid ->parse() function. This way it is possible to declare |
| 1199 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 1200 | * if some options are met. Note that if the requested keyword contains an |
| 1201 | * opening parenthesis, everything from this point is ignored. |
| 1202 | */ |
| 1203 | struct bind_kw *bind_find_kw(const char *kw) |
| 1204 | { |
| 1205 | int index; |
| 1206 | const char *kwend; |
| 1207 | struct bind_kw_list *kwl; |
| 1208 | struct bind_kw *ret = NULL; |
| 1209 | |
| 1210 | kwend = strchr(kw, '('); |
| 1211 | if (!kwend) |
| 1212 | kwend = kw + strlen(kw); |
| 1213 | |
| 1214 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 1215 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 1216 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 1217 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 1218 | if (kwl->kw[index].parse) |
| 1219 | return &kwl->kw[index]; /* found it !*/ |
| 1220 | else |
| 1221 | ret = &kwl->kw[index]; /* may be OK */ |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | return ret; |
| 1226 | } |
| 1227 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1228 | /* Dumps all registered "bind" keywords to the <out> string pointer. The |
| 1229 | * unsupported keywords are only dumped if their supported form was not |
| 1230 | * found. |
| 1231 | */ |
| 1232 | void bind_dump_kws(char **out) |
| 1233 | { |
| 1234 | struct bind_kw_list *kwl; |
| 1235 | int index; |
| 1236 | |
Christopher Faulet | 784063e | 2020-05-18 12:14:18 +0200 | [diff] [blame] | 1237 | if (!out) |
| 1238 | return; |
| 1239 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1240 | *out = NULL; |
| 1241 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 1242 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 1243 | if (kwl->kw[index].parse || |
| 1244 | bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1245 | memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "", |
| 1246 | kwl->scope, |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1247 | kwl->kw[index].kw, |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1248 | kwl->kw[index].skip ? " <arg>" : "", |
| 1249 | kwl->kw[index].parse ? "" : " (not supported)"); |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
Willy Tarreau | 433b05f | 2021-03-12 10:14:07 +0100 | [diff] [blame] | 1255 | /* Try to find in srv_keyword the word that looks closest to <word> by counting |
| 1256 | * transitions between letters, digits and other characters. Will return the |
| 1257 | * best matching word if found, otherwise NULL. |
| 1258 | */ |
| 1259 | const char *bind_find_best_kw(const char *word) |
| 1260 | { |
| 1261 | uint8_t word_sig[1024]; |
| 1262 | uint8_t list_sig[1024]; |
| 1263 | const struct bind_kw_list *kwl; |
| 1264 | const char *best_ptr = NULL; |
| 1265 | int dist, best_dist = INT_MAX; |
| 1266 | int index; |
| 1267 | |
| 1268 | make_word_fingerprint(word_sig, word); |
| 1269 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 1270 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 1271 | make_word_fingerprint(list_sig, kwl->kw[index].kw); |
| 1272 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 1273 | if (dist < best_dist) { |
| 1274 | best_dist = dist; |
| 1275 | best_ptr = kwl->kw[index].kw; |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr))) |
| 1281 | best_ptr = NULL; |
| 1282 | |
| 1283 | return best_ptr; |
| 1284 | } |
| 1285 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1286 | /************************************************************************/ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 1287 | /* All supported sample and ACL keywords must be declared here. */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1288 | /************************************************************************/ |
| 1289 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1290 | /* 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] | 1291 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1292 | 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] | 1293 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1294 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1295 | smp->data.u.sint = smp->sess->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1296 | return 1; |
| 1297 | } |
| 1298 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1299 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1300 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1301 | 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] | 1302 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1303 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1304 | smp->data.u.sint = smp->sess->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1305 | return 1; |
| 1306 | } |
Jerome Magnin | eb421b2 | 2020-03-27 22:08:40 +0100 | [diff] [blame] | 1307 | static int |
| 1308 | smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1309 | { |
| 1310 | smp->data.u.str.area = smp->sess->listener->name; |
| 1311 | if (!smp->data.u.str.area) |
| 1312 | return 0; |
| 1313 | |
| 1314 | smp->data.type = SMP_T_STR; |
| 1315 | smp->flags = SMP_F_CONST; |
| 1316 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
| 1317 | return 1; |
| 1318 | } |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1319 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1320 | /* parse the "accept-proxy" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1321 | 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] | 1322 | { |
| 1323 | struct listener *l; |
| 1324 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1325 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1326 | l->options |= LI_O_ACC_PROXY; |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1331 | /* parse the "accept-netscaler-cip" bind keyword */ |
| 1332 | static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1333 | { |
| 1334 | struct listener *l; |
| 1335 | uint32_t val; |
| 1336 | |
| 1337 | if (!*args[cur_arg + 1]) { |
| 1338 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 1339 | return ERR_ALERT | ERR_FATAL; |
| 1340 | } |
| 1341 | |
| 1342 | val = atol(args[cur_arg + 1]); |
| 1343 | if (val <= 0) { |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 1344 | 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] | 1345 | return ERR_ALERT | ERR_FATAL; |
| 1346 | } |
| 1347 | |
| 1348 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1349 | l->options |= LI_O_ACC_CIP; |
| 1350 | conf->ns_cip_magic = val; |
| 1351 | } |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1356 | /* parse the "backlog" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1357 | 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] | 1358 | { |
| 1359 | struct listener *l; |
| 1360 | int val; |
| 1361 | |
| 1362 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1363 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1364 | return ERR_ALERT | ERR_FATAL; |
| 1365 | } |
| 1366 | |
| 1367 | val = atol(args[cur_arg + 1]); |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 1368 | if (val < 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1369 | 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] | 1370 | return ERR_ALERT | ERR_FATAL; |
| 1371 | } |
| 1372 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1373 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1374 | l->backlog = val; |
| 1375 | |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | /* parse the "id" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1380 | 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] | 1381 | { |
| 1382 | struct eb32_node *node; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1383 | struct listener *l, *new; |
Thierry Fournier | e7fe8eb | 2016-02-26 08:45:58 +0100 | [diff] [blame] | 1384 | char *error; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1385 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1386 | if (conf->listeners.n != conf->listeners.p) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1387 | 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] | 1388 | return ERR_ALERT | ERR_FATAL; |
| 1389 | } |
| 1390 | |
| 1391 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1392 | memprintf(err, "'%s' : expects an integer argument", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1393 | return ERR_ALERT | ERR_FATAL; |
| 1394 | } |
| 1395 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1396 | new = LIST_NEXT(&conf->listeners, struct listener *, by_bind); |
Thierry Fournier | e7fe8eb | 2016-02-26 08:45:58 +0100 | [diff] [blame] | 1397 | new->luid = strtol(args[cur_arg + 1], &error, 10); |
| 1398 | if (*error != '\0') { |
| 1399 | memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]); |
| 1400 | return ERR_ALERT | ERR_FATAL; |
| 1401 | } |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1402 | new->conf.id.key = new->luid; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1403 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1404 | if (new->luid <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1405 | memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1406 | return ERR_ALERT | ERR_FATAL; |
| 1407 | } |
| 1408 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1409 | node = eb32_lookup(&px->conf.used_listener_id, new->luid); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1410 | if (node) { |
| 1411 | l = container_of(node, struct listener, conf.id); |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1412 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')", |
| 1413 | args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line, |
| 1414 | l->bind_conf->arg); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1415 | return ERR_ALERT | ERR_FATAL; |
| 1416 | } |
| 1417 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1418 | eb32_insert(&px->conf.used_listener_id, &new->conf.id); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | /* parse the "maxconn" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1423 | 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] | 1424 | { |
| 1425 | struct listener *l; |
| 1426 | int val; |
| 1427 | |
| 1428 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1429 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1430 | return ERR_ALERT | ERR_FATAL; |
| 1431 | } |
| 1432 | |
| 1433 | val = atol(args[cur_arg + 1]); |
Willy Tarreau | a8cf66b | 2019-02-27 16:49:00 +0100 | [diff] [blame] | 1434 | if (val < 0) { |
| 1435 | 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] | 1436 | return ERR_ALERT | ERR_FATAL; |
| 1437 | } |
| 1438 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1439 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1440 | l->maxconn = val; |
| 1441 | |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | /* parse the "name" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1446 | 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] | 1447 | { |
| 1448 | struct listener *l; |
| 1449 | |
| 1450 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1451 | memprintf(err, "'%s' : missing name", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1452 | return ERR_ALERT | ERR_FATAL; |
| 1453 | } |
| 1454 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1455 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1456 | l->name = strdup(args[cur_arg + 1]); |
| 1457 | |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | /* parse the "nice" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1462 | 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] | 1463 | { |
| 1464 | struct listener *l; |
| 1465 | int val; |
| 1466 | |
| 1467 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1468 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1469 | return ERR_ALERT | ERR_FATAL; |
| 1470 | } |
| 1471 | |
| 1472 | val = atol(args[cur_arg + 1]); |
| 1473 | if (val < -1024 || val > 1024) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1474 | 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] | 1475 | return ERR_ALERT | ERR_FATAL; |
| 1476 | } |
| 1477 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1478 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1479 | l->nice = val; |
| 1480 | |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 1484 | /* parse the "process" bind keyword */ |
| 1485 | static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1486 | { |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 1487 | char *slash; |
| 1488 | unsigned long proc = 0, thread = 0; |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 1489 | |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 1490 | if ((slash = strchr(args[cur_arg + 1], '/')) != NULL) |
| 1491 | *slash = 0; |
| 1492 | |
Willy Tarreau | 72faef3 | 2021-06-15 08:36:30 +0200 | [diff] [blame] | 1493 | if (parse_process_number(args[cur_arg + 1], &proc, 1, NULL, err)) { |
Christopher Faulet | f1f0c5f | 2017-11-22 12:06:43 +0100 | [diff] [blame] | 1494 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 1495 | return ERR_ALERT | ERR_FATAL; |
| 1496 | } |
| 1497 | |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 1498 | if (slash) { |
Willy Tarreau | c9a82e4 | 2019-01-26 13:25:14 +0100 | [diff] [blame] | 1499 | if (parse_process_number(slash+1, &thread, MAX_THREADS, NULL, err)) { |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 1500 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
| 1501 | return ERR_ALERT | ERR_FATAL; |
| 1502 | } |
| 1503 | *slash = '/'; |
| 1504 | } |
| 1505 | |
Willy Tarreau | e26993c | 2020-09-03 07:18:55 +0200 | [diff] [blame] | 1506 | conf->settings.bind_thread |= thread; |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 1507 | return 0; |
| 1508 | } |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1509 | |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 1510 | /* parse the "proto" bind keyword */ |
| 1511 | static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1512 | { |
| 1513 | struct ist proto; |
| 1514 | |
| 1515 | if (!*args[cur_arg + 1]) { |
| 1516 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 1517 | return ERR_ALERT | ERR_FATAL; |
| 1518 | } |
| 1519 | |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 1520 | proto = ist(args[cur_arg + 1]); |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 1521 | conf->mux_proto = get_mux_proto(proto); |
| 1522 | if (!conf->mux_proto) { |
| 1523 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]); |
| 1524 | return ERR_ALERT | ERR_FATAL; |
| 1525 | } |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 1526 | return 0; |
| 1527 | } |
| 1528 | |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 1529 | /* config parser for global "tune.listener.multi-queue", accepts "on" or "off" */ |
| 1530 | 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] | 1531 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 1532 | char **err) |
| 1533 | { |
| 1534 | if (too_many_args(1, args, err, NULL)) |
| 1535 | return -1; |
| 1536 | |
| 1537 | if (strcmp(args[1], "on") == 0) |
| 1538 | global.tune.options |= GTUNE_LISTENER_MQ; |
| 1539 | else if (strcmp(args[1], "off") == 0) |
| 1540 | global.tune.options &= ~GTUNE_LISTENER_MQ; |
| 1541 | else { |
| 1542 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 1543 | return -1; |
| 1544 | } |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 1548 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1549 | * Please take care of keeping this list alphabetically sorted. |
| 1550 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1551 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1552 | { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, }, |
| 1553 | { "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] | 1554 | { "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] | 1555 | { /* END */ }, |
| 1556 | }}; |
| 1557 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1558 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 1559 | |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 1560 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1561 | * Please take care of keeping this list alphabetically sorted. |
| 1562 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1563 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 1564 | { /* END */ }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1565 | }}; |
| 1566 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1567 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 1568 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1569 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1570 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1571 | * all code contributors. |
| 1572 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1573 | * the config parser can report an appropriate error when a known keyword was |
| 1574 | * not enabled. |
| 1575 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1576 | static struct bind_kw_list bind_kws = { "ALL", { }, { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1577 | { "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] | 1578 | { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */ |
| 1579 | { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */ |
| 1580 | { "id", bind_parse_id, 1 }, /* set id of listening socket */ |
| 1581 | { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */ |
| 1582 | { "name", bind_parse_name, 1 }, /* set name of listening socket */ |
| 1583 | { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */ |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 1584 | { "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] | 1585 | { "proto", bind_parse_proto, 1 }, /* set the proto to use for all incoming connections */ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 1586 | { /* END */ }, |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1587 | }}; |
| 1588 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1589 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 1590 | |
Willy Tarreau | 7ac908b | 2019-02-27 12:02:18 +0100 | [diff] [blame] | 1591 | /* config keyword parsers */ |
| 1592 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1593 | { CFG_GLOBAL, "tune.listener.multi-queue", cfg_parse_tune_listener_mq }, |
| 1594 | { 0, NULL, NULL } |
| 1595 | }}; |
| 1596 | |
| 1597 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 1598 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1599 | /* |
| 1600 | * Local variables: |
| 1601 | * c-indent-level: 8 |
| 1602 | * c-basic-offset: 8 |
| 1603 | * End: |
| 1604 | */ |