2 * Copyright (c) 2021 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>
30 #include <quicly/constants.h>
31 #include <quicly/defaults.h>
34 #include <quic/quic_crypto.h>
36 extern quicly_crypto_engine_t quic_crypto_engine;
38 static char *quic_error_strings[] = {
39 #define quic_error(n,s) s,
40 #include <quic/quic_error.def>
44 #define DEFAULT_MAX_PACKETS_PER_KEY 16777216
46 quic_main_t quic_main;
47 static void quic_update_timer (quic_ctx_t * ctx);
48 static void quic_check_quic_session_connected (quic_ctx_t * ctx);
49 static int quic_reset_connection (u64 udp_session_handle,
50 quic_rx_packet_ctx_t * pctx);
51 static void quic_proto_on_close (u32 ctx_index, u32 thread_index);
53 static quicly_stream_open_t on_stream_open;
54 static quicly_closed_by_remote_t on_closed_by_remote;
55 static quicly_now_t quicly_vpp_now_cb;
60 quic_crypto_context_make_key_from_ctx (clib_bihash_kv_24_8_t * kv,
63 application_t *app = application_get (ctx->parent_app_id);
64 kv->key[0] = ((u64) ctx->ckpair_index) << 32 | (u64) ctx->crypto_engine;
65 kv->key[1] = app->sm_properties.rx_fifo_size - 1;
66 kv->key[2] = app->sm_properties.tx_fifo_size - 1;
70 quic_crypto_context_make_key_from_crctx (clib_bihash_kv_24_8_t * kv,
71 crypto_context_t * crctx)
73 quic_crypto_context_data_t *data =
74 (quic_crypto_context_data_t *) crctx->data;
75 kv->key[0] = ((u64) crctx->ckpair_index) << 32 | (u64) crctx->crypto_engine;
76 kv->key[1] = data->quicly_ctx.transport_params.max_stream_data.bidi_local;
77 kv->key[2] = data->quicly_ctx.transport_params.max_stream_data.bidi_remote;
81 quic_crypto_context_free_if_needed (crypto_context_t * crctx, u8 thread_index)
83 quic_main_t *qm = &quic_main;
84 clib_bihash_kv_24_8_t kv;
85 if (crctx->n_subscribers)
87 quic_crypto_context_make_key_from_crctx (&kv, crctx);
88 clib_bihash_add_del_24_8 (&qm->wrk_ctx[thread_index].crypto_context_hash,
89 &kv, 0 /* is_add */ );
90 clib_mem_free (crctx->data);
91 pool_put (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
95 quic_app_cert_key_pair_delete_callback (app_cert_key_pair_t * ckpair)
97 quic_main_t *qm = &quic_main;
98 crypto_context_t *crctx;
99 clib_bihash_kv_24_8_t kv;
100 vlib_thread_main_t *vtm = vlib_get_thread_main ();
101 int num_threads = 1 /* main thread */ + vtm->n_threads;
104 for (i = 0; i < num_threads; i++)
107 pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool) {
108 if (crctx->ckpair_index == ckpair->cert_key_index)
110 quic_crypto_context_make_key_from_crctx (&kv, crctx);
111 clib_bihash_add_del_24_8 (&qm->wrk_ctx[i].crypto_context_hash, &kv, 0 /* is_add */ );
119 static crypto_context_t *
120 quic_crypto_context_alloc (u8 thread_index)
122 quic_main_t *qm = &quic_main;
123 crypto_context_t *crctx;
126 pool_get (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
127 clib_memset (crctx, 0, sizeof (*crctx));
128 idx = (crctx - qm->wrk_ctx[thread_index].crypto_ctx_pool);
129 crctx->ctx_index = ((u32) thread_index) << 24 | idx;
134 static crypto_context_t *
135 quic_crypto_context_get (u32 cr_index, u32 thread_index)
137 quic_main_t *qm = &quic_main;
138 ASSERT (cr_index >> 24 == thread_index);
139 return pool_elt_at_index (qm->wrk_ctx[thread_index].crypto_ctx_pool,
140 cr_index & 0x00ffffff);
143 static clib_error_t *
144 quic_list_crypto_context_command_fn (vlib_main_t * vm,
145 unformat_input_t * input,
146 vlib_cli_command_t * cmd)
148 quic_main_t *qm = &quic_main;
149 crypto_context_t *crctx;
150 vlib_thread_main_t *vtm = vlib_get_thread_main ();
151 int i, num_threads = 1 /* main thread */ + vtm->n_threads;
152 for (i = 0; i < num_threads; i++)
155 pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool) {
156 vlib_cli_output (vm, "[%d][Q]%U", i, format_crypto_context, crctx);
163 static clib_error_t *
164 quic_set_max_packets_per_key_fn (vlib_main_t * vm,
165 unformat_input_t * input,
166 vlib_cli_command_t * cmd)
168 quic_main_t *qm = &quic_main;
169 unformat_input_t _line_input, *line_input = &_line_input;
172 if (!unformat_user (input, unformat_line_input, line_input))
175 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
177 if (unformat (line_input, "%U", unformat_memory_size, &tmp))
179 qm->max_packets_per_key = tmp;
182 return clib_error_return (0, "unknown input '%U'",
183 format_unformat_error, line_input);
189 static clib_error_t *
190 quic_set_cc_fn (vlib_main_t *vm, unformat_input_t *input,
191 vlib_cli_command_t *cmd)
193 unformat_input_t _line_input, *line_input = &_line_input;
194 quic_main_t *qm = &quic_main;
197 if (!unformat_user (input, unformat_line_input, line_input))
200 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
202 if (unformat (line_input, "reno"))
203 qm->default_quic_cc = QUIC_CC_RENO;
204 else if (unformat (line_input, "cubic"))
205 qm->default_quic_cc = QUIC_CC_CUBIC;
208 e = clib_error_return (0, "unknown input '%U'",
209 format_unformat_error, line_input);
214 unformat_free (line_input);
219 quic_release_crypto_context (u32 crypto_context_index, u8 thread_index)
221 crypto_context_t *crctx;
222 crctx = quic_crypto_context_get (crypto_context_index, thread_index);
223 crctx->n_subscribers--;
224 quic_crypto_context_free_if_needed (crctx, thread_index);
228 quic_init_crypto_context (crypto_context_t * crctx, quic_ctx_t * ctx)
230 quic_main_t *qm = &quic_main;
231 quicly_context_t *quicly_ctx;
232 ptls_iovec_t key_vec;
233 app_cert_key_pair_t *ckpair;
235 quic_crypto_context_data_t *data;
236 ptls_context_t *ptls_ctx;
238 QUIC_DBG (2, "Init quic crctx %d thread %d", crctx->ctx_index,
239 ctx->c_thread_index);
241 data = clib_mem_alloc (sizeof (*data));
242 /* picotls depends on data being zeroed */
243 clib_memset (data, 0, sizeof (*data));
244 crctx->data = (void *) data;
245 quicly_ctx = &data->quicly_ctx;
246 ptls_ctx = &data->ptls_ctx;
248 ptls_ctx->random_bytes = ptls_openssl_random_bytes;
249 ptls_ctx->get_time = &ptls_get_time;
250 ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
251 ptls_ctx->cipher_suites = qm->quic_ciphers[ctx->crypto_engine];
252 ptls_ctx->certificates.list = NULL;
253 ptls_ctx->certificates.count = 0;
254 ptls_ctx->esni = NULL;
255 ptls_ctx->on_client_hello = NULL;
256 ptls_ctx->emit_certificate = NULL;
257 ptls_ctx->sign_certificate = NULL;
258 ptls_ctx->verify_certificate = NULL;
259 ptls_ctx->ticket_lifetime = 86400;
260 ptls_ctx->max_early_data_size = 8192;
261 ptls_ctx->hkdf_label_prefix__obsolete = NULL;
262 ptls_ctx->require_dhe_on_psk = 1;
263 ptls_ctx->encrypt_ticket = &qm->session_cache.super;
264 clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
266 quicly_ctx->max_packets_per_key = qm->max_packets_per_key;
267 quicly_ctx->tls = ptls_ctx;
268 quicly_ctx->stream_open = &on_stream_open;
269 quicly_ctx->closed_by_remote = &on_closed_by_remote;
270 quicly_ctx->now = &quicly_vpp_now_cb;
271 quicly_amend_ptls_context (quicly_ctx->tls);
273 if (qm->vnet_crypto_enabled &&
274 qm->default_crypto_engine == CRYPTO_ENGINE_VPP)
275 quicly_ctx->crypto_engine = &quic_crypto_engine;
277 quicly_ctx->crypto_engine = &quicly_default_crypto_engine;
279 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
280 quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
281 quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
282 quicly_ctx->transport_params.max_idle_timeout = qm->connection_timeout;
284 if (qm->default_quic_cc == QUIC_CC_CUBIC)
285 quicly_ctx->init_cc = &quicly_cc_cubic_init;
286 else if (qm->default_quic_cc == QUIC_CC_RENO)
287 quicly_ctx->init_cc = &quicly_cc_reno_init;
289 app = application_get (ctx->parent_app_id);
290 quicly_ctx->transport_params.max_stream_data.bidi_local =
291 app->sm_properties.rx_fifo_size - 1;
292 quicly_ctx->transport_params.max_stream_data.bidi_remote =
293 app->sm_properties.tx_fifo_size - 1;
294 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
296 quicly_ctx->transport_params.max_udp_payload_size = QUIC_MAX_PACKET_SIZE;
297 if (!app->quic_iv_set)
299 ptls_openssl_random_bytes (app->quic_iv, QUIC_IV_LEN - 1);
300 app->quic_iv[QUIC_IV_LEN - 1] = 0;
301 app->quic_iv_set = 1;
304 clib_memcpy (data->cid_key, app->quic_iv, QUIC_IV_LEN);
305 key_vec = ptls_iovec_init (data->cid_key, QUIC_IV_LEN);
306 quicly_ctx->cid_encryptor =
307 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
308 &ptls_openssl_aes128ecb,
309 &ptls_openssl_sha256, key_vec);
311 ckpair = app_cert_key_pair_get_if_valid (crctx->ckpair_index);
312 if (!ckpair || !ckpair->key || !ckpair->cert)
314 QUIC_DBG (1, "Wrong ckpair id %d\n", crctx->ckpair_index);
317 if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
319 QUIC_DBG (1, "failed to read private key from app configuration\n");
322 if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
324 QUIC_DBG (1, "failed to load certificate\n");
332 quic_acquire_crypto_context (quic_ctx_t * ctx)
334 quic_main_t *qm = &quic_main;
335 crypto_context_t *crctx;
336 clib_bihash_kv_24_8_t kv;
338 if (ctx->crypto_engine == CRYPTO_ENGINE_NONE)
340 QUIC_DBG (2, "No crypto engine specified, using %d",
341 qm->default_crypto_engine);
342 ctx->crypto_engine = qm->default_crypto_engine;
344 if (!clib_bitmap_get (qm->available_crypto_engines, ctx->crypto_engine))
346 QUIC_DBG (1, "Quic does not support crypto engine %d",
348 return SESSION_E_NOCRYPTOENG;
351 /* Check for exisiting crypto ctx */
352 quic_crypto_context_make_key_from_ctx (&kv, ctx);
353 if (clib_bihash_search_24_8
354 (&qm->wrk_ctx[ctx->c_thread_index].crypto_context_hash, &kv, &kv) == 0)
356 crctx = quic_crypto_context_get (kv.value, ctx->c_thread_index);
357 QUIC_DBG (2, "Found exisiting crypto context %d", kv.value);
358 ctx->crypto_context_index = kv.value;
359 crctx->n_subscribers++;
363 crctx = quic_crypto_context_alloc (ctx->c_thread_index);
364 ctx->crypto_context_index = crctx->ctx_index;
365 kv.value = crctx->ctx_index;
366 crctx->crypto_engine = ctx->crypto_engine;
367 crctx->ckpair_index = ctx->ckpair_index;
368 if (quic_init_crypto_context (crctx, ctx))
370 if (vnet_app_add_cert_key_interest (ctx->ckpair_index, qm->app_index))
372 crctx->n_subscribers++;
373 clib_bihash_add_del_24_8 (&qm->
374 wrk_ctx[ctx->c_thread_index].crypto_context_hash,
375 &kv, 1 /* is_add */ );
379 quic_crypto_context_free_if_needed (crctx, ctx->c_thread_index);
380 return SESSION_E_NOCRYPTOCKP;
383 /* Helper functions */
386 quic_ctx_alloc (u32 thread_index)
388 quic_main_t *qm = &quic_main;
391 pool_get (qm->ctx_pool[thread_index], ctx);
393 clib_memset (ctx, 0, sizeof (quic_ctx_t));
394 ctx->c_thread_index = thread_index;
395 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
396 QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
397 ctx - qm->ctx_pool[thread_index], thread_index);
398 return ctx - qm->ctx_pool[thread_index];
402 quic_ctx_free (quic_ctx_t * ctx)
404 QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
405 u32 thread_index = ctx->c_thread_index;
406 QUIC_ASSERT (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID);
408 clib_memset (ctx, 0xfb, sizeof (*ctx));
409 pool_put (quic_main.ctx_pool[thread_index], ctx);
413 quic_ctx_get (u32 ctx_index, u32 thread_index)
415 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
419 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
421 if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
423 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
427 quic_get_conn_ctx (quicly_conn_t * conn)
430 conn_data = (u64) * quicly_get_data (conn);
431 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
435 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
437 *quicly_get_data (conn) =
438 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
442 quic_ctx_is_stream (quic_ctx_t * ctx)
444 return (ctx->flags & QUIC_F_IS_STREAM);
448 quic_ctx_is_listener (quic_ctx_t * ctx)
450 return (ctx->flags & QUIC_F_IS_LISTENER);
454 quic_ctx_is_conn (quic_ctx_t * ctx)
456 return !(quic_ctx_is_listener (ctx) || quic_ctx_is_stream (ctx));
459 static inline session_t *
460 get_stream_session_and_ctx_from_stream (quicly_stream_t * stream,
463 quic_stream_data_t *stream_data;
465 stream_data = (quic_stream_data_t *) stream->data;
466 *ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
467 return session_get ((*ctx)->c_s_index, stream_data->thread_index);
471 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
472 const quicly_cid_plaintext_t * id)
474 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
475 kv->key[1] = id->node_id;
479 quic_sendable_packet_count (session_t * udp_session)
482 u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
483 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
484 return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
487 static quicly_context_t *
488 quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
490 crypto_context_t *crctx =
491 quic_crypto_context_get (ctx->crypto_context_index, ctx->c_thread_index);
492 quic_crypto_context_data_t *data =
493 (quic_crypto_context_data_t *) crctx->data;
494 return &data->quicly_ctx;
497 static quicly_context_t *
498 quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
500 session_t *udp_session = session_get_from_handle (udp_session_handle);
502 quic_ctx_get (udp_session->opaque, udp_session->thread_index);
503 return quic_get_quicly_ctx_from_ctx (ctx);
507 quic_set_udp_tx_evt (session_t * udp_session)
510 if (svm_fifo_set_event (udp_session->tx_fifo))
511 rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
513 if (PREDICT_FALSE (rv))
514 clib_warning ("Event enqueue errored %d", rv);
518 quic_stop_ctx_timer (quic_ctx_t * ctx)
520 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
521 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
523 tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
524 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
525 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
526 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
529 /* QUIC protocol actions */
532 quic_ack_rx_data (session_t * stream_session)
537 quicly_stream_t *stream;
538 quic_stream_data_t *stream_data;
540 sctx = quic_ctx_get (stream_session->connection_index,
541 stream_session->thread_index);
542 QUIC_ASSERT (quic_ctx_is_stream (sctx));
543 stream = sctx->stream;
544 stream_data = (quic_stream_data_t *) stream->data;
546 f = stream_session->rx_fifo;
547 max_deq = svm_fifo_max_dequeue (f);
549 QUIC_ASSERT (stream_data->app_rx_data_len >= max_deq);
550 quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
551 QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
552 stream_data->app_rx_data_len = max_deq;
556 quic_disconnect_transport (quic_ctx_t * ctx)
558 QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
559 vnet_disconnect_args_t a = {
560 .handle = ctx->udp_session_handle,
561 .app_index = quic_main.app_index,
564 if (vnet_disconnect_session (&a))
565 clib_warning ("UDP session 0x%lx disconnect errored",
566 ctx->udp_session_handle);
570 quic_connection_delete (quic_ctx_t * ctx)
572 clib_bihash_kv_16_8_t kv;
575 if (ctx->conn == NULL)
577 QUIC_DBG (2, "Skipping redundant delete of connection %u",
581 QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
583 QUIC_ASSERT (!quic_ctx_is_stream (ctx));
584 quic_stop_ctx_timer (ctx);
586 /* Delete the connection from the connection map */
589 quic_make_connection_key (&kv, quicly_get_master_id (conn));
590 QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
592 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
594 quic_disconnect_transport (ctx);
598 session_transport_delete_notify (&ctx->connection);
602 quic_increment_counter (u8 evt, u8 val)
604 vlib_main_t *vm = vlib_get_main ();
605 vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
609 * Called when quicly return an error
610 * This function interacts tightly with quic_proto_on_close
613 quic_connection_closed (quic_ctx_t * ctx)
615 QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
618 /* TODO if connection is not established, just delete the session? */
619 /* Actually should send connect or accept error */
621 switch (ctx->conn_state)
623 case QUIC_CONN_STATE_READY:
624 /* Error on an opened connection (timeout...)
625 This puts the session in closing state, we should receive a notification
626 when the app has closed its session */
627 session_transport_reset_notify (&ctx->connection);
628 /* This ensures we delete the connection when the app confirms the close */
629 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
631 case QUIC_CONN_STATE_PASSIVE_CLOSING:
632 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
633 /* quic_proto_on_close will eventually be called when the app confirms the close
634 , we delete the connection at that point */
636 case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
637 /* App already confirmed close, we can delete the connection */
638 quic_connection_delete (ctx);
640 case QUIC_CONN_STATE_OPENED:
641 case QUIC_CONN_STATE_HANDSHAKE:
642 case QUIC_CONN_STATE_ACTIVE_CLOSING:
643 quic_connection_delete (ctx);
646 QUIC_DBG (0, "BUG %d", ctx->conn_state);
652 quic_send_datagram (session_t *udp_session, struct iovec *packet,
653 quicly_address_t *dest, quicly_address_t *src)
655 u32 max_enqueue, len;
656 session_dgram_hdr_t hdr;
658 transport_connection_t *tc;
661 len = packet->iov_len;
662 f = udp_session->tx_fifo;
663 tc = session_get_transport (udp_session);
664 max_enqueue = svm_fifo_max_enqueue (f);
665 if (max_enqueue < SESSION_CONN_HDR_LEN + len)
667 QUIC_ERR ("Too much data to send, max_enqueue %u, len %u",
668 max_enqueue, len + SESSION_CONN_HDR_LEN);
669 return QUIC_ERROR_FULL_FIFO;
672 /* Build packet header for fifo */
673 hdr.data_length = len;
675 hdr.is_ip4 = tc->is_ip4;
676 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
677 hdr.lcl_port = tc->lcl_port;
679 /* Read dest address from quicly-provided sockaddr */
682 QUIC_ASSERT (dest->sa.sa_family == AF_INET);
683 struct sockaddr_in *sa4 = (struct sockaddr_in *) &dest->sa;
684 hdr.rmt_port = sa4->sin_port;
685 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
689 QUIC_ASSERT (dest->sa.sa_family == AF_INET6);
690 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &dest->sa;
691 hdr.rmt_port = sa6->sin6_port;
692 clib_memcpy_fast (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
695 svm_fifo_seg_t segs[2] = { { (u8 *) &hdr, sizeof (hdr) },
696 { packet->iov_base, len } };
698 ret = svm_fifo_enqueue_segments (f, segs, 2, 0 /* allow partial */);
699 if (PREDICT_FALSE (ret < 0))
701 QUIC_ERR ("Not enough space to enqueue dgram");
702 return QUIC_ERROR_FULL_FIFO;
705 quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
711 quic_send_packets (quic_ctx_t * ctx)
713 struct iovec packets[QUIC_SEND_PACKET_VEC_SIZE];
715 buf[QUIC_SEND_PACKET_VEC_SIZE * quic_get_quicly_ctx_from_ctx (ctx)
716 ->transport_params.max_udp_payload_size];
717 session_t *udp_session;
719 size_t num_packets, i, max_packets;
720 quicly_address_t dest, src;
724 /* We have sctx, get qctx */
725 if (quic_ctx_is_stream (ctx))
726 ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
728 QUIC_ASSERT (!quic_ctx_is_stream (ctx));
730 udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
740 /* TODO : quicly can assert it can send min_packets up to 2 */
741 max_packets = quic_sendable_packet_count (udp_session);
745 num_packets = max_packets;
746 if ((err = quicly_send (conn, &dest, &src, packets, &num_packets, buf,
750 for (i = 0; i != num_packets; ++i)
754 quic_send_datagram (udp_session, &packets[i], &dest, &src)))
758 n_sent += num_packets;
760 while (num_packets > 0 && num_packets == max_packets);
762 quic_set_udp_tx_evt (udp_session);
764 QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
765 svm_fifo_max_dequeue (udp_session->rx_fifo));
766 quic_update_timer (ctx);
770 if (err && err != QUICLY_ERROR_PACKET_IGNORED
771 && err != QUICLY_ERROR_FREE_CONNECTION)
772 clib_warning ("Quic error '%U'.", quic_format_err, err);
773 quic_connection_closed (ctx);
777 /* Quicly callbacks */
780 quic_on_stream_destroy (quicly_stream_t * stream, int err)
782 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
783 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
784 stream_data->thread_index);
785 session_t *stream_session = session_get (sctx->c_s_index,
786 sctx->c_thread_index);
787 QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
788 session_handle (stream_session), quic_format_err, err);
790 stream_session->session_state = SESSION_STATE_CLOSED;
791 session_transport_delete_notify (&sctx->connection);
793 quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
794 quic_ctx_free (sctx);
795 clib_mem_free (stream->data);
799 quic_on_stop_sending (quicly_stream_t * stream, int err)
802 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
803 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
804 stream_data->thread_index);
805 session_t *stream_session = session_get (sctx->c_s_index,
806 sctx->c_thread_index);
807 clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
808 session_handle (stream_session), quic_format_err, err);
810 /* TODO : handle STOP_SENDING */
814 quic_on_receive_reset (quicly_stream_t * stream, int err)
816 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
817 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
818 stream_data->thread_index);
820 session_t *stream_session = session_get (sctx->c_s_index,
821 sctx->c_thread_index);
822 clib_warning ("RESET_STREAM: session 0x%lx (%U)",
823 session_handle (stream_session), quic_format_err, err);
825 session_transport_closing_notify (&sctx->connection);
829 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
832 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
833 u32 max_enq, rlen, rv;
835 session_t *stream_session;
836 app_worker_t *app_wrk;
838 quic_stream_data_t *stream_data;
843 stream_data = (quic_stream_data_t *) stream->data;
844 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
845 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
846 f = stream_session->rx_fifo;
848 max_enq = svm_fifo_max_enqueue_prod (f);
849 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
850 /* Handle duplicate packet/chunk from quicly */
851 if (off < stream_data->app_rx_data_len)
853 QUIC_DBG (3, "Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
854 "DUPLICATE PACKET (max_enq %u, len %u, "
855 "app_rx_data_len %u, off %u, ToBeNQ %u)",
856 stream_session->session_index,
857 stream_session->app_wrk_index,
858 stream_session->thread_index, f,
859 max_enq, len, stream_data->app_rx_data_len, off,
860 off - stream_data->app_rx_data_len + len);
863 if (PREDICT_FALSE ((off - stream_data->app_rx_data_len + len) > max_enq))
865 QUIC_ERR ("Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
866 "RX FIFO IS FULL (max_enq %u, len %u, "
867 "app_rx_data_len %u, off %u, ToBeNQ %u)",
868 stream_session->session_index,
869 stream_session->app_wrk_index,
870 stream_session->thread_index, f,
871 max_enq, len, stream_data->app_rx_data_len, off,
872 off - stream_data->app_rx_data_len + len);
873 return; /* This shouldn't happen */
875 if (off == stream_data->app_rx_data_len)
877 /* Streams live on the same thread so (f, stream_data) should stay consistent */
878 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
879 QUIC_DBG (3, "Session [idx %u, app_wrk %u, ti %u, rx-fifo 0x%llx]: "
880 "Enqueuing %u (rlen %u) at off %u in %u space, ",
881 stream_session->session_index,
882 stream_session->app_wrk_index,
883 stream_session->thread_index, f, len, rlen, off, max_enq);
884 stream_data->app_rx_data_len += rlen;
885 QUIC_ASSERT (rlen >= len);
886 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
887 if (PREDICT_TRUE (app_wrk != 0))
889 rv = app_worker_lock_and_send_event (app_wrk, stream_session,
892 QUIC_ERR ("Failed to ping app for RX");
894 quic_ack_rx_data (stream_session);
898 rlen = svm_fifo_enqueue_with_offset (f,
899 off - stream_data->app_rx_data_len,
901 QUIC_ASSERT (rlen == 0);
907 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
909 quic_stream_data_t *stream_data;
910 session_t *stream_session;
915 stream_data = (quic_stream_data_t *) stream->data;
916 stream_session = get_stream_session_and_ctx_from_stream (stream, &ctx);
917 f = stream_session->tx_fifo;
919 QUIC_ASSERT (stream_data->app_tx_data_len >= delta);
920 stream_data->app_tx_data_len -= delta;
921 ctx->bytes_written += delta;
922 rv = svm_fifo_dequeue_drop (f, delta);
923 QUIC_ASSERT (rv == delta);
925 rv = quicly_stream_sync_sendbuf (stream, 0);
930 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
931 size_t * len, int *wrote_all)
933 quic_stream_data_t *stream_data;
935 session_t *stream_session;
939 stream_data = (quic_stream_data_t *) stream->data;
940 stream_session = get_stream_session_and_ctx_from_stream (stream, &ctx);
941 f = stream_session->tx_fifo;
943 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
945 deq_max = svm_fifo_max_dequeue_cons (f);
946 QUIC_ASSERT (off <= deq_max);
947 if (off + *len < deq_max)
954 *len = deq_max - off;
956 QUIC_ASSERT (*len > 0);
958 if (off + *len > stream_data->app_tx_data_len)
959 stream_data->app_tx_data_len = off + *len;
961 svm_fifo_peek (f, off, *len, dst);
964 static const quicly_stream_callbacks_t quic_stream_callbacks = {
965 .on_destroy = quic_on_stream_destroy,
966 .on_send_shift = quic_fifo_egress_shift,
967 .on_send_emit = quic_fifo_egress_emit,
968 .on_send_stop = quic_on_stop_sending,
969 .on_receive = quic_on_receive,
970 .on_receive_reset = quic_on_receive_reset
974 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
976 /* Return code for this function ends either
977 * - in quicly_receive : if not QUICLY_ERROR_PACKET_IGNORED, will close connection
978 * - in quicly_open_stream, returned directly
981 session_t *stream_session, *quic_session;
982 quic_stream_data_t *stream_data;
983 app_worker_t *app_wrk;
984 quic_ctx_t *qctx, *sctx;
988 QUIC_DBG (2, "on_stream_open called");
989 stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
990 stream->callbacks = &quic_stream_callbacks;
991 /* Notify accept on parent qsession, but only if this is not a locally
992 * initiated stream */
993 if (quicly_stream_is_self_initiated (stream))
996 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
997 qctx = quic_get_conn_ctx (stream->conn);
999 /* Might need to signal that the connection is ready if the first thing the
1000 * server does is open a stream */
1001 quic_check_quic_session_connected (qctx);
1002 /* ctx might be invalidated */
1003 qctx = quic_get_conn_ctx (stream->conn);
1005 stream_session = session_alloc (qctx->c_thread_index);
1006 QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
1007 session_handle (stream_session), sctx_id);
1008 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
1009 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
1010 sctx->parent_app_id = qctx->parent_app_id;
1011 sctx->quic_connection_ctx_id = qctx->c_c_index;
1012 sctx->c_c_index = sctx_id;
1013 sctx->c_s_index = stream_session->session_index;
1014 sctx->stream = stream;
1015 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1016 sctx->flags |= QUIC_F_IS_STREAM;
1017 if (quicly_stream_is_unidirectional (stream->stream_id))
1018 stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
1020 stream_data = (quic_stream_data_t *) stream->data;
1021 stream_data->ctx_id = sctx_id;
1022 stream_data->thread_index = sctx->c_thread_index;
1023 stream_data->app_rx_data_len = 0;
1024 stream_data->app_tx_data_len = 0;
1026 sctx->c_s_index = stream_session->session_index;
1027 stream_session->session_state = SESSION_STATE_CREATED;
1028 stream_session->app_wrk_index = sctx->parent_app_wrk_id;
1029 stream_session->connection_index = sctx->c_c_index;
1030 stream_session->session_type =
1031 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1032 quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
1033 stream_session->listener_handle = listen_session_get_handle (quic_session);
1035 app_wrk = app_worker_get (stream_session->app_wrk_index);
1036 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
1038 QUIC_ERR ("failed to allocate fifos");
1039 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
1040 return 0; /* Frame is still valid */
1042 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1043 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1044 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1046 if ((rv = app_worker_accept_notify (app_wrk, stream_session)))
1048 QUIC_ERR ("failed to notify accept worker app");
1049 quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
1050 return 0; /* Frame is still valid */
1057 quic_on_closed_by_remote (quicly_closed_by_remote_t *self, quicly_conn_t *conn,
1058 int code, uint64_t frame_type, const char *reason,
1061 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
1063 session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
1064 clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
1065 session_handle (quic_session), quic_format_err, code,
1066 reason_len, reason);
1068 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
1069 session_transport_closing_notify (&ctx->connection);
1072 /* Timer handling */
1075 quic_get_thread_time (u8 thread_index)
1077 return quic_main.wrk_ctx[thread_index].time_now;
1081 quic_get_time (quicly_now_t * self)
1083 u8 thread_index = vlib_get_thread_index ();
1084 return quic_get_thread_time (thread_index);
1088 quic_set_time_now (u32 thread_index)
1090 vlib_main_t *vlib_main = vlib_get_main ();
1091 f64 time = vlib_time_now (vlib_main);
1092 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
1093 return quic_main.wrk_ctx[thread_index].time_now;
1096 /* Transport proto callback */
1098 quic_update_time (f64 now, u8 thread_index)
1100 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1102 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1103 quic_set_time_now (thread_index);
1104 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
1108 quic_timer_expired (u32 conn_index)
1111 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
1112 quic_get_time (NULL));
1113 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
1114 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1115 quic_send_packets (ctx);
1119 quic_update_timer (quic_ctx_t * ctx)
1121 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1122 int64_t next_timeout, next_interval;
1123 session_t *quic_session;
1126 /* This timeout is in ms which is the unit of our timer */
1127 next_timeout = quicly_get_first_timeout (ctx->conn);
1128 next_interval = next_timeout - quic_get_time (NULL);
1130 if (next_timeout == 0 || next_interval <= 0)
1132 if (ctx->c_s_index == QUIC_SESSION_INVALID)
1138 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
1139 if (svm_fifo_set_event (quic_session->tx_fifo))
1141 rv = session_send_io_evt_to_thread_custom (quic_session,
1142 quic_session->thread_index,
1143 SESSION_IO_EVT_BUILTIN_TX);
1144 if (PREDICT_FALSE (rv))
1145 QUIC_ERR ("Failed to enqueue builtin_tx %d", rv);
1151 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
1153 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
1154 next_interval, ctx->c_c_index);
1156 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
1158 if (next_timeout == INT64_MAX)
1160 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
1163 ctx->timer_handle = tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index,
1168 if (next_timeout == INT64_MAX)
1170 quic_stop_ctx_timer (ctx);
1173 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
1180 quic_expired_timers_dispatch (u32 * expired_timers)
1184 for (i = 0; i < vec_len (expired_timers); i++)
1186 quic_timer_expired (expired_timers[i]);
1190 /* Transport proto functions */
1192 quic_connect_stream (session_t * quic_session, session_endpoint_cfg_t * sep)
1194 uint64_t quic_session_handle;
1195 session_t *stream_session;
1196 quic_stream_data_t *stream_data;
1197 quicly_stream_t *stream;
1198 quicly_conn_t *conn;
1199 app_worker_t *app_wrk;
1200 quic_ctx_t *qctx, *sctx;
1205 /* Find base session to which the user want to attach a stream */
1206 quic_session_handle = session_handle (quic_session);
1207 QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
1209 if (session_type_transport_proto (quic_session->session_type) !=
1210 TRANSPORT_PROTO_QUIC)
1212 QUIC_ERR ("received incompatible session");
1216 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
1219 QUIC_ERR ("Invalid app worker :(");
1223 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
1224 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
1225 qctx = quic_ctx_get (quic_session->connection_index,
1226 quic_session->thread_index);
1227 if (quic_ctx_is_stream (qctx))
1229 QUIC_ERR ("session is a stream");
1230 quic_ctx_free (sctx);
1234 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
1235 sctx->parent_app_id = qctx->parent_app_id;
1236 sctx->quic_connection_ctx_id = qctx->c_c_index;
1237 sctx->c_c_index = sctx_index;
1238 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1239 sctx->flags |= QUIC_F_IS_STREAM;
1243 if (!conn || !quicly_connection_is_ready (conn))
1246 is_unidir = sep->transport_flags & TRANSPORT_CFG_F_UNIDIRECTIONAL;
1247 if ((rv = quicly_open_stream (conn, &stream, is_unidir)))
1249 QUIC_DBG (2, "Stream open failed with %d", rv);
1252 quic_increment_counter (QUIC_ERROR_OPENED_STREAM, 1);
1254 sctx->stream = stream;
1256 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1258 stream_session = session_alloc (qctx->c_thread_index);
1259 QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
1260 session_handle (stream_session), sctx_index);
1261 stream_session->app_wrk_index = app_wrk->wrk_index;
1262 stream_session->connection_index = sctx_index;
1263 stream_session->listener_handle = quic_session_handle;
1264 stream_session->session_type =
1265 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1267 stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
1269 sctx->c_s_index = stream_session->session_index;
1270 stream_data = (quic_stream_data_t *) stream->data;
1271 stream_data->ctx_id = sctx->c_c_index;
1272 stream_data->thread_index = sctx->c_thread_index;
1273 stream_data->app_rx_data_len = 0;
1274 stream_data->app_tx_data_len = 0;
1275 stream_session->session_state = SESSION_STATE_READY;
1277 /* For now we only reset streams. Cleanup will be triggered by timers */
1278 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
1280 QUIC_ERR ("failed to app_worker_init_connected");
1281 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1282 return app_worker_connect_notify (app_wrk, NULL, rv, sep->opaque);
1285 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1286 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1287 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1289 if (app_worker_connect_notify (app_wrk, stream_session, SESSION_E_NONE,
1292 QUIC_ERR ("failed to notify app");
1293 quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
1294 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1302 quic_connect_connection (session_endpoint_cfg_t * sep)
1304 vnet_connect_args_t _cargs, *cargs = &_cargs;
1305 transport_endpt_crypto_cfg_t *ccfg;
1306 quic_main_t *qm = &quic_main;
1307 u32 ctx_index, thread_index;
1309 app_worker_t *app_wrk;
1314 return SESSION_E_NOEXTCFG;
1316 /* Use pool on thread 1 if we have workers because of UDP */
1317 thread_index = vlib_num_workers () ? 1 : 0;
1318 ccfg = &sep->ext_cfg->crypto;
1320 clib_memset (cargs, 0, sizeof (*cargs));
1321 ctx_index = quic_ctx_alloc (thread_index);
1322 ctx = quic_ctx_get (ctx_index, thread_index);
1323 ctx->parent_app_wrk_id = sep->app_wrk_index;
1324 ctx->c_s_index = QUIC_SESSION_INVALID;
1325 ctx->c_c_index = ctx_index;
1326 ctx->udp_is_ip4 = sep->is_ip4;
1327 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1328 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1329 ctx->client_opaque = sep->opaque;
1330 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1331 if (ccfg->hostname[0])
1332 ctx->srv_hostname = format (0, "%s", ccfg->hostname);
1334 /* needed by quic for crypto + determining client / server */
1335 ctx->srv_hostname = format (0, "%U", format_ip46_address,
1336 &sep->ip, sep->is_ip4);
1337 vec_terminate_c_string (ctx->srv_hostname);
1339 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1340 cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
1341 cargs->app_index = qm->app_index;
1342 cargs->api_context = ctx_index;
1344 app_wrk = app_worker_get (sep->app_wrk_index);
1345 app = application_get (app_wrk->app_index);
1346 ctx->parent_app_id = app_wrk->app_index;
1347 cargs->sep_ext.ns_index = app->ns_index;
1348 cargs->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
1350 ctx->crypto_engine = ccfg->crypto_engine;
1351 ctx->ckpair_index = ccfg->ckpair_index;
1352 if ((error = quic_acquire_crypto_context (ctx)))
1355 if ((error = vnet_connect (cargs)))
1362 quic_connect (transport_endpoint_cfg_t * tep)
1364 QUIC_DBG (2, "Called quic_connect");
1365 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1366 session_t *quic_session;
1367 sep = (session_endpoint_cfg_t *) tep;
1369 quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1371 return quic_connect_stream (quic_session, sep);
1373 return quic_connect_connection (sep);
1377 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1380 quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1383 session_t *stream_session = session_get (ctx->c_s_index,
1384 ctx->c_thread_index);
1386 clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1388 if (quic_ctx_is_stream (ctx))
1390 quicly_stream_t *stream = ctx->stream;
1391 if (!quicly_stream_has_send_side (quicly_is_client (stream->conn),
1394 quicly_sendstate_shutdown (&stream->sendstate, ctx->bytes_written +
1395 svm_fifo_max_dequeue
1396 (stream_session->tx_fifo));
1397 err = quicly_stream_sync_sendbuf (stream, 1);
1400 QUIC_DBG (1, "sendstate_shutdown failed for stream session %lu",
1401 session_handle (stream_session));
1402 quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1404 quic_send_packets (ctx);
1408 switch (ctx->conn_state)
1410 case QUIC_CONN_STATE_OPENED:
1411 case QUIC_CONN_STATE_HANDSHAKE:
1412 case QUIC_CONN_STATE_READY:
1413 ctx->conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1414 quicly_conn_t *conn = ctx->conn;
1415 /* Start connection closing. Keep sending packets until quicly_send
1416 returns QUICLY_ERROR_FREE_CONNECTION */
1418 quic_increment_counter (QUIC_ERROR_CLOSED_CONNECTION, 1);
1419 quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1420 /* This also causes all streams to be closed (and the cb called) */
1421 quic_send_packets (ctx);
1423 case QUIC_CONN_STATE_PASSIVE_CLOSING:
1424 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1425 /* send_packets will eventually return an error, we delete the conn at
1428 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1429 quic_connection_delete (ctx);
1431 case QUIC_CONN_STATE_ACTIVE_CLOSING:
1434 QUIC_ERR ("Trying to close conn in state %d", ctx->conn_state);
1440 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1442 vnet_listen_args_t _bargs, *args = &_bargs;
1443 transport_endpt_crypto_cfg_t *ccfg;
1444 quic_main_t *qm = &quic_main;
1445 session_handle_t udp_handle;
1446 session_endpoint_cfg_t *sep;
1447 session_t *udp_listen_session;
1448 app_worker_t *app_wrk;
1452 app_listener_t *app_listener;
1455 sep = (session_endpoint_cfg_t *) tep;
1457 return SESSION_E_NOEXTCFG;
1459 ccfg = &sep->ext_cfg->crypto;
1460 app_wrk = app_worker_get (sep->app_wrk_index);
1461 app = application_get (app_wrk->app_index);
1462 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1464 clib_memset (args, 0, sizeof (*args));
1465 args->app_index = qm->app_index;
1466 args->sep_ext = *sep;
1467 args->sep_ext.ns_index = app->ns_index;
1468 args->sep_ext.transport_proto = TRANSPORT_PROTO_UDP;
1469 args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
1470 if ((rv = vnet_listen (args)))
1473 lctx_index = quic_ctx_alloc (0);
1474 udp_handle = args->handle;
1475 app_listener = app_listener_get_w_handle (udp_handle);
1476 udp_listen_session = app_listener_get_session (app_listener);
1477 udp_listen_session->opaque = lctx_index;
1479 lctx = quic_ctx_get (lctx_index, 0);
1480 lctx->flags |= QUIC_F_IS_LISTENER;
1482 clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1483 clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1484 lctx->c_rmt_port = args->sep.peer.port;
1485 lctx->c_lcl_port = args->sep.port;
1486 lctx->c_is_ip4 = args->sep.is_ip4;
1487 lctx->c_fib_index = args->sep.fib_index;
1488 lctx->c_proto = TRANSPORT_PROTO_QUIC;
1489 lctx->parent_app_wrk_id = sep->app_wrk_index;
1490 lctx->parent_app_id = app_wrk->app_index;
1491 lctx->udp_session_handle = udp_handle;
1492 lctx->c_s_index = quic_listen_session_index;
1493 lctx->crypto_engine = ccfg->crypto_engine;
1494 lctx->ckpair_index = ccfg->ckpair_index;
1495 if ((rv = quic_acquire_crypto_context (lctx)))
1498 QUIC_DBG (2, "Listening UDP session 0x%lx",
1499 session_handle (udp_listen_session));
1500 QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1505 quic_stop_listen (u32 lctx_index)
1507 QUIC_DBG (2, "Called quic_stop_listen");
1509 lctx = quic_ctx_get (lctx_index, 0);
1510 QUIC_ASSERT (quic_ctx_is_listener (lctx));
1511 vnet_unlisten_args_t a = {
1512 .handle = lctx->udp_session_handle,
1513 .app_index = quic_main.app_index,
1514 .wrk_map_index = 0 /* default wrk */
1516 if (vnet_unlisten (&a))
1517 clib_warning ("unlisten errored");
1519 quic_release_crypto_context (lctx->crypto_context_index,
1520 0 /* thread_index */ );
1521 quic_ctx_free (lctx);
1525 static transport_connection_t *
1526 quic_connection_get (u32 ctx_index, u32 thread_index)
1529 ctx = quic_ctx_get (ctx_index, thread_index);
1530 return &ctx->connection;
1533 static transport_connection_t *
1534 quic_listener_get (u32 listener_index)
1536 QUIC_DBG (2, "Called quic_listener_get");
1538 ctx = quic_ctx_get (listener_index, 0);
1539 return &ctx->connection;
1543 format_quic_ctx (u8 * s, va_list * args)
1545 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1546 u32 verbose = va_arg (*args, u32);
1551 str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1553 if (quic_ctx_is_listener (ctx))
1554 str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1555 else if (quic_ctx_is_stream (ctx))
1556 str = format (str, "Stream %ld conn %d",
1557 ctx->stream->stream_id, ctx->quic_connection_ctx_id);
1558 else /* connection */
1559 str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1560 ctx->udp_session_handle);
1562 str = format (str, " app %d wrk %d", ctx->parent_app_id,
1563 ctx->parent_app_wrk_id);
1566 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-" SESSION_CLI_STATE_LEN "d",
1567 str, ctx->conn_state);
1569 s = format (s, "%s\n", str);
1575 format_quic_connection (u8 * s, va_list * args)
1577 u32 qc_index = va_arg (*args, u32);
1578 u32 thread_index = va_arg (*args, u32);
1579 u32 verbose = va_arg (*args, u32);
1580 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1581 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1586 format_quic_half_open (u8 * s, va_list * args)
1588 u32 qc_index = va_arg (*args, u32);
1589 u32 thread_index = va_arg (*args, u32);
1590 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1591 s = format (s, "[#%d][Q] half-open app %u", thread_index,
1592 ctx->parent_app_id);
1598 format_quic_listener (u8 * s, va_list * args)
1600 u32 tci = va_arg (*args, u32);
1601 u32 thread_index = va_arg (*args, u32);
1602 u32 verbose = va_arg (*args, u32);
1603 quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1604 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1608 /* Session layer callbacks */
1611 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1612 ip46_address_t * addr, u16 port, u8 is_ip4)
1616 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1617 sa4->sin_family = AF_INET;
1618 sa4->sin_port = port;
1619 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1620 *salen = sizeof (struct sockaddr_in);
1624 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1625 sa6->sin6_family = AF_INET6;
1626 sa6->sin6_port = port;
1627 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1628 *salen = sizeof (struct sockaddr_in6);
1633 quic_on_quic_session_connected (quic_ctx_t * ctx)
1635 session_t *quic_session;
1636 app_worker_t *app_wrk;
1637 u32 ctx_id = ctx->c_c_index;
1638 u32 thread_index = ctx->c_thread_index;
1641 quic_session = session_alloc (thread_index);
1643 QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1644 ctx->c_s_index = quic_session->session_index;
1645 quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1646 quic_session->connection_index = ctx->c_c_index;
1647 quic_session->listener_handle = SESSION_INVALID_HANDLE;
1648 quic_session->session_type =
1649 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1651 /* If quic session connected fails, immediatly close connection */
1652 app_wrk = app_worker_get (ctx->parent_app_wrk_id);
1653 if ((rv = app_worker_init_connected (app_wrk, quic_session)))
1655 QUIC_ERR ("failed to app_worker_init_connected");
1656 quic_proto_on_close (ctx_id, thread_index);
1657 app_worker_connect_notify (app_wrk, NULL, rv, ctx->client_opaque);
1661 quic_session->session_state = SESSION_STATE_CONNECTING;
1662 if ((rv = app_worker_connect_notify (app_wrk, quic_session,
1663 SESSION_E_NONE, ctx->client_opaque)))
1665 QUIC_ERR ("failed to notify app %d", rv);
1666 quic_proto_on_close (ctx_id, thread_index);
1670 /* If the app opens a stream in its callback it may invalidate ctx */
1671 ctx = quic_ctx_get (ctx_id, thread_index);
1673 * app_worker_connect_notify() might have reallocated pool, reload
1674 * quic_session pointer
1676 quic_session = session_get (ctx->c_s_index, thread_index);
1677 quic_session->session_state = SESSION_STATE_LISTENING;
1681 quic_check_quic_session_connected (quic_ctx_t * ctx)
1683 /* Called when we need to trigger quic session connected
1684 * we may call this function on the server side / at
1687 /* Conn may be set to null if the connection is terminated */
1688 if (!ctx->conn || ctx->conn_state != QUIC_CONN_STATE_HANDSHAKE)
1690 if (!quicly_connection_is_ready (ctx->conn))
1692 ctx->conn_state = QUIC_CONN_STATE_READY;
1693 if (!quicly_is_client (ctx->conn))
1695 quic_on_quic_session_connected (ctx);
1699 quic_update_conn_ctx (quicly_conn_t * conn, quicly_context_t * quicly_context)
1701 /* we need to update the quicly_conn on migrate
1702 * as it contains a pointer to the crypto context */
1703 ptls_context_t **tls;
1704 quicly_context_t **_quicly_context;
1705 _quicly_context = (quicly_context_t **) conn;
1706 *_quicly_context = quicly_context;
1707 tls = (ptls_context_t **) quicly_get_tls (conn);
1708 *tls = quicly_context->tls;
1712 quic_receive_connection (void *arg)
1714 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1715 quic_ctx_t *temp_ctx, *new_ctx;
1716 clib_bihash_kv_16_8_t kv;
1717 quicly_conn_t *conn;
1718 quicly_context_t *quicly_context;
1719 session_t *udp_session;
1722 new_ctx_id = quic_ctx_alloc (thread_index);
1723 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1725 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1728 clib_memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1729 clib_mem_free (temp_ctx);
1731 new_ctx->c_thread_index = thread_index;
1732 new_ctx->c_c_index = new_ctx_id;
1733 quic_acquire_crypto_context (new_ctx);
1735 conn = new_ctx->conn;
1736 quicly_context = quic_get_quicly_ctx_from_ctx (new_ctx);
1737 quic_update_conn_ctx (conn, quicly_context);
1739 quic_store_conn_ctx (conn, new_ctx);
1740 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1741 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1742 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1743 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1744 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1745 quic_update_timer (new_ctx);
1747 /* Trigger write on this connection if necessary */
1748 udp_session = session_get_from_handle (new_ctx->udp_session_handle);
1749 udp_session->opaque = new_ctx_id;
1750 udp_session->flags &= ~SESSION_F_IS_MIGRATING;
1751 if (svm_fifo_max_dequeue (udp_session->tx_fifo))
1752 quic_set_udp_tx_evt (udp_session);
1756 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1758 quic_ctx_t *ctx, *temp_ctx;
1759 u32 thread_index = vlib_get_thread_index ();
1761 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1763 temp_ctx = clib_mem_alloc (sizeof (quic_ctx_t));
1764 QUIC_ASSERT (temp_ctx != NULL);
1765 ctx = quic_ctx_get (ctx_index, thread_index);
1767 clib_memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1769 quic_stop_ctx_timer (ctx);
1770 quic_release_crypto_context (ctx->crypto_context_index, thread_index);
1771 quic_ctx_free (ctx);
1773 /* Send connection to destination thread */
1774 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1779 quic_udp_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1780 session_t * udp_session,
1781 session_error_t err)
1783 QUIC_DBG (2, "UDP Session is now connected (id %u)",
1784 udp_session->session_index);
1785 /* This should always be called before quic_connect returns since UDP always
1786 * connects instantly. */
1787 clib_bihash_kv_16_8_t kv;
1788 struct sockaddr_in6 sa6;
1789 struct sockaddr *sa = (struct sockaddr *) &sa6;
1791 transport_connection_t *tc;
1792 app_worker_t *app_wrk;
1793 quicly_conn_t *conn;
1797 quicly_context_t *quicly_ctx;
1799 /* Allocate session on whatever thread udp used, i.e., probably first
1800 * worker, although this may be main thread. If it is main, it's done
1801 * with a worker barrier */
1802 thread_index = udp_session->thread_index;
1803 ASSERT (thread_index == 0 || thread_index == 1);
1804 ctx = quic_ctx_get (ctx_index, thread_index);
1808 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1811 api_context = ctx->c_s_index;
1812 app_worker_connect_notify (app_wrk, 0, SESSION_E_NONE, api_context);
1817 ctx->c_thread_index = thread_index;
1818 ctx->c_c_index = ctx_index;
1820 QUIC_DBG (2, "New ctx [%u]%x", thread_index, (ctx) ? ctx_index : ~0);
1822 ctx->udp_session_handle = session_handle (udp_session);
1823 udp_session->opaque = ctx_index;
1825 /* Init QUIC lib connection
1826 * Generate required sockaddr & salen */
1827 tc = session_get_transport (udp_session);
1828 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1830 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1831 ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
1832 sa, NULL, &quic_main.wrk_ctx[thread_index].next_cid,
1833 ptls_iovec_init (NULL, 0), &quic_main.hs_properties,
1835 ++quic_main.wrk_ctx[thread_index].next_cid.master_id;
1836 /* Save context handle in quicly connection */
1837 quic_store_conn_ctx (ctx->conn, ctx);
1840 /* Register connection in connections map */
1842 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1843 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1844 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1845 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1847 quic_send_packets (ctx);
1853 quic_udp_session_disconnect_callback (session_t * s)
1855 clib_warning ("UDP session disconnected???");
1859 quic_udp_session_cleanup_callback (session_t * udp_session,
1860 session_cleanup_ntf_t ntf)
1864 if (ntf != SESSION_CLEANUP_SESSION)
1867 ctx = quic_ctx_get (udp_session->opaque, udp_session->thread_index);
1868 quic_stop_ctx_timer (ctx);
1869 quic_release_crypto_context (ctx->crypto_context_index,
1870 ctx->c_thread_index);
1871 quic_ctx_free (ctx);
1875 quic_udp_session_reset_callback (session_t * s)
1877 clib_warning ("UDP session reset???");
1881 quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
1883 u32 new_thread = session_thread_from_handle (new_sh);
1886 QUIC_DBG (2, "Session %x migrated to %lx", s->session_index, new_sh);
1887 QUIC_ASSERT (vlib_get_thread_index () == s->thread_index);
1888 ctx = quic_ctx_get (s->opaque, s->thread_index);
1889 QUIC_ASSERT (ctx->udp_session_handle == session_handle (s));
1891 ctx->udp_session_handle = new_sh;
1893 s->opaque = 0xfeedface;
1895 quic_transfer_connection (ctx->c_c_index, new_thread);
1899 quic_udp_session_accepted_callback (session_t * udp_session)
1901 /* New UDP connection, try to accept it */
1903 quic_ctx_t *ctx, *lctx;
1904 session_t *udp_listen_session;
1905 u32 thread_index = vlib_get_thread_index ();
1907 udp_listen_session =
1908 listen_session_get_from_handle (udp_session->listener_handle);
1910 ctx_index = quic_ctx_alloc (thread_index);
1911 ctx = quic_ctx_get (ctx_index, thread_index);
1912 ctx->c_thread_index = udp_session->thread_index;
1913 ctx->c_c_index = ctx_index;
1914 ctx->c_s_index = QUIC_SESSION_INVALID;
1915 ctx->udp_session_handle = session_handle (udp_session);
1916 QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
1917 ctx->listener_ctx_id = udp_listen_session->opaque;
1918 lctx = quic_ctx_get (udp_listen_session->opaque,
1919 udp_listen_session->thread_index);
1920 ctx->udp_is_ip4 = lctx->c_is_ip4;
1921 ctx->parent_app_id = lctx->parent_app_id;
1922 ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
1923 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1924 ctx->conn_state = QUIC_CONN_STATE_OPENED;
1925 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1927 ctx->crypto_engine = lctx->crypto_engine;
1928 ctx->ckpair_index = lctx->ckpair_index;
1929 quic_acquire_crypto_context (ctx);
1930 udp_session->opaque = ctx_index;
1932 /* TODO timeout to delete these if they never connect */
1937 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1939 /* No-op for builtin */
1944 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1946 /* No-op for builtin */
1951 quic_custom_app_rx_callback (transport_connection_t * tc)
1954 session_t *stream_session = session_get (tc->s_index, tc->thread_index);
1955 QUIC_DBG (3, "Received app READ notification");
1956 quic_ack_rx_data (stream_session);
1957 svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
1959 /* Need to send packets (acks may never be sent otherwise) */
1960 ctx = quic_ctx_get (stream_session->connection_index,
1961 stream_session->thread_index);
1962 quic_send_packets (ctx);
1967 quic_custom_tx_callback (void *s, transport_send_params_t * sp)
1969 session_t *stream_session = (session_t *) s;
1970 quic_stream_data_t *stream_data;
1971 quicly_stream_t *stream;
1977 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1979 ctx = quic_ctx_get (stream_session->connection_index,
1980 stream_session->thread_index);
1981 if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
1983 goto tx_end; /* Most probably a reschedule */
1986 QUIC_DBG (3, "Stream TX event");
1987 quic_ack_rx_data (stream_session);
1988 stream = ctx->stream;
1989 if (!quicly_sendstate_is_open (&stream->sendstate))
1991 QUIC_ERR ("Warning: tried to send on closed stream");
1995 stream_data = (quic_stream_data_t *) stream->data;
1996 max_deq = svm_fifo_max_dequeue (stream_session->tx_fifo);
1997 QUIC_ASSERT (max_deq >= stream_data->app_tx_data_len);
1998 if (max_deq == stream_data->app_tx_data_len)
2000 QUIC_DBG (3, "TX but no data %d / %d", max_deq,
2001 stream_data->app_tx_data_len);
2004 stream_data->app_tx_data_len = max_deq;
2005 rv = quicly_stream_sync_sendbuf (stream, 1);
2009 return quic_send_packets (ctx);
2013 * Returns 0 if a matching connection is found and is on the right thread.
2014 * Otherwise returns -1.
2015 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
2019 quic_find_packet_ctx (quic_rx_packet_ctx_t * pctx, u32 caller_thread_index)
2021 clib_bihash_kv_16_8_t kv;
2022 clib_bihash_16_8_t *h;
2024 u32 index, thread_id;
2026 h = &quic_main.connection_hash;
2027 quic_make_connection_key (&kv, &pctx->packet.cid.dest.plaintext);
2028 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
2030 if (clib_bihash_search_16_8 (h, &kv, &kv))
2032 QUIC_DBG (3, "connection not found");
2033 return QUIC_PACKET_TYPE_NONE;
2036 index = kv.value & UINT32_MAX;
2037 thread_id = kv.value >> 32;
2038 /* Check if this connection belongs to this thread, otherwise
2039 * ask for it to be moved */
2040 if (thread_id != caller_thread_index)
2042 QUIC_DBG (2, "Connection is on wrong thread");
2043 /* Cannot make full check with quicly_is_destination... */
2044 pctx->ctx_index = index;
2045 pctx->thread_index = thread_id;
2046 return QUIC_PACKET_TYPE_MIGRATE;
2048 ctx = quic_ctx_get (index, vlib_get_thread_index ());
2051 QUIC_ERR ("ctx has no conn");
2052 return QUIC_PACKET_TYPE_NONE;
2054 if (!quicly_is_destination (ctx->conn, NULL, &pctx->sa, &pctx->packet))
2055 return QUIC_PACKET_TYPE_NONE;
2057 QUIC_DBG (3, "Connection found");
2058 pctx->ctx_index = index;
2059 pctx->thread_index = thread_id;
2060 return QUIC_PACKET_TYPE_RECEIVE;
2064 quic_accept_connection (quic_rx_packet_ctx_t * pctx)
2066 quicly_context_t *quicly_ctx;
2067 session_t *quic_session;
2068 clib_bihash_kv_16_8_t kv;
2069 app_worker_t *app_wrk;
2070 quicly_conn_t *conn;
2075 /* new connection, accept and create context if packet is valid
2076 * TODO: check if socket is actually listening? */
2077 ctx = quic_ctx_get (pctx->ctx_index, pctx->thread_index);
2078 if (ctx->c_s_index != QUIC_SESSION_INVALID)
2080 QUIC_DBG (2, "already accepted ctx 0x%x", ctx->c_s_index);
2084 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
2085 if ((rv = quicly_accept (&conn, quicly_ctx, NULL, &pctx->sa,
2086 &pctx->packet, NULL,
2087 &quic_main.wrk_ctx[pctx->thread_index].next_cid,
2090 /* Invalid packet, pass */
2091 assert (conn == NULL);
2092 QUIC_ERR ("Accept failed with %U", quic_format_err, rv);
2093 /* TODO: cleanup created quic ctx and UDP session */
2096 assert (conn != NULL);
2098 ++quic_main.wrk_ctx[pctx->thread_index].next_cid.master_id;
2099 /* Save ctx handle in quicly connection */
2100 quic_store_conn_ctx (conn, ctx);
2103 quic_session = session_alloc (ctx->c_thread_index);
2104 QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
2105 session_handle (quic_session), ctx->c_c_index);
2106 quic_session->session_state = SESSION_STATE_LISTENING;
2107 ctx->c_s_index = quic_session->session_index;
2109 lctx = quic_ctx_get (ctx->listener_ctx_id, 0);
2111 quic_session->app_wrk_index = lctx->parent_app_wrk_id;
2112 quic_session->connection_index = ctx->c_c_index;
2113 quic_session->session_type =
2114 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
2115 quic_session->listener_handle = lctx->c_s_index;
2117 /* Register connection in connections map */
2118 quic_make_connection_key (&kv, quicly_get_master_id (conn));
2119 kv.value = ((u64) pctx->thread_index) << 32 | (u64) pctx->ctx_index;
2120 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
2121 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
2123 /* If notify fails, reset connection immediatly */
2124 if ((rv = app_worker_init_accepted (quic_session)))
2126 QUIC_ERR ("failed to allocate fifos");
2127 quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2131 app_wrk = app_worker_get (quic_session->app_wrk_index);
2132 if ((rv = app_worker_accept_notify (app_wrk, quic_session)))
2134 QUIC_ERR ("failed to notify accept worker app");
2135 quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2139 ctx->conn_state = QUIC_CONN_STATE_READY;
2143 quic_reset_connection (u64 udp_session_handle, quic_rx_packet_ctx_t * pctx)
2145 /* short header packet; potentially a dead connection. No need to check the
2146 * length of the incoming packet, because loop is prevented by authenticating
2147 * the CID (by checking node_id and thread_id). If the peer is also sending a
2148 * reset, then the next CID is highly likely to contain a non-authenticating
2150 QUIC_DBG (2, "Sending stateless reset");
2152 session_t *udp_session;
2153 quicly_context_t *quicly_ctx;
2154 if (pctx->packet.cid.dest.plaintext.node_id != 0
2155 || pctx->packet.cid.dest.plaintext.thread_id != 0)
2157 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2158 quic_ctx_t *qctx = quic_ctx_get (pctx->ctx_index, pctx->thread_index);
2160 quicly_address_t src;
2161 uint8_t payload[quicly_ctx->transport_params.max_udp_payload_size];
2162 size_t payload_len =
2163 quicly_send_stateless_reset (quicly_ctx, &src.sa, payload);
2164 if (payload_len == 0)
2167 struct iovec packet;
2168 packet.iov_len = payload_len;
2169 packet.iov_base = payload;
2171 struct _st_quicly_conn_public_t *conn =
2172 (struct _st_quicly_conn_public_t *) qctx->conn;
2174 udp_session = session_get_from_handle (udp_session_handle);
2175 rv = quic_send_datagram (udp_session, &packet, &conn->remote.address,
2176 &conn->local.address);
2177 quic_set_udp_tx_evt (udp_session);
2182 quic_process_one_rx_packet (u64 udp_session_handle, svm_fifo_t * f,
2183 u32 fifo_offset, quic_rx_packet_ctx_t * pctx)
2187 u32 thread_index = vlib_get_thread_index ();
2188 u32 cur_deq = svm_fifo_max_dequeue (f) - fifo_offset;
2189 quicly_context_t *quicly_ctx;
2190 session_t *udp_session;
2193 ret = svm_fifo_peek (f, fifo_offset,
2194 SESSION_CONN_HDR_LEN, (u8 *) & pctx->ph);
2195 QUIC_ASSERT (ret == SESSION_CONN_HDR_LEN);
2196 QUIC_ASSERT (pctx->ph.data_offset == 0);
2197 full_len = pctx->ph.data_length + SESSION_CONN_HDR_LEN;
2198 if (full_len > cur_deq)
2200 QUIC_ERR ("Not enough data in fifo RX");
2204 /* Quicly can read len bytes from the fifo at offset:
2205 * ph.data_offset + SESSION_CONN_HDR_LEN */
2206 ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN + fifo_offset,
2207 pctx->ph.data_length, pctx->data);
2208 if (ret != pctx->ph.data_length)
2210 QUIC_ERR ("Not enough data peeked in RX");
2214 quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
2215 quic_build_sockaddr (&pctx->sa, &pctx->salen, &pctx->ph.rmt_ip,
2216 pctx->ph.rmt_port, pctx->ph.is_ip4);
2217 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2220 plen = quicly_decode_packet (quicly_ctx, &pctx->packet, pctx->data,
2221 pctx->ph.data_length, &off);
2223 if (plen == SIZE_MAX)
2228 rv = quic_find_packet_ctx (pctx, thread_index);
2229 if (rv == QUIC_PACKET_TYPE_RECEIVE)
2231 pctx->ptype = QUIC_PACKET_TYPE_RECEIVE;
2233 if (quic_main.vnet_crypto_enabled &&
2234 quic_main.default_crypto_engine == CRYPTO_ENGINE_VPP)
2236 quic_ctx_t *qctx = quic_ctx_get (pctx->ctx_index, thread_index);
2237 quic_crypto_decrypt_packet (qctx, pctx);
2241 else if (rv == QUIC_PACKET_TYPE_MIGRATE)
2243 pctx->ptype = QUIC_PACKET_TYPE_MIGRATE;
2244 /* Connection found but on wrong thread, ask move */
2246 else if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
2248 pctx->ptype = QUIC_PACKET_TYPE_ACCEPT;
2249 udp_session = session_get_from_handle (udp_session_handle);
2250 pctx->ctx_index = udp_session->opaque;
2251 pctx->thread_index = thread_index;
2255 pctx->ptype = QUIC_PACKET_TYPE_RESET;
2261 quic_udp_session_rx_callback (session_t * udp_session)
2263 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
2264 quic_ctx_t *ctx = NULL, *prev_ctx = NULL;
2265 svm_fifo_t *f = udp_session->rx_fifo;
2267 u64 udp_session_handle = session_handle (udp_session);
2269 u32 thread_index = vlib_get_thread_index ();
2270 u32 cur_deq, fifo_offset, max_packets, i;
2272 quic_rx_packet_ctx_t packets_ctx[QUIC_RCV_MAX_PACKETS];
2274 if (udp_session->flags & SESSION_F_IS_MIGRATING)
2276 QUIC_DBG (3, "RX on migrating udp session");
2281 max_deq = svm_fifo_max_dequeue (f);
2286 max_packets = QUIC_RCV_MAX_PACKETS;
2289 clib_memset (packets_ctx, 0xfa,
2290 QUIC_RCV_MAX_PACKETS * sizeof (quic_rx_packet_ctx_t));
2292 for (i = 0; i < max_packets; i++)
2294 packets_ctx[i].thread_index = UINT32_MAX;
2295 packets_ctx[i].ctx_index = UINT32_MAX;
2296 packets_ctx[i].ptype = QUIC_PACKET_TYPE_DROP;
2298 cur_deq = max_deq - fifo_offset;
2301 max_packets = i + 1;
2304 if (cur_deq < SESSION_CONN_HDR_LEN)
2306 fifo_offset = max_deq;
2307 max_packets = i + 1;
2308 QUIC_ERR ("Fifo %d < header size in RX", cur_deq);
2311 rv = quic_process_one_rx_packet (udp_session_handle, f,
2312 fifo_offset, &packets_ctx[i]);
2313 if (packets_ctx[i].ptype != QUIC_PACKET_TYPE_MIGRATE)
2314 fifo_offset += SESSION_CONN_HDR_LEN + packets_ctx[i].ph.data_length;
2317 max_packets = i + 1;
2322 for (i = 0; i < max_packets; i++)
2324 switch (packets_ctx[i].ptype)
2326 case QUIC_PACKET_TYPE_RECEIVE:
2327 ctx = quic_ctx_get (packets_ctx[i].ctx_index, thread_index);
2328 rv = quicly_receive (ctx->conn, NULL, &packets_ctx[i].sa,
2329 &packets_ctx[i].packet);
2330 if (rv && rv != QUICLY_ERROR_PACKET_IGNORED)
2332 QUIC_ERR ("quicly_receive return error %U",
2333 quic_format_err, rv);
2336 case QUIC_PACKET_TYPE_ACCEPT:
2337 quic_accept_connection (&packets_ctx[i]);
2339 case QUIC_PACKET_TYPE_RESET:
2340 quic_reset_connection (udp_session_handle, &packets_ctx[i]);
2344 ctx = prev_ctx = NULL;
2345 for (i = 0; i < max_packets; i++)
2348 switch (packets_ctx[i].ptype)
2350 case QUIC_PACKET_TYPE_RECEIVE:
2351 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2352 packets_ctx[i].thread_index);
2353 quic_check_quic_session_connected (ctx);
2354 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2355 packets_ctx[i].thread_index);
2357 case QUIC_PACKET_TYPE_ACCEPT:
2358 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2359 packets_ctx[i].thread_index);
2362 continue; /* this exits the for loop since other packet types are
2363 necessarily the last in the batch */
2365 if (ctx != prev_ctx)
2366 quic_send_packets (ctx);
2369 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
2370 f = udp_session->rx_fifo;
2371 svm_fifo_dequeue_drop (f, fifo_offset);
2373 if (svm_fifo_max_dequeue (f))
2380 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2381 transport_endpoint_t * tep, u8 is_lcl)
2383 session_t *udp_session;
2384 if (!quic_ctx_is_stream (ctx))
2386 udp_session = session_get_from_handle (ctx->udp_session_handle);
2387 session_get_endpoint (udp_session, tep, is_lcl);
2392 quic_get_transport_listener_endpoint (u32 listener_index,
2393 transport_endpoint_t * tep, u8 is_lcl)
2396 app_listener_t *app_listener;
2397 session_t *udp_listen_session;
2398 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2399 if (quic_ctx_is_listener (ctx))
2401 app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2402 udp_listen_session = app_listener_get_session (app_listener);
2403 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2405 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2409 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2410 transport_endpoint_t * tep, u8 is_lcl)
2413 ctx = quic_ctx_get (ctx_index, thread_index);
2414 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2418 static session_cb_vft_t quic_app_cb_vft = {
2419 .session_accept_callback = quic_udp_session_accepted_callback,
2420 .session_disconnect_callback = quic_udp_session_disconnect_callback,
2421 .session_connected_callback = quic_udp_session_connected_callback,
2422 .session_reset_callback = quic_udp_session_reset_callback,
2423 .session_migrate_callback = quic_udp_session_migrate_callback,
2424 .add_segment_callback = quic_add_segment_callback,
2425 .del_segment_callback = quic_del_segment_callback,
2426 .builtin_app_rx_callback = quic_udp_session_rx_callback,
2427 .session_cleanup_callback = quic_udp_session_cleanup_callback,
2428 .app_cert_key_pair_delete_callback = quic_app_cert_key_pair_delete_callback,
2431 static const transport_proto_vft_t quic_proto = {
2432 .connect = quic_connect,
2433 .close = quic_proto_on_close,
2434 .start_listen = quic_start_listen,
2435 .stop_listen = quic_stop_listen,
2436 .get_connection = quic_connection_get,
2437 .get_listener = quic_listener_get,
2438 .update_time = quic_update_time,
2439 .app_rx_evt = quic_custom_app_rx_callback,
2440 .custom_tx = quic_custom_tx_callback,
2441 .format_connection = format_quic_connection,
2442 .format_half_open = format_quic_half_open,
2443 .format_listener = format_quic_listener,
2444 .get_transport_endpoint = quic_get_transport_endpoint,
2445 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2446 .transport_options = {
2449 .tx_type = TRANSPORT_TX_INTERNAL,
2450 .service_type = TRANSPORT_SERVICE_APP,
2455 static quicly_stream_open_t on_stream_open = { quic_on_stream_open };
2456 static quicly_closed_by_remote_t on_closed_by_remote = {
2457 quic_on_closed_by_remote
2459 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
2462 quic_register_cipher_suite (crypto_engine_type_t type,
2463 ptls_cipher_suite_t ** ciphers)
2465 quic_main_t *qm = &quic_main;
2466 vec_validate (qm->quic_ciphers, type);
2467 clib_bitmap_set (qm->available_crypto_engines, type, 1);
2468 qm->quic_ciphers[type] = ciphers;
2472 quic_update_fifo_size ()
2474 quic_main_t *qm = &quic_main;
2475 segment_manager_props_t *seg_mgr_props =
2476 application_get_segment_manager_properties (qm->app_index);
2481 ("error while getting segment_manager_props_t, can't update fifo-size");
2485 seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
2486 seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
2489 static clib_error_t *
2490 quic_init (vlib_main_t * vm)
2492 u32 segment_size = 256 << 20;
2493 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2494 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2495 vnet_app_attach_args_t _a, *a = &_a;
2496 u64 options[APP_OPTIONS_N_OPTIONS];
2497 quic_main_t *qm = &quic_main;
2500 num_threads = 1 /* main thread */ + vtm->n_threads;
2502 clib_memset (a, 0, sizeof (*a));
2503 clib_memset (options, 0, sizeof (options));
2505 a->session_cb_vft = &quic_app_cb_vft;
2506 a->api_client_index = APP_INVALID_INDEX;
2507 a->options = options;
2508 a->name = format (0, "quic");
2509 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2510 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2511 a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
2512 a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
2513 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
2514 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2515 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2516 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2518 if (vnet_application_attach (a))
2520 clib_warning ("failed to attach quic app");
2521 return clib_error_return (0, "failed to attach quic app");
2524 vec_validate (qm->ctx_pool, num_threads - 1);
2525 vec_validate (qm->wrk_ctx, num_threads - 1);
2527 for (i = 0; i < num_threads; i++)
2529 qm->wrk_ctx[i].next_cid.thread_id = i;
2530 tw = &qm->wrk_ctx[i].timer_wheel;
2531 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2532 1e-3 /* timer period 1ms */ , ~0);
2533 tw->last_run_time = vlib_time_now (vlib_get_main ());
2534 clib_bihash_init_24_8 (&qm->wrk_ctx[i].crypto_context_hash,
2535 "quic crypto contexts", 64, 128 << 10);
2538 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2541 qm->app_index = a->app_index;
2542 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2543 / QUIC_TSTAMP_RESOLUTION;
2544 qm->session_cache.super.cb = quic_encrypt_ticket_cb;
2546 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2547 FIB_PROTOCOL_IP4, ~0);
2548 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2549 FIB_PROTOCOL_IP6, ~0);
2551 clib_bitmap_alloc (qm->available_crypto_engines,
2552 app_crypto_engine_n_types ());
2553 quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
2554 ptls_openssl_cipher_suites);
2555 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2557 vnet_crypto_main_t *cm = &crypto_main;
2558 if (vec_len (cm->engines) == 0)
2559 qm->vnet_crypto_enabled = 0;
2561 qm->vnet_crypto_enabled = 1;
2562 if (qm->vnet_crypto_enabled == 1)
2564 quic_register_cipher_suite (CRYPTO_ENGINE_VPP,
2565 quic_crypto_cipher_suites);
2566 qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2569 qm->max_packets_per_key = DEFAULT_MAX_PACKETS_PER_KEY;
2570 clib_rwlock_init (&qm->crypto_keys_quic_rw_lock);
2572 qm->default_quic_cc = QUIC_CC_RENO;
2578 VLIB_INIT_FUNCTION (quic_init);
2580 static clib_error_t *
2581 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2582 unformat_input_t * input,
2583 vlib_cli_command_t * cmd)
2585 unformat_input_t _line_input, *line_input = &_line_input;
2586 quic_main_t *qm = &quic_main;
2587 clib_error_t *e = 0;
2589 if (!unformat_user (input, unformat_line_input, line_input))
2592 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2594 if (unformat (line_input, "vpp"))
2595 qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2596 else if (unformat (line_input, "picotls"))
2597 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2600 e = clib_error_return (0, "unknown input '%U'",
2601 format_unformat_error, line_input);
2606 unformat_free (line_input);
2610 u64 quic_fifosize = 0;
2611 static clib_error_t *
2612 quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
2613 unformat_input_t * input,
2614 vlib_cli_command_t * cmd)
2616 quic_main_t *qm = &quic_main;
2617 unformat_input_t _line_input, *line_input = &_line_input;
2620 if (!unformat_user (input, unformat_line_input, line_input))
2623 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2625 if (unformat (line_input, "%U", unformat_memory_size, &tmp))
2627 if (tmp >= 0x100000000ULL)
2629 return clib_error_return
2630 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
2632 qm->udp_fifo_size = tmp;
2633 quic_update_fifo_size ();
2636 return clib_error_return (0, "unknown input '%U'",
2637 format_unformat_error, line_input);
2644 quic_get_counter_value (u32 event_code)
2648 vlib_error_main_t *em;
2654 vm = vlib_get_main ();
2655 em = &vm->error_main;
2656 n = vlib_get_node (vm, quic_input_node.index);
2658 foreach_vlib_main ()
2660 em = &this_vlib_main->error_main;
2661 i = n->error_heap_index + code;
2662 c = em->counters[i];
2664 if (i < vec_len (em->counters_last_clear))
2665 c -= em->counters_last_clear[i];
2673 quic_show_aggregated_stats (vlib_main_t * vm)
2675 u32 num_workers = vlib_num_workers ();
2676 quic_main_t *qm = &quic_main;
2677 quic_ctx_t *ctx = NULL;
2678 quicly_stats_t st, agg_stats;
2679 u32 i, nconn = 0, nstream = 0;
2681 clib_memset (&agg_stats, 0, sizeof (agg_stats));
2682 for (i = 0; i < num_workers + 1; i++)
2685 pool_foreach (ctx, qm->ctx_pool[i])
2687 if (quic_ctx_is_conn (ctx) && ctx->conn)
2689 quicly_get_stats (ctx->conn, &st);
2690 agg_stats.rtt.smoothed += st.rtt.smoothed;
2691 agg_stats.rtt.minimum += st.rtt.minimum;
2692 agg_stats.rtt.variance += st.rtt.variance;
2693 agg_stats.num_packets.received += st.num_packets.received;
2694 agg_stats.num_packets.sent += st.num_packets.sent;
2695 agg_stats.num_packets.lost += st.num_packets.lost;
2696 agg_stats.num_packets.ack_received += st.num_packets.ack_received;
2697 agg_stats.num_bytes.received += st.num_bytes.received;
2698 agg_stats.num_bytes.sent += st.num_bytes.sent;
2701 else if (quic_ctx_is_stream (ctx))
2706 vlib_cli_output (vm, "-------- Connections --------");
2707 vlib_cli_output (vm, "Current: %u", nconn);
2708 vlib_cli_output (vm, "Opened: %d",
2709 quic_get_counter_value (QUIC_ERROR_OPENED_CONNECTION));
2710 vlib_cli_output (vm, "Closed: %d",
2711 quic_get_counter_value (QUIC_ERROR_CLOSED_CONNECTION));
2712 vlib_cli_output (vm, "---------- Streams ----------");
2713 vlib_cli_output (vm, "Current: %u", nstream);
2714 vlib_cli_output (vm, "Opened: %d",
2715 quic_get_counter_value (QUIC_ERROR_OPENED_STREAM));
2716 vlib_cli_output (vm, "Closed: %d",
2717 quic_get_counter_value (QUIC_ERROR_CLOSED_STREAM));
2718 vlib_cli_output (vm, "---------- Packets ----------");
2719 vlib_cli_output (vm, "RX Total: %d",
2720 quic_get_counter_value (QUIC_ERROR_RX_PACKETS));
2721 vlib_cli_output (vm, "RX 0RTT: %d",
2722 quic_get_counter_value (QUIC_ERROR_ZERO_RTT_RX_PACKETS));
2723 vlib_cli_output (vm, "RX 1RTT: %d",
2724 quic_get_counter_value (QUIC_ERROR_ONE_RTT_RX_PACKETS));
2725 vlib_cli_output (vm, "TX Total: %d",
2726 quic_get_counter_value (QUIC_ERROR_TX_PACKETS));
2727 vlib_cli_output (vm, "----------- Stats -----------");
2728 vlib_cli_output (vm, "Min RTT %f",
2729 nconn > 0 ? agg_stats.rtt.minimum / nconn : 0);
2730 vlib_cli_output (vm, "Smoothed RTT %f",
2731 nconn > 0 ? agg_stats.rtt.smoothed / nconn : 0);
2732 vlib_cli_output (vm, "Variance on RTT %f",
2733 nconn > 0 ? agg_stats.rtt.variance / nconn : 0);
2734 vlib_cli_output (vm, "Packets Received %lu",
2735 agg_stats.num_packets.received);
2736 vlib_cli_output (vm, "Packets Sent %lu", agg_stats.num_packets.sent);
2737 vlib_cli_output (vm, "Packets Lost %lu", agg_stats.num_packets.lost);
2738 vlib_cli_output (vm, "Packets Acks %lu",
2739 agg_stats.num_packets.ack_received);
2740 vlib_cli_output (vm, "RX bytes %lu", agg_stats.num_bytes.received);
2741 vlib_cli_output (vm, "TX bytes %lu", agg_stats.num_bytes.sent);
2745 quic_format_quicly_conn_id (u8 * s, va_list * args)
2747 quicly_cid_plaintext_t *mid = va_arg (*args, quicly_cid_plaintext_t *);
2748 s = format (s, "C%x_%x", mid->master_id, mid->thread_id);
2753 quic_format_quicly_stream_id (u8 * s, va_list * args)
2755 quicly_stream_t *stream = va_arg (*args, quicly_stream_t *);
2757 format (s, "%U S%lx", quic_format_quicly_conn_id,
2758 quicly_get_master_id (stream->conn), stream->stream_id);
2763 quic_format_listener_ctx (u8 * s, va_list * args)
2765 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2766 s = format (s, "[#%d][%x][Listener]", ctx->c_thread_index, ctx->c_c_index);
2771 quic_format_connection_ctx (u8 * s, va_list * args)
2773 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2774 quicly_stats_t quicly_stats;
2776 s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2780 s = format (s, "- no conn -\n");
2783 s = format (s, "[%U]",
2784 quic_format_quicly_conn_id, quicly_get_master_id (ctx->conn));
2785 quicly_get_stats (ctx->conn, &quicly_stats);
2787 s = format (s, "[RTT >%3d, ~%3d, V%3d, last %3d]",
2788 quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
2789 quicly_stats.rtt.variance, quicly_stats.rtt.latest);
2790 s = format (s, " TX:%d RX:%d loss:%d ack:%d",
2791 quicly_stats.num_packets.sent,
2792 quicly_stats.num_packets.received,
2793 quicly_stats.num_packets.lost,
2794 quicly_stats.num_packets.ack_received);
2796 format (s, "\ncwnd:%u ssthresh:%u recovery_end:%lu", quicly_stats.cc.cwnd,
2797 quicly_stats.cc.ssthresh, quicly_stats.cc.recovery_end);
2799 quicly_context_t *quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
2800 if (quicly_ctx->init_cc == &quicly_cc_cubic_init)
2804 "\nk:%d w_max:%u w_last_max:%u avoidance_start:%ld last_sent_time:%ld",
2805 quicly_stats.cc.state.cubic.k, quicly_stats.cc.state.cubic.w_max,
2806 quicly_stats.cc.state.cubic.w_last_max,
2807 quicly_stats.cc.state.cubic.avoidance_start,
2808 quicly_stats.cc.state.cubic.last_sent_time);
2810 else if (quicly_ctx->init_cc == &quicly_cc_reno_init)
2812 s = format (s, " stash:%u", quicly_stats.cc.state.reno.stash);
2819 quic_format_stream_ctx (u8 * s, va_list * args)
2821 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2822 session_t *stream_session;
2823 quicly_stream_t *stream = ctx->stream;
2826 s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2827 s = format (s, "[%U]", quic_format_quicly_stream_id, stream);
2829 stream_session = session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
2830 if (!stream_session)
2832 s = format (s, "- no session -\n");
2835 txs = svm_fifo_max_dequeue (stream_session->tx_fifo);
2836 rxs = svm_fifo_max_dequeue (stream_session->rx_fifo);
2837 s = format (s, "[rx %d tx %d]\n", rxs, txs);
2841 static clib_error_t *
2842 quic_show_connections_command_fn (vlib_main_t * vm,
2843 unformat_input_t * input,
2844 vlib_cli_command_t * cmd)
2846 unformat_input_t _line_input, *line_input = &_line_input;
2847 u8 show_listeners = 0, show_conn = 0, show_stream = 0;
2848 u32 num_workers = vlib_num_workers ();
2849 quic_main_t *qm = &quic_main;
2850 clib_error_t *error = 0;
2851 quic_ctx_t *ctx = NULL;
2853 session_cli_return_if_not_enabled ();
2855 if (!unformat_user (input, unformat_line_input, line_input))
2857 quic_show_aggregated_stats (vm);
2861 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2863 if (unformat (line_input, "listener"))
2865 else if (unformat (line_input, "conn"))
2867 else if (unformat (line_input, "stream"))
2871 error = clib_error_return (0, "unknown input `%U'",
2872 format_unformat_error, line_input);
2877 for (int i = 0; i < num_workers + 1; i++)
2880 pool_foreach (ctx, qm->ctx_pool[i])
2882 if (quic_ctx_is_stream (ctx) && show_stream)
2883 vlib_cli_output (vm, "%U", quic_format_stream_ctx, ctx);
2884 else if (quic_ctx_is_listener (ctx) && show_listeners)
2885 vlib_cli_output (vm, "%U", quic_format_listener_ctx, ctx);
2886 else if (quic_ctx_is_conn (ctx) && show_conn)
2887 vlib_cli_output (vm, "%U", quic_format_connection_ctx, ctx);
2893 unformat_free (line_input);
2898 VLIB_CLI_COMMAND (quic_plugin_crypto_command, static) = {
2899 .path = "quic set crypto api",
2900 .short_help = "quic set crypto api [picotls|vpp]",
2901 .function = quic_plugin_crypto_command_fn,
2903 VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
2905 .path = "quic set fifo-size",
2906 .short_help = "quic set fifo-size N[K|M|G] (default 64K)",
2907 .function = quic_plugin_set_fifo_size_command_fn,
2909 VLIB_CLI_COMMAND(quic_show_ctx_command, static)=
2911 .path = "show quic",
2912 .short_help = "show quic",
2913 .function = quic_show_connections_command_fn,
2915 VLIB_CLI_COMMAND (quic_list_crypto_context_command, static) =
2917 .path = "show quic crypto context",
2918 .short_help = "list quic crypto contextes",
2919 .function = quic_list_crypto_context_command_fn,
2921 VLIB_CLI_COMMAND (quic_set_max_packets_per_key, static) =
2923 .path = "set quic max_packets_per_key",
2924 .short_help = "set quic max_packets_per_key 16777216",
2925 .function = quic_set_max_packets_per_key_fn,
2927 VLIB_CLI_COMMAND (quic_set_cc, static) = {
2928 .path = "set quic cc",
2929 .short_help = "set quic cc [reno|cubic]",
2930 .function = quic_set_cc_fn,
2932 VLIB_PLUGIN_REGISTER () =
2934 .version = VPP_BUILD_VER,
2935 .description = "Quic transport protocol",
2936 .default_disabled = 1,
2940 static clib_error_t *
2941 quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
2943 unformat_input_t _line_input, *line_input = &_line_input;
2944 quic_main_t *qm = &quic_main;
2945 clib_error_t *error = 0;
2949 qm->udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
2950 qm->udp_fifo_prealloc = 0;
2951 qm->connection_timeout = QUIC_DEFAULT_CONN_TIMEOUT;
2953 if (!unformat_user (input, unformat_line_input, line_input))
2956 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2958 if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
2960 if (tmp >= 0x100000000ULL)
2962 error = clib_error_return (0,
2963 "fifo-size %llu (0x%llx) too large",
2967 qm->udp_fifo_size = tmp;
2969 else if (unformat (input, "conn-timeout %u", &i))
2970 qm->connection_timeout = i;
2971 else if (unformat (input, "fifo-prealloc %u", &i))
2972 qm->udp_fifo_prealloc = i;
2975 error = clib_error_return (0, "unknown input '%U'",
2976 format_unformat_error, line_input);
2981 unformat_free (line_input);
2985 VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
2988 quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
2989 vlib_frame_t * frame)
2995 VLIB_REGISTER_NODE (quic_input_node) =
2997 .function = quic_node_fn,
2998 .name = "quic-input",
2999 .vector_size = sizeof (u32),
3000 .type = VLIB_NODE_TYPE_INTERNAL,
3001 .n_errors = ARRAY_LEN (quic_error_strings),
3002 .error_strings = quic_error_strings,
3007 * fd.io coding-style-patch-verification: ON
3010 * eval: (c-set-style "gnu")