Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AF_INET/AF_INET6 QUIC protocol layer. |
| 3 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2020 Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 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 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <time.h> |
| 19 | |
| 20 | #include <sys/param.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <netinet/udp.h> |
| 25 | #include <netinet/in.h> |
| 26 | |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 27 | #include <import/ebtree-t.h> |
| 28 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/arg.h> |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 31 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 32 | #include <haproxy/connection.h> |
| 33 | #include <haproxy/errors.h> |
| 34 | #include <haproxy/fd.h> |
| 35 | #include <haproxy/global.h> |
| 36 | #include <haproxy/list.h> |
| 37 | #include <haproxy/listener.h> |
| 38 | #include <haproxy/log.h> |
| 39 | #include <haproxy/namespace.h> |
| 40 | #include <haproxy/port_range.h> |
| 41 | #include <haproxy/protocol.h> |
| 42 | #include <haproxy/proto_quic.h> |
| 43 | #include <haproxy/proto_udp.h> |
| 44 | #include <haproxy/proxy-t.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 45 | #include <haproxy/quic_conn.h> |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 46 | #include <haproxy/quic_sock.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 47 | #include <haproxy/sock.h> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 48 | #include <haproxy/sock_inet.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 49 | #include <haproxy/task.h> |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 50 | #include <haproxy/tools.h> |
| 51 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 52 | /* per-thread quic datagram handlers */ |
| 53 | struct quic_dghdlr *quic_dghdlrs; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 54 | struct eb_root *quic_cid_tree; |
| 55 | |
| 56 | /* global CID trees */ |
| 57 | #define QUIC_CID_TREES_CNT 256 |
| 58 | struct quic_cid_tree *quic_cid_trees; |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 59 | |
Frédéric Lécaille | 71f3abb | 2022-02-15 16:59:48 +0100 | [diff] [blame] | 60 | /* Size of the internal buffer of QUIC RX buffer at the fd level */ |
| 61 | #define QUIC_RX_BUFSZ (1UL << 18) |
| 62 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 63 | DECLARE_STATIC_POOL(pool_head_quic_rxbuf, "quic_rxbuf", QUIC_RX_BUFSZ); |
Frédéric Lécaille | 71f3abb | 2022-02-15 16:59:48 +0100 | [diff] [blame] | 64 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 65 | static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen); |
| 66 | static int quic_connect_server(struct connection *conn, int flags); |
| 67 | static void quic_enable_listener(struct listener *listener); |
| 68 | static void quic_disable_listener(struct listener *listener); |
Amaury Denoyelle | 1acbbca | 2023-04-05 18:17:51 +0200 | [diff] [blame] | 69 | static int quic_set_affinity(struct connection *conn, int new_tid); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 70 | |
| 71 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 72 | struct protocol proto_quic4 = { |
| 73 | .name = "quic4", |
| 74 | |
| 75 | /* connection layer */ |
Willy Tarreau | 91b4726 | 2022-05-20 16:36:46 +0200 | [diff] [blame] | 76 | .xprt_type = PROTO_TYPE_STREAM, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 77 | .listen = quic_bind_listener, |
| 78 | .enable = quic_enable_listener, |
| 79 | .disable = quic_disable_listener, |
Amaury Denoyelle | 1acbbca | 2023-04-05 18:17:51 +0200 | [diff] [blame] | 80 | .add = default_add_listener, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 81 | .unbind = default_unbind_listener, |
| 82 | .suspend = default_suspend_listener, |
| 83 | .resume = default_resume_listener, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 84 | .accept_conn = quic_sock_accept_conn, |
Willy Tarreau | cdf7c8e | 2022-04-11 16:20:00 +0200 | [diff] [blame] | 85 | .get_src = quic_sock_get_src, |
| 86 | .get_dst = quic_sock_get_dst, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 87 | .connect = quic_connect_server, |
Amaury Denoyelle | 1acbbca | 2023-04-05 18:17:51 +0200 | [diff] [blame] | 88 | .set_affinity = quic_set_affinity, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 89 | |
| 90 | /* binding layer */ |
| 91 | .rx_suspend = udp_suspend_receiver, |
| 92 | .rx_resume = udp_resume_receiver, |
| 93 | |
| 94 | /* address family */ |
| 95 | .fam = &proto_fam_inet4, |
| 96 | |
| 97 | /* socket layer */ |
Willy Tarreau | 337edfd | 2021-10-27 17:05:36 +0200 | [diff] [blame] | 98 | .proto_type = PROTO_TYPE_DGRAM, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 99 | .sock_type = SOCK_DGRAM, |
| 100 | .sock_prot = IPPROTO_UDP, |
| 101 | .rx_enable = sock_enable, |
| 102 | .rx_disable = sock_disable, |
| 103 | .rx_unbind = sock_unbind, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 104 | .rx_listening = quic_sock_accepting_conn, |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 105 | .default_iocb = quic_lstnr_sock_fd_iocb, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 106 | .receivers = LIST_HEAD_INIT(proto_quic4.receivers), |
| 107 | .nb_receivers = 0, |
Willy Tarreau | 785b89f | 2023-04-22 15:09:07 +0200 | [diff] [blame] | 108 | #ifdef SO_REUSEPORT |
| 109 | .flags = PROTO_F_REUSEPORT_SUPPORTED, |
| 110 | #endif |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | INITCALL1(STG_REGISTER, protocol_register, &proto_quic4); |
| 114 | |
| 115 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 116 | struct protocol proto_quic6 = { |
| 117 | .name = "quic6", |
| 118 | |
| 119 | /* connection layer */ |
Willy Tarreau | 91b4726 | 2022-05-20 16:36:46 +0200 | [diff] [blame] | 120 | .xprt_type = PROTO_TYPE_STREAM, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 121 | .listen = quic_bind_listener, |
| 122 | .enable = quic_enable_listener, |
| 123 | .disable = quic_disable_listener, |
Amaury Denoyelle | 1acbbca | 2023-04-05 18:17:51 +0200 | [diff] [blame] | 124 | .add = default_add_listener, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 125 | .unbind = default_unbind_listener, |
| 126 | .suspend = default_suspend_listener, |
| 127 | .resume = default_resume_listener, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 128 | .accept_conn = quic_sock_accept_conn, |
Willy Tarreau | cdf7c8e | 2022-04-11 16:20:00 +0200 | [diff] [blame] | 129 | .get_src = quic_sock_get_src, |
| 130 | .get_dst = quic_sock_get_dst, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 131 | .connect = quic_connect_server, |
Amaury Denoyelle | 1acbbca | 2023-04-05 18:17:51 +0200 | [diff] [blame] | 132 | .set_affinity = quic_set_affinity, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 133 | |
| 134 | /* binding layer */ |
| 135 | .rx_suspend = udp_suspend_receiver, |
| 136 | .rx_resume = udp_resume_receiver, |
| 137 | |
| 138 | /* address family */ |
| 139 | .fam = &proto_fam_inet6, |
| 140 | |
| 141 | /* socket layer */ |
Willy Tarreau | 337edfd | 2021-10-27 17:05:36 +0200 | [diff] [blame] | 142 | .proto_type = PROTO_TYPE_DGRAM, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 143 | .sock_type = SOCK_DGRAM, |
| 144 | .sock_prot = IPPROTO_UDP, |
| 145 | .rx_enable = sock_enable, |
| 146 | .rx_disable = sock_disable, |
| 147 | .rx_unbind = sock_unbind, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 148 | .rx_listening = quic_sock_accepting_conn, |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 149 | .default_iocb = quic_lstnr_sock_fd_iocb, |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 150 | .receivers = LIST_HEAD_INIT(proto_quic6.receivers), |
| 151 | .nb_receivers = 0, |
Willy Tarreau | 785b89f | 2023-04-22 15:09:07 +0200 | [diff] [blame] | 152 | #ifdef SO_REUSEPORT |
| 153 | .flags = PROTO_F_REUSEPORT_SUPPORTED, |
| 154 | #endif |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | INITCALL1(STG_REGISTER, protocol_register, &proto_quic6); |
| 158 | |
| 159 | /* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which |
| 160 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 161 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 162 | * - 1 : use provided address |
| 163 | * - 2 : use provided port |
| 164 | * - 3 : use both |
| 165 | * |
| 166 | * The function supports multiple foreign binding methods : |
| 167 | * - linux_tproxy: we directly bind to the foreign address |
| 168 | * The second one can be used as a fallback for the first one. |
| 169 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 170 | * local address, 2 if it could not bind to the foreign address. |
| 171 | */ |
| 172 | int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote) |
| 173 | { |
| 174 | struct sockaddr_storage bind_addr; |
| 175 | int foreign_ok = 0; |
| 176 | int ret; |
| 177 | static THREAD_LOCAL int ip_transp_working = 1; |
| 178 | static THREAD_LOCAL int ip6_transp_working = 1; |
| 179 | |
| 180 | switch (local->ss_family) { |
| 181 | case AF_INET: |
| 182 | if (flags && ip_transp_working) { |
| 183 | /* This deserves some explanation. Some platforms will support |
| 184 | * multiple combinations of certain methods, so we try the |
| 185 | * supported ones until one succeeds. |
| 186 | */ |
| 187 | if (sock_inet4_make_foreign(fd)) |
| 188 | foreign_ok = 1; |
| 189 | else |
| 190 | ip_transp_working = 0; |
| 191 | } |
| 192 | break; |
| 193 | case AF_INET6: |
| 194 | if (flags && ip6_transp_working) { |
| 195 | if (sock_inet6_make_foreign(fd)) |
| 196 | foreign_ok = 1; |
| 197 | else |
| 198 | ip6_transp_working = 0; |
| 199 | } |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | if (flags) { |
| 204 | memset(&bind_addr, 0, sizeof(bind_addr)); |
| 205 | bind_addr.ss_family = remote->ss_family; |
| 206 | switch (remote->ss_family) { |
| 207 | case AF_INET: |
| 208 | if (flags & 1) |
| 209 | ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr; |
| 210 | if (flags & 2) |
| 211 | ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port; |
| 212 | break; |
| 213 | case AF_INET6: |
| 214 | if (flags & 1) |
| 215 | ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr; |
| 216 | if (flags & 2) |
| 217 | ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port; |
| 218 | break; |
| 219 | default: |
| 220 | /* we don't want to try to bind to an unknown address family */ |
| 221 | foreign_ok = 0; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 226 | if (foreign_ok) { |
| 227 | if (is_inet_addr(&bind_addr)) { |
| 228 | ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr)); |
| 229 | if (ret < 0) |
| 230 | return 2; |
| 231 | } |
| 232 | } |
| 233 | else { |
| 234 | if (is_inet_addr(local)) { |
| 235 | ret = bind(fd, (struct sockaddr *)local, get_addr_len(local)); |
| 236 | if (ret < 0) |
| 237 | return 1; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (!flags) |
| 242 | return 0; |
| 243 | |
| 244 | if (!foreign_ok) |
| 245 | /* we could not bind to a foreign address */ |
| 246 | return 2; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * This function initiates a QUIC connection establishment to the target assigned |
| 253 | * to connection <conn> using (si->{target,dst}). A source address may be |
| 254 | * pointed to by conn->src in case of transparent proxying. Normal source |
| 255 | * bind addresses are still determined locally (due to the possible need of a |
| 256 | * source port). conn->target may point either to a valid server or to a backend, |
| 257 | * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are |
| 258 | * supported. The <data> parameter is a boolean indicating whether there are data |
| 259 | * waiting for being sent or not, in order to adjust data write polling and on |
| 260 | * some platforms, the ability to avoid an empty initial ACK. The <flags> argument |
| 261 | * is not used. |
| 262 | * |
| 263 | * Note that a pending send_proxy message accounts for data. |
| 264 | * |
| 265 | * It can return one of : |
| 266 | * - SF_ERR_NONE if everything's OK |
| 267 | * - SF_ERR_SRVTO if there are no more servers |
| 268 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 269 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 270 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 271 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 272 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 273 | * |
| 274 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
| 275 | * it's invalid and the caller has nothing to do. |
| 276 | */ |
| 277 | |
| 278 | int quic_connect_server(struct connection *conn, int flags) |
| 279 | { |
| 280 | int fd; |
| 281 | struct server *srv; |
| 282 | struct proxy *be; |
| 283 | struct conn_src *src; |
| 284 | struct sockaddr_storage *addr; |
| 285 | |
Willy Tarreau | 158b6cf | 2022-05-02 17:45:12 +0200 | [diff] [blame] | 286 | BUG_ON(!conn->dst); |
| 287 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 288 | conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */ |
| 289 | |
| 290 | switch (obj_type(conn->target)) { |
| 291 | case OBJ_TYPE_PROXY: |
Frédéric Lécaille | e1c3546 | 2022-02-14 19:01:21 +0100 | [diff] [blame] | 292 | be = __objt_proxy(conn->target); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 293 | srv = NULL; |
| 294 | break; |
| 295 | case OBJ_TYPE_SERVER: |
Frédéric Lécaille | e1c3546 | 2022-02-14 19:01:21 +0100 | [diff] [blame] | 296 | srv = __objt_server(conn->target); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 297 | be = srv->proxy; |
| 298 | break; |
| 299 | default: |
| 300 | conn->flags |= CO_FL_ERROR; |
| 301 | return SF_ERR_INTERNAL; |
| 302 | } |
| 303 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 304 | fd = conn->handle.fd = sock_create_server_socket(conn); |
| 305 | |
| 306 | if (fd == -1) { |
| 307 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 308 | |
| 309 | if (errno == ENFILE) { |
| 310 | conn->err_code = CO_ER_SYS_FDLIM; |
| 311 | send_log(be, LOG_EMERG, |
| 312 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 313 | be->id, global.maxsock); |
| 314 | } |
| 315 | else if (errno == EMFILE) { |
| 316 | conn->err_code = CO_ER_PROC_FDLIM; |
| 317 | send_log(be, LOG_EMERG, |
| 318 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 319 | be->id, global.maxsock); |
| 320 | } |
| 321 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 322 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 323 | send_log(be, LOG_EMERG, |
| 324 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 325 | be->id, global.maxsock); |
| 326 | } |
| 327 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 328 | conn->err_code = CO_ER_NOPROTO; |
| 329 | } |
| 330 | else |
| 331 | conn->err_code = CO_ER_SOCK_ERR; |
| 332 | |
| 333 | /* this is a resource error */ |
| 334 | conn->flags |= CO_FL_ERROR; |
| 335 | return SF_ERR_RESOURCE; |
| 336 | } |
| 337 | |
| 338 | if (fd >= global.maxsock) { |
| 339 | /* do not log anything there, it's a normal condition when this option |
| 340 | * is used to serialize connections to a server ! |
| 341 | */ |
| 342 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 343 | close(fd); |
| 344 | conn->err_code = CO_ER_CONF_FDLIM; |
| 345 | conn->flags |= CO_FL_ERROR; |
| 346 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
| 347 | } |
| 348 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 349 | if (fd_set_nonblock(fd) == -1) { |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 350 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 351 | close(fd); |
| 352 | conn->err_code = CO_ER_SOCK_ERR; |
| 353 | conn->flags |= CO_FL_ERROR; |
| 354 | return SF_ERR_INTERNAL; |
| 355 | } |
| 356 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 357 | if (master == 1 && fd_set_cloexec(fd) == -1) { |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 358 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 359 | close(fd); |
| 360 | conn->err_code = CO_ER_SOCK_ERR; |
| 361 | conn->flags |= CO_FL_ERROR; |
| 362 | return SF_ERR_INTERNAL; |
| 363 | } |
| 364 | |
| 365 | /* allow specific binding : |
| 366 | * - server-specific at first |
| 367 | * - proxy-specific next |
| 368 | */ |
| 369 | if (srv && srv->conn_src.opts & CO_SRC_BIND) |
| 370 | src = &srv->conn_src; |
| 371 | else if (be->conn_src.opts & CO_SRC_BIND) |
| 372 | src = &be->conn_src; |
| 373 | else |
| 374 | src = NULL; |
| 375 | |
| 376 | if (src) { |
| 377 | int ret, flags = 0; |
| 378 | |
| 379 | if (conn->src && is_inet_addr(conn->src)) { |
| 380 | switch (src->opts & CO_SRC_TPROXY_MASK) { |
| 381 | case CO_SRC_TPROXY_CLI: |
| 382 | conn_set_private(conn); |
Willy Tarreau | 5c8b52f | 2022-11-14 07:02:00 +0100 | [diff] [blame] | 383 | __fallthrough; |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 384 | case CO_SRC_TPROXY_ADDR: |
| 385 | flags = 3; |
| 386 | break; |
| 387 | case CO_SRC_TPROXY_CIP: |
| 388 | case CO_SRC_TPROXY_DYN: |
| 389 | conn_set_private(conn); |
| 390 | flags = 1; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | #ifdef SO_BINDTODEVICE |
| 396 | /* Note: this might fail if not CAP_NET_RAW */ |
| 397 | if (src->iface_name) |
| 398 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1); |
| 399 | #endif |
| 400 | |
| 401 | if (src->sport_range) { |
| 402 | int attempts = 10; /* should be more than enough to find a spare port */ |
| 403 | struct sockaddr_storage sa; |
| 404 | |
| 405 | ret = 1; |
| 406 | memcpy(&sa, &src->source_addr, sizeof(sa)); |
| 407 | |
| 408 | do { |
| 409 | /* note: in case of retry, we may have to release a previously |
| 410 | * allocated port, hence this loop's construct. |
| 411 | */ |
| 412 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 413 | fdinfo[fd].port_range = NULL; |
| 414 | |
| 415 | if (!attempts) |
| 416 | break; |
| 417 | attempts--; |
| 418 | |
| 419 | fdinfo[fd].local_port = port_range_alloc_port(src->sport_range); |
| 420 | if (!fdinfo[fd].local_port) { |
| 421 | conn->err_code = CO_ER_PORT_RANGE; |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | fdinfo[fd].port_range = src->sport_range; |
| 426 | set_host_port(&sa, fdinfo[fd].local_port); |
| 427 | |
| 428 | ret = quic_bind_socket(fd, flags, &sa, conn->src); |
| 429 | if (ret != 0) |
| 430 | conn->err_code = CO_ER_CANT_BIND; |
| 431 | } while (ret != 0); /* binding NOK */ |
| 432 | } |
| 433 | else { |
| 434 | #ifdef IP_BIND_ADDRESS_NO_PORT |
| 435 | static THREAD_LOCAL int bind_address_no_port = 1; |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 436 | setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int)); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 437 | #endif |
| 438 | ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src); |
| 439 | if (ret != 0) |
| 440 | conn->err_code = CO_ER_CANT_BIND; |
| 441 | } |
| 442 | |
| 443 | if (unlikely(ret != 0)) { |
| 444 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 445 | fdinfo[fd].port_range = NULL; |
| 446 | close(fd); |
| 447 | |
| 448 | if (ret == 1) { |
| 449 | ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n", |
| 450 | be->id); |
| 451 | send_log(be, LOG_EMERG, |
| 452 | "Cannot bind to source address before connect() for backend %s.\n", |
| 453 | be->id); |
| 454 | } else { |
| 455 | ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n", |
| 456 | be->id); |
| 457 | send_log(be, LOG_EMERG, |
| 458 | "Cannot bind to tproxy source address before connect() for backend %s.\n", |
| 459 | be->id); |
| 460 | } |
| 461 | conn->flags |= CO_FL_ERROR; |
| 462 | return SF_ERR_RESOURCE; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (global.tune.server_sndbuf) |
| 467 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 468 | |
| 469 | if (global.tune.server_rcvbuf) |
| 470 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 471 | |
| 472 | addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst; |
| 473 | if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) { |
| 474 | if (errno == EINPROGRESS || errno == EALREADY) { |
| 475 | /* common case, let's wait for connect status */ |
| 476 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 477 | } |
| 478 | else if (errno == EISCONN) { |
| 479 | /* should normally not happen but if so, indicates that it's OK */ |
| 480 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 481 | } |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 482 | else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 483 | char *msg; |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 484 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) { |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 485 | msg = "no free ports"; |
| 486 | conn->err_code = CO_ER_FREE_PORTS; |
| 487 | } |
| 488 | else { |
| 489 | msg = "local address already in use"; |
| 490 | conn->err_code = CO_ER_ADDR_INUSE; |
| 491 | } |
| 492 | |
| 493 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
| 494 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 495 | fdinfo[fd].port_range = NULL; |
| 496 | close(fd); |
| 497 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
| 498 | conn->flags |= CO_FL_ERROR; |
| 499 | return SF_ERR_RESOURCE; |
| 500 | } else if (errno == ETIMEDOUT) { |
| 501 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
| 502 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 503 | fdinfo[fd].port_range = NULL; |
| 504 | close(fd); |
| 505 | conn->err_code = CO_ER_SOCK_ERR; |
| 506 | conn->flags |= CO_FL_ERROR; |
| 507 | return SF_ERR_SRVTO; |
| 508 | } else { |
| 509 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 510 | //qfprintf(stderr,"Connect(): %d", errno); |
| 511 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 512 | fdinfo[fd].port_range = NULL; |
| 513 | close(fd); |
| 514 | conn->err_code = CO_ER_SOCK_ERR; |
| 515 | conn->flags |= CO_FL_ERROR; |
| 516 | return SF_ERR_SRVCL; |
| 517 | } |
| 518 | } |
| 519 | else { |
| 520 | /* connect() == 0, this is great! */ |
| 521 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 522 | } |
| 523 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 524 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 525 | HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */ |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 526 | |
| 527 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 528 | fd_want_send(fd); |
| 529 | fd_cant_send(fd); |
| 530 | fd_cant_recv(fd); |
| 531 | } |
| 532 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 533 | return SF_ERR_NONE; /* connection is OK */ |
| 534 | } |
| 535 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 536 | /* Allocate the RX buffers for <l> listener. |
| 537 | * Return 1 if succeeded, 0 if not. |
| 538 | */ |
| 539 | static int quic_alloc_rxbufs_listener(struct listener *l) |
| 540 | { |
| 541 | int i; |
Amaury Denoyelle | 1cba8d6 | 2022-10-06 15:16:22 +0200 | [diff] [blame] | 542 | struct quic_receiver_buf *tmp; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 543 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 544 | MT_LIST_INIT(&l->rx.rxbuf_list); |
Willy Tarreau | 8d49253 | 2022-12-21 09:16:55 +0100 | [diff] [blame] | 545 | for (i = 0; i < my_popcountl(l->rx.bind_thread); i++) { |
Amaury Denoyelle | 1cba8d6 | 2022-10-06 15:16:22 +0200 | [diff] [blame] | 546 | struct quic_receiver_buf *rxbuf; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 547 | char *buf; |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 548 | |
Amaury Denoyelle | 1cba8d6 | 2022-10-06 15:16:22 +0200 | [diff] [blame] | 549 | rxbuf = calloc(1, sizeof(*rxbuf)); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 550 | if (!rxbuf) |
| 551 | goto err; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 552 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 553 | buf = pool_alloc(pool_head_quic_rxbuf); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 554 | if (!buf) { |
| 555 | free(rxbuf); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 556 | goto err; |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 559 | rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0); |
Amaury Denoyelle | 1cba8d6 | 2022-10-06 15:16:22 +0200 | [diff] [blame] | 560 | LIST_INIT(&rxbuf->dgram_list); |
| 561 | MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->rxbuf_el); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | return 1; |
| 565 | |
| 566 | err: |
Amaury Denoyelle | 1cba8d6 | 2022-10-06 15:16:22 +0200 | [diff] [blame] | 567 | while ((tmp = MT_LIST_POP(&l->rx.rxbuf_list, typeof(tmp), rxbuf_el))) { |
| 568 | pool_free(pool_head_quic_rxbuf, tmp->buf.area); |
| 569 | free(tmp); |
Frédéric Lécaille | 794d068 | 2022-01-27 10:23:31 +0100 | [diff] [blame] | 570 | } |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Amaury Denoyelle | 75839a4 | 2022-11-21 10:04:14 +0100 | [diff] [blame] | 574 | /* Check if platform supports the required feature set for quic-conn owned |
| 575 | * socket. <l> listener must already be binded; a dummy socket will be opened |
| 576 | * on the same address as one of the support test. |
| 577 | * |
| 578 | * Returns true if platform is deemed compatible else false. |
| 579 | */ |
| 580 | static int quic_test_sock_per_conn_support(struct listener *l) |
| 581 | { |
| 582 | const struct receiver *rx = &l->rx; |
| 583 | int ret = 1, fdtest; |
| 584 | |
Amaury Denoyelle | 8d46acd | 2022-12-01 14:51:16 +0100 | [diff] [blame] | 585 | /* Check if IP destination address can be retrieved on recvfrom() |
| 586 | * operation. |
| 587 | */ |
| 588 | #if !defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR) |
| 589 | ha_alert("Your platform does not seem to support UDP source address retrieval through IP_PKTINFO or an alternative flag. " |
| 590 | "QUIC connections will use listener socket.\n"); |
| 591 | ret = 0; |
| 592 | #endif |
| 593 | |
Amaury Denoyelle | 75839a4 | 2022-11-21 10:04:14 +0100 | [diff] [blame] | 594 | /* Check if platform support multiple UDP sockets bind on the same |
| 595 | * local address. Create a dummy socket and bind it on the same address |
| 596 | * as <l> listener. If bind system call fails, deactivate socket per |
| 597 | * connection. All other errors are not taken into account. |
| 598 | */ |
| 599 | if (ret) { |
| 600 | fdtest = socket(rx->proto->fam->sock_domain, |
| 601 | rx->proto->sock_type, rx->proto->sock_prot); |
Amaury Denoyelle | 30fc277 | 2022-12-05 10:24:54 +0100 | [diff] [blame] | 602 | if (fdtest >= 0) { |
Amaury Denoyelle | 75839a4 | 2022-11-21 10:04:14 +0100 | [diff] [blame] | 603 | if (setsockopt(fdtest, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) && |
| 604 | bind(fdtest, (struct sockaddr *)&rx->addr, rx->proto->fam->sock_addrlen) < 0) { |
| 605 | ha_alert("Your platform does not seem to support multiple UDP sockets binded on the same address. " |
| 606 | "QUIC connections will use listener socket.\n"); |
| 607 | ret = 0; |
| 608 | } |
| 609 | |
| 610 | close(fdtest); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return ret; |
| 615 | } |
| 616 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 617 | /* This function tries to bind a QUIC4/6 listener. It may return a warning or |
| 618 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 619 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 620 | * The return value is composed from ERR_ABORT, ERR_WARN, |
| 621 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 622 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 623 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 624 | * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter |
| 625 | * the meaning of the error, but just indicate that a message is present which |
| 626 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 627 | * that it's pointless to try to start other listeners. No error message is |
| 628 | * returned if errlen is NULL. |
| 629 | */ |
| 630 | static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 631 | { |
Amaury Denoyelle | 487d04f | 2022-10-11 16:22:18 +0200 | [diff] [blame] | 632 | const struct sockaddr_storage addr = listener->rx.addr; |
| 633 | int fd, err = ERR_NONE; |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 634 | char *msg = NULL; |
| 635 | |
| 636 | /* ensure we never return garbage */ |
| 637 | if (errlen) |
| 638 | *errmsg = 0; |
| 639 | |
| 640 | if (listener->state != LI_ASSIGNED) |
| 641 | return ERR_NONE; /* already bound */ |
| 642 | |
| 643 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 644 | msg = "receiving socket not bound"; |
| 645 | goto udp_return; |
| 646 | } |
| 647 | |
Amaury Denoyelle | 487d04f | 2022-10-11 16:22:18 +0200 | [diff] [blame] | 648 | /* Set IP_PKTINFO to retrieve destination address on recv. */ |
| 649 | fd = listener->rx.fd; |
| 650 | switch (addr.ss_family) { |
| 651 | case AF_INET: |
| 652 | #if defined(IP_PKTINFO) |
| 653 | setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one)); |
| 654 | #elif defined(IP_RECVDSTADDR) |
| 655 | setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one)); |
| 656 | #endif /* IP_PKTINFO || IP_RECVDSTADDR */ |
| 657 | break; |
| 658 | case AF_INET6: |
| 659 | #ifdef IPV6_RECVPKTINFO |
| 660 | setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one)); |
| 661 | #endif |
| 662 | break; |
| 663 | default: |
| 664 | break; |
| 665 | } |
| 666 | |
Willy Tarreau | cab054b | 2022-10-11 08:36:21 +0200 | [diff] [blame] | 667 | if (!quic_alloc_rxbufs_listener(listener)) { |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 668 | msg = "could not initialize tx/rx rings"; |
Frédéric Lécaille | 48f8e19 | 2021-07-06 15:39:26 +0200 | [diff] [blame] | 669 | err |= ERR_WARN; |
| 670 | goto udp_return; |
| 671 | } |
| 672 | |
Amaury Denoyelle | 75839a4 | 2022-11-21 10:04:14 +0100 | [diff] [blame] | 673 | if (global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) { |
| 674 | if (!quic_test_sock_per_conn_support(listener)) |
| 675 | global.tune.options &= ~GTUNE_QUIC_SOCK_PER_CONN; |
| 676 | } |
| 677 | |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 678 | listener_set_state(listener, LI_LISTEN); |
| 679 | |
| 680 | udp_return: |
| 681 | if (msg && errlen) { |
| 682 | char pn[INET6_ADDRSTRLEN]; |
| 683 | |
| 684 | addr_to_str(&listener->rx.addr, pn, sizeof(pn)); |
Willy Tarreau | 6823a3a | 2021-10-14 11:59:15 +0200 | [diff] [blame] | 685 | snprintf(errmsg, errlen, "%s for [%s:%d]", msg, pn, get_host_port(&listener->rx.addr)); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 686 | } |
| 687 | return err; |
| 688 | } |
| 689 | |
| 690 | /* Enable receipt of incoming connections for listener <l>. The receiver must |
| 691 | * still be valid. Does nothing in early boot (needs fd_updt). |
| 692 | */ |
| 693 | static void quic_enable_listener(struct listener *l) |
| 694 | { |
| 695 | /* FIXME: The following statements are incorrect. This |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 696 | * is the responsibility of the QUIC xprt to stop accepting new |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 697 | * connections. |
| 698 | */ |
| 699 | if (fd_updt) |
| 700 | fd_want_recv(l->rx.fd); |
| 701 | } |
| 702 | |
| 703 | /* Disable receipt of incoming connections for listener <l>. The receiver must |
| 704 | * still be valid. Does nothing in early boot (needs fd_updt). |
| 705 | */ |
| 706 | static void quic_disable_listener(struct listener *l) |
| 707 | { |
| 708 | /* FIXME: The following statements are incorrect. This |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 709 | * is the responsibility of the QUIC xprt to start accepting new |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 710 | * connections again. |
| 711 | */ |
| 712 | if (fd_updt) |
| 713 | fd_stop_recv(l->rx.fd); |
| 714 | } |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 715 | |
Willy Tarreau | 77d37b0 | 2023-04-20 19:03:49 +0200 | [diff] [blame] | 716 | /* change the connection's thread to <new_tid>. For frontend connections, the |
| 717 | * target is a listener, and the caller is responsible for guaranteeing that |
| 718 | * the listener assigned to the connection is bound to the requested thread. |
| 719 | */ |
Amaury Denoyelle | 1acbbca | 2023-04-05 18:17:51 +0200 | [diff] [blame] | 720 | static int quic_set_affinity(struct connection *conn, int new_tid) |
| 721 | { |
| 722 | struct quic_conn *qc = conn->handle.qc; |
Willy Tarreau | 77d37b0 | 2023-04-20 19:03:49 +0200 | [diff] [blame] | 723 | return qc_set_tid_affinity(qc, new_tid, objt_listener(conn->target)); |
Amaury Denoyelle | 1acbbca | 2023-04-05 18:17:51 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 726 | static int quic_alloc_dghdlrs(void) |
| 727 | { |
| 728 | int i; |
| 729 | |
Tim Duesterhus | 9fb57e8 | 2022-06-01 21:58:37 +0200 | [diff] [blame] | 730 | quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs)); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 731 | if (!quic_dghdlrs) { |
| 732 | ha_alert("Failed to allocate the quic datagram handlers.\n"); |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | for (i = 0; i < global.nbthread; i++) { |
| 737 | struct quic_dghdlr *dghdlr = &quic_dghdlrs[i]; |
| 738 | |
| 739 | dghdlr->task = tasklet_new(); |
| 740 | if (!dghdlr->task) { |
| 741 | ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i); |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | tasklet_set_tid(dghdlr->task, i); |
| 746 | dghdlr->task->context = dghdlr; |
| 747 | dghdlr->task->process = quic_lstnr_dghdlr; |
| 748 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 749 | MT_LIST_INIT(&dghdlr->dgrams); |
| 750 | } |
| 751 | |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 752 | quic_cid_trees = calloc(QUIC_CID_TREES_CNT, sizeof(struct quic_cid_tree)); |
| 753 | if (!quic_cid_trees) { |
| 754 | ha_alert("Failed to allocate global CIDs trees.\n"); |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | for (i = 0; i < QUIC_CID_TREES_CNT; ++i) { |
| 759 | HA_RWLOCK_INIT(&quic_cid_trees[i].lock); |
| 760 | quic_cid_trees[i].root = EB_ROOT_UNIQUE; |
| 761 | } |
| 762 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 763 | return 1; |
| 764 | } |
| 765 | REGISTER_POST_CHECK(quic_alloc_dghdlrs); |
| 766 | |
| 767 | static int quic_deallocate_dghdlrs(void) |
| 768 | { |
| 769 | int i; |
| 770 | |
| 771 | if (quic_dghdlrs) { |
| 772 | for (i = 0; i < global.nbthread; ++i) |
| 773 | tasklet_free(quic_dghdlrs[i].task); |
| 774 | free(quic_dghdlrs); |
| 775 | } |
| 776 | |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 777 | ha_free(&quic_cid_trees); |
| 778 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 779 | return 1; |
| 780 | } |
| 781 | REGISTER_POST_DEINIT(quic_deallocate_dghdlrs); |
Frédéric Lécaille | ca42b2c | 2020-11-02 14:27:08 +0100 | [diff] [blame] | 782 | |
| 783 | /* |
| 784 | * Local variables: |
| 785 | * c-indent-level: 8 |
| 786 | * c-basic-offset: 8 |
| 787 | * End: |
| 788 | */ |