blob: 25ed6b757e7397b089310ad80cd7ee79a38b123d [file] [log] [blame]
Willy Tarreaud1d54542012-09-12 22:58:11 +02001/*
2 * Protocol registration functions.
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
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 Tarreaua6e3be72016-08-10 18:24:48 +020013#include <sys/types.h>
Willy Tarreaub550d002015-02-20 16:53:25 +010014#include <sys/socket.h>
15
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/errors.h>
Frédéric Lécaille12a03172023-01-12 15:23:54 +010018#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020019#include <haproxy/list.h>
Willy Tarreau94320852020-09-01 18:48:35 +020020#include <haproxy/listener.h>
Frédéric Lécaille12a03172023-01-12 15:23:54 +010021#include <haproxy/proto_quic.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/protocol.h>
Willy Tarreaue91bff22020-09-02 11:11:43 +020023#include <haproxy/proxy.h>
Willy Tarreauf1003ea2023-04-22 18:26:56 +020024#include <haproxy/sock.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020025#include <haproxy/tools.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020026
Willy Tarreaud1d54542012-09-12 22:58:11 +020027
28/* List head of all registered protocols */
29static struct list protocols = LIST_HEAD_INIT(protocols);
Willy Tarreaue3b45182021-10-27 17:28:55 +020030struct protocol *__protocol_by_family[AF_CUST_MAX][PROTO_NUM_TYPES][2] __read_mostly = { };
Willy Tarreaud1d54542012-09-12 22:58:11 +020031
Willy Tarreaudaacf362019-07-24 16:45:02 +020032/* This is the global spinlock we may need to register/unregister listeners or
33 * protocols. Its main purpose is in fact to serialize the rare stop/deinit()
34 * phases.
35 */
36__decl_spinlock(proto_lock);
37
Willy Tarreaud1d54542012-09-12 22:58:11 +020038/* Registers the protocol <proto> */
39void protocol_register(struct protocol *proto)
40{
Willy Tarreaubdcee7f2021-10-27 15:06:35 +020041 int sock_domain = proto->fam->sock_domain;
42
43 BUG_ON(sock_domain < 0 || sock_domain >= AF_CUST_MAX);
Willy Tarreaue3b45182021-10-27 17:28:55 +020044 BUG_ON(proto->proto_type >= PROTO_NUM_TYPES);
Willy Tarreaubdcee7f2021-10-27 15:06:35 +020045
Willy Tarreaudaacf362019-07-24 16:45:02 +020046 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau2b718102021-04-21 07:32:39 +020047 LIST_APPEND(&protocols, &proto->list);
Willy Tarreaubdcee7f2021-10-27 15:06:35 +020048 __protocol_by_family[sock_domain]
Willy Tarreaue3b45182021-10-27 17:28:55 +020049 [proto->proto_type]
Willy Tarreau91b47262022-05-20 16:36:46 +020050 [proto->xprt_type == PROTO_TYPE_DGRAM] = proto;
Willy Tarreaudaacf362019-07-24 16:45:02 +020051 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020052}
53
54/* Unregisters the protocol <proto>. Note that all listeners must have
55 * previously been unbound.
56 */
57void protocol_unregister(struct protocol *proto)
58{
Willy Tarreaudaacf362019-07-24 16:45:02 +020059 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau2b718102021-04-21 07:32:39 +020060 LIST_DELETE(&proto->list);
Willy Tarreaud1d54542012-09-12 22:58:11 +020061 LIST_INIT(&proto->list);
Willy Tarreaudaacf362019-07-24 16:45:02 +020062 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020063}
64
Willy Tarreau65df7e02023-04-22 15:02:35 +020065/* clears flag <flag> on all protocols. */
66void protocol_clrf_all(uint flag)
67{
68 struct protocol *proto;
69
70 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
71 list_for_each_entry(proto, &protocols, list)
72 proto->flags &= ~flag;
73 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
74}
75
76/* sets flag <flag> on all protocols. */
77void protocol_setf_all(uint flag)
78{
79 struct protocol *proto;
80
81 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
82 list_for_each_entry(proto, &protocols, list)
83 proto->flags |= flag;
84 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
85}
86
Willy Tarreau8a5e6f42023-04-22 17:39:30 +020087/* Checks if protocol <proto> supports PROTO_F flag <flag>. Returns zero if not,
88 * non-zero if supported. It may return a cached value from a previous test,
89 * and may run live tests then update the proto's flags to cache a result. It's
90 * better to call it only if needed so that it doesn't result in modules being
91 * loaded in case of a live test. It is only supposed to be used during boot.
92 */
93int protocol_supports_flag(struct protocol *proto, uint flag)
94{
95 if (flag == PROTO_F_REUSEPORT_SUPPORTED) {
Willy Tarreauf1003ea2023-04-22 18:26:56 +020096 int ret = 0;
97
Willy Tarreau8a5e6f42023-04-22 17:39:30 +020098 /* check if the protocol supports SO_REUSEPORT */
99 if (!(_HA_ATOMIC_LOAD(&proto->flags) & PROTO_F_REUSEPORT_SUPPORTED))
100 return 0;
101
Willy Tarreauf1003ea2023-04-22 18:26:56 +0200102 /* at least nobody said it was not supported */
103 if (_HA_ATOMIC_LOAD(&proto->flags) & PROTO_F_REUSEPORT_TESTED)
104 return 1;
105
106 /* run a live check */
107 ret = _sock_supports_reuseport(proto->fam, proto->sock_type, proto->sock_prot);
108 if (!ret)
109 _HA_ATOMIC_AND(&proto->flags, ~PROTO_F_REUSEPORT_SUPPORTED);
110
111 _HA_ATOMIC_OR(&proto->flags, PROTO_F_REUSEPORT_TESTED);
112 return ret;
Willy Tarreau8a5e6f42023-04-22 17:39:30 +0200113 }
114 return 0;
115}
116
Frédéric Lécailledf3f4572023-07-21 18:22:38 +0200117#ifdef USE_QUIC
118/* Return 1 if QUIC protocol may be bound, 0 if no, depending on the tuning
119 * parameters.
120 */
Frédéric Lécailleee7b2bb2023-08-08 11:41:13 +0200121static inline int protocol_may_bind_quic(struct listener *l)
Frédéric Lécailledf3f4572023-07-21 18:22:38 +0200122{
123 if (global.tune.options & GTUNE_NO_QUIC)
124 return 0;
Frédéric Lécailledf3f4572023-07-21 18:22:38 +0200125 return 1;
126}
127#endif
128
Willy Tarreaud1d54542012-09-12 22:58:11 +0200129/* binds all listeners of all registered protocols. Returns a composition
130 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
131 */
Willy Tarreaue91bff22020-09-02 11:11:43 +0200132int protocol_bind_all(int verbose)
Willy Tarreaud1d54542012-09-12 22:58:11 +0200133{
134 struct protocol *proto;
Willy Tarreau94320852020-09-01 18:48:35 +0200135 struct listener *listener;
Willy Tarreaufc974882020-09-02 18:22:11 +0200136 struct receiver *receiver;
Bjoern Jackeed174852021-01-12 19:24:43 +0100137 char msg[1000];
Willy Tarreaufc974882020-09-02 18:22:11 +0200138 char *errmsg;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200139 int err, lerr;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200140
141 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200142 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200143 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200144 list_for_each_entry(receiver, &proto->receivers, proto_list) {
Frédéric Lécailleee7b2bb2023-08-08 11:41:13 +0200145 listener = LIST_ELEM(receiver, struct listener *, rx);
Frédéric Lécaille12a03172023-01-12 15:23:54 +0100146#ifdef USE_QUIC
Frédéric Lécailledf3f4572023-07-21 18:22:38 +0200147 if ((proto == &proto_quic4 || proto == &proto_quic6) &&
Frédéric Lécailleee7b2bb2023-08-08 11:41:13 +0200148 !protocol_may_bind_quic(listener))
Frédéric Lécaille12a03172023-01-12 15:23:54 +0100149 continue;
150#endif
Willy Tarreaufc974882020-09-02 18:22:11 +0200151
Willy Tarreau233ad282020-10-15 21:45:15 +0200152 lerr = proto->fam->bind(receiver, &errmsg);
Willy Tarreaufc974882020-09-02 18:22:11 +0200153 err |= lerr;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200154
155 /* errors are reported if <verbose> is set or if they are fatal */
156 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
157 struct proxy *px = listener->bind_conf->frontend;
158
159 if (lerr & ERR_ALERT)
Willy Tarreau37de5532021-10-14 11:55:48 +0200160 ha_alert("Binding [%s:%d] for %s %s: %s\n",
161 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200162 proxy_type_str(px), px->id, errmsg);
Willy Tarreaue91bff22020-09-02 11:11:43 +0200163 else if (lerr & ERR_WARN)
Willy Tarreau37de5532021-10-14 11:55:48 +0200164 ha_warning("Binding [%s:%d] for %s %s: %s\n",
165 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200166 proxy_type_str(px), px->id, errmsg);
Willy Tarreaue91bff22020-09-02 11:11:43 +0200167 }
Aurelien DARRAGON84296272023-02-07 15:51:58 +0100168 if (lerr != ERR_NONE)
169 ha_free(&errmsg);
170
Willy Tarreaufc974882020-09-02 18:22:11 +0200171 if (lerr & ERR_ABORT)
172 break;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200173
Willy Tarreaufc974882020-09-02 18:22:11 +0200174 if (lerr & ~ERR_WARN)
175 continue;
176
177 /* for now there's still always a listening function */
178 BUG_ON(!proto->listen);
179 lerr = proto->listen(listener, msg, sizeof(msg));
Willy Tarreaue91bff22020-09-02 11:11:43 +0200180 err |= lerr;
Willy Tarreaufc974882020-09-02 18:22:11 +0200181
182 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
183 struct proxy *px = listener->bind_conf->frontend;
184
185 if (lerr & ERR_ALERT)
Willy Tarreau37de5532021-10-14 11:55:48 +0200186 ha_alert("Starting [%s:%d] for %s %s: %s\n",
187 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200188 proxy_type_str(px), px->id, msg);
189 else if (lerr & ERR_WARN)
Willy Tarreau37de5532021-10-14 11:55:48 +0200190 ha_warning("Starting [%s:%d] for %s %s: %s\n",
191 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200192 proxy_type_str(px), px->id, msg);
193 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200194 if (lerr & ERR_ABORT)
Willy Tarreaud1d54542012-09-12 22:58:11 +0200195 break;
196 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200197 if (err & ERR_ABORT)
198 break;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200199 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200200 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200201 return err;
202}
203
204/* unbinds all listeners of all registered protocols. They are also closed.
205 * This must be performed before calling exit() in order to get a chance to
206 * remove file-system based sockets and pipes.
207 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
208 */
209int protocol_unbind_all(void)
210{
211 struct protocol *proto;
Willy Tarreauca212622020-09-02 10:31:31 +0200212 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200213 int err;
214
215 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200216 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200217 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200218 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreauca212622020-09-02 10:31:31 +0200219 unbind_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200220 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200221 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200222 return err;
223}
224
Willy Tarreaueb778242021-06-11 16:27:10 +0200225/* stops all listeners of all registered protocols. This will normally catch
226 * every single listener, all protocols included. This is to be used during
227 * soft_stop() only. It does not return any error.
Willy Tarreau02e85572020-10-07 16:50:49 +0200228 */
229void protocol_stop_now(void)
230{
231 struct protocol *proto;
232 struct listener *listener, *lback;
233
234 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
235 list_for_each_entry(proto, &protocols, list) {
236 list_for_each_entry_safe(listener, lback, &proto->receivers, rx.proto_list)
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100237 stop_listener(listener, 0, 1, 0);
Willy Tarreau02e85572020-10-07 16:50:49 +0200238 }
239 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
240}
241
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100242/* suspends all listeners of all registered protocols. This is typically
Willy Tarreau09819d12020-09-24 16:26:50 +0200243 * used on SIG_TTOU to release all listening sockets for the time needed to
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100244 * try to bind a new process. The listeners enter LI_PAUSED or LI_ASSIGNED.
245 * It returns ERR_NONE, with ERR_FATAL on failure.
Willy Tarreau09819d12020-09-24 16:26:50 +0200246 */
247int protocol_pause_all(void)
248{
249 struct protocol *proto;
250 struct listener *listener;
251 int err;
252
253 err = 0;
254 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
255 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200256 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100257 if (!suspend_listener(listener, 0, 0))
Willy Tarreau09819d12020-09-24 16:26:50 +0200258 err |= ERR_FATAL;
259 }
260 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
261 return err;
262}
263
264/* resumes all listeners of all registered protocols. This is typically used on
265 * SIG_TTIN to re-enable listening sockets after a new process failed to bind.
266 * The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL
267 * on failure.
268 */
269int protocol_resume_all(void)
270{
271 struct protocol *proto;
272 struct listener *listener;
273 int err;
274
275 err = 0;
276 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
277 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200278 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100279 if (!resume_listener(listener, 0, 0))
Willy Tarreau09819d12020-09-24 16:26:50 +0200280 err |= ERR_FATAL;
281 }
282 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
283 return err;
284}
285
Willy Tarreaud1d54542012-09-12 22:58:11 +0200286/* enables all listeners of all registered protocols. This is intended to be
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200287 * used after a fork() to enable reading on all file descriptors. Returns
288 * composition of ERR_NONE.
Willy Tarreaud1d54542012-09-12 22:58:11 +0200289 */
290int protocol_enable_all(void)
291{
292 struct protocol *proto;
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200293 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200294
Willy Tarreaudaacf362019-07-24 16:45:02 +0200295 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200296 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200297 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200298 enable_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200299 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200300 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200301 return ERR_NONE;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200302}
303
Willy Tarreaud1d54542012-09-12 22:58:11 +0200304/*
305 * Local variables:
306 * c-indent-level: 8
307 * c-basic-offset: 8
308 * End:
309 */