blob: 8086ff65a60baf07edc1c37950a8636e21ca6a61 [file] [log] [blame]
Frédéric Lécaille70da8892020-11-06 15:49:49 +01001/*
2 * QUIC socket management.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaille70da8892020-11-06 15:49:49 +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 <errno.h>
14
15#include <sys/socket.h>
16#include <sys/types.h>
17
18#include <haproxy/connection.h>
19#include <haproxy/listener.h>
Amaury Denoyelle4d295042022-01-19 16:18:44 +010020#include <haproxy/quic_sock.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020021#include <haproxy/session.h>
Amaury Denoyelle777969c2022-03-24 16:06:26 +010022#include <haproxy/tools.h>
Frédéric Lécaille026a7922020-11-23 15:46:36 +010023#include <haproxy/xprt_quic.h>
24
25/* This function is called from the protocol layer accept() in order to
26 * instantiate a new session on behalf of a given listener and frontend. It
27 * returns a positive value upon success, 0 if the connection can be ignored,
28 * or a negative value upon critical failure. The accepted connection is
29 * closed if we return <= 0. If no handshake is needed, it immediately tries
30 * to instantiate a new stream. The connection must already have been filled
31 * with the incoming connection handle (a fd), a target (the listener) and a
32 * source address.
33 */
34int quic_session_accept(struct connection *cli_conn)
35{
36 struct listener *l = __objt_listener(cli_conn->target);
37 struct proxy *p = l->bind_conf->frontend;
38 struct session *sess;
39
40 cli_conn->proxy_netns = l->rx.settings->netns;
Frédéric Lécaille026a7922020-11-23 15:46:36 +010041 /* This flag is ordinarily set by conn_ctrl_init() which cannot
42 * be called for now.
43 */
44 cli_conn->flags |= CO_FL_CTRL_READY;
45
46 /* wait for a PROXY protocol header */
47 if (l->options & LI_O_ACC_PROXY)
48 cli_conn->flags |= CO_FL_ACCEPT_PROXY;
49
50 /* wait for a NetScaler client IP insertion protocol header */
51 if (l->options & LI_O_ACC_CIP)
52 cli_conn->flags |= CO_FL_ACCEPT_CIP;
53
Frédéric Lécaille026a7922020-11-23 15:46:36 +010054 /* Add the handshake pseudo-XPRT */
55 if (cli_conn->flags & (CO_FL_ACCEPT_PROXY | CO_FL_ACCEPT_CIP)) {
56 if (xprt_add_hs(cli_conn) != 0)
57 goto out_free_conn;
58 }
Olivier Houchard1b3c9312021-03-05 23:37:48 +010059
Frédéric Lécaille026a7922020-11-23 15:46:36 +010060 sess = session_new(p, l, &cli_conn->obj_type);
61 if (!sess)
62 goto out_free_conn;
63
64 conn_set_owner(cli_conn, sess, NULL);
65
Frédéric Lécailleecb58722021-05-27 17:12:36 +020066 if (conn_complete_session(cli_conn) < 0)
67 goto out_free_sess;
68
69 if (conn_xprt_start(cli_conn) >= 0)
Frédéric Lécaille27faba72021-03-03 16:21:00 +010070 return 1;
71
Frédéric Lécaille026a7922020-11-23 15:46:36 +010072 out_free_sess:
73 /* prevent call to listener_release during session_free. It will be
74 * done below, for all errors. */
75 sess->listener = NULL;
76 session_free(sess);
77 out_free_conn:
78 cli_conn->qc->conn = NULL;
79 conn_stop_tracking(cli_conn);
80 conn_xprt_close(cli_conn);
81 conn_free(cli_conn);
82 out:
83
Frédéric Lécaillee8139f32021-03-11 17:06:30 +010084 return -1;
Frédéric Lécaille026a7922020-11-23 15:46:36 +010085}
86
87/*
88 * Inspired from session_accept_fd().
89 * Instantiate a new connection (connection struct) to be attached to <qc>
90 * QUIC connection of <l> listener.
91 * Returns 1 if succeeded, 0 if not.
92 */
93static int new_quic_cli_conn(struct quic_conn *qc, struct listener *l,
94 struct sockaddr_storage *saddr)
95{
96 struct connection *cli_conn;
Frédéric Lécaille026a7922020-11-23 15:46:36 +010097
Frédéric Lécaille026a7922020-11-23 15:46:36 +010098 if (unlikely((cli_conn = conn_new(&l->obj_type)) == NULL))
99 goto out;
100
Willy Tarreau9cc88c32022-04-08 14:34:31 +0200101 if (!sockaddr_alloc(&cli_conn->src, saddr, sizeof *saddr))
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100102 goto out_free_conn;
103
Willy Tarreauc78a9692022-04-11 17:26:56 +0200104 cli_conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_FDLESS;
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100105 qc->conn = cli_conn;
106 cli_conn->qc = qc;
107
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100108 cli_conn->handle.fd = l->rx.fd;
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100109 cli_conn->target = &l->obj_type;
110
Frédéric Lécaille01ab6612021-06-14 10:31:43 +0200111 /* We need the xprt context before accepting (->accept()) the connection:
112 * we may receive packet before this connection acception.
113 */
114 if (conn_prepare(cli_conn, l->rx.proto, l->bind_conf->xprt) < 0)
115 goto out_free_conn;
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100116
117 return 1;
118
119 out_free_conn:
Frédéric Lécaille01ab6612021-06-14 10:31:43 +0200120 qc->conn = NULL;
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100121 conn_stop_tracking(cli_conn);
122 conn_xprt_close(cli_conn);
123 conn_free(cli_conn);
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100124 out:
125
126 return 0;
127}
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100128
129/* Tests if the receiver supports accepting connections. Returns positive on
130 * success, 0 if not possible
131 */
132int quic_sock_accepting_conn(const struct receiver *rx)
133{
134 return 1;
135}
136
137/* Accept an incoming connection from listener <l>, and return it, as well as
138 * a CO_AC_* status code into <status> if not null. Null is returned on error.
139 * <l> must be a valid listener with a valid frontend.
140 */
141struct connection *quic_sock_accept_conn(struct listener *l, int *status)
142{
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100143 struct quic_conn *qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100144 struct li_per_thread *lthr = &l->per_thr[tid];
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100145
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100146 qc = MT_LIST_POP(&lthr->quic_accept.conns, struct quic_conn *, accept_list);
147 if (!qc)
148 goto done;
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100149
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100150 if (!new_quic_cli_conn(qc, l, &qc->peer_addr))
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100151 goto err;
152
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100153 done:
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100154 *status = CO_AC_DONE;
Frédéric Lécaille026a7922020-11-23 15:46:36 +0100155 return qc ? qc->conn : NULL;
156
157 err:
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100158 /* in case of error reinsert the element to process it later. */
159 MT_LIST_INSERT(&lthr->quic_accept.conns, &qc->accept_list);
160
161 *status = CO_AC_PAUSE;
162 return NULL;
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100163}
164
165/* Function called on a read event from a listening socket. It tries
166 * to handle as many connections as possible.
167 */
168void quic_sock_fd_iocb(int fd)
169{
170 ssize_t ret;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100171 struct rxbuf *rxbuf;
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100172 struct buffer *buf;
173 struct listener *l = objt_listener(fdtab[fd].owner);
Frédéric Lécaillec4becf52021-11-08 11:23:17 +0100174 struct quic_transport_params *params;
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100175 /* Source address */
176 struct sockaddr_storage saddr = {0};
Frédéric Lécaille320744b2022-01-27 12:19:28 +0100177 size_t max_sz, cspace;
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100178 socklen_t saddrlen;
Frédéric Lécaille37ae5052022-01-27 11:31:50 +0100179 struct quic_dgram *dgram, *dgramp, *new_dgram;
Frédéric Lécaillef6f75202022-02-02 09:44:22 +0100180 unsigned char *dgram_buf;
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100181
Tim Duesterhus16554242021-09-15 13:58:49 +0200182 BUG_ON(!l);
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100183
Frédéric Lécaillec4becf52021-11-08 11:23:17 +0100184 if (!l)
185 return;
186
Willy Tarreauf5090652021-04-06 17:23:40 +0200187 if (!(fdtab[fd].state & FD_POLL_IN) || !fd_recv_ready(fd))
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100188 return;
189
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100190 rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list);
Amaury Denoyelleee72a432021-11-19 15:49:29 +0100191 if (!rxbuf)
Frédéric Lécaillec4becf52021-11-08 11:23:17 +0100192 goto out;
Frédéric Lécaille37ae5052022-01-27 11:31:50 +0100193
Amaury Denoyelleee72a432021-11-19 15:49:29 +0100194 buf = &rxbuf->buf;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +0100195
Frédéric Lécaille37ae5052022-01-27 11:31:50 +0100196 new_dgram = NULL;
197 /* Remove all consumed datagrams of this buffer */
198 list_for_each_entry_safe(dgram, dgramp, &rxbuf->dgrams, list) {
199 if (HA_ATOMIC_LOAD(&dgram->buf))
200 break;
201
202 LIST_DELETE(&dgram->list);
203 b_del(buf, dgram->len);
204 if (!new_dgram)
205 new_dgram = dgram;
206 else
207 pool_free(pool_head_quic_dgram, dgram);
208 }
209
Frédéric Lécaillec4becf52021-11-08 11:23:17 +0100210 params = &l->bind_conf->quic_params;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100211 max_sz = params->max_udp_payload_size;
Frédéric Lécaille320744b2022-01-27 12:19:28 +0100212 cspace = b_contig_space(buf);
213 if (cspace < max_sz) {
Frédéric Lécaille1712b1d2022-01-28 13:10:24 +0100214 struct quic_dgram *dgram;
215
216 /* Allocate a fake datagram, without data to locate
217 * the end of the RX buffer (required during purging).
218 */
219 dgram = pool_zalloc(pool_head_quic_dgram);
220 if (!dgram)
221 goto out;
222
223 dgram->len = cspace;
224 LIST_APPEND(&rxbuf->dgrams, &dgram->list);
Frédéric Lécaille320744b2022-01-27 12:19:28 +0100225 /* Consume the remaining space */
226 b_add(buf, cspace);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100227 if (b_contig_space(buf) < max_sz)
228 goto out;
Frédéric Lécaillef6f75202022-02-02 09:44:22 +0100229
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100230 }
231
Frédéric Lécaillef6f75202022-02-02 09:44:22 +0100232 dgram_buf = (unsigned char *)b_tail(buf);
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100233 saddrlen = sizeof saddr;
234 do {
Frédéric Lécaillef6f75202022-02-02 09:44:22 +0100235 ret = recvfrom(fd, dgram_buf, max_sz, 0,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100236 (struct sockaddr *)&saddr, &saddrlen);
Frédéric Lécaille439c4642022-02-02 14:33:10 +0100237 if (ret < 0 && errno == EAGAIN) {
238 fd_cant_recv(fd);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100239 goto out;
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100240 }
Frédéric Lécaille439c4642022-02-02 14:33:10 +0100241 } while (ret < 0 && errno == EINTR);
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100242
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100243 b_add(buf, ret);
Frédéric Lécaillef6f75202022-02-02 09:44:22 +0100244 if (!quic_lstnr_dgram_dispatch(dgram_buf, ret, l, &saddr,
245 new_dgram, &rxbuf->dgrams)) {
Frédéric Lécaille37ae5052022-01-27 11:31:50 +0100246 /* If wrong, consume this datagram */
247 b_del(buf, ret);
248 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100249 out:
250 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list);
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100251}
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100252
Amaury Denoyelle58a77042022-02-09 15:43:07 +0100253/* TODO standardize this function for a generic UDP sendto wrapper. This can be
254 * done by removing the <qc> arg and replace it with address/port.
255 */
256size_t qc_snd_buf(struct quic_conn *qc, const struct buffer *buf, size_t count,
257 int flags)
258{
259 ssize_t ret;
260 size_t try, done;
261 int send_flag;
262
263 done = 0;
264 /* send the largest possible block. For this we perform only one call
265 * to send() unless the buffer wraps and we exactly fill the first hunk,
266 * in which case we accept to do it once again.
267 */
268 while (count) {
269 try = b_contig_data(buf, done);
270 if (try > count)
271 try = count;
272
273 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
274 if (try < count || flags & CO_SFL_MSG_MORE)
275 send_flag |= MSG_MORE;
276
277 ret = sendto(qc->li->rx.fd, b_peek(buf, done), try, send_flag,
278 (struct sockaddr *)&qc->peer_addr, get_addr_len(&qc->peer_addr));
279 if (ret > 0) {
280 /* TODO remove partial sending support for UDP */
281 count -= ret;
282 done += ret;
283
284 if (ret < try)
285 break;
286 }
287 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
288 /* TODO must be handle properly. It is justified for UDP ? */
289 ABORT_NOW();
290 }
291 else if (errno != EINTR) {
292 /* TODO must be handle properly. It is justified for UDP ? */
293 ABORT_NOW();
294 }
295 }
296
297 if (done > 0) {
298 /* we count the total bytes sent, and the send rate for 32-byte
299 * blocks. The reason for the latter is that freq_ctr are
300 * limited to 4GB and that it's not enough per second.
301 */
302 _HA_ATOMIC_ADD(&global.out_bytes, done);
303 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
304 }
305 return done;
306}
307
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100308
309/*********************** QUIC accept queue management ***********************/
310/* per-thread accept queues */
311struct quic_accept_queue *quic_accept_queues;
312
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100313/* Install <qc> on the queue ready to be accepted. The queue task is then woken
Frédéric Lécaille91f083a2022-01-28 21:43:48 +0100314 * up. If <qc> accept is already scheduled or done, nothing is done.
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100315 */
316void quic_accept_push_qc(struct quic_conn *qc)
317{
318 struct quic_accept_queue *queue = &quic_accept_queues[qc->tid];
319 struct li_per_thread *lthr = &qc->li->per_thr[qc->tid];
320
Frédéric Lécaille91f083a2022-01-28 21:43:48 +0100321 /* early return if accept is already in progress/done for this
322 * connection
323 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200324 if (qc->flags & QUIC_FL_CONN_ACCEPT_REGISTERED)
Frédéric Lécaille91f083a2022-01-28 21:43:48 +0100325 return;
326
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100327 BUG_ON(MT_LIST_INLIST(&qc->accept_list));
328
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200329 qc->flags |= QUIC_FL_CONN_ACCEPT_REGISTERED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +0100330 /* 1. insert the listener in the accept queue
331 *
332 * Use TRY_APPEND as there is a possible race even with INLIST if
333 * multiple threads try to add the same listener instance from several
334 * quic_conn.
335 */
336 if (!MT_LIST_INLIST(&(lthr->quic_accept.list)))
337 MT_LIST_TRY_APPEND(&queue->listeners, &(lthr->quic_accept.list));
338
339 /* 2. insert the quic_conn in the listener per-thread queue. */
340 MT_LIST_APPEND(&lthr->quic_accept.conns, &qc->accept_list);
341
342 /* 3. wake up the queue tasklet */
343 tasklet_wakeup(quic_accept_queues[qc->tid].tasklet);
344}
345
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100346/* Tasklet handler to accept QUIC connections. Call listener_accept on every
347 * listener instances registered in the accept queue.
348 */
349static struct task *quic_accept_run(struct task *t, void *ctx, unsigned int i)
350{
351 struct li_per_thread *lthr;
352 struct mt_list *elt1, elt2;
353 struct quic_accept_queue *queue = &quic_accept_queues[tid];
354
355 mt_list_for_each_entry_safe(lthr, &queue->listeners, quic_accept.list, elt1, elt2) {
356 listener_accept(lthr->li);
357 MT_LIST_DELETE_SAFE(elt1);
358 }
359
360 return NULL;
361}
362
363static int quic_alloc_accept_queues(void)
364{
365 int i;
366
367 quic_accept_queues = calloc(global.nbthread, sizeof(struct quic_accept_queue));
368 if (!quic_accept_queues) {
369 ha_alert("Failed to allocate the quic accept queues.\n");
370 return 0;
371 }
372
373 for (i = 0; i < global.nbthread; ++i) {
374 struct tasklet *task;
375 if (!(task = tasklet_new())) {
376 ha_alert("Failed to allocate the quic accept queue on thread %d.\n", i);
377 return 0;
378 }
379
380 tasklet_set_tid(task, i);
381 task->process = quic_accept_run;
382 quic_accept_queues[i].tasklet = task;
383
384 MT_LIST_INIT(&quic_accept_queues[i].listeners);
385 }
386
387 return 1;
388}
389REGISTER_POST_CHECK(quic_alloc_accept_queues);
390
391static int quic_deallocate_accept_queues(void)
392{
393 int i;
394
395 if (quic_accept_queues) {
396 for (i = 0; i < global.nbthread; ++i)
397 tasklet_free(quic_accept_queues[i].tasklet);
398 free(quic_accept_queues);
399 }
400
401 return 1;
402}
403REGISTER_POST_DEINIT(quic_deallocate_accept_queues);