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