blob: b33e494633cc994c0ef3734a5d3aa1b72afc6911 [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>
Willy Tarreau853b2972020-05-27 18:01:47 +020018#include <haproxy/list.h>
Willy Tarreau94320852020-09-01 18:48:35 +020019#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/protocol.h>
Willy Tarreaue91bff22020-09-02 11:11:43 +020021#include <haproxy/proxy.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020022#include <haproxy/tools.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020023
Willy Tarreaud1d54542012-09-12 22:58:11 +020024
25/* List head of all registered protocols */
26static struct list protocols = LIST_HEAD_INIT(protocols);
Willy Tarreauad6722e2021-04-10 17:38:05 +020027struct protocol *__protocol_by_family[AF_CUST_MAX][2][2] __read_mostly = { };
Willy Tarreaud1d54542012-09-12 22:58:11 +020028
Willy Tarreaudaacf362019-07-24 16:45:02 +020029/* This is the global spinlock we may need to register/unregister listeners or
30 * protocols. Its main purpose is in fact to serialize the rare stop/deinit()
31 * phases.
32 */
33__decl_spinlock(proto_lock);
34
Willy Tarreaud1d54542012-09-12 22:58:11 +020035/* Registers the protocol <proto> */
36void protocol_register(struct protocol *proto)
37{
Willy Tarreaudaacf362019-07-24 16:45:02 +020038 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau2b718102021-04-21 07:32:39 +020039 LIST_APPEND(&protocols, &proto->list);
Willy Tarreauf9ad06c2020-12-08 12:13:54 +010040 if (proto->fam->sock_domain >= 0 && proto->fam->sock_domain < AF_CUST_MAX)
41 __protocol_by_family[proto->fam->sock_domain]
Willy Tarreau910c64d2020-09-16 21:28:15 +020042 [proto->sock_type == SOCK_DGRAM]
43 [proto->ctrl_type == SOCK_DGRAM] = proto;
Willy Tarreaudaacf362019-07-24 16:45:02 +020044 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020045}
46
47/* Unregisters the protocol <proto>. Note that all listeners must have
48 * previously been unbound.
49 */
50void protocol_unregister(struct protocol *proto)
51{
Willy Tarreaudaacf362019-07-24 16:45:02 +020052 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau2b718102021-04-21 07:32:39 +020053 LIST_DELETE(&proto->list);
Willy Tarreaud1d54542012-09-12 22:58:11 +020054 LIST_INIT(&proto->list);
Willy Tarreaudaacf362019-07-24 16:45:02 +020055 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020056}
57
58/* binds all listeners of all registered protocols. Returns a composition
59 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
60 */
Willy Tarreaue91bff22020-09-02 11:11:43 +020061int protocol_bind_all(int verbose)
Willy Tarreaud1d54542012-09-12 22:58:11 +020062{
63 struct protocol *proto;
Willy Tarreau94320852020-09-01 18:48:35 +020064 struct listener *listener;
Willy Tarreaufc974882020-09-02 18:22:11 +020065 struct receiver *receiver;
Bjoern Jackeed174852021-01-12 19:24:43 +010066 char msg[1000];
Willy Tarreaufc974882020-09-02 18:22:11 +020067 char *errmsg;
Willy Tarreaue91bff22020-09-02 11:11:43 +020068 int err, lerr;
Willy Tarreaud1d54542012-09-12 22:58:11 +020069
70 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +020071 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020072 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +020073 list_for_each_entry(receiver, &proto->receivers, proto_list) {
Willy Tarreaufc974882020-09-02 18:22:11 +020074 listener = LIST_ELEM(receiver, struct listener *, rx);
75
Willy Tarreau233ad282020-10-15 21:45:15 +020076 lerr = proto->fam->bind(receiver, &errmsg);
Willy Tarreaufc974882020-09-02 18:22:11 +020077 err |= lerr;
Willy Tarreaue91bff22020-09-02 11:11:43 +020078
79 /* errors are reported if <verbose> is set or if they are fatal */
80 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
81 struct proxy *px = listener->bind_conf->frontend;
82
83 if (lerr & ERR_ALERT)
Willy Tarreau37de5532021-10-14 11:55:48 +020084 ha_alert("Binding [%s:%d] for %s %s: %s\n",
85 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +020086 proxy_type_str(px), px->id, errmsg);
Willy Tarreaue91bff22020-09-02 11:11:43 +020087 else if (lerr & ERR_WARN)
Willy Tarreau37de5532021-10-14 11:55:48 +020088 ha_warning("Binding [%s:%d] for %s %s: %s\n",
89 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +020090 proxy_type_str(px), px->id, errmsg);
Willy Tarreau61cfdf42021-02-20 10:46:51 +010091 ha_free(&errmsg);
Willy Tarreaue91bff22020-09-02 11:11:43 +020092 }
Willy Tarreaufc974882020-09-02 18:22:11 +020093 if (lerr & ERR_ABORT)
94 break;
Willy Tarreaue91bff22020-09-02 11:11:43 +020095
Willy Tarreaufc974882020-09-02 18:22:11 +020096 if (lerr & ~ERR_WARN)
97 continue;
98
99 /* for now there's still always a listening function */
100 BUG_ON(!proto->listen);
101 lerr = proto->listen(listener, msg, sizeof(msg));
Willy Tarreaue91bff22020-09-02 11:11:43 +0200102 err |= lerr;
Willy Tarreaufc974882020-09-02 18:22:11 +0200103
104 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
105 struct proxy *px = listener->bind_conf->frontend;
106
107 if (lerr & ERR_ALERT)
Willy Tarreau37de5532021-10-14 11:55:48 +0200108 ha_alert("Starting [%s:%d] for %s %s: %s\n",
109 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200110 proxy_type_str(px), px->id, msg);
111 else if (lerr & ERR_WARN)
Willy Tarreau37de5532021-10-14 11:55:48 +0200112 ha_warning("Starting [%s:%d] for %s %s: %s\n",
113 listener->bind_conf->file, listener->bind_conf->line,
Willy Tarreaufc974882020-09-02 18:22:11 +0200114 proxy_type_str(px), px->id, msg);
115 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200116 if (lerr & ERR_ABORT)
Willy Tarreaud1d54542012-09-12 22:58:11 +0200117 break;
118 }
Willy Tarreaue91bff22020-09-02 11:11:43 +0200119 if (err & ERR_ABORT)
120 break;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200121 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200122 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200123 return err;
124}
125
126/* unbinds all listeners of all registered protocols. They are also closed.
127 * This must be performed before calling exit() in order to get a chance to
128 * remove file-system based sockets and pipes.
129 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
130 */
131int protocol_unbind_all(void)
132{
133 struct protocol *proto;
Willy Tarreauca212622020-09-02 10:31:31 +0200134 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200135 int err;
136
137 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200138 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200139 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200140 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreauca212622020-09-02 10:31:31 +0200141 unbind_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200142 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200143 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200144 return err;
145}
146
Willy Tarreaueb778242021-06-11 16:27:10 +0200147/* stops all listeners of all registered protocols. This will normally catch
148 * every single listener, all protocols included. This is to be used during
149 * soft_stop() only. It does not return any error.
Willy Tarreau02e85572020-10-07 16:50:49 +0200150 */
151void protocol_stop_now(void)
152{
153 struct protocol *proto;
154 struct listener *listener, *lback;
155
156 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
157 list_for_each_entry(proto, &protocols, list) {
158 list_for_each_entry_safe(listener, lback, &proto->receivers, rx.proto_list)
Willy Tarreaueb778242021-06-11 16:27:10 +0200159 stop_listener(listener, 0, 1, 0);
Willy Tarreau02e85572020-10-07 16:50:49 +0200160 }
161 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
162}
163
Willy Tarreau09819d12020-09-24 16:26:50 +0200164/* pauses all listeners of all registered protocols. This is typically
165 * used on SIG_TTOU to release all listening sockets for the time needed to
166 * try to bind a new process. The listeners enter LI_PAUSED. It returns
167 * ERR_NONE, with ERR_FATAL on failure.
168 */
169int protocol_pause_all(void)
170{
171 struct protocol *proto;
172 struct listener *listener;
173 int err;
174
175 err = 0;
176 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
177 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200178 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau09819d12020-09-24 16:26:50 +0200179 if (!pause_listener(listener))
180 err |= ERR_FATAL;
181 }
182 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
183 return err;
184}
185
186/* resumes all listeners of all registered protocols. This is typically used on
187 * SIG_TTIN to re-enable listening sockets after a new process failed to bind.
188 * The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL
189 * on failure.
190 */
191int protocol_resume_all(void)
192{
193 struct protocol *proto;
194 struct listener *listener;
195 int err;
196
197 err = 0;
198 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
199 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200200 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau09819d12020-09-24 16:26:50 +0200201 if (!resume_listener(listener))
202 err |= ERR_FATAL;
203 }
204 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
205 return err;
206}
207
Willy Tarreaud1d54542012-09-12 22:58:11 +0200208/* enables all listeners of all registered protocols. This is intended to be
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200209 * used after a fork() to enable reading on all file descriptors. Returns
210 * composition of ERR_NONE.
Willy Tarreaud1d54542012-09-12 22:58:11 +0200211 */
212int protocol_enable_all(void)
213{
214 struct protocol *proto;
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200215 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200216
Willy Tarreaudaacf362019-07-24 16:45:02 +0200217 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200218 list_for_each_entry(proto, &protocols, list) {
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200219 list_for_each_entry(listener, &proto->receivers, rx.proto_list)
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200220 enable_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200221 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200222 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreau5b95ae62020-09-25 16:41:05 +0200223 return ERR_NONE;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200224}
225
Willy Tarreaud1d54542012-09-12 22:58:11 +0200226/*
227 * Local variables:
228 * c-indent-level: 8
229 * c-basic-offset: 8
230 * End:
231 */