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>
24 #include <vppinfra/lock.h>
26 #include <quic/quic.h>
27 #include <quic/certs.h>
28 #include <quic/error.h>
29 #include <quic/quic_crypto.h>
31 #include <quicly/defaults.h>
33 static char *quic_error_strings[] = {
34 #define quic_error(n,s) s,
35 #include "quic_error.def"
39 static quic_main_t quic_main;
40 static void quic_update_timer (quic_ctx_t * ctx);
41 static int quic_check_quic_session_connected (quic_ctx_t * ctx);
43 /* Helper functions */
46 quic_ctx_alloc (u32 thread_index)
48 quic_main_t *qm = &quic_main;
51 pool_get (qm->ctx_pool[thread_index], ctx);
53 clib_memset (ctx, 0, sizeof (quic_ctx_t));
54 ctx->c_thread_index = thread_index;
55 QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
56 ctx - qm->ctx_pool[thread_index], thread_index);
57 return ctx - qm->ctx_pool[thread_index];
61 quic_ctx_free (quic_ctx_t * ctx)
63 QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
64 u32 thread_index = ctx->c_thread_index;
66 clib_memset (ctx, 0xfb, sizeof (*ctx));
67 pool_put (quic_main.ctx_pool[thread_index], ctx);
71 quic_ctx_get (u32 ctx_index, u32 thread_index)
73 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
77 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
79 if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
81 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
85 quic_get_conn_ctx (quicly_conn_t * conn)
88 conn_data = (u64) * quicly_get_data (conn);
89 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
93 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
95 *quicly_get_data (conn) =
96 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
100 quic_ctx_is_stream (quic_ctx_t * ctx)
102 return (ctx->flags & QUIC_F_IS_STREAM);
106 quic_ctx_is_listener (quic_ctx_t * ctx)
108 return (ctx->flags & QUIC_F_IS_LISTENER);
112 get_stream_session_from_stream (quicly_stream_t * stream)
115 quic_stream_data_t *stream_data;
117 stream_data = (quic_stream_data_t *) stream->data;
118 ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
119 return session_get (ctx->c_s_index, stream_data->thread_index);
123 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
124 const quicly_cid_plaintext_t * id)
126 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
127 kv->key[1] = id->node_id;
131 quic_sendable_packet_count (session_t * udp_session)
134 u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
135 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
136 return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
139 static quicly_context_t *
140 quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
142 return ctx->quicly_ctx;
145 static quicly_context_t *
146 quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
148 session_t *udp_session = session_get_from_handle (udp_session_handle);
150 quic_ctx_get (udp_session->opaque, udp_session->thread_index);
151 return ctx->quicly_ctx;
155 quic_set_udp_tx_evt (session_t * udp_session)
158 if (svm_fifo_set_event (udp_session->tx_fifo))
159 rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
161 if (PREDICT_FALSE (rv))
162 clib_warning ("Event enqueue errored %d", rv);
165 /* QUIC protocol actions */
168 quic_ack_rx_data (session_t * stream_session)
173 quicly_stream_t *stream;
174 quic_stream_data_t *stream_data;
176 sctx = quic_ctx_get (stream_session->connection_index,
177 stream_session->thread_index);
178 ASSERT (quic_ctx_is_stream (sctx));
179 stream = sctx->stream;
180 stream_data = (quic_stream_data_t *) stream->data;
182 f = stream_session->rx_fifo;
183 max_deq = svm_fifo_max_dequeue (f);
185 ASSERT (stream_data->app_rx_data_len >= max_deq);
186 quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
187 QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
188 stream_data->app_rx_data_len = max_deq;
192 quic_disconnect_transport (quic_ctx_t * ctx)
194 QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
195 vnet_disconnect_args_t a = {
196 .handle = ctx->udp_session_handle,
197 .app_index = quic_main.app_index,
200 if (vnet_disconnect_session (&a))
201 clib_warning ("UDP session 0x%lx disconnect errored",
202 ctx->udp_session_handle);
206 quic_connection_delete (quic_ctx_t * ctx)
208 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
209 clib_bihash_kv_16_8_t kv;
212 QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
214 ASSERT (!quic_ctx_is_stream (ctx));
217 if (ctx->timer_handle != QUIC_TIMER_HANDLE_INVALID)
219 tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
220 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
223 /* Delete the connection from the connection map */
225 quic_make_connection_key (&kv, quicly_get_master_id (conn));
226 QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
228 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
230 quic_disconnect_transport (ctx);
233 quicly_free (ctx->conn);
236 session_transport_delete_notify (&ctx->connection);
241 quic_increment_counter (u8 evt, u8 val)
243 vlib_main_t *vm = vlib_get_main ();
244 vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
250 * Called when quicly return an error
251 * This function interacts tightly with quic_proto_on_close
254 quic_connection_closed (quic_ctx_t * ctx)
256 QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
259 /* TODO if connection is not established, just delete the session? */
260 /* Actually should send connect or accept error */
262 switch (ctx->conn_state)
264 case QUIC_CONN_STATE_READY:
265 /* Error on an opened connection (timeout...)
266 This puts the session in closing state, we should receive a notification
267 when the app has closed its session */
268 session_transport_reset_notify (&ctx->connection);
269 /* This ensures we delete the connection when the app confirms the close */
270 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
272 case QUIC_CONN_STATE_PASSIVE_CLOSING:
273 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
274 /* quic_proto_on_close will eventually be called when the app confirms the close
275 , we delete the connection at that point */
277 case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
278 /* App already confirmed close, we can delete the connection */
279 session_transport_delete_notify (&ctx->connection);
280 quic_connection_delete (ctx);
282 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
285 case QUIC_CONN_STATE_ACTIVE_CLOSING:
286 session_transport_delete_notify (&ctx->connection);
287 quic_connection_delete (ctx);
290 QUIC_DBG (0, "BUG %d", ctx->conn_state);
296 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
299 session_dgram_hdr_t hdr;
302 transport_connection_t *tc;
304 len = packet->data.len;
305 f = udp_session->tx_fifo;
306 tc = session_get_transport (udp_session);
307 max_enqueue = svm_fifo_max_enqueue (f);
308 if (max_enqueue < SESSION_CONN_HDR_LEN + len)
310 QUIC_DBG (1, "Too much data to send, max_enqueue %u, len %u",
311 max_enqueue, len + SESSION_CONN_HDR_LEN);
312 return QUIC_ERROR_FULL_FIFO;
315 /* Build packet header for fifo */
316 hdr.data_length = len;
318 hdr.is_ip4 = tc->is_ip4;
319 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
320 hdr.lcl_port = tc->lcl_port;
322 /* Read dest address from quicly-provided sockaddr */
325 ASSERT (packet->dest.sa.sa_family == AF_INET);
326 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
327 hdr.rmt_port = sa4->sin_port;
328 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
332 ASSERT (packet->dest.sa.sa_family == AF_INET6);
333 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
334 hdr.rmt_port = sa6->sin6_port;
335 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
338 ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
339 if (ret != sizeof (hdr))
341 QUIC_DBG (1, "Not enough space to enqueue header");
342 return QUIC_ERROR_FULL_FIFO;
344 ret = svm_fifo_enqueue (f, len, packet->data.base);
347 QUIC_DBG (1, "Not enough space to enqueue payload");
348 return QUIC_ERROR_FULL_FIFO;
351 quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
357 quic_send_packets (quic_ctx_t * ctx)
359 quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
360 session_t *udp_session;
362 size_t num_packets, i, max_packets;
363 quicly_packet_allocator_t *pa;
364 quicly_context_t *quicly_context;
367 /* We have sctx, get qctx */
368 if (quic_ctx_is_stream (ctx))
369 ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
371 ASSERT (!quic_ctx_is_stream (ctx));
373 udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
382 /* TODO : quicly can assert it can send min_packets up to 2 */
383 if (quic_sendable_packet_count (udp_session) < 2)
386 quicly_context = quic_get_quicly_ctx_from_ctx (ctx);
389 clib_warning ("Tried to send packets on non existing app worker %u",
390 ctx->parent_app_wrk_id);
391 quic_connection_delete (ctx);
394 pa = quicly_context->packet_allocator;
397 max_packets = quic_sendable_packet_count (udp_session);
400 num_packets = max_packets;
401 if ((err = quicly_send (conn, packets, &num_packets)))
404 for (i = 0; i != num_packets; ++i)
406 if ((err = quic_send_datagram (udp_session, packets[i])))
409 pa->free_packet (pa, packets[i]);
412 while (num_packets > 0 && num_packets == max_packets);
415 quic_set_udp_tx_evt (udp_session);
417 QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
418 svm_fifo_max_dequeue (udp_session->rx_fifo));
419 quic_update_timer (ctx);
423 if (err && err != QUICLY_ERROR_PACKET_IGNORED
424 && err != QUICLY_ERROR_FREE_CONNECTION)
425 clib_warning ("Quic error '%U'.", quic_format_err, err);
426 quic_connection_closed (ctx);
430 /* Quicly callbacks */
433 quic_on_stream_destroy (quicly_stream_t * stream, int err)
435 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
436 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
437 stream_data->thread_index);
438 session_t *stream_session = session_get (sctx->c_s_index,
439 sctx->c_thread_index);
440 QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
441 session_handle (stream_session), quic_format_err, err);
443 stream_session->session_state = SESSION_STATE_CLOSED;
444 session_transport_delete_notify (&sctx->connection);
446 quic_ctx_free (sctx);
447 clib_mem_free (stream->data);
451 quic_on_stop_sending (quicly_stream_t * stream, int err)
454 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
455 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
456 stream_data->thread_index);
457 session_t *stream_session = session_get (sctx->c_s_index,
458 sctx->c_thread_index);
459 clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
460 session_handle (stream_session), quic_format_err, err);
462 /* TODO : handle STOP_SENDING */
467 quic_on_receive_reset (quicly_stream_t * stream, int err)
469 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
470 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
471 stream_data->thread_index);
473 session_t *stream_session = session_get (sctx->c_s_index,
474 sctx->c_thread_index);
475 clib_warning ("RESET_STREAM: session 0x%lx (%U)",
476 session_handle (stream_session), quic_format_err, err);
478 session_transport_closing_notify (&sctx->connection);
483 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
486 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
489 session_t *stream_session;
490 app_worker_t *app_wrk;
492 quic_stream_data_t *stream_data;
495 stream_data = (quic_stream_data_t *) stream->data;
496 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
497 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
498 f = stream_session->rx_fifo;
500 max_enq = svm_fifo_max_enqueue_prod (f);
501 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
502 if (off - stream_data->app_rx_data_len + len > max_enq)
504 QUIC_DBG (1, "Error RX fifo is full");
507 if (off == stream_data->app_rx_data_len)
509 /* Streams live on the same thread so (f, stream_data) should stay consistent */
510 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
511 stream_data->app_rx_data_len += rlen;
512 ASSERT (rlen >= len);
513 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
514 if (PREDICT_TRUE (app_wrk != 0))
515 app_worker_lock_and_send_event (app_wrk, stream_session,
517 quic_ack_rx_data (stream_session);
521 rlen = svm_fifo_enqueue_with_offset (f,
522 off - stream_data->app_rx_data_len,
530 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
532 session_t *stream_session;
536 stream_session = get_stream_session_from_stream (stream);
537 f = stream_session->tx_fifo;
539 rv = svm_fifo_dequeue_drop (f, delta);
540 ASSERT (rv == delta);
541 quicly_stream_sync_sendbuf (stream, 0);
545 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
546 size_t * len, int *wrote_all)
548 session_t *stream_session;
550 u32 deq_max, first_deq, max_rd_chunk, rem_offset;
552 stream_session = get_stream_session_from_stream (stream);
553 f = stream_session->tx_fifo;
555 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
557 deq_max = svm_fifo_max_dequeue_cons (f);
558 ASSERT (off <= deq_max);
559 if (off + *len < deq_max)
566 *len = deq_max - off;
567 QUIC_DBG (3, "Wrote ALL, %u", *len);
570 /* TODO, use something like : return svm_fifo_peek (f, off, *len, dst); */
571 max_rd_chunk = svm_fifo_max_read_chunk (f);
574 if (off < max_rd_chunk)
576 first_deq = clib_min (*len, max_rd_chunk - off);
577 clib_memcpy_fast (dst, svm_fifo_head (f) + off, first_deq);
580 if (max_rd_chunk < off + *len)
582 rem_offset = max_rd_chunk < off ? off - max_rd_chunk : 0;
583 clib_memcpy_fast (dst + first_deq, f->head_chunk->data + rem_offset,
590 static const quicly_stream_callbacks_t quic_stream_callbacks = {
591 .on_destroy = quic_on_stream_destroy,
592 .on_send_shift = quic_fifo_egress_shift,
593 .on_send_emit = quic_fifo_egress_emit,
594 .on_send_stop = quic_on_stop_sending,
595 .on_receive = quic_on_receive,
596 .on_receive_reset = quic_on_receive_reset
600 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
602 session_t *stream_session, *quic_session;
603 quic_stream_data_t *stream_data;
604 app_worker_t *app_wrk;
605 quic_ctx_t *qctx, *sctx;
609 QUIC_DBG (2, "on_stream_open called");
610 stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
611 stream->callbacks = &quic_stream_callbacks;
612 /* Notify accept on parent qsession, but only if this is not a locally
613 * initiated stream */
614 if (quicly_stream_is_self_initiated (stream))
617 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
618 qctx = quic_get_conn_ctx (stream->conn);
620 /* Might need to signal that the connection is ready if the first thing the
621 * server does is open a stream */
622 quic_check_quic_session_connected (qctx);
623 /* ctx might be invalidated */
624 qctx = quic_get_conn_ctx (stream->conn);
626 stream_session = session_alloc (qctx->c_thread_index);
627 QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
628 session_handle (stream_session), sctx_id);
629 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
630 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
631 sctx->parent_app_id = qctx->parent_app_id;
632 sctx->quic_connection_ctx_id = qctx->c_c_index;
633 sctx->c_c_index = sctx_id;
634 sctx->c_s_index = stream_session->session_index;
635 sctx->stream = stream;
636 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
637 sctx->flags |= QUIC_F_IS_STREAM;
639 stream_data = (quic_stream_data_t *) stream->data;
640 stream_data->ctx_id = sctx_id;
641 stream_data->thread_index = sctx->c_thread_index;
642 stream_data->app_rx_data_len = 0;
644 sctx->c_s_index = stream_session->session_index;
645 stream_session->session_state = SESSION_STATE_CREATED;
646 stream_session->app_wrk_index = sctx->parent_app_wrk_id;
647 stream_session->connection_index = sctx->c_c_index;
648 stream_session->session_type =
649 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
650 quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
651 stream_session->listener_handle = listen_session_get_handle (quic_session);
653 app_wrk = app_worker_get (stream_session->app_wrk_index);
654 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
656 QUIC_DBG (1, "failed to allocate fifos");
657 session_free (stream_session);
658 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
659 return 0; /* Frame is still valid */
661 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
662 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
663 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
665 if ((rv = app_worker_accept_notify (app_wrk, stream_session)))
667 QUIC_DBG (1, "failed to notify accept worker app");
668 session_free_w_fifos (stream_session);
669 quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
670 return 0; /* Frame is still valid */
677 quic_on_closed_by_peer (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
678 int code, uint64_t frame_type,
679 const char *reason, size_t reason_len)
681 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
683 session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
684 clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
685 session_handle (quic_session), quic_format_err, code,
688 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
689 session_transport_closing_notify (&ctx->connection);
692 static quicly_stream_open_t on_stream_open = { quic_on_stream_open };
693 static quicly_closed_by_peer_t on_closed_by_peer = { quic_on_closed_by_peer };
698 quic_get_thread_time (u8 thread_index)
700 return quic_main.wrk_ctx[thread_index].time_now;
704 quic_get_time (quicly_now_t * self)
706 u8 thread_index = vlib_get_thread_index ();
707 return quic_get_thread_time (thread_index);
710 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
713 quic_set_time_now (u32 thread_index)
715 vlib_main_t *vlib_main = vlib_get_main ();
716 f64 time = vlib_time_now (vlib_main);
717 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
718 return quic_main.wrk_ctx[thread_index].time_now;
721 /* Transport proto callback */
723 quic_update_time (f64 now, u8 thread_index)
725 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
727 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
728 quic_set_time_now (thread_index);
729 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
733 quic_timer_expired (u32 conn_index)
736 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
737 quic_get_time (NULL));
738 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
739 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
740 quic_send_packets (ctx);
744 quic_update_timer (quic_ctx_t * ctx)
746 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
747 int64_t next_timeout, next_interval;
748 session_t *quic_session;
750 /* This timeout is in ms which is the unit of our timer */
751 next_timeout = quicly_get_first_timeout (ctx->conn);
752 next_interval = next_timeout - quic_get_time (NULL);
754 if (next_timeout == 0 || next_interval <= 0)
756 if (ctx->c_s_index == QUIC_SESSION_INVALID)
762 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
763 if (svm_fifo_set_event (quic_session->tx_fifo))
764 session_send_io_evt_to_thread_custom (quic_session,
765 quic_session->thread_index,
766 SESSION_IO_EVT_BUILTIN_TX);
771 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
773 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
774 next_interval, ctx->c_c_index);
776 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
778 if (next_timeout == INT64_MAX)
780 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
783 ctx->timer_handle = tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index,
788 if (next_timeout == INT64_MAX)
790 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
791 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
792 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
795 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
802 quic_expired_timers_dispatch (u32 * expired_timers)
806 for (i = 0; i < vec_len (expired_timers); i++)
808 quic_timer_expired (expired_timers[i]);
813 quic_encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
814 int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
816 quic_session_cache_t *self = (void *) _self;
822 /* replace the cached entry along with a newly generated session id */
823 clib_mem_free (self->data.base);
824 if ((self->data.base = clib_mem_alloc (src.len)) == NULL)
825 return PTLS_ERROR_NO_MEMORY;
827 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
828 clib_memcpy (self->data.base, src.base, src.len);
829 self->data.len = src.len;
831 /* store the session id in buffer */
832 if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
834 clib_memcpy (dst->base + dst->off, self->id, sizeof (self->id));
835 dst->off += sizeof (self->id);
841 /* check if session id is the one stored in cache */
842 if (src.len != sizeof (self->id))
843 return PTLS_ERROR_SESSION_NOT_FOUND;
844 if (clib_memcmp (self->id, src.base, sizeof (self->id)) != 0)
845 return PTLS_ERROR_SESSION_NOT_FOUND;
847 /* return the cached value */
848 if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
850 clib_memcpy (dst->base + dst->off, self->data.base, self->data.len);
851 dst->off += self->data.len;
858 quic_store_quicly_ctx (application_t * app, u32 cert_key_index)
860 quic_main_t *qm = &quic_main;
861 quicly_context_t *quicly_ctx;
862 ptls_iovec_t key_vec;
863 app_cert_key_pair_t *ckpair;
867 quicly_ctx_data_t *quicly_ctx_data =
868 clib_mem_alloc (sizeof (quicly_ctx_data_t));
869 clib_memset (quicly_ctx_data, 0, sizeof (*quicly_ctx_data)); /* picotls depends on this */
870 quicly_ctx = &quicly_ctx_data->quicly_ctx;
871 ptls_context_t *ptls_ctx = &quicly_ctx_data->ptls_ctx;
872 ptls_ctx->random_bytes = ptls_openssl_random_bytes;
873 ptls_ctx->get_time = &ptls_get_time;
874 ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
875 ptls_ctx->cipher_suites = qm->quic_ciphers[qm->default_cipher];
876 ptls_ctx->certificates.list = NULL;
877 ptls_ctx->certificates.count = 0;
878 ptls_ctx->esni = NULL;
879 ptls_ctx->on_client_hello = NULL;
880 ptls_ctx->emit_certificate = NULL;
881 ptls_ctx->sign_certificate = NULL;
882 ptls_ctx->verify_certificate = NULL;
883 ptls_ctx->ticket_lifetime = 86400;
884 ptls_ctx->max_early_data_size = 8192;
885 ptls_ctx->hkdf_label_prefix__obsolete = NULL;
886 ptls_ctx->require_dhe_on_psk = 1;
887 ptls_ctx->encrypt_ticket = &qm->session_cache.super;
889 app->quicly_ctx = (u64 *) quicly_ctx;
890 clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
892 quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
893 quicly_ctx->tls = ptls_ctx;
894 quicly_ctx->stream_open = &on_stream_open;
895 quicly_ctx->closed_by_peer = &on_closed_by_peer;
896 quicly_ctx->now = &quicly_vpp_now_cb;
897 quicly_amend_ptls_context (quicly_ctx->tls);
899 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
900 quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
901 quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
902 quicly_ctx->transport_params.max_stream_data.bidi_local = (qm->udp_fifo_size - 1); /* max_enq is SIZE - 1 */
903 quicly_ctx->transport_params.max_stream_data.bidi_remote = (qm->udp_fifo_size - 1); /* max_enq is SIZE - 1 */
904 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
906 quicly_ctx->tls->random_bytes (quicly_ctx_data->cid_key, 16);
907 quicly_ctx_data->cid_key[16] = 0;
908 key_vec = ptls_iovec_init (quicly_ctx_data->cid_key,
909 strlen (quicly_ctx_data->cid_key));
910 quicly_ctx->cid_encryptor =
911 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
912 &ptls_openssl_aes128ecb,
913 &ptls_openssl_sha256, key_vec);
915 ckpair = app_cert_key_pair_get_if_valid (cert_key_index);
916 if (ckpair && ckpair->key != NULL && ckpair->cert != NULL)
918 if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
920 QUIC_DBG (1, "failed to read private key from app configuration\n");
922 if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
924 QUIC_DBG (1, "failed to load certificate\n");
929 /* Transport proto functions */
932 quic_connect_stream (session_t * quic_session, u32 opaque)
934 uint64_t quic_session_handle;
935 session_t *stream_session;
936 quic_stream_data_t *stream_data;
937 quicly_stream_t *stream;
939 app_worker_t *app_wrk;
940 quic_ctx_t *qctx, *sctx;
944 /* Find base session to which the user want to attach a stream */
945 quic_session_handle = session_handle (quic_session);
946 QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
948 if (session_type_transport_proto (quic_session->session_type) !=
949 TRANSPORT_PROTO_QUIC)
951 QUIC_DBG (1, "received incompatible session");
955 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
958 QUIC_DBG (1, "Invalid app worker :(");
962 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
963 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
964 qctx = quic_ctx_get (quic_session->connection_index,
965 quic_session->thread_index);
966 if (quic_ctx_is_stream (qctx))
968 QUIC_DBG (1, "session is a stream");
969 quic_ctx_free (sctx);
973 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
974 sctx->parent_app_id = qctx->parent_app_id;
975 sctx->quic_connection_ctx_id = qctx->c_c_index;
976 sctx->c_c_index = sctx_index;
977 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
978 sctx->flags |= QUIC_F_IS_STREAM;
982 if (!conn || !quicly_connection_is_ready (conn))
985 if ((rv = quicly_open_stream (conn, &stream, 0 /* uni */ )))
987 QUIC_DBG (2, "Stream open failed with %d", rv);
990 sctx->stream = stream;
992 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
994 stream_session = session_alloc (qctx->c_thread_index);
995 QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
996 session_handle (stream_session), sctx_index);
997 stream_session->app_wrk_index = app_wrk->wrk_index;
998 stream_session->connection_index = sctx_index;
999 stream_session->listener_handle = quic_session_handle;
1000 stream_session->session_type =
1001 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1003 sctx->c_s_index = stream_session->session_index;
1005 if (app_worker_init_connected (app_wrk, stream_session))
1007 QUIC_DBG (1, "failed to app_worker_init_connected");
1008 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
1009 session_free_w_fifos (stream_session);
1010 quic_ctx_free (sctx);
1011 return app_worker_connect_notify (app_wrk, NULL, opaque);
1014 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1015 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1016 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1018 stream_session->session_state = SESSION_STATE_READY;
1019 if (app_worker_connect_notify (app_wrk, stream_session, opaque))
1021 QUIC_DBG (1, "failed to notify app");
1022 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1023 session_free_w_fifos (stream_session);
1024 quic_ctx_free (sctx);
1027 stream_data = (quic_stream_data_t *) stream->data;
1028 stream_data->ctx_id = sctx->c_c_index;
1029 stream_data->thread_index = sctx->c_thread_index;
1030 stream_data->app_rx_data_len = 0;
1035 quic_connect_connection (session_endpoint_cfg_t * sep)
1037 vnet_connect_args_t _cargs, *cargs = &_cargs;
1038 quic_main_t *qm = &quic_main;
1040 app_worker_t *app_wrk;
1045 clib_memset (cargs, 0, sizeof (*cargs));
1046 ctx_index = quic_ctx_alloc (vlib_get_thread_index ());
1047 ctx = quic_ctx_get (ctx_index, vlib_get_thread_index ());
1048 ctx->parent_app_wrk_id = sep->app_wrk_index;
1049 ctx->c_s_index = QUIC_SESSION_INVALID;
1050 ctx->c_c_index = ctx_index;
1051 ctx->udp_is_ip4 = sep->is_ip4;
1052 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1053 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1054 ctx->client_opaque = sep->opaque;
1055 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1057 ctx->srv_hostname = format (0, "%v", sep->hostname);
1059 /* needed by quic for crypto + determining client / server */
1060 ctx->srv_hostname = format (0, "%U", format_ip46_address,
1061 &sep->ip, sep->is_ip4);
1062 vec_terminate_c_string (ctx->srv_hostname);
1064 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1065 cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1066 cargs->app_index = qm->app_index;
1067 cargs->api_context = ctx_index;
1069 app_wrk = app_worker_get (sep->app_wrk_index);
1070 app = application_get (app_wrk->app_index);
1071 ctx->parent_app_id = app_wrk->app_index;
1072 cargs->sep_ext.ns_index = app->ns_index;
1074 quic_store_quicly_ctx (app, ctx->ckpair_index);
1075 /* Also store it in ctx for convenience
1076 * Waiting for crypto_ctx logic */
1077 ctx->quicly_ctx = (quicly_context_t *) app->quicly_ctx;
1079 if ((error = vnet_connect (cargs)))
1086 quic_connect (transport_endpoint_cfg_t * tep)
1088 QUIC_DBG (2, "Called quic_connect");
1089 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1090 session_t *quic_session;
1091 sep = (session_endpoint_cfg_t *) tep;
1093 quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1095 return quic_connect_stream (quic_session, sep->opaque);
1097 return quic_connect_connection (sep);
1101 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1103 quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1107 session_t *stream_session = session_get (ctx->c_s_index,
1108 ctx->c_thread_index);
1109 clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1111 if (quic_ctx_is_stream (ctx))
1113 quicly_stream_t *stream = ctx->stream;
1114 quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1115 quic_send_packets (ctx);
1119 switch (ctx->conn_state)
1121 case QUIC_CONN_STATE_READY:
1122 ctx->conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1123 quicly_conn_t *conn = ctx->conn;
1124 /* Start connection closing. Keep sending packets until quicly_send
1125 returns QUICLY_ERROR_FREE_CONNECTION */
1126 quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1127 /* This also causes all streams to be closed (and the cb called) */
1128 quic_send_packets (ctx);
1130 case QUIC_CONN_STATE_PASSIVE_CLOSING:
1131 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1132 /* send_packets will eventually return an error, we delete the conn at
1135 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1136 quic_connection_delete (ctx);
1139 QUIC_DBG (0, "BUG");
1145 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1147 vnet_listen_args_t _bargs, *args = &_bargs;
1148 quic_main_t *qm = &quic_main;
1149 session_handle_t udp_handle;
1150 session_endpoint_cfg_t *sep;
1151 session_t *udp_listen_session;
1152 app_worker_t *app_wrk;
1156 app_listener_t *app_listener;
1158 sep = (session_endpoint_cfg_t *) tep;
1159 app_wrk = app_worker_get (sep->app_wrk_index);
1160 /* We need to call this because we call app_worker_init_connected in
1161 * quic_accept_stream, which assumes the connect segment manager exists */
1162 app_worker_alloc_connects_segment_manager (app_wrk);
1163 app = application_get (app_wrk->app_index);
1164 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1166 quic_store_quicly_ctx (app, sep->ckpair_index);
1168 sep->transport_proto = TRANSPORT_PROTO_UDPC;
1169 clib_memset (args, 0, sizeof (*args));
1170 args->app_index = qm->app_index;
1171 args->sep_ext = *sep;
1172 args->sep_ext.ns_index = app->ns_index;
1173 if (vnet_listen (args))
1176 lctx_index = quic_ctx_alloc (0);
1177 udp_handle = args->handle;
1178 app_listener = app_listener_get_w_handle (udp_handle);
1179 udp_listen_session = app_listener_get_session (app_listener);
1180 udp_listen_session->opaque = lctx_index;
1182 lctx = quic_ctx_get (lctx_index, 0);
1183 lctx->flags |= QUIC_F_IS_LISTENER;
1184 /* Also store it in ctx for convenience
1185 * Waiting for crypto_ctx logic */
1186 lctx->quicly_ctx = (quicly_context_t *) app->quicly_ctx;
1188 clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1189 clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1190 lctx->c_rmt_port = args->sep.peer.port;
1191 lctx->c_lcl_port = args->sep.port;
1192 lctx->c_is_ip4 = args->sep.is_ip4;
1193 lctx->c_fib_index = args->sep.fib_index;
1194 lctx->c_proto = TRANSPORT_PROTO_QUIC;
1195 lctx->parent_app_wrk_id = sep->app_wrk_index;
1196 lctx->parent_app_id = app_wrk->app_index;
1197 lctx->udp_session_handle = udp_handle;
1198 lctx->c_s_index = quic_listen_session_index;
1200 QUIC_DBG (2, "Listening UDP session 0x%lx",
1201 session_handle (udp_listen_session));
1202 QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1207 quic_stop_listen (u32 lctx_index)
1209 QUIC_DBG (2, "Called quic_stop_listen");
1211 lctx = quic_ctx_get (lctx_index, 0);
1212 ASSERT (quic_ctx_is_listener (lctx));
1213 vnet_unlisten_args_t a = {
1214 .handle = lctx->udp_session_handle,
1215 .app_index = quic_main.app_index,
1216 .wrk_map_index = 0 /* default wrk */
1218 if (vnet_unlisten (&a))
1219 clib_warning ("unlisten errored");
1221 /* TODO: crypto state cleanup */
1223 quic_ctx_free (lctx);
1227 static transport_connection_t *
1228 quic_connection_get (u32 ctx_index, u32 thread_index)
1231 ctx = quic_ctx_get (ctx_index, thread_index);
1232 return &ctx->connection;
1235 static transport_connection_t *
1236 quic_listener_get (u32 listener_index)
1238 QUIC_DBG (2, "Called quic_listener_get");
1240 ctx = quic_ctx_get (listener_index, 0);
1241 return &ctx->connection;
1245 format_quic_ctx (u8 * s, va_list * args)
1247 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1248 u32 verbose = va_arg (*args, u32);
1253 str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1255 if (quic_ctx_is_listener (ctx))
1256 str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1257 else if (quic_ctx_is_stream (ctx))
1258 str = format (str, "Stream %ld conn %d",
1259 ctx->stream->stream_id, ctx->quic_connection_ctx_id);
1260 else /* connection */
1261 str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1262 ctx->udp_session_handle);
1264 str = format (str, " app %d wrk %d", ctx->parent_app_id,
1265 ctx->parent_app_wrk_id);
1268 s = format (s, "%-50s%-15d", str, ctx->conn_state);
1270 s = format (s, "%s\n", str);
1276 format_quic_connection (u8 * s, va_list * args)
1278 u32 qc_index = va_arg (*args, u32);
1279 u32 thread_index = va_arg (*args, u32);
1280 u32 verbose = va_arg (*args, u32);
1281 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1282 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1287 format_quic_half_open (u8 * s, va_list * args)
1289 u32 qc_index = va_arg (*args, u32);
1290 u32 thread_index = va_arg (*args, u32);
1291 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1292 s = format (s, "[#%d][Q] half-open app %u", thread_index,
1293 ctx->parent_app_id);
1299 format_quic_listener (u8 * s, va_list * args)
1301 u32 tci = va_arg (*args, u32);
1302 u32 thread_index = va_arg (*args, u32);
1303 u32 verbose = va_arg (*args, u32);
1304 quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1305 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1309 /* Session layer callbacks */
1312 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1313 ip46_address_t * addr, u16 port, u8 is_ip4)
1317 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1318 sa4->sin_family = AF_INET;
1319 sa4->sin_port = port;
1320 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1321 *salen = sizeof (struct sockaddr_in);
1325 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1326 sa6->sin6_family = AF_INET6;
1327 sa6->sin6_port = port;
1328 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1329 *salen = sizeof (struct sockaddr_in6);
1334 quic_on_quic_session_connected (quic_ctx_t * ctx)
1336 session_t *quic_session;
1337 app_worker_t *app_wrk;
1338 u32 ctx_id = ctx->c_c_index;
1339 u32 thread_index = ctx->c_thread_index;
1342 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1345 quic_disconnect_transport (ctx);
1349 quic_session = session_alloc (thread_index);
1351 QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1352 ctx->c_s_index = quic_session->session_index;
1353 quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1354 quic_session->connection_index = ctx->c_c_index;
1355 quic_session->listener_handle = SESSION_INVALID_HANDLE;
1356 quic_session->session_type =
1357 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1359 if (app_worker_init_connected (app_wrk, quic_session))
1361 QUIC_DBG (1, "failed to app_worker_init_connected");
1362 quic_proto_on_close (ctx_id, thread_index);
1363 return app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1366 quic_session->session_state = SESSION_STATE_CONNECTING;
1367 if ((rv = app_worker_connect_notify (app_wrk, quic_session,
1368 ctx->client_opaque)))
1370 QUIC_DBG (1, "failed to notify app %d", rv);
1371 quic_proto_on_close (ctx_id, thread_index);
1375 /* If the app opens a stream in its callback it may invalidate ctx */
1376 ctx = quic_ctx_get (ctx_id, thread_index);
1378 * app_worker_connect_notify() might have reallocated pool, reload
1379 * quic_session pointer
1381 quic_session = session_get (ctx->c_s_index, thread_index);
1382 quic_session->session_state = SESSION_STATE_LISTENING;
1388 quic_check_quic_session_connected (quic_ctx_t * ctx)
1390 /* Called when we need to trigger quic session connected
1391 * we may call this function on the server side / at
1394 /* Conn may be set to null if the connection is terminated */
1395 if (!ctx->conn || ctx->conn_state != QUIC_CONN_STATE_HANDSHAKE)
1397 if (!quicly_connection_is_ready (ctx->conn))
1399 ctx->conn_state = QUIC_CONN_STATE_READY;
1400 if (!quicly_is_client (ctx->conn))
1402 return quic_on_quic_session_connected (ctx);
1406 quic_receive_connection (void *arg)
1408 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1409 quic_ctx_t *temp_ctx, *new_ctx;
1410 clib_bihash_kv_16_8_t kv;
1411 quicly_conn_t *conn;
1412 session_t *udp_session;
1415 new_ctx_id = quic_ctx_alloc (thread_index);
1416 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1418 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1422 clib_memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1423 clib_mem_free (temp_ctx);
1425 new_ctx->c_thread_index = thread_index;
1426 new_ctx->c_c_index = new_ctx_id;
1428 conn = new_ctx->conn;
1429 quic_store_conn_ctx (conn, new_ctx);
1430 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1431 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1432 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1433 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1434 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1435 quic_update_timer (new_ctx);
1437 /* Trigger write on this connection if necessary */
1438 udp_session = session_get_from_handle (new_ctx->udp_session_handle);
1439 udp_session->opaque = new_ctx_id;
1440 udp_session->flags &= ~SESSION_F_IS_MIGRATING;
1441 if (svm_fifo_max_dequeue (udp_session->tx_fifo))
1442 quic_set_udp_tx_evt (udp_session);
1446 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1448 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1449 quic_ctx_t *ctx, *temp_ctx;
1450 u32 thread_index = vlib_get_thread_index ();
1452 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1454 temp_ctx = clib_mem_alloc (sizeof (quic_ctx_t));
1456 ctx = quic_ctx_get (ctx_index, thread_index);
1458 clib_memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1460 /* Remove from timer wheel and thread-local pool */
1461 if (ctx->timer_handle != QUIC_TIMER_HANDLE_INVALID)
1463 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1464 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
1466 quic_ctx_free (ctx);
1468 /* Send connection to destination thread */
1469 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1474 quic_udp_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1475 session_t * udp_session, u8 is_fail)
1477 QUIC_DBG (2, "QSession is now connected (id %u)",
1478 udp_session->session_index);
1479 /* This should always be called before quic_connect returns since UDP always
1480 * connects instantly. */
1481 clib_bihash_kv_16_8_t kv;
1482 struct sockaddr_in6 sa6;
1483 struct sockaddr *sa = (struct sockaddr *) &sa6;
1485 transport_connection_t *tc;
1486 app_worker_t *app_wrk;
1487 quicly_conn_t *conn;
1489 u32 thread_index = vlib_get_thread_index ();
1491 quicly_context_t *quicly_ctx;
1494 ctx = quic_ctx_get (ctx_index, thread_index);
1498 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1501 api_context = ctx->c_s_index;
1502 app_worker_connect_notify (app_wrk, 0, api_context);
1507 ctx->c_thread_index = thread_index;
1508 ctx->c_c_index = ctx_index;
1510 QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1511 is_fail, thread_index, (ctx) ? ctx_index : ~0);
1513 ctx->udp_session_handle = session_handle (udp_session);
1514 udp_session->opaque = ctx_index;
1516 /* Init QUIC lib connection
1517 * Generate required sockaddr & salen */
1518 tc = session_get_transport (udp_session);
1519 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1521 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1522 ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
1523 sa, NULL, &quic_main.next_cid, ptls_iovec_init (NULL,
1525 &quic_main.hs_properties, NULL);
1526 ++quic_main.next_cid.master_id;
1527 /* Save context handle in quicly connection */
1528 quic_store_conn_ctx (ctx->conn, ctx);
1531 /* Register connection in connections map */
1533 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1534 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1535 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1536 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1538 /* UDP stack quirk? preemptively transfer connection if that happens */
1539 if (udp_session->thread_index != thread_index)
1540 quic_transfer_connection (ctx_index, udp_session->thread_index);
1542 quic_send_packets (ctx);
1548 quic_udp_session_disconnect_callback (session_t * s)
1550 clib_warning ("UDP session disconnected???");
1554 quic_udp_session_reset_callback (session_t * s)
1556 clib_warning ("UDP session reset???");
1560 quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
1562 u32 new_thread = session_thread_from_handle (new_sh);
1565 QUIC_DBG (1, "Session %x migrated to %lx", s->session_index, new_sh);
1566 ASSERT (vlib_get_thread_index () == s->thread_index);
1567 ctx = quic_ctx_get (s->opaque, s->thread_index);
1568 ASSERT (ctx->udp_session_handle == session_handle (s));
1570 ctx->udp_session_handle = new_sh;
1572 s->opaque = 0xfeedface;
1574 quic_transfer_connection (ctx->c_c_index, new_thread);
1578 quic_udp_session_accepted_callback (session_t * udp_session)
1580 /* New UDP connection, try to accept it */
1583 quic_ctx_t *ctx, *lctx;
1584 session_t *udp_listen_session;
1585 u32 thread_index = vlib_get_thread_index ();
1587 udp_listen_session =
1588 listen_session_get_from_handle (udp_session->listener_handle);
1590 ctx_index = quic_ctx_alloc (thread_index);
1591 ctx = quic_ctx_get (ctx_index, thread_index);
1592 ctx->c_thread_index = udp_session->thread_index;
1593 ctx->c_c_index = ctx_index;
1594 ctx->c_s_index = QUIC_SESSION_INVALID;
1595 ctx->udp_session_handle = session_handle (udp_session);
1596 QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
1597 ctx->listener_ctx_id = udp_listen_session->opaque;
1598 lctx = quic_ctx_get (udp_listen_session->opaque,
1599 udp_listen_session->thread_index);
1600 ctx->udp_is_ip4 = lctx->c_is_ip4;
1601 ctx->parent_app_id = lctx->parent_app_id;
1602 ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
1603 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1604 ctx->conn_state = QUIC_CONN_STATE_OPENED;
1605 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1607 /* Also store it in ctx for convenience
1608 * Waiting for crypto_ctx logic */
1609 ctx->quicly_ctx = lctx->quicly_ctx;
1611 udp_session->opaque = ctx_index;
1613 /* Put this ctx in the "opening" pool */
1614 pool_get (quic_main.wrk_ctx[ctx->c_thread_index].opening_ctx_pool,
1616 *pool_index = ctx_index;
1618 /* TODO timeout to delete these if they never connect */
1623 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1625 /* No-op for builtin */
1630 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1632 /* No-op for builtin */
1637 quic_custom_app_rx_callback (transport_connection_t * tc)
1640 session_t *stream_session = session_get (tc->s_index, tc->thread_index);
1641 QUIC_DBG (3, "Received app READ notification");
1642 quic_ack_rx_data (stream_session);
1643 svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
1645 /* Need to send packets (acks may never be sent otherwise) */
1646 ctx = quic_ctx_get (stream_session->connection_index,
1647 stream_session->thread_index);
1648 quic_send_packets (ctx);
1653 quic_custom_tx_callback (void *s, u32 max_burst_size)
1655 session_t *stream_session = (session_t *) s;
1656 quicly_stream_t *stream;
1661 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1663 ctx = quic_ctx_get (stream_session->connection_index,
1664 stream_session->thread_index);
1665 if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
1667 goto tx_end; /* Most probably a reschedule */
1670 QUIC_DBG (3, "Stream TX event");
1671 quic_ack_rx_data (stream_session);
1672 if (!svm_fifo_max_dequeue (stream_session->tx_fifo))
1675 stream = ctx->stream;
1676 if (!quicly_sendstate_is_open (&stream->sendstate))
1678 QUIC_DBG (1, "Warning: tried to send on closed stream");
1682 if ((rv = quicly_stream_sync_sendbuf (stream, 1)) != 0)
1686 quic_send_packets (ctx);
1692 * Returns 0 if a matching connection is found and is on the right thread.
1693 * Otherwise returns -1.
1694 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
1698 quic_find_packet_ctx (u32 * ctx_thread, u32 * ctx_index,
1699 struct sockaddr *sa, socklen_t salen,
1700 quicly_decoded_packet_t * packet,
1701 u32 caller_thread_index)
1704 quicly_conn_t *conn_;
1705 clib_bihash_kv_16_8_t kv;
1706 clib_bihash_16_8_t *h;
1708 h = &quic_main.connection_hash;
1709 quic_make_connection_key (&kv, &packet->cid.dest.plaintext);
1710 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
1712 if (clib_bihash_search_16_8 (h, &kv, &kv) == 0)
1714 u32 index = kv.value & UINT32_MAX;
1715 u32 thread_id = kv.value >> 32;
1716 /* Check if this connection belongs to this thread, otherwise
1717 * ask for it to be moved */
1718 if (thread_id != caller_thread_index)
1720 QUIC_DBG (2, "Connection is on wrong thread");
1721 /* Cannot make full check with quicly_is_destination... */
1723 *ctx_thread = thread_id;
1726 ctx_ = quic_ctx_get (index, vlib_get_thread_index ());
1728 if (conn_ && quicly_is_destination (conn_, NULL, sa, packet))
1730 QUIC_DBG (3, "Connection found");
1732 *ctx_thread = thread_id;
1736 QUIC_DBG (3, "connection not found");
1741 quic_accept_connection (u32 ctx_index, struct sockaddr *sa,
1742 socklen_t salen, quicly_decoded_packet_t packet)
1744 u32 thread_index = vlib_get_thread_index ();
1745 quicly_context_t *quicly_ctx;
1746 session_t *quic_session;
1747 clib_bihash_kv_16_8_t kv;
1748 app_worker_t *app_wrk;
1749 quicly_conn_t *conn;
1754 /* new connection, accept and create context if packet is valid
1755 * TODO: check if socket is actually listening? */
1756 ctx = quic_ctx_get (ctx_index, thread_index);
1757 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1758 if ((rv = quicly_accept (&conn, quicly_ctx, NULL, sa,
1759 &packet, NULL, &quic_main.next_cid, NULL)))
1761 /* Invalid packet, pass */
1762 assert (conn == NULL);
1763 QUIC_DBG (1, "Accept failed with %d", rv);
1764 /* TODO: cleanup created quic ctx and UDP session */
1767 assert (conn != NULL);
1769 ++quic_main.next_cid.master_id;
1770 /* Save ctx handle in quicly connection */
1771 quic_store_conn_ctx (conn, ctx);
1773 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1775 quic_session = session_alloc (ctx->c_thread_index);
1776 QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
1777 session_handle (quic_session), ctx->c_c_index);
1778 quic_session->session_state = SESSION_STATE_LISTENING;
1779 ctx->c_s_index = quic_session->session_index;
1781 lctx = quic_ctx_get (ctx->listener_ctx_id, 0);
1783 quic_session->app_wrk_index = lctx->parent_app_wrk_id;
1784 quic_session->connection_index = ctx->c_c_index;
1785 quic_session->session_type =
1786 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1787 quic_session->listener_handle = lctx->c_s_index;
1789 /* TODO: don't alloc fifos when we don't transfer data on this session
1790 * but we still need fifos for the events? */
1791 if ((rv = app_worker_init_accepted (quic_session)))
1793 QUIC_DBG (1, "failed to allocate fifos");
1794 session_free (quic_session);
1797 app_wrk = app_worker_get (quic_session->app_wrk_index);
1798 if ((rv = app_worker_accept_notify (app_wrk, quic_session)))
1800 QUIC_DBG (1, "failed to notify accept worker app");
1804 /* Register connection in connections map */
1805 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1806 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1807 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1808 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1810 return quic_send_packets (ctx);
1814 quic_reset_connection (u64 udp_session_handle,
1815 struct sockaddr *sa, socklen_t salen,
1816 quicly_decoded_packet_t packet)
1818 /* short header packet; potentially a dead connection. No need to check the
1819 * length of the incoming packet, because loop is prevented by authenticating
1820 * the CID (by checking node_id and thread_id). If the peer is also sending a
1821 * reset, then the next CID is highly likely to contain a non-authenticating
1823 QUIC_DBG (2, "Sending stateless reset");
1825 quicly_datagram_t *dgram;
1826 session_t *udp_session;
1827 quicly_context_t *quicly_ctx;
1828 if (packet.cid.dest.plaintext.node_id != 0
1829 || packet.cid.dest.plaintext.thread_id != 0)
1831 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
1832 dgram = quicly_send_stateless_reset (quicly_ctx, sa, NULL,
1833 &packet.cid.dest.plaintext);
1836 udp_session = session_get_from_handle (udp_session_handle);
1837 rv = quic_send_datagram (udp_session, dgram);
1838 quic_set_udp_tx_evt (udp_session);
1843 quic_process_one_rx_packet (u64 udp_session_handle, svm_fifo_t * f,
1844 u32 * fifo_offset, u32 * max_packet, u32 packet_n,
1845 quic_rx_packet_ctx_t * packet_ctx)
1847 session_dgram_hdr_t ph;
1848 quic_ctx_t *ctx = NULL;
1850 struct sockaddr_in6 sa6;
1851 struct sockaddr *sa = (struct sockaddr *) &sa6;
1855 packet_ctx->thread_index = UINT32_MAX;
1856 packet_ctx->ctx_index = UINT32_MAX;
1857 u32 thread_index = vlib_get_thread_index ();
1858 u32 *opening_ctx_pool, *ctx_index_ptr;
1859 u32 cur_deq = svm_fifo_max_dequeue (f) - *fifo_offset;
1860 quicly_context_t *quicly_ctx;
1864 *max_packet = packet_n + 1;
1868 if (cur_deq < SESSION_CONN_HDR_LEN)
1870 QUIC_DBG (1, "Not enough data for even a header in RX");
1873 ret = svm_fifo_peek (f, *fifo_offset, SESSION_CONN_HDR_LEN, (u8 *) & ph);
1874 if (ret != SESSION_CONN_HDR_LEN)
1876 QUIC_DBG (1, "Not enough data for header in RX");
1879 ASSERT (ph.data_offset == 0);
1880 full_len = ph.data_length + SESSION_CONN_HDR_LEN;
1881 if (full_len > cur_deq)
1883 QUIC_DBG (1, "Not enough data in fifo RX");
1887 /* Quicly can read len bytes from the fifo at offset:
1888 * ph.data_offset + SESSION_CONN_HDR_LEN */
1889 ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN + *fifo_offset,
1890 ph.data_length, packet_ctx->data);
1891 if (ret != ph.data_length)
1893 QUIC_DBG (1, "Not enough data peeked in RX");
1897 quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
1899 quic_build_sockaddr (sa, &salen, &ph.rmt_ip, ph.rmt_port, ph.is_ip4);
1900 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
1901 plen = quicly_decode_packet (quicly_ctx, &packet_ctx->packet,
1902 packet_ctx->data, ph.data_length);
1904 if (plen == SIZE_MAX)
1906 *fifo_offset += SESSION_CONN_HDR_LEN + ph.data_length;
1910 err = quic_find_packet_ctx (&packet_ctx->thread_index,
1911 &packet_ctx->ctx_index, sa, salen,
1912 &packet_ctx->packet, thread_index);
1915 ctx = quic_ctx_get (packet_ctx->ctx_index, thread_index);
1916 rv = quicly_receive (ctx->conn, NULL, sa, &packet_ctx->packet);
1918 QUIC_DBG (1, "quicly_receive return error %d", rv);
1920 else if (packet_ctx->ctx_index != UINT32_MAX)
1922 /* Connection found but on wrong thread, ask move */
1923 *max_packet = packet_n + 1;
1926 else if ((packet_ctx->packet.octets.base[0] & QUICLY_PACKET_TYPE_BITMASK) ==
1927 QUICLY_PACKET_TYPE_INITIAL)
1929 /* Try to find matching "opening" ctx */
1930 opening_ctx_pool = quic_main.wrk_ctx[thread_index].opening_ctx_pool;
1933 pool_foreach (ctx_index_ptr, opening_ctx_pool,
1935 ctx = quic_ctx_get (*ctx_index_ptr, thread_index);
1936 if (ctx->udp_session_handle == udp_session_handle)
1938 /* Right ctx found, create conn & remove from pool */
1939 quic_accept_connection (*ctx_index_ptr, sa, salen, packet_ctx->packet);
1940 *max_packet = packet_n + 1;
1941 packet_ctx->thread_index = thread_index;
1942 packet_ctx->ctx_index = *ctx_index_ptr;
1943 pool_put (opening_ctx_pool, ctx_index_ptr);
1951 quic_reset_connection (udp_session_handle, sa, salen,
1952 packet_ctx->packet);
1956 *fifo_offset += SESSION_CONN_HDR_LEN + ph.data_length;
1961 quic_udp_session_rx_callback (session_t * udp_session)
1963 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
1964 quic_ctx_t *ctx = NULL;
1967 u64 udp_session_handle = session_handle (udp_session);
1969 u32 thread_index = vlib_get_thread_index ();
1970 quic_rx_packet_ctx_t packets_ctx[16];
1971 u32 i, fifo_offset, max_packets;
1973 if (udp_session->flags & SESSION_F_IS_MIGRATING)
1975 QUIC_DBG (3, "RX on migrating udp session");
1981 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
1982 f = udp_session->rx_fifo;
1983 max_deq = svm_fifo_max_dequeue (f);
1989 for (i = 0; i < max_packets; i++)
1990 quic_process_one_rx_packet (udp_session_handle, f, &fifo_offset,
1991 &max_packets, i, &packets_ctx[i]);
1993 for (i = 0; i < max_packets; i++)
1995 if (packets_ctx[i].thread_index != thread_index)
1997 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
1998 packets_ctx[i].thread_index);
1999 quic_check_quic_session_connected (ctx);
2000 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2001 packets_ctx[i].thread_index);
2002 quic_send_packets (ctx);
2004 svm_fifo_dequeue_drop (f, fifo_offset);
2010 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2011 transport_endpoint_t * tep, u8 is_lcl)
2013 session_t *udp_session;
2014 if (!quic_ctx_is_stream (ctx))
2016 udp_session = session_get_from_handle (ctx->udp_session_handle);
2017 session_get_endpoint (udp_session, tep, is_lcl);
2022 quic_get_transport_listener_endpoint (u32 listener_index,
2023 transport_endpoint_t * tep, u8 is_lcl)
2026 app_listener_t *app_listener;
2027 session_t *udp_listen_session;
2028 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2029 if (quic_ctx_is_listener (ctx))
2031 app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2032 udp_listen_session = app_listener_get_session (app_listener);
2033 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2035 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2039 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2040 transport_endpoint_t * tep, u8 is_lcl)
2043 ctx = quic_ctx_get (ctx_index, thread_index);
2044 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2048 static session_cb_vft_t quic_app_cb_vft = {
2049 .session_accept_callback = quic_udp_session_accepted_callback,
2050 .session_disconnect_callback = quic_udp_session_disconnect_callback,
2051 .session_connected_callback = quic_udp_session_connected_callback,
2052 .session_reset_callback = quic_udp_session_reset_callback,
2053 .session_migrate_callback = quic_udp_session_migrate_callback,
2054 .add_segment_callback = quic_add_segment_callback,
2055 .del_segment_callback = quic_del_segment_callback,
2056 .builtin_app_rx_callback = quic_udp_session_rx_callback,
2059 static const transport_proto_vft_t quic_proto = {
2060 .connect = quic_connect,
2061 .close = quic_proto_on_close,
2062 .start_listen = quic_start_listen,
2063 .stop_listen = quic_stop_listen,
2064 .get_connection = quic_connection_get,
2065 .get_listener = quic_listener_get,
2066 .update_time = quic_update_time,
2067 .app_rx_evt = quic_custom_app_rx_callback,
2068 .custom_tx = quic_custom_tx_callback,
2069 .format_connection = format_quic_connection,
2070 .format_half_open = format_quic_half_open,
2071 .format_listener = format_quic_listener,
2072 .get_transport_endpoint = quic_get_transport_endpoint,
2073 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2074 .transport_options = {
2075 .tx_type = TRANSPORT_TX_INTERNAL,
2076 .service_type = TRANSPORT_SERVICE_APP,
2082 quic_register_cipher_suite (crypto_engine_type_t type,
2083 ptls_cipher_suite_t ** ciphers)
2085 quic_main_t *qm = &quic_main;
2086 vec_validate (qm->quic_ciphers, type);
2087 qm->quic_ciphers[type] = ciphers;
2091 quic_update_fifo_size ()
2093 quic_main_t *qm = &quic_main;
2094 segment_manager_props_t *seg_mgr_props =
2095 application_get_segment_manager_properties (qm->app_index);
2100 ("error while getting segment_manager_props_t, can't update fifo-size");
2104 seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
2105 seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
2108 static clib_error_t *
2109 quic_init (vlib_main_t * vm)
2111 u32 segment_size = 256 << 20;
2112 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2113 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2114 vnet_app_attach_args_t _a, *a = &_a;
2115 u64 options[APP_OPTIONS_N_OPTIONS];
2116 quic_main_t *qm = &quic_main;
2119 num_threads = 1 /* main thread */ + vtm->n_threads;
2121 clib_memset (a, 0, sizeof (*a));
2122 clib_memset (options, 0, sizeof (options));
2124 a->session_cb_vft = &quic_app_cb_vft;
2125 a->api_client_index = APP_INVALID_INDEX;
2126 a->options = options;
2127 a->name = format (0, "quic");
2128 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2129 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2130 a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
2131 a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
2132 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
2133 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2134 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2135 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2137 if (vnet_application_attach (a))
2139 clib_warning ("failed to attach quic app");
2140 return clib_error_return (0, "failed to attach quic app");
2143 vec_validate (qm->ctx_pool, num_threads - 1);
2144 vec_validate (qm->wrk_ctx, num_threads - 1);
2145 /* Timer wheels, one per thread. */
2146 for (i = 0; i < num_threads; i++)
2148 tw = &qm->wrk_ctx[i].timer_wheel;
2149 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2150 1e-3 /* timer period 1ms */ , ~0);
2151 tw->last_run_time = vlib_time_now (vlib_get_main ());
2154 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2158 qm->app_index = a->app_index;
2159 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2160 / QUIC_TSTAMP_RESOLUTION;
2161 qm->session_cache.super.cb = quic_encrypt_ticket_cb;
2163 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2164 FIB_PROTOCOL_IP4, ~0);
2165 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2166 FIB_PROTOCOL_IP6, ~0);
2168 quic_register_cipher_suite (CRYPTO_ENGINE_VPP, quic_crypto_cipher_suites);
2169 quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
2170 ptls_openssl_cipher_suites);
2171 qm->default_cipher = CRYPTO_ENGINE_PICOTLS;
2176 VLIB_INIT_FUNCTION (quic_init);
2178 static clib_error_t *
2179 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2180 unformat_input_t * input,
2181 vlib_cli_command_t * cmd)
2183 quic_main_t *qm = &quic_main;
2184 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
2185 return clib_error_return (0, "unknown input '%U'",
2186 format_unformat_error, input);
2187 if (unformat (input, "vpp"))
2188 qm->default_cipher = CRYPTO_ENGINE_VPP;
2189 else if (unformat (input, "picotls"))
2190 qm->default_cipher = CRYPTO_ENGINE_PICOTLS;
2192 return clib_error_return (0, "unknown input '%U'",
2193 format_unformat_error, input);
2197 u64 quic_fifosize = 0;
2198 static clib_error_t *
2199 quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
2200 unformat_input_t * input,
2201 vlib_cli_command_t * cmd)
2203 unformat_input_t _line_input, *line_input = &_line_input;
2204 if (!unformat_user (input, unformat_line_input, line_input))
2207 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2210 (line_input, "%U", unformat_data_size, &quic_main.udp_fifo_size))
2211 quic_update_fifo_size ();
2213 return clib_error_return (0, "unknown input '%U'",
2214 format_unformat_error, line_input);
2221 quic_format_ctx_stat (u8 * s, va_list * args)
2223 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2224 quicly_stats_t quicly_stats;
2226 quicly_get_stats (ctx->conn, &quicly_stats);
2228 s = format (s, "\n\rQUIC conn stats \n\r");
2231 format (s, "RTT: min:%d, smoothed:%d, variance:%d, latest:%d \n\r",
2232 quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
2233 quicly_stats.rtt.variance, quicly_stats.rtt.latest);
2234 s = format (s, "Packet loss:%d \n\r", quicly_stats.num_packets.lost);
2239 static clib_error_t *
2240 quic_plugin_showstats_command_fn (vlib_main_t * vm,
2241 unformat_input_t * input,
2242 vlib_cli_command_t * cmd)
2244 quic_main_t *qm = &quic_main;
2245 quic_ctx_t *ctx = NULL;
2246 u32 num_workers = vlib_num_workers ();
2248 for (int i = 0; i < num_workers + 1; i++)
2251 pool_foreach (ctx, qm->ctx_pool[i],
2253 if(!(ctx->flags & QUIC_F_IS_LISTENER) && !(ctx->flags & QUIC_F_IS_STREAM))
2254 vlib_cli_output (vm, "%U", quic_format_ctx_stat, ctx);
2262 VLIB_CLI_COMMAND (quic_plugin_crypto_command, static) =
2264 .path = "quic set crypto api",
2265 .short_help = "quic set crypto api [picotls, vpp]",
2266 .function = quic_plugin_crypto_command_fn,
2268 VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
2270 .path = "quic set fifo-size",
2271 .short_help = "quic set fifo-size N[Kb|Mb|GB] (default 64K)",
2272 .function = quic_plugin_set_fifo_size_command_fn,
2274 VLIB_CLI_COMMAND(quic_plugin_stats_command, static)=
2276 .path = "show quic stats",
2277 .short_help = "show quic stats",
2278 .function = quic_plugin_showstats_command_fn,
2280 VLIB_PLUGIN_REGISTER () =
2282 .version = VPP_BUILD_VER,
2283 .description = "Quic transport protocol",
2284 .default_disabled = 1,
2288 static clib_error_t *
2289 quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
2291 quic_main.udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
2292 quic_main.udp_fifo_prealloc = 0;
2294 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2297 (input, "fifo-size %U", unformat_data_size,
2298 &quic_main.udp_fifo_size))
2302 (input, "fifo-prealloc %u", &quic_main.udp_fifo_prealloc))
2305 return clib_error_return (0, "unknown input '%U'",
2306 format_unformat_error, input);
2312 VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
2315 quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
2316 vlib_frame_t * frame)
2322 VLIB_REGISTER_NODE (quic_input_node) =
2324 .function = quic_node_fn,
2325 .name = "quic-input",
2326 .vector_size = sizeof (u32),
2327 .type = VLIB_NODE_TYPE_INTERNAL,
2328 .n_errors = ARRAY_LEN (quic_error_strings),
2329 .error_strings = quic_error_strings,
2334 * fd.io coding-style-patch-verification: ON
2337 * eval: (c-set-style "gnu")