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/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);
42 static quicly_stream_open_t on_stream_open;
43 static quicly_closed_by_peer_t on_closed_by_peer;
44 static quicly_now_t quicly_vpp_now_cb;
47 quic_store_quicly_ctx (application_t * app, u32 ckpair_index,
50 quic_main_t *qm = &quic_main;
51 quicly_context_t *quicly_ctx;
53 app_cert_key_pair_t *ckpair;
58 if (crypto_engine == CRYPTO_ENGINE_NONE)
60 QUIC_DBG (2, "No crypto engine specified, using %d", crypto_engine);
61 crypto_engine = qm->default_crypto_engine;
63 if (!clib_bitmap_get (qm->available_crypto_engines, crypto_engine))
65 QUIC_DBG (1, "Quic does not support crypto engine %d", crypto_engine);
66 return VNET_API_ERROR_MISSING_CERT_KEY;
69 quicly_ctx_data_t *quicly_ctx_data =
70 clib_mem_alloc (sizeof (quicly_ctx_data_t));
71 clib_memset (quicly_ctx_data, 0, sizeof (*quicly_ctx_data)); /* picotls depends on this */
72 quicly_ctx = &quicly_ctx_data->quicly_ctx;
73 ptls_context_t *ptls_ctx = &quicly_ctx_data->ptls_ctx;
74 ptls_ctx->random_bytes = ptls_openssl_random_bytes;
75 ptls_ctx->get_time = &ptls_get_time;
76 ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
77 ptls_ctx->cipher_suites = qm->quic_ciphers[crypto_engine];
78 ptls_ctx->certificates.list = NULL;
79 ptls_ctx->certificates.count = 0;
80 ptls_ctx->esni = NULL;
81 ptls_ctx->on_client_hello = NULL;
82 ptls_ctx->emit_certificate = NULL;
83 ptls_ctx->sign_certificate = NULL;
84 ptls_ctx->verify_certificate = NULL;
85 ptls_ctx->ticket_lifetime = 86400;
86 ptls_ctx->max_early_data_size = 8192;
87 ptls_ctx->hkdf_label_prefix__obsolete = NULL;
88 ptls_ctx->require_dhe_on_psk = 1;
89 ptls_ctx->encrypt_ticket = &qm->session_cache.super;
91 app->quicly_ctx = (u64 *) quicly_ctx;
92 clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
94 quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
95 quicly_ctx->tls = ptls_ctx;
96 quicly_ctx->stream_open = &on_stream_open;
97 quicly_ctx->closed_by_peer = &on_closed_by_peer;
98 quicly_ctx->now = &quicly_vpp_now_cb;
99 quicly_amend_ptls_context (quicly_ctx->tls);
101 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
102 quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
103 quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
104 quicly_ctx->transport_params.idle_timeout = qm->connection_timeout;
106 /* max_enq is FIFO_SIZE - 1 */
107 max_enq = app->sm_properties.rx_fifo_size - 1;
108 quicly_ctx->transport_params.max_stream_data.bidi_local = max_enq;
109 max_enq = app->sm_properties.tx_fifo_size - 1;
110 quicly_ctx->transport_params.max_stream_data.bidi_remote = max_enq;
111 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
113 quicly_ctx->tls->random_bytes (quicly_ctx_data->cid_key, 16);
114 quicly_ctx_data->cid_key[16] = 0;
115 key_vec = ptls_iovec_init (quicly_ctx_data->cid_key,
116 strlen (quicly_ctx_data->cid_key));
117 quicly_ctx->cid_encryptor =
118 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
119 &ptls_openssl_aes128ecb,
120 &ptls_openssl_sha256, key_vec);
122 ckpair = app_cert_key_pair_get_if_valid (ckpair_index);
123 if (!ckpair || !ckpair->key || !ckpair->cert)
125 QUIC_DBG (1, "Wrong ckpair id %d\n", ckpair_index);
128 if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
130 QUIC_DBG (1, "failed to read private key from app configuration\n");
133 if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
135 QUIC_DBG (1, "failed to load certificate\n");
141 clib_mem_free (quicly_ctx_data);
142 return VNET_API_ERROR_MISSING_CERT_KEY;
145 /* Helper functions */
148 quic_ctx_alloc (u32 thread_index)
150 quic_main_t *qm = &quic_main;
153 pool_get (qm->ctx_pool[thread_index], ctx);
155 clib_memset (ctx, 0, sizeof (quic_ctx_t));
156 ctx->c_thread_index = thread_index;
157 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
158 QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
159 ctx - qm->ctx_pool[thread_index], thread_index);
160 return ctx - qm->ctx_pool[thread_index];
164 quic_ctx_free (quic_ctx_t * ctx)
166 QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
167 u32 thread_index = ctx->c_thread_index;
168 ASSERT (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID);
170 clib_memset (ctx, 0xfb, sizeof (*ctx));
171 pool_put (quic_main.ctx_pool[thread_index], ctx);
175 quic_ctx_get (u32 ctx_index, u32 thread_index)
177 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
181 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
183 if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
185 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
189 quic_get_conn_ctx (quicly_conn_t * conn)
192 conn_data = (u64) * quicly_get_data (conn);
193 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
197 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
199 *quicly_get_data (conn) =
200 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
204 quic_ctx_is_stream (quic_ctx_t * ctx)
206 return (ctx->flags & QUIC_F_IS_STREAM);
210 quic_ctx_is_listener (quic_ctx_t * ctx)
212 return (ctx->flags & QUIC_F_IS_LISTENER);
216 get_stream_session_from_stream (quicly_stream_t * stream)
219 quic_stream_data_t *stream_data;
221 stream_data = (quic_stream_data_t *) stream->data;
222 ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
223 return session_get (ctx->c_s_index, stream_data->thread_index);
227 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
228 const quicly_cid_plaintext_t * id)
230 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
231 kv->key[1] = id->node_id;
235 quic_sendable_packet_count (session_t * udp_session)
238 u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
239 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
240 return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
243 static quicly_context_t *
244 quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
246 return ctx->quicly_ctx;
249 static quicly_context_t *
250 quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
252 session_t *udp_session = session_get_from_handle (udp_session_handle);
254 quic_ctx_get (udp_session->opaque, udp_session->thread_index);
255 return ctx->quicly_ctx;
259 quic_set_udp_tx_evt (session_t * udp_session)
262 if (svm_fifo_set_event (udp_session->tx_fifo))
263 rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
265 if (PREDICT_FALSE (rv))
266 clib_warning ("Event enqueue errored %d", rv);
270 quic_stop_ctx_timer (quic_ctx_t * ctx)
272 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
273 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
275 tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
276 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
277 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
278 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
281 /* QUIC protocol actions */
284 quic_ack_rx_data (session_t * stream_session)
289 quicly_stream_t *stream;
290 quic_stream_data_t *stream_data;
292 sctx = quic_ctx_get (stream_session->connection_index,
293 stream_session->thread_index);
294 ASSERT (quic_ctx_is_stream (sctx));
295 stream = sctx->stream;
296 stream_data = (quic_stream_data_t *) stream->data;
298 f = stream_session->rx_fifo;
299 max_deq = svm_fifo_max_dequeue (f);
301 ASSERT (stream_data->app_rx_data_len >= max_deq);
302 quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
303 QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
304 stream_data->app_rx_data_len = max_deq;
308 quic_disconnect_transport (quic_ctx_t * ctx)
310 QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
311 vnet_disconnect_args_t a = {
312 .handle = ctx->udp_session_handle,
313 .app_index = quic_main.app_index,
316 if (vnet_disconnect_session (&a))
317 clib_warning ("UDP session 0x%lx disconnect errored",
318 ctx->udp_session_handle);
322 quic_connection_delete (quic_ctx_t * ctx)
324 clib_bihash_kv_16_8_t kv;
327 QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
329 ASSERT (!quic_ctx_is_stream (ctx));
330 quic_stop_ctx_timer (ctx);
332 /* Delete the connection from the connection map */
334 quic_make_connection_key (&kv, quicly_get_master_id (conn));
335 QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
337 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
339 quic_disconnect_transport (ctx);
342 quicly_free (ctx->conn);
344 session_transport_delete_notify (&ctx->connection);
348 quic_increment_counter (u8 evt, u8 val)
350 vlib_main_t *vm = vlib_get_main ();
351 vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
355 * Called when quicly return an error
356 * This function interacts tightly with quic_proto_on_close
359 quic_connection_closed (quic_ctx_t * ctx)
361 QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
364 /* TODO if connection is not established, just delete the session? */
365 /* Actually should send connect or accept error */
367 switch (ctx->conn_state)
369 case QUIC_CONN_STATE_READY:
370 /* Error on an opened connection (timeout...)
371 This puts the session in closing state, we should receive a notification
372 when the app has closed its session */
373 session_transport_reset_notify (&ctx->connection);
374 /* This ensures we delete the connection when the app confirms the close */
375 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
377 case QUIC_CONN_STATE_PASSIVE_CLOSING:
378 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
379 /* quic_proto_on_close will eventually be called when the app confirms the close
380 , we delete the connection at that point */
382 case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
383 /* App already confirmed close, we can delete the connection */
384 quic_connection_delete (ctx);
386 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
389 case QUIC_CONN_STATE_ACTIVE_CLOSING:
390 quic_connection_delete (ctx);
393 QUIC_DBG (0, "BUG %d", ctx->conn_state);
399 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
402 session_dgram_hdr_t hdr;
405 transport_connection_t *tc;
407 len = packet->data.len;
408 f = udp_session->tx_fifo;
409 tc = session_get_transport (udp_session);
410 max_enqueue = svm_fifo_max_enqueue (f);
411 if (max_enqueue < SESSION_CONN_HDR_LEN + len)
413 QUIC_ERR ("Too much data to send, max_enqueue %u, len %u",
414 max_enqueue, len + SESSION_CONN_HDR_LEN);
415 return QUIC_ERROR_FULL_FIFO;
418 /* Build packet header for fifo */
419 hdr.data_length = len;
421 hdr.is_ip4 = tc->is_ip4;
422 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
423 hdr.lcl_port = tc->lcl_port;
425 /* Read dest address from quicly-provided sockaddr */
428 ASSERT (packet->dest.sa.sa_family == AF_INET);
429 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
430 hdr.rmt_port = sa4->sin_port;
431 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
435 ASSERT (packet->dest.sa.sa_family == AF_INET6);
436 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
437 hdr.rmt_port = sa6->sin6_port;
438 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
441 ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
442 if (ret != sizeof (hdr))
444 QUIC_ERR ("Not enough space to enqueue header");
445 return QUIC_ERROR_FULL_FIFO;
447 ret = svm_fifo_enqueue (f, len, packet->data.base);
450 QUIC_ERR ("Not enough space to enqueue payload");
451 return QUIC_ERROR_FULL_FIFO;
454 quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
460 quic_send_packets (quic_ctx_t * ctx)
462 quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
463 session_t *udp_session;
465 size_t num_packets, i, max_packets;
466 quicly_packet_allocator_t *pa;
467 quicly_context_t *quicly_context;
470 /* We have sctx, get qctx */
471 if (quic_ctx_is_stream (ctx))
472 ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
474 ASSERT (!quic_ctx_is_stream (ctx));
476 udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
485 /* TODO : quicly can assert it can send min_packets up to 2 */
486 if (quic_sendable_packet_count (udp_session) < 2)
489 quicly_context = quic_get_quicly_ctx_from_ctx (ctx);
492 clib_warning ("Tried to send packets on non existing app worker %u",
493 ctx->parent_app_wrk_id);
494 quic_connection_delete (ctx);
497 pa = quicly_context->packet_allocator;
500 max_packets = quic_sendable_packet_count (udp_session);
503 num_packets = max_packets;
504 if ((err = quicly_send (conn, packets, &num_packets)))
507 for (i = 0; i != num_packets; ++i)
509 if ((err = quic_send_datagram (udp_session, packets[i])))
512 pa->free_packet (pa, packets[i]);
515 while (num_packets > 0 && num_packets == max_packets);
518 quic_set_udp_tx_evt (udp_session);
520 QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
521 svm_fifo_max_dequeue (udp_session->rx_fifo));
522 quic_update_timer (ctx);
526 if (err && err != QUICLY_ERROR_PACKET_IGNORED
527 && err != QUICLY_ERROR_FREE_CONNECTION)
528 clib_warning ("Quic error '%U'.", quic_format_err, err);
529 quic_connection_closed (ctx);
533 /* Quicly callbacks */
536 quic_on_stream_destroy (quicly_stream_t * stream, int err)
538 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
539 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
540 stream_data->thread_index);
541 session_t *stream_session = session_get (sctx->c_s_index,
542 sctx->c_thread_index);
543 QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
544 session_handle (stream_session), quic_format_err, err);
546 stream_session->session_state = SESSION_STATE_CLOSED;
547 session_transport_delete_notify (&sctx->connection);
549 quic_ctx_free (sctx);
550 clib_mem_free (stream->data);
554 quic_on_stop_sending (quicly_stream_t * stream, int err)
557 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
558 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
559 stream_data->thread_index);
560 session_t *stream_session = session_get (sctx->c_s_index,
561 sctx->c_thread_index);
562 clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
563 session_handle (stream_session), quic_format_err, err);
565 /* TODO : handle STOP_SENDING */
570 quic_on_receive_reset (quicly_stream_t * stream, int err)
572 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
573 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
574 stream_data->thread_index);
576 session_t *stream_session = session_get (sctx->c_s_index,
577 sctx->c_thread_index);
578 clib_warning ("RESET_STREAM: session 0x%lx (%U)",
579 session_handle (stream_session), quic_format_err, err);
581 session_transport_closing_notify (&sctx->connection);
586 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
589 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
592 session_t *stream_session;
593 app_worker_t *app_wrk;
595 quic_stream_data_t *stream_data;
598 stream_data = (quic_stream_data_t *) stream->data;
599 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
600 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
601 f = stream_session->rx_fifo;
603 max_enq = svm_fifo_max_enqueue_prod (f);
604 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
605 /* Handle duplicate packet/chunk from quicly */
606 if (off < stream_data->app_rx_data_len)
608 QUIC_DBG (3, "Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
609 "DUPLICATE PACKET (max_enq %u, len %u, "
610 "app_rx_data_len %u, off %u, ToBeNQ %u)",
611 stream_session->session_index,
612 stream_session->app_wrk_index,
613 stream_session->thread_index, f,
614 max_enq, len, stream_data->app_rx_data_len, off,
615 off - stream_data->app_rx_data_len + len);
618 if (PREDICT_FALSE ((off - stream_data->app_rx_data_len + len) > max_enq))
620 QUIC_ERR ("Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
621 "RX FIFO IS FULL (max_enq %u, len %u, "
622 "app_rx_data_len %u, off %u, ToBeNQ %u)",
623 stream_session->session_index,
624 stream_session->app_wrk_index,
625 stream_session->thread_index, f,
626 max_enq, len, stream_data->app_rx_data_len, off,
627 off - stream_data->app_rx_data_len + len);
630 if (off == stream_data->app_rx_data_len)
632 /* Streams live on the same thread so (f, stream_data) should stay consistent */
633 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
634 QUIC_DBG (3, "Session [idx %u, app_wrk %u, ti %u, rx-fifo 0x%llx]: "
635 "Enqueuing %u (rlen %u) at off %u in %u space, ",
636 stream_session->session_index,
637 stream_session->app_wrk_index,
638 stream_session->thread_index, f, len, rlen, off, max_enq);
639 stream_data->app_rx_data_len += rlen;
640 ASSERT (rlen >= len);
641 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
642 if (PREDICT_TRUE (app_wrk != 0))
644 rv = app_worker_lock_and_send_event (app_wrk, stream_session,
647 QUIC_ERR ("Failed to ping app for RX");
649 quic_ack_rx_data (stream_session);
653 rlen = svm_fifo_enqueue_with_offset (f,
654 off - stream_data->app_rx_data_len,
662 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
664 session_t *stream_session;
668 stream_session = get_stream_session_from_stream (stream);
669 f = stream_session->tx_fifo;
671 rv = svm_fifo_dequeue_drop (f, delta);
672 ASSERT (rv == delta);
673 quicly_stream_sync_sendbuf (stream, 0);
677 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
678 size_t * len, int *wrote_all)
680 session_t *stream_session;
682 u32 deq_max, first_deq, max_rd_chunk, rem_offset;
684 stream_session = get_stream_session_from_stream (stream);
685 f = stream_session->tx_fifo;
687 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
689 deq_max = svm_fifo_max_dequeue_cons (f);
690 ASSERT (off <= deq_max);
691 if (off + *len < deq_max)
698 *len = deq_max - off;
699 QUIC_DBG (3, "Wrote ALL, %u", *len);
702 /* TODO, use something like : return svm_fifo_peek (f, off, *len, dst); */
703 max_rd_chunk = svm_fifo_max_read_chunk (f);
706 if (off < max_rd_chunk)
708 first_deq = clib_min (*len, max_rd_chunk - off);
709 clib_memcpy_fast (dst, svm_fifo_head (f) + off, first_deq);
712 if (max_rd_chunk < off + *len)
714 rem_offset = max_rd_chunk < off ? off - max_rd_chunk : 0;
715 clib_memcpy_fast (dst + first_deq, f->head_chunk->data + rem_offset,
722 static const quicly_stream_callbacks_t quic_stream_callbacks = {
723 .on_destroy = quic_on_stream_destroy,
724 .on_send_shift = quic_fifo_egress_shift,
725 .on_send_emit = quic_fifo_egress_emit,
726 .on_send_stop = quic_on_stop_sending,
727 .on_receive = quic_on_receive,
728 .on_receive_reset = quic_on_receive_reset
732 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
734 session_t *stream_session, *quic_session;
735 quic_stream_data_t *stream_data;
736 app_worker_t *app_wrk;
737 quic_ctx_t *qctx, *sctx;
741 QUIC_DBG (2, "on_stream_open called");
742 stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
743 stream->callbacks = &quic_stream_callbacks;
744 /* Notify accept on parent qsession, but only if this is not a locally
745 * initiated stream */
746 if (quicly_stream_is_self_initiated (stream))
749 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
750 qctx = quic_get_conn_ctx (stream->conn);
752 /* Might need to signal that the connection is ready if the first thing the
753 * server does is open a stream */
754 quic_check_quic_session_connected (qctx);
755 /* ctx might be invalidated */
756 qctx = quic_get_conn_ctx (stream->conn);
758 stream_session = session_alloc (qctx->c_thread_index);
759 QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
760 session_handle (stream_session), sctx_id);
761 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
762 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
763 sctx->parent_app_id = qctx->parent_app_id;
764 sctx->quic_connection_ctx_id = qctx->c_c_index;
765 sctx->c_c_index = sctx_id;
766 sctx->c_s_index = stream_session->session_index;
767 sctx->stream = stream;
768 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
769 sctx->flags |= QUIC_F_IS_STREAM;
771 stream_data = (quic_stream_data_t *) stream->data;
772 stream_data->ctx_id = sctx_id;
773 stream_data->thread_index = sctx->c_thread_index;
774 stream_data->app_rx_data_len = 0;
776 sctx->c_s_index = stream_session->session_index;
777 stream_session->session_state = SESSION_STATE_CREATED;
778 stream_session->app_wrk_index = sctx->parent_app_wrk_id;
779 stream_session->connection_index = sctx->c_c_index;
780 stream_session->session_type =
781 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
782 quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
783 stream_session->listener_handle = listen_session_get_handle (quic_session);
785 app_wrk = app_worker_get (stream_session->app_wrk_index);
786 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
788 QUIC_ERR ("failed to allocate fifos");
789 session_free (stream_session);
790 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
791 return 0; /* Frame is still valid */
793 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
794 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
795 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
797 if ((rv = app_worker_accept_notify (app_wrk, stream_session)))
799 QUIC_ERR ("failed to notify accept worker app");
800 session_free_w_fifos (stream_session);
801 quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
802 return 0; /* Frame is still valid */
809 quic_on_closed_by_peer (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
810 int code, uint64_t frame_type,
811 const char *reason, size_t reason_len)
813 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
815 session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
816 clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
817 session_handle (quic_session), quic_format_err, code,
820 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
821 session_transport_closing_notify (&ctx->connection);
827 quic_get_thread_time (u8 thread_index)
829 return quic_main.wrk_ctx[thread_index].time_now;
833 quic_get_time (quicly_now_t * self)
835 u8 thread_index = vlib_get_thread_index ();
836 return quic_get_thread_time (thread_index);
840 quic_set_time_now (u32 thread_index)
842 vlib_main_t *vlib_main = vlib_get_main ();
843 f64 time = vlib_time_now (vlib_main);
844 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
845 return quic_main.wrk_ctx[thread_index].time_now;
848 /* Transport proto callback */
850 quic_update_time (f64 now, u8 thread_index)
852 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
854 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
855 quic_set_time_now (thread_index);
856 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
860 quic_timer_expired (u32 conn_index)
863 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
864 quic_get_time (NULL));
865 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
866 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
867 quic_send_packets (ctx);
871 quic_update_timer (quic_ctx_t * ctx)
873 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
874 int64_t next_timeout, next_interval;
875 session_t *quic_session;
878 /* This timeout is in ms which is the unit of our timer */
879 next_timeout = quicly_get_first_timeout (ctx->conn);
880 next_interval = next_timeout - quic_get_time (NULL);
882 if (next_timeout == 0 || next_interval <= 0)
884 if (ctx->c_s_index == QUIC_SESSION_INVALID)
890 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
891 if (svm_fifo_set_event (quic_session->tx_fifo))
893 rv = session_send_io_evt_to_thread_custom (quic_session,
894 quic_session->thread_index,
895 SESSION_IO_EVT_BUILTIN_TX);
896 if (PREDICT_FALSE (rv))
897 QUIC_ERR ("Failed to enqueue builtin_tx %d", rv);
903 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
905 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
906 next_interval, ctx->c_c_index);
908 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
910 if (next_timeout == INT64_MAX)
912 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
915 ctx->timer_handle = tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index,
920 if (next_timeout == INT64_MAX)
922 quic_stop_ctx_timer (ctx);
925 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
932 quic_expired_timers_dispatch (u32 * expired_timers)
936 for (i = 0; i < vec_len (expired_timers); i++)
938 quic_timer_expired (expired_timers[i]);
942 /* Transport proto functions */
945 quic_connect_stream (session_t * quic_session, u32 opaque)
947 uint64_t quic_session_handle;
948 session_t *stream_session;
949 quic_stream_data_t *stream_data;
950 quicly_stream_t *stream;
952 app_worker_t *app_wrk;
953 quic_ctx_t *qctx, *sctx;
957 /* Find base session to which the user want to attach a stream */
958 quic_session_handle = session_handle (quic_session);
959 QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
961 if (session_type_transport_proto (quic_session->session_type) !=
962 TRANSPORT_PROTO_QUIC)
964 QUIC_ERR ("received incompatible session");
968 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
971 QUIC_ERR ("Invalid app worker :(");
975 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
976 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
977 qctx = quic_ctx_get (quic_session->connection_index,
978 quic_session->thread_index);
979 if (quic_ctx_is_stream (qctx))
981 QUIC_ERR ("session is a stream");
982 quic_ctx_free (sctx);
986 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
987 sctx->parent_app_id = qctx->parent_app_id;
988 sctx->quic_connection_ctx_id = qctx->c_c_index;
989 sctx->c_c_index = sctx_index;
990 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
991 sctx->flags |= QUIC_F_IS_STREAM;
995 if (!conn || !quicly_connection_is_ready (conn))
998 if ((rv = quicly_open_stream (conn, &stream, 0 /* uni */ )))
1000 QUIC_DBG (2, "Stream open failed with %d", rv);
1003 sctx->stream = stream;
1005 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1007 stream_session = session_alloc (qctx->c_thread_index);
1008 QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
1009 session_handle (stream_session), sctx_index);
1010 stream_session->app_wrk_index = app_wrk->wrk_index;
1011 stream_session->connection_index = sctx_index;
1012 stream_session->listener_handle = quic_session_handle;
1013 stream_session->session_type =
1014 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1016 sctx->c_s_index = stream_session->session_index;
1018 if (app_worker_init_connected (app_wrk, stream_session))
1020 QUIC_ERR ("failed to app_worker_init_connected");
1021 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
1022 session_free_w_fifos (stream_session);
1023 quic_ctx_free (sctx);
1024 return app_worker_connect_notify (app_wrk, NULL, opaque);
1027 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1028 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1029 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1031 stream_session->session_state = SESSION_STATE_READY;
1032 if (app_worker_connect_notify (app_wrk, stream_session, opaque))
1034 QUIC_ERR ("failed to notify app");
1035 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1036 session_free_w_fifos (stream_session);
1037 quic_ctx_free (sctx);
1040 stream_data = (quic_stream_data_t *) stream->data;
1041 stream_data->ctx_id = sctx->c_c_index;
1042 stream_data->thread_index = sctx->c_thread_index;
1043 stream_data->app_rx_data_len = 0;
1048 quic_connect_connection (session_endpoint_cfg_t * sep)
1050 vnet_connect_args_t _cargs, *cargs = &_cargs;
1051 quic_main_t *qm = &quic_main;
1053 app_worker_t *app_wrk;
1056 u32 thread_index = vlib_get_thread_index ();
1059 clib_memset (cargs, 0, sizeof (*cargs));
1060 ctx_index = quic_ctx_alloc (thread_index);
1061 ctx = quic_ctx_get (ctx_index, thread_index);
1062 ctx->parent_app_wrk_id = sep->app_wrk_index;
1063 ctx->c_s_index = QUIC_SESSION_INVALID;
1064 ctx->c_c_index = ctx_index;
1065 ctx->udp_is_ip4 = sep->is_ip4;
1066 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1067 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1068 ctx->client_opaque = sep->opaque;
1069 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1071 ctx->srv_hostname = format (0, "%v", sep->hostname);
1073 /* needed by quic for crypto + determining client / server */
1074 ctx->srv_hostname = format (0, "%U", format_ip46_address,
1075 &sep->ip, sep->is_ip4);
1076 vec_terminate_c_string (ctx->srv_hostname);
1078 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1079 cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1080 cargs->app_index = qm->app_index;
1081 cargs->api_context = ctx_index;
1083 app_wrk = app_worker_get (sep->app_wrk_index);
1084 app = application_get (app_wrk->app_index);
1085 ctx->parent_app_id = app_wrk->app_index;
1086 cargs->sep_ext.ns_index = app->ns_index;
1089 quic_store_quicly_ctx (app, sep->ckpair_index, sep->crypto_engine)))
1091 /* Also store it in ctx for convenience
1092 * Waiting for crypto_ctx logic */
1093 ctx->quicly_ctx = (quicly_context_t *) app->quicly_ctx;
1095 if ((error = vnet_connect (cargs)))
1102 quic_connect (transport_endpoint_cfg_t * tep)
1104 QUIC_DBG (2, "Called quic_connect");
1105 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1106 session_t *quic_session;
1107 sep = (session_endpoint_cfg_t *) tep;
1109 quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1111 return quic_connect_stream (quic_session, sep->opaque);
1113 return quic_connect_connection (sep);
1117 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1119 quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1123 session_t *stream_session = session_get (ctx->c_s_index,
1124 ctx->c_thread_index);
1125 clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1127 if (quic_ctx_is_stream (ctx))
1129 quicly_stream_t *stream = ctx->stream;
1130 quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1131 quic_send_packets (ctx);
1135 switch (ctx->conn_state)
1137 case QUIC_CONN_STATE_READY:
1138 ctx->conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1139 quicly_conn_t *conn = ctx->conn;
1140 /* Start connection closing. Keep sending packets until quicly_send
1141 returns QUICLY_ERROR_FREE_CONNECTION */
1142 quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1143 /* This also causes all streams to be closed (and the cb called) */
1144 quic_send_packets (ctx);
1146 case QUIC_CONN_STATE_PASSIVE_CLOSING:
1147 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1148 /* send_packets will eventually return an error, we delete the conn at
1151 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1152 quic_connection_delete (ctx);
1155 QUIC_DBG (0, "BUG");
1161 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1163 vnet_listen_args_t _bargs, *args = &_bargs;
1164 quic_main_t *qm = &quic_main;
1165 session_handle_t udp_handle;
1166 session_endpoint_cfg_t *sep;
1167 session_t *udp_listen_session;
1168 app_worker_t *app_wrk;
1172 app_listener_t *app_listener;
1175 sep = (session_endpoint_cfg_t *) tep;
1176 app_wrk = app_worker_get (sep->app_wrk_index);
1177 /* We need to call this because we call app_worker_init_connected in
1178 * quic_accept_stream, which assumes the connect segment manager exists */
1179 app_worker_alloc_connects_segment_manager (app_wrk);
1180 app = application_get (app_wrk->app_index);
1181 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1183 if (quic_store_quicly_ctx (app, sep->ckpair_index, sep->crypto_engine))
1186 sep->transport_proto = TRANSPORT_PROTO_UDPC;
1187 clib_memset (args, 0, sizeof (*args));
1188 args->app_index = qm->app_index;
1189 args->sep_ext = *sep;
1190 args->sep_ext.ns_index = app->ns_index;
1191 if ((rv = vnet_listen (args)))
1194 lctx_index = quic_ctx_alloc (0);
1195 udp_handle = args->handle;
1196 app_listener = app_listener_get_w_handle (udp_handle);
1197 udp_listen_session = app_listener_get_session (app_listener);
1198 udp_listen_session->opaque = lctx_index;
1200 lctx = quic_ctx_get (lctx_index, 0);
1201 lctx->flags |= QUIC_F_IS_LISTENER;
1202 /* Also store it in ctx for convenience
1203 * Waiting for crypto_ctx logic */
1204 lctx->quicly_ctx = (quicly_context_t *) app->quicly_ctx;
1206 clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1207 clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1208 lctx->c_rmt_port = args->sep.peer.port;
1209 lctx->c_lcl_port = args->sep.port;
1210 lctx->c_is_ip4 = args->sep.is_ip4;
1211 lctx->c_fib_index = args->sep.fib_index;
1212 lctx->c_proto = TRANSPORT_PROTO_QUIC;
1213 lctx->parent_app_wrk_id = sep->app_wrk_index;
1214 lctx->parent_app_id = app_wrk->app_index;
1215 lctx->udp_session_handle = udp_handle;
1216 lctx->c_s_index = quic_listen_session_index;
1218 QUIC_DBG (2, "Listening UDP session 0x%lx",
1219 session_handle (udp_listen_session));
1220 QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1225 quic_stop_listen (u32 lctx_index)
1227 QUIC_DBG (2, "Called quic_stop_listen");
1229 lctx = quic_ctx_get (lctx_index, 0);
1230 ASSERT (quic_ctx_is_listener (lctx));
1231 vnet_unlisten_args_t a = {
1232 .handle = lctx->udp_session_handle,
1233 .app_index = quic_main.app_index,
1234 .wrk_map_index = 0 /* default wrk */
1236 if (vnet_unlisten (&a))
1237 clib_warning ("unlisten errored");
1239 /* TODO: crypto state cleanup */
1241 quic_ctx_free (lctx);
1245 static transport_connection_t *
1246 quic_connection_get (u32 ctx_index, u32 thread_index)
1249 ctx = quic_ctx_get (ctx_index, thread_index);
1250 return &ctx->connection;
1253 static transport_connection_t *
1254 quic_listener_get (u32 listener_index)
1256 QUIC_DBG (2, "Called quic_listener_get");
1258 ctx = quic_ctx_get (listener_index, 0);
1259 return &ctx->connection;
1263 format_quic_ctx (u8 * s, va_list * args)
1265 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1266 u32 verbose = va_arg (*args, u32);
1271 str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1273 if (quic_ctx_is_listener (ctx))
1274 str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1275 else if (quic_ctx_is_stream (ctx))
1276 str = format (str, "Stream %ld conn %d",
1277 ctx->stream->stream_id, ctx->quic_connection_ctx_id);
1278 else /* connection */
1279 str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1280 ctx->udp_session_handle);
1282 str = format (str, " app %d wrk %d", ctx->parent_app_id,
1283 ctx->parent_app_wrk_id);
1286 s = format (s, "%-50s%-15d", str, ctx->conn_state);
1288 s = format (s, "%s\n", str);
1294 format_quic_connection (u8 * s, va_list * args)
1296 u32 qc_index = va_arg (*args, u32);
1297 u32 thread_index = va_arg (*args, u32);
1298 u32 verbose = va_arg (*args, u32);
1299 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1300 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1305 format_quic_half_open (u8 * s, va_list * args)
1307 u32 qc_index = va_arg (*args, u32);
1308 u32 thread_index = va_arg (*args, u32);
1309 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1310 s = format (s, "[#%d][Q] half-open app %u", thread_index,
1311 ctx->parent_app_id);
1317 format_quic_listener (u8 * s, va_list * args)
1319 u32 tci = va_arg (*args, u32);
1320 u32 thread_index = va_arg (*args, u32);
1321 u32 verbose = va_arg (*args, u32);
1322 quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1323 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1327 /* Session layer callbacks */
1330 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1331 ip46_address_t * addr, u16 port, u8 is_ip4)
1335 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1336 sa4->sin_family = AF_INET;
1337 sa4->sin_port = port;
1338 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1339 *salen = sizeof (struct sockaddr_in);
1343 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1344 sa6->sin6_family = AF_INET6;
1345 sa6->sin6_port = port;
1346 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1347 *salen = sizeof (struct sockaddr_in6);
1352 quic_on_quic_session_connected (quic_ctx_t * ctx)
1354 session_t *quic_session;
1355 app_worker_t *app_wrk;
1356 u32 ctx_id = ctx->c_c_index;
1357 u32 thread_index = ctx->c_thread_index;
1360 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1363 quic_disconnect_transport (ctx);
1367 quic_session = session_alloc (thread_index);
1369 QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1370 ctx->c_s_index = quic_session->session_index;
1371 quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1372 quic_session->connection_index = ctx->c_c_index;
1373 quic_session->listener_handle = SESSION_INVALID_HANDLE;
1374 quic_session->session_type =
1375 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1377 if (app_worker_init_connected (app_wrk, quic_session))
1379 QUIC_ERR ("failed to app_worker_init_connected");
1380 quic_proto_on_close (ctx_id, thread_index);
1381 return app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1384 quic_session->session_state = SESSION_STATE_CONNECTING;
1385 if ((rv = app_worker_connect_notify (app_wrk, quic_session,
1386 ctx->client_opaque)))
1388 QUIC_ERR ("failed to notify app %d", rv);
1389 quic_proto_on_close (ctx_id, thread_index);
1393 /* If the app opens a stream in its callback it may invalidate ctx */
1394 ctx = quic_ctx_get (ctx_id, thread_index);
1396 * app_worker_connect_notify() might have reallocated pool, reload
1397 * quic_session pointer
1399 quic_session = session_get (ctx->c_s_index, thread_index);
1400 quic_session->session_state = SESSION_STATE_LISTENING;
1406 quic_check_quic_session_connected (quic_ctx_t * ctx)
1408 /* Called when we need to trigger quic session connected
1409 * we may call this function on the server side / at
1412 /* Conn may be set to null if the connection is terminated */
1413 if (!ctx->conn || ctx->conn_state != QUIC_CONN_STATE_HANDSHAKE)
1415 if (!quicly_connection_is_ready (ctx->conn))
1417 ctx->conn_state = QUIC_CONN_STATE_READY;
1418 if (!quicly_is_client (ctx->conn))
1420 return quic_on_quic_session_connected (ctx);
1424 quic_receive_connection (void *arg)
1426 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1427 quic_ctx_t *temp_ctx, *new_ctx;
1428 clib_bihash_kv_16_8_t kv;
1429 quicly_conn_t *conn;
1430 session_t *udp_session;
1433 new_ctx_id = quic_ctx_alloc (thread_index);
1434 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1436 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1439 clib_memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1440 clib_mem_free (temp_ctx);
1442 new_ctx->c_thread_index = thread_index;
1443 new_ctx->c_c_index = new_ctx_id;
1445 conn = new_ctx->conn;
1446 quic_store_conn_ctx (conn, new_ctx);
1447 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1448 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1449 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1450 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1451 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1452 quic_update_timer (new_ctx);
1454 /* Trigger write on this connection if necessary */
1455 udp_session = session_get_from_handle (new_ctx->udp_session_handle);
1456 udp_session->opaque = new_ctx_id;
1457 udp_session->flags &= ~SESSION_F_IS_MIGRATING;
1458 if (svm_fifo_max_dequeue (udp_session->tx_fifo))
1459 quic_set_udp_tx_evt (udp_session);
1463 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1465 quic_ctx_t *ctx, *temp_ctx;
1466 u32 thread_index = vlib_get_thread_index ();
1468 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1470 temp_ctx = clib_mem_alloc (sizeof (quic_ctx_t));
1472 ctx = quic_ctx_get (ctx_index, thread_index);
1474 clib_memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1476 quic_stop_ctx_timer (ctx);
1477 quic_ctx_free (ctx);
1479 /* Send connection to destination thread */
1480 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1485 quic_udp_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1486 session_t * udp_session, u8 is_fail)
1488 QUIC_DBG (2, "QSession is now connected (id %u)",
1489 udp_session->session_index);
1490 /* This should always be called before quic_connect returns since UDP always
1491 * connects instantly. */
1492 clib_bihash_kv_16_8_t kv;
1493 struct sockaddr_in6 sa6;
1494 struct sockaddr *sa = (struct sockaddr *) &sa6;
1496 transport_connection_t *tc;
1497 app_worker_t *app_wrk;
1498 quicly_conn_t *conn;
1500 u32 thread_index = vlib_get_thread_index ();
1502 quicly_context_t *quicly_ctx;
1505 ctx = quic_ctx_get (ctx_index, thread_index);
1509 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1512 api_context = ctx->c_s_index;
1513 app_worker_connect_notify (app_wrk, 0, api_context);
1518 ctx->c_thread_index = thread_index;
1519 ctx->c_c_index = ctx_index;
1521 QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1522 is_fail, thread_index, (ctx) ? ctx_index : ~0);
1524 ctx->udp_session_handle = session_handle (udp_session);
1525 udp_session->opaque = ctx_index;
1527 /* Init QUIC lib connection
1528 * Generate required sockaddr & salen */
1529 tc = session_get_transport (udp_session);
1530 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1532 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1533 ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
1534 sa, NULL, &quic_main.next_cid, ptls_iovec_init (NULL,
1536 &quic_main.hs_properties, NULL);
1537 ++quic_main.next_cid.master_id;
1538 /* Save context handle in quicly connection */
1539 quic_store_conn_ctx (ctx->conn, ctx);
1542 /* Register connection in connections map */
1544 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1545 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1546 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1547 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1549 /* UDP stack quirk? preemptively transfer connection if that happens */
1550 if (udp_session->thread_index != thread_index)
1551 quic_transfer_connection (ctx_index, udp_session->thread_index);
1553 quic_send_packets (ctx);
1559 quic_udp_session_disconnect_callback (session_t * s)
1561 clib_warning ("UDP session disconnected???");
1565 quic_udp_session_cleanup_callback (session_t * udp_session,
1566 session_cleanup_ntf_t ntf)
1570 if (ntf != SESSION_CLEANUP_SESSION)
1573 ctx = quic_ctx_get (udp_session->opaque, udp_session->thread_index);
1574 quic_stop_ctx_timer (ctx);
1575 quic_ctx_free (ctx);
1579 quic_udp_session_reset_callback (session_t * s)
1581 clib_warning ("UDP session reset???");
1585 quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
1587 u32 new_thread = session_thread_from_handle (new_sh);
1590 QUIC_ERR ("Session %x migrated to %lx", s->session_index, new_sh);
1591 ASSERT (vlib_get_thread_index () == s->thread_index);
1592 ctx = quic_ctx_get (s->opaque, s->thread_index);
1593 ASSERT (ctx->udp_session_handle == session_handle (s));
1595 ctx->udp_session_handle = new_sh;
1597 s->opaque = 0xfeedface;
1599 quic_transfer_connection (ctx->c_c_index, new_thread);
1603 quic_udp_session_accepted_callback (session_t * udp_session)
1605 /* New UDP connection, try to accept it */
1607 quic_ctx_t *ctx, *lctx;
1608 session_t *udp_listen_session;
1609 u32 thread_index = vlib_get_thread_index ();
1611 udp_listen_session =
1612 listen_session_get_from_handle (udp_session->listener_handle);
1614 ctx_index = quic_ctx_alloc (thread_index);
1615 ctx = quic_ctx_get (ctx_index, thread_index);
1616 ctx->c_thread_index = udp_session->thread_index;
1617 ctx->c_c_index = ctx_index;
1618 ctx->c_s_index = QUIC_SESSION_INVALID;
1619 ctx->udp_session_handle = session_handle (udp_session);
1620 QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
1621 ctx->listener_ctx_id = udp_listen_session->opaque;
1622 lctx = quic_ctx_get (udp_listen_session->opaque,
1623 udp_listen_session->thread_index);
1624 ctx->udp_is_ip4 = lctx->c_is_ip4;
1625 ctx->parent_app_id = lctx->parent_app_id;
1626 ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
1627 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1628 ctx->conn_state = QUIC_CONN_STATE_OPENED;
1629 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1631 /* Also store it in ctx for convenience
1632 * Waiting for crypto_ctx logic */
1633 ctx->quicly_ctx = lctx->quicly_ctx;
1635 udp_session->opaque = ctx_index;
1637 /* TODO timeout to delete these if they never connect */
1642 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1644 /* No-op for builtin */
1649 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1651 /* No-op for builtin */
1656 quic_custom_app_rx_callback (transport_connection_t * tc)
1659 session_t *stream_session = session_get (tc->s_index, tc->thread_index);
1660 QUIC_DBG (3, "Received app READ notification");
1661 quic_ack_rx_data (stream_session);
1662 svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
1664 /* Need to send packets (acks may never be sent otherwise) */
1665 ctx = quic_ctx_get (stream_session->connection_index,
1666 stream_session->thread_index);
1667 quic_send_packets (ctx);
1672 quic_custom_tx_callback (void *s, u32 max_burst_size)
1674 session_t *stream_session = (session_t *) s;
1675 quicly_stream_t *stream;
1680 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1682 ctx = quic_ctx_get (stream_session->connection_index,
1683 stream_session->thread_index);
1684 if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
1686 goto tx_end; /* Most probably a reschedule */
1689 QUIC_DBG (3, "Stream TX event");
1690 quic_ack_rx_data (stream_session);
1691 if (!svm_fifo_max_dequeue (stream_session->tx_fifo))
1694 stream = ctx->stream;
1695 if (!quicly_sendstate_is_open (&stream->sendstate))
1697 QUIC_ERR ("Warning: tried to send on closed stream");
1701 if ((rv = quicly_stream_sync_sendbuf (stream, 1)) != 0)
1705 quic_send_packets (ctx);
1710 * Returns 0 if a matching connection is found and is on the right thread.
1711 * Otherwise returns -1.
1712 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
1716 quic_find_packet_ctx (quic_rx_packet_ctx_t * pctx, u32 caller_thread_index)
1719 quicly_conn_t *conn_;
1720 clib_bihash_kv_16_8_t kv;
1721 clib_bihash_16_8_t *h;
1723 h = &quic_main.connection_hash;
1724 quic_make_connection_key (&kv, &pctx->packet.cid.dest.plaintext);
1725 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
1727 if (clib_bihash_search_16_8 (h, &kv, &kv) == 0)
1729 u32 index = kv.value & UINT32_MAX;
1730 u32 thread_id = kv.value >> 32;
1731 /* Check if this connection belongs to this thread, otherwise
1732 * ask for it to be moved */
1733 if (thread_id != caller_thread_index)
1735 QUIC_DBG (2, "Connection is on wrong thread");
1736 /* Cannot make full check with quicly_is_destination... */
1737 pctx->ctx_index = index;
1738 pctx->thread_index = thread_id;
1739 return QUIC_PACKET_TYPE_MIGRATE;
1741 ctx_ = quic_ctx_get (index, vlib_get_thread_index ());
1744 && quicly_is_destination (conn_, NULL, &pctx->sa, &pctx->packet))
1746 QUIC_DBG (3, "Connection found");
1747 pctx->ctx_index = index;
1748 pctx->thread_index = thread_id;
1749 return QUIC_PACKET_TYPE_RECEIVE;
1752 QUIC_DBG (3, "connection not found");
1753 return QUIC_PACKET_TYPE_NONE;
1757 quic_accept_connection (u32 ctx_index, quic_rx_packet_ctx_t * pctx)
1759 u32 thread_index = vlib_get_thread_index ();
1760 quicly_context_t *quicly_ctx;
1761 session_t *quic_session;
1762 clib_bihash_kv_16_8_t kv;
1763 app_worker_t *app_wrk;
1764 quicly_conn_t *conn;
1769 /* new connection, accept and create context if packet is valid
1770 * TODO: check if socket is actually listening? */
1771 ctx = quic_ctx_get (ctx_index, thread_index);
1772 if (ctx->c_s_index != QUIC_SESSION_INVALID)
1774 QUIC_DBG (2, "already accepted ctx 0x%x", ctx_index);
1778 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1779 if ((rv = quicly_accept (&conn, quicly_ctx, NULL, &pctx->sa,
1780 &pctx->packet, NULL, &quic_main.next_cid, NULL)))
1782 /* Invalid packet, pass */
1783 assert (conn == NULL);
1784 QUIC_ERR ("Accept failed with %U", quic_format_err, rv);
1785 /* TODO: cleanup created quic ctx and UDP session */
1788 assert (conn != NULL);
1790 ++quic_main.next_cid.master_id;
1791 /* Save ctx handle in quicly connection */
1792 quic_store_conn_ctx (conn, ctx);
1794 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1796 quic_session = session_alloc (ctx->c_thread_index);
1797 QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
1798 session_handle (quic_session), ctx->c_c_index);
1799 quic_session->session_state = SESSION_STATE_LISTENING;
1800 ctx->c_s_index = quic_session->session_index;
1802 lctx = quic_ctx_get (ctx->listener_ctx_id, 0);
1804 quic_session->app_wrk_index = lctx->parent_app_wrk_id;
1805 quic_session->connection_index = ctx->c_c_index;
1806 quic_session->session_type =
1807 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1808 quic_session->listener_handle = lctx->c_s_index;
1810 /* TODO: don't alloc fifos when we don't transfer data on this session
1811 * but we still need fifos for the events? */
1812 if ((rv = app_worker_init_accepted (quic_session)))
1814 QUIC_ERR ("failed to allocate fifos");
1815 session_free (quic_session);
1818 app_wrk = app_worker_get (quic_session->app_wrk_index);
1819 if ((rv = app_worker_accept_notify (app_wrk, quic_session)))
1821 QUIC_ERR ("failed to notify accept worker app");
1825 /* Register connection in connections map */
1826 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1827 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1828 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1829 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1831 return quic_send_packets (ctx);
1835 quic_reset_connection (u64 udp_session_handle, quic_rx_packet_ctx_t * pctx)
1837 /* short header packet; potentially a dead connection. No need to check the
1838 * length of the incoming packet, because loop is prevented by authenticating
1839 * the CID (by checking node_id and thread_id). If the peer is also sending a
1840 * reset, then the next CID is highly likely to contain a non-authenticating
1842 QUIC_DBG (2, "Sending stateless reset");
1844 quicly_datagram_t *dgram;
1845 session_t *udp_session;
1846 quicly_context_t *quicly_ctx;
1847 if (pctx->packet.cid.dest.plaintext.node_id != 0
1848 || pctx->packet.cid.dest.plaintext.thread_id != 0)
1850 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
1851 dgram = quicly_send_stateless_reset (quicly_ctx, &pctx->sa, NULL,
1852 &pctx->packet.cid.dest.plaintext);
1855 udp_session = session_get_from_handle (udp_session_handle);
1856 rv = quic_send_datagram (udp_session, dgram);
1857 quic_set_udp_tx_evt (udp_session);
1862 quic_process_one_rx_packet (u64 udp_session_handle, svm_fifo_t * f,
1863 u32 fifo_offset, quic_rx_packet_ctx_t * pctx)
1867 u32 thread_index = vlib_get_thread_index ();
1868 u32 cur_deq = svm_fifo_max_dequeue (f) - fifo_offset;
1869 quicly_context_t *quicly_ctx;
1872 ret = svm_fifo_peek (f, fifo_offset,
1873 SESSION_CONN_HDR_LEN, (u8 *) & pctx->ph);
1874 ASSERT (ret == SESSION_CONN_HDR_LEN);
1875 ASSERT (pctx->ph.data_offset == 0);
1876 full_len = pctx->ph.data_length + SESSION_CONN_HDR_LEN;
1877 if (full_len > cur_deq)
1879 QUIC_ERR ("Not enough data in fifo RX");
1883 /* Quicly can read len bytes from the fifo at offset:
1884 * ph.data_offset + SESSION_CONN_HDR_LEN */
1885 ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN + fifo_offset,
1886 pctx->ph.data_length, pctx->data);
1887 if (ret != pctx->ph.data_length)
1889 QUIC_ERR ("Not enough data peeked in RX");
1893 quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
1894 quic_build_sockaddr (&pctx->sa, &pctx->salen, &pctx->ph.rmt_ip,
1895 pctx->ph.rmt_port, pctx->ph.is_ip4);
1896 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
1897 plen = quicly_decode_packet (quicly_ctx, &pctx->packet,
1898 pctx->data, pctx->ph.data_length);
1900 if (plen == SIZE_MAX)
1905 rv = quic_find_packet_ctx (pctx, thread_index);
1906 if (rv == QUIC_PACKET_TYPE_RECEIVE)
1908 pctx->ptype = QUIC_PACKET_TYPE_RECEIVE;
1911 else if (rv == QUIC_PACKET_TYPE_MIGRATE)
1913 pctx->ptype = QUIC_PACKET_TYPE_MIGRATE;
1914 /* Connection found but on wrong thread, ask move */
1916 else if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
1918 pctx->ptype = QUIC_PACKET_TYPE_ACCEPT;
1922 pctx->ptype = QUIC_PACKET_TYPE_RESET;
1928 quic_udp_session_rx_callback (session_t * udp_session)
1930 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
1931 quic_ctx_t *ctx = NULL;
1932 svm_fifo_t *f = udp_session->rx_fifo;
1934 u64 udp_session_handle = session_handle (udp_session);
1936 u32 thread_index = vlib_get_thread_index ();
1937 u32 cur_deq, fifo_offset, max_packets, i;
1939 quic_rx_packet_ctx_t packets_ctx[QUIC_RCV_MAX_BATCH_PACKETS];
1941 if (udp_session->flags & SESSION_F_IS_MIGRATING)
1943 QUIC_DBG (3, "RX on migrating udp session");
1948 max_deq = svm_fifo_max_dequeue (f);
1953 max_packets = QUIC_RCV_MAX_BATCH_PACKETS;
1955 for (i = 0; i < max_packets; i++)
1957 packets_ctx[i].thread_index = UINT32_MAX;
1958 packets_ctx[i].ctx_index = UINT32_MAX;
1959 packets_ctx[i].ptype = QUIC_PACKET_TYPE_DROP;
1961 cur_deq = max_deq - fifo_offset;
1964 max_packets = i + 1;
1967 if (cur_deq < SESSION_CONN_HDR_LEN)
1969 fifo_offset = max_deq;
1970 max_packets = i + 1;
1971 QUIC_ERR ("Fifo %d < header size in RX", cur_deq);
1974 rv = quic_process_one_rx_packet (udp_session_handle, f,
1975 fifo_offset, &packets_ctx[i]);
1976 if (packets_ctx[i].ptype != QUIC_PACKET_TYPE_MIGRATE)
1977 fifo_offset += SESSION_CONN_HDR_LEN + packets_ctx[i].ph.data_length;
1980 max_packets = i + 1;
1985 for (i = 0; i < max_packets; i++)
1987 switch (packets_ctx[i].ptype)
1989 case QUIC_PACKET_TYPE_RECEIVE:
1990 ctx = quic_ctx_get (packets_ctx[i].ctx_index, thread_index);
1991 rv = quicly_receive (ctx->conn, NULL, &packets_ctx[i].sa,
1992 &packets_ctx[i].packet);
1995 QUIC_DBG (1, "quicly_receive return error %U",
1996 quic_format_err, rv);
1999 case QUIC_PACKET_TYPE_ACCEPT:
2000 udp_session = session_get_from_handle (udp_session_handle);
2001 if ((rv = quic_accept_connection (udp_session->opaque,
2004 QUIC_ERR ("quic accept errored with %d", rv);
2007 case QUIC_PACKET_TYPE_RESET:
2008 quic_reset_connection (udp_session_handle, &packets_ctx[i]);
2012 for (i = 0; i < max_packets; i++)
2014 if (packets_ctx[i].ptype != QUIC_PACKET_TYPE_RECEIVE)
2016 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2017 packets_ctx[i].thread_index);
2018 quic_check_quic_session_connected (ctx);
2019 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2020 packets_ctx[i].thread_index);
2021 quic_send_packets (ctx);
2024 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
2025 f = udp_session->rx_fifo;
2026 svm_fifo_dequeue_drop (f, fifo_offset);
2028 if (svm_fifo_max_dequeue (f))
2035 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2036 transport_endpoint_t * tep, u8 is_lcl)
2038 session_t *udp_session;
2039 if (!quic_ctx_is_stream (ctx))
2041 udp_session = session_get_from_handle (ctx->udp_session_handle);
2042 session_get_endpoint (udp_session, tep, is_lcl);
2047 quic_get_transport_listener_endpoint (u32 listener_index,
2048 transport_endpoint_t * tep, u8 is_lcl)
2051 app_listener_t *app_listener;
2052 session_t *udp_listen_session;
2053 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2054 if (quic_ctx_is_listener (ctx))
2056 app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2057 udp_listen_session = app_listener_get_session (app_listener);
2058 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2060 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2064 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2065 transport_endpoint_t * tep, u8 is_lcl)
2068 ctx = quic_ctx_get (ctx_index, thread_index);
2069 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2073 static session_cb_vft_t quic_app_cb_vft = {
2074 .session_accept_callback = quic_udp_session_accepted_callback,
2075 .session_disconnect_callback = quic_udp_session_disconnect_callback,
2076 .session_connected_callback = quic_udp_session_connected_callback,
2077 .session_reset_callback = quic_udp_session_reset_callback,
2078 .session_migrate_callback = quic_udp_session_migrate_callback,
2079 .add_segment_callback = quic_add_segment_callback,
2080 .del_segment_callback = quic_del_segment_callback,
2081 .builtin_app_rx_callback = quic_udp_session_rx_callback,
2082 .session_cleanup_callback = quic_udp_session_cleanup_callback,
2085 static const transport_proto_vft_t quic_proto = {
2086 .connect = quic_connect,
2087 .close = quic_proto_on_close,
2088 .start_listen = quic_start_listen,
2089 .stop_listen = quic_stop_listen,
2090 .get_connection = quic_connection_get,
2091 .get_listener = quic_listener_get,
2092 .update_time = quic_update_time,
2093 .app_rx_evt = quic_custom_app_rx_callback,
2094 .custom_tx = quic_custom_tx_callback,
2095 .format_connection = format_quic_connection,
2096 .format_half_open = format_quic_half_open,
2097 .format_listener = format_quic_listener,
2098 .get_transport_endpoint = quic_get_transport_endpoint,
2099 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2100 .transport_options = {
2101 .tx_type = TRANSPORT_TX_INTERNAL,
2102 .service_type = TRANSPORT_SERVICE_APP,
2107 static quicly_stream_open_t on_stream_open = { quic_on_stream_open };
2108 static quicly_closed_by_peer_t on_closed_by_peer = { quic_on_closed_by_peer };
2109 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
2112 quic_register_cipher_suite (crypto_engine_type_t type,
2113 ptls_cipher_suite_t ** ciphers)
2115 quic_main_t *qm = &quic_main;
2116 vec_validate (qm->quic_ciphers, type);
2117 clib_bitmap_set (qm->available_crypto_engines, type, 1);
2118 qm->quic_ciphers[type] = ciphers;
2122 quic_update_fifo_size ()
2124 quic_main_t *qm = &quic_main;
2125 segment_manager_props_t *seg_mgr_props =
2126 application_get_segment_manager_properties (qm->app_index);
2131 ("error while getting segment_manager_props_t, can't update fifo-size");
2135 seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
2136 seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
2139 static clib_error_t *
2140 quic_init (vlib_main_t * vm)
2142 u32 segment_size = 256 << 20;
2143 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2144 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2145 vnet_app_attach_args_t _a, *a = &_a;
2146 u64 options[APP_OPTIONS_N_OPTIONS];
2147 quic_main_t *qm = &quic_main;
2150 num_threads = 1 /* main thread */ + vtm->n_threads;
2152 clib_memset (a, 0, sizeof (*a));
2153 clib_memset (options, 0, sizeof (options));
2155 a->session_cb_vft = &quic_app_cb_vft;
2156 a->api_client_index = APP_INVALID_INDEX;
2157 a->options = options;
2158 a->name = format (0, "quic");
2159 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2160 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2161 a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
2162 a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
2163 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
2164 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2165 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2166 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2168 if (vnet_application_attach (a))
2170 clib_warning ("failed to attach quic app");
2171 return clib_error_return (0, "failed to attach quic app");
2174 vec_validate (qm->ctx_pool, num_threads - 1);
2175 vec_validate (qm->wrk_ctx, num_threads - 1);
2176 /* Timer wheels, one per thread. */
2177 for (i = 0; i < num_threads; i++)
2179 tw = &qm->wrk_ctx[i].timer_wheel;
2180 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2181 1e-3 /* timer period 1ms */ , ~0);
2182 tw->last_run_time = vlib_time_now (vlib_get_main ());
2185 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2189 qm->app_index = a->app_index;
2190 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2191 / QUIC_TSTAMP_RESOLUTION;
2192 qm->session_cache.super.cb = quic_encrypt_ticket_cb;
2194 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2195 FIB_PROTOCOL_IP4, ~0);
2196 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2197 FIB_PROTOCOL_IP6, ~0);
2199 clib_bitmap_alloc (qm->available_crypto_engines,
2200 app_crypto_engine_n_types ());
2201 quic_register_cipher_suite (CRYPTO_ENGINE_VPP, quic_crypto_cipher_suites);
2202 quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
2203 ptls_openssl_cipher_suites);
2204 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2209 VLIB_INIT_FUNCTION (quic_init);
2211 static clib_error_t *
2212 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2213 unformat_input_t * input,
2214 vlib_cli_command_t * cmd)
2216 quic_main_t *qm = &quic_main;
2217 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
2218 return clib_error_return (0, "unknown input '%U'",
2219 format_unformat_error, input);
2220 if (unformat (input, "vpp"))
2221 qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2222 else if (unformat (input, "picotls"))
2223 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2225 return clib_error_return (0, "unknown input '%U'",
2226 format_unformat_error, input);
2230 u64 quic_fifosize = 0;
2231 static clib_error_t *
2232 quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
2233 unformat_input_t * input,
2234 vlib_cli_command_t * cmd)
2236 quic_main_t *qm = &quic_main;
2237 unformat_input_t _line_input, *line_input = &_line_input;
2240 if (!unformat_user (input, unformat_line_input, line_input))
2243 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2245 if (unformat (line_input, "%U", unformat_memory_size, &tmp))
2247 if (tmp >= 0x100000000ULL)
2249 return clib_error_return
2250 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
2252 qm->udp_fifo_size = tmp;
2253 quic_update_fifo_size ();
2256 return clib_error_return (0, "unknown input '%U'",
2257 format_unformat_error, line_input);
2264 quic_format_ctx_stat (u8 * s, va_list * args)
2266 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2267 quicly_stats_t quicly_stats;
2269 quicly_get_stats (ctx->conn, &quicly_stats);
2271 s = format (s, "\n\rQUIC conn stats \n\r");
2274 format (s, "RTT: min:%d, smoothed:%d, variance:%d, latest:%d \n\r",
2275 quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
2276 quicly_stats.rtt.variance, quicly_stats.rtt.latest);
2277 s = format (s, "Packet loss:%d \n\r", quicly_stats.num_packets.lost);
2282 static clib_error_t *
2283 quic_plugin_showstats_command_fn (vlib_main_t * vm,
2284 unformat_input_t * input,
2285 vlib_cli_command_t * cmd)
2287 quic_main_t *qm = &quic_main;
2288 quic_ctx_t *ctx = NULL;
2289 u32 num_workers = vlib_num_workers ();
2291 for (int i = 0; i < num_workers + 1; i++)
2294 pool_foreach (ctx, qm->ctx_pool[i],
2296 if(!(ctx->flags & QUIC_F_IS_LISTENER) && !(ctx->flags & QUIC_F_IS_STREAM))
2297 vlib_cli_output (vm, "%U", quic_format_ctx_stat, ctx);
2304 static clib_error_t *
2305 quic_show_ctx_command_fn (vlib_main_t * vm, unformat_input_t * input,
2306 vlib_cli_command_t * cmd)
2308 quic_main_t *qm = &quic_main;
2309 quic_ctx_t *ctx = NULL;
2310 u32 num_workers = vlib_num_workers ();
2312 for (int i = 0; i < num_workers + 1; i++)
2315 pool_foreach (ctx, qm->ctx_pool[i],
2317 vlib_cli_output (vm, "%U", format_quic_ctx, ctx, 1);
2325 VLIB_CLI_COMMAND (quic_plugin_crypto_command, static) =
2327 .path = "quic set crypto api",
2328 .short_help = "quic set crypto api [picotls, vpp]",
2329 .function = quic_plugin_crypto_command_fn,
2331 VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
2333 .path = "quic set fifo-size",
2334 .short_help = "quic set fifo-size N[K|M|G] (default 64K)",
2335 .function = quic_plugin_set_fifo_size_command_fn,
2337 VLIB_CLI_COMMAND(quic_plugin_stats_command, static)=
2339 .path = "show quic stats",
2340 .short_help = "show quic stats",
2341 .function = quic_plugin_showstats_command_fn,
2343 VLIB_CLI_COMMAND(quic_show_ctx_command, static)=
2345 .path = "show quic ctx",
2346 .short_help = "show quic ctx",
2347 .function = quic_show_ctx_command_fn,
2349 VLIB_PLUGIN_REGISTER () =
2351 .version = VPP_BUILD_VER,
2352 .description = "Quic transport protocol",
2353 .default_disabled = 1,
2357 static clib_error_t *
2358 quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
2360 quic_main_t *qm = &quic_main;
2364 qm->udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
2365 qm->udp_fifo_prealloc = 0;
2366 qm->connection_timeout = QUIC_DEFAULT_CONN_TIMEOUT;
2367 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2369 if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
2371 if (tmp >= 0x100000000ULL)
2373 return clib_error_return (0,
2374 "fifo-size %llu (0x%llx) too large",
2377 qm->udp_fifo_size = tmp;
2379 else if (unformat (input, "conn-timeout %u", &i))
2380 qm->connection_timeout = i;
2381 else if (unformat (input, "fifo-prealloc %u", &i))
2382 qm->udp_fifo_prealloc = i;
2384 return clib_error_return (0, "unknown input '%U'",
2385 format_unformat_error, input);
2391 VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
2394 quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
2395 vlib_frame_t * frame)
2401 VLIB_REGISTER_NODE (quic_input_node) =
2403 .function = quic_node_fn,
2404 .name = "quic-input",
2405 .vector_size = sizeof (u32),
2406 .type = VLIB_NODE_TYPE_INTERNAL,
2407 .n_errors = ARRAY_LEN (quic_error_strings),
2408 .error_strings = quic_error_strings,
2413 * fd.io coding-style-patch-verification: ON
2416 * eval: (c-set-style "gnu")