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> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 18 | #include <haproxy/list.h> |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame] | 19 | #include <haproxy/listener.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/protocol.h> |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 21 | #include <haproxy/proxy.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 22 | #include <haproxy/tools.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 23 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 24 | |
| 25 | /* List head of all registered protocols */ |
| 26 | static struct list protocols = LIST_HEAD_INIT(protocols); |
Willy Tarreau | ad6722e | 2021-04-10 17:38:05 +0200 | [diff] [blame] | 27 | struct protocol *__protocol_by_family[AF_CUST_MAX][2][2] __read_mostly = { }; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 29 | /* 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 Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 35 | /* Registers the protocol <proto> */ |
| 36 | void protocol_register(struct protocol *proto) |
| 37 | { |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 38 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 39 | LIST_APPEND(&protocols, &proto->list); |
Willy Tarreau | f9ad06c | 2020-12-08 12:13:54 +0100 | [diff] [blame] | 40 | if (proto->fam->sock_domain >= 0 && proto->fam->sock_domain < AF_CUST_MAX) |
| 41 | __protocol_by_family[proto->fam->sock_domain] |
Willy Tarreau | 910c64d | 2020-09-16 21:28:15 +0200 | [diff] [blame] | 42 | [proto->sock_type == SOCK_DGRAM] |
| 43 | [proto->ctrl_type == SOCK_DGRAM] = proto; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 44 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* Unregisters the protocol <proto>. Note that all listeners must have |
| 48 | * previously been unbound. |
| 49 | */ |
| 50 | void protocol_unregister(struct protocol *proto) |
| 51 | { |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 52 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 53 | LIST_DELETE(&proto->list); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 54 | LIST_INIT(&proto->list); |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 55 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* binds all listeners of all registered protocols. Returns a composition |
| 59 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 60 | */ |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 61 | int protocol_bind_all(int verbose) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 62 | { |
| 63 | struct protocol *proto; |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame] | 64 | struct listener *listener; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 65 | struct receiver *receiver; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 66 | char msg[100]; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 67 | char *errmsg; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 68 | int err, lerr; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 69 | |
| 70 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 71 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 72 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 73 | list_for_each_entry(receiver, &proto->receivers, proto_list) { |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 74 | listener = LIST_ELEM(receiver, struct listener *, rx); |
| 75 | |
Willy Tarreau | 233ad28 | 2020-10-15 21:45:15 +0200 | [diff] [blame] | 76 | lerr = proto->fam->bind(receiver, &errmsg); |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 77 | err |= lerr; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 78 | |
| 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) |
| 84 | ha_alert("Starting %s %s: %s\n", |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 85 | proxy_type_str(px), px->id, errmsg); |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 86 | else if (lerr & ERR_WARN) |
| 87 | ha_warning("Starting %s %s: %s\n", |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 88 | proxy_type_str(px), px->id, errmsg); |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 89 | } |
Aurelien DARRAGON | 8af0800 | 2023-02-07 15:51:58 +0100 | [diff] [blame] | 90 | if (lerr != ERR_NONE) |
| 91 | ha_free(&errmsg); |
| 92 | |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 93 | if (lerr & ERR_ABORT) |
| 94 | break; |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 95 | |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 96 | 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 Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 102 | err |= lerr; |
Willy Tarreau | fc97488 | 2020-09-02 18:22:11 +0200 | [diff] [blame] | 103 | |
| 104 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 105 | struct proxy *px = listener->bind_conf->frontend; |
| 106 | |
| 107 | if (lerr & ERR_ALERT) |
| 108 | ha_alert("Starting %s %s: %s\n", |
| 109 | proxy_type_str(px), px->id, msg); |
| 110 | else if (lerr & ERR_WARN) |
| 111 | ha_warning("Starting %s %s: %s\n", |
| 112 | proxy_type_str(px), px->id, msg); |
| 113 | } |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 114 | if (lerr & ERR_ABORT) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 115 | break; |
| 116 | } |
Willy Tarreau | e91bff2 | 2020-09-02 11:11:43 +0200 | [diff] [blame] | 117 | if (err & ERR_ABORT) |
| 118 | break; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 119 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 120 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 121 | return err; |
| 122 | } |
| 123 | |
| 124 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 125 | * This must be performed before calling exit() in order to get a chance to |
| 126 | * remove file-system based sockets and pipes. |
| 127 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
| 128 | */ |
| 129 | int protocol_unbind_all(void) |
| 130 | { |
| 131 | struct protocol *proto; |
Willy Tarreau | ca21262 | 2020-09-02 10:31:31 +0200 | [diff] [blame] | 132 | struct listener *listener; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 133 | int err; |
| 134 | |
| 135 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 136 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 137 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 138 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Willy Tarreau | ca21262 | 2020-09-02 10:31:31 +0200 | [diff] [blame] | 139 | unbind_listener(listener); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 140 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 141 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 142 | return err; |
| 143 | } |
| 144 | |
Willy Tarreau | 02e8557 | 2020-10-07 16:50:49 +0200 | [diff] [blame] | 145 | /* stops all listeners of all registered protocols, except when the belong to a |
| 146 | * proxy configured with a grace time. This will normally catch every single |
| 147 | * listener, all protocols included, and the grace ones will have to be handled |
| 148 | * by the proxy stopping loop. This is to be used during soft_stop() only. It |
| 149 | * does not return any error. |
| 150 | */ |
| 151 | void 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) |
| 159 | if (!listener->bind_conf->frontend->grace) |
Aurelien DARRAGON | 7e2eee0 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 160 | stop_listener(listener, 0, 1, 0); |
Willy Tarreau | 02e8557 | 2020-10-07 16:50:49 +0200 | [diff] [blame] | 161 | } |
| 162 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 163 | } |
| 164 | |
Aurelien DARRAGON | 8283d59 | 2023-02-13 17:45:08 +0100 | [diff] [blame^] | 165 | /* suspends all listeners of all registered protocols. This is typically |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 166 | * used on SIG_TTOU to release all listening sockets for the time needed to |
Aurelien DARRAGON | 8283d59 | 2023-02-13 17:45:08 +0100 | [diff] [blame^] | 167 | * try to bind a new process. The listeners enter LI_PAUSED or LI_ASSIGNED. |
| 168 | * It returns ERR_NONE, with ERR_FATAL on failure. |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 169 | */ |
| 170 | int protocol_pause_all(void) |
| 171 | { |
| 172 | struct protocol *proto; |
| 173 | struct listener *listener; |
| 174 | int err; |
| 175 | |
| 176 | err = 0; |
| 177 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 178 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 179 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Aurelien DARRAGON | 8283d59 | 2023-02-13 17:45:08 +0100 | [diff] [blame^] | 180 | if (!suspend_listener(listener, 0, 0)) |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 181 | err |= ERR_FATAL; |
| 182 | } |
| 183 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 184 | return err; |
| 185 | } |
| 186 | |
| 187 | /* resumes all listeners of all registered protocols. This is typically used on |
| 188 | * SIG_TTIN to re-enable listening sockets after a new process failed to bind. |
| 189 | * The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL |
| 190 | * on failure. |
| 191 | */ |
| 192 | int protocol_resume_all(void) |
| 193 | { |
| 194 | struct protocol *proto; |
| 195 | struct listener *listener; |
| 196 | int err; |
| 197 | |
| 198 | err = 0; |
| 199 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
| 200 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 201 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Aurelien DARRAGON | 7e2eee0 | 2023-02-06 17:06:03 +0100 | [diff] [blame] | 202 | if (!resume_listener(listener, 0, 0)) |
Willy Tarreau | 09819d1 | 2020-09-24 16:26:50 +0200 | [diff] [blame] | 203 | err |= ERR_FATAL; |
| 204 | } |
| 205 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
| 206 | return err; |
| 207 | } |
| 208 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 209 | /* enables all listeners of all registered protocols. This is intended to be |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 210 | * used after a fork() to enable reading on all file descriptors. Returns |
| 211 | * composition of ERR_NONE. |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 212 | */ |
| 213 | int protocol_enable_all(void) |
| 214 | { |
| 215 | struct protocol *proto; |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 216 | struct listener *listener; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 217 | |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 218 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 219 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 220 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 221 | enable_listener(listener); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 222 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 223 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | 5b95ae6 | 2020-09-25 16:41:05 +0200 | [diff] [blame] | 224 | return ERR_NONE; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 227 | /* |
| 228 | * Local variables: |
| 229 | * c-indent-level: 8 |
| 230 | * c-basic-offset: 8 |
| 231 | * End: |
| 232 | */ |