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