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