blob: dd37c7655c0c9bd9a182febe2969203120af06a5 [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,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010078 .connect = quic_connect_server,
79
80 /* binding layer */
81 .rx_suspend = udp_suspend_receiver,
82 .rx_resume = udp_resume_receiver,
83
84 /* address family */
85 .fam = &proto_fam_inet4,
86
87 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020088 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010089 .sock_type = SOCK_DGRAM,
90 .sock_prot = IPPROTO_UDP,
91 .rx_enable = sock_enable,
92 .rx_disable = sock_disable,
93 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010094 .rx_listening = quic_sock_accepting_conn,
95 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010096 .receivers = LIST_HEAD_INIT(proto_quic4.receivers),
97 .nb_receivers = 0,
98};
99
100INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
101
102/* Note: must not be declared <const> as its list will be overwritten */
103struct protocol proto_quic6 = {
104 .name = "quic6",
105
106 /* connection layer */
107 .ctrl_type = SOCK_STREAM,
108 .listen = quic_bind_listener,
109 .enable = quic_enable_listener,
110 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100111 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100112 .unbind = default_unbind_listener,
113 .suspend = default_suspend_listener,
114 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100115 .accept_conn = quic_sock_accept_conn,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100116 .connect = quic_connect_server,
117
118 /* binding layer */
119 .rx_suspend = udp_suspend_receiver,
120 .rx_resume = udp_resume_receiver,
121
122 /* address family */
123 .fam = &proto_fam_inet6,
124
125 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200126 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100127 .sock_type = SOCK_DGRAM,
128 .sock_prot = IPPROTO_UDP,
129 .rx_enable = sock_enable,
130 .rx_disable = sock_disable,
131 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100132 .rx_listening = quic_sock_accepting_conn,
133 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100134 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
135 .nb_receivers = 0,
136};
137
138INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
139
140/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
141 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
142 * - 0 : ignore remote address (may even be a NULL pointer)
143 * - 1 : use provided address
144 * - 2 : use provided port
145 * - 3 : use both
146 *
147 * The function supports multiple foreign binding methods :
148 * - linux_tproxy: we directly bind to the foreign address
149 * The second one can be used as a fallback for the first one.
150 * This function returns 0 when everything's OK, 1 if it could not bind, to the
151 * local address, 2 if it could not bind to the foreign address.
152 */
153int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
154{
155 struct sockaddr_storage bind_addr;
156 int foreign_ok = 0;
157 int ret;
158 static THREAD_LOCAL int ip_transp_working = 1;
159 static THREAD_LOCAL int ip6_transp_working = 1;
160
161 switch (local->ss_family) {
162 case AF_INET:
163 if (flags && ip_transp_working) {
164 /* This deserves some explanation. Some platforms will support
165 * multiple combinations of certain methods, so we try the
166 * supported ones until one succeeds.
167 */
168 if (sock_inet4_make_foreign(fd))
169 foreign_ok = 1;
170 else
171 ip_transp_working = 0;
172 }
173 break;
174 case AF_INET6:
175 if (flags && ip6_transp_working) {
176 if (sock_inet6_make_foreign(fd))
177 foreign_ok = 1;
178 else
179 ip6_transp_working = 0;
180 }
181 break;
182 }
183
184 if (flags) {
185 memset(&bind_addr, 0, sizeof(bind_addr));
186 bind_addr.ss_family = remote->ss_family;
187 switch (remote->ss_family) {
188 case AF_INET:
189 if (flags & 1)
190 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
191 if (flags & 2)
192 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
193 break;
194 case AF_INET6:
195 if (flags & 1)
196 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
197 if (flags & 2)
198 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
199 break;
200 default:
201 /* we don't want to try to bind to an unknown address family */
202 foreign_ok = 0;
203 }
204 }
205
206 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
207 if (foreign_ok) {
208 if (is_inet_addr(&bind_addr)) {
209 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
210 if (ret < 0)
211 return 2;
212 }
213 }
214 else {
215 if (is_inet_addr(local)) {
216 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
217 if (ret < 0)
218 return 1;
219 }
220 }
221
222 if (!flags)
223 return 0;
224
225 if (!foreign_ok)
226 /* we could not bind to a foreign address */
227 return 2;
228
229 return 0;
230}
231
232/*
233 * This function initiates a QUIC connection establishment to the target assigned
234 * to connection <conn> using (si->{target,dst}). A source address may be
235 * pointed to by conn->src in case of transparent proxying. Normal source
236 * bind addresses are still determined locally (due to the possible need of a
237 * source port). conn->target may point either to a valid server or to a backend,
238 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
239 * supported. The <data> parameter is a boolean indicating whether there are data
240 * waiting for being sent or not, in order to adjust data write polling and on
241 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
242 * is not used.
243 *
244 * Note that a pending send_proxy message accounts for data.
245 *
246 * It can return one of :
247 * - SF_ERR_NONE if everything's OK
248 * - SF_ERR_SRVTO if there are no more servers
249 * - SF_ERR_SRVCL if the connection was refused by the server
250 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
251 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
252 * - SF_ERR_INTERNAL for any other purely internal errors
253 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
254 *
255 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
256 * it's invalid and the caller has nothing to do.
257 */
258
259int quic_connect_server(struct connection *conn, int flags)
260{
261 int fd;
262 struct server *srv;
263 struct proxy *be;
264 struct conn_src *src;
265 struct sockaddr_storage *addr;
266
267 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
268
269 switch (obj_type(conn->target)) {
270 case OBJ_TYPE_PROXY:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100271 be = __objt_proxy(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100272 srv = NULL;
273 break;
274 case OBJ_TYPE_SERVER:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100275 srv = __objt_server(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100276 be = srv->proxy;
277 break;
278 default:
279 conn->flags |= CO_FL_ERROR;
280 return SF_ERR_INTERNAL;
281 }
282
283 if (!conn->dst) {
284 conn->flags |= CO_FL_ERROR;
285 return SF_ERR_INTERNAL;
286 }
287
288 fd = conn->handle.fd = sock_create_server_socket(conn);
289
290 if (fd == -1) {
291 qfprintf(stderr, "Cannot get a server socket.\n");
292
293 if (errno == ENFILE) {
294 conn->err_code = CO_ER_SYS_FDLIM;
295 send_log(be, LOG_EMERG,
296 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
297 be->id, global.maxsock);
298 }
299 else if (errno == EMFILE) {
300 conn->err_code = CO_ER_PROC_FDLIM;
301 send_log(be, LOG_EMERG,
302 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
303 be->id, global.maxsock);
304 }
305 else if (errno == ENOBUFS || errno == ENOMEM) {
306 conn->err_code = CO_ER_SYS_MEMLIM;
307 send_log(be, LOG_EMERG,
308 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
309 be->id, global.maxsock);
310 }
311 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
312 conn->err_code = CO_ER_NOPROTO;
313 }
314 else
315 conn->err_code = CO_ER_SOCK_ERR;
316
317 /* this is a resource error */
318 conn->flags |= CO_FL_ERROR;
319 return SF_ERR_RESOURCE;
320 }
321
322 if (fd >= global.maxsock) {
323 /* do not log anything there, it's a normal condition when this option
324 * is used to serialize connections to a server !
325 */
326 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
327 close(fd);
328 conn->err_code = CO_ER_CONF_FDLIM;
329 conn->flags |= CO_FL_ERROR;
330 return SF_ERR_PRXCOND; /* it is a configuration limit */
331 }
332
333 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1)) {
334 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
335 close(fd);
336 conn->err_code = CO_ER_SOCK_ERR;
337 conn->flags |= CO_FL_ERROR;
338 return SF_ERR_INTERNAL;
339 }
340
341 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
342 ha_alert("Cannot set CLOEXEC on client socket.\n");
343 close(fd);
344 conn->err_code = CO_ER_SOCK_ERR;
345 conn->flags |= CO_FL_ERROR;
346 return SF_ERR_INTERNAL;
347 }
348
349 /* allow specific binding :
350 * - server-specific at first
351 * - proxy-specific next
352 */
353 if (srv && srv->conn_src.opts & CO_SRC_BIND)
354 src = &srv->conn_src;
355 else if (be->conn_src.opts & CO_SRC_BIND)
356 src = &be->conn_src;
357 else
358 src = NULL;
359
360 if (src) {
361 int ret, flags = 0;
362
363 if (conn->src && is_inet_addr(conn->src)) {
364 switch (src->opts & CO_SRC_TPROXY_MASK) {
365 case CO_SRC_TPROXY_CLI:
366 conn_set_private(conn);
367 /* fall through */
368 case CO_SRC_TPROXY_ADDR:
369 flags = 3;
370 break;
371 case CO_SRC_TPROXY_CIP:
372 case CO_SRC_TPROXY_DYN:
373 conn_set_private(conn);
374 flags = 1;
375 break;
376 }
377 }
378
379#ifdef SO_BINDTODEVICE
380 /* Note: this might fail if not CAP_NET_RAW */
381 if (src->iface_name)
382 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
383#endif
384
385 if (src->sport_range) {
386 int attempts = 10; /* should be more than enough to find a spare port */
387 struct sockaddr_storage sa;
388
389 ret = 1;
390 memcpy(&sa, &src->source_addr, sizeof(sa));
391
392 do {
393 /* note: in case of retry, we may have to release a previously
394 * allocated port, hence this loop's construct.
395 */
396 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
397 fdinfo[fd].port_range = NULL;
398
399 if (!attempts)
400 break;
401 attempts--;
402
403 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
404 if (!fdinfo[fd].local_port) {
405 conn->err_code = CO_ER_PORT_RANGE;
406 break;
407 }
408
409 fdinfo[fd].port_range = src->sport_range;
410 set_host_port(&sa, fdinfo[fd].local_port);
411
412 ret = quic_bind_socket(fd, flags, &sa, conn->src);
413 if (ret != 0)
414 conn->err_code = CO_ER_CANT_BIND;
415 } while (ret != 0); /* binding NOK */
416 }
417 else {
418#ifdef IP_BIND_ADDRESS_NO_PORT
419 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200420 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 +0100421#endif
422 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
423 if (ret != 0)
424 conn->err_code = CO_ER_CANT_BIND;
425 }
426
427 if (unlikely(ret != 0)) {
428 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
429 fdinfo[fd].port_range = NULL;
430 close(fd);
431
432 if (ret == 1) {
433 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
434 be->id);
435 send_log(be, LOG_EMERG,
436 "Cannot bind to source address before connect() for backend %s.\n",
437 be->id);
438 } else {
439 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
440 be->id);
441 send_log(be, LOG_EMERG,
442 "Cannot bind to tproxy source address before connect() for backend %s.\n",
443 be->id);
444 }
445 conn->flags |= CO_FL_ERROR;
446 return SF_ERR_RESOURCE;
447 }
448 }
449
450 if (global.tune.server_sndbuf)
451 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
452
453 if (global.tune.server_rcvbuf)
454 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
455
456 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
457 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
458 if (errno == EINPROGRESS || errno == EALREADY) {
459 /* common case, let's wait for connect status */
460 conn->flags |= CO_FL_WAIT_L4_CONN;
461 }
462 else if (errno == EISCONN) {
463 /* should normally not happen but if so, indicates that it's OK */
464 conn->flags &= ~CO_FL_WAIT_L4_CONN;
465 }
466 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
467 char *msg;
468 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
469 msg = "no free ports";
470 conn->err_code = CO_ER_FREE_PORTS;
471 }
472 else {
473 msg = "local address already in use";
474 conn->err_code = CO_ER_ADDR_INUSE;
475 }
476
477 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
478 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
479 fdinfo[fd].port_range = NULL;
480 close(fd);
481 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
482 conn->flags |= CO_FL_ERROR;
483 return SF_ERR_RESOURCE;
484 } else if (errno == ETIMEDOUT) {
485 //qfprintf(stderr,"Connect(): ETIMEDOUT");
486 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
487 fdinfo[fd].port_range = NULL;
488 close(fd);
489 conn->err_code = CO_ER_SOCK_ERR;
490 conn->flags |= CO_FL_ERROR;
491 return SF_ERR_SRVTO;
492 } else {
493 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
494 //qfprintf(stderr,"Connect(): %d", errno);
495 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
496 fdinfo[fd].port_range = NULL;
497 close(fd);
498 conn->err_code = CO_ER_SOCK_ERR;
499 conn->flags |= CO_FL_ERROR;
500 return SF_ERR_SRVCL;
501 }
502 }
503 else {
504 /* connect() == 0, this is great! */
505 conn->flags &= ~CO_FL_WAIT_L4_CONN;
506 }
507
508 conn->flags |= CO_FL_ADDR_TO_SET;
509
510 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200511 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100512
513 if (conn->flags & CO_FL_WAIT_L4_CONN) {
514 fd_want_send(fd);
515 fd_cant_send(fd);
516 fd_cant_recv(fd);
517 }
518
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100519 return SF_ERR_NONE; /* connection is OK */
520}
521
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100522/* Add listener <listener> to protocol <proto>. Technically speaking we just
523 * initialize a few entries which should be doable during quic_bind_listener().
524 * The end of the initialization goes on with the default function.
525 */
526static void quic_add_listener(struct protocol *proto, struct listener *listener)
527{
Amaury Denoyelleb59b8892022-01-25 17:48:47 +0100528 listener->flags |= LI_F_QUIC_LISTENER;
Amaury Denoyelle683b5fc2022-01-26 11:56:48 +0100529 listener->rx.flags |= RX_F_LOCAL_ACCEPT;
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100530
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100531 default_add_listener(proto, listener);
532}
533
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200534/* Allocate the TX ring buffers for <l> listener.
535 * Return 1 if succeeded, 0 if not.
536 */
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200537static int quic_alloc_tx_rings_listener(struct listener *l)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200538{
539 struct qring *qr;
540 int i;
541
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200542 l->rx.tx_qrings = calloc(global.nbthread, sizeof *l->rx.tx_qrings);
543 if (!l->rx.tx_qrings)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200544 return 0;
545
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200546 MT_LIST_INIT(&l->rx.tx_qring_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200547 for (i = 0; i < global.nbthread; i++) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200548 unsigned char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100549 struct qring *qr;
550
551 qr = calloc(1, sizeof *qr);
552 if (!qr)
553 goto err;
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200554
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200555 buf = pool_alloc(pool_head_quic_tx_ring);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100556 if (!buf) {
557 free(qr);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200558 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100559 }
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200560
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200561 qr->cbuf = cbuf_new(buf, QUIC_TX_RING_BUFSZ);
562 if (!qr->cbuf) {
563 pool_free(pool_head_quic_tx_ring, buf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100564 free(qr);
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200565 goto err;
566 }
567
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100568 l->rx.tx_qrings[i] = qr;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200569 MT_LIST_APPEND(&l->rx.tx_qring_list, &qr->mt_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200570 }
571
572 return 1;
573
574 err:
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200575 while ((qr = MT_LIST_POP(&l->rx.tx_qring_list, typeof(qr), mt_list))) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200576 pool_free(pool_head_quic_tx_ring, qr->cbuf->buf);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200577 cbuf_free(qr->cbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100578 free(qr);
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200579 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200580 free(l->rx.tx_qrings);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200581 return 0;
582}
583
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200584/* Allocate the RX buffers for <l> listener.
585 * Return 1 if succeeded, 0 if not.
586 */
587static int quic_alloc_rxbufs_listener(struct listener *l)
588{
589 int i;
590 struct rxbuf *rxbuf;
591
592 l->rx.rxbufs = calloc(global.nbthread, sizeof *l->rx.rxbufs);
593 if (!l->rx.rxbufs)
594 return 0;
595
596 MT_LIST_INIT(&l->rx.rxbuf_list);
597 for (i = 0; i < global.nbthread; i++) {
598 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100599 struct rxbuf *rxbuf;
600
601 rxbuf = calloc(1, sizeof *rxbuf);
602 if (!rxbuf)
603 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200604
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200605 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100606 if (!buf) {
607 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200608 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100609 }
610
611 l->rx.rxbufs[i] = rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200612
613 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Frédéric Lécaille53898bb2022-01-26 15:55:21 +0100614 LIST_INIT(&rxbuf->dgrams);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200615 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list);
616 }
617
618 return 1;
619
620 err:
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100621 while ((rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list))) {
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200622 pool_free(pool_head_quic_rxbuf, rxbuf->buf.area);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100623 free(rxbuf);
624 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200625 free(l->rx.rxbufs);
626 return 0;
627}
628
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100629/* This function tries to bind a QUIC4/6 listener. It may return a warning or
630 * an error message in <errmsg> if the message is at most <errlen> bytes long
631 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
632 * The return value is composed from ERR_ABORT, ERR_WARN,
633 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
634 * was alright and that no message was returned. ERR_RETRYABLE means that an
635 * error occurred but that it may vanish after a retry (eg: port in use), and
636 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
637 * the meaning of the error, but just indicate that a message is present which
638 * should be displayed with the respective level. Last, ERR_ABORT indicates
639 * that it's pointless to try to start other listeners. No error message is
640 * returned if errlen is NULL.
641 */
642static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
643{
644 int err = ERR_NONE;
645 char *msg = NULL;
646
647 /* ensure we never return garbage */
648 if (errlen)
649 *errmsg = 0;
650
651 if (listener->state != LI_ASSIGNED)
652 return ERR_NONE; /* already bound */
653
654 if (!(listener->rx.flags & RX_F_BOUND)) {
655 msg = "receiving socket not bound";
656 goto udp_return;
657 }
658
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200659 if (!quic_alloc_tx_rings_listener(listener) ||
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100660 !quic_alloc_rxbufs_listener(listener)) {
661 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200662 err |= ERR_WARN;
663 goto udp_return;
664 }
665
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100666 listener_set_state(listener, LI_LISTEN);
667
668 udp_return:
669 if (msg && errlen) {
670 char pn[INET6_ADDRSTRLEN];
671
672 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200673 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 +0100674 }
675 return err;
676}
677
678/* Enable receipt of incoming connections for listener <l>. The receiver must
679 * still be valid. Does nothing in early boot (needs fd_updt).
680 */
681static void quic_enable_listener(struct listener *l)
682{
683 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500684 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100685 * connections.
686 */
687 if (fd_updt)
688 fd_want_recv(l->rx.fd);
689}
690
691/* Disable receipt of incoming connections for listener <l>. The receiver must
692 * still be valid. Does nothing in early boot (needs fd_updt).
693 */
694static void quic_disable_listener(struct listener *l)
695{
696 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500697 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100698 * connections again.
699 */
700 if (fd_updt)
701 fd_stop_recv(l->rx.fd);
702}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100703
704static int quic_alloc_dghdlrs(void)
705{
706 int i;
707
708 quic_dghdlrs = calloc(global.nbthread, sizeof(struct quic_dghdlr));
709 if (!quic_dghdlrs) {
710 ha_alert("Failed to allocate the quic datagram handlers.\n");
711 return 0;
712 }
713
714 for (i = 0; i < global.nbthread; i++) {
715 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
716
717 dghdlr->task = tasklet_new();
718 if (!dghdlr->task) {
719 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
720 return 0;
721 }
722
723 tasklet_set_tid(dghdlr->task, i);
724 dghdlr->task->context = dghdlr;
725 dghdlr->task->process = quic_lstnr_dghdlr;
726
727 dghdlr->odcids = EB_ROOT_UNIQUE;
728 dghdlr->cids = EB_ROOT_UNIQUE;
729
730 MT_LIST_INIT(&dghdlr->dgrams);
731 }
732
733 return 1;
734}
735REGISTER_POST_CHECK(quic_alloc_dghdlrs);
736
737static int quic_deallocate_dghdlrs(void)
738{
739 int i;
740
741 if (quic_dghdlrs) {
742 for (i = 0; i < global.nbthread; ++i)
743 tasklet_free(quic_dghdlrs[i].task);
744 free(quic_dghdlrs);
745 }
746
747 return 1;
748}
749REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100750
751/*
752 * Local variables:
753 * c-indent-level: 8
754 * c-basic-offset: 8
755 * End:
756 */