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