blob: 9c2bbd6ef76da0da11d460da89c9b4328b4db52a [file] [log] [blame]
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +01001/*
2 * AF_INET/AF_INET6 QUIC protocol layer.
3 *
4 * Copyright 2020 Frédéric Lécaille <flecaille@haproxy.com>
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>
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écaille48f8e192021-07-06 15:39:26 +020048#include <haproxy/xprt_quic-t.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010049
50
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010051static void quic_add_listener(struct protocol *proto, struct listener *listener);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010052static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen);
53static int quic_connect_server(struct connection *conn, int flags);
54static void quic_enable_listener(struct listener *listener);
55static void quic_disable_listener(struct listener *listener);
56
57/* Note: must not be declared <const> as its list will be overwritten */
58struct protocol proto_quic4 = {
59 .name = "quic4",
60
61 /* connection layer */
62 .ctrl_type = SOCK_STREAM,
63 .listen = quic_bind_listener,
64 .enable = quic_enable_listener,
65 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010066 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010067 .unbind = default_unbind_listener,
68 .suspend = default_suspend_listener,
69 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010070 .accept_conn = quic_sock_accept_conn,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010071 .connect = quic_connect_server,
72
73 /* binding layer */
74 .rx_suspend = udp_suspend_receiver,
75 .rx_resume = udp_resume_receiver,
76
77 /* address family */
78 .fam = &proto_fam_inet4,
79
80 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020081 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010082 .sock_type = SOCK_DGRAM,
83 .sock_prot = IPPROTO_UDP,
84 .rx_enable = sock_enable,
85 .rx_disable = sock_disable,
86 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010087 .rx_listening = quic_sock_accepting_conn,
88 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010089 .receivers = LIST_HEAD_INIT(proto_quic4.receivers),
90 .nb_receivers = 0,
91};
92
93INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
94
95/* Note: must not be declared <const> as its list will be overwritten */
96struct protocol proto_quic6 = {
97 .name = "quic6",
98
99 /* connection layer */
100 .ctrl_type = SOCK_STREAM,
101 .listen = quic_bind_listener,
102 .enable = quic_enable_listener,
103 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100104 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100105 .unbind = default_unbind_listener,
106 .suspend = default_suspend_listener,
107 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100108 .accept_conn = quic_sock_accept_conn,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100109 .connect = quic_connect_server,
110
111 /* binding layer */
112 .rx_suspend = udp_suspend_receiver,
113 .rx_resume = udp_resume_receiver,
114
115 /* address family */
116 .fam = &proto_fam_inet6,
117
118 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200119 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100120 .sock_type = SOCK_DGRAM,
121 .sock_prot = IPPROTO_UDP,
122 .rx_enable = sock_enable,
123 .rx_disable = sock_disable,
124 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100125 .rx_listening = quic_sock_accepting_conn,
126 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100127 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
128 .nb_receivers = 0,
129};
130
131INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
132
133/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
134 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
135 * - 0 : ignore remote address (may even be a NULL pointer)
136 * - 1 : use provided address
137 * - 2 : use provided port
138 * - 3 : use both
139 *
140 * The function supports multiple foreign binding methods :
141 * - linux_tproxy: we directly bind to the foreign address
142 * The second one can be used as a fallback for the first one.
143 * This function returns 0 when everything's OK, 1 if it could not bind, to the
144 * local address, 2 if it could not bind to the foreign address.
145 */
146int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
147{
148 struct sockaddr_storage bind_addr;
149 int foreign_ok = 0;
150 int ret;
151 static THREAD_LOCAL int ip_transp_working = 1;
152 static THREAD_LOCAL int ip6_transp_working = 1;
153
154 switch (local->ss_family) {
155 case AF_INET:
156 if (flags && ip_transp_working) {
157 /* This deserves some explanation. Some platforms will support
158 * multiple combinations of certain methods, so we try the
159 * supported ones until one succeeds.
160 */
161 if (sock_inet4_make_foreign(fd))
162 foreign_ok = 1;
163 else
164 ip_transp_working = 0;
165 }
166 break;
167 case AF_INET6:
168 if (flags && ip6_transp_working) {
169 if (sock_inet6_make_foreign(fd))
170 foreign_ok = 1;
171 else
172 ip6_transp_working = 0;
173 }
174 break;
175 }
176
177 if (flags) {
178 memset(&bind_addr, 0, sizeof(bind_addr));
179 bind_addr.ss_family = remote->ss_family;
180 switch (remote->ss_family) {
181 case AF_INET:
182 if (flags & 1)
183 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
184 if (flags & 2)
185 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
186 break;
187 case AF_INET6:
188 if (flags & 1)
189 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
190 if (flags & 2)
191 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
192 break;
193 default:
194 /* we don't want to try to bind to an unknown address family */
195 foreign_ok = 0;
196 }
197 }
198
199 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
200 if (foreign_ok) {
201 if (is_inet_addr(&bind_addr)) {
202 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
203 if (ret < 0)
204 return 2;
205 }
206 }
207 else {
208 if (is_inet_addr(local)) {
209 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
210 if (ret < 0)
211 return 1;
212 }
213 }
214
215 if (!flags)
216 return 0;
217
218 if (!foreign_ok)
219 /* we could not bind to a foreign address */
220 return 2;
221
222 return 0;
223}
224
225/*
226 * This function initiates a QUIC connection establishment to the target assigned
227 * to connection <conn> using (si->{target,dst}). A source address may be
228 * pointed to by conn->src in case of transparent proxying. Normal source
229 * bind addresses are still determined locally (due to the possible need of a
230 * source port). conn->target may point either to a valid server or to a backend,
231 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
232 * supported. The <data> parameter is a boolean indicating whether there are data
233 * waiting for being sent or not, in order to adjust data write polling and on
234 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
235 * is not used.
236 *
237 * Note that a pending send_proxy message accounts for data.
238 *
239 * It can return one of :
240 * - SF_ERR_NONE if everything's OK
241 * - SF_ERR_SRVTO if there are no more servers
242 * - SF_ERR_SRVCL if the connection was refused by the server
243 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
244 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
245 * - SF_ERR_INTERNAL for any other purely internal errors
246 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
247 *
248 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
249 * it's invalid and the caller has nothing to do.
250 */
251
252int quic_connect_server(struct connection *conn, int flags)
253{
254 int fd;
255 struct server *srv;
256 struct proxy *be;
257 struct conn_src *src;
258 struct sockaddr_storage *addr;
259
260 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
261
262 switch (obj_type(conn->target)) {
263 case OBJ_TYPE_PROXY:
264 be = objt_proxy(conn->target);
265 srv = NULL;
266 break;
267 case OBJ_TYPE_SERVER:
268 srv = objt_server(conn->target);
269 be = srv->proxy;
270 break;
271 default:
272 conn->flags |= CO_FL_ERROR;
273 return SF_ERR_INTERNAL;
274 }
275
276 if (!conn->dst) {
277 conn->flags |= CO_FL_ERROR;
278 return SF_ERR_INTERNAL;
279 }
280
281 fd = conn->handle.fd = sock_create_server_socket(conn);
282
283 if (fd == -1) {
284 qfprintf(stderr, "Cannot get a server socket.\n");
285
286 if (errno == ENFILE) {
287 conn->err_code = CO_ER_SYS_FDLIM;
288 send_log(be, LOG_EMERG,
289 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
290 be->id, global.maxsock);
291 }
292 else if (errno == EMFILE) {
293 conn->err_code = CO_ER_PROC_FDLIM;
294 send_log(be, LOG_EMERG,
295 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
296 be->id, global.maxsock);
297 }
298 else if (errno == ENOBUFS || errno == ENOMEM) {
299 conn->err_code = CO_ER_SYS_MEMLIM;
300 send_log(be, LOG_EMERG,
301 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
302 be->id, global.maxsock);
303 }
304 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
305 conn->err_code = CO_ER_NOPROTO;
306 }
307 else
308 conn->err_code = CO_ER_SOCK_ERR;
309
310 /* this is a resource error */
311 conn->flags |= CO_FL_ERROR;
312 return SF_ERR_RESOURCE;
313 }
314
315 if (fd >= global.maxsock) {
316 /* do not log anything there, it's a normal condition when this option
317 * is used to serialize connections to a server !
318 */
319 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
320 close(fd);
321 conn->err_code = CO_ER_CONF_FDLIM;
322 conn->flags |= CO_FL_ERROR;
323 return SF_ERR_PRXCOND; /* it is a configuration limit */
324 }
325
326 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1)) {
327 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
328 close(fd);
329 conn->err_code = CO_ER_SOCK_ERR;
330 conn->flags |= CO_FL_ERROR;
331 return SF_ERR_INTERNAL;
332 }
333
334 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
335 ha_alert("Cannot set CLOEXEC on client socket.\n");
336 close(fd);
337 conn->err_code = CO_ER_SOCK_ERR;
338 conn->flags |= CO_FL_ERROR;
339 return SF_ERR_INTERNAL;
340 }
341
342 /* allow specific binding :
343 * - server-specific at first
344 * - proxy-specific next
345 */
346 if (srv && srv->conn_src.opts & CO_SRC_BIND)
347 src = &srv->conn_src;
348 else if (be->conn_src.opts & CO_SRC_BIND)
349 src = &be->conn_src;
350 else
351 src = NULL;
352
353 if (src) {
354 int ret, flags = 0;
355
356 if (conn->src && is_inet_addr(conn->src)) {
357 switch (src->opts & CO_SRC_TPROXY_MASK) {
358 case CO_SRC_TPROXY_CLI:
359 conn_set_private(conn);
360 /* fall through */
361 case CO_SRC_TPROXY_ADDR:
362 flags = 3;
363 break;
364 case CO_SRC_TPROXY_CIP:
365 case CO_SRC_TPROXY_DYN:
366 conn_set_private(conn);
367 flags = 1;
368 break;
369 }
370 }
371
372#ifdef SO_BINDTODEVICE
373 /* Note: this might fail if not CAP_NET_RAW */
374 if (src->iface_name)
375 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
376#endif
377
378 if (src->sport_range) {
379 int attempts = 10; /* should be more than enough to find a spare port */
380 struct sockaddr_storage sa;
381
382 ret = 1;
383 memcpy(&sa, &src->source_addr, sizeof(sa));
384
385 do {
386 /* note: in case of retry, we may have to release a previously
387 * allocated port, hence this loop's construct.
388 */
389 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
390 fdinfo[fd].port_range = NULL;
391
392 if (!attempts)
393 break;
394 attempts--;
395
396 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
397 if (!fdinfo[fd].local_port) {
398 conn->err_code = CO_ER_PORT_RANGE;
399 break;
400 }
401
402 fdinfo[fd].port_range = src->sport_range;
403 set_host_port(&sa, fdinfo[fd].local_port);
404
405 ret = quic_bind_socket(fd, flags, &sa, conn->src);
406 if (ret != 0)
407 conn->err_code = CO_ER_CANT_BIND;
408 } while (ret != 0); /* binding NOK */
409 }
410 else {
411#ifdef IP_BIND_ADDRESS_NO_PORT
412 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200413 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 +0100414#endif
415 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
416 if (ret != 0)
417 conn->err_code = CO_ER_CANT_BIND;
418 }
419
420 if (unlikely(ret != 0)) {
421 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
422 fdinfo[fd].port_range = NULL;
423 close(fd);
424
425 if (ret == 1) {
426 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
427 be->id);
428 send_log(be, LOG_EMERG,
429 "Cannot bind to source address before connect() for backend %s.\n",
430 be->id);
431 } else {
432 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
433 be->id);
434 send_log(be, LOG_EMERG,
435 "Cannot bind to tproxy source address before connect() for backend %s.\n",
436 be->id);
437 }
438 conn->flags |= CO_FL_ERROR;
439 return SF_ERR_RESOURCE;
440 }
441 }
442
443 if (global.tune.server_sndbuf)
444 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
445
446 if (global.tune.server_rcvbuf)
447 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
448
449 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
450 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
451 if (errno == EINPROGRESS || errno == EALREADY) {
452 /* common case, let's wait for connect status */
453 conn->flags |= CO_FL_WAIT_L4_CONN;
454 }
455 else if (errno == EISCONN) {
456 /* should normally not happen but if so, indicates that it's OK */
457 conn->flags &= ~CO_FL_WAIT_L4_CONN;
458 }
459 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
460 char *msg;
461 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
462 msg = "no free ports";
463 conn->err_code = CO_ER_FREE_PORTS;
464 }
465 else {
466 msg = "local address already in use";
467 conn->err_code = CO_ER_ADDR_INUSE;
468 }
469
470 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
471 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
472 fdinfo[fd].port_range = NULL;
473 close(fd);
474 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
475 conn->flags |= CO_FL_ERROR;
476 return SF_ERR_RESOURCE;
477 } else if (errno == ETIMEDOUT) {
478 //qfprintf(stderr,"Connect(): ETIMEDOUT");
479 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
480 fdinfo[fd].port_range = NULL;
481 close(fd);
482 conn->err_code = CO_ER_SOCK_ERR;
483 conn->flags |= CO_FL_ERROR;
484 return SF_ERR_SRVTO;
485 } else {
486 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
487 //qfprintf(stderr,"Connect(): %d", errno);
488 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
489 fdinfo[fd].port_range = NULL;
490 close(fd);
491 conn->err_code = CO_ER_SOCK_ERR;
492 conn->flags |= CO_FL_ERROR;
493 return SF_ERR_SRVCL;
494 }
495 }
496 else {
497 /* connect() == 0, this is great! */
498 conn->flags &= ~CO_FL_WAIT_L4_CONN;
499 }
500
501 conn->flags |= CO_FL_ADDR_TO_SET;
502
503 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200504 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100505
506 if (conn->flags & CO_FL_WAIT_L4_CONN) {
507 fd_want_send(fd);
508 fd_cant_send(fd);
509 fd_cant_recv(fd);
510 }
511
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100512 return SF_ERR_NONE; /* connection is OK */
513}
514
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100515/* Add listener <listener> to protocol <proto>. Technically speaking we just
516 * initialize a few entries which should be doable during quic_bind_listener().
517 * The end of the initialization goes on with the default function.
518 */
519static void quic_add_listener(struct protocol *proto, struct listener *listener)
520{
Frédéric Lécaillec28aba22021-06-07 10:28:10 +0200521 MT_LIST_INIT(&listener->rx.pkts);
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100522 listener->rx.odcids = EB_ROOT_UNIQUE;
523 listener->rx.cids = EB_ROOT_UNIQUE;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +0200524 HA_RWLOCK_INIT(&listener->rx.cids_lock);
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100525 default_add_listener(proto, listener);
526}
527
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200528/* Allocate the TX ring buffers for <l> listener.
529 * Return 1 if succeeded, 0 if not.
530 */
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200531static int quic_alloc_tx_rings_listener(struct listener *l)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200532{
533 struct qring *qr;
534 int i;
535
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200536 l->rx.tx_qrings = calloc(global.nbthread, sizeof *l->rx.tx_qrings);
537 if (!l->rx.tx_qrings)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200538 return 0;
539
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200540 MT_LIST_INIT(&l->rx.tx_qring_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200541 for (i = 0; i < global.nbthread; i++) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200542 unsigned char *buf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200543 struct qring *qr = &l->rx.tx_qrings[i];
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200544
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200545 buf = pool_alloc(pool_head_quic_tx_ring);
546 if (!buf)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200547 goto err;
548
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200549 qr->cbuf = cbuf_new(buf, QUIC_TX_RING_BUFSZ);
550 if (!qr->cbuf) {
551 pool_free(pool_head_quic_tx_ring, buf);
552 goto err;
553 }
554
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200555 MT_LIST_APPEND(&l->rx.tx_qring_list, &qr->mt_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200556 }
557
558 return 1;
559
560 err:
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200561 while ((qr = MT_LIST_POP(&l->rx.tx_qring_list, typeof(qr), mt_list))) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200562 pool_free(pool_head_quic_tx_ring, qr->cbuf->buf);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200563 cbuf_free(qr->cbuf);
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200564 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200565 free(l->rx.tx_qrings);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200566 return 0;
567}
568
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200569/* Allocate the RX buffers for <l> listener.
570 * Return 1 if succeeded, 0 if not.
571 */
572static int quic_alloc_rxbufs_listener(struct listener *l)
573{
574 int i;
575 struct rxbuf *rxbuf;
576
577 l->rx.rxbufs = calloc(global.nbthread, sizeof *l->rx.rxbufs);
578 if (!l->rx.rxbufs)
579 return 0;
580
581 MT_LIST_INIT(&l->rx.rxbuf_list);
582 for (i = 0; i < global.nbthread; i++) {
583 char *buf;
584
585 rxbuf = &l->rx.rxbufs[i];
586 buf = pool_alloc(pool_head_quic_rxbuf);
587 if (!buf)
588 goto err;
589
590 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
591 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list);
592 }
593
594 return 1;
595
596 err:
597 while ((rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list)))
598 pool_free(pool_head_quic_rxbuf, rxbuf->buf.area);
599 free(l->rx.rxbufs);
600 return 0;
601}
602
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100603/* This function tries to bind a QUIC4/6 listener. It may return a warning or
604 * an error message in <errmsg> if the message is at most <errlen> bytes long
605 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
606 * The return value is composed from ERR_ABORT, ERR_WARN,
607 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
608 * was alright and that no message was returned. ERR_RETRYABLE means that an
609 * error occurred but that it may vanish after a retry (eg: port in use), and
610 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
611 * the meaning of the error, but just indicate that a message is present which
612 * should be displayed with the respective level. Last, ERR_ABORT indicates
613 * that it's pointless to try to start other listeners. No error message is
614 * returned if errlen is NULL.
615 */
616static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
617{
618 int err = ERR_NONE;
619 char *msg = NULL;
620
621 /* ensure we never return garbage */
622 if (errlen)
623 *errmsg = 0;
624
625 if (listener->state != LI_ASSIGNED)
626 return ERR_NONE; /* already bound */
627
628 if (!(listener->rx.flags & RX_F_BOUND)) {
629 msg = "receiving socket not bound";
630 goto udp_return;
631 }
632
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200633 if (!quic_alloc_tx_rings_listener(listener) ||
634 !quic_alloc_rxbufs_listener(listener)) {
635 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200636 err |= ERR_WARN;
637 goto udp_return;
638 }
639
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100640 listener_set_state(listener, LI_LISTEN);
641
642 udp_return:
643 if (msg && errlen) {
644 char pn[INET6_ADDRSTRLEN];
645
646 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200647 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 +0100648 }
649 return err;
650}
651
652/* Enable receipt of incoming connections for listener <l>. The receiver must
653 * still be valid. Does nothing in early boot (needs fd_updt).
654 */
655static void quic_enable_listener(struct listener *l)
656{
657 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500658 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100659 * connections.
660 */
661 if (fd_updt)
662 fd_want_recv(l->rx.fd);
663}
664
665/* Disable receipt of incoming connections for listener <l>. The receiver must
666 * still be valid. Does nothing in early boot (needs fd_updt).
667 */
668static void quic_disable_listener(struct listener *l)
669{
670 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500671 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100672 * connections again.
673 */
674 if (fd_updt)
675 fd_stop_recv(l->rx.fd);
676}
677
678/*
679 * Local variables:
680 * c-indent-level: 8
681 * c-basic-offset: 8
682 * End:
683 */