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 | * include/proto/listener.h |
| 3 | * This file declares listener management primitives. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 22 | #ifndef _PROTO_LISTENER_H |
| 23 | #define _PROTO_LISTENER_H |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 24 | |
Thierry FOURNIER | d3879e8 | 2013-06-10 15:09:19 +0200 | [diff] [blame] | 25 | #include <string.h> |
| 26 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 27 | #include <types/listener.h> |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 28 | #include <types/cli.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 30 | /* This function tries to temporarily disable a listener, depending on the OS |
| 31 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 32 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 33 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 34 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 35 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 36 | */ |
| 37 | int pause_listener(struct listener *l); |
| 38 | |
| 39 | /* This function tries to resume a temporarily disabled listener. |
| 40 | * The resulting state will either be LI_READY or LI_FULL. 0 is returned |
| 41 | * in case of failure to resume (eg: dead socket). |
| 42 | */ |
| 43 | int resume_listener(struct listener *l); |
| 44 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 45 | /* This function adds all of the protocol's listener's file descriptors to the |
| 46 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 47 | * used as a protocol's generic enable_all() primitive, for use after the |
| 48 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 49 | * their number of connections. It always returns ERR_NONE. |
| 50 | */ |
| 51 | int enable_all_listeners(struct protocol *proto); |
| 52 | |
| 53 | /* This function removes all of the protocol's listener's file descriptors from |
| 54 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 55 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 56 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 57 | */ |
| 58 | int disable_all_listeners(struct protocol *proto); |
| 59 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 60 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 61 | void dequeue_all_listeners(struct list *list); |
| 62 | |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 63 | /* Must be called with the lock held. Depending on <do_close> value, it does |
| 64 | * what unbind_listener or unbind_listener_no_close should do. |
| 65 | */ |
| 66 | void do_unbind_listener(struct listener *listener, int do_close); |
| 67 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 68 | /* This function closes the listening socket for the specified listener, |
| 69 | * 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] | 70 | * LI_ASSIGNED state. This function is intended to be used as a generic |
| 71 | * function for standard protocols. |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 72 | */ |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 73 | void unbind_listener(struct listener *listener); |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 74 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 75 | /* This function pretends the listener is dead, but keeps the FD opened, so |
| 76 | * that we can provide it, for conf reloading. |
| 77 | */ |
Willy Tarreau | bbd09b9 | 2017-11-05 11:38:44 +0100 | [diff] [blame] | 78 | void unbind_listener_no_close(struct listener *listener); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 79 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 80 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 81 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 82 | * detach them from the protocol. It always returns ERR_NONE. |
| 83 | */ |
| 84 | int unbind_all_listeners(struct protocol *proto); |
| 85 | |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 86 | |
| 87 | /* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port |
| 88 | * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto |
| 89 | * allocation). The address family is taken from ss->ss_family. The number of |
| 90 | * jobs and listeners is automatically increased by the number of listeners |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 91 | * created. If the <inherited> argument is set to 1, it specifies that the FD |
| 92 | * was obtained from a parent process. |
| 93 | * 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] | 94 | * set in <err>. |
| 95 | */ |
| 96 | int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss, |
William Lallemand | 75ea0a0 | 2017-11-15 19:02:58 +0100 | [diff] [blame] | 97 | int portl, int porth, int fd, int inherited, char **err); |
Willy Tarreau | 0de59fd | 2017-09-15 08:10:44 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 99 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 100 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 101 | * number of listeners is updated. Note that the listener must have previously |
| 102 | * been unbound. This is the generic function to use to remove a listener. |
| 103 | */ |
| 104 | void delete_listener(struct listener *listener); |
| 105 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 106 | /* This function is called on a read event from a listening socket, corresponding |
| 107 | * to an accept. It tries to accept as many connections as possible, and for each |
| 108 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 109 | */ |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 110 | void listener_accept(int fd); |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 111 | |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 112 | /* Returns a suitable value for a listener's backlog. It uses the listener's, |
| 113 | * otherwise the frontend's backlog, otherwise the listener's maxconn, |
| 114 | * otherwise the frontend's maxconn, otherwise 1024. |
| 115 | */ |
| 116 | int listener_backlog(const struct listener *l); |
| 117 | |
Willy Tarreau | 05f5047 | 2017-09-15 09:19:58 +0200 | [diff] [blame] | 118 | /* Notify the listener that a connection initiated from it was released. This |
| 119 | * is used to keep the connection count consistent and to possibly re-open |
| 120 | * listening when it was limited. |
| 121 | */ |
| 122 | void listener_release(struct listener *l); |
| 123 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 124 | /* |
| 125 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 126 | * parsing sessions. |
| 127 | */ |
| 128 | void bind_register_keywords(struct bind_kw_list *kwl); |
| 129 | |
| 130 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. */ |
| 131 | struct bind_kw *bind_find_kw(const char *kw); |
| 132 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 133 | /* Dumps all registered "bind" keywords to the <out> string pointer. */ |
| 134 | void bind_dump_kws(char **out); |
| 135 | |
Willy Tarreau | b2b50a7 | 2019-02-03 11:14:25 +0100 | [diff] [blame] | 136 | void bind_recount_thread_bits(struct bind_conf *conf); |
| 137 | unsigned int bind_map_thread_id(const struct bind_conf *conf, unsigned int r); |
| 138 | |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 139 | /* allocate an bind_conf struct for a bind line, and chain it to the frontend <fe>. |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 140 | * If <arg> is not NULL, it is duplicated into ->arg to store useful config |
| 141 | * information for error reporting. |
| 142 | */ |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 143 | static inline struct bind_conf *bind_conf_alloc(struct proxy *fe, const char *file, |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 144 | int line, const char *arg, struct xprt_ops *xprt) |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 145 | { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 146 | struct bind_conf *bind_conf = (void *)calloc(1, sizeof(struct bind_conf)); |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 147 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 148 | bind_conf->file = strdup(file); |
| 149 | bind_conf->line = line; |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 150 | LIST_ADDQ(&fe->conf.bind, &bind_conf->by_fe); |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 151 | if (arg) |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 152 | bind_conf->arg = strdup(arg); |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 153 | |
| 154 | bind_conf->ux.uid = -1; |
| 155 | bind_conf->ux.gid = -1; |
| 156 | bind_conf->ux.mode = 0; |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 157 | bind_conf->xprt = xprt; |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 158 | bind_conf->frontend = fe; |
Andjelko Iharos | c4df59e | 2017-07-20 11:59:48 +0200 | [diff] [blame] | 159 | bind_conf->severity_output = CLI_SEVERITY_NONE; |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 161 | LIST_INIT(&bind_conf->listeners); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 162 | return bind_conf; |
Willy Tarreau | f5ae8f7 | 2012-09-07 16:58:00 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Willy Tarreau | 6d0d3f6 | 2017-07-25 19:46:06 +0200 | [diff] [blame] | 165 | static inline const char *listener_state_str(const struct listener *l) |
| 166 | { |
| 167 | static const char *states[9] = { |
| 168 | "NEW", "INI", "ASS", "PAU", "ZOM", "LIS", "RDY", "FUL", "LIM", |
| 169 | }; |
| 170 | unsigned int st = l->state; |
| 171 | |
Willy Tarreau | 4a1cfa7 | 2019-12-11 15:51:37 +0100 | [diff] [blame] | 172 | if (st >= sizeof(states) / sizeof(*states)) |
Willy Tarreau | 6d0d3f6 | 2017-07-25 19:46:06 +0200 | [diff] [blame] | 173 | return "INVALID"; |
| 174 | return states[st]; |
| 175 | } |
| 176 | |
Olivier Houchard | f73629d | 2017-04-05 22:33:04 +0200 | [diff] [blame] | 177 | extern struct xfer_sock_list *xfer_sock_list; |
Willy Tarreau | 1efafce | 2019-01-27 15:37:19 +0100 | [diff] [blame] | 178 | |
| 179 | extern struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64))); |
| 180 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 181 | #endif /* _PROTO_LISTENER_H */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * Local variables: |
| 185 | * c-indent-level: 8 |
| 186 | * c-basic-offset: 8 |
| 187 | * End: |
| 188 | */ |