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/streambuf.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 int64_t quic_get_time (quicly_now_cb * self);
37 static void quic_connection_closed (u32 conn_index);
38 static void quic_disconnect (u32 ctx_index, u32 thread_index);
39 static int quic_connect_new_stream (session_endpoint_cfg_t * sep);
40 static int quic_connect_new_connection (session_endpoint_cfg_t * sep);
42 #define QUIC_INT_MAX 0x3FFFFFFFFFFFFFFF
47 u8 thread_index = vlib_get_thread_index ();
48 quic_main_t *qm = &quic_main;
51 pool_get (qm->ctx_pool[thread_index], ctx);
53 memset (ctx, 0, sizeof (quic_ctx_t));
54 ctx->c_thread_index = thread_index;
55 return ctx - qm->ctx_pool[thread_index];
59 quic_ctx_free (quic_ctx_t * ctx)
61 QUIC_DBG (2, "Free ctx %u", ctx->c_c_index);
62 u32 thread_index = ctx->c_thread_index;
64 memset (ctx, 0xfb, sizeof (*ctx));
65 pool_put (quic_main.ctx_pool[thread_index], ctx);
69 quic_ctx_get (u32 ctx_index)
71 return pool_elt_at_index (quic_main.ctx_pool[vlib_get_thread_index ()],
76 quic_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
78 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
82 quic_disconnect_transport (quic_ctx_t * ctx)
84 QUIC_DBG (2, "Called quic_disconnect_transport");
85 vnet_disconnect_args_t a = {
86 .handle = ctx->c_quic_ctx_id.udp_session_handle,
87 .app_index = quic_main.app_index,
90 if (vnet_disconnect_session (&a))
91 clib_warning ("UDP session disconnect errored");
95 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
97 // QUIC_DBG (2, "Called quic_send_datagram at %ld", quic_get_time (NULL));
99 session_dgram_hdr_t hdr;
103 transport_connection_t *tc;
105 len = packet->data.len;
106 f = udp_session->tx_fifo;
107 tc = session_get_transport (udp_session);
109 max_enqueue = svm_fifo_max_enqueue (f);
110 if (max_enqueue <= sizeof (session_dgram_hdr_t))
113 max_enqueue -= sizeof (session_dgram_hdr_t);
115 if (max_enqueue < len)
118 // Build packet header for fifo
119 hdr.data_length = len;
121 hdr.is_ip4 = tc->is_ip4;
122 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
123 hdr.lcl_port = tc->lcl_port;
125 // Read dest address from quicly-provided sockaddr
128 ASSERT (packet->sa.sa_family == AF_INET);
129 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->sa;
130 hdr.rmt_port = sa4->sin_port;
131 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
135 ASSERT (packet->sa.sa_family == AF_INET6);
136 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->sa;
137 hdr.rmt_port = sa6->sin6_port;
138 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
141 rv = svm_fifo_enqueue_nowait (f, sizeof (hdr), (u8 *) & hdr);
142 ASSERT (rv == sizeof (hdr));
143 if (svm_fifo_enqueue_nowait (f, len, packet->data.base) != len)
149 quic_send_packets (quic_ctx_t * ctx)
151 //QUIC_DBG (2, "Called quic_send_packets");
152 quicly_datagram_t *packets[16];
153 session_t *udp_session;
155 size_t num_packets, i;
158 if (ctx->c_quic_ctx_id.is_stream)
160 // We have sctx, get qctx
161 ctx = quic_ctx_get (ctx->c_quic_ctx_id.quic_connection_ctx_id);
164 ASSERT (!ctx->c_quic_ctx_id.is_stream);
167 session_get_from_handle (ctx->c_quic_ctx_id.udp_session_handle);
168 conn = ctx->c_quic_ctx_id.conn;
175 num_packets = sizeof (packets) / sizeof (packets[0]);
176 if ((ret = quicly_send (conn, packets, &num_packets)) == 0)
178 for (i = 0; i != num_packets; ++i)
180 if (quic_send_datagram (udp_session, packets[i]))
182 QUIC_DBG (2, "quic_send_datagram failed");
186 quicly_default_free_packet_cb.cb
187 (&quicly_default_free_packet_cb, packets[i]);
192 QUIC_DBG (2, "quicly_send returned %d, closing connection\n", ret);
196 while (ret == 0 && num_packets == sizeof (packets) / sizeof (packets[0]));
199 if (svm_fifo_set_event (udp_session->tx_fifo))
200 session_send_io_evt_to_thread (udp_session->tx_fifo, FIFO_EVENT_APP_TX);
202 quic_update_timer (ctx);
206 /*****************************************************************************
207 * START QUICLY CALLBACKS
208 * Called from QUIC lib
209 *****************************************************************************/
212 quic_on_stop_sending (quicly_stream_t * stream, int error_code)
214 QUIC_DBG (2, "received STOP_SENDING: %d", error_code);
219 quic_on_receive_reset (quicly_stream_t * stream, int error_code)
221 QUIC_DBG (2, "received RESET_STREAM: %d", error_code);
226 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
229 QUIC_DBG (2, "received data: %lu bytes", len);
230 u32 to_enqueue, ctx_id;
232 session_t *stream_session;
234 app_worker_t *app_wrk;
236 ctx_id = ((quic_stream_data_t *) stream->data)->ctx_id;
237 sctx = quic_ctx_get (ctx_id);
238 stream_session = session_get (sctx->c_s_index, vlib_get_thread_index ());
239 rx_fifo = stream_session->rx_fifo;
240 to_enqueue = svm_fifo_max_enqueue (rx_fifo);
241 if (to_enqueue > len)
243 // TODO what happens to the excess bytes?
245 svm_fifo_enqueue_nowait (rx_fifo, to_enqueue, src);
248 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
249 if (PREDICT_TRUE (app_wrk != 0))
250 app_worker_lock_and_send_event (app_wrk, stream_session,
255 static const quicly_stream_callbacks_t quic_stream_callbacks = {
256 .on_destroy = quicly_streambuf_destroy,
257 .on_send_shift = quicly_streambuf_egress_shift,
258 .on_send_emit = quicly_streambuf_egress_emit,
259 .on_send_stop = quic_on_stop_sending,
260 .on_receive = quic_on_receive,
261 .on_receive_reset = quic_on_receive_reset
265 quic_accept_stream (void *s)
267 quicly_stream_t *stream = (quicly_stream_t *) s;
268 session_t *stream_session, *quic_session;
269 quic_stream_data_t *stream_data;
270 app_worker_t *app_wrk;
271 quic_ctx_t *qctx, *sctx;
272 u32 qctx_id, sctx_id;
275 sctx_id = quic_ctx_alloc ();
277 qctx_id = (u64) * quicly_get_data (stream->conn);
278 qctx = quic_ctx_get (qctx_id);
280 stream_session = session_alloc (qctx->c_thread_index);
281 QUIC_DBG (1, "Created stream_session, id %u ctx %u",
282 stream_session->session_index, sctx_id);
284 sctx = quic_ctx_get (sctx_id);
285 sctx->c_quic_ctx_id.parent_app_wrk_id =
286 qctx->c_quic_ctx_id.parent_app_wrk_id;
287 sctx->c_quic_ctx_id.parent_app_id = qctx->c_quic_ctx_id.parent_app_id;
288 sctx->c_quic_ctx_id.quic_connection_ctx_id = qctx->c_c_index;
289 sctx->c_c_index = sctx_id;
290 sctx->c_quic_ctx_id.is_stream = 1;
291 sctx->c_s_index = stream_session->session_index;
292 sctx->c_quic_ctx_id.stream = stream;
293 sctx->c_quic_ctx_id.stream_session_handle = session_handle (stream_session);
296 session_get_from_handle (qctx->c_quic_ctx_id.quic_session_handle);
297 stream_data = (quic_stream_data_t *) stream->data;
298 stream_data->ctx_id = sctx_id;
300 sctx->c_s_index = stream_session->session_index;
301 stream_session->session_state = SESSION_STATE_CREATED;
302 stream_session->app_wrk_index = sctx->c_quic_ctx_id.parent_app_wrk_id;
303 stream_session->connection_index = sctx->c_c_index;
304 stream_session->session_type =
305 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
306 qctx->c_quic_ctx_id.udp_is_ip4);
307 stream_session->opaque = QUIC_SESSION_TYPE_STREAM;
308 stream_session->listener_index = quic_session->session_index;
309 stream_session->app_index = sctx->c_quic_ctx_id.parent_app_id;
311 app_wrk = app_worker_get (stream_session->app_wrk_index);
312 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
314 QUIC_DBG (1, "failed to allocate fifos");
315 session_free (stream_session);
316 quicly_reset_stream (stream, 0x30001);
320 rv = app_worker_accept_notify (app_wrk, stream_session);
323 QUIC_DBG (1, "failed to notify accept worker app");
324 session_free_w_fifos (stream_session);
325 quicly_reset_stream (stream, 0x30002);
328 session_lookup_add_connection (&sctx->connection,
329 session_handle (stream_session));
333 quic_on_stream_open (quicly_stream_open_cb * self, quicly_stream_t * stream)
335 QUIC_DBG (2, "on_stream_open called");
338 quicly_streambuf_create (stream, sizeof (quic_stream_data_t))) != 0)
342 stream->callbacks = &quic_stream_callbacks;
343 // Notify accept on parent qsession, but only if this is not a locally
345 if (!quicly_stream_is_self_initiated (stream))
347 quic_accept_stream (stream);
352 static quicly_stream_open_cb on_stream_open = { &quic_on_stream_open };
355 quic_on_conn_close (quicly_closed_by_peer_cb * self, quicly_conn_t * conn,
356 int code, uint64_t frame_type,
357 const char *reason, size_t reason_len)
359 QUIC_DBG (2, "connection closed, reason: %s", reason);
360 u32 ctx_index = (u64) * quicly_get_data (conn);
361 quic_ctx_t *ctx = quic_ctx_get (ctx_index);
362 session_transport_closing_notify (&ctx->connection);
365 static quicly_closed_by_peer_cb on_closed_by_peer = { &quic_on_conn_close };
368 /*****************************************************************************
369 * END QUICLY CALLBACKS
370 *****************************************************************************/
372 /* single-entry session cache */
373 struct st_util_session_cache_t
375 ptls_encrypt_ticket_t super;
381 encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
382 int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
384 struct st_util_session_cache_t *self = (void *) _self;
390 /* replace the cached entry along with a newly generated session id */
391 free (self->data.base);
392 if ((self->data.base = malloc (src.len)) == NULL)
393 return PTLS_ERROR_NO_MEMORY;
395 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
396 memcpy (self->data.base, src.base, src.len);
397 self->data.len = src.len;
399 /* store the session id in buffer */
400 if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
402 memcpy (dst->base + dst->off, self->id, sizeof (self->id));
403 dst->off += sizeof (self->id);
409 /* check if session id is the one stored in cache */
410 if (src.len != sizeof (self->id))
411 return PTLS_ERROR_SESSION_NOT_FOUND;
412 if (memcmp (self->id, src.base, sizeof (self->id)) != 0)
413 return PTLS_ERROR_SESSION_NOT_FOUND;
415 /* return the cached value */
416 if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
418 memcpy (dst->base + dst->off, self->data.base, self->data.len);
419 dst->off += self->data.len;
426 static struct st_util_session_cache_t sc = {
428 .cb = encrypt_ticket_cb,
432 static ptls_context_t quic_tlsctx = {
433 .random_bytes = ptls_openssl_random_bytes,
434 .get_time = &ptls_get_time,
435 .key_exchanges = ptls_openssl_key_exchanges,
436 .cipher_suites = ptls_openssl_cipher_suites,
442 .on_client_hello = NULL,
443 .emit_certificate = NULL,
444 .sign_certificate = NULL,
445 .verify_certificate = NULL,
446 .ticket_lifetime = 86400,
447 .max_early_data_size = 8192,
448 .hkdf_label_prefix__obsolete = NULL,
449 .require_dhe_on_psk = 1,
450 .encrypt_ticket = &sc.super,
455 ptls_compare_separator_line (const char *line, const char *begin_or_end,
458 int ret = strncmp (line, "-----", 5);
459 size_t text_index = 5;
463 size_t begin_or_end_length = strlen (begin_or_end);
464 ret = strncmp (line + text_index, begin_or_end, begin_or_end_length);
465 text_index += begin_or_end_length;
470 ret = line[text_index] - ' ';
476 size_t label_length = strlen (label);
477 ret = strncmp (line + text_index, label, label_length);
478 text_index += label_length;
483 ret = strncmp (line + text_index, "-----", 5);
490 ptls_get_bio_pem_object (BIO * bio, const char *label, ptls_buffer_t * buf)
492 int ret = PTLS_ERROR_PEM_LABEL_NOT_FOUND;
494 ptls_base64_decode_state_t state;
496 /* Get the label on a line by itself */
497 while (BIO_gets (bio, line, 256))
499 if (ptls_compare_separator_line (line, "BEGIN", label) == 0)
502 ptls_base64_decode_init (&state);
506 /* Get the data in the buffer */
507 while (ret == 0 && BIO_gets (bio, line, 256))
509 if (ptls_compare_separator_line (line, "END", label) == 0)
511 if (state.status == PTLS_BASE64_DECODE_DONE
512 || (state.status == PTLS_BASE64_DECODE_IN_PROGRESS
519 ret = PTLS_ERROR_INCORRECT_BASE64;
525 ret = ptls_base64_decode (line, &state, buf);
533 ptls_load_bio_pem_objects (BIO * bio, const char *label, ptls_iovec_t * list,
534 size_t list_max, size_t * nb_objects)
543 while (count < list_max)
547 ptls_buffer_init (&buf, "", 0);
549 ret = ptls_get_bio_pem_object (bio, label, &buf);
553 if (buf.off > 0 && buf.is_allocated)
555 list[count].base = buf.base;
556 list[count].len = buf.off;
561 ptls_buffer_dispose (&buf);
566 ptls_buffer_dispose (&buf);
572 if (ret == PTLS_ERROR_PEM_LABEL_NOT_FOUND && count > 0)
582 #define PTLS_MAX_CERTS_IN_CONTEXT 16
585 ptls_load_bio_certificates (ptls_context_t * ctx, BIO * bio)
589 ctx->certificates.list =
590 (ptls_iovec_t *) malloc (PTLS_MAX_CERTS_IN_CONTEXT *
591 sizeof (ptls_iovec_t));
593 if (ctx->certificates.list == NULL)
595 ret = PTLS_ERROR_NO_MEMORY;
600 ptls_load_bio_pem_objects (bio, "CERTIFICATE", ctx->certificates.list,
601 PTLS_MAX_CERTS_IN_CONTEXT,
602 &ctx->certificates.count);
609 load_bio_certificate_chain (ptls_context_t * ctx, const char *cert_data)
612 cert_bio = BIO_new_mem_buf (cert_data, -1);
613 if (ptls_load_bio_certificates (ctx, cert_bio) != 0)
616 fprintf (stderr, "failed to load certificate:%s\n", strerror (errno));
623 load_bio_private_key (ptls_context_t * ctx, const char *pk_data)
625 static ptls_openssl_sign_certificate_t sc;
629 key_bio = BIO_new_mem_buf (pk_data, -1);
630 pkey = PEM_read_bio_PrivateKey (key_bio, NULL, NULL, NULL);
635 fprintf (stderr, "failed to read private key from app configuration\n");
639 ptls_openssl_init_sign_certificate (&sc, pkey);
640 EVP_PKEY_free (pkey);
642 ctx->sign_certificate = &sc.super;
646 quic_connection_closed (u32 ctx_index)
648 QUIC_DBG (2, "QUIC connection closed");
651 ctx = quic_ctx_get (ctx_index);
653 ASSERT (!ctx->c_quic_ctx_id.is_stream);
654 // TODO if connection is not established, just delete the session?
656 // TODO: close all streams? or is the streams closed cb called by quicly?
658 session_transport_delete_notify (&ctx->connection);
659 // Do not try to send anything anymore
660 quicly_free (ctx->c_quic_ctx_id.conn);
661 ctx->c_quic_ctx_id.conn = NULL;
666 quic_get_time (quicly_now_cb * self)
668 // TODO read value set by set_time_now?
669 // (needs to change it not to call this function)
670 vlib_main_t *vlib_main = vlib_get_main ();
671 f64 time = vlib_time_now (vlib_main);
672 return (int64_t) (time * 1000.f);
674 quicly_now_cb quicly_vpp_now_cb = { quic_get_time };
677 allocate_quicly_ctx (application_t * app, u8 is_client)
679 QUIC_DBG (2, "Called allocate_quicly_ctx");
685 quicly_context_t *quicly_ctx;
688 ctx_data = malloc (sizeof (*ctx_data));
689 quicly_ctx = &ctx_data->_;
690 app->quicly_ctx = (u64 *) quicly_ctx;
691 memcpy (quicly_ctx, &quicly_default_context, sizeof (quicly_context_t));
693 quicly_ctx->tls = &quic_tlsctx;
694 quicly_ctx->stream_open = &on_stream_open;
695 quicly_ctx->closed_by_peer = &on_closed_by_peer;
696 quicly_ctx->now = &quicly_vpp_now_cb;
698 quicly_amend_ptls_context (quicly_ctx->tls);
700 quicly_ctx->event_log.mask = 0;
701 quicly_ctx->event_log.cb = quicly_new_default_event_log_cb (stderr);
703 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
704 quicly_ctx->transport_params.max_streams_uni = QUIC_INT_MAX;
705 quicly_ctx->transport_params.max_streams_bidi = QUIC_INT_MAX;
706 quicly_ctx->transport_params.max_stream_data.bidi_local = QUIC_INT_MAX;
707 quicly_ctx->transport_params.max_stream_data.bidi_remote = QUIC_INT_MAX;
708 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
712 load_bio_private_key (quicly_ctx->tls, (char *) app->tls_key);
713 load_bio_certificate_chain (quicly_ctx->tls, (char *) app->tls_cert);
714 cid_key = ctx_data->cid_key;
715 quicly_ctx->tls->random_bytes (cid_key, 16);
717 quicly_ctx->encrypt_cid =
718 quicly_new_default_encrypt_cid_cb (&ptls_openssl_bfecb,
719 &ptls_openssl_sha256,
720 ptls_iovec_init (cid_key,
723 quicly_ctx->decrypt_cid =
724 quicly_new_default_decrypt_cid_cb (&ptls_openssl_bfecb,
725 &ptls_openssl_sha256,
726 ptls_iovec_init (cid_key,
733 /*****************************************************************************
734 * BEGIN TIMERS HANDLING
735 *****************************************************************************/
738 quic_set_time_now (u32 thread_index)
740 quic_main.wrk_ctx[thread_index].time_now = quic_get_time (NULL);
741 return quic_main.wrk_ctx[thread_index].time_now;
745 quic_timer_expired (u32 conn_index)
748 QUIC_DBG (2, "Timer expired for conn %u at %ld", conn_index,
749 quic_get_time (NULL));
750 ctx = quic_ctx_get (conn_index);
751 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
752 if (quic_send_packets (ctx))
754 quic_connection_closed (conn_index);
759 quic_update_timer (quic_ctx_t * ctx)
761 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
762 int64_t next_timeout;
764 // This timeout is in ms which is the unit of our timer
765 next_timeout = quicly_get_first_timeout (ctx->c_quic_ctx_id.conn);
766 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
767 f64 next_timeout_f = ((f64) next_timeout) / 1000.f;
769 // clib_warning ("Timer set to %ld (%lf)", next_timeout, next_timeout_f);
771 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
773 if (next_timeout == INT64_MAX)
776 tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index, 0,
781 if (next_timeout == INT64_MAX)
783 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
784 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
787 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
793 quic_expired_timers_dispatch (u32 * expired_timers)
797 for (i = 0; i < vec_len (expired_timers); i++)
799 quic_timer_expired (expired_timers[i]);
804 /*****************************************************************************
805 * END TIMERS HANDLING
807 * BEGIN TRANSPORT PROTO FUNCTIONS
808 *****************************************************************************/
811 quic_connect (transport_endpoint_cfg_t * tep)
813 QUIC_DBG (2, "Called quic_connect");
814 session_endpoint_cfg_t *sep;
815 int connect_stream = 0;
817 sep = (session_endpoint_cfg_t *) tep;
821 // TODO: better logic to detect if this is a stream or a connection request
827 return quic_connect_new_stream (sep);
831 return quic_connect_new_connection (sep);
836 quic_connect_new_stream (session_endpoint_cfg_t * sep)
838 uint64_t quic_session_handle;
839 session_t *quic_session, *stream_session;
840 quic_stream_data_t *stream_data;
841 quicly_stream_t *stream;
843 app_worker_t *app_wrk;
844 quic_ctx_t *qctx, *sctx;
848 // Find base session to which the user want to attach a stream
849 quic_session_handle = sep->transport_opts;
850 QUIC_DBG (2, "Opening new stream (qsession %u)", sep->transport_opts);
851 quic_session = session_get_from_handle (quic_session_handle);
853 if (quic_session->session_type !=
854 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, sep->is_ip4))
856 QUIC_DBG (1, "received incompatible session");
860 sctx_index = quic_ctx_alloc (); // Allocate before we get pointers
861 sctx = quic_ctx_get (sctx_index);
862 qctx = quic_ctx_get (quic_session->connection_index);
863 if (qctx->c_quic_ctx_id.is_stream)
865 QUIC_DBG (1, "session is a stream");
866 quic_ctx_free (sctx);
870 sctx->c_quic_ctx_id.parent_app_wrk_id =
871 qctx->c_quic_ctx_id.parent_app_wrk_id;
872 sctx->c_quic_ctx_id.parent_app_id = qctx->c_quic_ctx_id.parent_app_id;
873 sctx->c_quic_ctx_id.quic_connection_ctx_id = qctx->c_c_index;
874 sctx->c_c_index = sctx_index;
875 sctx->c_quic_ctx_id.is_stream = 1;
877 conn = qctx->c_quic_ctx_id.conn;
879 if (!conn || !quicly_connection_is_ready (conn))
882 if ((rv = quicly_open_stream (conn, &stream, 0)))
884 QUIC_DBG (2, "Stream open failed with %d", rv);
887 sctx->c_quic_ctx_id.stream = stream;
889 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
891 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
893 stream_session = session_alloc (qctx->c_thread_index);
894 QUIC_DBG (1, "Created stream_session, id %u ctx %u",
895 stream_session->session_index, sctx_index);
896 stream_session->app_wrk_index = quic_session->app_wrk_index;
897 stream_session->connection_index = sctx_index;
898 stream_session->session_type =
899 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
900 qctx->c_quic_ctx_id.udp_is_ip4);
901 stream_session->opaque = QUIC_SESSION_TYPE_STREAM;
902 sctx->c_s_index = stream_session->session_index;
903 sctx->c_quic_ctx_id.stream_session_handle = session_handle (stream_session);
905 if (app_worker_init_connected (app_wrk, stream_session))
907 QUIC_DBG (1, "failed to app_worker_init_connected");
908 quicly_reset_stream (stream, 0x30003);
909 session_free_w_fifos (stream_session);
910 quic_ctx_free (sctx);
911 return app_worker_connect_notify (app_wrk, NULL, sep->opaque);
914 stream_session->session_state = SESSION_STATE_READY;
915 if (app_worker_connect_notify (app_wrk, stream_session, sep->opaque))
917 QUIC_DBG (1, "failed to notify app");
918 quicly_reset_stream (stream, 0x30004);
919 session_free_w_fifos (stream_session);
920 quic_ctx_free (sctx);
923 session_lookup_add_connection (&sctx->connection,
924 session_handle (stream_session));
925 stream_data = (quic_stream_data_t *) stream->data;
926 stream_data->ctx_id = sctx->c_c_index;
931 quic_connect_new_connection (session_endpoint_cfg_t * sep)
933 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
934 quic_main_t *qm = &quic_main;
936 app_worker_t *app_wrk;
941 ctx_index = quic_ctx_alloc ();
942 ctx = quic_ctx_get (ctx_index);
943 ctx->c_quic_ctx_id.parent_app_wrk_id = sep->app_wrk_index;
944 ctx->c_s_index = 0xFAFAFAFA;
945 ctx->c_quic_ctx_id.udp_is_ip4 = sep->is_ip4;
946 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
947 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
948 ctx->client_opaque = sep->opaque;
951 ctx->srv_hostname = format (0, "%v", sep->hostname);
952 vec_terminate_c_string (ctx->srv_hostname);
956 // needed by quic for crypto + determining client / server
958 format (0, "%U", format_ip46_address, &sep->ip, sep->is_ip4);
961 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
962 cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
963 cargs->app_index = qm->app_index;
964 cargs->api_context = ctx_index;
966 app_wrk = app_worker_get (sep->app_wrk_index);
967 app = application_get (app_wrk->app_index);
968 ctx->c_quic_ctx_id.parent_app_id = app_wrk->app_index;
969 cargs->sep_ext.ns_index = app->ns_index;
971 allocate_quicly_ctx (app, 1 /* is client */ );
973 if ((error = vnet_connect (cargs)))
976 QUIC_DBG (1, "New connect request %u", ctx_index);
981 quic_disconnect (u32 ctx_index, u32 thread_index)
983 QUIC_DBG (2, "Called quic_disconnect");
984 //tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
987 QUIC_DBG (1, "Closing connection %x", ctx_index);
989 ctx = quic_ctx_get (ctx_index);
990 if (ctx->c_quic_ctx_id.is_stream)
992 quicly_stream_t *stream = ctx->c_quic_ctx_id.stream;
993 quicly_reset_stream (stream, 0x30000);
994 session_transport_delete_notify (&ctx->connection);
999 quicly_conn_t *conn = ctx->c_quic_ctx_id.conn;
1000 // Start connection closing. Keep sending packets until quicly_send
1001 // returns QUICLY_ERROR_FREE_CONNECTION
1002 quicly_close (conn, 0, "");
1003 quic_send_packets (ctx);
1008 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1010 vnet_listen_args_t _bargs, *args = &_bargs;
1011 quic_main_t *qm = &quic_main;
1012 session_handle_t udp_handle;
1013 session_endpoint_cfg_t *sep;
1014 session_t *udp_listen_session, *quic_listen_session;
1015 app_worker_t *app_wrk;
1019 app_listener_t *app_listener;
1021 sep = (session_endpoint_cfg_t *) tep;
1022 app_wrk = app_worker_get (sep->app_wrk_index);
1023 // We need to call this because we call app_worker_init_connected in
1024 // quic_accept_stream, which assumes the connect segment manager exists
1025 app_worker_alloc_connects_segment_manager (app_wrk);
1026 app = application_get (app_wrk->app_index);
1027 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1029 allocate_quicly_ctx (app, 0 /* is_client */ );
1031 sep->transport_proto = TRANSPORT_PROTO_UDP;
1032 memset (args, 0, sizeof (*args));
1033 args->app_index = qm->app_index;
1034 args->sep_ext = *sep;
1035 args->sep_ext.ns_index = app->ns_index;
1036 if (vnet_listen (args))
1039 lctx_index = quic_ctx_alloc (); // listener
1040 udp_handle = args->handle;
1041 app_listener = app_listener_get_w_handle (udp_handle);
1042 udp_listen_session = app_listener_get_session (app_listener);
1043 udp_listen_session->opaque = lctx_index;
1045 quic_listen_session = listen_session_get (quic_listen_session_index);
1046 quic_listen_session->opaque = QUIC_SESSION_TYPE_LISTEN;
1048 lctx = quic_ctx_get (lctx_index); // listener
1049 lctx->is_listener = 1;
1050 lctx->c_quic_ctx_id.parent_app_wrk_id = sep->app_wrk_index;
1051 lctx->c_quic_ctx_id.parent_app_id = app_wrk->app_index;
1052 lctx->c_quic_ctx_id.udp_session_handle = udp_handle;
1053 lctx->c_quic_ctx_id.quic_session_handle =
1054 listen_session_get_handle (quic_listen_session);
1055 lctx->c_quic_ctx_id.udp_is_ip4 = sep->is_ip4;
1057 QUIC_DBG (1, "Started listening %d", lctx_index);
1062 quic_stop_listen (u32 lctx_index)
1064 QUIC_DBG (2, "Called quic_stop_listen");
1067 lctx = quic_ctx_get (lctx_index); // listener
1068 vnet_unlisten_args_t a = {
1069 .handle = lctx->c_quic_ctx_id.udp_session_handle,
1070 .app_index = quic_main.app_index,
1071 .wrk_map_index = 0 /* default wrk */
1073 if (vnet_unlisten (&a))
1074 clib_warning ("unlisten errored");
1076 // TODO: crypto state cleanup
1078 quic_ctx_free (lctx); // listener
1082 static transport_connection_t *
1083 quic_connection_get (u32 ctx_index, u32 thread_index)
1085 QUIC_DBG (2, "Called quic_connection_get");
1087 ctx = quic_ctx_get_w_thread (ctx_index, thread_index);
1088 return &ctx->connection;
1091 static transport_connection_t *
1092 quic_listener_get (u32 listener_index)
1094 QUIC_DBG (2, "Called quic_listener_get");
1096 ctx = quic_ctx_get (listener_index);
1097 return &ctx->connection;
1101 quic_update_time (f64 now, u8 thread_index)
1103 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1105 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1106 quic_set_time_now (thread_index);
1107 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
1111 format_quic_connection (u8 * s, va_list * args)
1113 s = format (s, "[QUIC] connection"); //TODO
1118 format_quic_half_open (u8 * s, va_list * args)
1120 u32 qc_index = va_arg (*args, u32);
1121 quic_ctx_t *ctx = quic_ctx_get (qc_index);
1122 s = format (s, "[QUIC] half-open app %u", ctx->c_quic_ctx_id.parent_app_id);
1128 format_quic_listener (u8 * s, va_list * args)
1130 s = format (s, "[QUIC] listener"); // TODO
1134 /*****************************************************************************
1135 * END TRANSPORT PROTO FUNCTIONS
1137 * START SESSION CALLBACKS
1138 * Called from UDP layer
1139 *****************************************************************************/
1142 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1143 ip46_address_t * addr, u16 port, u8 is_ip4)
1147 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1148 sa4->sin_family = AF_INET;
1149 sa4->sin_port = port;
1150 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1151 *salen = sizeof (struct sockaddr_in);
1155 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1156 sa6->sin6_family = AF_INET6;
1157 sa6->sin6_port = port;
1158 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1159 *salen = sizeof (struct sockaddr_in6);
1164 quic_notify_app_connected (quic_ctx_t * ctx)
1166 QUIC_DBG (1, "quic_notify_app_connected");
1167 session_t *quic_session;
1168 app_worker_t *app_wrk;
1169 u32 ctx_id = ctx->c_c_index;
1171 app_wrk = app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_id);
1174 quic_disconnect_transport (ctx);
1178 quic_session = session_alloc (ctx->c_thread_index);
1180 QUIC_DBG (1, "Created quic_session, id %u", quic_session->session_index);
1181 ctx->c_s_index = quic_session->session_index;
1182 quic_session->app_wrk_index = ctx->c_quic_ctx_id.parent_app_wrk_id;
1183 quic_session->connection_index = ctx->c_c_index;
1184 quic_session->opaque = QUIC_SESSION_TYPE_QUIC;
1185 quic_session->session_type =
1186 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
1187 ctx->c_quic_ctx_id.udp_is_ip4);
1189 if (app_worker_init_connected (app_wrk, quic_session))
1191 QUIC_DBG (1, "failed to app_worker_init_connected");
1192 quic_disconnect (ctx->c_c_index, vlib_get_thread_index ());
1193 return app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1196 quic_session->session_state = SESSION_STATE_CONNECTING;
1197 if (app_worker_connect_notify (app_wrk, quic_session, ctx->client_opaque))
1199 QUIC_DBG (1, "failed to notify app");
1200 quic_disconnect (ctx->c_c_index, vlib_get_thread_index ());
1204 // If the app opens a stream in its callback it may invalidate ctx
1205 ctx = quic_ctx_get (ctx_id);
1206 ctx->c_quic_ctx_id.quic_session_handle = session_handle (quic_session);
1207 quic_session->session_state = SESSION_STATE_LISTENING;
1208 session_lookup_add_connection (&ctx->connection,
1209 session_handle (quic_session));
1215 quic_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1216 session_t * udp_session, u8 is_fail)
1218 QUIC_DBG (2, "QSession is now connected (id %u)",
1219 udp_session->session_index);
1220 // This should always be called before quic_connect returns since UDP always
1221 // connects instantly.
1222 struct sockaddr_in6 sa6;
1223 struct sockaddr *sa = (struct sockaddr *) &sa6;
1225 transport_connection_t *tc;
1229 app_worker_t *app_wrk;
1231 ctx = quic_ctx_get (ctx_index);
1238 app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_id);
1241 api_context = ctx->c_s_index;
1242 app_worker_connect_notify (app_wrk, 0, api_context);
1247 app_wrk = app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_id);
1250 QUIC_DBG (1, "Appwrk not found");
1253 app = application_get (app_wrk->app_index);
1255 ctx->c_thread_index = vlib_get_thread_index ();
1256 ctx->c_c_index = ctx_index;
1258 QUIC_DBG (1, "Quic connect returned %u. New ctx [%u]%x",
1259 is_fail, vlib_get_thread_index (), (ctx) ? ctx_index : ~0);
1261 ctx->c_quic_ctx_id.udp_session_handle = session_handle (udp_session);
1262 udp_session->opaque = ctx_index;
1263 udp_session->session_state = SESSION_STATE_READY;
1265 // Init QUIC lib connection
1266 // Generate required sockaddr & salen
1267 tc = session_get_transport (udp_session);
1268 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1271 quicly_connect (&ctx->c_quic_ctx_id.conn,
1272 (quicly_context_t *) app->quicly_ctx,
1273 (char *) ctx->srv_hostname, sa, salen,
1274 &quic_main.next_cid, &quic_main.hs_properties, NULL);
1275 ++quic_main.next_cid.master_id;
1276 // Save context handle in quicly connection
1277 *quicly_get_data (ctx->c_quic_ctx_id.conn) = (void *) (u64) ctx_index;
1280 int rv = quic_send_packets (ctx);
1283 QUIC_DBG (1, "Error sending packets %d, closing connection", rv);
1284 quic_connection_closed (ctx_index);
1290 quic_session_disconnect_callback (session_t * s)
1292 clib_warning ("UDP session disconnected???");
1296 quic_session_reset_callback (session_t * s)
1298 clib_warning ("UDP session reset???");
1302 quic_session_accepted_callback (session_t * s)
1309 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1311 QUIC_DBG (2, "Called quic_add_segment_callback");
1312 QUIC_DBG (2, "NOT IMPLEMENTED");
1313 /* No-op for builtin */
1318 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1320 QUIC_DBG (2, "Called quic_del_segment_callback");
1321 QUIC_DBG (2, "NOT IMPLEMENTED");
1322 /* No-op for builtin */
1327 quic_custom_tx_callback (void *s)
1329 QUIC_DBG (2, "Called quic_custom_tx_callback");
1330 session_t *stream_session = (session_t *) s;
1333 quicly_stream_t *stream;
1338 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1340 ctx = quic_ctx_get (stream_session->connection_index);
1341 if (PREDICT_FALSE (!ctx->c_quic_ctx_id.is_stream))
1343 QUIC_DBG (1, "Error: trying to send on quic session not stream");
1347 stream = ctx->c_quic_ctx_id.stream;
1349 f = stream_session->tx_fifo;
1350 deq_max = svm_fifo_max_dequeue (f);
1354 data = svm_fifo_head (f);
1355 if (quicly_streambuf_egress_write (stream, data, deq_max))
1360 QUIC_DBG (2, "Sent %u bytes", deq_max);
1361 svm_fifo_dequeue_drop (f, deq_max);
1362 int rv = quic_send_packets (ctx);
1365 QUIC_DBG (1, "TX error sending packets %d, closing connection", rv);
1366 quic_connection_closed (ctx->c_quic_ctx_id.quic_connection_ctx_id);
1372 quic_find_packet_ctx (quic_ctx_t ** ctx, quicly_conn_t ** conn,
1373 struct sockaddr *sa, socklen_t salen,
1374 quicly_decoded_packet_t packet)
1377 quicly_conn_t *conn_;
1379 pool_foreach (ctx_, quic_main.ctx_pool[vlib_get_thread_index()],
1381 conn_ = ctx_->c_quic_ctx_id.conn;
1382 if (!ctx_->c_quic_ctx_id.is_stream && conn_ && !ctx_->is_listener)
1384 if (quicly_is_destination(conn_, sa, salen, &packet))
1388 // QUIC_DBG (2, "connection_found");
1398 quic_receive (quic_ctx_t * ctx, quicly_conn_t * conn,
1399 quicly_decoded_packet_t packet)
1401 u32 ctx_id = ctx->c_c_index;
1402 quicly_receive (conn, &packet);
1403 // ctx pointer may change if a new stream is opened
1404 ctx = quic_ctx_get (ctx_id);
1405 // Conn may be set to null if the connection is terminated
1406 if (ctx->c_quic_ctx_id.conn && ctx->conn_state == QUIC_CONN_STATE_HANDSHAKE)
1408 if (quicly_connection_is_ready (conn))
1410 ctx->conn_state = QUIC_CONN_STATE_READY;
1411 if (quicly_is_client (conn))
1413 quic_notify_app_connected (ctx);
1414 ctx = quic_ctx_get (ctx_id);
1418 if (quic_send_packets (ctx))
1420 quic_connection_closed (ctx->c_c_index);
1426 quic_create_quic_session (quic_ctx_t * ctx)
1428 session_t *quic_session, *quic_listen_session;
1429 app_worker_t *app_wrk;
1433 quic_session = session_alloc (ctx->c_thread_index);
1434 QUIC_DBG (1, "Created quic session, id %u ctx %u",
1435 quic_session->session_index, ctx->c_c_index);
1436 quic_session->session_state = SESSION_STATE_LISTENING;
1437 ctx->c_s_index = quic_session->session_index;
1439 lctx = quic_ctx_get (ctx->c_quic_ctx_id.listener_ctx_id);
1441 quic_listen_session =
1442 listen_session_get_from_handle (lctx->c_quic_ctx_id.quic_session_handle);
1443 quic_session->app_wrk_index = lctx->c_quic_ctx_id.parent_app_wrk_id;
1444 quic_session->connection_index = ctx->c_c_index;
1445 quic_session->session_type =
1446 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
1447 ctx->c_quic_ctx_id.udp_is_ip4);
1448 quic_session->listener_index = quic_listen_session->session_index;
1449 quic_session->app_index = quic_main.app_index;
1450 quic_session->opaque = QUIC_SESSION_TYPE_QUIC;
1452 // TODO: don't alloc fifos when we don't transfer data on this session
1453 // but we still need fifos for the events?
1454 if ((rv = app_worker_init_accepted (quic_session)))
1456 QUIC_DBG (1, "failed to allocate fifos");
1457 session_free (quic_session);
1460 ctx->c_quic_ctx_id.quic_session_handle = session_handle (quic_session);
1461 ctx->c_quic_ctx_id.parent_app_id = lctx->c_quic_ctx_id.parent_app_id;
1462 ctx->c_quic_ctx_id.udp_is_ip4 = lctx->c_quic_ctx_id.udp_is_ip4;
1463 ctx->c_quic_ctx_id.parent_app_wrk_id = quic_session->app_wrk_index;
1464 session_lookup_add_connection (&ctx->connection,
1465 session_handle (quic_session));
1466 app_wrk = app_worker_get (quic_session->app_wrk_index);
1467 rv = app_worker_accept_notify (app_wrk, quic_session);
1470 QUIC_DBG (1, "failed to notify accept worker app");
1477 quic_create_connection (quicly_context_t * quicly_ctx,
1478 u64 udp_session_handle, u32 lctx_index,
1479 struct sockaddr *sa,
1480 socklen_t salen, quicly_decoded_packet_t packet)
1484 quicly_conn_t *conn;
1487 /* new connection, accept and create context if packet is valid */
1488 // TODO: check if socket is actually listening?
1489 if ((rv = quicly_accept (&conn, quicly_ctx, sa, salen,
1490 &packet, ptls_iovec_init (NULL, 0),
1491 &quic_main.next_cid, NULL)))
1493 // Invalid packet, pass
1494 assert (conn == NULL);
1495 QUIC_DBG (2, "Accept failed with %d", rv);
1498 assert (conn != NULL);
1500 ++quic_main.next_cid.master_id;
1502 ctx_index = quic_ctx_alloc ();
1503 ctx = quic_ctx_get (ctx_index);
1504 // Save ctx handle in quicly connection
1505 *quicly_get_data (conn) = (void *) (u64) ctx_index;
1507 ctx->c_thread_index = vlib_get_thread_index ();
1508 ctx->c_c_index = ctx_index;
1509 ctx->c_quic_ctx_id.udp_session_handle = udp_session_handle;
1510 ctx->c_quic_ctx_id.listener_ctx_id = lctx_index;
1511 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1512 ctx->c_quic_ctx_id.conn = conn;
1514 quic_create_quic_session (ctx);
1516 if (quic_send_packets (ctx))
1518 quic_connection_closed (ctx_index);
1524 quic_reset_connection (quicly_context_t * quicly_ctx, u64 udp_session_handle,
1525 struct sockaddr *sa, socklen_t salen,
1526 quicly_decoded_packet_t packet)
1528 /* short header packet; potentially a dead connection. No need to check the
1529 * length of the incoming packet, because loop is prevented by authenticating
1530 * the CID (by checking node_id and thread_id). If the peer is also sending a
1531 * reset, then the next CID is highly likely to contain a non-authenticating
1533 QUIC_DBG (2, "Sending stateless reset");
1534 quicly_datagram_t *dgram;
1535 session_t *udp_session;
1536 if (packet.cid.dest.plaintext.node_id == 0
1537 && packet.cid.dest.plaintext.thread_id == 0)
1539 dgram = quicly_send_stateless_reset (quicly_ctx, sa, salen,
1540 &packet.cid.dest.plaintext);
1541 udp_session = session_get_from_handle (udp_session_handle);
1542 if (quic_send_datagram (udp_session, dgram)) // TODO : set event on fifo
1543 QUIC_DBG (2, "Send reset failed");
1549 quic_app_rx_callback (session_t * udp_session)
1551 // Read data from UDP rx_fifo and pass it to the quicly conn.
1552 quicly_decoded_packet_t packet;
1553 session_dgram_hdr_t ph;
1555 quicly_conn_t *conn = NULL;
1556 quic_ctx_t *lctx, *ctx = NULL;
1559 struct sockaddr_in6 sa6;
1560 struct sockaddr *sa = (struct sockaddr *) &sa6;
1564 u32 lctx_index = udp_session->opaque;
1565 u64 udp_session_handle = session_handle (udp_session);
1568 // lctx = quic_ctx_get (lctx_index);
1569 // QUIC_DBG (2, "Got RX data on session %d",
1570 // lctx->c_quic_ctx_id.udp_session_handle);
1572 f = udp_session->rx_fifo;
1577 max_deq = svm_fifo_max_dequeue (f);
1578 if (max_deq < sizeof (session_dgram_hdr_t))
1580 svm_fifo_unset_event (f);
1583 // QUIC_DBG (2, "Processing one packet at %ld", quic_get_time (NULL));
1585 svm_fifo_unset_event (f);
1586 svm_fifo_peek (f, 0, sizeof (ph), (u8 *) & ph);
1587 ASSERT (ph.data_length >= ph.data_offset);
1588 len = ph.data_length - ph.data_offset;
1590 quic_build_sockaddr (sa, &salen, &ph.rmt_ip, ph.rmt_port, ph.is_ip4);
1592 // Quicly can read len bytes from the fifo at offset:
1593 // ph.data_offset + SESSION_CONN_HDR_LEN
1594 data = svm_fifo_head (f) + ph.data_offset + SESSION_CONN_HDR_LEN;
1596 lctx = quic_ctx_get (lctx_index);
1597 app = application_get (lctx->c_quic_ctx_id.parent_app_id);
1600 quicly_decode_packet ((quicly_context_t *) app->quicly_ctx, &packet,
1602 if (plen != SIZE_MAX)
1604 quic_find_packet_ctx (&ctx, &conn, sa, salen, packet);
1606 quic_receive (ctx, conn, packet);
1607 else if (QUICLY_PACKET_IS_LONG_HEADER (packet.octets.base[0]))
1608 quic_create_connection ((quicly_context_t *) app->quicly_ctx,
1609 udp_session_handle, lctx_index,
1611 else if (((quicly_context_t *) app->quicly_ctx)->encrypt_cid)
1612 quic_reset_connection ((quicly_context_t *) app->quicly_ctx,
1613 udp_session_handle, sa, salen, packet);
1615 svm_fifo_dequeue_drop (f,
1616 ph.data_length + ph.data_offset +
1617 SESSION_CONN_HDR_LEN);
1624 /*****************************************************************************
1625 * END TRANSPORT PROTO FUNCTIONS
1626 *****************************************************************************/
1629 static session_cb_vft_t quic_app_cb_vft = {
1630 .session_accept_callback = quic_session_accepted_callback,
1631 .session_disconnect_callback = quic_session_disconnect_callback,
1632 .session_connected_callback = quic_session_connected_callback,
1633 .session_reset_callback = quic_session_reset_callback,
1634 .add_segment_callback = quic_add_segment_callback,
1635 .del_segment_callback = quic_del_segment_callback,
1636 .builtin_app_rx_callback = quic_app_rx_callback,
1639 const static transport_proto_vft_t quic_proto = {
1640 .connect = quic_connect,
1641 .close = quic_disconnect,
1642 .start_listen = quic_start_listen,
1643 .stop_listen = quic_stop_listen,
1644 .get_connection = quic_connection_get,
1645 .get_listener = quic_listener_get,
1646 .update_time = quic_update_time,
1647 .custom_tx = quic_custom_tx_callback,
1648 .tx_type = TRANSPORT_TX_INTERNAL,
1649 .service_type = TRANSPORT_SERVICE_APP,
1650 .format_connection = format_quic_connection,
1651 .format_half_open = format_quic_half_open,
1652 .format_listener = format_quic_listener,
1656 static clib_error_t *
1657 quic_init (vlib_main_t * vm)
1659 QUIC_DBG (2, "Called quic_init");
1660 u32 add_segment_size = (4096ULL << 20) - 1, segment_size = 512 << 20;
1661 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1662 vnet_app_attach_args_t _a, *a = &_a;
1663 u64 options[APP_OPTIONS_N_OPTIONS];
1664 quic_main_t *qm = &quic_main;
1665 u32 fifo_size = 64 << 10;
1668 num_threads = 1 /* main thread */ + vtm->n_threads;
1670 memset (a, 0, sizeof (*a));
1671 memset (options, 0, sizeof (options));
1673 a->session_cb_vft = &quic_app_cb_vft;
1674 a->api_client_index = APP_INVALID_INDEX;
1675 a->options = options;
1676 a->name = format (0, "quic");
1677 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
1678 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = add_segment_size;
1679 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
1680 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
1681 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1682 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1683 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1685 if (vnet_application_attach (a))
1687 clib_warning ("failed to attach quic app");
1688 return clib_error_return (0, "failed to attach quic app");
1691 vec_validate (qm->ctx_pool, num_threads - 1);
1692 vec_validate (qm->wrk_ctx, num_threads - 1);
1693 // Timers, one per thread.
1694 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1696 foreach_vlib_main (({
1697 tw = &qm->wrk_ctx[ii].timer_wheel;
1698 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
1699 10e-3 /* timer period 1ms */ , ~0);
1700 tw->last_run_time = vlib_time_now (this_vlib_main);
1704 if (!qm->ca_cert_path)
1705 qm->ca_cert_path = QUIC_DEFAULT_CA_CERT_PATH;
1707 qm->app_index = a->app_index;
1708 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1709 / QUIC_TSTAMP_RESOLUTION;
1711 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
1712 FIB_PROTOCOL_IP4, ~0);
1713 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
1714 FIB_PROTOCOL_IP6, ~0);
1720 VLIB_INIT_FUNCTION (quic_init);
1723 VLIB_PLUGIN_REGISTER () =
1725 .version = VPP_BUILD_VER,
1726 .description = "Quic transport protocol",
1731 * fd.io coding-style-patch-verification: ON
1734 * eval: (c-set-style "gnu")