blob: b7d50df36d7bc0970e2763bdeccdd4b11ee4bc35 [file] [log] [blame]
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +01001/*
2 * AF_INET/AF_INET6 QUIC protocol layer.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +01005 *
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>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24
25#include <netinet/udp.h>
26#include <netinet/in.h>
27
28#include <haproxy/api.h>
29#include <haproxy/arg.h>
Frédéric Lécaille48f8e192021-07-06 15:39:26 +020030#include <haproxy/cbuf.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010031#include <haproxy/connection.h>
32#include <haproxy/errors.h>
33#include <haproxy/fd.h>
34#include <haproxy/global.h>
35#include <haproxy/list.h>
36#include <haproxy/listener.h>
37#include <haproxy/log.h>
38#include <haproxy/namespace.h>
39#include <haproxy/port_range.h>
40#include <haproxy/protocol.h>
41#include <haproxy/proto_quic.h>
42#include <haproxy/proto_udp.h>
43#include <haproxy/proxy-t.h>
44#include <haproxy/sock.h>
Frédéric Lécaille70da8892020-11-06 15:49:49 +010045#include <haproxy/quic_sock.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010046#include <haproxy/sock_inet.h>
47#include <haproxy/tools.h>
Frédéric Lécaille25bc8872022-01-27 09:15:40 +010048#include <haproxy/xprt_quic.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010049
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010050/* per-thread quic datagram handlers */
51struct quic_dghdlr *quic_dghdlrs;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010052
Frédéric Lécaille71f3abb2022-02-15 16:59:48 +010053/* Size of the internal buffer of QUIC RX buffer at the fd level */
54#define QUIC_RX_BUFSZ (1UL << 18)
55
56DECLARE_STATIC_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
57
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010058static void quic_add_listener(struct protocol *proto, struct listener *listener);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010059static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen);
60static int quic_connect_server(struct connection *conn, int flags);
61static void quic_enable_listener(struct listener *listener);
62static void quic_disable_listener(struct listener *listener);
63
64/* Note: must not be declared <const> as its list will be overwritten */
65struct protocol proto_quic4 = {
66 .name = "quic4",
67
68 /* connection layer */
69 .ctrl_type = SOCK_STREAM,
70 .listen = quic_bind_listener,
71 .enable = quic_enable_listener,
72 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010073 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010074 .unbind = default_unbind_listener,
75 .suspend = default_suspend_listener,
76 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010077 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +020078 .get_src = quic_sock_get_src,
79 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010080 .connect = quic_connect_server,
81
82 /* binding layer */
83 .rx_suspend = udp_suspend_receiver,
84 .rx_resume = udp_resume_receiver,
85
86 /* address family */
87 .fam = &proto_fam_inet4,
88
89 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020090 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010091 .sock_type = SOCK_DGRAM,
92 .sock_prot = IPPROTO_UDP,
93 .rx_enable = sock_enable,
94 .rx_disable = sock_disable,
95 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010096 .rx_listening = quic_sock_accepting_conn,
97 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010098 .receivers = LIST_HEAD_INIT(proto_quic4.receivers),
99 .nb_receivers = 0,
100};
101
102INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
103
104/* Note: must not be declared <const> as its list will be overwritten */
105struct protocol proto_quic6 = {
106 .name = "quic6",
107
108 /* connection layer */
109 .ctrl_type = SOCK_STREAM,
110 .listen = quic_bind_listener,
111 .enable = quic_enable_listener,
112 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100113 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100114 .unbind = default_unbind_listener,
115 .suspend = default_suspend_listener,
116 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100117 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +0200118 .get_src = quic_sock_get_src,
119 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100120 .connect = quic_connect_server,
121
122 /* binding layer */
123 .rx_suspend = udp_suspend_receiver,
124 .rx_resume = udp_resume_receiver,
125
126 /* address family */
127 .fam = &proto_fam_inet6,
128
129 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200130 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100131 .sock_type = SOCK_DGRAM,
132 .sock_prot = IPPROTO_UDP,
133 .rx_enable = sock_enable,
134 .rx_disable = sock_disable,
135 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100136 .rx_listening = quic_sock_accepting_conn,
137 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100138 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
139 .nb_receivers = 0,
140};
141
142INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
143
144/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
145 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
146 * - 0 : ignore remote address (may even be a NULL pointer)
147 * - 1 : use provided address
148 * - 2 : use provided port
149 * - 3 : use both
150 *
151 * The function supports multiple foreign binding methods :
152 * - linux_tproxy: we directly bind to the foreign address
153 * The second one can be used as a fallback for the first one.
154 * This function returns 0 when everything's OK, 1 if it could not bind, to the
155 * local address, 2 if it could not bind to the foreign address.
156 */
157int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
158{
159 struct sockaddr_storage bind_addr;
160 int foreign_ok = 0;
161 int ret;
162 static THREAD_LOCAL int ip_transp_working = 1;
163 static THREAD_LOCAL int ip6_transp_working = 1;
164
165 switch (local->ss_family) {
166 case AF_INET:
167 if (flags && ip_transp_working) {
168 /* This deserves some explanation. Some platforms will support
169 * multiple combinations of certain methods, so we try the
170 * supported ones until one succeeds.
171 */
172 if (sock_inet4_make_foreign(fd))
173 foreign_ok = 1;
174 else
175 ip_transp_working = 0;
176 }
177 break;
178 case AF_INET6:
179 if (flags && ip6_transp_working) {
180 if (sock_inet6_make_foreign(fd))
181 foreign_ok = 1;
182 else
183 ip6_transp_working = 0;
184 }
185 break;
186 }
187
188 if (flags) {
189 memset(&bind_addr, 0, sizeof(bind_addr));
190 bind_addr.ss_family = remote->ss_family;
191 switch (remote->ss_family) {
192 case AF_INET:
193 if (flags & 1)
194 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
195 if (flags & 2)
196 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
197 break;
198 case AF_INET6:
199 if (flags & 1)
200 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
201 if (flags & 2)
202 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
203 break;
204 default:
205 /* we don't want to try to bind to an unknown address family */
206 foreign_ok = 0;
207 }
208 }
209
210 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
211 if (foreign_ok) {
212 if (is_inet_addr(&bind_addr)) {
213 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
214 if (ret < 0)
215 return 2;
216 }
217 }
218 else {
219 if (is_inet_addr(local)) {
220 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
221 if (ret < 0)
222 return 1;
223 }
224 }
225
226 if (!flags)
227 return 0;
228
229 if (!foreign_ok)
230 /* we could not bind to a foreign address */
231 return 2;
232
233 return 0;
234}
235
236/*
237 * This function initiates a QUIC connection establishment to the target assigned
238 * to connection <conn> using (si->{target,dst}). A source address may be
239 * pointed to by conn->src in case of transparent proxying. Normal source
240 * bind addresses are still determined locally (due to the possible need of a
241 * source port). conn->target may point either to a valid server or to a backend,
242 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
243 * supported. The <data> parameter is a boolean indicating whether there are data
244 * waiting for being sent or not, in order to adjust data write polling and on
245 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
246 * is not used.
247 *
248 * Note that a pending send_proxy message accounts for data.
249 *
250 * It can return one of :
251 * - SF_ERR_NONE if everything's OK
252 * - SF_ERR_SRVTO if there are no more servers
253 * - SF_ERR_SRVCL if the connection was refused by the server
254 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
255 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
256 * - SF_ERR_INTERNAL for any other purely internal errors
257 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
258 *
259 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
260 * it's invalid and the caller has nothing to do.
261 */
262
263int quic_connect_server(struct connection *conn, int flags)
264{
265 int fd;
266 struct server *srv;
267 struct proxy *be;
268 struct conn_src *src;
269 struct sockaddr_storage *addr;
270
271 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
272
273 switch (obj_type(conn->target)) {
274 case OBJ_TYPE_PROXY:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100275 be = __objt_proxy(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100276 srv = NULL;
277 break;
278 case OBJ_TYPE_SERVER:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100279 srv = __objt_server(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100280 be = srv->proxy;
281 break;
282 default:
283 conn->flags |= CO_FL_ERROR;
284 return SF_ERR_INTERNAL;
285 }
286
287 if (!conn->dst) {
288 conn->flags |= CO_FL_ERROR;
289 return SF_ERR_INTERNAL;
290 }
291
292 fd = conn->handle.fd = sock_create_server_socket(conn);
293
294 if (fd == -1) {
295 qfprintf(stderr, "Cannot get a server socket.\n");
296
297 if (errno == ENFILE) {
298 conn->err_code = CO_ER_SYS_FDLIM;
299 send_log(be, LOG_EMERG,
300 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
301 be->id, global.maxsock);
302 }
303 else if (errno == EMFILE) {
304 conn->err_code = CO_ER_PROC_FDLIM;
305 send_log(be, LOG_EMERG,
306 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
307 be->id, global.maxsock);
308 }
309 else if (errno == ENOBUFS || errno == ENOMEM) {
310 conn->err_code = CO_ER_SYS_MEMLIM;
311 send_log(be, LOG_EMERG,
312 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
313 be->id, global.maxsock);
314 }
315 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
316 conn->err_code = CO_ER_NOPROTO;
317 }
318 else
319 conn->err_code = CO_ER_SOCK_ERR;
320
321 /* this is a resource error */
322 conn->flags |= CO_FL_ERROR;
323 return SF_ERR_RESOURCE;
324 }
325
326 if (fd >= global.maxsock) {
327 /* do not log anything there, it's a normal condition when this option
328 * is used to serialize connections to a server !
329 */
330 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
331 close(fd);
332 conn->err_code = CO_ER_CONF_FDLIM;
333 conn->flags |= CO_FL_ERROR;
334 return SF_ERR_PRXCOND; /* it is a configuration limit */
335 }
336
337 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1)) {
338 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
339 close(fd);
340 conn->err_code = CO_ER_SOCK_ERR;
341 conn->flags |= CO_FL_ERROR;
342 return SF_ERR_INTERNAL;
343 }
344
345 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
346 ha_alert("Cannot set CLOEXEC on client socket.\n");
347 close(fd);
348 conn->err_code = CO_ER_SOCK_ERR;
349 conn->flags |= CO_FL_ERROR;
350 return SF_ERR_INTERNAL;
351 }
352
353 /* allow specific binding :
354 * - server-specific at first
355 * - proxy-specific next
356 */
357 if (srv && srv->conn_src.opts & CO_SRC_BIND)
358 src = &srv->conn_src;
359 else if (be->conn_src.opts & CO_SRC_BIND)
360 src = &be->conn_src;
361 else
362 src = NULL;
363
364 if (src) {
365 int ret, flags = 0;
366
367 if (conn->src && is_inet_addr(conn->src)) {
368 switch (src->opts & CO_SRC_TPROXY_MASK) {
369 case CO_SRC_TPROXY_CLI:
370 conn_set_private(conn);
371 /* fall through */
372 case CO_SRC_TPROXY_ADDR:
373 flags = 3;
374 break;
375 case CO_SRC_TPROXY_CIP:
376 case CO_SRC_TPROXY_DYN:
377 conn_set_private(conn);
378 flags = 1;
379 break;
380 }
381 }
382
383#ifdef SO_BINDTODEVICE
384 /* Note: this might fail if not CAP_NET_RAW */
385 if (src->iface_name)
386 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
387#endif
388
389 if (src->sport_range) {
390 int attempts = 10; /* should be more than enough to find a spare port */
391 struct sockaddr_storage sa;
392
393 ret = 1;
394 memcpy(&sa, &src->source_addr, sizeof(sa));
395
396 do {
397 /* note: in case of retry, we may have to release a previously
398 * allocated port, hence this loop's construct.
399 */
400 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
401 fdinfo[fd].port_range = NULL;
402
403 if (!attempts)
404 break;
405 attempts--;
406
407 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
408 if (!fdinfo[fd].local_port) {
409 conn->err_code = CO_ER_PORT_RANGE;
410 break;
411 }
412
413 fdinfo[fd].port_range = src->sport_range;
414 set_host_port(&sa, fdinfo[fd].local_port);
415
416 ret = quic_bind_socket(fd, flags, &sa, conn->src);
417 if (ret != 0)
418 conn->err_code = CO_ER_CANT_BIND;
419 } while (ret != 0); /* binding NOK */
420 }
421 else {
422#ifdef IP_BIND_ADDRESS_NO_PORT
423 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200424 setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100425#endif
426 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
427 if (ret != 0)
428 conn->err_code = CO_ER_CANT_BIND;
429 }
430
431 if (unlikely(ret != 0)) {
432 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
433 fdinfo[fd].port_range = NULL;
434 close(fd);
435
436 if (ret == 1) {
437 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
438 be->id);
439 send_log(be, LOG_EMERG,
440 "Cannot bind to source address before connect() for backend %s.\n",
441 be->id);
442 } else {
443 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
444 be->id);
445 send_log(be, LOG_EMERG,
446 "Cannot bind to tproxy source address before connect() for backend %s.\n",
447 be->id);
448 }
449 conn->flags |= CO_FL_ERROR;
450 return SF_ERR_RESOURCE;
451 }
452 }
453
454 if (global.tune.server_sndbuf)
455 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
456
457 if (global.tune.server_rcvbuf)
458 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
459
460 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
461 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
462 if (errno == EINPROGRESS || errno == EALREADY) {
463 /* common case, let's wait for connect status */
464 conn->flags |= CO_FL_WAIT_L4_CONN;
465 }
466 else if (errno == EISCONN) {
467 /* should normally not happen but if so, indicates that it's OK */
468 conn->flags &= ~CO_FL_WAIT_L4_CONN;
469 }
470 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
471 char *msg;
472 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
473 msg = "no free ports";
474 conn->err_code = CO_ER_FREE_PORTS;
475 }
476 else {
477 msg = "local address already in use";
478 conn->err_code = CO_ER_ADDR_INUSE;
479 }
480
481 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
482 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
483 fdinfo[fd].port_range = NULL;
484 close(fd);
485 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
486 conn->flags |= CO_FL_ERROR;
487 return SF_ERR_RESOURCE;
488 } else if (errno == ETIMEDOUT) {
489 //qfprintf(stderr,"Connect(): ETIMEDOUT");
490 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
491 fdinfo[fd].port_range = NULL;
492 close(fd);
493 conn->err_code = CO_ER_SOCK_ERR;
494 conn->flags |= CO_FL_ERROR;
495 return SF_ERR_SRVTO;
496 } else {
497 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
498 //qfprintf(stderr,"Connect(): %d", errno);
499 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
500 fdinfo[fd].port_range = NULL;
501 close(fd);
502 conn->err_code = CO_ER_SOCK_ERR;
503 conn->flags |= CO_FL_ERROR;
504 return SF_ERR_SRVCL;
505 }
506 }
507 else {
508 /* connect() == 0, this is great! */
509 conn->flags &= ~CO_FL_WAIT_L4_CONN;
510 }
511
512 conn->flags |= CO_FL_ADDR_TO_SET;
513
514 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200515 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100516
517 if (conn->flags & CO_FL_WAIT_L4_CONN) {
518 fd_want_send(fd);
519 fd_cant_send(fd);
520 fd_cant_recv(fd);
521 }
522
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100523 return SF_ERR_NONE; /* connection is OK */
524}
525
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100526/* Add listener <listener> to protocol <proto>. Technically speaking we just
527 * initialize a few entries which should be doable during quic_bind_listener().
528 * The end of the initialization goes on with the default function.
529 */
530static void quic_add_listener(struct protocol *proto, struct listener *listener)
531{
Amaury Denoyelleb59b8892022-01-25 17:48:47 +0100532 listener->flags |= LI_F_QUIC_LISTENER;
Amaury Denoyelle683b5fc2022-01-26 11:56:48 +0100533 listener->rx.flags |= RX_F_LOCAL_ACCEPT;
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100534
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100535 default_add_listener(proto, listener);
536}
537
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200538/* Allocate the TX ring buffers for <l> listener.
539 * Return 1 if succeeded, 0 if not.
540 */
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200541static int quic_alloc_tx_rings_listener(struct listener *l)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200542{
543 struct qring *qr;
544 int i;
545
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200546 l->rx.tx_qrings = calloc(global.nbthread, sizeof *l->rx.tx_qrings);
547 if (!l->rx.tx_qrings)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200548 return 0;
549
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200550 MT_LIST_INIT(&l->rx.tx_qring_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200551 for (i = 0; i < global.nbthread; i++) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200552 unsigned char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100553 struct qring *qr;
554
555 qr = calloc(1, sizeof *qr);
556 if (!qr)
557 goto err;
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200558
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200559 buf = pool_alloc(pool_head_quic_tx_ring);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100560 if (!buf) {
561 free(qr);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200562 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100563 }
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200564
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200565 qr->cbuf = cbuf_new(buf, QUIC_TX_RING_BUFSZ);
566 if (!qr->cbuf) {
567 pool_free(pool_head_quic_tx_ring, buf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100568 free(qr);
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200569 goto err;
570 }
571
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100572 l->rx.tx_qrings[i] = qr;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200573 MT_LIST_APPEND(&l->rx.tx_qring_list, &qr->mt_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200574 }
575
576 return 1;
577
578 err:
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200579 while ((qr = MT_LIST_POP(&l->rx.tx_qring_list, typeof(qr), mt_list))) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200580 pool_free(pool_head_quic_tx_ring, qr->cbuf->buf);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200581 cbuf_free(qr->cbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100582 free(qr);
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200583 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200584 free(l->rx.tx_qrings);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200585 return 0;
586}
587
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200588/* Allocate the RX buffers for <l> listener.
589 * Return 1 if succeeded, 0 if not.
590 */
591static int quic_alloc_rxbufs_listener(struct listener *l)
592{
593 int i;
594 struct rxbuf *rxbuf;
595
596 l->rx.rxbufs = calloc(global.nbthread, sizeof *l->rx.rxbufs);
597 if (!l->rx.rxbufs)
598 return 0;
599
600 MT_LIST_INIT(&l->rx.rxbuf_list);
601 for (i = 0; i < global.nbthread; i++) {
602 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100603 struct rxbuf *rxbuf;
604
605 rxbuf = calloc(1, sizeof *rxbuf);
606 if (!rxbuf)
607 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200608
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200609 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100610 if (!buf) {
611 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200612 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100613 }
614
615 l->rx.rxbufs[i] = rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200616
617 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Frédéric Lécaille53898bb2022-01-26 15:55:21 +0100618 LIST_INIT(&rxbuf->dgrams);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200619 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list);
620 }
621
622 return 1;
623
624 err:
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100625 while ((rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list))) {
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200626 pool_free(pool_head_quic_rxbuf, rxbuf->buf.area);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100627 free(rxbuf);
628 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200629 free(l->rx.rxbufs);
630 return 0;
631}
632
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100633/* This function tries to bind a QUIC4/6 listener. It may return a warning or
634 * an error message in <errmsg> if the message is at most <errlen> bytes long
635 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
636 * The return value is composed from ERR_ABORT, ERR_WARN,
637 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
638 * was alright and that no message was returned. ERR_RETRYABLE means that an
639 * error occurred but that it may vanish after a retry (eg: port in use), and
640 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
641 * the meaning of the error, but just indicate that a message is present which
642 * should be displayed with the respective level. Last, ERR_ABORT indicates
643 * that it's pointless to try to start other listeners. No error message is
644 * returned if errlen is NULL.
645 */
646static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
647{
648 int err = ERR_NONE;
649 char *msg = NULL;
650
651 /* ensure we never return garbage */
652 if (errlen)
653 *errmsg = 0;
654
655 if (listener->state != LI_ASSIGNED)
656 return ERR_NONE; /* already bound */
657
658 if (!(listener->rx.flags & RX_F_BOUND)) {
659 msg = "receiving socket not bound";
660 goto udp_return;
661 }
662
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200663 if (!quic_alloc_tx_rings_listener(listener) ||
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100664 !quic_alloc_rxbufs_listener(listener)) {
665 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200666 err |= ERR_WARN;
667 goto udp_return;
668 }
669
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100670 listener_set_state(listener, LI_LISTEN);
671
672 udp_return:
673 if (msg && errlen) {
674 char pn[INET6_ADDRSTRLEN];
675
676 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200677 snprintf(errmsg, errlen, "%s for [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100678 }
679 return err;
680}
681
682/* Enable receipt of incoming connections for listener <l>. The receiver must
683 * still be valid. Does nothing in early boot (needs fd_updt).
684 */
685static void quic_enable_listener(struct listener *l)
686{
687 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500688 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100689 * connections.
690 */
691 if (fd_updt)
692 fd_want_recv(l->rx.fd);
693}
694
695/* Disable receipt of incoming connections for listener <l>. The receiver must
696 * still be valid. Does nothing in early boot (needs fd_updt).
697 */
698static void quic_disable_listener(struct listener *l)
699{
700 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500701 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100702 * connections again.
703 */
704 if (fd_updt)
705 fd_stop_recv(l->rx.fd);
706}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100707
708static int quic_alloc_dghdlrs(void)
709{
710 int i;
711
712 quic_dghdlrs = calloc(global.nbthread, sizeof(struct quic_dghdlr));
713 if (!quic_dghdlrs) {
714 ha_alert("Failed to allocate the quic datagram handlers.\n");
715 return 0;
716 }
717
718 for (i = 0; i < global.nbthread; i++) {
719 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
720
721 dghdlr->task = tasklet_new();
722 if (!dghdlr->task) {
723 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
724 return 0;
725 }
726
727 tasklet_set_tid(dghdlr->task, i);
728 dghdlr->task->context = dghdlr;
729 dghdlr->task->process = quic_lstnr_dghdlr;
730
731 dghdlr->odcids = EB_ROOT_UNIQUE;
732 dghdlr->cids = EB_ROOT_UNIQUE;
733
734 MT_LIST_INIT(&dghdlr->dgrams);
735 }
736
737 return 1;
738}
739REGISTER_POST_CHECK(quic_alloc_dghdlrs);
740
741static int quic_deallocate_dghdlrs(void)
742{
743 int i;
744
745 if (quic_dghdlrs) {
746 for (i = 0; i < global.nbthread; ++i)
747 tasklet_free(quic_dghdlrs[i].task);
748 free(quic_dghdlrs);
749 }
750
751 return 1;
752}
753REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100754
755/*
756 * Local variables:
757 * c-indent-level: 8
758 * c-basic-offset: 8
759 * End:
760 */