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 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
56 QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
57 ctx - qm->ctx_pool[thread_index], thread_index);
58 return ctx - qm->ctx_pool[thread_index];
62 quic_ctx_free (quic_ctx_t * ctx)
64 QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
65 u32 thread_index = ctx->c_thread_index;
66 ASSERT (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID);
68 clib_memset (ctx, 0xfb, sizeof (*ctx));
69 pool_put (quic_main.ctx_pool[thread_index], ctx);
73 quic_ctx_get (u32 ctx_index, u32 thread_index)
75 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
79 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
81 if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
83 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
87 quic_get_conn_ctx (quicly_conn_t * conn)
90 conn_data = (u64) * quicly_get_data (conn);
91 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
95 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
97 *quicly_get_data (conn) =
98 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
102 quic_ctx_is_stream (quic_ctx_t * ctx)
104 return (ctx->flags & QUIC_F_IS_STREAM);
108 quic_ctx_is_listener (quic_ctx_t * ctx)
110 return (ctx->flags & QUIC_F_IS_LISTENER);
114 get_stream_session_from_stream (quicly_stream_t * stream)
117 quic_stream_data_t *stream_data;
119 stream_data = (quic_stream_data_t *) stream->data;
120 ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
121 return session_get (ctx->c_s_index, stream_data->thread_index);
125 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
126 const quicly_cid_plaintext_t * id)
128 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
129 kv->key[1] = id->node_id;
133 quic_sendable_packet_count (session_t * udp_session)
136 u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
137 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
138 return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
141 static quicly_context_t *
142 quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
144 return ctx->quicly_ctx;
147 static quicly_context_t *
148 quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
150 session_t *udp_session = session_get_from_handle (udp_session_handle);
152 quic_ctx_get (udp_session->opaque, udp_session->thread_index);
153 return ctx->quicly_ctx;
157 quic_set_udp_tx_evt (session_t * udp_session)
160 if (svm_fifo_set_event (udp_session->tx_fifo))
161 rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
163 if (PREDICT_FALSE (rv))
164 clib_warning ("Event enqueue errored %d", rv);
168 quic_stop_ctx_timer (quic_ctx_t * ctx)
170 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
171 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
173 tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
174 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
175 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
176 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
179 /* QUIC protocol actions */
182 quic_ack_rx_data (session_t * stream_session)
187 quicly_stream_t *stream;
188 quic_stream_data_t *stream_data;
190 sctx = quic_ctx_get (stream_session->connection_index,
191 stream_session->thread_index);
192 ASSERT (quic_ctx_is_stream (sctx));
193 stream = sctx->stream;
194 stream_data = (quic_stream_data_t *) stream->data;
196 f = stream_session->rx_fifo;
197 max_deq = svm_fifo_max_dequeue (f);
199 ASSERT (stream_data->app_rx_data_len >= max_deq);
200 quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
201 QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
202 stream_data->app_rx_data_len = max_deq;
206 quic_disconnect_transport (quic_ctx_t * ctx)
208 QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
209 vnet_disconnect_args_t a = {
210 .handle = ctx->udp_session_handle,
211 .app_index = quic_main.app_index,
214 if (vnet_disconnect_session (&a))
215 clib_warning ("UDP session 0x%lx disconnect errored",
216 ctx->udp_session_handle);
220 quic_connection_delete (quic_ctx_t * ctx)
222 clib_bihash_kv_16_8_t kv;
225 QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
227 ASSERT (!quic_ctx_is_stream (ctx));
228 quic_stop_ctx_timer (ctx);
230 /* Delete the connection from the connection map */
232 quic_make_connection_key (&kv, quicly_get_master_id (conn));
233 QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
235 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
237 quic_disconnect_transport (ctx);
240 quicly_free (ctx->conn);
242 session_transport_delete_notify (&ctx->connection);
246 quic_increment_counter (u8 evt, u8 val)
248 vlib_main_t *vm = vlib_get_main ();
249 vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
253 * Called when quicly return an error
254 * This function interacts tightly with quic_proto_on_close
257 quic_connection_closed (quic_ctx_t * ctx)
259 QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
262 /* TODO if connection is not established, just delete the session? */
263 /* Actually should send connect or accept error */
265 switch (ctx->conn_state)
267 case QUIC_CONN_STATE_READY:
268 /* Error on an opened connection (timeout...)
269 This puts the session in closing state, we should receive a notification
270 when the app has closed its session */
271 session_transport_reset_notify (&ctx->connection);
272 /* This ensures we delete the connection when the app confirms the close */
273 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
275 case QUIC_CONN_STATE_PASSIVE_CLOSING:
276 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
277 /* quic_proto_on_close will eventually be called when the app confirms the close
278 , we delete the connection at that point */
280 case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
281 /* App already confirmed close, we can delete the connection */
282 quic_connection_delete (ctx);
284 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
287 case QUIC_CONN_STATE_ACTIVE_CLOSING:
288 quic_connection_delete (ctx);
291 QUIC_DBG (0, "BUG %d", ctx->conn_state);
297 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
300 session_dgram_hdr_t hdr;
303 transport_connection_t *tc;
305 len = packet->data.len;
306 f = udp_session->tx_fifo;
307 tc = session_get_transport (udp_session);
308 max_enqueue = svm_fifo_max_enqueue (f);
309 if (max_enqueue < SESSION_CONN_HDR_LEN + len)
311 QUIC_DBG (1, "Too much data to send, max_enqueue %u, len %u",
312 max_enqueue, len + SESSION_CONN_HDR_LEN);
313 return QUIC_ERROR_FULL_FIFO;
316 /* Build packet header for fifo */
317 hdr.data_length = len;
319 hdr.is_ip4 = tc->is_ip4;
320 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
321 hdr.lcl_port = tc->lcl_port;
323 /* Read dest address from quicly-provided sockaddr */
326 ASSERT (packet->dest.sa.sa_family == AF_INET);
327 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
328 hdr.rmt_port = sa4->sin_port;
329 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
333 ASSERT (packet->dest.sa.sa_family == AF_INET6);
334 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
335 hdr.rmt_port = sa6->sin6_port;
336 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
339 ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
340 if (ret != sizeof (hdr))
342 QUIC_DBG (1, "Not enough space to enqueue header");
343 return QUIC_ERROR_FULL_FIFO;
345 ret = svm_fifo_enqueue (f, len, packet->data.base);
348 QUIC_DBG (1, "Not enough space to enqueue payload");
349 return QUIC_ERROR_FULL_FIFO;
352 quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
358 quic_send_packets (quic_ctx_t * ctx)
360 quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
361 session_t *udp_session;
363 size_t num_packets, i, max_packets;
364 quicly_packet_allocator_t *pa;
365 quicly_context_t *quicly_context;
368 /* We have sctx, get qctx */
369 if (quic_ctx_is_stream (ctx))
370 ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
372 ASSERT (!quic_ctx_is_stream (ctx));
374 udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
383 /* TODO : quicly can assert it can send min_packets up to 2 */
384 if (quic_sendable_packet_count (udp_session) < 2)
387 quicly_context = quic_get_quicly_ctx_from_ctx (ctx);
390 clib_warning ("Tried to send packets on non existing app worker %u",
391 ctx->parent_app_wrk_id);
392 quic_connection_delete (ctx);
395 pa = quicly_context->packet_allocator;
398 max_packets = quic_sendable_packet_count (udp_session);
401 num_packets = max_packets;
402 if ((err = quicly_send (conn, packets, &num_packets)))
405 for (i = 0; i != num_packets; ++i)
407 if ((err = quic_send_datagram (udp_session, packets[i])))
410 pa->free_packet (pa, packets[i]);
413 while (num_packets > 0 && num_packets == max_packets);
416 quic_set_udp_tx_evt (udp_session);
418 QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
419 svm_fifo_max_dequeue (udp_session->rx_fifo));
420 quic_update_timer (ctx);
424 if (err && err != QUICLY_ERROR_PACKET_IGNORED
425 && err != QUICLY_ERROR_FREE_CONNECTION)
426 clib_warning ("Quic error '%U'.", quic_format_err, err);
427 quic_connection_closed (ctx);
431 /* Quicly callbacks */
434 quic_on_stream_destroy (quicly_stream_t * stream, int err)
436 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
437 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
438 stream_data->thread_index);
439 session_t *stream_session = session_get (sctx->c_s_index,
440 sctx->c_thread_index);
441 QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
442 session_handle (stream_session), quic_format_err, err);
444 stream_session->session_state = SESSION_STATE_CLOSED;
445 session_transport_delete_notify (&sctx->connection);
447 quic_ctx_free (sctx);
448 clib_mem_free (stream->data);
452 quic_on_stop_sending (quicly_stream_t * stream, int err)
455 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
456 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
457 stream_data->thread_index);
458 session_t *stream_session = session_get (sctx->c_s_index,
459 sctx->c_thread_index);
460 clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
461 session_handle (stream_session), quic_format_err, err);
463 /* TODO : handle STOP_SENDING */
468 quic_on_receive_reset (quicly_stream_t * stream, int err)
470 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
471 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
472 stream_data->thread_index);
474 session_t *stream_session = session_get (sctx->c_s_index,
475 sctx->c_thread_index);
476 clib_warning ("RESET_STREAM: session 0x%lx (%U)",
477 session_handle (stream_session), quic_format_err, err);
479 session_transport_closing_notify (&sctx->connection);
484 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
487 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
490 session_t *stream_session;
491 app_worker_t *app_wrk;
493 quic_stream_data_t *stream_data;
496 stream_data = (quic_stream_data_t *) stream->data;
497 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
498 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
499 f = stream_session->rx_fifo;
501 max_enq = svm_fifo_max_enqueue_prod (f);
502 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
503 /* Handle duplicate packet/chunk from quicly */
504 if (off < stream_data->app_rx_data_len)
506 QUIC_DBG (3, "Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
507 "DUPLICATE PACKET (max_enq %u, len %u, "
508 "app_rx_data_len %u, off %u, ToBeNQ %u)",
509 stream_session->session_index,
510 stream_session->app_wrk_index,
511 stream_session->thread_index, f,
512 max_enq, len, stream_data->app_rx_data_len, off,
513 off - stream_data->app_rx_data_len + len);
516 if (PREDICT_FALSE ((off - stream_data->app_rx_data_len + len) > max_enq))
518 QUIC_ERR ("Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
519 "RX FIFO IS FULL (max_enq %u, len %u, "
520 "app_rx_data_len %u, off %u, ToBeNQ %u)",
521 stream_session->session_index,
522 stream_session->app_wrk_index,
523 stream_session->thread_index, f,
524 max_enq, len, stream_data->app_rx_data_len, off,
525 off - stream_data->app_rx_data_len + len);
528 if (off == stream_data->app_rx_data_len)
530 /* Streams live on the same thread so (f, stream_data) should stay consistent */
531 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
532 QUIC_DBG (3, "Session [idx %u, app_wrk %u, ti %u, rx-fifo 0x%llx]: "
533 "Enqueuing %u (rlen %u) at off %u in %u space, ",
534 stream_session->session_index,
535 stream_session->app_wrk_index,
536 stream_session->thread_index, f, len, rlen, off, max_enq);
537 stream_data->app_rx_data_len += rlen;
538 ASSERT (rlen >= len);
539 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
540 if (PREDICT_TRUE (app_wrk != 0))
541 app_worker_lock_and_send_event (app_wrk, stream_session,
543 quic_ack_rx_data (stream_session);
547 rlen = svm_fifo_enqueue_with_offset (f,
548 off - stream_data->app_rx_data_len,
556 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
558 session_t *stream_session;
562 stream_session = get_stream_session_from_stream (stream);
563 f = stream_session->tx_fifo;
565 rv = svm_fifo_dequeue_drop (f, delta);
566 ASSERT (rv == delta);
567 quicly_stream_sync_sendbuf (stream, 0);
571 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
572 size_t * len, int *wrote_all)
574 session_t *stream_session;
576 u32 deq_max, first_deq, max_rd_chunk, rem_offset;
578 stream_session = get_stream_session_from_stream (stream);
579 f = stream_session->tx_fifo;
581 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
583 deq_max = svm_fifo_max_dequeue_cons (f);
584 ASSERT (off <= deq_max);
585 if (off + *len < deq_max)
592 *len = deq_max - off;
593 QUIC_DBG (3, "Wrote ALL, %u", *len);
596 /* TODO, use something like : return svm_fifo_peek (f, off, *len, dst); */
597 max_rd_chunk = svm_fifo_max_read_chunk (f);
600 if (off < max_rd_chunk)
602 first_deq = clib_min (*len, max_rd_chunk - off);
603 clib_memcpy_fast (dst, svm_fifo_head (f) + off, first_deq);
606 if (max_rd_chunk < off + *len)
608 rem_offset = max_rd_chunk < off ? off - max_rd_chunk : 0;
609 clib_memcpy_fast (dst + first_deq, f->head_chunk->data + rem_offset,
616 static const quicly_stream_callbacks_t quic_stream_callbacks = {
617 .on_destroy = quic_on_stream_destroy,
618 .on_send_shift = quic_fifo_egress_shift,
619 .on_send_emit = quic_fifo_egress_emit,
620 .on_send_stop = quic_on_stop_sending,
621 .on_receive = quic_on_receive,
622 .on_receive_reset = quic_on_receive_reset
626 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
628 session_t *stream_session, *quic_session;
629 quic_stream_data_t *stream_data;
630 app_worker_t *app_wrk;
631 quic_ctx_t *qctx, *sctx;
635 QUIC_DBG (2, "on_stream_open called");
636 stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
637 stream->callbacks = &quic_stream_callbacks;
638 /* Notify accept on parent qsession, but only if this is not a locally
639 * initiated stream */
640 if (quicly_stream_is_self_initiated (stream))
643 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
644 qctx = quic_get_conn_ctx (stream->conn);
646 /* Might need to signal that the connection is ready if the first thing the
647 * server does is open a stream */
648 quic_check_quic_session_connected (qctx);
649 /* ctx might be invalidated */
650 qctx = quic_get_conn_ctx (stream->conn);
652 stream_session = session_alloc (qctx->c_thread_index);
653 QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
654 session_handle (stream_session), sctx_id);
655 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
656 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
657 sctx->parent_app_id = qctx->parent_app_id;
658 sctx->quic_connection_ctx_id = qctx->c_c_index;
659 sctx->c_c_index = sctx_id;
660 sctx->c_s_index = stream_session->session_index;
661 sctx->stream = stream;
662 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
663 sctx->flags |= QUIC_F_IS_STREAM;
665 stream_data = (quic_stream_data_t *) stream->data;
666 stream_data->ctx_id = sctx_id;
667 stream_data->thread_index = sctx->c_thread_index;
668 stream_data->app_rx_data_len = 0;
670 sctx->c_s_index = stream_session->session_index;
671 stream_session->session_state = SESSION_STATE_CREATED;
672 stream_session->app_wrk_index = sctx->parent_app_wrk_id;
673 stream_session->connection_index = sctx->c_c_index;
674 stream_session->session_type =
675 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
676 quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
677 stream_session->listener_handle = listen_session_get_handle (quic_session);
679 app_wrk = app_worker_get (stream_session->app_wrk_index);
680 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
682 QUIC_DBG (1, "failed to allocate fifos");
683 session_free (stream_session);
684 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
685 return 0; /* Frame is still valid */
687 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
688 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
689 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
691 if ((rv = app_worker_accept_notify (app_wrk, stream_session)))
693 QUIC_DBG (1, "failed to notify accept worker app");
694 session_free_w_fifos (stream_session);
695 quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
696 return 0; /* Frame is still valid */
703 quic_on_closed_by_peer (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
704 int code, uint64_t frame_type,
705 const char *reason, size_t reason_len)
707 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
709 session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
710 clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
711 session_handle (quic_session), quic_format_err, code,
714 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
715 session_transport_closing_notify (&ctx->connection);
718 static quicly_stream_open_t on_stream_open = { quic_on_stream_open };
719 static quicly_closed_by_peer_t on_closed_by_peer = { quic_on_closed_by_peer };
724 quic_get_thread_time (u8 thread_index)
726 return quic_main.wrk_ctx[thread_index].time_now;
730 quic_get_time (quicly_now_t * self)
732 u8 thread_index = vlib_get_thread_index ();
733 return quic_get_thread_time (thread_index);
736 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
739 quic_set_time_now (u32 thread_index)
741 vlib_main_t *vlib_main = vlib_get_main ();
742 f64 time = vlib_time_now (vlib_main);
743 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
744 return quic_main.wrk_ctx[thread_index].time_now;
747 /* Transport proto callback */
749 quic_update_time (f64 now, u8 thread_index)
751 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
753 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
754 quic_set_time_now (thread_index);
755 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
759 quic_timer_expired (u32 conn_index)
762 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
763 quic_get_time (NULL));
764 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
765 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
766 quic_send_packets (ctx);
770 quic_update_timer (quic_ctx_t * ctx)
772 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
773 int64_t next_timeout, next_interval;
774 session_t *quic_session;
776 /* This timeout is in ms which is the unit of our timer */
777 next_timeout = quicly_get_first_timeout (ctx->conn);
778 next_interval = next_timeout - quic_get_time (NULL);
780 if (next_timeout == 0 || next_interval <= 0)
782 if (ctx->c_s_index == QUIC_SESSION_INVALID)
788 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
789 if (svm_fifo_set_event (quic_session->tx_fifo))
790 session_send_io_evt_to_thread_custom (quic_session,
791 quic_session->thread_index,
792 SESSION_IO_EVT_BUILTIN_TX);
797 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
799 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
800 next_interval, ctx->c_c_index);
802 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
804 if (next_timeout == INT64_MAX)
806 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
809 ctx->timer_handle = tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index,
814 if (next_timeout == INT64_MAX)
816 quic_stop_ctx_timer (ctx);
819 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
826 quic_expired_timers_dispatch (u32 * expired_timers)
830 for (i = 0; i < vec_len (expired_timers); i++)
832 quic_timer_expired (expired_timers[i]);
837 quic_encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
838 int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
840 quic_session_cache_t *self = (void *) _self;
846 /* replace the cached entry along with a newly generated session id */
847 clib_mem_free (self->data.base);
848 if ((self->data.base = clib_mem_alloc (src.len)) == NULL)
849 return PTLS_ERROR_NO_MEMORY;
851 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
852 clib_memcpy (self->data.base, src.base, src.len);
853 self->data.len = src.len;
855 /* store the session id in buffer */
856 if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
858 clib_memcpy (dst->base + dst->off, self->id, sizeof (self->id));
859 dst->off += sizeof (self->id);
865 /* check if session id is the one stored in cache */
866 if (src.len != sizeof (self->id))
867 return PTLS_ERROR_SESSION_NOT_FOUND;
868 if (clib_memcmp (self->id, src.base, sizeof (self->id)) != 0)
869 return PTLS_ERROR_SESSION_NOT_FOUND;
871 /* return the cached value */
872 if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
874 clib_memcpy (dst->base + dst->off, self->data.base, self->data.len);
875 dst->off += self->data.len;
882 quic_store_quicly_ctx (application_t * app, u32 ckpair_index,
885 quic_main_t *qm = &quic_main;
886 quicly_context_t *quicly_ctx;
887 ptls_iovec_t key_vec;
888 app_cert_key_pair_t *ckpair;
893 if (crypto_engine == CRYPTO_ENGINE_NONE)
895 QUIC_DBG (2, "No crypto engine specified, using %d", crypto_engine);
896 crypto_engine = qm->default_crypto_engine;
898 if (!clib_bitmap_get (qm->available_crypto_engines, crypto_engine))
900 QUIC_DBG (1, "Quic does not support crypto engine %d", crypto_engine);
901 return VNET_API_ERROR_MISSING_CERT_KEY;
904 quicly_ctx_data_t *quicly_ctx_data =
905 clib_mem_alloc (sizeof (quicly_ctx_data_t));
906 clib_memset (quicly_ctx_data, 0, sizeof (*quicly_ctx_data)); /* picotls depends on this */
907 quicly_ctx = &quicly_ctx_data->quicly_ctx;
908 ptls_context_t *ptls_ctx = &quicly_ctx_data->ptls_ctx;
909 ptls_ctx->random_bytes = ptls_openssl_random_bytes;
910 ptls_ctx->get_time = &ptls_get_time;
911 ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
912 ptls_ctx->cipher_suites = qm->quic_ciphers[crypto_engine];
913 ptls_ctx->certificates.list = NULL;
914 ptls_ctx->certificates.count = 0;
915 ptls_ctx->esni = NULL;
916 ptls_ctx->on_client_hello = NULL;
917 ptls_ctx->emit_certificate = NULL;
918 ptls_ctx->sign_certificate = NULL;
919 ptls_ctx->verify_certificate = NULL;
920 ptls_ctx->ticket_lifetime = 86400;
921 ptls_ctx->max_early_data_size = 8192;
922 ptls_ctx->hkdf_label_prefix__obsolete = NULL;
923 ptls_ctx->require_dhe_on_psk = 1;
924 ptls_ctx->encrypt_ticket = &qm->session_cache.super;
926 app->quicly_ctx = (u64 *) quicly_ctx;
927 clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
929 quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
930 quicly_ctx->tls = ptls_ctx;
931 quicly_ctx->stream_open = &on_stream_open;
932 quicly_ctx->closed_by_peer = &on_closed_by_peer;
933 quicly_ctx->now = &quicly_vpp_now_cb;
934 quicly_amend_ptls_context (quicly_ctx->tls);
936 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
937 quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
938 quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
940 /* max_enq is FIFO_SIZE - 1 */
941 max_enq = app->sm_properties.rx_fifo_size - 1;
942 quicly_ctx->transport_params.max_stream_data.bidi_local = max_enq;
943 max_enq = app->sm_properties.tx_fifo_size - 1;
944 quicly_ctx->transport_params.max_stream_data.bidi_remote = max_enq;
945 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
947 quicly_ctx->tls->random_bytes (quicly_ctx_data->cid_key, 16);
948 quicly_ctx_data->cid_key[16] = 0;
949 key_vec = ptls_iovec_init (quicly_ctx_data->cid_key,
950 strlen (quicly_ctx_data->cid_key));
951 quicly_ctx->cid_encryptor =
952 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
953 &ptls_openssl_aes128ecb,
954 &ptls_openssl_sha256, key_vec);
956 ckpair = app_cert_key_pair_get_if_valid (ckpair_index);
957 if (!ckpair || !ckpair->key || !ckpair->cert)
959 QUIC_DBG (1, "Wrong ckpair id %d\n", ckpair_index);
962 if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
964 QUIC_DBG (1, "failed to read private key from app configuration\n");
967 if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
969 QUIC_DBG (1, "failed to load certificate\n");
975 clib_mem_free (quicly_ctx_data);
976 return VNET_API_ERROR_MISSING_CERT_KEY;
979 /* Transport proto functions */
982 quic_connect_stream (session_t * quic_session, u32 opaque)
984 uint64_t quic_session_handle;
985 session_t *stream_session;
986 quic_stream_data_t *stream_data;
987 quicly_stream_t *stream;
989 app_worker_t *app_wrk;
990 quic_ctx_t *qctx, *sctx;
994 /* Find base session to which the user want to attach a stream */
995 quic_session_handle = session_handle (quic_session);
996 QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
998 if (session_type_transport_proto (quic_session->session_type) !=
999 TRANSPORT_PROTO_QUIC)
1001 QUIC_DBG (1, "received incompatible session");
1005 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
1008 QUIC_DBG (1, "Invalid app worker :(");
1012 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
1013 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
1014 qctx = quic_ctx_get (quic_session->connection_index,
1015 quic_session->thread_index);
1016 if (quic_ctx_is_stream (qctx))
1018 QUIC_DBG (1, "session is a stream");
1019 quic_ctx_free (sctx);
1023 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
1024 sctx->parent_app_id = qctx->parent_app_id;
1025 sctx->quic_connection_ctx_id = qctx->c_c_index;
1026 sctx->c_c_index = sctx_index;
1027 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1028 sctx->flags |= QUIC_F_IS_STREAM;
1032 if (!conn || !quicly_connection_is_ready (conn))
1035 if ((rv = quicly_open_stream (conn, &stream, 0 /* uni */ )))
1037 QUIC_DBG (2, "Stream open failed with %d", rv);
1040 sctx->stream = stream;
1042 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1044 stream_session = session_alloc (qctx->c_thread_index);
1045 QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
1046 session_handle (stream_session), sctx_index);
1047 stream_session->app_wrk_index = app_wrk->wrk_index;
1048 stream_session->connection_index = sctx_index;
1049 stream_session->listener_handle = quic_session_handle;
1050 stream_session->session_type =
1051 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1053 sctx->c_s_index = stream_session->session_index;
1055 if (app_worker_init_connected (app_wrk, stream_session))
1057 QUIC_DBG (1, "failed to app_worker_init_connected");
1058 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
1059 session_free_w_fifos (stream_session);
1060 quic_ctx_free (sctx);
1061 return app_worker_connect_notify (app_wrk, NULL, opaque);
1064 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1065 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1066 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1068 stream_session->session_state = SESSION_STATE_READY;
1069 if (app_worker_connect_notify (app_wrk, stream_session, opaque))
1071 QUIC_DBG (1, "failed to notify app");
1072 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1073 session_free_w_fifos (stream_session);
1074 quic_ctx_free (sctx);
1077 stream_data = (quic_stream_data_t *) stream->data;
1078 stream_data->ctx_id = sctx->c_c_index;
1079 stream_data->thread_index = sctx->c_thread_index;
1080 stream_data->app_rx_data_len = 0;
1085 quic_connect_connection (session_endpoint_cfg_t * sep)
1087 vnet_connect_args_t _cargs, *cargs = &_cargs;
1088 quic_main_t *qm = &quic_main;
1090 app_worker_t *app_wrk;
1095 clib_memset (cargs, 0, sizeof (*cargs));
1096 ctx_index = quic_ctx_alloc (vlib_get_thread_index ());
1097 ctx = quic_ctx_get (ctx_index, vlib_get_thread_index ());
1098 ctx->parent_app_wrk_id = sep->app_wrk_index;
1099 ctx->c_s_index = QUIC_SESSION_INVALID;
1100 ctx->c_c_index = ctx_index;
1101 ctx->udp_is_ip4 = sep->is_ip4;
1102 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1103 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1104 ctx->client_opaque = sep->opaque;
1105 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1107 ctx->srv_hostname = format (0, "%v", sep->hostname);
1109 /* needed by quic for crypto + determining client / server */
1110 ctx->srv_hostname = format (0, "%U", format_ip46_address,
1111 &sep->ip, sep->is_ip4);
1112 vec_terminate_c_string (ctx->srv_hostname);
1114 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1115 cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1116 cargs->app_index = qm->app_index;
1117 cargs->api_context = ctx_index;
1119 app_wrk = app_worker_get (sep->app_wrk_index);
1120 app = application_get (app_wrk->app_index);
1121 ctx->parent_app_id = app_wrk->app_index;
1122 cargs->sep_ext.ns_index = app->ns_index;
1125 quic_store_quicly_ctx (app, sep->ckpair_index, sep->crypto_engine)))
1127 /* Also store it in ctx for convenience
1128 * Waiting for crypto_ctx logic */
1129 ctx->quicly_ctx = (quicly_context_t *) app->quicly_ctx;
1131 if ((error = vnet_connect (cargs)))
1138 quic_connect (transport_endpoint_cfg_t * tep)
1140 QUIC_DBG (2, "Called quic_connect");
1141 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1142 session_t *quic_session;
1143 sep = (session_endpoint_cfg_t *) tep;
1145 quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1147 return quic_connect_stream (quic_session, sep->opaque);
1149 return quic_connect_connection (sep);
1153 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1155 quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1159 session_t *stream_session = session_get (ctx->c_s_index,
1160 ctx->c_thread_index);
1161 clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1163 if (quic_ctx_is_stream (ctx))
1165 quicly_stream_t *stream = ctx->stream;
1166 quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1167 quic_send_packets (ctx);
1171 switch (ctx->conn_state)
1173 case QUIC_CONN_STATE_READY:
1174 ctx->conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1175 quicly_conn_t *conn = ctx->conn;
1176 /* Start connection closing. Keep sending packets until quicly_send
1177 returns QUICLY_ERROR_FREE_CONNECTION */
1178 quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1179 /* This also causes all streams to be closed (and the cb called) */
1180 quic_send_packets (ctx);
1182 case QUIC_CONN_STATE_PASSIVE_CLOSING:
1183 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1184 /* send_packets will eventually return an error, we delete the conn at
1187 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1188 quic_connection_delete (ctx);
1191 QUIC_DBG (0, "BUG");
1197 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1199 vnet_listen_args_t _bargs, *args = &_bargs;
1200 quic_main_t *qm = &quic_main;
1201 session_handle_t udp_handle;
1202 session_endpoint_cfg_t *sep;
1203 session_t *udp_listen_session;
1204 app_worker_t *app_wrk;
1208 app_listener_t *app_listener;
1210 sep = (session_endpoint_cfg_t *) tep;
1211 app_wrk = app_worker_get (sep->app_wrk_index);
1212 /* We need to call this because we call app_worker_init_connected in
1213 * quic_accept_stream, which assumes the connect segment manager exists */
1214 app_worker_alloc_connects_segment_manager (app_wrk);
1215 app = application_get (app_wrk->app_index);
1216 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1218 if (quic_store_quicly_ctx (app, sep->ckpair_index, sep->crypto_engine))
1221 sep->transport_proto = TRANSPORT_PROTO_UDPC;
1222 clib_memset (args, 0, sizeof (*args));
1223 args->app_index = qm->app_index;
1224 args->sep_ext = *sep;
1225 args->sep_ext.ns_index = app->ns_index;
1226 if (vnet_listen (args))
1229 lctx_index = quic_ctx_alloc (0);
1230 udp_handle = args->handle;
1231 app_listener = app_listener_get_w_handle (udp_handle);
1232 udp_listen_session = app_listener_get_session (app_listener);
1233 udp_listen_session->opaque = lctx_index;
1235 lctx = quic_ctx_get (lctx_index, 0);
1236 lctx->flags |= QUIC_F_IS_LISTENER;
1237 /* Also store it in ctx for convenience
1238 * Waiting for crypto_ctx logic */
1239 lctx->quicly_ctx = (quicly_context_t *) app->quicly_ctx;
1241 clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1242 clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1243 lctx->c_rmt_port = args->sep.peer.port;
1244 lctx->c_lcl_port = args->sep.port;
1245 lctx->c_is_ip4 = args->sep.is_ip4;
1246 lctx->c_fib_index = args->sep.fib_index;
1247 lctx->c_proto = TRANSPORT_PROTO_QUIC;
1248 lctx->parent_app_wrk_id = sep->app_wrk_index;
1249 lctx->parent_app_id = app_wrk->app_index;
1250 lctx->udp_session_handle = udp_handle;
1251 lctx->c_s_index = quic_listen_session_index;
1253 QUIC_DBG (2, "Listening UDP session 0x%lx",
1254 session_handle (udp_listen_session));
1255 QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1260 quic_stop_listen (u32 lctx_index)
1262 QUIC_DBG (2, "Called quic_stop_listen");
1264 lctx = quic_ctx_get (lctx_index, 0);
1265 ASSERT (quic_ctx_is_listener (lctx));
1266 vnet_unlisten_args_t a = {
1267 .handle = lctx->udp_session_handle,
1268 .app_index = quic_main.app_index,
1269 .wrk_map_index = 0 /* default wrk */
1271 if (vnet_unlisten (&a))
1272 clib_warning ("unlisten errored");
1274 /* TODO: crypto state cleanup */
1276 quic_ctx_free (lctx);
1280 static transport_connection_t *
1281 quic_connection_get (u32 ctx_index, u32 thread_index)
1284 ctx = quic_ctx_get (ctx_index, thread_index);
1285 return &ctx->connection;
1288 static transport_connection_t *
1289 quic_listener_get (u32 listener_index)
1291 QUIC_DBG (2, "Called quic_listener_get");
1293 ctx = quic_ctx_get (listener_index, 0);
1294 return &ctx->connection;
1298 format_quic_ctx (u8 * s, va_list * args)
1300 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1301 u32 verbose = va_arg (*args, u32);
1306 str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1308 if (quic_ctx_is_listener (ctx))
1309 str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1310 else if (quic_ctx_is_stream (ctx))
1311 str = format (str, "Stream %ld conn %d",
1312 ctx->stream->stream_id, ctx->quic_connection_ctx_id);
1313 else /* connection */
1314 str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1315 ctx->udp_session_handle);
1317 str = format (str, " app %d wrk %d", ctx->parent_app_id,
1318 ctx->parent_app_wrk_id);
1321 s = format (s, "%-50s%-15d", str, ctx->conn_state);
1323 s = format (s, "%s\n", str);
1329 format_quic_connection (u8 * s, va_list * args)
1331 u32 qc_index = va_arg (*args, u32);
1332 u32 thread_index = va_arg (*args, u32);
1333 u32 verbose = va_arg (*args, u32);
1334 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1335 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1340 format_quic_half_open (u8 * s, va_list * args)
1342 u32 qc_index = va_arg (*args, u32);
1343 u32 thread_index = va_arg (*args, u32);
1344 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1345 s = format (s, "[#%d][Q] half-open app %u", thread_index,
1346 ctx->parent_app_id);
1352 format_quic_listener (u8 * s, va_list * args)
1354 u32 tci = va_arg (*args, u32);
1355 u32 thread_index = va_arg (*args, u32);
1356 u32 verbose = va_arg (*args, u32);
1357 quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1358 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1362 /* Session layer callbacks */
1365 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1366 ip46_address_t * addr, u16 port, u8 is_ip4)
1370 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1371 sa4->sin_family = AF_INET;
1372 sa4->sin_port = port;
1373 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1374 *salen = sizeof (struct sockaddr_in);
1378 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1379 sa6->sin6_family = AF_INET6;
1380 sa6->sin6_port = port;
1381 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1382 *salen = sizeof (struct sockaddr_in6);
1387 quic_on_quic_session_connected (quic_ctx_t * ctx)
1389 session_t *quic_session;
1390 app_worker_t *app_wrk;
1391 u32 ctx_id = ctx->c_c_index;
1392 u32 thread_index = ctx->c_thread_index;
1395 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1398 quic_disconnect_transport (ctx);
1402 quic_session = session_alloc (thread_index);
1404 QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1405 ctx->c_s_index = quic_session->session_index;
1406 quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1407 quic_session->connection_index = ctx->c_c_index;
1408 quic_session->listener_handle = SESSION_INVALID_HANDLE;
1409 quic_session->session_type =
1410 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1412 if (app_worker_init_connected (app_wrk, quic_session))
1414 QUIC_DBG (1, "failed to app_worker_init_connected");
1415 quic_proto_on_close (ctx_id, thread_index);
1416 return app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1419 quic_session->session_state = SESSION_STATE_CONNECTING;
1420 if ((rv = app_worker_connect_notify (app_wrk, quic_session,
1421 ctx->client_opaque)))
1423 QUIC_DBG (1, "failed to notify app %d", rv);
1424 quic_proto_on_close (ctx_id, thread_index);
1428 /* If the app opens a stream in its callback it may invalidate ctx */
1429 ctx = quic_ctx_get (ctx_id, thread_index);
1431 * app_worker_connect_notify() might have reallocated pool, reload
1432 * quic_session pointer
1434 quic_session = session_get (ctx->c_s_index, thread_index);
1435 quic_session->session_state = SESSION_STATE_LISTENING;
1441 quic_check_quic_session_connected (quic_ctx_t * ctx)
1443 /* Called when we need to trigger quic session connected
1444 * we may call this function on the server side / at
1447 /* Conn may be set to null if the connection is terminated */
1448 if (!ctx->conn || ctx->conn_state != QUIC_CONN_STATE_HANDSHAKE)
1450 if (!quicly_connection_is_ready (ctx->conn))
1452 ctx->conn_state = QUIC_CONN_STATE_READY;
1453 if (!quicly_is_client (ctx->conn))
1455 return quic_on_quic_session_connected (ctx);
1459 quic_receive_connection (void *arg)
1461 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1462 quic_ctx_t *temp_ctx, *new_ctx;
1463 clib_bihash_kv_16_8_t kv;
1464 quicly_conn_t *conn;
1465 session_t *udp_session;
1468 new_ctx_id = quic_ctx_alloc (thread_index);
1469 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1471 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1475 clib_memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1476 clib_mem_free (temp_ctx);
1478 new_ctx->c_thread_index = thread_index;
1479 new_ctx->c_c_index = new_ctx_id;
1481 conn = new_ctx->conn;
1482 quic_store_conn_ctx (conn, new_ctx);
1483 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1484 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1485 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1486 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1487 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1488 quic_update_timer (new_ctx);
1490 /* Trigger write on this connection if necessary */
1491 udp_session = session_get_from_handle (new_ctx->udp_session_handle);
1492 udp_session->opaque = new_ctx_id;
1493 udp_session->flags &= ~SESSION_F_IS_MIGRATING;
1494 if (svm_fifo_max_dequeue (udp_session->tx_fifo))
1495 quic_set_udp_tx_evt (udp_session);
1499 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1501 quic_ctx_t *ctx, *temp_ctx;
1502 u32 thread_index = vlib_get_thread_index ();
1504 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1506 temp_ctx = clib_mem_alloc (sizeof (quic_ctx_t));
1508 ctx = quic_ctx_get (ctx_index, thread_index);
1510 clib_memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1512 quic_stop_ctx_timer (ctx);
1513 quic_ctx_free (ctx);
1515 /* Send connection to destination thread */
1516 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1521 quic_udp_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1522 session_t * udp_session, u8 is_fail)
1524 QUIC_DBG (2, "QSession is now connected (id %u)",
1525 udp_session->session_index);
1526 /* This should always be called before quic_connect returns since UDP always
1527 * connects instantly. */
1528 clib_bihash_kv_16_8_t kv;
1529 struct sockaddr_in6 sa6;
1530 struct sockaddr *sa = (struct sockaddr *) &sa6;
1532 transport_connection_t *tc;
1533 app_worker_t *app_wrk;
1534 quicly_conn_t *conn;
1536 u32 thread_index = vlib_get_thread_index ();
1538 quicly_context_t *quicly_ctx;
1541 ctx = quic_ctx_get (ctx_index, thread_index);
1545 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1548 api_context = ctx->c_s_index;
1549 app_worker_connect_notify (app_wrk, 0, api_context);
1554 ctx->c_thread_index = thread_index;
1555 ctx->c_c_index = ctx_index;
1557 QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1558 is_fail, thread_index, (ctx) ? ctx_index : ~0);
1560 ctx->udp_session_handle = session_handle (udp_session);
1561 udp_session->opaque = ctx_index;
1563 /* Init QUIC lib connection
1564 * Generate required sockaddr & salen */
1565 tc = session_get_transport (udp_session);
1566 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1568 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1569 ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
1570 sa, NULL, &quic_main.next_cid, ptls_iovec_init (NULL,
1572 &quic_main.hs_properties, NULL);
1573 ++quic_main.next_cid.master_id;
1574 /* Save context handle in quicly connection */
1575 quic_store_conn_ctx (ctx->conn, ctx);
1578 /* Register connection in connections map */
1580 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1581 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1582 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1583 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1585 /* UDP stack quirk? preemptively transfer connection if that happens */
1586 if (udp_session->thread_index != thread_index)
1587 quic_transfer_connection (ctx_index, udp_session->thread_index);
1589 quic_send_packets (ctx);
1595 quic_udp_session_disconnect_callback (session_t * s)
1597 clib_warning ("UDP session disconnected???");
1601 quic_udp_session_cleanup_callback (session_t * udp_session,
1602 session_cleanup_ntf_t ntf)
1606 if (ntf != SESSION_CLEANUP_SESSION)
1609 ctx = quic_ctx_get (udp_session->opaque, udp_session->thread_index);
1610 quic_stop_ctx_timer (ctx);
1611 quic_ctx_free (ctx);
1615 quic_udp_session_reset_callback (session_t * s)
1617 clib_warning ("UDP session reset???");
1621 quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
1623 u32 new_thread = session_thread_from_handle (new_sh);
1626 QUIC_DBG (1, "Session %x migrated to %lx", s->session_index, new_sh);
1627 ASSERT (vlib_get_thread_index () == s->thread_index);
1628 ctx = quic_ctx_get (s->opaque, s->thread_index);
1629 ASSERT (ctx->udp_session_handle == session_handle (s));
1631 ctx->udp_session_handle = new_sh;
1633 s->opaque = 0xfeedface;
1635 quic_transfer_connection (ctx->c_c_index, new_thread);
1639 quic_udp_session_accepted_callback (session_t * udp_session)
1641 /* New UDP connection, try to accept it */
1644 quic_ctx_t *ctx, *lctx;
1645 session_t *udp_listen_session;
1646 u32 thread_index = vlib_get_thread_index ();
1648 udp_listen_session =
1649 listen_session_get_from_handle (udp_session->listener_handle);
1651 ctx_index = quic_ctx_alloc (thread_index);
1652 ctx = quic_ctx_get (ctx_index, thread_index);
1653 ctx->c_thread_index = udp_session->thread_index;
1654 ctx->c_c_index = ctx_index;
1655 ctx->c_s_index = QUIC_SESSION_INVALID;
1656 ctx->udp_session_handle = session_handle (udp_session);
1657 QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
1658 ctx->listener_ctx_id = udp_listen_session->opaque;
1659 lctx = quic_ctx_get (udp_listen_session->opaque,
1660 udp_listen_session->thread_index);
1661 ctx->udp_is_ip4 = lctx->c_is_ip4;
1662 ctx->parent_app_id = lctx->parent_app_id;
1663 ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
1664 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1665 ctx->conn_state = QUIC_CONN_STATE_OPENED;
1666 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1668 /* Also store it in ctx for convenience
1669 * Waiting for crypto_ctx logic */
1670 ctx->quicly_ctx = lctx->quicly_ctx;
1672 udp_session->opaque = ctx_index;
1674 /* Put this ctx in the "opening" pool */
1675 pool_get (quic_main.wrk_ctx[ctx->c_thread_index].opening_ctx_pool,
1677 *pool_index = ctx_index;
1679 /* TODO timeout to delete these if they never connect */
1684 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1686 /* No-op for builtin */
1691 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1693 /* No-op for builtin */
1698 quic_custom_app_rx_callback (transport_connection_t * tc)
1701 session_t *stream_session = session_get (tc->s_index, tc->thread_index);
1702 QUIC_DBG (3, "Received app READ notification");
1703 quic_ack_rx_data (stream_session);
1704 svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
1706 /* Need to send packets (acks may never be sent otherwise) */
1707 ctx = quic_ctx_get (stream_session->connection_index,
1708 stream_session->thread_index);
1709 quic_send_packets (ctx);
1714 quic_custom_tx_callback (void *s, u32 max_burst_size)
1716 session_t *stream_session = (session_t *) s;
1717 quicly_stream_t *stream;
1722 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1724 ctx = quic_ctx_get (stream_session->connection_index,
1725 stream_session->thread_index);
1726 if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
1728 goto tx_end; /* Most probably a reschedule */
1731 QUIC_DBG (3, "Stream TX event");
1732 quic_ack_rx_data (stream_session);
1733 if (!svm_fifo_max_dequeue (stream_session->tx_fifo))
1736 stream = ctx->stream;
1737 if (!quicly_sendstate_is_open (&stream->sendstate))
1739 QUIC_DBG (1, "Warning: tried to send on closed stream");
1743 if ((rv = quicly_stream_sync_sendbuf (stream, 1)) != 0)
1747 quic_send_packets (ctx);
1753 * Returns 0 if a matching connection is found and is on the right thread.
1754 * Otherwise returns -1.
1755 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
1759 quic_find_packet_ctx (u32 * ctx_thread, u32 * ctx_index,
1760 struct sockaddr *sa, socklen_t salen,
1761 quicly_decoded_packet_t * packet,
1762 u32 caller_thread_index)
1765 quicly_conn_t *conn_;
1766 clib_bihash_kv_16_8_t kv;
1767 clib_bihash_16_8_t *h;
1769 h = &quic_main.connection_hash;
1770 quic_make_connection_key (&kv, &packet->cid.dest.plaintext);
1771 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
1773 if (clib_bihash_search_16_8 (h, &kv, &kv) == 0)
1775 u32 index = kv.value & UINT32_MAX;
1776 u32 thread_id = kv.value >> 32;
1777 /* Check if this connection belongs to this thread, otherwise
1778 * ask for it to be moved */
1779 if (thread_id != caller_thread_index)
1781 QUIC_DBG (2, "Connection is on wrong thread");
1782 /* Cannot make full check with quicly_is_destination... */
1784 *ctx_thread = thread_id;
1787 ctx_ = quic_ctx_get (index, vlib_get_thread_index ());
1789 if (conn_ && quicly_is_destination (conn_, NULL, sa, packet))
1791 QUIC_DBG (3, "Connection found");
1793 *ctx_thread = thread_id;
1797 QUIC_DBG (3, "connection not found");
1802 quic_accept_connection (u32 ctx_index, struct sockaddr *sa,
1803 socklen_t salen, quicly_decoded_packet_t packet)
1805 u32 thread_index = vlib_get_thread_index ();
1806 quicly_context_t *quicly_ctx;
1807 session_t *quic_session;
1808 clib_bihash_kv_16_8_t kv;
1809 app_worker_t *app_wrk;
1810 quicly_conn_t *conn;
1815 /* new connection, accept and create context if packet is valid
1816 * TODO: check if socket is actually listening? */
1817 ctx = quic_ctx_get (ctx_index, thread_index);
1818 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1819 if ((rv = quicly_accept (&conn, quicly_ctx, NULL, sa,
1820 &packet, NULL, &quic_main.next_cid, NULL)))
1822 /* Invalid packet, pass */
1823 assert (conn == NULL);
1824 QUIC_DBG (1, "Accept failed with %d", rv);
1825 /* TODO: cleanup created quic ctx and UDP session */
1828 assert (conn != NULL);
1830 ++quic_main.next_cid.master_id;
1831 /* Save ctx handle in quicly connection */
1832 quic_store_conn_ctx (conn, ctx);
1834 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1836 quic_session = session_alloc (ctx->c_thread_index);
1837 QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
1838 session_handle (quic_session), ctx->c_c_index);
1839 quic_session->session_state = SESSION_STATE_LISTENING;
1840 ctx->c_s_index = quic_session->session_index;
1842 lctx = quic_ctx_get (ctx->listener_ctx_id, 0);
1844 quic_session->app_wrk_index = lctx->parent_app_wrk_id;
1845 quic_session->connection_index = ctx->c_c_index;
1846 quic_session->session_type =
1847 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1848 quic_session->listener_handle = lctx->c_s_index;
1850 /* TODO: don't alloc fifos when we don't transfer data on this session
1851 * but we still need fifos for the events? */
1852 if ((rv = app_worker_init_accepted (quic_session)))
1854 QUIC_DBG (1, "failed to allocate fifos");
1855 session_free (quic_session);
1858 app_wrk = app_worker_get (quic_session->app_wrk_index);
1859 if ((rv = app_worker_accept_notify (app_wrk, quic_session)))
1861 QUIC_DBG (1, "failed to notify accept worker app");
1865 /* Register connection in connections map */
1866 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1867 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1868 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1869 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1871 return quic_send_packets (ctx);
1875 quic_reset_connection (u64 udp_session_handle,
1876 struct sockaddr *sa, socklen_t salen,
1877 quicly_decoded_packet_t packet)
1879 /* short header packet; potentially a dead connection. No need to check the
1880 * length of the incoming packet, because loop is prevented by authenticating
1881 * the CID (by checking node_id and thread_id). If the peer is also sending a
1882 * reset, then the next CID is highly likely to contain a non-authenticating
1884 QUIC_DBG (2, "Sending stateless reset");
1886 quicly_datagram_t *dgram;
1887 session_t *udp_session;
1888 quicly_context_t *quicly_ctx;
1889 if (packet.cid.dest.plaintext.node_id != 0
1890 || packet.cid.dest.plaintext.thread_id != 0)
1892 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
1893 dgram = quicly_send_stateless_reset (quicly_ctx, sa, NULL,
1894 &packet.cid.dest.plaintext);
1897 udp_session = session_get_from_handle (udp_session_handle);
1898 rv = quic_send_datagram (udp_session, dgram);
1899 quic_set_udp_tx_evt (udp_session);
1904 quic_process_one_rx_packet (u64 udp_session_handle, svm_fifo_t * f,
1905 u32 * fifo_offset, u32 * max_packet, u32 packet_n,
1906 quic_rx_packet_ctx_t * packet_ctx)
1908 session_dgram_hdr_t ph;
1909 quic_ctx_t *ctx = NULL;
1911 struct sockaddr_in6 sa6;
1912 struct sockaddr *sa = (struct sockaddr *) &sa6;
1916 packet_ctx->thread_index = UINT32_MAX;
1917 packet_ctx->ctx_index = UINT32_MAX;
1918 u32 thread_index = vlib_get_thread_index ();
1919 u32 *opening_ctx_pool, *ctx_index_ptr;
1920 u32 cur_deq = svm_fifo_max_dequeue (f) - *fifo_offset;
1921 quicly_context_t *quicly_ctx;
1925 *max_packet = packet_n + 1;
1929 if (cur_deq < SESSION_CONN_HDR_LEN)
1931 QUIC_ERR ("Not enough data for even a header in RX");
1934 ret = svm_fifo_peek (f, *fifo_offset, SESSION_CONN_HDR_LEN, (u8 *) & ph);
1935 if (ret != SESSION_CONN_HDR_LEN)
1937 QUIC_ERR ("Not enough data for header in RX");
1940 ASSERT (ph.data_offset == 0);
1941 full_len = ph.data_length + SESSION_CONN_HDR_LEN;
1942 if (full_len > cur_deq)
1944 QUIC_ERR ("Not enough data in fifo RX");
1948 /* Quicly can read len bytes from the fifo at offset:
1949 * ph.data_offset + SESSION_CONN_HDR_LEN */
1950 ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN + *fifo_offset,
1951 ph.data_length, packet_ctx->data);
1952 if (ret != ph.data_length)
1954 QUIC_ERR ("Not enough data peeked in RX");
1958 quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
1960 quic_build_sockaddr (sa, &salen, &ph.rmt_ip, ph.rmt_port, ph.is_ip4);
1961 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
1962 plen = quicly_decode_packet (quicly_ctx, &packet_ctx->packet,
1963 packet_ctx->data, ph.data_length);
1965 if (plen == SIZE_MAX)
1967 *fifo_offset += SESSION_CONN_HDR_LEN + ph.data_length;
1971 err = quic_find_packet_ctx (&packet_ctx->thread_index,
1972 &packet_ctx->ctx_index, sa, salen,
1973 &packet_ctx->packet, thread_index);
1976 ctx = quic_ctx_get (packet_ctx->ctx_index, thread_index);
1977 rv = quicly_receive (ctx->conn, NULL, sa, &packet_ctx->packet);
1979 QUIC_ERR ("quicly_receive return error %d", rv);
1981 else if (packet_ctx->ctx_index != UINT32_MAX)
1983 /* Connection found but on wrong thread, ask move */
1984 *max_packet = packet_n + 1;
1987 else if ((packet_ctx->packet.octets.base[0] & QUICLY_PACKET_TYPE_BITMASK) ==
1988 QUICLY_PACKET_TYPE_INITIAL)
1990 /* Try to find matching "opening" ctx */
1991 opening_ctx_pool = quic_main.wrk_ctx[thread_index].opening_ctx_pool;
1994 pool_foreach (ctx_index_ptr, opening_ctx_pool,
1996 ctx = quic_ctx_get (*ctx_index_ptr, thread_index);
1997 if (ctx->udp_session_handle == udp_session_handle)
1999 /* Right ctx found, create conn & remove from pool */
2000 quic_accept_connection (*ctx_index_ptr, sa, salen, packet_ctx->packet);
2001 *max_packet = packet_n + 1;
2002 packet_ctx->thread_index = thread_index;
2003 packet_ctx->ctx_index = *ctx_index_ptr;
2004 pool_put (opening_ctx_pool, ctx_index_ptr);
2009 QUIC_ERR ("Opening ctx not found!");;
2013 quic_reset_connection (udp_session_handle, sa, salen,
2014 packet_ctx->packet);
2018 *fifo_offset += SESSION_CONN_HDR_LEN + ph.data_length;
2023 quic_udp_session_rx_callback (session_t * udp_session)
2025 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
2026 quic_ctx_t *ctx = NULL;
2029 u64 udp_session_handle = session_handle (udp_session);
2031 u32 thread_index = vlib_get_thread_index ();
2032 quic_rx_packet_ctx_t packets_ctx[16];
2033 u32 i, fifo_offset, max_packets;
2035 if (udp_session->flags & SESSION_F_IS_MIGRATING)
2037 QUIC_DBG (3, "RX on migrating udp session");
2043 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
2044 f = udp_session->rx_fifo;
2045 max_deq = svm_fifo_max_dequeue (f);
2051 for (i = 0; i < max_packets; i++)
2052 quic_process_one_rx_packet (udp_session_handle, f, &fifo_offset,
2053 &max_packets, i, &packets_ctx[i]);
2055 for (i = 0; i < max_packets; i++)
2057 if (packets_ctx[i].thread_index != thread_index)
2059 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2060 packets_ctx[i].thread_index);
2061 quic_check_quic_session_connected (ctx);
2062 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2063 packets_ctx[i].thread_index);
2064 quic_send_packets (ctx);
2066 svm_fifo_dequeue_drop (f, fifo_offset);
2072 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2073 transport_endpoint_t * tep, u8 is_lcl)
2075 session_t *udp_session;
2076 if (!quic_ctx_is_stream (ctx))
2078 udp_session = session_get_from_handle (ctx->udp_session_handle);
2079 session_get_endpoint (udp_session, tep, is_lcl);
2084 quic_get_transport_listener_endpoint (u32 listener_index,
2085 transport_endpoint_t * tep, u8 is_lcl)
2088 app_listener_t *app_listener;
2089 session_t *udp_listen_session;
2090 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2091 if (quic_ctx_is_listener (ctx))
2093 app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2094 udp_listen_session = app_listener_get_session (app_listener);
2095 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2097 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2101 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2102 transport_endpoint_t * tep, u8 is_lcl)
2105 ctx = quic_ctx_get (ctx_index, thread_index);
2106 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2110 static session_cb_vft_t quic_app_cb_vft = {
2111 .session_accept_callback = quic_udp_session_accepted_callback,
2112 .session_disconnect_callback = quic_udp_session_disconnect_callback,
2113 .session_connected_callback = quic_udp_session_connected_callback,
2114 .session_reset_callback = quic_udp_session_reset_callback,
2115 .session_migrate_callback = quic_udp_session_migrate_callback,
2116 .add_segment_callback = quic_add_segment_callback,
2117 .del_segment_callback = quic_del_segment_callback,
2118 .builtin_app_rx_callback = quic_udp_session_rx_callback,
2119 .session_cleanup_callback = quic_udp_session_cleanup_callback,
2122 static const transport_proto_vft_t quic_proto = {
2123 .connect = quic_connect,
2124 .close = quic_proto_on_close,
2125 .start_listen = quic_start_listen,
2126 .stop_listen = quic_stop_listen,
2127 .get_connection = quic_connection_get,
2128 .get_listener = quic_listener_get,
2129 .update_time = quic_update_time,
2130 .app_rx_evt = quic_custom_app_rx_callback,
2131 .custom_tx = quic_custom_tx_callback,
2132 .format_connection = format_quic_connection,
2133 .format_half_open = format_quic_half_open,
2134 .format_listener = format_quic_listener,
2135 .get_transport_endpoint = quic_get_transport_endpoint,
2136 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2137 .transport_options = {
2138 .tx_type = TRANSPORT_TX_INTERNAL,
2139 .service_type = TRANSPORT_SERVICE_APP,
2145 quic_register_cipher_suite (crypto_engine_type_t type,
2146 ptls_cipher_suite_t ** ciphers)
2148 quic_main_t *qm = &quic_main;
2149 vec_validate (qm->quic_ciphers, type);
2150 clib_bitmap_set (qm->available_crypto_engines, type, 1);
2151 qm->quic_ciphers[type] = ciphers;
2155 quic_update_fifo_size ()
2157 quic_main_t *qm = &quic_main;
2158 segment_manager_props_t *seg_mgr_props =
2159 application_get_segment_manager_properties (qm->app_index);
2164 ("error while getting segment_manager_props_t, can't update fifo-size");
2168 seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
2169 seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
2172 static clib_error_t *
2173 quic_init (vlib_main_t * vm)
2175 u32 segment_size = 256 << 20;
2176 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2177 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2178 vnet_app_attach_args_t _a, *a = &_a;
2179 u64 options[APP_OPTIONS_N_OPTIONS];
2180 quic_main_t *qm = &quic_main;
2183 num_threads = 1 /* main thread */ + vtm->n_threads;
2185 clib_memset (a, 0, sizeof (*a));
2186 clib_memset (options, 0, sizeof (options));
2188 a->session_cb_vft = &quic_app_cb_vft;
2189 a->api_client_index = APP_INVALID_INDEX;
2190 a->options = options;
2191 a->name = format (0, "quic");
2192 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2193 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2194 a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
2195 a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
2196 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
2197 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2198 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2199 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2201 if (vnet_application_attach (a))
2203 clib_warning ("failed to attach quic app");
2204 return clib_error_return (0, "failed to attach quic app");
2207 vec_validate (qm->ctx_pool, num_threads - 1);
2208 vec_validate (qm->wrk_ctx, num_threads - 1);
2209 /* Timer wheels, one per thread. */
2210 for (i = 0; i < num_threads; i++)
2212 tw = &qm->wrk_ctx[i].timer_wheel;
2213 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2214 1e-3 /* timer period 1ms */ , ~0);
2215 tw->last_run_time = vlib_time_now (vlib_get_main ());
2218 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2222 qm->app_index = a->app_index;
2223 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2224 / QUIC_TSTAMP_RESOLUTION;
2225 qm->session_cache.super.cb = quic_encrypt_ticket_cb;
2227 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2228 FIB_PROTOCOL_IP4, ~0);
2229 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2230 FIB_PROTOCOL_IP6, ~0);
2232 clib_bitmap_alloc (qm->available_crypto_engines,
2233 app_crypto_engine_n_types ());
2234 quic_register_cipher_suite (CRYPTO_ENGINE_VPP, quic_crypto_cipher_suites);
2235 quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
2236 ptls_openssl_cipher_suites);
2237 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2242 VLIB_INIT_FUNCTION (quic_init);
2244 static clib_error_t *
2245 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2246 unformat_input_t * input,
2247 vlib_cli_command_t * cmd)
2249 quic_main_t *qm = &quic_main;
2250 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
2251 return clib_error_return (0, "unknown input '%U'",
2252 format_unformat_error, input);
2253 if (unformat (input, "vpp"))
2254 qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2255 else if (unformat (input, "picotls"))
2256 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2258 return clib_error_return (0, "unknown input '%U'",
2259 format_unformat_error, input);
2263 u64 quic_fifosize = 0;
2264 static clib_error_t *
2265 quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
2266 unformat_input_t * input,
2267 vlib_cli_command_t * cmd)
2269 quic_main_t *qm = &quic_main;
2270 unformat_input_t _line_input, *line_input = &_line_input;
2273 if (!unformat_user (input, unformat_line_input, line_input))
2276 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2278 if (unformat (line_input, "%U", unformat_memory_size, &tmp))
2280 if (tmp >= 0x100000000ULL)
2282 return clib_error_return
2283 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
2285 qm->udp_fifo_size = tmp;
2286 quic_update_fifo_size ();
2289 return clib_error_return (0, "unknown input '%U'",
2290 format_unformat_error, line_input);
2297 quic_format_ctx_stat (u8 * s, va_list * args)
2299 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2300 quicly_stats_t quicly_stats;
2302 quicly_get_stats (ctx->conn, &quicly_stats);
2304 s = format (s, "\n\rQUIC conn stats \n\r");
2307 format (s, "RTT: min:%d, smoothed:%d, variance:%d, latest:%d \n\r",
2308 quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
2309 quicly_stats.rtt.variance, quicly_stats.rtt.latest);
2310 s = format (s, "Packet loss:%d \n\r", quicly_stats.num_packets.lost);
2315 static clib_error_t *
2316 quic_plugin_showstats_command_fn (vlib_main_t * vm,
2317 unformat_input_t * input,
2318 vlib_cli_command_t * cmd)
2320 quic_main_t *qm = &quic_main;
2321 quic_ctx_t *ctx = NULL;
2322 u32 num_workers = vlib_num_workers ();
2324 for (int i = 0; i < num_workers + 1; i++)
2327 pool_foreach (ctx, qm->ctx_pool[i],
2329 if(!(ctx->flags & QUIC_F_IS_LISTENER) && !(ctx->flags & QUIC_F_IS_STREAM))
2330 vlib_cli_output (vm, "%U", quic_format_ctx_stat, ctx);
2337 static clib_error_t *
2338 quic_show_ctx_command_fn (vlib_main_t * vm, unformat_input_t * input,
2339 vlib_cli_command_t * cmd)
2341 quic_main_t *qm = &quic_main;
2342 quic_ctx_t *ctx = NULL;
2343 u32 num_workers = vlib_num_workers ();
2345 for (int i = 0; i < num_workers + 1; i++)
2348 pool_foreach (ctx, qm->ctx_pool[i],
2350 vlib_cli_output (vm, "%U", format_quic_ctx, ctx, 1);
2358 VLIB_CLI_COMMAND (quic_plugin_crypto_command, static) =
2360 .path = "quic set crypto api",
2361 .short_help = "quic set crypto api [picotls, vpp]",
2362 .function = quic_plugin_crypto_command_fn,
2364 VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
2366 .path = "quic set fifo-size",
2367 .short_help = "quic set fifo-size N[K|M|G] (default 64K)",
2368 .function = quic_plugin_set_fifo_size_command_fn,
2370 VLIB_CLI_COMMAND(quic_plugin_stats_command, static)=
2372 .path = "show quic stats",
2373 .short_help = "show quic stats",
2374 .function = quic_plugin_showstats_command_fn,
2376 VLIB_CLI_COMMAND(quic_show_ctx_command, static)=
2378 .path = "show quic ctx",
2379 .short_help = "show quic ctx",
2380 .function = quic_show_ctx_command_fn,
2382 VLIB_PLUGIN_REGISTER () =
2384 .version = VPP_BUILD_VER,
2385 .description = "Quic transport protocol",
2386 .default_disabled = 1,
2390 static clib_error_t *
2391 quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
2393 quic_main_t *qm = &quic_main;
2396 qm->udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
2397 qm->udp_fifo_prealloc = 0;
2398 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2400 if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
2402 if (tmp >= 0x100000000ULL)
2404 return clib_error_return
2405 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
2407 qm->udp_fifo_size = tmp;
2411 (input, "fifo-prealloc %u", &quic_main.udp_fifo_prealloc))
2414 return clib_error_return (0, "unknown input '%U'",
2415 format_unformat_error, input);
2421 VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
2424 quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
2425 vlib_frame_t * frame)
2431 VLIB_REGISTER_NODE (quic_input_node) =
2433 .function = quic_node_fn,
2434 .name = "quic-input",
2435 .vector_size = sizeof (u32),
2436 .type = VLIB_NODE_TYPE_INTERNAL,
2437 .n_errors = ARRAY_LEN (quic_error_strings),
2438 .error_strings = quic_error_strings,
2443 * fd.io coding-style-patch-verification: ON
2446 * eval: (c-set-style "gnu")