2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <sys/socket.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/transport.h>
20 #include <vnet/session/session.h>
21 #include <vlib/unix/plugin.h>
22 #include <vpp/app/version.h>
23 #include <openssl/pem.h>
25 #include <vppinfra/lock.h>
27 #include <quic/quic.h>
29 #include <quicly/defaults.h>
30 #include <picotls/openssl.h>
31 #include <picotls/pembase64.h>
33 static quic_main_t quic_main;
35 static void quic_update_timer (quic_ctx_t * ctx);
36 static void quic_connection_closed (u32 conn_index, u32 thread_index);
37 static void quic_disconnect (u32 ctx_index, u32 thread_index);
38 static int quic_connect_new_stream (session_endpoint_cfg_t * sep);
39 static int quic_connect_new_connection (session_endpoint_cfg_t * sep);
41 static int64_t quic_get_time (quicly_now_t * self);
42 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
44 static void quic_transfer_connection (u32 ctx_index, u32 dest_thread);
46 #define QUIC_TIMER_HANDLE_INVALID ((u32) ~0)
47 #define QUIC_SESSION_INVALID ((u32) ~0 - 1)
48 #define QUIC_MAX_PACKET_SIZE 1280
50 #define QUIC_INT_MAX 0x3FFFFFFFFFFFFFFF
52 /* Taken from quicly.c */
53 #define QUICLY_QUIC_BIT 0x40
55 #define QUICLY_PACKET_TYPE_INITIAL (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0)
56 #define QUICLY_PACKET_TYPE_0RTT (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x10)
57 #define QUICLY_PACKET_TYPE_HANDSHAKE (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x20)
58 #define QUICLY_PACKET_TYPE_RETRY (QUICLY_LONG_HEADER_BIT | QUICLY_QUIC_BIT | 0x30)
59 #define QUICLY_PACKET_TYPE_BITMASK 0xf0
60 #define QUIC_FIFO_SIZE (64 << 10)
62 #define QUIC_ERROR_FULL_FIFO 0xff10
65 quic_format_err (u64 code)
69 case QUIC_ERROR_FULL_FIFO:
71 case QUICLY_ERROR_PACKET_IGNORED:
72 return "QUICLY_ERROR_PACKET_IGNORED";
73 case QUICLY_ERROR_SENDBUF_FULL:
74 return "QUICLY_ERROR_SENDBUF_FULL";
75 case QUICLY_ERROR_FREE_CONNECTION:
76 return "no open stream on connection";
77 case QUICLY_ERROR_RECEIVED_STATELESS_RESET:
78 return "QUICLY_ERROR_RECEIVED_STATELESS_RESET";
79 case QUICLY_TRANSPORT_ERROR_NONE:
80 return "QUICLY_TRANSPORT_ERROR_NONE";
81 case QUICLY_TRANSPORT_ERROR_INTERNAL:
82 return "QUICLY_TRANSPORT_ERROR_INTERNAL";
83 case QUICLY_TRANSPORT_ERROR_SERVER_BUSY:
84 return "QUICLY_TRANSPORT_ERROR_SERVER_BUSY";
85 case QUICLY_TRANSPORT_ERROR_FLOW_CONTROL:
86 return "QUICLY_TRANSPORT_ERROR_FLOW_CONTROL";
87 case QUICLY_TRANSPORT_ERROR_STREAM_ID:
88 return "QUICLY_TRANSPORT_ERROR_STREAM_ID";
89 case QUICLY_TRANSPORT_ERROR_STREAM_STATE:
90 return "QUICLY_TRANSPORT_ERROR_STREAM_STATE";
91 case QUICLY_TRANSPORT_ERROR_FINAL_OFFSET:
92 return "QUICLY_TRANSPORT_ERROR_FINAL_OFFSET";
93 case QUICLY_TRANSPORT_ERROR_FRAME_ENCODING:
94 return "QUICLY_TRANSPORT_ERROR_FRAME_ENCODING";
95 case QUICLY_TRANSPORT_ERROR_TRANSPORT_PARAMETER:
96 return "QUICLY_TRANSPORT_ERROR_TRANSPORT_PARAMETER";
97 case QUICLY_TRANSPORT_ERROR_VERSION_NEGOTIATION:
98 return "QUICLY_TRANSPORT_ERROR_VERSION_NEGOTIATION";
99 case QUICLY_TRANSPORT_ERROR_PROTOCOL_VIOLATION:
100 return "QUICLY_TRANSPORT_ERROR_PROTOCOL_VIOLATION";
101 case QUICLY_TRANSPORT_ERROR_INVALID_MIGRATION:
102 return "QUICLY_TRANSPORT_ERROR_INVALID_MIGRATION";
103 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE:
104 return "QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE";
106 return "unknown error";
111 quic_ctx_alloc (u32 thread_index)
113 quic_main_t *qm = &quic_main;
116 pool_get (qm->ctx_pool[thread_index], ctx);
118 memset (ctx, 0, sizeof (quic_ctx_t));
119 ctx->c_thread_index = thread_index;
120 QUIC_DBG (1, "Allocated quic_ctx %u on thread %u",
121 ctx - qm->ctx_pool[thread_index], thread_index);
122 return ctx - qm->ctx_pool[thread_index];
126 quic_ctx_free (quic_ctx_t * ctx)
128 QUIC_DBG (2, "Free ctx %u", ctx->c_c_index);
129 u32 thread_index = ctx->c_thread_index;
131 memset (ctx, 0xfb, sizeof (*ctx));
132 pool_put (quic_main.ctx_pool[thread_index], ctx);
136 quic_ctx_get (u32 ctx_index, u32 thread_index)
138 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
142 quic_get_conn_ctx (quicly_conn_t * conn)
145 conn_data = (u64) * quicly_get_data (conn);
146 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
150 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
152 *quicly_get_data (conn) =
153 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
157 quic_disconnect_transport (quic_ctx_t * ctx)
159 QUIC_DBG (2, "Called quic_disconnect_transport");
160 vnet_disconnect_args_t a = {
161 .handle = ctx->c_quic_ctx_id.udp_session_handle,
162 .app_index = quic_main.app_index,
165 if (vnet_disconnect_session (&a))
166 clib_warning ("UDP session disconnect errored");
170 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
172 /* QUIC_DBG (2, "Called quic_send_datagram at %ld", quic_get_time (NULL)); */
174 session_dgram_hdr_t hdr;
177 transport_connection_t *tc;
179 len = packet->data.len;
180 f = udp_session->tx_fifo;
181 tc = session_get_transport (udp_session);
183 max_enqueue = svm_fifo_max_enqueue (f);
184 if (max_enqueue <= sizeof (session_dgram_hdr_t))
186 QUIC_DBG (1, "Not enough space to enqueue header");
187 return QUIC_ERROR_FULL_FIFO;
190 max_enqueue -= sizeof (session_dgram_hdr_t);
192 if (max_enqueue < len)
194 QUIC_DBG (1, "Too much data to send, max_enqueue %u, len %u",
196 return QUIC_ERROR_FULL_FIFO;
199 /* Build packet header for fifo */
200 hdr.data_length = len;
202 hdr.is_ip4 = tc->is_ip4;
203 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
204 hdr.lcl_port = tc->lcl_port;
206 /* Read dest address from quicly-provided sockaddr */
209 ASSERT (packet->sa.sa_family == AF_INET);
210 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->sa;
211 hdr.rmt_port = sa4->sin_port;
212 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
216 ASSERT (packet->sa.sa_family == AF_INET6);
217 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->sa;
218 hdr.rmt_port = sa6->sin6_port;
219 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
222 ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
223 if (ret != sizeof (hdr))
225 QUIC_DBG (1, "Not enough space to enqueue header");
226 return QUIC_ERROR_FULL_FIFO;
228 ret = svm_fifo_enqueue (f, len, packet->data.base);
231 QUIC_DBG (1, "Not enough space to enqueue payload");
232 return QUIC_ERROR_FULL_FIFO;
237 #define QUIC_SEND_PACKET_VEC_SIZE 16
240 quic_sendable_packet_count (session_t * udp_session)
243 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
244 return clib_min (max_enqueue /
245 (QUIC_MAX_PACKET_SIZE + sizeof (session_dgram_hdr_t)),
246 QUIC_SEND_PACKET_VEC_SIZE);
250 quic_send_packets (quic_ctx_t * ctx)
252 quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
253 session_t *udp_session;
255 size_t num_packets, i, max_packets;
256 quicly_context_t *quicly_context;
257 app_worker_t *app_wrk;
261 /* We have sctx, get qctx */
262 if (ctx->c_quic_ctx_id.is_stream)
264 quic_ctx_get (ctx->c_quic_ctx_id.quic_connection_ctx_id,
265 ctx->c_thread_index);
267 ASSERT (!ctx->c_quic_ctx_id.is_stream);
270 session_get_from_handle (ctx->c_quic_ctx_id.udp_session_handle);
271 conn = ctx->c_quic_ctx_id.conn;
276 /* TODO : quicly can assert it can send min_packets up to 2 */
277 if (quic_sendable_packet_count (udp_session) < 2)
280 app_wrk = app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_id);
283 clib_warning ("Tried to send packets on non existing app worker %u",
284 ctx->c_quic_ctx_id.parent_app_wrk_id);
285 quic_connection_closed (ctx->c_c_index, ctx->c_thread_index);
288 app = application_get (app_wrk->app_index);
290 quicly_context = (quicly_context_t *) app->quicly_ctx;
293 max_packets = quic_sendable_packet_count (udp_session);
296 num_packets = max_packets;
297 if ((err = quicly_send (conn, packets, &num_packets)))
300 for (i = 0; i != num_packets; ++i)
302 if ((err = quic_send_datagram (udp_session, packets[i])))
305 quicly_context->packet_allocator->
306 free_packet (quicly_context->packet_allocator, packets[i]);
309 while (num_packets > 0 && num_packets == max_packets);
311 if (svm_fifo_set_event (udp_session->tx_fifo))
312 session_send_io_evt_to_thread (udp_session->tx_fifo, SESSION_IO_EVT_TX);
315 quic_update_timer (ctx);
319 if (err != QUICLY_ERROR_PACKET_IGNORED)
320 clib_warning ("Quic error '%s'.", quic_format_err (err));
321 quic_connection_closed (ctx->c_c_index, ctx->c_thread_index);
325 /*****************************************************************************
326 * START QUICLY CALLBACKS
327 * Called from QUIC lib
328 *****************************************************************************/
331 quic_on_stream_destroy (quicly_stream_t * stream, int err)
333 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
334 u32 sctx_id = stream_data->ctx_id;
335 session_t *stream_session;
336 quic_ctx_t *sctx = quic_ctx_get (sctx_id, stream_data->thread_index);
337 QUIC_DBG (2, "Stream %ld (ctx %u) destroyed", stream->stream_id, sctx_id);
338 stream_session = session_get (sctx->c_s_index, sctx->c_thread_index);
339 stream_session->session_state = SESSION_STATE_CLOSED;
340 session_transport_delete_notify (&sctx->connection);
341 quic_ctx_free (sctx);
346 quic_on_stop_sending (quicly_stream_t * stream, int error_code)
348 QUIC_DBG (2, "received STOP_SENDING: %d", error_code);
353 quic_on_receive_reset (quicly_stream_t * stream, int error_code)
355 QUIC_DBG (2, "received RESET_STREAM: %d", error_code);
360 get_stream_session_from_stream (quicly_stream_t * stream)
363 quic_stream_data_t *stream_data;
365 stream_data = (quic_stream_data_t *) stream->data;
366 ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
367 return session_get (ctx->c_s_index, stream_data->thread_index);
371 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
374 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
377 session_t *stream_session;
378 app_worker_t *app_wrk;
380 quic_stream_data_t *stream_data;
383 stream_data = (quic_stream_data_t *) stream->data;
384 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
385 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
386 f = stream_session->rx_fifo;
388 max_enq = svm_fifo_max_enqueue_prod (f);
389 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
390 if (off + len > max_enq)
392 /* TODO : can we find a better solution, listening on RX fifo evts ? */
393 QUIC_DBG (3, "Ingoring packet, RX fifo is full");
394 return QUICLY_ERROR_PACKET_IGNORED;
398 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
399 ASSERT (rlen >= len);
401 quicly_stream_sync_recvbuf (stream, rlen);
402 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
403 if (PREDICT_TRUE (app_wrk != 0))
404 app_worker_lock_and_send_event (app_wrk, stream_session,
409 rlen = svm_fifo_enqueue_with_offset (f, off, len, (u8 *) src);
416 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
418 session_t *stream_session;
421 stream_session = get_stream_session_from_stream (stream);
422 f = stream_session->tx_fifo;
424 ASSERT (svm_fifo_dequeue_drop (f, delta) == delta);
425 quicly_stream_sync_sendbuf (stream, 0);
429 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
430 size_t * len, int *wrote_all)
432 session_t *stream_session;
434 u32 deq_max, first_deq, max_rd_chunk, rem_offset;
436 stream_session = get_stream_session_from_stream (stream);
437 f = stream_session->tx_fifo;
439 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
441 deq_max = svm_fifo_max_dequeue_cons (f);
442 ASSERT (off <= deq_max);
443 if (off + *len < deq_max)
449 QUIC_DBG (3, "Wrote ALL");
451 *len = deq_max - off;
454 /* TODO, use something like : return svm_fifo_peek (f, off, *len, dst); */
455 max_rd_chunk = svm_fifo_max_read_chunk (f);
458 if (off < max_rd_chunk)
460 first_deq = clib_min (*len, max_rd_chunk - off);
461 clib_memcpy_fast (dst, svm_fifo_head (f) + off, first_deq);
464 if (max_rd_chunk < off + *len)
466 rem_offset = max_rd_chunk < off ? off - max_rd_chunk : 0;
467 clib_memcpy_fast (dst + first_deq, f->head_chunk->data + rem_offset,
474 static const quicly_stream_callbacks_t quic_stream_callbacks = {
475 .on_destroy = quic_on_stream_destroy,
476 .on_send_shift = quic_fifo_egress_shift,
477 .on_send_emit = quic_fifo_egress_emit,
478 .on_send_stop = quic_on_stop_sending,
479 .on_receive = quic_on_receive,
480 .on_receive_reset = quic_on_receive_reset
484 quic_accept_stream (void *s)
486 quicly_stream_t *stream = (quicly_stream_t *) s;
487 session_t *stream_session;
488 quic_stream_data_t *stream_data;
489 app_worker_t *app_wrk;
490 quic_ctx_t *qctx, *sctx;
492 quic_main_t *qm = &quic_main;
495 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
497 qctx = quic_get_conn_ctx (stream->conn);
499 stream_session = session_alloc (qctx->c_thread_index);
500 QUIC_DBG (2, "Allocated stream_session, id %u, thread %u ctx %u",
501 stream_session->session_index, stream_session->thread_index,
503 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
504 sctx->c_quic_ctx_id.parent_app_wrk_id =
505 qctx->c_quic_ctx_id.parent_app_wrk_id;
506 sctx->c_quic_ctx_id.parent_app_id = qctx->c_quic_ctx_id.parent_app_id;
507 sctx->c_quic_ctx_id.quic_connection_ctx_id = qctx->c_c_index;
508 sctx->c_c_index = sctx_id;
509 sctx->c_quic_ctx_id.is_stream = 1;
510 sctx->c_s_index = stream_session->session_index;
511 sctx->c_quic_ctx_id.stream = stream;
513 stream_data = (quic_stream_data_t *) stream->data;
514 stream_data->ctx_id = sctx_id;
515 stream_data->thread_index = sctx->c_thread_index;
517 sctx->c_s_index = stream_session->session_index;
518 stream_session->session_state = SESSION_STATE_CREATED;
519 stream_session->flags |= SESSION_F_QUIC_STREAM;
520 stream_session->app_wrk_index = sctx->c_quic_ctx_id.parent_app_wrk_id;
521 stream_session->connection_index = sctx->c_c_index;
522 stream_session->session_type =
523 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
524 qctx->c_quic_ctx_id.udp_is_ip4);
525 stream_session->listener_index = qm->fake_app_listener_index;
526 stream_session->app_index = sctx->c_quic_ctx_id.parent_app_id;
528 app_wrk = app_worker_get (stream_session->app_wrk_index);
529 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
531 QUIC_DBG (1, "failed to allocate fifos");
532 session_free (stream_session);
533 quicly_reset_stream (stream, 0x30001);
537 rv = app_worker_accept_notify (app_wrk, stream_session);
540 QUIC_DBG (1, "failed to notify accept worker app");
541 session_free_w_fifos (stream_session);
542 quicly_reset_stream (stream, 0x30002);
545 session_lookup_add_connection (&sctx->connection,
546 session_handle (stream_session));
550 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
552 QUIC_DBG (2, "on_stream_open called");
553 stream->data = malloc (sizeof (quic_stream_data_t));
554 stream->callbacks = &quic_stream_callbacks;
555 /* Notify accept on parent qsession, but only if this is not a locally
556 * initiated stream */
557 if (!quicly_stream_is_self_initiated (stream))
559 quic_accept_stream (stream);
564 static quicly_stream_open_t on_stream_open = { &quic_on_stream_open };
567 quic_on_conn_close (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
568 int code, uint64_t frame_type,
569 const char *reason, size_t reason_len)
571 QUIC_DBG (2, "connection closed, reason: %.*s", reason, reason_len);
572 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
573 session_transport_closing_notify (&ctx->connection);
576 static quicly_closed_by_peer_t on_closed_by_peer = { &quic_on_conn_close };
579 /*****************************************************************************
580 * END QUICLY CALLBACKS
581 *****************************************************************************/
583 /* single-entry session cache */
584 struct st_util_session_cache_t
586 ptls_encrypt_ticket_t super;
592 encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
593 int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
595 struct st_util_session_cache_t *self = (void *) _self;
601 /* replace the cached entry along with a newly generated session id */
602 free (self->data.base);
603 if ((self->data.base = malloc (src.len)) == NULL)
604 return PTLS_ERROR_NO_MEMORY;
606 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
607 memcpy (self->data.base, src.base, src.len);
608 self->data.len = src.len;
610 /* store the session id in buffer */
611 if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
613 memcpy (dst->base + dst->off, self->id, sizeof (self->id));
614 dst->off += sizeof (self->id);
620 /* check if session id is the one stored in cache */
621 if (src.len != sizeof (self->id))
622 return PTLS_ERROR_SESSION_NOT_FOUND;
623 if (memcmp (self->id, src.base, sizeof (self->id)) != 0)
624 return PTLS_ERROR_SESSION_NOT_FOUND;
626 /* return the cached value */
627 if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
629 memcpy (dst->base + dst->off, self->data.base, self->data.len);
630 dst->off += self->data.len;
637 static struct st_util_session_cache_t sc = {
639 .cb = encrypt_ticket_cb,
643 static ptls_context_t quic_tlsctx = {
644 .random_bytes = ptls_openssl_random_bytes,
645 .get_time = &ptls_get_time,
646 .key_exchanges = ptls_openssl_key_exchanges,
647 .cipher_suites = ptls_openssl_cipher_suites,
653 .on_client_hello = NULL,
654 .emit_certificate = NULL,
655 .sign_certificate = NULL,
656 .verify_certificate = NULL,
657 .ticket_lifetime = 86400,
658 .max_early_data_size = 8192,
659 .hkdf_label_prefix__obsolete = NULL,
660 .require_dhe_on_psk = 1,
661 .encrypt_ticket = &sc.super,
666 ptls_compare_separator_line (const char *line, const char *begin_or_end,
669 int ret = strncmp (line, "-----", 5);
670 size_t text_index = 5;
674 size_t begin_or_end_length = strlen (begin_or_end);
675 ret = strncmp (line + text_index, begin_or_end, begin_or_end_length);
676 text_index += begin_or_end_length;
681 ret = line[text_index] - ' ';
687 size_t label_length = strlen (label);
688 ret = strncmp (line + text_index, label, label_length);
689 text_index += label_length;
694 ret = strncmp (line + text_index, "-----", 5);
701 ptls_get_bio_pem_object (BIO * bio, const char *label, ptls_buffer_t * buf)
703 int ret = PTLS_ERROR_PEM_LABEL_NOT_FOUND;
705 ptls_base64_decode_state_t state;
707 /* Get the label on a line by itself */
708 while (BIO_gets (bio, line, 256))
710 if (ptls_compare_separator_line (line, "BEGIN", label) == 0)
713 ptls_base64_decode_init (&state);
717 /* Get the data in the buffer */
718 while (ret == 0 && BIO_gets (bio, line, 256))
720 if (ptls_compare_separator_line (line, "END", label) == 0)
722 if (state.status == PTLS_BASE64_DECODE_DONE
723 || (state.status == PTLS_BASE64_DECODE_IN_PROGRESS
730 ret = PTLS_ERROR_INCORRECT_BASE64;
736 ret = ptls_base64_decode (line, &state, buf);
744 ptls_load_bio_pem_objects (BIO * bio, const char *label, ptls_iovec_t * list,
745 size_t list_max, size_t * nb_objects)
754 while (count < list_max)
758 ptls_buffer_init (&buf, "", 0);
760 ret = ptls_get_bio_pem_object (bio, label, &buf);
764 if (buf.off > 0 && buf.is_allocated)
766 list[count].base = buf.base;
767 list[count].len = buf.off;
772 ptls_buffer_dispose (&buf);
777 ptls_buffer_dispose (&buf);
783 if (ret == PTLS_ERROR_PEM_LABEL_NOT_FOUND && count > 0)
793 #define PTLS_MAX_CERTS_IN_CONTEXT 16
796 ptls_load_bio_certificates (ptls_context_t * ctx, BIO * bio)
800 ctx->certificates.list =
801 (ptls_iovec_t *) malloc (PTLS_MAX_CERTS_IN_CONTEXT *
802 sizeof (ptls_iovec_t));
804 if (ctx->certificates.list == NULL)
806 ret = PTLS_ERROR_NO_MEMORY;
811 ptls_load_bio_pem_objects (bio, "CERTIFICATE", ctx->certificates.list,
812 PTLS_MAX_CERTS_IN_CONTEXT,
813 &ctx->certificates.count);
820 load_bio_certificate_chain (ptls_context_t * ctx, const char *cert_data)
823 cert_bio = BIO_new_mem_buf (cert_data, -1);
824 if (ptls_load_bio_certificates (ctx, cert_bio) != 0)
827 fprintf (stderr, "failed to load certificate:%s\n", strerror (errno));
834 load_bio_private_key (ptls_context_t * ctx, const char *pk_data)
836 static ptls_openssl_sign_certificate_t sc;
840 key_bio = BIO_new_mem_buf (pk_data, -1);
841 pkey = PEM_read_bio_PrivateKey (key_bio, NULL, NULL, NULL);
846 fprintf (stderr, "failed to read private key from app configuration\n");
850 ptls_openssl_init_sign_certificate (&sc, pkey);
851 EVP_PKEY_free (pkey);
853 ctx->sign_certificate = &sc.super;
857 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
858 const quicly_cid_plaintext_t * id)
860 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
861 kv->key[1] = id->node_id;
865 quic_connection_closed (u32 ctx_index, u32 thread_index)
867 /* TODO : free fifos */
868 QUIC_DBG (2, "QUIC connection closed");
869 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
870 clib_bihash_kv_16_8_t kv;
874 ctx = quic_ctx_get (ctx_index, thread_index);
876 ASSERT (!ctx->c_quic_ctx_id.is_stream);
877 /* TODO if connection is not established, just delete the session? */
880 if (ctx->timer_handle != QUIC_TIMER_HANDLE_INVALID)
882 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
883 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
886 /* Delete the connection from the connection map */
887 conn = ctx->c_quic_ctx_id.conn;
888 quic_make_connection_key (&kv, quicly_get_master_id (conn));
889 QUIC_DBG (2, "Deleting conn with id %lu %lu", kv.key[0], kv.key[1]);
890 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
892 // session_close (session_get_from_handle (ctx->c_quic_ctx_id.udp_session_handle));
893 quic_disconnect_transport (ctx);
894 session_transport_delete_notify (&ctx->connection);
895 /* Do not try to send anything anymore */
896 quicly_free (ctx->c_quic_ctx_id.conn);
897 ctx->c_quic_ctx_id.conn = NULL;
902 allocate_quicly_ctx (application_t * app, u8 is_client)
909 quicly_context_t *quicly_ctx;
910 ptls_iovec_t key_vec;
911 QUIC_DBG (2, "Called allocate_quicly_ctx");
915 QUIC_DBG (1, "Trying to reallocate quicly_ctx");
919 ctx_data = malloc (sizeof (*ctx_data));
920 quicly_ctx = &ctx_data->_;
921 app->quicly_ctx = (u64 *) quicly_ctx;
922 memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
924 quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
925 quicly_ctx->tls = &quic_tlsctx;
926 quicly_ctx->stream_open = &on_stream_open;
927 quicly_ctx->closed_by_peer = &on_closed_by_peer;
928 quicly_ctx->now = &quicly_vpp_now_cb;
930 quicly_amend_ptls_context (quicly_ctx->tls);
932 quicly_ctx->event_log.mask = 0; /* logs */
933 quicly_ctx->event_log.cb = quicly_new_default_event_logger (stderr);
935 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
936 quicly_ctx->transport_params.max_streams_uni = QUIC_INT_MAX;
937 quicly_ctx->transport_params.max_streams_bidi = QUIC_INT_MAX;
938 quicly_ctx->transport_params.max_stream_data.bidi_local = (QUIC_FIFO_SIZE - 1); /* max_enq is SIZE - 1 */
939 quicly_ctx->transport_params.max_stream_data.bidi_remote = (QUIC_FIFO_SIZE - 1); /* max_enq is SIZE - 1 */
940 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
942 quicly_ctx->tls->random_bytes (ctx_data->cid_key, 16);
943 ctx_data->cid_key[16] = 0;
944 key_vec = ptls_iovec_init (ctx_data->cid_key, strlen (ctx_data->cid_key));
945 quicly_ctx->cid_encryptor =
946 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
947 &ptls_openssl_sha256, key_vec);
948 if (!is_client && app->tls_key != NULL && app->tls_cert != NULL)
950 load_bio_private_key (quicly_ctx->tls, (char *) app->tls_key);
951 load_bio_certificate_chain (quicly_ctx->tls, (char *) app->tls_cert);
956 /*****************************************************************************
957 * BEGIN TIMERS HANDLING
958 *****************************************************************************/
961 quic_get_thread_time (u8 thread_index)
963 return quic_main.wrk_ctx[thread_index].time_now;
967 quic_get_time (quicly_now_t * self)
969 u8 thread_index = vlib_get_thread_index ();
970 return quic_get_thread_time (thread_index);
974 quic_set_time_now (u32 thread_index)
976 vlib_main_t *vlib_main = vlib_get_main ();
977 f64 time = vlib_time_now (vlib_main);
978 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
979 return quic_main.wrk_ctx[thread_index].time_now;
982 /* Transport proto callback */
984 quic_update_time (f64 now, u8 thread_index)
986 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
988 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
989 quic_set_time_now (thread_index);
990 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
994 quic_timer_expired (u32 conn_index)
997 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
998 quic_get_time (NULL));
999 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
1000 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1001 quic_send_packets (ctx);
1005 quic_update_timer (quic_ctx_t * ctx)
1007 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1008 int64_t next_timeout, next_interval;
1009 session_t *quic_session;
1011 /* This timeout is in ms which is the unit of our timer */
1012 next_timeout = quicly_get_first_timeout (ctx->c_quic_ctx_id.conn);
1013 next_interval = next_timeout - quic_get_time (NULL);
1015 if (next_timeout == 0 || next_interval <= 0)
1017 if (ctx->c_s_index == QUIC_SESSION_INVALID)
1023 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
1024 if (svm_fifo_set_event (quic_session->tx_fifo))
1025 session_send_io_evt_to_thread_custom (quic_session,
1026 quic_session->thread_index,
1027 SESSION_IO_EVT_BUILTIN_TX);
1032 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
1034 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
1035 next_interval, ctx->c_c_index);
1037 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
1039 if (next_timeout == INT64_MAX)
1041 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
1045 tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index, 0, next_interval);
1049 if (next_timeout == INT64_MAX)
1051 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
1052 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1053 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
1056 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
1063 quic_expired_timers_dispatch (u32 * expired_timers)
1067 for (i = 0; i < vec_len (expired_timers); i++)
1069 quic_timer_expired (expired_timers[i]);
1074 /*****************************************************************************
1075 * END TIMERS HANDLING
1077 * BEGIN TRANSPORT PROTO FUNCTIONS
1078 *****************************************************************************/
1081 quic_connect (transport_endpoint_cfg_t * tep)
1083 QUIC_DBG (2, "Called quic_connect");
1084 session_endpoint_cfg_t *sep;
1085 int connect_stream = 0;
1087 sep = (session_endpoint_cfg_t *) tep;
1091 /* TODO: better logic to detect if this is a stream or a connection request */
1097 return quic_connect_new_stream (sep);
1101 return quic_connect_new_connection (sep);
1106 quic_connect_new_stream (session_endpoint_cfg_t * sep)
1108 uint64_t quic_session_handle;
1109 session_t *quic_session, *stream_session;
1110 quic_stream_data_t *stream_data;
1111 quicly_stream_t *stream;
1112 quicly_conn_t *conn;
1113 app_worker_t *app_wrk;
1114 quic_ctx_t *qctx, *sctx;
1116 quic_main_t *qm = &quic_main;
1119 /* Find base session to which the user want to attach a stream */
1120 quic_session_handle = sep->transport_opts;
1121 QUIC_DBG (2, "Opening new stream (qsession %u)", sep->transport_opts);
1122 quic_session = session_get_from_handle (quic_session_handle);
1124 if (quic_session->session_type !=
1125 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, sep->is_ip4))
1127 QUIC_DBG (1, "received incompatible session");
1131 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
1134 QUIC_DBG (1, "Invalid app worker :(");
1138 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
1139 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
1141 quic_ctx_get (quic_session->connection_index, quic_session->thread_index);
1142 if (qctx->c_quic_ctx_id.is_stream)
1144 QUIC_DBG (1, "session is a stream");
1145 quic_ctx_free (sctx);
1149 sctx->c_quic_ctx_id.parent_app_wrk_id =
1150 qctx->c_quic_ctx_id.parent_app_wrk_id;
1151 sctx->c_quic_ctx_id.parent_app_id = qctx->c_quic_ctx_id.parent_app_id;
1152 sctx->c_quic_ctx_id.quic_connection_ctx_id = qctx->c_c_index;
1153 sctx->c_c_index = sctx_index;
1154 sctx->c_quic_ctx_id.is_stream = 1;
1156 conn = qctx->c_quic_ctx_id.conn;
1158 if (!conn || !quicly_connection_is_ready (conn))
1161 if ((rv = quicly_open_stream (conn, &stream, 0 /* uni */ )))
1163 QUIC_DBG (2, "Stream open failed with %d", rv);
1166 sctx->c_quic_ctx_id.stream = stream;
1168 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1170 stream_session = session_alloc (qctx->c_thread_index);
1171 QUIC_DBG (2, "Allocated stream_session, id %u, thread %u ctx %u",
1172 stream_session->session_index, stream_session->thread_index,
1174 stream_session->flags |= SESSION_F_QUIC_STREAM;
1175 stream_session->app_wrk_index = app_wrk->wrk_index;
1176 stream_session->connection_index = sctx_index;
1177 stream_session->listener_index = qm->fake_app_listener_index;
1178 stream_session->session_type =
1179 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
1180 qctx->c_quic_ctx_id.udp_is_ip4);
1182 sctx->c_s_index = stream_session->session_index;
1184 if (app_worker_init_connected (app_wrk, stream_session))
1186 QUIC_DBG (1, "failed to app_worker_init_connected");
1187 quicly_reset_stream (stream, 0x30003);
1188 session_free_w_fifos (stream_session);
1189 quic_ctx_free (sctx);
1190 return app_worker_connect_notify (app_wrk, NULL, sep->opaque);
1193 stream_session->session_state = SESSION_STATE_READY;
1194 if (app_worker_connect_notify (app_wrk, stream_session, sep->opaque))
1196 QUIC_DBG (1, "failed to notify app");
1197 quicly_reset_stream (stream, 0x30004);
1198 session_free_w_fifos (stream_session);
1199 quic_ctx_free (sctx);
1202 session_lookup_add_connection (&sctx->connection,
1203 session_handle (stream_session));
1204 stream_data = (quic_stream_data_t *) stream->data;
1205 stream_data->ctx_id = sctx->c_c_index;
1206 stream_data->thread_index = sctx->c_thread_index;
1211 quic_connect_new_connection (session_endpoint_cfg_t * sep)
1213 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
1214 quic_main_t *qm = &quic_main;
1216 app_worker_t *app_wrk;
1221 ctx_index = quic_ctx_alloc (vlib_get_thread_index ());
1222 ctx = quic_ctx_get (ctx_index, vlib_get_thread_index ());
1223 ctx->c_quic_ctx_id.parent_app_wrk_id = sep->app_wrk_index;
1224 ctx->c_s_index = QUIC_SESSION_INVALID;
1225 ctx->c_c_index = ctx_index;
1226 ctx->c_quic_ctx_id.udp_is_ip4 = sep->is_ip4;
1227 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1228 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1229 ctx->client_opaque = sep->opaque;
1232 ctx->srv_hostname = format (0, "%v", sep->hostname);
1233 vec_terminate_c_string (ctx->srv_hostname);
1237 /* needed by quic for crypto + determining client / server */
1239 format (0, "%U", format_ip46_address, &sep->ip, sep->is_ip4);
1242 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1243 cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1244 cargs->app_index = qm->app_index;
1245 cargs->api_context = ctx_index;
1247 app_wrk = app_worker_get (sep->app_wrk_index);
1248 app = application_get (app_wrk->app_index);
1249 ctx->c_quic_ctx_id.parent_app_id = app_wrk->app_index;
1250 cargs->sep_ext.ns_index = app->ns_index;
1252 allocate_quicly_ctx (app, 1 /* is client */ );
1254 if ((error = vnet_connect (cargs)))
1261 quic_disconnect (u32 ctx_index, u32 thread_index)
1263 QUIC_DBG (2, "Called quic_disconnect");
1266 ctx = quic_ctx_get (ctx_index, thread_index);
1267 if (ctx->c_quic_ctx_id.is_stream)
1269 QUIC_DBG (2, "Closing stream %x, session %x", ctx_index,
1271 quicly_stream_t *stream = ctx->c_quic_ctx_id.stream;
1272 quicly_reset_stream (stream, 0x30000);
1276 QUIC_DBG (2, "Closing connection %x, session %x", ctx_index,
1278 quicly_conn_t *conn = ctx->c_quic_ctx_id.conn;
1279 /* Start connection closing. Keep sending packets until quicly_send
1280 returns QUICLY_ERROR_FREE_CONNECTION */
1281 quicly_close (conn, 0, "");
1282 /* This also causes all streams to be closed (and the cb called) */
1283 quic_send_packets (ctx);
1288 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1290 vnet_listen_args_t _bargs, *args = &_bargs;
1291 quic_main_t *qm = &quic_main;
1292 session_handle_t udp_handle;
1293 session_endpoint_cfg_t *sep;
1294 session_t *udp_listen_session;
1295 app_worker_t *app_wrk;
1299 app_listener_t *app_listener;
1301 sep = (session_endpoint_cfg_t *) tep;
1302 app_wrk = app_worker_get (sep->app_wrk_index);
1303 /* We need to call this because we call app_worker_init_connected in
1304 * quic_accept_stream, which assumes the connect segment manager exists */
1305 app_worker_alloc_connects_segment_manager (app_wrk);
1306 app = application_get (app_wrk->app_index);
1307 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1309 allocate_quicly_ctx (app, 0 /* is_client */ );
1311 sep->transport_proto = TRANSPORT_PROTO_UDPC;
1312 memset (args, 0, sizeof (*args));
1313 args->app_index = qm->app_index;
1314 args->sep_ext = *sep;
1315 args->sep_ext.ns_index = app->ns_index;
1316 if (vnet_listen (args))
1319 lctx_index = quic_ctx_alloc (0); /* listener */
1320 udp_handle = args->handle;
1321 app_listener = app_listener_get_w_handle (udp_handle);
1322 udp_listen_session = app_listener_get_session (app_listener);
1323 udp_listen_session->opaque = lctx_index;
1325 lctx = quic_ctx_get (lctx_index, 0); /* listener */
1326 lctx->is_listener = 1;
1327 lctx->c_quic_ctx_id.parent_app_wrk_id = sep->app_wrk_index;
1328 lctx->c_quic_ctx_id.parent_app_id = app_wrk->app_index;
1329 lctx->c_quic_ctx_id.udp_session_handle = udp_handle;
1330 lctx->c_quic_ctx_id.udp_is_ip4 = sep->is_ip4;
1331 lctx->c_s_index = quic_listen_session_index;
1333 QUIC_DBG (2, "Started listening %d", lctx_index);
1338 quic_stop_listen (u32 lctx_index)
1340 QUIC_DBG (2, "Called quic_stop_listen");
1343 lctx = quic_ctx_get (lctx_index, 0); /* listener */
1344 vnet_unlisten_args_t a = {
1345 .handle = lctx->c_quic_ctx_id.udp_session_handle,
1346 .app_index = quic_main.app_index,
1347 .wrk_map_index = 0 /* default wrk */
1349 if (vnet_unlisten (&a))
1350 clib_warning ("unlisten errored");
1352 /* TODO: crypto state cleanup */
1354 quic_ctx_free (lctx); /* listener */
1358 static transport_connection_t *
1359 quic_connection_get (u32 ctx_index, u32 thread_index)
1361 QUIC_DBG (2, "Called quic_connection_get");
1363 ctx = quic_ctx_get (ctx_index, thread_index);
1364 return &ctx->connection;
1367 static transport_connection_t *
1368 quic_listener_get (u32 listener_index)
1370 QUIC_DBG (2, "Called quic_listener_get");
1372 ctx = quic_ctx_get (listener_index, 0);
1373 return &ctx->connection;
1377 format_quic_connection (u8 * s, va_list * args)
1379 s = format (s, "[QUIC] connection"); /* TODO */
1384 format_quic_half_open (u8 * s, va_list * args)
1386 u32 qc_index = va_arg (*args, u32);
1387 quic_ctx_t *ctx = quic_ctx_get (qc_index, vlib_get_thread_index ());
1388 s = format (s, "[QUIC] half-open app %u", ctx->c_quic_ctx_id.parent_app_id);
1394 format_quic_listener (u8 * s, va_list * args)
1396 s = format (s, "[QUIC] listener"); /* TODO */
1400 /*****************************************************************************
1401 * END TRANSPORT PROTO FUNCTIONS
1403 * START SESSION CALLBACKS
1404 * Called from UDP layer
1405 *****************************************************************************/
1408 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1409 ip46_address_t * addr, u16 port, u8 is_ip4)
1413 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1414 sa4->sin_family = AF_INET;
1415 sa4->sin_port = port;
1416 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1417 *salen = sizeof (struct sockaddr_in);
1421 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1422 sa6->sin6_family = AF_INET6;
1423 sa6->sin6_port = port;
1424 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1425 *salen = sizeof (struct sockaddr_in6);
1430 quic_notify_app_connected (quic_ctx_t * ctx)
1432 QUIC_DBG (1, "quic_notify_app_connected");
1433 session_t *quic_session;
1434 app_worker_t *app_wrk;
1435 u32 ctx_id = ctx->c_c_index;
1436 u32 thread_index = ctx->c_thread_index;
1437 quic_main_t *qm = &quic_main;
1439 app_wrk = app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_id);
1442 quic_disconnect_transport (ctx);
1446 quic_session = session_alloc (thread_index);
1448 QUIC_DBG (2, "Allocated quic_session, id %u, thread %u",
1449 quic_session->session_index, quic_session->thread_index);
1450 ctx->c_s_index = quic_session->session_index;
1451 quic_session->app_wrk_index = ctx->c_quic_ctx_id.parent_app_wrk_id;
1452 quic_session->connection_index = ctx->c_c_index;
1453 quic_session->listener_index = qm->fake_app_listener_index;
1454 quic_session->session_type =
1455 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
1456 ctx->c_quic_ctx_id.udp_is_ip4);
1458 if (app_worker_init_connected (app_wrk, quic_session))
1460 QUIC_DBG (1, "failed to app_worker_init_connected");
1461 quic_disconnect (ctx_id, thread_index);
1462 return app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1465 quic_session->session_state = SESSION_STATE_CONNECTING;
1466 if (app_worker_connect_notify (app_wrk, quic_session, ctx->client_opaque))
1468 QUIC_DBG (1, "failed to notify app");
1469 quic_disconnect (ctx_id, thread_index);
1473 /* If the app opens a stream in its callback it may invalidate ctx */
1474 ctx = quic_ctx_get (ctx_id, thread_index);
1475 quic_session->session_state = SESSION_STATE_LISTENING;
1476 session_lookup_add_connection (&ctx->connection,
1477 session_handle (quic_session));
1483 quic_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1484 session_t * udp_session, u8 is_fail)
1486 QUIC_DBG (2, "QSession is now connected (id %u)",
1487 udp_session->session_index);
1488 /* This should always be called before quic_connect returns since UDP always
1489 * connects instantly. */
1490 clib_bihash_kv_16_8_t kv;
1491 struct sockaddr_in6 sa6;
1492 struct sockaddr *sa = (struct sockaddr *) &sa6;
1494 transport_connection_t *tc;
1495 app_worker_t *app_wrk;
1496 quicly_conn_t *conn;
1499 u32 thread_index = vlib_get_thread_index ();
1502 ctx = quic_ctx_get (ctx_index, thread_index);
1509 app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_id);
1512 api_context = ctx->c_s_index;
1513 app_worker_connect_notify (app_wrk, 0, api_context);
1518 app_wrk = app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_id);
1521 QUIC_DBG (1, "Appwrk not found");
1524 app = application_get (app_wrk->app_index);
1526 ctx->c_thread_index = thread_index;
1527 ctx->c_c_index = ctx_index;
1529 QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1530 is_fail, thread_index, (ctx) ? ctx_index : ~0);
1532 ctx->c_quic_ctx_id.udp_session_handle = session_handle (udp_session);
1533 udp_session->opaque = ctx->c_quic_ctx_id.parent_app_id;
1534 udp_session->session_state = SESSION_STATE_READY;
1536 /* Init QUIC lib connection
1537 * Generate required sockaddr & salen */
1538 tc = session_get_transport (udp_session);
1539 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1542 quicly_connect (&ctx->c_quic_ctx_id.conn,
1543 (quicly_context_t *) app->quicly_ctx,
1544 (char *) ctx->srv_hostname, sa, salen,
1545 &quic_main.next_cid, &quic_main.hs_properties, NULL);
1546 ++quic_main.next_cid.master_id;
1547 /* Save context handle in quicly connection */
1548 quic_store_conn_ctx (ctx->c_quic_ctx_id.conn, ctx);
1551 /* Register connection in connections map */
1552 conn = ctx->c_quic_ctx_id.conn;
1553 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1554 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1555 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1556 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1558 quic_send_packets (ctx);
1560 /* UDP stack quirk? preemptively transfer connection if that happens */
1561 if (udp_session->thread_index != thread_index)
1562 quic_transfer_connection (ctx_index, udp_session->thread_index);
1568 quic_receive_connection (void *arg)
1570 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1571 quic_ctx_t *temp_ctx, *new_ctx;
1572 clib_bihash_kv_16_8_t kv;
1573 quicly_conn_t *conn;
1576 new_ctx_id = quic_ctx_alloc (thread_index);
1577 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1579 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1583 memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1586 new_ctx->c_thread_index = thread_index;
1587 new_ctx->c_c_index = new_ctx_id;
1589 conn = new_ctx->c_quic_ctx_id.conn;
1590 quic_store_conn_ctx (conn, new_ctx);
1591 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1592 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1593 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1594 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1595 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1596 quic_update_timer (new_ctx);
1598 /* Trigger read on this connection ? */
1602 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1604 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1605 quic_ctx_t *ctx, *temp_ctx;
1606 clib_bihash_kv_16_8_t kv;
1607 quicly_conn_t *conn;
1608 u32 thread_index = vlib_get_thread_index ();
1610 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1612 temp_ctx = malloc (sizeof (quic_ctx_t));
1614 ctx = quic_ctx_get (ctx_index, thread_index);
1616 memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1618 /* Remove from lookup hash, timer wheel and thread-local pool */
1619 conn = ctx->c_quic_ctx_id.conn;
1620 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1621 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
1622 if (ctx->timer_handle != QUIC_TIMER_HANDLE_INVALID)
1624 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1625 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
1627 quic_ctx_free (ctx);
1629 /* Send connection to destination thread */
1630 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1635 quic_transfer_connection_rpc (void *arg)
1637 u64 arg_int = (u64) arg;
1638 u32 ctx_index, dest_thread;
1640 ctx_index = (u32) (arg_int >> 32);
1641 dest_thread = (u32) (arg_int & UINT32_MAX);
1642 quic_transfer_connection (ctx_index, dest_thread);
1646 * This assumes that the connection is not yet associated to a session
1647 * So currently it only works on the client side when receiving the first packet
1651 quic_move_connection_to_thread (u32 ctx_index, u32 owner_thread,
1654 QUIC_DBG (2, "Requesting transfer of conn %u from thread %u", ctx_index,
1656 u64 arg = ((u64) ctx_index) << 32 | to_thread;
1657 session_send_rpc_evt_to_thread (owner_thread, quic_transfer_connection_rpc,
1662 quic_session_disconnect_callback (session_t * s)
1664 clib_warning ("UDP session disconnected???");
1668 quic_session_reset_callback (session_t * s)
1670 clib_warning ("UDP session reset???");
1674 quic_session_accepted_callback (session_t * udp_session)
1676 /* New UDP connection, try to accept it */
1677 QUIC_DBG (2, "UDP session accepted");
1680 quic_ctx_t *ctx, *lctx;
1681 session_t *udp_listen_session;
1682 u32 thread_index = vlib_get_thread_index ();
1684 udp_listen_session = listen_session_get (udp_session->listener_index);
1686 ctx_index = quic_ctx_alloc (thread_index);
1687 ctx = quic_ctx_get (ctx_index, thread_index);
1688 ctx->c_thread_index = udp_session->thread_index;
1689 ctx->c_c_index = ctx_index;
1690 ctx->c_s_index = QUIC_SESSION_INVALID;
1691 ctx->c_quic_ctx_id.udp_session_handle = session_handle (udp_session);
1692 ctx->c_quic_ctx_id.listener_ctx_id = udp_listen_session->opaque;
1693 lctx = quic_ctx_get (udp_listen_session->opaque,
1694 udp_listen_session->thread_index);
1695 ctx->c_quic_ctx_id.udp_is_ip4 = lctx->c_quic_ctx_id.udp_is_ip4;
1696 ctx->c_quic_ctx_id.parent_app_id = lctx->c_quic_ctx_id.parent_app_id;
1697 ctx->c_quic_ctx_id.parent_app_wrk_id =
1698 lctx->c_quic_ctx_id.parent_app_wrk_id;
1699 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1700 ctx->conn_state = QUIC_CONN_STATE_OPENED;
1702 udp_session->opaque = ctx->c_quic_ctx_id.parent_app_id;
1704 /* Put this ctx in the "opening" pool */
1705 pool_get (quic_main.wrk_ctx[ctx->c_thread_index].opening_ctx_pool,
1707 *pool_index = ctx_index;
1709 /* TODO timeout to delete these if they never connect */
1714 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1716 QUIC_DBG (2, "Called quic_add_segment_callback");
1717 QUIC_DBG (2, "NOT IMPLEMENTED");
1718 /* No-op for builtin */
1723 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1725 QUIC_DBG (2, "Called quic_del_segment_callback");
1726 QUIC_DBG (2, "NOT IMPLEMENTED");
1727 /* No-op for builtin */
1732 quic_custom_tx_callback (void *s)
1734 session_t *stream_session = (session_t *) s;
1735 quicly_stream_t *stream;
1739 svm_fifo_unset_event (stream_session->tx_fifo);
1741 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1744 quic_ctx_get (stream_session->connection_index,
1745 stream_session->thread_index);
1746 if (PREDICT_FALSE (!ctx->c_quic_ctx_id.is_stream))
1748 goto tx_end; /* Most probably a reschedule */
1751 stream = ctx->c_quic_ctx_id.stream;
1752 if (!quicly_sendstate_is_open (&stream->sendstate))
1754 QUIC_DBG (1, "Warning: tried to send on closed stream");
1758 if ((rv = quicly_stream_sync_sendbuf (stream, 1)) != 0)
1762 quic_send_packets (ctx);
1768 * Returns 0 if a matching connection is found and is on the right thread.
1769 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
1773 quic_find_packet_ctx (u32 * ctx_thread, u32 * ctx_index,
1774 struct sockaddr *sa, socklen_t salen,
1775 quicly_decoded_packet_t * packet,
1776 u32 caller_thread_index)
1779 quicly_conn_t *conn_;
1780 clib_bihash_kv_16_8_t kv;
1781 clib_bihash_16_8_t *h;
1783 h = &quic_main.connection_hash;
1784 quic_make_connection_key (&kv, &packet->cid.dest.plaintext);
1785 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
1787 if (clib_bihash_search_16_8 (h, &kv, &kv) == 0)
1789 u32 index = kv.value & UINT32_MAX;
1790 u8 thread_id = kv.value >> 32;
1791 /* Check if this connection belongs to this thread, otherwise
1792 * ask for it to be moved */
1793 if (thread_id != caller_thread_index)
1795 QUIC_DBG (2, "Connection is on wrong thread");
1796 /* Cannot make full check with quicly_is_destination... */
1798 *ctx_thread = thread_id;
1801 ctx_ = quic_ctx_get (index, vlib_get_thread_index ());
1802 conn_ = ctx_->c_quic_ctx_id.conn;
1803 if (conn_ && quicly_is_destination (conn_, sa, salen, packet))
1805 QUIC_DBG (3, "Connection found");
1807 *ctx_thread = thread_id;
1811 QUIC_DBG (3, "connection not found");
1816 quic_receive (quic_ctx_t * ctx, quicly_conn_t * conn,
1817 quicly_decoded_packet_t packet)
1820 u32 ctx_id = ctx->c_c_index;
1821 u32 thread_index = ctx->c_thread_index;
1822 /* TODO : QUICLY_ERROR_PACKET_IGNORED sould be handled */
1823 rv = quicly_receive (conn, &packet);
1826 QUIC_DBG (2, "Quicly receive ignored packet code : %u", rv);
1829 /* ctx pointer may change if a new stream is opened */
1830 ctx = quic_ctx_get (ctx_id, thread_index);
1831 /* Conn may be set to null if the connection is terminated */
1832 if (ctx->c_quic_ctx_id.conn && ctx->conn_state == QUIC_CONN_STATE_HANDSHAKE)
1834 if (quicly_connection_is_ready (conn))
1836 ctx->conn_state = QUIC_CONN_STATE_READY;
1837 if (quicly_is_client (conn))
1839 quic_notify_app_connected (ctx);
1840 ctx = quic_ctx_get (ctx_id, thread_index);
1844 return quic_send_packets (ctx);
1848 quic_create_quic_session (quic_ctx_t * ctx)
1850 session_t *quic_session;
1851 app_worker_t *app_wrk;
1853 quic_main_t *qm = &quic_main;
1856 quic_session = session_alloc (ctx->c_thread_index);
1857 QUIC_DBG (2, "Allocated quic_session, id %u, thread %u ctx %u",
1858 quic_session->session_index, quic_session->thread_index,
1860 quic_session->session_state = SESSION_STATE_LISTENING;
1861 ctx->c_s_index = quic_session->session_index;
1863 lctx = quic_ctx_get (ctx->c_quic_ctx_id.listener_ctx_id, 0);
1865 quic_session->app_wrk_index = lctx->c_quic_ctx_id.parent_app_wrk_id;
1866 quic_session->connection_index = ctx->c_c_index;
1867 quic_session->session_type =
1868 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
1869 ctx->c_quic_ctx_id.udp_is_ip4);
1870 quic_session->listener_index = qm->fake_app_listener_index;
1871 quic_session->app_index = quic_main.app_index;
1873 /* TODO: don't alloc fifos when we don't transfer data on this session
1874 * but we still need fifos for the events? */
1875 if ((rv = app_worker_init_accepted (quic_session)))
1877 QUIC_DBG (1, "failed to allocate fifos");
1878 session_free (quic_session);
1881 session_lookup_add_connection (&ctx->connection,
1882 session_handle (quic_session));
1883 app_wrk = app_worker_get (quic_session->app_wrk_index);
1884 rv = app_worker_accept_notify (app_wrk, quic_session);
1887 QUIC_DBG (1, "failed to notify accept worker app");
1894 quic_create_connection (quicly_context_t * quicly_ctx,
1895 u64 udp_session_handle, u32 ctx_index,
1896 struct sockaddr *sa,
1897 socklen_t salen, quicly_decoded_packet_t packet)
1899 clib_bihash_kv_16_8_t kv;
1901 quicly_conn_t *conn;
1902 u32 thread_index = vlib_get_thread_index ();
1905 /* new connection, accept and create context if packet is valid
1906 * TODO: check if socket is actually listening? */
1907 if ((rv = quicly_accept (&conn, quicly_ctx, sa, salen,
1908 &packet, ptls_iovec_init (NULL, 0),
1909 &quic_main.next_cid, NULL)))
1911 /* Invalid packet, pass */
1912 assert (conn == NULL);
1913 QUIC_DBG (1, "Accept failed with %d", rv);
1914 /* TODO: cleanup created quic ctx and UDP session */
1917 assert (conn != NULL);
1919 ++quic_main.next_cid.master_id;
1920 ctx = quic_ctx_get (ctx_index, thread_index);
1921 /* Save ctx handle in quicly connection */
1922 quic_store_conn_ctx (conn, ctx);
1923 ctx->c_quic_ctx_id.conn = conn;
1924 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1926 quic_create_quic_session (ctx);
1928 /* Register connection in connections map */
1929 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1930 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1931 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1932 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1934 return quic_send_packets (ctx);
1938 quic_reset_connection (quicly_context_t * quicly_ctx, u64 udp_session_handle,
1939 struct sockaddr *sa, socklen_t salen,
1940 quicly_decoded_packet_t packet)
1942 /* short header packet; potentially a dead connection. No need to check the
1943 * length of the incoming packet, because loop is prevented by authenticating
1944 * the CID (by checking node_id and thread_id). If the peer is also sending a
1945 * reset, then the next CID is highly likely to contain a non-authenticating
1947 QUIC_DBG (2, "Sending stateless reset");
1948 quicly_datagram_t *dgram;
1949 session_t *udp_session;
1950 if (packet.cid.dest.plaintext.node_id == 0
1951 && packet.cid.dest.plaintext.thread_id == 0)
1953 dgram = quicly_send_stateless_reset (quicly_ctx, sa, salen,
1954 &packet.cid.dest.plaintext);
1957 udp_session = session_get_from_handle (udp_session_handle);
1958 return quic_send_datagram (udp_session, dgram); /* TODO : set event on fifo */
1964 quic_app_rx_callback (session_t * udp_session)
1966 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
1967 quicly_decoded_packet_t packet;
1968 session_dgram_hdr_t ph;
1970 quic_ctx_t *ctx = NULL;
1973 struct sockaddr_in6 sa6;
1974 struct sockaddr *sa = (struct sockaddr *) &sa6;
1976 u32 max_deq, len, full_len, ctx_index = UINT32_MAX, ctx_thread =
1980 u32 *opening_ctx_pool, *ctx_index_ptr;
1981 u32 app_index = udp_session->opaque;
1982 u64 udp_session_handle = session_handle (udp_session);
1984 u32 thread_index = vlib_get_thread_index ();
1986 app = application_get_if_valid (app_index);
1989 QUIC_DBG (1, "Got RX on detached app");
1990 /* TODO: close this session, cleanup state? */
1996 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
1997 f = udp_session->rx_fifo;
1998 svm_fifo_unset_event (f);
1999 max_deq = svm_fifo_max_dequeue (f);
2000 if (max_deq < sizeof (session_dgram_hdr_t))
2003 ret = svm_fifo_peek (f, 0, SESSION_CONN_HDR_LEN, (u8 *) & ph);
2004 if (ret != SESSION_CONN_HDR_LEN)
2006 QUIC_DBG (1, "Not enough data for header in RX");
2009 if (ph.data_length < ph.data_offset)
2011 QUIC_DBG (1, "Not enough data vs offset in RX");
2014 len = ph.data_length - ph.data_offset;
2015 full_len = ph.data_length + ph.data_offset + SESSION_CONN_HDR_LEN;
2016 if (full_len > max_deq)
2018 QUIC_DBG (1, "Not enough data in fifo RX");
2022 /* Quicly can read len bytes from the fifo at offset:
2023 * ph.data_offset + SESSION_CONN_HDR_LEN */
2024 data = malloc (ph.data_length);
2026 svm_fifo_peek (f, ph.data_offset + SESSION_CONN_HDR_LEN,
2027 ph.data_length, data);
2028 if (ret != ph.data_length)
2030 QUIC_DBG (1, "Not enough data peeked in RX");
2036 quicly_decode_packet ((quicly_context_t *) app->quicly_ctx, &packet,
2040 quic_build_sockaddr (sa, &salen, &ph.rmt_ip, ph.rmt_port, ph.is_ip4);
2042 quicly_decode_packet ((quicly_context_t *) app->quicly_ctx, &packet,
2045 if (plen != SIZE_MAX)
2048 err = quic_find_packet_ctx (&ctx_thread, &ctx_index, sa, salen,
2049 &packet, thread_index);
2052 ctx = quic_ctx_get (ctx_index, thread_index);
2053 quic_receive (ctx, ctx->c_quic_ctx_id.conn, packet);
2055 else if (ctx_thread != UINT32_MAX)
2057 /* Connection found but on wrong thread, ask move */
2058 quic_move_connection_to_thread (ctx_index, ctx_thread,
2061 else if ((packet.octets.base[0] & QUICLY_PACKET_TYPE_BITMASK) ==
2062 QUICLY_PACKET_TYPE_INITIAL)
2064 /* Try to find matching "opening" ctx */
2066 quic_main.wrk_ctx[thread_index].opening_ctx_pool;
2069 pool_foreach (ctx_index_ptr, opening_ctx_pool,
2071 ctx = quic_ctx_get (*ctx_index_ptr, thread_index);
2072 if (ctx->c_quic_ctx_id.udp_session_handle == udp_session_handle)
2074 /* Right ctx found, create conn & remove from pool */
2075 quic_create_connection ((quicly_context_t *) app->quicly_ctx,
2076 udp_session_handle, *ctx_index_ptr,
2078 pool_put (opening_ctx_pool, ctx_index_ptr);
2079 goto ctx_search_done;
2087 quic_reset_connection ((quicly_context_t *) app->quicly_ctx,
2088 udp_session_handle, sa, salen, packet);
2092 svm_fifo_dequeue_drop (f,
2093 ph.data_length + ph.data_offset +
2094 SESSION_CONN_HDR_LEN);
2102 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2103 transport_endpoint_t * tep, u8 is_lcl)
2105 session_t *udp_session;
2106 if (ctx->c_quic_ctx_id.is_stream)
2108 tep->is_ip4 = 255; /* well this is ugly */
2113 session_get_from_handle (ctx->c_quic_ctx_id.udp_session_handle);
2114 session_get_endpoint (udp_session, tep, is_lcl);
2119 quic_get_transport_listener_endpoint (u32 listener_index,
2120 transport_endpoint_t * tep, u8 is_lcl)
2123 app_listener_t *app_listener;
2124 session_t *udp_listen_session;
2125 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2126 if (ctx->is_listener)
2129 app_listener_get_w_handle (ctx->c_quic_ctx_id.udp_session_handle);
2130 udp_listen_session = app_listener_get_session (app_listener);
2131 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2133 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2137 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2138 transport_endpoint_t * tep, u8 is_lcl)
2141 ctx = quic_ctx_get (ctx_index, thread_index);
2142 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2145 /*****************************************************************************
2146 * END TRANSPORT PROTO FUNCTIONS
2147 *****************************************************************************/
2150 static session_cb_vft_t quic_app_cb_vft = {
2151 .session_accept_callback = quic_session_accepted_callback,
2152 .session_disconnect_callback = quic_session_disconnect_callback,
2153 .session_connected_callback = quic_session_connected_callback,
2154 .session_reset_callback = quic_session_reset_callback,
2155 .add_segment_callback = quic_add_segment_callback,
2156 .del_segment_callback = quic_del_segment_callback,
2157 .builtin_app_rx_callback = quic_app_rx_callback,
2160 static const transport_proto_vft_t quic_proto = {
2161 .connect = quic_connect,
2162 .close = quic_disconnect,
2163 .start_listen = quic_start_listen,
2164 .stop_listen = quic_stop_listen,
2165 .get_connection = quic_connection_get,
2166 .get_listener = quic_listener_get,
2167 .update_time = quic_update_time,
2168 .custom_tx = quic_custom_tx_callback,
2169 .tx_type = TRANSPORT_TX_INTERNAL,
2170 .service_type = TRANSPORT_SERVICE_APP,
2171 .format_connection = format_quic_connection,
2172 .format_half_open = format_quic_half_open,
2173 .format_listener = format_quic_listener,
2174 .get_transport_endpoint = quic_get_transport_endpoint,
2175 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2179 static clib_error_t *
2180 quic_init (vlib_main_t * vm)
2182 u32 add_segment_size = (4096ULL << 20) - 1, segment_size = 512 << 20;
2183 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2184 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2185 vnet_app_attach_args_t _a, *a = &_a;
2186 u64 options[APP_OPTIONS_N_OPTIONS];
2187 quic_main_t *qm = &quic_main;
2188 u32 fifo_size = QUIC_FIFO_SIZE;
2192 num_threads = 1 /* main thread */ + vtm->n_threads;
2194 memset (a, 0, sizeof (*a));
2195 memset (options, 0, sizeof (options));
2197 a->session_cb_vft = &quic_app_cb_vft;
2198 a->api_client_index = APP_INVALID_INDEX;
2199 a->options = options;
2200 a->name = format (0, "quic");
2201 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2202 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = add_segment_size;
2203 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
2204 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
2205 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2206 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2207 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2209 if (vnet_application_attach (a))
2211 clib_warning ("failed to attach quic app");
2212 return clib_error_return (0, "failed to attach quic app");
2215 vec_validate (qm->ctx_pool, num_threads - 1);
2216 vec_validate (qm->wrk_ctx, num_threads - 1);
2217 /* Timer wheels, one per thread. */
2218 for (i = 0; i < num_threads; i++)
2220 tw = &qm->wrk_ctx[i].timer_wheel;
2221 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2222 1e-3 /* timer period 1ms */ , ~0);
2223 tw->last_run_time = vlib_time_now (vlib_get_main ());
2226 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2229 if (!qm->ca_cert_path)
2230 qm->ca_cert_path = QUIC_DEFAULT_CA_CERT_PATH;
2232 qm->app_index = a->app_index;
2234 /* Fake app listener hack, to remove */
2235 app = application_get (a->app_index);
2236 app_listener_t *fake_app_listener;
2237 pool_get (app->listeners, fake_app_listener);
2238 clib_memset (fake_app_listener, 0, sizeof (*fake_app_listener));
2239 fake_app_listener->al_index = fake_app_listener - app->listeners;
2240 fake_app_listener->app_index = app->app_index;
2241 fake_app_listener->session_index = SESSION_INVALID_INDEX;
2242 fake_app_listener->local_index = SESSION_INVALID_INDEX;
2243 qm->fake_app_listener_index = fake_app_listener->al_index;
2244 /* End fake listener hack */
2246 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2247 / QUIC_TSTAMP_RESOLUTION;
2249 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2250 FIB_PROTOCOL_IP4, ~0);
2251 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2252 FIB_PROTOCOL_IP6, ~0);
2258 VLIB_INIT_FUNCTION (quic_init);
2261 VLIB_PLUGIN_REGISTER () =
2263 .version = VPP_BUILD_VER,
2264 .description = "Quic transport protocol",
2269 * fd.io coding-style-patch-verification: ON
2272 * eval: (c-set-style "gnu")