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 | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 25 | #include <common/mini-clist.h> |
| 26 | #include <common/standard.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 27 | #include <common/time.h> |
| 28 | |
| 29 | #include <types/global.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 30 | #include <types/protocol.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 32 | #include <proto/acl.h> |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 33 | #include <proto/connection.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 34 | #include <proto/fd.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 35 | #include <proto/freq_ctr.h> |
| 36 | #include <proto/log.h> |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 37 | #include <proto/listener.h> |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 38 | #include <proto/protocol.h> |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 39 | #include <proto/sample.h> |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 40 | #include <proto/stream.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 41 | #include <proto/task.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 42 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 43 | /* listner_queue lock (same for global and per proxy queues) */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 44 | __decl_hathreads(static HA_SPINLOCK_T lq_lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 46 | /* List head of all known bind keywords */ |
| 47 | static struct bind_kw_list bind_keywords = { |
| 48 | .list = LIST_HEAD_INIT(bind_keywords.list) |
| 49 | }; |
| 50 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 51 | struct xfer_sock_list *xfer_sock_list = NULL; |
| 52 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 53 | /* This function adds the specified listener's file descriptor to the polling |
| 54 | * 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] | 55 | * LI_FULL state depending on its number of connections. In deamon mode, we |
| 56 | * also support binding only the relevant processes to their respective |
| 57 | * listeners. We don't do that in debug mode however. |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 58 | */ |
Christopher Faulet | f5b8adc | 2017-06-02 10:00:35 +0200 | [diff] [blame] | 59 | static void enable_listener(struct listener *listener) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 60 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 61 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 62 | if (listener->state == LI_LISTEN) { |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 63 | if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) && |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 64 | listener->bind_conf->bind_proc && |
Willy Tarreau | 387bd4f | 2017-11-10 19:08:14 +0100 | [diff] [blame] | 65 | !(listener->bind_conf->bind_proc & pid_bit)) { |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 66 | /* we don't want to enable this listener and don't |
| 67 | * want any fd event to reach it. |
| 68 | */ |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 69 | if (!(global.tune.options & GTUNE_SOCKET_TRANSFER)) |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 70 | do_unbind_listener(listener, 1); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 71 | else { |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 72 | do_unbind_listener(listener, 0); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 73 | listener->state = LI_LISTEN; |
| 74 | } |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 75 | } |
| 76 | else if (listener->nbconn < listener->maxconn) { |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 77 | fd_want_recv(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 78 | listener->state = LI_READY; |
Willy Tarreau | ae30253 | 2014-05-07 19:22:24 +0200 | [diff] [blame] | 79 | } |
| 80 | else { |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 81 | listener->state = LI_FULL; |
| 82 | } |
| 83 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 84 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* This function removes the specified listener's file descriptor from the |
| 88 | * polling lists if it is in the LI_READY or in the LI_FULL state. The listener |
| 89 | * enters LI_LISTEN. |
| 90 | */ |
Christopher Faulet | f5b8adc | 2017-06-02 10:00:35 +0200 | [diff] [blame] | 91 | static void disable_listener(struct listener *listener) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 92 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 93 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 94 | if (listener->state < LI_READY) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 95 | goto end; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 96 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 97 | fd_stop_recv(listener->fd); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 98 | if (listener->state == LI_LIMITED) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 99 | HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 100 | LIST_DEL(&listener->wait_queue); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 101 | HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 102 | } |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 103 | listener->state = LI_LISTEN; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 104 | end: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 105 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 108 | /* This function tries to temporarily disable a listener, depending on the OS |
| 109 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 110 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 111 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 112 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 113 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 114 | */ |
| 115 | int pause_listener(struct listener *l) |
| 116 | { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 117 | int ret = 1; |
| 118 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 119 | HA_SPIN_LOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 120 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 121 | if (l->state <= LI_ZOMBIE) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 122 | goto end; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 124 | if (l->proto->pause) { |
| 125 | /* Returns < 0 in case of failure, 0 if the listener |
| 126 | * was totally stopped, or > 0 if correctly paused. |
| 127 | */ |
| 128 | int ret = l->proto->pause(l); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 129 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 130 | if (ret < 0) { |
| 131 | ret = 0; |
| 132 | goto end; |
| 133 | } |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 134 | else if (ret == 0) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 135 | goto end; |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 136 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 137 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 138 | if (l->state == LI_LIMITED) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 139 | HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 140 | LIST_DEL(&l->wait_queue); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 141 | HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 142 | } |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 143 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 144 | fd_stop_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 145 | l->state = LI_PAUSED; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 146 | end: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 147 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 148 | return ret; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 151 | /* This function tries to resume a temporarily disabled listener. Paused, full, |
| 152 | * limited and disabled listeners are handled, which means that this function |
| 153 | * may replace enable_listener(). The resulting state will either be LI_READY |
| 154 | * 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] | 155 | * 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] | 156 | * foreground mode, and are ignored. If the listener was only in the assigned |
| 157 | * state, it's totally rebound. This can happen if a pause() has completely |
| 158 | * stopped it. If the resume fails, 0 is returned and an error might be |
| 159 | * displayed. |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 160 | */ |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 161 | static int __resume_listener(struct listener *l) |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 162 | { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 163 | int ret = 1; |
| 164 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 165 | HA_SPIN_LOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 166 | |
William Lallemand | 095ba4c | 2017-06-01 17:38:50 +0200 | [diff] [blame] | 167 | if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) && |
Willy Tarreau | 3569df3 | 2017-03-15 12:47:46 +0100 | [diff] [blame] | 168 | l->bind_conf->bind_proc && |
Willy Tarreau | 387bd4f | 2017-11-10 19:08:14 +0100 | [diff] [blame] | 169 | !(l->bind_conf->bind_proc & pid_bit)) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 170 | goto end; |
Willy Tarreau | 3569df3 | 2017-03-15 12:47:46 +0100 | [diff] [blame] | 171 | |
Willy Tarreau | 1c4b814 | 2014-07-07 21:06:24 +0200 | [diff] [blame] | 172 | if (l->state == LI_ASSIGNED) { |
| 173 | char msg[100]; |
| 174 | int err; |
| 175 | |
| 176 | err = l->proto->bind(l, msg, sizeof(msg)); |
| 177 | if (err & ERR_ALERT) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 178 | ha_alert("Resuming listener: %s\n", msg); |
Willy Tarreau | 1c4b814 | 2014-07-07 21:06:24 +0200 | [diff] [blame] | 179 | else if (err & ERR_WARN) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 180 | ha_warning("Resuming listener: %s\n", msg); |
Willy Tarreau | 1c4b814 | 2014-07-07 21:06:24 +0200 | [diff] [blame] | 181 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 182 | if (err & (ERR_FATAL | ERR_ABORT)) { |
| 183 | ret = 0; |
| 184 | goto end; |
| 185 | } |
Willy Tarreau | 1c4b814 | 2014-07-07 21:06:24 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 188 | if (l->state < LI_PAUSED || l->state == LI_ZOMBIE) { |
| 189 | ret = 0; |
| 190 | goto end; |
| 191 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 192 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 193 | if (l->proto->sock_prot == IPPROTO_TCP && |
| 194 | l->state == LI_PAUSED && |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 195 | listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) { |
| 196 | ret = 0; |
| 197 | goto end; |
| 198 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 199 | |
| 200 | if (l->state == LI_READY) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 201 | goto end; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 202 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 203 | if (l->state == LI_LIMITED) |
| 204 | LIST_DEL(&l->wait_queue); |
| 205 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 206 | if (l->nbconn >= l->maxconn) { |
| 207 | l->state = LI_FULL; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 208 | goto end; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 211 | fd_want_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 212 | l->state = LI_READY; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 213 | end: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 214 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | int resume_listener(struct listener *l) |
| 219 | { |
| 220 | int ret; |
| 221 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 222 | HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 223 | ret = __resume_listener(l); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 224 | HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 225 | return ret; |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 228 | /* 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] | 229 | * it upon next close() using resume_listener(). |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 230 | * |
| 231 | * Note: this function is only called from listener_accept so <l> is already |
| 232 | * locked. |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 233 | */ |
Christopher Faulet | 5580ba2 | 2017-08-28 15:29:20 +0200 | [diff] [blame] | 234 | static void listener_full(struct listener *l) |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 235 | { |
| 236 | if (l->state >= LI_READY) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 237 | if (l->state == LI_LIMITED) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 238 | HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 239 | LIST_DEL(&l->wait_queue); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 240 | HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 241 | } |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 242 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 243 | fd_stop_recv(l->fd); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 244 | l->state = LI_FULL; |
| 245 | } |
| 246 | } |
| 247 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 248 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 249 | * resources are free again. It will be queued into the specified queue. |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 250 | * |
| 251 | * Note: this function is only called from listener_accept so <l> is already |
| 252 | * locked. |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 253 | */ |
Christopher Faulet | 5580ba2 | 2017-08-28 15:29:20 +0200 | [diff] [blame] | 254 | static void limit_listener(struct listener *l, struct list *list) |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 255 | { |
| 256 | if (l->state == LI_READY) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 257 | HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 258 | LIST_ADDQ(list, &l->wait_queue); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 259 | HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 260 | fd_stop_recv(l->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 261 | l->state = LI_LIMITED; |
| 262 | } |
| 263 | } |
| 264 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 265 | /* This function adds all of the protocol's listener's file descriptors to the |
| 266 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 267 | * used as a protocol's generic enable_all() primitive, for use after the |
| 268 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 269 | * their number of connections. It always returns ERR_NONE. |
| 270 | */ |
| 271 | int enable_all_listeners(struct protocol *proto) |
| 272 | { |
| 273 | struct listener *listener; |
| 274 | |
| 275 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 276 | enable_listener(listener); |
| 277 | return ERR_NONE; |
| 278 | } |
| 279 | |
| 280 | /* This function removes all of the protocol's listener's file descriptors from |
| 281 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 282 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 283 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 284 | */ |
| 285 | int disable_all_listeners(struct protocol *proto) |
| 286 | { |
| 287 | struct listener *listener; |
| 288 | |
| 289 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 290 | disable_listener(listener); |
| 291 | return ERR_NONE; |
| 292 | } |
| 293 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 294 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 295 | void dequeue_all_listeners(struct list *list) |
| 296 | { |
| 297 | struct listener *listener, *l_back; |
| 298 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 299 | HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 300 | list_for_each_entry_safe(listener, l_back, list, wait_queue) { |
| 301 | /* This cannot fail because the listeners are by definition in |
| 302 | * the LI_LIMITED state. The function also removes the entry |
| 303 | * from the queue. |
| 304 | */ |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 305 | __resume_listener(listener); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 306 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 307 | HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 310 | /* Must be called with the lock held. Depending on <do_close> value, it does |
| 311 | * what unbind_listener or unbind_listener_no_close should do. |
| 312 | */ |
| 313 | void do_unbind_listener(struct listener *listener, int do_close) |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 314 | { |
| 315 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 316 | fd_stop_recv(listener->fd); |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 317 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 318 | if (listener->state == LI_LIMITED) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 319 | HA_SPIN_LOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 320 | LIST_DEL(&listener->wait_queue); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 321 | HA_SPIN_UNLOCK(LISTENER_QUEUE_LOCK, &lq_lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 322 | } |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 323 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 324 | if (listener->state >= LI_PAUSED) { |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 325 | if (do_close) { |
| 326 | fd_delete(listener->fd); |
| 327 | listener->fd = -1; |
| 328 | } |
| 329 | else |
| 330 | fd_remove(listener->fd); |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 331 | listener->state = LI_ASSIGNED; |
| 332 | } |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 335 | /* This function closes the listening socket for the specified listener, |
| 336 | * 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] | 337 | * LI_ASSIGNED state. This function is intended to be used as a generic |
| 338 | * function for standard protocols. |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 339 | */ |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 340 | void unbind_listener(struct listener *listener) |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 341 | { |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 342 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 343 | do_unbind_listener(listener, 1); |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 344 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* This function pretends the listener is dead, but keeps the FD opened, so |
| 348 | * that we can provide it, for conf reloading. |
| 349 | */ |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 350 | void unbind_listener_no_close(struct listener *listener) |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 351 | { |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 352 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 353 | do_unbind_listener(listener, 0); |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 354 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 357 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 358 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 359 | * detach them from the protocol. It always returns ERR_NONE. |
| 360 | */ |
| 361 | int unbind_all_listeners(struct protocol *proto) |
| 362 | { |
| 363 | struct listener *listener; |
| 364 | |
| 365 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 366 | unbind_listener(listener); |
| 367 | return ERR_NONE; |
| 368 | } |
| 369 | |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 370 | /* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port |
| 371 | * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto |
| 372 | * allocation). The address family is taken from ss->ss_family. The number of |
| 373 | * jobs and listeners is automatically increased by the number of listeners |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 374 | * created. If the <inherited> argument is set to 1, it specifies that the FD |
| 375 | * was obtained from a parent process. |
| 376 | * 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] | 377 | * set in <err>. |
| 378 | */ |
| 379 | int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss, |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 380 | int portl, int porth, int fd, int inherited, char **err) |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 381 | { |
| 382 | struct protocol *proto = protocol_by_family(ss->ss_family); |
| 383 | struct listener *l; |
| 384 | int port; |
| 385 | |
| 386 | if (!proto) { |
| 387 | memprintf(err, "unsupported protocol family %d", ss->ss_family); |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | for (port = portl; port <= porth; port++) { |
| 392 | l = calloc(1, sizeof(*l)); |
| 393 | if (!l) { |
| 394 | memprintf(err, "out of memory"); |
| 395 | return 0; |
| 396 | } |
| 397 | l->obj_type = OBJ_TYPE_LISTENER; |
| 398 | LIST_ADDQ(&bc->frontend->conf.listeners, &l->by_fe); |
| 399 | LIST_ADDQ(&bc->listeners, &l->by_bind); |
| 400 | l->bind_conf = bc; |
| 401 | |
| 402 | l->fd = fd; |
| 403 | memcpy(&l->addr, ss, sizeof(*ss)); |
| 404 | l->state = LI_INIT; |
| 405 | |
| 406 | proto->add(l, port); |
| 407 | |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 408 | if (inherited) |
| 409 | l->options |= LI_O_INHERITED; |
| 410 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 411 | HA_SPIN_INIT(&l->lock); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 412 | HA_ATOMIC_ADD(&jobs, 1); |
| 413 | HA_ATOMIC_ADD(&listeners, 1); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 414 | } |
| 415 | return 1; |
| 416 | } |
| 417 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 418 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 419 | * 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] | 420 | * number of listeners is updated, as well as the global number of listeners |
| 421 | * and jobs. Note that the listener must have previously been unbound. This |
| 422 | * is the generic function to use to remove a listener. |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 423 | */ |
| 424 | void delete_listener(struct listener *listener) |
| 425 | { |
| 426 | if (listener->state != LI_ASSIGNED) |
| 427 | return; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 428 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 429 | HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 430 | listener->state = LI_INIT; |
| 431 | LIST_DEL(&listener->proto_list); |
| 432 | listener->proto->nb_listeners--; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 433 | HA_ATOMIC_SUB(&jobs, 1); |
| 434 | HA_ATOMIC_SUB(&listeners, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 435 | HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock); |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 438 | /* This function is called on a read event from a listening socket, corresponding |
| 439 | * to an accept. It tries to accept as many connections as possible, and for each |
| 440 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 441 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 442 | void listener_accept(int fd) |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 443 | { |
| 444 | struct listener *l = fdtab[fd].owner; |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 445 | struct proxy *p = l->bind_conf->frontend; |
Willy Tarreau | 50de90a | 2012-11-23 20:11:45 +0100 | [diff] [blame] | 446 | int max_accept = l->maxaccept ? l->maxaccept : 1; |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 447 | int expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 448 | int cfd; |
| 449 | int ret; |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 450 | #ifdef USE_ACCEPT4 |
| 451 | static int accept4_broken; |
| 452 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 453 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 454 | if (HA_SPIN_TRYLOCK(LISTENER_LOCK, &l->lock)) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 455 | return; |
| 456 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 457 | if (unlikely(l->nbconn >= l->maxconn)) { |
| 458 | listener_full(l); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 459 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 462 | if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) { |
| 463 | 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] | 464 | |
| 465 | if (unlikely(!max)) { |
| 466 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 467 | expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0)); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 468 | goto wait_expire; |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (max_accept > max) |
| 472 | max_accept = max; |
| 473 | } |
| 474 | |
| 475 | if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) { |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 476 | int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); |
| 477 | |
| 478 | if (unlikely(!max)) { |
| 479 | /* frontend accept rate limit was reached */ |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 480 | expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0)); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 481 | goto wait_expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (max_accept > max) |
| 485 | max_accept = max; |
| 486 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 487 | #ifdef USE_OPENSSL |
| 488 | if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) { |
| 489 | 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] | 490 | |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 491 | if (unlikely(!max)) { |
| 492 | /* frontend accept rate limit was reached */ |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 493 | expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0)); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 494 | goto wait_expire; |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (max_accept > max) |
| 498 | max_accept = max; |
| 499 | } |
| 500 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 501 | if (p && p->fe_sps_lim) { |
| 502 | int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); |
| 503 | |
| 504 | if (unlikely(!max)) { |
| 505 | /* frontend accept rate limit was reached */ |
| 506 | limit_listener(l, &p->listener_queue); |
| 507 | task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 508 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | if (max_accept > max) |
| 512 | max_accept = max; |
| 513 | } |
| 514 | |
| 515 | /* Note: if we fail to allocate a connection because of configured |
| 516 | * limits, we'll schedule a new attempt worst 1 second later in the |
| 517 | * worst case. If we fail due to system limits or temporary resource |
| 518 | * shortage, we try again 100ms later in the worst case. |
| 519 | */ |
| 520 | while (max_accept--) { |
| 521 | struct sockaddr_storage addr; |
| 522 | socklen_t laddr = sizeof(addr); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 523 | unsigned int count; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 524 | |
| 525 | if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) { |
| 526 | limit_listener(l, &global_listener_queue); |
| 527 | task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 528 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (unlikely(p && p->feconn >= p->maxconn)) { |
| 532 | limit_listener(l, &p->listener_queue); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 533 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Willy Tarreau | 1bc4aab | 2012-10-08 20:11:03 +0200 | [diff] [blame] | 536 | #ifdef USE_ACCEPT4 |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 537 | /* only call accept4() if it's known to be safe, otherwise |
| 538 | * fallback to the legacy accept() + fcntl(). |
| 539 | */ |
| 540 | if (unlikely(accept4_broken || |
| 541 | ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 && |
| 542 | (errno == ENOSYS || errno == EINVAL || errno == EBADF) && |
| 543 | (accept4_broken = 1)))) |
| 544 | #endif |
Willy Tarreau | 6b3b0d4 | 2012-10-22 19:32:55 +0200 | [diff] [blame] | 545 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1) |
| 546 | fcntl(cfd, F_SETFL, O_NONBLOCK); |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 547 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 548 | if (unlikely(cfd == -1)) { |
| 549 | switch (errno) { |
| 550 | case EAGAIN: |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 551 | if (fdtab[fd].ev & FD_POLL_HUP) { |
| 552 | /* the listening socket might have been disabled in a shared |
| 553 | * process and we're a collateral victim. We'll just pause for |
| 554 | * a while in case it comes back. In the mean time, we need to |
| 555 | * clear this sticky flag. |
| 556 | */ |
| 557 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 558 | goto transient_error; |
| 559 | } |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 560 | fd_cant_recv(fd); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 561 | goto end; /* nothing more to accept */ |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 562 | case EINVAL: |
| 563 | /* might be trying to accept on a shut fd (eg: soft stop) */ |
| 564 | goto transient_error; |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 565 | case EINTR: |
| 566 | case ECONNABORTED: |
| 567 | continue; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 568 | case ENFILE: |
| 569 | if (p) |
| 570 | send_log(p, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 571 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 572 | p->id, global.maxsock); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 573 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 574 | case EMFILE: |
| 575 | if (p) |
| 576 | send_log(p, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 577 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 578 | p->id, global.maxsock); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 579 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 580 | case ENOBUFS: |
| 581 | case ENOMEM: |
| 582 | if (p) |
| 583 | send_log(p, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 584 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 585 | p->id, global.maxsock); |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 586 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 587 | default: |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 588 | /* unexpected result, let's give up and let other tasks run */ |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 589 | goto stop; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | if (unlikely(cfd >= global.maxsock)) { |
| 594 | send_log(p, LOG_EMERG, |
| 595 | "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", |
| 596 | p->id); |
| 597 | close(cfd); |
| 598 | limit_listener(l, &global_listener_queue); |
| 599 | task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 600 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* increase the per-process number of cumulated connections */ |
| 604 | if (!(l->options & LI_O_UNLIMITED)) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 605 | count = update_freq_ctr(&global.conn_per_sec, 1); |
| 606 | HA_ATOMIC_UPDATE_MAX(&global.cps_max, count); |
| 607 | HA_ATOMIC_ADD(&actconn, 1); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 610 | count = HA_ATOMIC_ADD(&l->nbconn, 1); |
| 611 | if (l->counters) |
| 612 | HA_ATOMIC_UPDATE_MAX(&l->counters->conn_max, count); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 613 | |
| 614 | ret = l->accept(l, cfd, &addr); |
| 615 | if (unlikely(ret <= 0)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 616 | /* The connection was closed by stream_accept(). Either |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 617 | * we just have to ignore it (ret == 0) or it's a critical |
| 618 | * error due to a resource shortage, and we must stop the |
| 619 | * listener (ret < 0). |
| 620 | */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 621 | if (ret == 0) /* successful termination */ |
| 622 | continue; |
| 623 | |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 624 | goto transient_error; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | if (l->nbconn >= l->maxconn) { |
| 628 | listener_full(l); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 629 | goto end; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 632 | /* increase the per-process number of cumulated connections */ |
| 633 | if (!(l->options & LI_O_UNLIMITED)) { |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 634 | count = update_freq_ctr(&global.sess_per_sec, 1); |
| 635 | HA_ATOMIC_UPDATE_MAX(&global.sps_max, count); |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 636 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 637 | #ifdef USE_OPENSSL |
| 638 | 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] | 639 | count = update_freq_ctr(&global.ssl_per_sec, 1); |
| 640 | HA_ATOMIC_UPDATE_MAX(&global.ssl_max, count); |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 641 | } |
| 642 | #endif |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 643 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 644 | } /* end of while (max_accept--) */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 645 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 646 | /* we've exhausted max_accept, so there is no need to poll again */ |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 647 | stop: |
| 648 | fd_done_recv(fd); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 649 | goto end; |
Willy Tarreau | bb66030 | 2014-05-07 19:47:02 +0200 | [diff] [blame] | 650 | |
| 651 | transient_error: |
| 652 | /* pause the listener and try again in 100 ms */ |
| 653 | expire = tick_add(now_ms, 100); |
| 654 | |
| 655 | wait_expire: |
| 656 | limit_listener(l, &global_listener_queue); |
| 657 | task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire)); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 658 | end: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 659 | HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 662 | /* Notify the listener that a connection initiated from it was released. This |
| 663 | * is used to keep the connection count consistent and to possibly re-open |
| 664 | * listening when it was limited. |
| 665 | */ |
| 666 | void listener_release(struct listener *l) |
| 667 | { |
| 668 | struct proxy *fe = l->bind_conf->frontend; |
| 669 | |
| 670 | if (!(l->options & LI_O_UNLIMITED)) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 671 | HA_ATOMIC_SUB(&actconn, 1); |
| 672 | HA_ATOMIC_SUB(&l->nbconn, 1); |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 673 | if (l->state == LI_FULL) |
| 674 | resume_listener(l); |
| 675 | |
| 676 | /* Dequeues all of the listeners waiting for a resource */ |
| 677 | if (!LIST_ISEMPTY(&global_listener_queue)) |
| 678 | dequeue_all_listeners(&global_listener_queue); |
| 679 | |
| 680 | if (!LIST_ISEMPTY(&fe->listener_queue) && |
| 681 | (!fe->fe_sps_lim || freq_ctr_remain(&fe->fe_sess_per_sec, fe->fe_sps_lim, 0) > 0)) |
| 682 | dequeue_all_listeners(&fe->listener_queue); |
| 683 | } |
| 684 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 685 | /* |
| 686 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 687 | * parsing sessions. |
| 688 | */ |
| 689 | void bind_register_keywords(struct bind_kw_list *kwl) |
| 690 | { |
| 691 | LIST_ADDQ(&bind_keywords.list, &kwl->list); |
| 692 | } |
| 693 | |
| 694 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. If the |
| 695 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 696 | * find one with a valid ->parse() function. This way it is possible to declare |
| 697 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 698 | * if some options are met. Note that if the requested keyword contains an |
| 699 | * opening parenthesis, everything from this point is ignored. |
| 700 | */ |
| 701 | struct bind_kw *bind_find_kw(const char *kw) |
| 702 | { |
| 703 | int index; |
| 704 | const char *kwend; |
| 705 | struct bind_kw_list *kwl; |
| 706 | struct bind_kw *ret = NULL; |
| 707 | |
| 708 | kwend = strchr(kw, '('); |
| 709 | if (!kwend) |
| 710 | kwend = kw + strlen(kw); |
| 711 | |
| 712 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 713 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 714 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 715 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 716 | if (kwl->kw[index].parse) |
| 717 | return &kwl->kw[index]; /* found it !*/ |
| 718 | else |
| 719 | ret = &kwl->kw[index]; /* may be OK */ |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | return ret; |
| 724 | } |
| 725 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 726 | /* Dumps all registered "bind" keywords to the <out> string pointer. The |
| 727 | * unsupported keywords are only dumped if their supported form was not |
| 728 | * found. |
| 729 | */ |
| 730 | void bind_dump_kws(char **out) |
| 731 | { |
| 732 | struct bind_kw_list *kwl; |
| 733 | int index; |
| 734 | |
| 735 | *out = NULL; |
| 736 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 737 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 738 | if (kwl->kw[index].parse || |
| 739 | bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 740 | memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "", |
| 741 | kwl->scope, |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 742 | kwl->kw[index].kw, |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 743 | kwl->kw[index].skip ? " <arg>" : "", |
| 744 | kwl->kw[index].parse ? "" : " (not supported)"); |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 750 | /************************************************************************/ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 751 | /* All supported sample and ACL keywords must be declared here. */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 752 | /************************************************************************/ |
| 753 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 754 | /* 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] | 755 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 756 | 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] | 757 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 758 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 759 | smp->data.u.sint = smp->sess->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 760 | return 1; |
| 761 | } |
| 762 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 763 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 764 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 765 | 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] | 766 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 767 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 768 | smp->data.u.sint = smp->sess->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 769 | return 1; |
| 770 | } |
| 771 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 772 | /* parse the "accept-proxy" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 773 | 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] | 774 | { |
| 775 | struct listener *l; |
| 776 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 777 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 778 | l->options |= LI_O_ACC_PROXY; |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 783 | /* parse the "accept-netscaler-cip" bind keyword */ |
| 784 | static int bind_parse_accept_netscaler_cip(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 785 | { |
| 786 | struct listener *l; |
| 787 | uint32_t val; |
| 788 | |
| 789 | if (!*args[cur_arg + 1]) { |
| 790 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 791 | return ERR_ALERT | ERR_FATAL; |
| 792 | } |
| 793 | |
| 794 | val = atol(args[cur_arg + 1]); |
| 795 | if (val <= 0) { |
| 796 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
| 797 | return ERR_ALERT | ERR_FATAL; |
| 798 | } |
| 799 | |
| 800 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 801 | l->options |= LI_O_ACC_CIP; |
| 802 | conf->ns_cip_magic = val; |
| 803 | } |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 808 | /* parse the "backlog" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 809 | 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] | 810 | { |
| 811 | struct listener *l; |
| 812 | int val; |
| 813 | |
| 814 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 815 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 816 | return ERR_ALERT | ERR_FATAL; |
| 817 | } |
| 818 | |
| 819 | val = atol(args[cur_arg + 1]); |
| 820 | if (val <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 821 | 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] | 822 | return ERR_ALERT | ERR_FATAL; |
| 823 | } |
| 824 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 825 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 826 | l->backlog = val; |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | /* parse the "id" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 832 | 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] | 833 | { |
| 834 | struct eb32_node *node; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 835 | struct listener *l, *new; |
Thierry Fournier | e7fe8eb | 2016-02-26 08:45:58 +0100 | [diff] [blame] | 836 | char *error; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 837 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 838 | if (conf->listeners.n != conf->listeners.p) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 839 | 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] | 840 | return ERR_ALERT | ERR_FATAL; |
| 841 | } |
| 842 | |
| 843 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 844 | memprintf(err, "'%s' : expects an integer argument", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 845 | return ERR_ALERT | ERR_FATAL; |
| 846 | } |
| 847 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 848 | new = LIST_NEXT(&conf->listeners, struct listener *, by_bind); |
Thierry Fournier | e7fe8eb | 2016-02-26 08:45:58 +0100 | [diff] [blame] | 849 | new->luid = strtol(args[cur_arg + 1], &error, 10); |
| 850 | if (*error != '\0') { |
| 851 | memprintf(err, "'%s' : expects an integer argument, found '%s'", args[cur_arg], args[cur_arg + 1]); |
| 852 | return ERR_ALERT | ERR_FATAL; |
| 853 | } |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 854 | new->conf.id.key = new->luid; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 855 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 856 | if (new->luid <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 857 | memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 858 | return ERR_ALERT | ERR_FATAL; |
| 859 | } |
| 860 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 861 | node = eb32_lookup(&px->conf.used_listener_id, new->luid); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 862 | if (node) { |
| 863 | l = container_of(node, struct listener, conf.id); |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 864 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')", |
| 865 | args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line, |
| 866 | l->bind_conf->arg); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 867 | return ERR_ALERT | ERR_FATAL; |
| 868 | } |
| 869 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 870 | eb32_insert(&px->conf.used_listener_id, &new->conf.id); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /* parse the "maxconn" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 875 | 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] | 876 | { |
| 877 | struct listener *l; |
| 878 | int val; |
| 879 | |
| 880 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 881 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 882 | return ERR_ALERT | ERR_FATAL; |
| 883 | } |
| 884 | |
| 885 | val = atol(args[cur_arg + 1]); |
| 886 | if (val <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 887 | 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] | 888 | return ERR_ALERT | ERR_FATAL; |
| 889 | } |
| 890 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 891 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 892 | l->maxconn = val; |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /* parse the "name" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 898 | 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] | 899 | { |
| 900 | struct listener *l; |
| 901 | |
| 902 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 903 | memprintf(err, "'%s' : missing name", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 904 | return ERR_ALERT | ERR_FATAL; |
| 905 | } |
| 906 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 907 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 908 | l->name = strdup(args[cur_arg + 1]); |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | /* parse the "nice" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 914 | 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] | 915 | { |
| 916 | struct listener *l; |
| 917 | int val; |
| 918 | |
| 919 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 920 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 921 | return ERR_ALERT | ERR_FATAL; |
| 922 | } |
| 923 | |
| 924 | val = atol(args[cur_arg + 1]); |
| 925 | if (val < -1024 || val > 1024) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 926 | 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] | 927 | return ERR_ALERT | ERR_FATAL; |
| 928 | } |
| 929 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 930 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 931 | l->nice = val; |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 936 | /* parse the "process" bind keyword */ |
| 937 | static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 938 | { |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 939 | char *slash; |
| 940 | unsigned long proc = 0, thread = 0; |
| 941 | int i; |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 942 | |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 943 | if ((slash = strchr(args[cur_arg + 1], '/')) != NULL) |
| 944 | *slash = 0; |
| 945 | |
| 946 | if (parse_process_number(args[cur_arg + 1], &proc, NULL, err)) { |
Christopher Faulet | f1f0c5f | 2017-11-22 12:06:43 +0100 | [diff] [blame] | 947 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 948 | return ERR_ALERT | ERR_FATAL; |
| 949 | } |
| 950 | |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 951 | if (slash) { |
| 952 | if (parse_process_number(slash+1, &thread, NULL, err)) { |
| 953 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
| 954 | return ERR_ALERT | ERR_FATAL; |
| 955 | } |
| 956 | *slash = '/'; |
| 957 | } |
| 958 | |
| 959 | conf->bind_proc |= proc; |
| 960 | if (thread) { |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 961 | for (i = 0; i < MAX_THREADS; i++) |
Christopher Faulet | c644fa9 | 2017-11-23 22:44:11 +0100 | [diff] [blame] | 962 | if (!proc || (proc & (1UL << i))) |
| 963 | conf->bind_thread[i] |= thread; |
| 964 | } |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 965 | return 0; |
| 966 | } |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 967 | |
Christopher Faulet | a717b99 | 2018-04-10 14:43:00 +0200 | [diff] [blame] | 968 | /* parse the "proto" bind keyword */ |
| 969 | static int bind_parse_proto(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 970 | { |
| 971 | struct ist proto; |
| 972 | |
| 973 | if (!*args[cur_arg + 1]) { |
| 974 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
| 975 | return ERR_ALERT | ERR_FATAL; |
| 976 | } |
| 977 | |
| 978 | proto = ist2(args[cur_arg + 1], strlen(args[cur_arg + 1])); |
| 979 | conf->mux_proto = get_mux_proto(proto); |
| 980 | if (!conf->mux_proto) { |
| 981 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[cur_arg], args[cur_arg+1]); |
| 982 | return ERR_ALERT | ERR_FATAL; |
| 983 | } |
| 984 | else if (!(conf->mux_proto->side & PROTO_SIDE_FE)) { |
| 985 | memprintf(err, "'%s' : MUX protocol '%s' cannot be used for incoming connections", |
| 986 | args[cur_arg], args[cur_arg+1]); |
| 987 | return ERR_ALERT | ERR_FATAL; |
| 988 | } |
| 989 | return 0; |
| 990 | } |
| 991 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 992 | /* Note: must not be declared <const> as its list will be overwritten. |
| 993 | * Please take care of keeping this list alphabetically sorted. |
| 994 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 995 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 996 | { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, }, |
| 997 | { "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] | 998 | { /* END */ }, |
| 999 | }}; |
| 1000 | |
| 1001 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1002 | * Please take care of keeping this list alphabetically sorted. |
| 1003 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1004 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 1005 | { /* END */ }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1006 | }}; |
| 1007 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1008 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1009 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1010 | * all code contributors. |
| 1011 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1012 | * the config parser can report an appropriate error when a known keyword was |
| 1013 | * not enabled. |
| 1014 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1015 | static struct bind_kw_list bind_kws = { "ALL", { }, { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1016 | { "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] | 1017 | { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */ |
| 1018 | { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */ |
| 1019 | { "id", bind_parse_id, 1 }, /* set id of listening socket */ |
| 1020 | { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */ |
| 1021 | { "name", bind_parse_name, 1 }, /* set name of listening socket */ |
| 1022 | { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */ |
Willy Tarreau | 6ae1ba6 | 2014-05-07 19:01:58 +0200 | [diff] [blame] | 1023 | { "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] | 1024 | { "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] | 1025 | { /* END */ }, |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1026 | }}; |
| 1027 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1028 | __attribute__((constructor)) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 1029 | static void __listener_init(void) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1030 | { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 1031 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1032 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 1033 | bind_register_keywords(&bind_kws); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1034 | HA_SPIN_INIT(&lq_lock); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Local variables: |
| 1039 | * c-indent-level: 8 |
| 1040 | * c-basic-offset: 8 |
| 1041 | * End: |
| 1042 | */ |