blob: c4b57c5efa1a4d0edaa9a1849d40b35f12525351 [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
Willy Tarreaud1d54542012-09-12 22:58:11 +0200117/* binds all listeners of all registered protocols. Returns a composition
118 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
119 */
Willy Tarreaue91bff22020-09-02 11:11:43 +0200120int protocol_bind_all(int verbose)
Willy Tarreaud1d54542012-09-12 22:58:11 +0200121{
122 struct protocol *proto;
Willy Tarreau94320852020-09-01 18:48:35 +0200123 struct listener *listener;
Willy Tarreaufc974882020-09-02 18:22:11 +0200124 struct receiver *receiver;
Bjoern Jackeed174852021-01-12 19:24:43 +0100125 char msg[1000];
Willy Tarreaufc974882020-09-02 18:22:11 +0200126 char *errmsg;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200127 int err, lerr;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200128
129 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200130 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200131 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200132 list_for_each_entry(receiver, &proto->receivers, proto_list) {
Frédéric Lécaille12a03172023-01-12 15:23:54 +0100133#ifdef USE_QUIC
134 if ((global.tune.options & GTUNE_NO_QUIC) &&
135 (proto == &proto_quic4 || proto == &proto_quic6))
136 continue;
137#endif
Willy Tarreaufc974882020-09-02 18:22:11 +0200138 listener = LIST_ELEM(receiver, struct listener *, rx);
139
Willy Tarreau233ad282020-10-15 21:45:15 +0200140 lerr = proto->fam->bind(receiver, &errmsg);
Willy Tarreaufc974882020-09-02 18:22:11 +0200141 err |= lerr;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200142
143 /* errors are reported if <verbose> is set or if they are fatal */
144 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
145 struct proxy *px = listener->bind_conf->frontend;
146
147 if (lerr & ERR_ALERT)
Willy Tarreau37de5532021-10-14 11:55:48 +0200148 ha_alert("Binding [%s:%d] for %s %s: %s\n",
149 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200150 proxy_type_str(px), px->id, errmsg);
Willy Tarreaue91bff22020-09-02 11:11:43 +0200151 else if (lerr & ERR_WARN)
Willy Tarreau37de5532021-10-14 11:55:48 +0200152 ha_warning("Binding [%s:%d] for %s %s: %s\n",
153 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200154 proxy_type_str(px), px->id, errmsg);
Willy Tarreaue91bff22020-09-02 11:11:43 +0200155 }
Aurelien DARRAGON84296272023-02-07 15:51:58 +0100156 if (lerr != ERR_NONE)
157 ha_free(&errmsg);
158
Willy Tarreaufc974882020-09-02 18:22:11 +0200159 if (lerr & ERR_ABORT)
160 break;
Willy Tarreaue91bff22020-09-02 11:11:43 +0200161
Willy Tarreaufc974882020-09-02 18:22:11 +0200162 if (lerr & ~ERR_WARN)
163 continue;
164
165 /* for now there's still always a listening function */
166 BUG_ON(!proto->listen);
167 lerr = proto->listen(listener, msg, sizeof(msg));
Willy Tarreaue91bff22020-09-02 11:11:43 +0200168 err |= lerr;
Willy Tarreaufc974882020-09-02 18:22:11 +0200169
170 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
171 struct proxy *px = listener->bind_conf->frontend;
172
173 if (lerr & ERR_ALERT)
Willy Tarreau37de5532021-10-14 11:55:48 +0200174 ha_alert("Starting [%s:%d] for %s %s: %s\n",
175 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200176 proxy_type_str(px), px->id, msg);
177 else if (lerr & ERR_WARN)
Willy Tarreau37de5532021-10-14 11:55:48 +0200178 ha_warning("Starting [%s:%d] for %s %s: %s\n",
179 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200180 proxy_type_str(px), px->id, msg);
181 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200182 if (lerr & ERR_ABORT)
Willy Tarreaud1d54542012-09-12 22:58:11 +0200183 break;
184 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200185 if (err & ERR_ABORT)
186 break;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200187 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200188 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200189 return err;
190}
191
192/* unbinds all listeners of all registered protocols. They are also closed.
193 * This must be performed before calling exit() in order to get a chance to
194 * remove file-system based sockets and pipes.
195 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
196 */
197int protocol_unbind_all(void)
198{
199 struct protocol *proto;
Willy Tarreauca212622020-09-02 10:31:31 +0200200 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200201 int err;
202
203 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200204 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200205 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200206 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreauca212622020-09-02 10:31:31 +0200207 unbind_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200208 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200209 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200210 return err;
211}
212
Willy Tarreaueb778242021-06-11 16:27:10 +0200213/* stops all listeners of all registered protocols. This will normally catch
214 * every single listener, all protocols included. This is to be used during
215 * soft_stop() only. It does not return any error.
Willy Tarreau02e85572020-10-07 16:50:49 +0200216 */
217void protocol_stop_now(void)
218{
219 struct protocol *proto;
220 struct listener *listener, *lback;
221
222 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
223 list_for_each_entry(proto, &protocols, list) {
224 list_for_each_entry_safe(listener, lback, &proto->receivers, rx.proto_list)
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100225 stop_listener(listener, 0, 1, 0);
Willy Tarreau02e85572020-10-07 16:50:49 +0200226 }
227 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
228}
229
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100230/* suspends all listeners of all registered protocols. This is typically
Willy Tarreau09819d12020-09-24 16:26:50 +0200231 * used on SIG_TTOU to release all listening sockets for the time needed to
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100232 * try to bind a new process. The listeners enter LI_PAUSED or LI_ASSIGNED.
233 * It returns ERR_NONE, with ERR_FATAL on failure.
Willy Tarreau09819d12020-09-24 16:26:50 +0200234 */
235int protocol_pause_all(void)
236{
237 struct protocol *proto;
238 struct listener *listener;
239 int err;
240
241 err = 0;
242 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
243 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200244 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +0100245 if (!suspend_listener(listener, 0, 0))
Willy Tarreau09819d12020-09-24 16:26:50 +0200246 err |= ERR_FATAL;
247 }
248 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
249 return err;
250}
251
252/* resumes all listeners of all registered protocols. This is typically used on
253 * SIG_TTIN to re-enable listening sockets after a new process failed to bind.
254 * The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL
255 * on failure.
256 */
257int protocol_resume_all(void)
258{
259 struct protocol *proto;
260 struct listener *listener;
261 int err;
262
263 err = 0;
264 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
265 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200266 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Aurelien DARRAGON4059e092023-02-06 17:06:03 +0100267 if (!resume_listener(listener, 0, 0))
Willy Tarreau09819d12020-09-24 16:26:50 +0200268 err |= ERR_FATAL;
269 }
270 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
271 return err;
272}
273
Willy Tarreaud1d54542012-09-12 22:58:11 +0200274/* enables all listeners of all registered protocols. This is intended to be
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200275 * used after a fork() to enable reading on all file descriptors. Returns
276 * composition of ERR_NONE.
Willy Tarreaud1d54542012-09-12 22:58:11 +0200277 */
278int protocol_enable_all(void)
279{
280 struct protocol *proto;
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200281 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200282
Willy Tarreaudaacf362019-07-24 16:45:02 +0200283 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200284 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200285 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200286 enable_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200287 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200288 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200289 return ERR_NONE;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200290}
291
Willy Tarreaud1d54542012-09-12 22:58:11 +0200292/*
293 * Local variables:
294 * c-indent-level: 8
295 * c-basic-offset: 8
296 * End:
297 */