blob: ca7dc9c7cfd42e0b6dd787f95927824dc5bdd6d5 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * 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 Tarreaudd815982007-10-16 12:25:14 +020021
Willy Tarreaud1d54542012-09-12 22:58:11 +020022#ifndef _PROTO_LISTENER_H
23#define _PROTO_LISTENER_H
Willy Tarreaudd815982007-10-16 12:25:14 +020024
Thierry FOURNIERd3879e82013-06-10 15:09:19 +020025#include <string.h>
26
Willy Tarreaud1d54542012-09-12 22:58:11 +020027#include <types/listener.h>
Andjelko Iharosc4df59e2017-07-20 11:59:48 +020028#include <types/cli.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020029
Willy Tarreaube58c382011-07-24 18:28:10 +020030/* 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 */
37int 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 */
43int resume_listener(struct listener *l);
44
Willy Tarreaudabf2e22007-10-28 21:59:24 +010045/* 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 */
51int 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 */
58int disable_all_listeners(struct protocol *proto);
59
Willy Tarreau241797a2019-12-10 14:10:52 +010060/* Dequeues all listeners waiting for a resource the global wait queue */
61void dequeue_all_listeners();
62
63/* Dequeues all listeners waiting for a resource in proxy <px>'s queue */
64void dequeue_proxy_listeners(struct proxy *px);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020065
Christopher Faulet510c0d62018-03-16 10:04:47 +010066/* Must be called with the lock held. Depending on <do_close> value, it does
67 * what unbind_listener or unbind_listener_no_close should do.
68 */
69void do_unbind_listener(struct listener *listener, int do_close);
70
Willy Tarreaub648d632007-10-28 22:13:50 +010071/* This function closes the listening socket for the specified listener,
72 * provided that it's already in a listening state. The listener enters the
Willy Tarreaubbd09b92017-11-05 11:38:44 +010073 * LI_ASSIGNED state. This function is intended to be used as a generic
74 * function for standard protocols.
Willy Tarreaub648d632007-10-28 22:13:50 +010075 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +010076void unbind_listener(struct listener *listener);
Willy Tarreaub648d632007-10-28 22:13:50 +010077
Olivier Houchard1fc05162017-04-06 01:05:05 +020078/* This function pretends the listener is dead, but keeps the FD opened, so
79 * that we can provide it, for conf reloading.
80 */
Willy Tarreaubbd09b92017-11-05 11:38:44 +010081void unbind_listener_no_close(struct listener *listener);
Olivier Houchard1fc05162017-04-06 01:05:05 +020082
Willy Tarreau3acf8c32007-10-28 22:35:41 +010083/* This function closes all listening sockets bound to the protocol <proto>,
84 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
85 * detach them from the protocol. It always returns ERR_NONE.
86 */
87int unbind_all_listeners(struct protocol *proto);
88
Willy Tarreau0de59fd2017-09-15 08:10:44 +020089
90/* creates one or multiple listeners for bind_conf <bc> on sockaddr <ss> on port
91 * range <portl> to <porth>, and possibly attached to fd <fd> (or -1 for auto
92 * allocation). The address family is taken from ss->ss_family. The number of
93 * jobs and listeners is automatically increased by the number of listeners
William Lallemand75ea0a02017-11-15 19:02:58 +010094 * created. If the <inherited> argument is set to 1, it specifies that the FD
95 * was obtained from a parent process.
96 * It returns non-zero on success, zero on error with the error message
Willy Tarreau0de59fd2017-09-15 08:10:44 +020097 * set in <err>.
98 */
99int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
William Lallemand75ea0a02017-11-15 19:02:58 +0100100 int portl, int porth, int fd, int inherited, char **err);
Willy Tarreau0de59fd2017-09-15 08:10:44 +0200101
Willy Tarreau1a64d162007-10-28 22:26:05 +0100102/* Delete a listener from its protocol's list of listeners. The listener's
103 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
104 * number of listeners is updated. Note that the listener must have previously
105 * been unbound. This is the generic function to use to remove a listener.
106 */
107void delete_listener(struct listener *listener);
108
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200109/* This function is called on a read event from a listening socket, corresponding
110 * to an accept. It tries to accept as many connections as possible, and for each
111 * calls the listener's accept handler (generally the frontend's accept handler).
112 */
Willy Tarreau7a798e52016-04-14 11:13:20 +0200113void listener_accept(int fd);
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200114
Willy Tarreaue2711c72019-02-27 15:39:41 +0100115/* Returns a suitable value for a listener's backlog. It uses the listener's,
116 * otherwise the frontend's backlog, otherwise the listener's maxconn,
117 * otherwise the frontend's maxconn, otherwise 1024.
118 */
119int listener_backlog(const struct listener *l);
120
Willy Tarreau05f50472017-09-15 09:19:58 +0200121/* Notify the listener that a connection initiated from it was released. This
122 * is used to keep the connection count consistent and to possibly re-open
123 * listening when it was limited.
124 */
125void listener_release(struct listener *l);
126
Willy Tarreau26982662012-09-12 23:17:10 +0200127/*
128 * Registers the bind keyword list <kwl> as a list of valid keywords for next
129 * parsing sessions.
130 */
131void bind_register_keywords(struct bind_kw_list *kwl);
132
133/* Return a pointer to the bind keyword <kw>, or NULL if not found. */
134struct bind_kw *bind_find_kw(const char *kw);
135
Willy Tarreau8638f482012-09-18 18:01:17 +0200136/* Dumps all registered "bind" keywords to the <out> string pointer. */
137void bind_dump_kws(char **out);
138
Willy Tarreaub2b50a72019-02-03 11:14:25 +0100139void bind_recount_thread_bits(struct bind_conf *conf);
140unsigned int bind_map_thread_id(const struct bind_conf *conf, unsigned int r);
141
Willy Tarreauc95bad52016-12-22 00:13:31 +0100142/* allocate an bind_conf struct for a bind line, and chain it to the frontend <fe>.
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200143 * If <arg> is not NULL, it is duplicated into ->arg to store useful config
144 * information for error reporting.
145 */
Willy Tarreauc95bad52016-12-22 00:13:31 +0100146static inline struct bind_conf *bind_conf_alloc(struct proxy *fe, const char *file,
Willy Tarreau71a8c7c2016-12-21 22:04:54 +0100147 int line, const char *arg, struct xprt_ops *xprt)
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200148{
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200149 struct bind_conf *bind_conf = (void *)calloc(1, sizeof(struct bind_conf));
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200150
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200151 bind_conf->file = strdup(file);
152 bind_conf->line = line;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100153 LIST_ADDQ(&fe->conf.bind, &bind_conf->by_fe);
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200154 if (arg)
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200155 bind_conf->arg = strdup(arg);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200156
157 bind_conf->ux.uid = -1;
158 bind_conf->ux.gid = -1;
159 bind_conf->ux.mode = 0;
Willy Tarreau71a8c7c2016-12-21 22:04:54 +0100160 bind_conf->xprt = xprt;
Willy Tarreauc95bad52016-12-22 00:13:31 +0100161 bind_conf->frontend = fe;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200162 bind_conf->severity_output = CLI_SEVERITY_NONE;
William Lallemande0c51ae2019-10-14 11:12:35 +0200163#ifdef USE_OPENSSL
William Lallemand150bfa82019-09-19 17:12:49 +0200164 HA_RWLOCK_INIT(&bind_conf->sni_lock);
William Lallemand222a7c62019-10-04 11:44:57 +0200165 bind_conf->sni_ctx = EB_ROOT;
166 bind_conf->sni_w_ctx = EB_ROOT;
William Lallemande0c51ae2019-10-14 11:12:35 +0200167#endif
Willy Tarreau4348fad2012-09-20 16:48:07 +0200168 LIST_INIT(&bind_conf->listeners);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200169 return bind_conf;
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200170}
171
Willy Tarreau6d0d3f62017-07-25 19:46:06 +0200172static inline const char *listener_state_str(const struct listener *l)
173{
174 static const char *states[9] = {
175 "NEW", "INI", "ASS", "PAU", "ZOM", "LIS", "RDY", "FUL", "LIM",
176 };
177 unsigned int st = l->state;
178
Willy Tarreaufec56c62019-12-11 15:51:37 +0100179 if (st >= sizeof(states) / sizeof(*states))
Willy Tarreau6d0d3f62017-07-25 19:46:06 +0200180 return "INVALID";
181 return states[st];
182}
183
Olivier Houchardf73629d2017-04-05 22:33:04 +0200184extern struct xfer_sock_list *xfer_sock_list;
Willy Tarreau1efafce2019-01-27 15:37:19 +0100185
186extern struct accept_queue_ring accept_queue_rings[MAX_THREADS] __attribute__((aligned(64)));
187
Willy Tarreaud1d54542012-09-12 22:58:11 +0200188#endif /* _PROTO_LISTENER_H */
Willy Tarreaudd815982007-10-16 12:25:14 +0200189
190/*
191 * Local variables:
192 * c-indent-level: 8
193 * c-basic-offset: 8
194 * End:
195 */