Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | a6e3be7 | 2016-08-10 18:24:48 +0200 | [diff] [blame] | 13 | #include <sys/types.h> |
Willy Tarreau | b550d00 | 2015-02-20 16:53:25 +0100 | [diff] [blame] | 14 | #include <sys/socket.h> |
| 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 17 | #include <haproxy/errors.h> |
Frédéric Lécaille | 12a0317 | 2023-01-12 15:23:54 +0100 | [diff] [blame] | 18 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 19 | #include <haproxy/list.h> |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame] | 20 | #include <haproxy/listener.h> |
Frédéric Lécaille | 12a0317 | 2023-01-12 15:23:54 +0100 | [diff] [blame] | 21 | #include <haproxy/proto_quic.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/protocol.h> |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 23 | #include <haproxy/proxy.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 24 | #include <haproxy/tools.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 26 | |
| 27 | /* List head of all registered protocols */ |
| 28 | static struct list protocols = LIST_HEAD_INIT(protocols); |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 29 | struct protocol *__protocol_by_family[AF_CUST_MAX][PROTO_NUM_TYPES][2] __read_mostly = { }; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 31 | /* This is the global spinlock we may need to register/unregister listeners or |
| 32 | * protocols. Its main purpose is in fact to serialize the rare stop/deinit() |
| 33 | * phases. |
| 34 | */ |
| 35 | __decl_spinlock(proto_lock); |
| 36 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 37 | /* Registers the protocol <proto> */ |
| 38 | void protocol_register(struct protocol *proto) |
| 39 | { |
Willy Tarreau | bdcee7f | 2021-10-27 15:06:35 +0200 | [diff] [blame] | 40 | int sock_domain = proto->fam->sock_domain; |
| 41 | |
| 42 | BUG_ON(sock_domain < 0 || sock_domain >= AF_CUST_MAX); |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 43 | BUG_ON(proto->proto_type >= PROTO_NUM_TYPES); |
Willy Tarreau | bdcee7f | 2021-10-27 15:06:35 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 45 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 46 | LIST_APPEND(&protocols, &proto->list); |
Willy Tarreau | bdcee7f | 2021-10-27 15:06:35 +0200 | [diff] [blame] | 47 | __protocol_by_family[sock_domain] |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 48 | [proto->proto_type] |
Willy Tarreau | 91b4726 | 2022-05-20 16:36:46 +0200 | [diff] [blame] | 49 | [proto->xprt_type == PROTO_TYPE_DGRAM] = proto; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 50 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Unregisters the protocol <proto>. Note that all listeners must have |
| 54 | * previously been unbound. |
| 55 | */ |
| 56 | void protocol_unregister(struct protocol *proto) |
| 57 | { |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 58 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 59 | LIST_DELETE(&proto->list); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 60 | LIST_INIT(&proto->list); |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 61 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* binds all listeners of all registered protocols. Returns a composition |
| 65 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 66 | */ |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 67 | int protocol_bind_all(int verbose) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 68 | { |
| 69 | struct protocol *proto; |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame] | 70 | struct listener *listener; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 71 | struct receiver *receiver; |
Bjoern Jacke | ed17485 | 2021-01-12 19:24:43 +0100 | [diff] [blame] | 72 | char msg[1000]; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 73 | char *errmsg; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 74 | int err, lerr; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 75 | |
| 76 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 77 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 78 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 79 | list_for_each_entry(receiver, &proto->receivers, proto_list) { |
Frédéric Lécaille | 12a0317 | 2023-01-12 15:23:54 +0100 | [diff] [blame] | 80 | #ifdef USE_QUIC |
| 81 | if ((global.tune.options & GTUNE_NO_QUIC) && |
| 82 | (proto == &proto_quic4 || proto == &proto_quic6)) |
| 83 | continue; |
| 84 | #endif |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 85 | listener = LIST_ELEM(receiver, struct listener *, rx); |
| 86 | |
Willy Tarreau | 233ad28 | 2020-10-15 21:45:15 +0200 | [diff] [blame] | 87 | lerr = proto->fam->bind(receiver, &errmsg); |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 88 | err |= lerr; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 89 | |
| 90 | /* errors are reported if <verbose> is set or if they are fatal */ |
| 91 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 92 | struct proxy *px = listener->bind_conf->frontend; |
| 93 | |
| 94 | if (lerr & ERR_ALERT) |
Willy Tarreau | 37de553 | 2021-10-14 11:55:48 +0200 | [diff] [blame] | 95 | ha_alert("Binding [%s:%d] for %s %s: %s\n", |
| 96 | listener->bind_conf->file, listener->bind_conf->line, |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 97 | proxy_type_str(px), px->id, errmsg); |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 98 | else if (lerr & ERR_WARN) |
Willy Tarreau | 37de553 | 2021-10-14 11:55:48 +0200 | [diff] [blame] | 99 | ha_warning("Binding [%s:%d] for %s %s: %s\n", |
| 100 | listener->bind_conf->file, listener->bind_conf->line, |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 101 | proxy_type_str(px), px->id, errmsg); |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 102 | } |
Aurelien DARRAGON | 8429627 | 2023-02-07 15:51:58 +0100 | [diff] [blame] | 103 | if (lerr != ERR_NONE) |
| 104 | ha_free(&errmsg); |
| 105 | |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 106 | if (lerr & ERR_ABORT) |
| 107 | break; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 108 | |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 109 | if (lerr & ~ERR_WARN) |
| 110 | continue; |
| 111 | |
| 112 | /* for now there's still always a listening function */ |
| 113 | BUG_ON(!proto->listen); |
| 114 | lerr = proto->listen(listener, msg, sizeof(msg)); |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 115 | err |= lerr; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 116 | |
| 117 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 118 | struct proxy *px = listener->bind_conf->frontend; |
| 119 | |
| 120 | if (lerr & ERR_ALERT) |
Willy Tarreau | 37de553 | 2021-10-14 11:55:48 +0200 | [diff] [blame] | 121 | ha_alert("Starting [%s:%d] for %s %s: %s\n", |
| 122 | listener->bind_conf->file, listener->bind_conf->line, |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 123 | proxy_type_str(px), px->id, msg); |
| 124 | else if (lerr & ERR_WARN) |
Willy Tarreau | 37de553 | 2021-10-14 11:55:48 +0200 | [diff] [blame] | 125 | ha_warning("Starting [%s:%d] for %s %s: %s\n", |
| 126 | listener->bind_conf->file, listener->bind_conf->line, |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 127 | proxy_type_str(px), px->id, msg); |
| 128 | } |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 129 | if (lerr & ERR_ABORT) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 130 | break; |
| 131 | } |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 132 | if (err & ERR_ABORT) |
| 133 | break; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 134 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 135 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 136 | return err; |
| 137 | } |
| 138 | |
| 139 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 140 | * This must be performed before calling exit() in order to get a chance to |
| 141 | * remove file-system based sockets and pipes. |
| 142 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
| 143 | */ |
| 144 | int protocol_unbind_all(void) |
| 145 | { |
| 146 | struct protocol *proto; |
Willy Tarreau | ca21262 | 2020-09-02 10:31:31 +0200 | [diff] [blame] | 147 | struct listener *listener; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 148 | int err; |
| 149 | |
| 150 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 151 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 152 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 153 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Willy Tarreau | ca21262 | 2020-09-02 10:31:31 +0200 | [diff] [blame] | 154 | unbind_listener(listener); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 155 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 156 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 157 | return err; |
| 158 | } |
| 159 | |
Willy Tarreau | eb77824 | 2021-06-11 16:27:10 +0200 | [diff] [blame] | 160 | /* stops all listeners of all registered protocols. This will normally catch |
| 161 | * every single listener, all protocols included. This is to be used during |
| 162 | * soft_stop() only. It does not return any error. |
Willy Tarreau | 02e8557 | 2020-10-07 16:50:49 +0200 | [diff] [blame] | 163 | */ |
| 164 | void protocol_stop_now(void) |
| 165 | { |
| 166 | struct protocol *proto; |
| 167 | struct listener *listener, *lback; |
| 168 | |
| 169 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 170 | list_for_each_entry(proto, &protocols, list) { |
| 171 | list_for_each_entry_safe(listener, lback, &proto->receivers, rx.proto_list) |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 172 | stop_listener(listener, 0, 1, 0); |
Willy Tarreau | 02e8557 | 2020-10-07 16:50:49 +0200 | [diff] [blame] | 173 | } |
| 174 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 175 | } |
| 176 | |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 177 | /* suspends all listeners of all registered protocols. This is typically |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 178 | * used on SIG_TTOU to release all listening sockets for the time needed to |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 179 | * try to bind a new process. The listeners enter LI_PAUSED or LI_ASSIGNED. |
| 180 | * It returns ERR_NONE, with ERR_FATAL on failure. |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 181 | */ |
| 182 | int protocol_pause_all(void) |
| 183 | { |
| 184 | struct protocol *proto; |
| 185 | struct listener *listener; |
| 186 | int err; |
| 187 | |
| 188 | err = 0; |
| 189 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 190 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 191 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Aurelien DARRAGON | d3ffba4 | 2023-02-13 17:45:08 +0100 | [diff] [blame] | 192 | if (!suspend_listener(listener, 0, 0)) |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 193 | err |= ERR_FATAL; |
| 194 | } |
| 195 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 196 | return err; |
| 197 | } |
| 198 | |
| 199 | /* resumes all listeners of all registered protocols. This is typically used on |
| 200 | * SIG_TTIN to re-enable listening sockets after a new process failed to bind. |
| 201 | * The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL |
| 202 | * on failure. |
| 203 | */ |
| 204 | int protocol_resume_all(void) |
| 205 | { |
| 206 | struct protocol *proto; |
| 207 | struct listener *listener; |
| 208 | int err; |
| 209 | |
| 210 | err = 0; |
| 211 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 212 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 213 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Aurelien DARRAGON | 4059e09 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 214 | if (!resume_listener(listener, 0, 0)) |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 215 | err |= ERR_FATAL; |
| 216 | } |
| 217 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 218 | return err; |
| 219 | } |
| 220 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 221 | /* enables all listeners of all registered protocols. This is intended to be |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 222 | * used after a fork() to enable reading on all file descriptors. Returns |
| 223 | * composition of ERR_NONE. |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 224 | */ |
| 225 | int protocol_enable_all(void) |
| 226 | { |
| 227 | struct protocol *proto; |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 228 | struct listener *listener; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 229 | |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 230 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 231 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 232 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 233 | enable_listener(listener); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 234 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 235 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 236 | return ERR_NONE; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 239 | /* |
| 240 | * Local variables: |
| 241 | * c-indent-level: 8 |
| 242 | * c-basic-offset: 8 |
| 243 | * End: |
| 244 | */ |