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