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>
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_peer_t on_closed_by_peer;
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);
94 static quicly_datagram_t *
95 quic_alloc_packet (quicly_packet_allocator_t * self, size_t payloadsize)
97 quicly_datagram_t *packet;
99 clib_mem_alloc (sizeof (*packet) + payloadsize +
100 sizeof (quic_encrypt_cb_ctx))) == NULL)
103 (uint8_t *) packet + sizeof (*packet) + sizeof (quic_encrypt_cb_ctx);
104 quic_encrypt_cb_ctx *encrypt_cb_ctx =
105 (quic_encrypt_cb_ctx *) ((uint8_t *) packet + sizeof (*packet));
107 clib_memset (encrypt_cb_ctx, 0, sizeof (*encrypt_cb_ctx));
112 quic_free_packet (quicly_packet_allocator_t * self,
113 quicly_datagram_t * packet)
115 clib_mem_free (packet);
118 quicly_packet_allocator_t quic_packet_allocator =
119 { quic_alloc_packet, quic_free_packet };
122 quic_app_cert_key_pair_delete_callback (app_cert_key_pair_t * ckpair)
124 quic_main_t *qm = &quic_main;
125 crypto_context_t *crctx;
126 clib_bihash_kv_24_8_t kv;
127 vlib_thread_main_t *vtm = vlib_get_thread_main ();
128 int num_threads = 1 /* main thread */ + vtm->n_threads;
131 for (i = 0; i < num_threads; i++)
134 pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
135 if (crctx->ckpair_index == ckpair->cert_key_index)
137 quic_crypto_context_make_key_from_crctx (&kv, crctx);
138 clib_bihash_add_del_24_8 (&qm->wrk_ctx[i].crypto_context_hash, &kv, 0 /* is_add */ );
146 static crypto_context_t *
147 quic_crypto_context_alloc (u8 thread_index)
149 quic_main_t *qm = &quic_main;
150 crypto_context_t *crctx;
153 pool_get (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
154 clib_memset (crctx, 0, sizeof (*crctx));
155 idx = (crctx - qm->wrk_ctx[thread_index].crypto_ctx_pool);
156 crctx->ctx_index = ((u32) thread_index) << 24 | idx;
161 static crypto_context_t *
162 quic_crypto_context_get (u32 cr_index, u32 thread_index)
164 quic_main_t *qm = &quic_main;
165 ASSERT (cr_index >> 24 == thread_index);
166 return pool_elt_at_index (qm->wrk_ctx[thread_index].crypto_ctx_pool,
167 cr_index & 0x00ffffff);
170 static clib_error_t *
171 quic_list_crypto_context_command_fn (vlib_main_t * vm,
172 unformat_input_t * input,
173 vlib_cli_command_t * cmd)
175 quic_main_t *qm = &quic_main;
176 crypto_context_t *crctx;
177 vlib_thread_main_t *vtm = vlib_get_thread_main ();
178 int i, num_threads = 1 /* main thread */ + vtm->n_threads;
179 for (i = 0; i < num_threads; i++)
182 pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
183 vlib_cli_output (vm, "[%d][Q]%U", i, format_crypto_context, crctx);
190 static clib_error_t *
191 quic_set_max_packets_per_key_fn (vlib_main_t * vm,
192 unformat_input_t * input,
193 vlib_cli_command_t * cmd)
195 quic_main_t *qm = &quic_main;
196 unformat_input_t _line_input, *line_input = &_line_input;
199 if (!unformat_user (input, unformat_line_input, line_input))
202 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
204 if (unformat (line_input, "%U", unformat_memory_size, &tmp))
206 qm->max_packets_per_key = tmp;
209 return clib_error_return (0, "unknown input '%U'",
210 format_unformat_error, line_input);
217 quic_release_crypto_context (u32 crypto_context_index, u8 thread_index)
219 crypto_context_t *crctx;
220 crctx = quic_crypto_context_get (crypto_context_index, thread_index);
221 crctx->n_subscribers--;
222 quic_crypto_context_free_if_needed (crctx, thread_index);
226 quic_init_crypto_context (crypto_context_t * crctx, quic_ctx_t * ctx)
228 quic_main_t *qm = &quic_main;
229 quicly_context_t *quicly_ctx;
230 ptls_iovec_t key_vec;
231 app_cert_key_pair_t *ckpair;
233 quic_crypto_context_data_t *data;
234 ptls_context_t *ptls_ctx;
236 QUIC_DBG (2, "Init quic crctx %d thread %d", crctx->ctx_index,
237 ctx->c_thread_index);
239 data = clib_mem_alloc (sizeof (*data));
240 /* picotls depends on data being zeroed */
241 clib_memset (data, 0, sizeof (*data));
242 crctx->data = (void *) data;
243 quicly_ctx = &data->quicly_ctx;
244 ptls_ctx = &data->ptls_ctx;
246 ptls_ctx->random_bytes = ptls_openssl_random_bytes;
247 ptls_ctx->get_time = &ptls_get_time;
248 ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
249 ptls_ctx->cipher_suites = qm->quic_ciphers[ctx->crypto_engine];
250 ptls_ctx->certificates.list = NULL;
251 ptls_ctx->certificates.count = 0;
252 ptls_ctx->esni = NULL;
253 ptls_ctx->on_client_hello = NULL;
254 ptls_ctx->emit_certificate = NULL;
255 ptls_ctx->sign_certificate = NULL;
256 ptls_ctx->verify_certificate = NULL;
257 ptls_ctx->ticket_lifetime = 86400;
258 ptls_ctx->max_early_data_size = 8192;
259 ptls_ctx->hkdf_label_prefix__obsolete = NULL;
260 ptls_ctx->require_dhe_on_psk = 1;
261 ptls_ctx->encrypt_ticket = &qm->session_cache.super;
262 clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
264 quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
265 quicly_ctx->max_packets_per_key = qm->max_packets_per_key;
266 quicly_ctx->tls = ptls_ctx;
267 quicly_ctx->stream_open = &on_stream_open;
268 quicly_ctx->closed_by_peer = &on_closed_by_peer;
269 quicly_ctx->now = &quicly_vpp_now_cb;
270 quicly_amend_ptls_context (quicly_ctx->tls);
272 quicly_ctx->packet_allocator = &quic_packet_allocator;
273 quicly_ctx->crypto_engine = &quic_crypto_engine;
274 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
275 quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
276 quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
277 quicly_ctx->transport_params.max_idle_timeout = qm->connection_timeout;
279 app = application_get (ctx->parent_app_id);
280 quicly_ctx->transport_params.max_stream_data.bidi_local =
281 app->sm_properties.rx_fifo_size - 1;
282 quicly_ctx->transport_params.max_stream_data.bidi_remote =
283 app->sm_properties.tx_fifo_size - 1;
284 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
286 if (!app->quic_iv_set)
288 ptls_openssl_random_bytes (app->quic_iv, QUIC_IV_LEN - 1);
289 app->quic_iv[QUIC_IV_LEN - 1] = 0;
290 app->quic_iv_set = 1;
293 clib_memcpy (data->cid_key, app->quic_iv, QUIC_IV_LEN);
294 key_vec = ptls_iovec_init (data->cid_key, QUIC_IV_LEN);
295 quicly_ctx->cid_encryptor =
296 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
297 &ptls_openssl_aes128ecb,
298 &ptls_openssl_sha256, key_vec);
300 ckpair = app_cert_key_pair_get_if_valid (crctx->ckpair_index);
301 if (!ckpair || !ckpair->key || !ckpair->cert)
303 QUIC_DBG (1, "Wrong ckpair id %d\n", crctx->ckpair_index);
306 if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
308 QUIC_DBG (1, "failed to read private key from app configuration\n");
311 if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
313 QUIC_DBG (1, "failed to load certificate\n");
321 quic_acquire_crypto_context (quic_ctx_t * ctx)
323 quic_main_t *qm = &quic_main;
324 crypto_context_t *crctx;
325 clib_bihash_kv_24_8_t kv;
327 if (ctx->crypto_engine == CRYPTO_ENGINE_NONE)
329 QUIC_DBG (2, "No crypto engine specified, using %d",
330 qm->default_crypto_engine);
331 ctx->crypto_engine = qm->default_crypto_engine;
333 if (!clib_bitmap_get (qm->available_crypto_engines, ctx->crypto_engine))
335 QUIC_DBG (1, "Quic does not support crypto engine %d",
337 return VNET_API_ERROR_MISSING_CERT_KEY;
340 /* Check for exisiting crypto ctx */
341 quic_crypto_context_make_key_from_ctx (&kv, ctx);
342 if (clib_bihash_search_24_8
343 (&qm->wrk_ctx[ctx->c_thread_index].crypto_context_hash, &kv, &kv) == 0)
345 crctx = quic_crypto_context_get (kv.value, ctx->c_thread_index);
346 QUIC_DBG (2, "Found exisiting crypto context %d", kv.value);
347 ctx->crypto_context_index = kv.value;
348 crctx->n_subscribers++;
352 crctx = quic_crypto_context_alloc (ctx->c_thread_index);
353 ctx->crypto_context_index = crctx->ctx_index;
354 kv.value = crctx->ctx_index;
355 crctx->crypto_engine = ctx->crypto_engine;
356 crctx->ckpair_index = ctx->ckpair_index;
357 if (quic_init_crypto_context (crctx, ctx))
359 if (vnet_app_add_cert_key_interest (ctx->ckpair_index, qm->app_index))
361 crctx->n_subscribers++;
362 clib_bihash_add_del_24_8 (&qm->
363 wrk_ctx[ctx->c_thread_index].crypto_context_hash,
364 &kv, 1 /* is_add */ );
368 quic_crypto_context_free_if_needed (crctx, ctx->c_thread_index);
369 return VNET_API_ERROR_MISSING_CERT_KEY;
372 /* Helper functions */
375 quic_ctx_alloc (u32 thread_index)
377 quic_main_t *qm = &quic_main;
380 pool_get (qm->ctx_pool[thread_index], ctx);
382 clib_memset (ctx, 0, sizeof (quic_ctx_t));
383 ctx->c_thread_index = thread_index;
384 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
385 QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
386 ctx - qm->ctx_pool[thread_index], thread_index);
387 return ctx - qm->ctx_pool[thread_index];
391 quic_ctx_free (quic_ctx_t * ctx)
393 QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
394 u32 thread_index = ctx->c_thread_index;
395 QUIC_ASSERT (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID);
397 clib_memset (ctx, 0xfb, sizeof (*ctx));
398 pool_put (quic_main.ctx_pool[thread_index], ctx);
402 quic_ctx_get (u32 ctx_index, u32 thread_index)
404 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
408 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
410 if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
412 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
416 quic_get_conn_ctx (quicly_conn_t * conn)
419 conn_data = (u64) * quicly_get_data (conn);
420 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
424 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
426 *quicly_get_data (conn) =
427 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
431 quic_ctx_is_stream (quic_ctx_t * ctx)
433 return (ctx->flags & QUIC_F_IS_STREAM);
437 quic_ctx_is_listener (quic_ctx_t * ctx)
439 return (ctx->flags & QUIC_F_IS_LISTENER);
443 quic_ctx_is_conn (quic_ctx_t * ctx)
445 return !(quic_ctx_is_listener (ctx) || quic_ctx_is_stream (ctx));
449 get_stream_session_from_stream (quicly_stream_t * stream)
452 quic_stream_data_t *stream_data;
454 stream_data = (quic_stream_data_t *) stream->data;
455 ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
456 return session_get (ctx->c_s_index, stream_data->thread_index);
460 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
461 const quicly_cid_plaintext_t * id)
463 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
464 kv->key[1] = id->node_id;
468 quic_sendable_packet_count (session_t * udp_session)
471 u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
472 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
473 return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
476 static quicly_context_t *
477 quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
479 crypto_context_t *crctx =
480 quic_crypto_context_get (ctx->crypto_context_index, ctx->c_thread_index);
481 quic_crypto_context_data_t *data =
482 (quic_crypto_context_data_t *) crctx->data;
483 return &data->quicly_ctx;
486 static quicly_context_t *
487 quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
489 session_t *udp_session = session_get_from_handle (udp_session_handle);
491 quic_ctx_get (udp_session->opaque, udp_session->thread_index);
492 return quic_get_quicly_ctx_from_ctx (ctx);
496 quic_set_udp_tx_evt (session_t * udp_session)
499 if (svm_fifo_set_event (udp_session->tx_fifo))
500 rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
502 if (PREDICT_FALSE (rv))
503 clib_warning ("Event enqueue errored %d", rv);
507 quic_stop_ctx_timer (quic_ctx_t * ctx)
509 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
510 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
512 tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
513 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
514 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
515 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
518 /* QUIC protocol actions */
521 quic_ack_rx_data (session_t * stream_session)
526 quicly_stream_t *stream;
527 quic_stream_data_t *stream_data;
529 sctx = quic_ctx_get (stream_session->connection_index,
530 stream_session->thread_index);
531 QUIC_ASSERT (quic_ctx_is_stream (sctx));
532 stream = sctx->stream;
533 stream_data = (quic_stream_data_t *) stream->data;
535 f = stream_session->rx_fifo;
536 max_deq = svm_fifo_max_dequeue (f);
538 QUIC_ASSERT (stream_data->app_rx_data_len >= max_deq);
539 quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
540 QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
541 stream_data->app_rx_data_len = max_deq;
545 quic_disconnect_transport (quic_ctx_t * ctx)
547 QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
548 vnet_disconnect_args_t a = {
549 .handle = ctx->udp_session_handle,
550 .app_index = quic_main.app_index,
553 if (vnet_disconnect_session (&a))
554 clib_warning ("UDP session 0x%lx disconnect errored",
555 ctx->udp_session_handle);
559 quic_connection_delete (quic_ctx_t * ctx)
561 clib_bihash_kv_16_8_t kv;
564 QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
566 QUIC_ASSERT (!quic_ctx_is_stream (ctx));
567 quic_stop_ctx_timer (ctx);
569 /* Delete the connection from the connection map */
572 quic_make_connection_key (&kv, quicly_get_master_id (conn));
573 QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
575 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
577 quic_disconnect_transport (ctx);
580 quicly_free (ctx->conn);
581 session_transport_delete_notify (&ctx->connection);
585 quic_increment_counter (u8 evt, u8 val)
587 vlib_main_t *vm = vlib_get_main ();
588 vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
592 * Called when quicly return an error
593 * This function interacts tightly with quic_proto_on_close
596 quic_connection_closed (quic_ctx_t * ctx)
598 QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
601 /* TODO if connection is not established, just delete the session? */
602 /* Actually should send connect or accept error */
604 switch (ctx->conn_state)
606 case QUIC_CONN_STATE_READY:
607 /* Error on an opened connection (timeout...)
608 This puts the session in closing state, we should receive a notification
609 when the app has closed its session */
610 session_transport_reset_notify (&ctx->connection);
611 /* This ensures we delete the connection when the app confirms the close */
612 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
614 case QUIC_CONN_STATE_PASSIVE_CLOSING:
615 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
616 /* quic_proto_on_close will eventually be called when the app confirms the close
617 , we delete the connection at that point */
619 case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
620 /* App already confirmed close, we can delete the connection */
621 quic_connection_delete (ctx);
623 case QUIC_CONN_STATE_OPENED:
624 case QUIC_CONN_STATE_HANDSHAKE:
625 case QUIC_CONN_STATE_ACTIVE_CLOSING:
626 quic_connection_delete (ctx);
629 QUIC_DBG (0, "BUG %d", ctx->conn_state);
635 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
638 session_dgram_hdr_t hdr;
641 transport_connection_t *tc;
643 len = packet->data.len;
644 f = udp_session->tx_fifo;
645 tc = session_get_transport (udp_session);
646 max_enqueue = svm_fifo_max_enqueue (f);
647 if (max_enqueue < SESSION_CONN_HDR_LEN + len)
649 QUIC_ERR ("Too much data to send, max_enqueue %u, len %u",
650 max_enqueue, len + SESSION_CONN_HDR_LEN);
651 return QUIC_ERROR_FULL_FIFO;
654 /* Build packet header for fifo */
655 hdr.data_length = len;
657 hdr.is_ip4 = tc->is_ip4;
658 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
659 hdr.lcl_port = tc->lcl_port;
661 /* Read dest address from quicly-provided sockaddr */
664 QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET);
665 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
666 hdr.rmt_port = sa4->sin_port;
667 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
671 QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET6);
672 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
673 hdr.rmt_port = sa6->sin6_port;
674 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
677 ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
678 if (ret != sizeof (hdr))
680 QUIC_ERR ("Not enough space to enqueue header");
681 return QUIC_ERROR_FULL_FIFO;
683 ret = svm_fifo_enqueue (f, len, packet->data.base);
686 QUIC_ERR ("Not enough space to enqueue payload");
687 return QUIC_ERROR_FULL_FIFO;
690 quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
696 quic_send_packets (quic_ctx_t * ctx)
698 quic_main_t *qm = &quic_main;
699 quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
700 session_t *udp_session;
702 size_t num_packets, i, max_packets;
703 quicly_packet_allocator_t *pa;
705 u32 thread_index = vlib_get_thread_index ();
707 /* We have sctx, get qctx */
708 if (quic_ctx_is_stream (ctx))
709 ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
711 QUIC_ASSERT (!quic_ctx_is_stream (ctx));
713 udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
722 /* TODO : quicly can assert it can send min_packets up to 2 */
723 if (quic_sendable_packet_count (udp_session) < 2)
726 pa = quic_get_quicly_ctx_from_ctx (ctx)->packet_allocator;
729 max_packets = quic_sendable_packet_count (udp_session);
732 num_packets = max_packets;
733 if ((err = quicly_send (conn, packets, &num_packets)))
736 quic_crypto_batch_tx_packets (&qm->wrk_ctx
737 [thread_index].crypto_context_batch);
739 for (i = 0; i != num_packets; ++i)
741 quic_crypto_finalize_send_packet (packets[i]);
742 if ((err = quic_send_datagram (udp_session, packets[i])))
745 pa->free_packet (pa, packets[i]);
748 while (num_packets > 0 && num_packets == max_packets);
751 quic_set_udp_tx_evt (udp_session);
753 QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
754 svm_fifo_max_dequeue (udp_session->rx_fifo));
755 quic_update_timer (ctx);
759 if (err && err != QUICLY_ERROR_PACKET_IGNORED
760 && err != QUICLY_ERROR_FREE_CONNECTION)
761 clib_warning ("Quic error '%U'.", quic_format_err, err);
762 quic_connection_closed (ctx);
766 /* Quicly callbacks */
769 quic_on_stream_destroy (quicly_stream_t * stream, int err)
771 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
772 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
773 stream_data->thread_index);
774 session_t *stream_session = session_get (sctx->c_s_index,
775 sctx->c_thread_index);
776 QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
777 session_handle (stream_session), quic_format_err, err);
779 stream_session->session_state = SESSION_STATE_CLOSED;
780 session_transport_delete_notify (&sctx->connection);
782 quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
783 quic_ctx_free (sctx);
784 clib_mem_free (stream->data);
788 quic_on_stop_sending (quicly_stream_t * stream, int err)
791 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
792 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
793 stream_data->thread_index);
794 session_t *stream_session = session_get (sctx->c_s_index,
795 sctx->c_thread_index);
796 clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
797 session_handle (stream_session), quic_format_err, err);
799 /* TODO : handle STOP_SENDING */
804 quic_on_receive_reset (quicly_stream_t * stream, int err)
806 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
807 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
808 stream_data->thread_index);
810 session_t *stream_session = session_get (sctx->c_s_index,
811 sctx->c_thread_index);
812 clib_warning ("RESET_STREAM: session 0x%lx (%U)",
813 session_handle (stream_session), quic_format_err, err);
815 session_transport_closing_notify (&sctx->connection);
820 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
823 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
824 u32 max_enq, rlen, rv;
826 session_t *stream_session;
827 app_worker_t *app_wrk;
829 quic_stream_data_t *stream_data;
831 stream_data = (quic_stream_data_t *) stream->data;
832 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
833 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
834 f = stream_session->rx_fifo;
836 max_enq = svm_fifo_max_enqueue_prod (f);
837 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
838 /* Handle duplicate packet/chunk from quicly */
839 if (off < stream_data->app_rx_data_len)
841 QUIC_DBG (3, "Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
842 "DUPLICATE PACKET (max_enq %u, len %u, "
843 "app_rx_data_len %u, off %u, ToBeNQ %u)",
844 stream_session->session_index,
845 stream_session->app_wrk_index,
846 stream_session->thread_index, f,
847 max_enq, len, stream_data->app_rx_data_len, off,
848 off - stream_data->app_rx_data_len + len);
851 if (PREDICT_FALSE ((off - stream_data->app_rx_data_len + len) > max_enq))
853 QUIC_ERR ("Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
854 "RX FIFO IS FULL (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 (off == stream_data->app_rx_data_len)
865 /* Streams live on the same thread so (f, stream_data) should stay consistent */
866 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
867 QUIC_DBG (3, "Session [idx %u, app_wrk %u, ti %u, rx-fifo 0x%llx]: "
868 "Enqueuing %u (rlen %u) at off %u in %u space, ",
869 stream_session->session_index,
870 stream_session->app_wrk_index,
871 stream_session->thread_index, f, len, rlen, off, max_enq);
872 stream_data->app_rx_data_len += rlen;
873 QUIC_ASSERT (rlen >= len);
874 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
875 if (PREDICT_TRUE (app_wrk != 0))
877 rv = app_worker_lock_and_send_event (app_wrk, stream_session,
880 QUIC_ERR ("Failed to ping app for RX");
882 quic_ack_rx_data (stream_session);
886 rlen = svm_fifo_enqueue_with_offset (f,
887 off - stream_data->app_rx_data_len,
889 QUIC_ASSERT (rlen == 0);
895 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
897 quic_stream_data_t *stream_data;
898 session_t *stream_session;
902 stream_data = (quic_stream_data_t *) stream->data;
903 stream_session = get_stream_session_from_stream (stream);
904 f = stream_session->tx_fifo;
906 QUIC_ASSERT (stream_data->app_tx_data_len >= delta);
907 stream_data->app_tx_data_len -= delta;
908 rv = svm_fifo_dequeue_drop (f, delta);
909 QUIC_ASSERT (rv == delta);
911 rv = quicly_stream_sync_sendbuf (stream, 0);
916 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
917 size_t * len, int *wrote_all)
919 quic_stream_data_t *stream_data;
920 session_t *stream_session;
924 stream_data = (quic_stream_data_t *) stream->data;
925 stream_session = get_stream_session_from_stream (stream);
926 f = stream_session->tx_fifo;
928 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
930 deq_max = svm_fifo_max_dequeue_cons (f);
931 QUIC_ASSERT (off <= deq_max);
932 if (off + *len < deq_max)
939 *len = deq_max - off;
941 QUIC_ASSERT (*len > 0);
943 if (off + *len > stream_data->app_tx_data_len)
944 stream_data->app_tx_data_len = off + *len;
946 svm_fifo_peek (f, off, *len, dst);
951 static const quicly_stream_callbacks_t quic_stream_callbacks = {
952 .on_destroy = quic_on_stream_destroy,
953 .on_send_shift = quic_fifo_egress_shift,
954 .on_send_emit = quic_fifo_egress_emit,
955 .on_send_stop = quic_on_stop_sending,
956 .on_receive = quic_on_receive,
957 .on_receive_reset = quic_on_receive_reset
961 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
963 /* Return code for this function ends either
964 * - in quicly_receive : if not QUICLY_ERROR_PACKET_IGNORED, will close connection
965 * - in quicly_open_stream, returned directly
968 session_t *stream_session, *quic_session;
969 quic_stream_data_t *stream_data;
970 app_worker_t *app_wrk;
971 quic_ctx_t *qctx, *sctx;
975 QUIC_DBG (2, "on_stream_open called");
976 stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
977 stream->callbacks = &quic_stream_callbacks;
978 /* Notify accept on parent qsession, but only if this is not a locally
979 * initiated stream */
980 if (quicly_stream_is_self_initiated (stream))
983 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
984 qctx = quic_get_conn_ctx (stream->conn);
986 /* Might need to signal that the connection is ready if the first thing the
987 * server does is open a stream */
988 quic_check_quic_session_connected (qctx);
989 /* ctx might be invalidated */
990 qctx = quic_get_conn_ctx (stream->conn);
992 stream_session = session_alloc (qctx->c_thread_index);
993 QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
994 session_handle (stream_session), sctx_id);
995 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
996 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
997 sctx->parent_app_id = qctx->parent_app_id;
998 sctx->quic_connection_ctx_id = qctx->c_c_index;
999 sctx->c_c_index = sctx_id;
1000 sctx->c_s_index = stream_session->session_index;
1001 sctx->stream = stream;
1002 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1003 sctx->flags |= QUIC_F_IS_STREAM;
1004 if (quicly_stream_is_unidirectional (stream->stream_id))
1005 stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
1007 stream_data = (quic_stream_data_t *) stream->data;
1008 stream_data->ctx_id = sctx_id;
1009 stream_data->thread_index = sctx->c_thread_index;
1010 stream_data->app_rx_data_len = 0;
1011 stream_data->app_tx_data_len = 0;
1013 sctx->c_s_index = stream_session->session_index;
1014 stream_session->session_state = SESSION_STATE_CREATED;
1015 stream_session->app_wrk_index = sctx->parent_app_wrk_id;
1016 stream_session->connection_index = sctx->c_c_index;
1017 stream_session->session_type =
1018 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1019 quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
1020 stream_session->listener_handle = listen_session_get_handle (quic_session);
1022 app_wrk = app_worker_get (stream_session->app_wrk_index);
1023 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
1025 QUIC_ERR ("failed to allocate fifos");
1026 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
1027 return 0; /* Frame is still valid */
1029 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1030 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1031 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1033 if ((rv = app_worker_accept_notify (app_wrk, stream_session)))
1035 QUIC_ERR ("failed to notify accept worker app");
1036 quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
1037 return 0; /* Frame is still valid */
1044 quic_on_closed_by_peer (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
1045 int code, uint64_t frame_type,
1046 const char *reason, size_t reason_len)
1048 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
1050 session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
1051 clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
1052 session_handle (quic_session), quic_format_err, code,
1053 reason_len, reason);
1055 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
1056 session_transport_closing_notify (&ctx->connection);
1059 /* Timer handling */
1062 quic_get_thread_time (u8 thread_index)
1064 return quic_main.wrk_ctx[thread_index].time_now;
1068 quic_get_time (quicly_now_t * self)
1070 u8 thread_index = vlib_get_thread_index ();
1071 return quic_get_thread_time (thread_index);
1075 quic_set_time_now (u32 thread_index)
1077 vlib_main_t *vlib_main = vlib_get_main ();
1078 f64 time = vlib_time_now (vlib_main);
1079 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
1080 return quic_main.wrk_ctx[thread_index].time_now;
1083 /* Transport proto callback */
1085 quic_update_time (f64 now, u8 thread_index)
1087 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1089 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1090 quic_set_time_now (thread_index);
1091 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
1095 quic_timer_expired (u32 conn_index)
1098 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
1099 quic_get_time (NULL));
1100 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
1101 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1102 quic_send_packets (ctx);
1106 quic_update_timer (quic_ctx_t * ctx)
1108 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1109 int64_t next_timeout, next_interval;
1110 session_t *quic_session;
1113 /* This timeout is in ms which is the unit of our timer */
1114 next_timeout = quicly_get_first_timeout (ctx->conn);
1115 next_interval = next_timeout - quic_get_time (NULL);
1117 if (next_timeout == 0 || next_interval <= 0)
1119 if (ctx->c_s_index == QUIC_SESSION_INVALID)
1125 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
1126 if (svm_fifo_set_event (quic_session->tx_fifo))
1128 rv = session_send_io_evt_to_thread_custom (quic_session,
1129 quic_session->thread_index,
1130 SESSION_IO_EVT_BUILTIN_TX);
1131 if (PREDICT_FALSE (rv))
1132 QUIC_ERR ("Failed to enqueue builtin_tx %d", rv);
1138 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
1140 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
1141 next_interval, ctx->c_c_index);
1143 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
1145 if (next_timeout == INT64_MAX)
1147 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
1150 ctx->timer_handle = tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index,
1155 if (next_timeout == INT64_MAX)
1157 quic_stop_ctx_timer (ctx);
1160 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
1167 quic_expired_timers_dispatch (u32 * expired_timers)
1171 for (i = 0; i < vec_len (expired_timers); i++)
1173 quic_timer_expired (expired_timers[i]);
1177 /* Transport proto functions */
1179 quic_connect_stream (session_t * quic_session, session_endpoint_cfg_t * sep)
1181 uint64_t quic_session_handle;
1182 session_t *stream_session;
1183 quic_stream_data_t *stream_data;
1184 quicly_stream_t *stream;
1185 quicly_conn_t *conn;
1186 app_worker_t *app_wrk;
1187 quic_ctx_t *qctx, *sctx;
1191 /* Find base session to which the user want to attach a stream */
1192 quic_session_handle = session_handle (quic_session);
1193 QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
1195 if (session_type_transport_proto (quic_session->session_type) !=
1196 TRANSPORT_PROTO_QUIC)
1198 QUIC_ERR ("received incompatible session");
1202 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
1205 QUIC_ERR ("Invalid app worker :(");
1209 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
1210 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
1211 qctx = quic_ctx_get (quic_session->connection_index,
1212 quic_session->thread_index);
1213 if (quic_ctx_is_stream (qctx))
1215 QUIC_ERR ("session is a stream");
1216 quic_ctx_free (sctx);
1220 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
1221 sctx->parent_app_id = qctx->parent_app_id;
1222 sctx->quic_connection_ctx_id = qctx->c_c_index;
1223 sctx->c_c_index = sctx_index;
1224 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1225 sctx->flags |= QUIC_F_IS_STREAM;
1229 if (!conn || !quicly_connection_is_ready (conn))
1233 quicly_open_stream (conn, &stream,
1234 sep->flags & SESSION_F_UNIDIRECTIONAL)))
1236 QUIC_DBG (2, "Stream open failed with %d", rv);
1239 quic_increment_counter (QUIC_ERROR_OPENED_STREAM, 1);
1241 sctx->stream = stream;
1243 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1245 stream_session = session_alloc (qctx->c_thread_index);
1246 QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
1247 session_handle (stream_session), sctx_index);
1248 stream_session->app_wrk_index = app_wrk->wrk_index;
1249 stream_session->connection_index = sctx_index;
1250 stream_session->listener_handle = quic_session_handle;
1251 stream_session->session_type =
1252 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1253 if (sep->flags & SESSION_F_UNIDIRECTIONAL)
1254 stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
1256 sctx->c_s_index = stream_session->session_index;
1257 stream_data = (quic_stream_data_t *) stream->data;
1258 stream_data->ctx_id = sctx->c_c_index;
1259 stream_data->thread_index = sctx->c_thread_index;
1260 stream_data->app_rx_data_len = 0;
1261 stream_data->app_tx_data_len = 0;
1262 stream_session->session_state = SESSION_STATE_READY;
1264 /* For now we only reset streams. Cleanup will be triggered by timers */
1265 if (app_worker_init_connected (app_wrk, stream_session))
1267 QUIC_ERR ("failed to app_worker_init_connected");
1268 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1269 return app_worker_connect_notify (app_wrk, NULL, sep->opaque);
1272 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1273 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1274 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1276 if (app_worker_connect_notify (app_wrk, stream_session, sep->opaque))
1278 QUIC_ERR ("failed to notify app");
1279 quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
1280 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1288 quic_connect_connection (session_endpoint_cfg_t * sep)
1290 vnet_connect_args_t _cargs, *cargs = &_cargs;
1291 quic_main_t *qm = &quic_main;
1293 app_worker_t *app_wrk;
1296 u32 thread_index = vlib_get_thread_index ();
1299 clib_memset (cargs, 0, sizeof (*cargs));
1300 ctx_index = quic_ctx_alloc (thread_index);
1301 ctx = quic_ctx_get (ctx_index, thread_index);
1302 ctx->parent_app_wrk_id = sep->app_wrk_index;
1303 ctx->c_s_index = QUIC_SESSION_INVALID;
1304 ctx->c_c_index = ctx_index;
1305 ctx->udp_is_ip4 = sep->is_ip4;
1306 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1307 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1308 ctx->client_opaque = sep->opaque;
1309 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1311 ctx->srv_hostname = format (0, "%v", sep->hostname);
1313 /* needed by quic for crypto + determining client / server */
1314 ctx->srv_hostname = format (0, "%U", format_ip46_address,
1315 &sep->ip, sep->is_ip4);
1316 vec_terminate_c_string (ctx->srv_hostname);
1318 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1319 cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1320 cargs->app_index = qm->app_index;
1321 cargs->api_context = ctx_index;
1323 app_wrk = app_worker_get (sep->app_wrk_index);
1324 app = application_get (app_wrk->app_index);
1325 ctx->parent_app_id = app_wrk->app_index;
1326 cargs->sep_ext.ns_index = app->ns_index;
1328 ctx->crypto_engine = sep->crypto_engine;
1329 ctx->ckpair_index = sep->ckpair_index;
1330 if ((error = quic_acquire_crypto_context (ctx)))
1333 if ((error = vnet_connect (cargs)))
1340 quic_connect (transport_endpoint_cfg_t * tep)
1342 QUIC_DBG (2, "Called quic_connect");
1343 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1344 session_t *quic_session;
1345 sep = (session_endpoint_cfg_t *) tep;
1347 quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1349 return quic_connect_stream (quic_session, sep);
1351 return quic_connect_connection (sep);
1355 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1357 quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1361 session_t *stream_session = session_get (ctx->c_s_index,
1362 ctx->c_thread_index);
1363 clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1365 if (quic_ctx_is_stream (ctx))
1367 quicly_stream_t *stream = ctx->stream;
1368 if (!quicly_stream_has_send_side (quicly_is_client (stream->conn),
1371 quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1372 quic_send_packets (ctx);
1376 switch (ctx->conn_state)
1378 case QUIC_CONN_STATE_OPENED:
1379 case QUIC_CONN_STATE_HANDSHAKE:
1380 case QUIC_CONN_STATE_READY:
1381 ctx->conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1382 quicly_conn_t *conn = ctx->conn;
1383 /* Start connection closing. Keep sending packets until quicly_send
1384 returns QUICLY_ERROR_FREE_CONNECTION */
1386 quic_increment_counter (QUIC_ERROR_CLOSED_CONNECTION, 1);
1387 quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1388 /* This also causes all streams to be closed (and the cb called) */
1389 quic_send_packets (ctx);
1391 case QUIC_CONN_STATE_PASSIVE_CLOSING:
1392 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1393 /* send_packets will eventually return an error, we delete the conn at
1396 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1397 quic_connection_delete (ctx);
1399 case QUIC_CONN_STATE_ACTIVE_CLOSING:
1402 QUIC_ERR ("Trying to close conn in state %d", ctx->conn_state);
1408 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1410 vnet_listen_args_t _bargs, *args = &_bargs;
1411 quic_main_t *qm = &quic_main;
1412 session_handle_t udp_handle;
1413 session_endpoint_cfg_t *sep;
1414 session_t *udp_listen_session;
1415 app_worker_t *app_wrk;
1419 app_listener_t *app_listener;
1422 sep = (session_endpoint_cfg_t *) tep;
1423 app_wrk = app_worker_get (sep->app_wrk_index);
1424 /* We need to call this because we call app_worker_init_connected in
1425 * quic_accept_stream, which assumes the connect segment manager exists */
1426 app_worker_alloc_connects_segment_manager (app_wrk);
1427 app = application_get (app_wrk->app_index);
1428 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1430 clib_memset (args, 0, sizeof (*args));
1431 args->app_index = qm->app_index;
1432 args->sep_ext = *sep;
1433 args->sep_ext.ns_index = app->ns_index;
1434 args->sep_ext.transport_proto = TRANSPORT_PROTO_UDPC;
1435 if ((rv = vnet_listen (args)))
1438 lctx_index = quic_ctx_alloc (0);
1439 udp_handle = args->handle;
1440 app_listener = app_listener_get_w_handle (udp_handle);
1441 udp_listen_session = app_listener_get_session (app_listener);
1442 udp_listen_session->opaque = lctx_index;
1444 lctx = quic_ctx_get (lctx_index, 0);
1445 lctx->flags |= QUIC_F_IS_LISTENER;
1447 clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1448 clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1449 lctx->c_rmt_port = args->sep.peer.port;
1450 lctx->c_lcl_port = args->sep.port;
1451 lctx->c_is_ip4 = args->sep.is_ip4;
1452 lctx->c_fib_index = args->sep.fib_index;
1453 lctx->c_proto = TRANSPORT_PROTO_QUIC;
1454 lctx->parent_app_wrk_id = sep->app_wrk_index;
1455 lctx->parent_app_id = app_wrk->app_index;
1456 lctx->udp_session_handle = udp_handle;
1457 lctx->c_s_index = quic_listen_session_index;
1458 lctx->crypto_engine = sep->crypto_engine;
1459 lctx->ckpair_index = sep->ckpair_index;
1460 if (quic_acquire_crypto_context (lctx))
1463 QUIC_DBG (2, "Listening UDP session 0x%lx",
1464 session_handle (udp_listen_session));
1465 QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1470 quic_stop_listen (u32 lctx_index)
1472 QUIC_DBG (2, "Called quic_stop_listen");
1474 lctx = quic_ctx_get (lctx_index, 0);
1475 QUIC_ASSERT (quic_ctx_is_listener (lctx));
1476 vnet_unlisten_args_t a = {
1477 .handle = lctx->udp_session_handle,
1478 .app_index = quic_main.app_index,
1479 .wrk_map_index = 0 /* default wrk */
1481 if (vnet_unlisten (&a))
1482 clib_warning ("unlisten errored");
1484 quic_release_crypto_context (lctx->crypto_context_index,
1485 0 /* thread_index */ );
1486 quic_ctx_free (lctx);
1490 static transport_connection_t *
1491 quic_connection_get (u32 ctx_index, u32 thread_index)
1494 ctx = quic_ctx_get (ctx_index, thread_index);
1495 return &ctx->connection;
1498 static transport_connection_t *
1499 quic_listener_get (u32 listener_index)
1501 QUIC_DBG (2, "Called quic_listener_get");
1503 ctx = quic_ctx_get (listener_index, 0);
1504 return &ctx->connection;
1508 format_quic_ctx (u8 * s, va_list * args)
1510 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1511 u32 verbose = va_arg (*args, u32);
1516 str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1518 if (quic_ctx_is_listener (ctx))
1519 str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1520 else if (quic_ctx_is_stream (ctx))
1521 str = format (str, "Stream %ld conn %d",
1522 ctx->stream->stream_id, ctx->quic_connection_ctx_id);
1523 else /* connection */
1524 str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1525 ctx->udp_session_handle);
1527 str = format (str, " app %d wrk %d", ctx->parent_app_id,
1528 ctx->parent_app_wrk_id);
1531 s = format (s, "%-50s%-15d", str, ctx->conn_state);
1533 s = format (s, "%s\n", str);
1539 format_quic_connection (u8 * s, va_list * args)
1541 u32 qc_index = va_arg (*args, u32);
1542 u32 thread_index = va_arg (*args, u32);
1543 u32 verbose = va_arg (*args, u32);
1544 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1545 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1550 format_quic_half_open (u8 * s, va_list * args)
1552 u32 qc_index = va_arg (*args, u32);
1553 u32 thread_index = va_arg (*args, u32);
1554 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1555 s = format (s, "[#%d][Q] half-open app %u", thread_index,
1556 ctx->parent_app_id);
1562 format_quic_listener (u8 * s, va_list * args)
1564 u32 tci = va_arg (*args, u32);
1565 u32 thread_index = va_arg (*args, u32);
1566 u32 verbose = va_arg (*args, u32);
1567 quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1568 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1572 /* Session layer callbacks */
1575 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1576 ip46_address_t * addr, u16 port, u8 is_ip4)
1580 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1581 sa4->sin_family = AF_INET;
1582 sa4->sin_port = port;
1583 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1584 *salen = sizeof (struct sockaddr_in);
1588 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1589 sa6->sin6_family = AF_INET6;
1590 sa6->sin6_port = port;
1591 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1592 *salen = sizeof (struct sockaddr_in6);
1597 quic_on_quic_session_connected (quic_ctx_t * ctx)
1599 session_t *quic_session;
1600 app_worker_t *app_wrk;
1601 u32 ctx_id = ctx->c_c_index;
1602 u32 thread_index = ctx->c_thread_index;
1605 quic_session = session_alloc (thread_index);
1607 QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1608 ctx->c_s_index = quic_session->session_index;
1609 quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1610 quic_session->connection_index = ctx->c_c_index;
1611 quic_session->listener_handle = SESSION_INVALID_HANDLE;
1612 quic_session->session_type =
1613 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1615 /* If quic session connected fails, immediatly close connection */
1616 app_wrk = app_worker_get (ctx->parent_app_wrk_id);
1617 if (app_worker_init_connected (app_wrk, quic_session))
1619 QUIC_ERR ("failed to app_worker_init_connected");
1620 quic_proto_on_close (ctx_id, thread_index);
1621 app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1625 quic_session->session_state = SESSION_STATE_CONNECTING;
1626 if ((rv = app_worker_connect_notify (app_wrk, quic_session,
1627 ctx->client_opaque)))
1629 QUIC_ERR ("failed to notify app %d", rv);
1630 quic_proto_on_close (ctx_id, thread_index);
1634 /* If the app opens a stream in its callback it may invalidate ctx */
1635 ctx = quic_ctx_get (ctx_id, thread_index);
1637 * app_worker_connect_notify() might have reallocated pool, reload
1638 * quic_session pointer
1640 quic_session = session_get (ctx->c_s_index, thread_index);
1641 quic_session->session_state = SESSION_STATE_LISTENING;
1645 quic_check_quic_session_connected (quic_ctx_t * ctx)
1647 /* Called when we need to trigger quic session connected
1648 * we may call this function on the server side / at
1651 /* Conn may be set to null if the connection is terminated */
1652 if (!ctx->conn || ctx->conn_state != QUIC_CONN_STATE_HANDSHAKE)
1654 if (!quicly_connection_is_ready (ctx->conn))
1656 ctx->conn_state = QUIC_CONN_STATE_READY;
1657 if (!quicly_is_client (ctx->conn))
1659 quic_on_quic_session_connected (ctx);
1663 quic_update_conn_ctx (quicly_conn_t * conn, quicly_context_t * quicly_context)
1665 /* we need to update the quicly_conn on migrate
1666 * as it contains a pointer to the crypto context */
1667 ptls_context_t **tls;
1668 quicly_context_t **_quicly_context;
1669 _quicly_context = (quicly_context_t **) conn;
1670 *_quicly_context = quicly_context;
1671 tls = (ptls_context_t **) quicly_get_tls (conn);
1672 *tls = quicly_context->tls;
1676 quic_receive_connection (void *arg)
1678 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1679 quic_ctx_t *temp_ctx, *new_ctx;
1680 clib_bihash_kv_16_8_t kv;
1681 quicly_conn_t *conn;
1682 quicly_context_t *quicly_context;
1683 session_t *udp_session;
1686 new_ctx_id = quic_ctx_alloc (thread_index);
1687 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1689 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1692 clib_memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1693 clib_mem_free (temp_ctx);
1695 new_ctx->c_thread_index = thread_index;
1696 new_ctx->c_c_index = new_ctx_id;
1697 quic_acquire_crypto_context (new_ctx);
1699 conn = new_ctx->conn;
1700 quicly_context = quic_get_quicly_ctx_from_ctx (new_ctx);
1701 quic_update_conn_ctx (conn, quicly_context);
1703 quic_store_conn_ctx (conn, new_ctx);
1704 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1705 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1706 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1707 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1708 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1709 quic_update_timer (new_ctx);
1711 /* Trigger write on this connection if necessary */
1712 udp_session = session_get_from_handle (new_ctx->udp_session_handle);
1713 udp_session->opaque = new_ctx_id;
1714 udp_session->flags &= ~SESSION_F_IS_MIGRATING;
1715 if (svm_fifo_max_dequeue (udp_session->tx_fifo))
1716 quic_set_udp_tx_evt (udp_session);
1720 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1722 quic_ctx_t *ctx, *temp_ctx;
1723 u32 thread_index = vlib_get_thread_index ();
1725 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1727 temp_ctx = clib_mem_alloc (sizeof (quic_ctx_t));
1728 QUIC_ASSERT (temp_ctx != NULL);
1729 ctx = quic_ctx_get (ctx_index, thread_index);
1731 clib_memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1733 quic_stop_ctx_timer (ctx);
1734 quic_release_crypto_context (ctx->crypto_context_index, thread_index);
1735 quic_ctx_free (ctx);
1737 /* Send connection to destination thread */
1738 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1743 quic_udp_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1744 session_t * udp_session, u8 is_fail)
1746 QUIC_DBG (2, "QSession is now connected (id %u)",
1747 udp_session->session_index);
1748 /* This should always be called before quic_connect returns since UDP always
1749 * connects instantly. */
1750 clib_bihash_kv_16_8_t kv;
1751 struct sockaddr_in6 sa6;
1752 struct sockaddr *sa = (struct sockaddr *) &sa6;
1754 transport_connection_t *tc;
1755 app_worker_t *app_wrk;
1756 quicly_conn_t *conn;
1758 u32 thread_index = vlib_get_thread_index ();
1760 quicly_context_t *quicly_ctx;
1763 ctx = quic_ctx_get (ctx_index, thread_index);
1767 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1770 api_context = ctx->c_s_index;
1771 app_worker_connect_notify (app_wrk, 0, api_context);
1776 ctx->c_thread_index = thread_index;
1777 ctx->c_c_index = ctx_index;
1779 QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1780 is_fail, thread_index, (ctx) ? ctx_index : ~0);
1782 ctx->udp_session_handle = session_handle (udp_session);
1783 udp_session->opaque = ctx_index;
1785 /* Init QUIC lib connection
1786 * Generate required sockaddr & salen */
1787 tc = session_get_transport (udp_session);
1788 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1790 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1791 ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
1792 sa, NULL, &quic_main.wrk_ctx[thread_index].next_cid,
1793 ptls_iovec_init (NULL, 0), &quic_main.hs_properties,
1795 ++quic_main.wrk_ctx[thread_index].next_cid.master_id;
1796 /* Save context handle in quicly connection */
1797 quic_store_conn_ctx (ctx->conn, ctx);
1800 /* Register connection in connections map */
1802 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1803 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1804 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1805 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1807 /* UDP stack quirk? preemptively transfer connection if that happens */
1808 if (udp_session->thread_index != thread_index)
1809 quic_transfer_connection (ctx_index, udp_session->thread_index);
1811 quic_send_packets (ctx);
1817 quic_udp_session_disconnect_callback (session_t * s)
1819 clib_warning ("UDP session disconnected???");
1823 quic_udp_session_cleanup_callback (session_t * udp_session,
1824 session_cleanup_ntf_t ntf)
1828 if (ntf != SESSION_CLEANUP_SESSION)
1831 ctx = quic_ctx_get (udp_session->opaque, udp_session->thread_index);
1832 quic_stop_ctx_timer (ctx);
1833 quic_release_crypto_context (ctx->crypto_context_index,
1834 ctx->c_thread_index);
1835 quic_ctx_free (ctx);
1839 quic_udp_session_reset_callback (session_t * s)
1841 clib_warning ("UDP session reset???");
1845 quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
1847 u32 new_thread = session_thread_from_handle (new_sh);
1850 QUIC_DBG (2, "Session %x migrated to %lx", s->session_index, new_sh);
1851 QUIC_ASSERT (vlib_get_thread_index () == s->thread_index);
1852 ctx = quic_ctx_get (s->opaque, s->thread_index);
1853 QUIC_ASSERT (ctx->udp_session_handle == session_handle (s));
1855 ctx->udp_session_handle = new_sh;
1857 s->opaque = 0xfeedface;
1859 quic_transfer_connection (ctx->c_c_index, new_thread);
1863 quic_udp_session_accepted_callback (session_t * udp_session)
1865 /* New UDP connection, try to accept it */
1867 quic_ctx_t *ctx, *lctx;
1868 session_t *udp_listen_session;
1869 u32 thread_index = vlib_get_thread_index ();
1871 udp_listen_session =
1872 listen_session_get_from_handle (udp_session->listener_handle);
1874 ctx_index = quic_ctx_alloc (thread_index);
1875 ctx = quic_ctx_get (ctx_index, thread_index);
1876 ctx->c_thread_index = udp_session->thread_index;
1877 ctx->c_c_index = ctx_index;
1878 ctx->c_s_index = QUIC_SESSION_INVALID;
1879 ctx->udp_session_handle = session_handle (udp_session);
1880 QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
1881 ctx->listener_ctx_id = udp_listen_session->opaque;
1882 lctx = quic_ctx_get (udp_listen_session->opaque,
1883 udp_listen_session->thread_index);
1884 ctx->udp_is_ip4 = lctx->c_is_ip4;
1885 ctx->parent_app_id = lctx->parent_app_id;
1886 ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
1887 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1888 ctx->conn_state = QUIC_CONN_STATE_OPENED;
1889 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1891 ctx->crypto_engine = lctx->crypto_engine;
1892 ctx->ckpair_index = lctx->ckpair_index;
1893 quic_acquire_crypto_context (ctx);
1894 udp_session->opaque = ctx_index;
1896 /* TODO timeout to delete these if they never connect */
1901 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1903 /* No-op for builtin */
1908 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1910 /* No-op for builtin */
1915 quic_custom_app_rx_callback (transport_connection_t * tc)
1918 session_t *stream_session = session_get (tc->s_index, tc->thread_index);
1919 QUIC_DBG (3, "Received app READ notification");
1920 quic_ack_rx_data (stream_session);
1921 svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
1923 /* Need to send packets (acks may never be sent otherwise) */
1924 ctx = quic_ctx_get (stream_session->connection_index,
1925 stream_session->thread_index);
1926 quic_send_packets (ctx);
1931 quic_custom_tx_callback (void *s, u32 max_burst_size)
1933 session_t *stream_session = (session_t *) s;
1934 quic_stream_data_t *stream_data;
1935 quicly_stream_t *stream;
1941 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1943 ctx = quic_ctx_get (stream_session->connection_index,
1944 stream_session->thread_index);
1945 if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
1947 goto tx_end; /* Most probably a reschedule */
1950 QUIC_DBG (3, "Stream TX event");
1951 quic_ack_rx_data (stream_session);
1952 stream = ctx->stream;
1953 if (!quicly_sendstate_is_open (&stream->sendstate))
1955 QUIC_ERR ("Warning: tried to send on closed stream");
1959 stream_data = (quic_stream_data_t *) stream->data;
1960 max_deq = svm_fifo_max_dequeue (stream_session->tx_fifo);
1961 QUIC_ASSERT (max_deq >= stream_data->app_tx_data_len);
1962 if (max_deq == stream_data->app_tx_data_len)
1964 QUIC_DBG (3, "TX but no data %d / %d", max_deq,
1965 stream_data->app_tx_data_len);
1968 stream_data->app_tx_data_len = max_deq;
1969 rv = quicly_stream_sync_sendbuf (stream, 1);
1973 quic_send_packets (ctx);
1978 * Returns 0 if a matching connection is found and is on the right thread.
1979 * Otherwise returns -1.
1980 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
1984 quic_find_packet_ctx (quic_rx_packet_ctx_t * pctx, u32 caller_thread_index)
1986 clib_bihash_kv_16_8_t kv;
1987 clib_bihash_16_8_t *h;
1989 u32 index, thread_id;
1991 h = &quic_main.connection_hash;
1992 quic_make_connection_key (&kv, &pctx->packet.cid.dest.plaintext);
1993 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
1995 if (clib_bihash_search_16_8 (h, &kv, &kv))
1997 QUIC_DBG (3, "connection not found");
1998 return QUIC_PACKET_TYPE_NONE;
2001 index = kv.value & UINT32_MAX;
2002 thread_id = kv.value >> 32;
2003 /* Check if this connection belongs to this thread, otherwise
2004 * ask for it to be moved */
2005 if (thread_id != caller_thread_index)
2007 QUIC_DBG (2, "Connection is on wrong thread");
2008 /* Cannot make full check with quicly_is_destination... */
2009 pctx->ctx_index = index;
2010 pctx->thread_index = thread_id;
2011 return QUIC_PACKET_TYPE_MIGRATE;
2013 ctx = quic_ctx_get (index, vlib_get_thread_index ());
2016 QUIC_ERR ("ctx has no conn");
2017 return QUIC_PACKET_TYPE_NONE;
2019 if (!quicly_is_destination (ctx->conn, NULL, &pctx->sa, &pctx->packet))
2020 return QUIC_PACKET_TYPE_NONE;
2022 QUIC_DBG (3, "Connection found");
2023 pctx->ctx_index = index;
2024 pctx->thread_index = thread_id;
2025 return QUIC_PACKET_TYPE_RECEIVE;
2029 quic_accept_connection (quic_rx_packet_ctx_t * pctx)
2031 quicly_context_t *quicly_ctx;
2032 session_t *quic_session;
2033 clib_bihash_kv_16_8_t kv;
2034 app_worker_t *app_wrk;
2035 quicly_conn_t *conn;
2040 /* new connection, accept and create context if packet is valid
2041 * TODO: check if socket is actually listening? */
2042 ctx = quic_ctx_get (pctx->ctx_index, pctx->thread_index);
2043 if (ctx->c_s_index != QUIC_SESSION_INVALID)
2045 QUIC_DBG (2, "already accepted ctx 0x%x", ctx->c_s_index);
2049 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
2050 if ((rv = quicly_accept (&conn, quicly_ctx, NULL, &pctx->sa,
2051 &pctx->packet, NULL,
2052 &quic_main.wrk_ctx[pctx->thread_index].next_cid,
2055 /* Invalid packet, pass */
2056 assert (conn == NULL);
2057 QUIC_ERR ("Accept failed with %U", quic_format_err, rv);
2058 /* TODO: cleanup created quic ctx and UDP session */
2061 assert (conn != NULL);
2063 ++quic_main.wrk_ctx[pctx->thread_index].next_cid.master_id;
2064 /* Save ctx handle in quicly connection */
2065 quic_store_conn_ctx (conn, ctx);
2068 quic_session = session_alloc (ctx->c_thread_index);
2069 QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
2070 session_handle (quic_session), ctx->c_c_index);
2071 quic_session->session_state = SESSION_STATE_LISTENING;
2072 ctx->c_s_index = quic_session->session_index;
2074 lctx = quic_ctx_get (ctx->listener_ctx_id, 0);
2076 quic_session->app_wrk_index = lctx->parent_app_wrk_id;
2077 quic_session->connection_index = ctx->c_c_index;
2078 quic_session->session_type =
2079 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
2080 quic_session->listener_handle = lctx->c_s_index;
2082 /* Register connection in connections map */
2083 quic_make_connection_key (&kv, quicly_get_master_id (conn));
2084 kv.value = ((u64) pctx->thread_index) << 32 | (u64) pctx->ctx_index;
2085 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
2086 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
2088 /* If notify fails, reset connection immediatly */
2089 if ((rv = app_worker_init_accepted (quic_session)))
2091 QUIC_ERR ("failed to allocate fifos");
2092 quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2096 app_wrk = app_worker_get (quic_session->app_wrk_index);
2097 if ((rv = app_worker_accept_notify (app_wrk, quic_session)))
2099 QUIC_ERR ("failed to notify accept worker app");
2100 quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2104 ctx->conn_state = QUIC_CONN_STATE_READY;
2108 quic_reset_connection (u64 udp_session_handle, quic_rx_packet_ctx_t * pctx)
2110 /* short header packet; potentially a dead connection. No need to check the
2111 * length of the incoming packet, because loop is prevented by authenticating
2112 * the CID (by checking node_id and thread_id). If the peer is also sending a
2113 * reset, then the next CID is highly likely to contain a non-authenticating
2115 QUIC_DBG (2, "Sending stateless reset");
2117 quicly_datagram_t *dgram;
2118 session_t *udp_session;
2119 quicly_context_t *quicly_ctx;
2120 if (pctx->packet.cid.dest.plaintext.node_id != 0
2121 || pctx->packet.cid.dest.plaintext.thread_id != 0)
2123 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2124 dgram = quicly_send_stateless_reset (quicly_ctx, &pctx->sa, NULL,
2125 &pctx->packet.cid.dest.plaintext);
2128 udp_session = session_get_from_handle (udp_session_handle);
2129 rv = quic_send_datagram (udp_session, dgram);
2130 quic_set_udp_tx_evt (udp_session);
2135 quic_process_one_rx_packet (u64 udp_session_handle, svm_fifo_t * f,
2136 u32 fifo_offset, quic_rx_packet_ctx_t * pctx)
2140 u32 thread_index = vlib_get_thread_index ();
2141 u32 cur_deq = svm_fifo_max_dequeue (f) - fifo_offset;
2142 quicly_context_t *quicly_ctx;
2143 session_t *udp_session;
2146 ret = svm_fifo_peek (f, fifo_offset,
2147 SESSION_CONN_HDR_LEN, (u8 *) & pctx->ph);
2148 QUIC_ASSERT (ret == SESSION_CONN_HDR_LEN);
2149 QUIC_ASSERT (pctx->ph.data_offset == 0);
2150 full_len = pctx->ph.data_length + SESSION_CONN_HDR_LEN;
2151 if (full_len > cur_deq)
2153 QUIC_ERR ("Not enough data in fifo RX");
2157 /* Quicly can read len bytes from the fifo at offset:
2158 * ph.data_offset + SESSION_CONN_HDR_LEN */
2159 ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN + fifo_offset,
2160 pctx->ph.data_length, pctx->data);
2161 if (ret != pctx->ph.data_length)
2163 QUIC_ERR ("Not enough data peeked in RX");
2167 quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
2168 quic_build_sockaddr (&pctx->sa, &pctx->salen, &pctx->ph.rmt_ip,
2169 pctx->ph.rmt_port, pctx->ph.is_ip4);
2170 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2171 plen = quicly_decode_packet (quicly_ctx, &pctx->packet,
2172 pctx->data, pctx->ph.data_length);
2174 if (plen == SIZE_MAX)
2179 rv = quic_find_packet_ctx (pctx, thread_index);
2180 if (rv == QUIC_PACKET_TYPE_RECEIVE)
2182 pctx->ptype = QUIC_PACKET_TYPE_RECEIVE;
2183 quic_ctx_t *qctx = quic_ctx_get (pctx->ctx_index, thread_index);
2184 quic_crypto_decrypt_packet (qctx, pctx);
2187 else if (rv == QUIC_PACKET_TYPE_MIGRATE)
2189 pctx->ptype = QUIC_PACKET_TYPE_MIGRATE;
2190 /* Connection found but on wrong thread, ask move */
2192 else if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
2194 pctx->ptype = QUIC_PACKET_TYPE_ACCEPT;
2195 udp_session = session_get_from_handle (udp_session_handle);
2196 pctx->ctx_index = udp_session->opaque;
2197 pctx->thread_index = thread_index;
2201 pctx->ptype = QUIC_PACKET_TYPE_RESET;
2207 quic_udp_session_rx_callback (session_t * udp_session)
2209 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
2210 quic_main_t *qm = &quic_main;
2211 quic_ctx_t *ctx = NULL, *prev_ctx = NULL;
2212 svm_fifo_t *f = udp_session->rx_fifo;
2214 u64 udp_session_handle = session_handle (udp_session);
2216 u32 thread_index = vlib_get_thread_index ();
2217 u32 cur_deq, fifo_offset, max_packets, i;
2219 quic_rx_packet_ctx_t packets_ctx[QUIC_RCV_MAX_BATCH_PACKETS];
2221 if (udp_session->flags & SESSION_F_IS_MIGRATING)
2223 QUIC_DBG (3, "RX on migrating udp session");
2228 max_deq = svm_fifo_max_dequeue (f);
2233 max_packets = QUIC_RCV_MAX_BATCH_PACKETS;
2236 clib_memset (packets_ctx, 0xfa,
2237 QUIC_RCV_MAX_BATCH_PACKETS * sizeof (quic_rx_packet_ctx_t));
2240 for (i = 0; i < max_packets; i++)
2242 packets_ctx[i].thread_index = UINT32_MAX;
2243 packets_ctx[i].ctx_index = UINT32_MAX;
2244 packets_ctx[i].ptype = QUIC_PACKET_TYPE_DROP;
2246 cur_deq = max_deq - fifo_offset;
2249 max_packets = i + 1;
2252 if (cur_deq < SESSION_CONN_HDR_LEN)
2254 fifo_offset = max_deq;
2255 max_packets = i + 1;
2256 QUIC_ERR ("Fifo %d < header size in RX", cur_deq);
2259 rv = quic_process_one_rx_packet (udp_session_handle, f,
2260 fifo_offset, &packets_ctx[i]);
2261 if (packets_ctx[i].ptype != QUIC_PACKET_TYPE_MIGRATE)
2262 fifo_offset += SESSION_CONN_HDR_LEN + packets_ctx[i].ph.data_length;
2265 max_packets = i + 1;
2270 quic_crypto_batch_rx_packets (&qm->
2271 wrk_ctx[thread_index].crypto_context_batch);
2273 for (i = 0; i < max_packets; i++)
2275 switch (packets_ctx[i].ptype)
2277 case QUIC_PACKET_TYPE_RECEIVE:
2278 ctx = quic_ctx_get (packets_ctx[i].ctx_index, thread_index);
2279 rv = quicly_receive (ctx->conn, NULL, &packets_ctx[i].sa,
2280 &packets_ctx[i].packet);
2281 if (rv && rv != QUICLY_ERROR_PACKET_IGNORED)
2283 QUIC_ERR ("quicly_receive return error %U",
2284 quic_format_err, rv);
2287 case QUIC_PACKET_TYPE_ACCEPT:
2288 quic_accept_connection (&packets_ctx[i]);
2290 case QUIC_PACKET_TYPE_RESET:
2291 quic_reset_connection (udp_session_handle, &packets_ctx[i]);
2295 ctx = prev_ctx = NULL;
2296 for (i = 0; i < max_packets; i++)
2299 switch (packets_ctx[i].ptype)
2301 case QUIC_PACKET_TYPE_RECEIVE:
2302 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2303 packets_ctx[i].thread_index);
2304 quic_check_quic_session_connected (ctx);
2305 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2306 packets_ctx[i].thread_index);
2308 case QUIC_PACKET_TYPE_ACCEPT:
2309 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2310 packets_ctx[i].thread_index);
2313 continue; /* this exits the for loop since other packet types are
2314 necessarily the last in the batch */
2316 if (ctx != prev_ctx)
2317 quic_send_packets (ctx);
2320 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
2321 f = udp_session->rx_fifo;
2322 svm_fifo_dequeue_drop (f, fifo_offset);
2324 if (svm_fifo_max_dequeue (f))
2331 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2332 transport_endpoint_t * tep, u8 is_lcl)
2334 session_t *udp_session;
2335 if (!quic_ctx_is_stream (ctx))
2337 udp_session = session_get_from_handle (ctx->udp_session_handle);
2338 session_get_endpoint (udp_session, tep, is_lcl);
2343 quic_get_transport_listener_endpoint (u32 listener_index,
2344 transport_endpoint_t * tep, u8 is_lcl)
2347 app_listener_t *app_listener;
2348 session_t *udp_listen_session;
2349 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2350 if (quic_ctx_is_listener (ctx))
2352 app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2353 udp_listen_session = app_listener_get_session (app_listener);
2354 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2356 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2360 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2361 transport_endpoint_t * tep, u8 is_lcl)
2364 ctx = quic_ctx_get (ctx_index, thread_index);
2365 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2369 static session_cb_vft_t quic_app_cb_vft = {
2370 .session_accept_callback = quic_udp_session_accepted_callback,
2371 .session_disconnect_callback = quic_udp_session_disconnect_callback,
2372 .session_connected_callback = quic_udp_session_connected_callback,
2373 .session_reset_callback = quic_udp_session_reset_callback,
2374 .session_migrate_callback = quic_udp_session_migrate_callback,
2375 .add_segment_callback = quic_add_segment_callback,
2376 .del_segment_callback = quic_del_segment_callback,
2377 .builtin_app_rx_callback = quic_udp_session_rx_callback,
2378 .session_cleanup_callback = quic_udp_session_cleanup_callback,
2379 .app_cert_key_pair_delete_callback = quic_app_cert_key_pair_delete_callback,
2382 static const transport_proto_vft_t quic_proto = {
2383 .connect = quic_connect,
2384 .close = quic_proto_on_close,
2385 .start_listen = quic_start_listen,
2386 .stop_listen = quic_stop_listen,
2387 .get_connection = quic_connection_get,
2388 .get_listener = quic_listener_get,
2389 .update_time = quic_update_time,
2390 .app_rx_evt = quic_custom_app_rx_callback,
2391 .custom_tx = quic_custom_tx_callback,
2392 .format_connection = format_quic_connection,
2393 .format_half_open = format_quic_half_open,
2394 .format_listener = format_quic_listener,
2395 .get_transport_endpoint = quic_get_transport_endpoint,
2396 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2397 .transport_options = {
2398 .tx_type = TRANSPORT_TX_INTERNAL,
2399 .service_type = TRANSPORT_SERVICE_APP,
2404 static quicly_stream_open_t on_stream_open = { quic_on_stream_open };
2405 static quicly_closed_by_peer_t on_closed_by_peer = { quic_on_closed_by_peer };
2406 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
2409 quic_register_cipher_suite (crypto_engine_type_t type,
2410 ptls_cipher_suite_t ** ciphers)
2412 quic_main_t *qm = &quic_main;
2413 vec_validate (qm->quic_ciphers, type);
2414 clib_bitmap_set (qm->available_crypto_engines, type, 1);
2415 qm->quic_ciphers[type] = ciphers;
2419 quic_update_fifo_size ()
2421 quic_main_t *qm = &quic_main;
2422 segment_manager_props_t *seg_mgr_props =
2423 application_get_segment_manager_properties (qm->app_index);
2428 ("error while getting segment_manager_props_t, can't update fifo-size");
2432 seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
2433 seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
2436 static clib_error_t *
2437 quic_init (vlib_main_t * vm)
2439 u32 segment_size = 256 << 20;
2440 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2441 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2442 vnet_app_attach_args_t _a, *a = &_a;
2443 u64 options[APP_OPTIONS_N_OPTIONS];
2444 quic_main_t *qm = &quic_main;
2447 num_threads = 1 /* main thread */ + vtm->n_threads;
2449 clib_memset (a, 0, sizeof (*a));
2450 clib_memset (options, 0, sizeof (options));
2452 a->session_cb_vft = &quic_app_cb_vft;
2453 a->api_client_index = APP_INVALID_INDEX;
2454 a->options = options;
2455 a->name = format (0, "quic");
2456 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2457 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2458 a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
2459 a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
2460 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
2461 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2462 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2463 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2465 if (vnet_application_attach (a))
2467 clib_warning ("failed to attach quic app");
2468 return clib_error_return (0, "failed to attach quic app");
2471 vec_validate (qm->ctx_pool, num_threads - 1);
2472 vec_validate (qm->wrk_ctx, num_threads - 1);
2474 for (i = 0; i < num_threads; i++)
2476 qm->wrk_ctx[i].next_cid.thread_id = i;
2477 tw = &qm->wrk_ctx[i].timer_wheel;
2478 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2479 1e-3 /* timer period 1ms */ , ~0);
2480 tw->last_run_time = vlib_time_now (vlib_get_main ());
2481 clib_bihash_init_24_8 (&qm->wrk_ctx[i].crypto_context_hash,
2482 "quic crypto contexts", 64, 128 << 10);
2484 qm->wrk_ctx[i].crypto_context_batch.nb_rx_packets = 0;
2485 qm->wrk_ctx[i].crypto_context_batch.nb_tx_packets = 0;
2488 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2491 qm->app_index = a->app_index;
2492 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2493 / QUIC_TSTAMP_RESOLUTION;
2494 qm->session_cache.super.cb = quic_encrypt_ticket_cb;
2496 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2497 FIB_PROTOCOL_IP4, ~0);
2498 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2499 FIB_PROTOCOL_IP6, ~0);
2501 clib_bitmap_alloc (qm->available_crypto_engines,
2502 app_crypto_engine_n_types ());
2503 quic_register_cipher_suite (CRYPTO_ENGINE_VPP, quic_crypto_cipher_suites);
2504 quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
2505 ptls_openssl_cipher_suites);
2506 qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2507 qm->max_packets_per_key = DEFAULT_MAX_PACKETS_PER_KEY;
2508 clib_rwlock_init (&qm->crypto_keys_quic_rw_lock);
2513 VLIB_INIT_FUNCTION (quic_init);
2515 static clib_error_t *
2516 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2517 unformat_input_t * input,
2518 vlib_cli_command_t * cmd)
2520 quic_main_t *qm = &quic_main;
2521 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
2522 return clib_error_return (0, "unknown input '%U'",
2523 format_unformat_error, input);
2524 if (unformat (input, "vpp"))
2525 qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2526 else if (unformat (input, "picotls"))
2527 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2529 return clib_error_return (0, "unknown input '%U'",
2530 format_unformat_error, input);
2534 u64 quic_fifosize = 0;
2535 static clib_error_t *
2536 quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
2537 unformat_input_t * input,
2538 vlib_cli_command_t * cmd)
2540 quic_main_t *qm = &quic_main;
2541 unformat_input_t _line_input, *line_input = &_line_input;
2544 if (!unformat_user (input, unformat_line_input, line_input))
2547 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2549 if (unformat (line_input, "%U", unformat_memory_size, &tmp))
2551 if (tmp >= 0x100000000ULL)
2553 return clib_error_return
2554 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
2556 qm->udp_fifo_size = tmp;
2557 quic_update_fifo_size ();
2560 return clib_error_return (0, "unknown input '%U'",
2561 format_unformat_error, line_input);
2568 quic_get_counter_value (u32 event_code)
2572 vlib_error_main_t *em;
2578 vm = vlib_get_main ();
2579 em = &vm->error_main;
2580 n = vlib_get_node (vm, quic_input_node.index);
2583 foreach_vlib_main(({
2584 em = &this_vlib_main->error_main;
2585 i = n->error_heap_index + code;
2586 c = em->counters[i];
2588 if (i < vec_len (em->counters_last_clear))
2589 c -= em->counters_last_clear[i];
2598 quic_show_aggregated_stats (vlib_main_t * vm)
2600 u32 num_workers = vlib_num_workers ();
2601 quic_main_t *qm = &quic_main;
2602 quic_ctx_t *ctx = NULL;
2603 quicly_stats_t st, agg_stats;
2604 u32 i, nconn = 0, nstream = 0;
2606 clib_memset (&agg_stats, 0, sizeof (agg_stats));
2607 for (i = 0; i < num_workers + 1; i++)
2610 pool_foreach (ctx, qm->ctx_pool[i],
2612 if (quic_ctx_is_conn (ctx) && ctx->conn)
2614 quicly_get_stats (ctx->conn, &st);
2615 agg_stats.rtt.smoothed += st.rtt.smoothed;
2616 agg_stats.rtt.minimum += st.rtt.minimum;
2617 agg_stats.rtt.variance += st.rtt.variance;
2618 agg_stats.num_packets.received += st.num_packets.received;
2619 agg_stats.num_packets.sent += st.num_packets.sent;
2620 agg_stats.num_packets.lost += st.num_packets.lost;
2621 agg_stats.num_packets.ack_received += st.num_packets.ack_received;
2622 agg_stats.num_bytes.received += st.num_bytes.received;
2623 agg_stats.num_bytes.sent += st.num_bytes.sent;
2626 else if (quic_ctx_is_stream (ctx))
2631 vlib_cli_output (vm, "-------- Connections --------");
2632 vlib_cli_output (vm, "Current: %u", nconn);
2633 vlib_cli_output (vm, "Opened: %d",
2634 quic_get_counter_value (QUIC_ERROR_OPENED_CONNECTION));
2635 vlib_cli_output (vm, "Closed: %d",
2636 quic_get_counter_value (QUIC_ERROR_CLOSED_CONNECTION));
2637 vlib_cli_output (vm, "---------- Streams ----------");
2638 vlib_cli_output (vm, "Current: %u", nstream);
2639 vlib_cli_output (vm, "Opened: %d",
2640 quic_get_counter_value (QUIC_ERROR_OPENED_STREAM));
2641 vlib_cli_output (vm, "Closed: %d",
2642 quic_get_counter_value (QUIC_ERROR_CLOSED_STREAM));
2643 vlib_cli_output (vm, "---------- Packets ----------");
2644 vlib_cli_output (vm, "RX Total: %d",
2645 quic_get_counter_value (QUIC_ERROR_RX_PACKETS));
2646 vlib_cli_output (vm, "RX 0RTT: %d",
2647 quic_get_counter_value (QUIC_ERROR_ZERO_RTT_RX_PACKETS));
2648 vlib_cli_output (vm, "RX 1RTT: %d",
2649 quic_get_counter_value (QUIC_ERROR_ONE_RTT_RX_PACKETS));
2650 vlib_cli_output (vm, "TX Total: %d",
2651 quic_get_counter_value (QUIC_ERROR_TX_PACKETS));
2652 vlib_cli_output (vm, "----------- Stats -----------");
2653 vlib_cli_output (vm, "Min RTT %f",
2654 nconn > 0 ? agg_stats.rtt.minimum / nconn : 0);
2655 vlib_cli_output (vm, "Smoothed RTT %f",
2656 nconn > 0 ? agg_stats.rtt.smoothed / nconn : 0);
2657 vlib_cli_output (vm, "Variance on RTT %f",
2658 nconn > 0 ? agg_stats.rtt.variance / nconn : 0);
2659 vlib_cli_output (vm, "Packets Received %lu",
2660 agg_stats.num_packets.received);
2661 vlib_cli_output (vm, "Packets Sent %lu", agg_stats.num_packets.sent);
2662 vlib_cli_output (vm, "Packets Lost %lu", agg_stats.num_packets.lost);
2663 vlib_cli_output (vm, "Packets Acks %lu",
2664 agg_stats.num_packets.ack_received);
2665 vlib_cli_output (vm, "RX bytes %lu", agg_stats.num_bytes.received);
2666 vlib_cli_output (vm, "TX bytes %lu", agg_stats.num_bytes.sent);
2670 quic_format_quicly_conn_id (u8 * s, va_list * args)
2672 quicly_cid_plaintext_t *mid = va_arg (*args, quicly_cid_plaintext_t *);
2673 s = format (s, "C%x_%x", mid->master_id, mid->thread_id);
2678 quic_format_quicly_stream_id (u8 * s, va_list * args)
2680 quicly_stream_t *stream = va_arg (*args, quicly_stream_t *);
2682 format (s, "%U S%lx", quic_format_quicly_conn_id,
2683 quicly_get_master_id (stream->conn), stream->stream_id);
2688 quic_format_listener_ctx (u8 * s, va_list * args)
2690 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2691 s = format (s, "[#%d][%x][Listener]", ctx->c_thread_index, ctx->c_c_index);
2696 quic_format_connection_ctx (u8 * s, va_list * args)
2698 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2699 quicly_stats_t quicly_stats;
2701 s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2705 s = format (s, "- no conn -\n");
2708 s = format (s, "[%U]",
2709 quic_format_quicly_conn_id, quicly_get_master_id (ctx->conn));
2710 quicly_get_stats (ctx->conn, &quicly_stats);
2712 s = format (s, "[RTT >%3d, ~%3d, V%3d, last %3d]",
2713 quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
2714 quicly_stats.rtt.variance, quicly_stats.rtt.latest);
2715 s = format (s, " TX:%d RX:%d loss:%d ack:%d",
2716 quicly_stats.num_packets.sent,
2717 quicly_stats.num_packets.received,
2718 quicly_stats.num_packets.lost,
2719 quicly_stats.num_packets.ack_received);
2724 quic_format_stream_ctx (u8 * s, va_list * args)
2726 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2727 session_t *stream_session;
2728 quicly_stream_t *stream = ctx->stream;
2731 s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2732 s = format (s, "[%U]", quic_format_quicly_stream_id, stream);
2734 stream_session = session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
2735 if (!stream_session)
2737 s = format (s, "- no session -\n");
2740 txs = svm_fifo_max_dequeue (stream_session->tx_fifo);
2741 rxs = svm_fifo_max_dequeue (stream_session->rx_fifo);
2742 s = format (s, "[rx %d tx %d]\n", rxs, txs);
2746 static clib_error_t *
2747 quic_show_connections_command_fn (vlib_main_t * vm,
2748 unformat_input_t * input,
2749 vlib_cli_command_t * cmd)
2751 unformat_input_t _line_input, *line_input = &_line_input;
2752 u8 show_listeners = 0, show_conn = 0, show_stream = 0;
2753 u32 num_workers = vlib_num_workers ();
2754 quic_main_t *qm = &quic_main;
2755 clib_error_t *error = 0;
2756 quic_ctx_t *ctx = NULL;
2758 session_cli_return_if_not_enabled ();
2760 if (!unformat_user (input, unformat_line_input, line_input))
2762 quic_show_aggregated_stats (vm);
2766 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2768 if (unformat (line_input, "listener"))
2770 else if (unformat (line_input, "conn"))
2772 else if (unformat (line_input, "stream"))
2776 error = clib_error_return (0, "unknown input `%U'",
2777 format_unformat_error, line_input);
2782 for (int i = 0; i < num_workers + 1; i++)
2785 pool_foreach (ctx, qm->ctx_pool[i],
2787 if (quic_ctx_is_stream (ctx) && show_stream)
2788 vlib_cli_output (vm, "%U", quic_format_stream_ctx, ctx);
2789 else if (quic_ctx_is_listener (ctx) && show_listeners)
2790 vlib_cli_output (vm, "%U", quic_format_listener_ctx, ctx);
2791 else if (quic_ctx_is_conn (ctx) && show_conn)
2792 vlib_cli_output (vm, "%U", quic_format_connection_ctx, ctx);
2798 unformat_free (line_input);
2803 VLIB_CLI_COMMAND (quic_plugin_crypto_command, static) =
2805 .path = "quic set crypto api",
2806 .short_help = "quic set crypto api [picotls, vpp]",
2807 .function = quic_plugin_crypto_command_fn,
2809 VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
2811 .path = "quic set fifo-size",
2812 .short_help = "quic set fifo-size N[K|M|G] (default 64K)",
2813 .function = quic_plugin_set_fifo_size_command_fn,
2815 VLIB_CLI_COMMAND(quic_show_ctx_command, static)=
2817 .path = "show quic",
2818 .short_help = "show quic",
2819 .function = quic_show_connections_command_fn,
2821 VLIB_CLI_COMMAND (quic_list_crypto_context_command, static) =
2823 .path = "show quic crypto context",
2824 .short_help = "list quic crypto contextes",
2825 .function = quic_list_crypto_context_command_fn,
2827 VLIB_CLI_COMMAND (quic_set_max_packets_per_key, static) =
2829 .path = "set quic max_packets_per_key",
2830 .short_help = "set quic max_packets_per_key 16777216",
2831 .function = quic_set_max_packets_per_key_fn,
2833 VLIB_PLUGIN_REGISTER () =
2835 .version = VPP_BUILD_VER,
2836 .description = "Quic transport protocol",
2837 .default_disabled = 1,
2841 static clib_error_t *
2842 quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
2844 quic_main_t *qm = &quic_main;
2848 qm->udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
2849 qm->udp_fifo_prealloc = 0;
2850 qm->connection_timeout = QUIC_DEFAULT_CONN_TIMEOUT;
2851 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2853 if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
2855 if (tmp >= 0x100000000ULL)
2857 return clib_error_return (0,
2858 "fifo-size %llu (0x%llx) too large",
2861 qm->udp_fifo_size = tmp;
2863 else if (unformat (input, "conn-timeout %u", &i))
2864 qm->connection_timeout = i;
2865 else if (unformat (input, "fifo-prealloc %u", &i))
2866 qm->udp_fifo_prealloc = i;
2868 return clib_error_return (0, "unknown input '%U'",
2869 format_unformat_error, input);
2875 VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
2878 quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
2879 vlib_frame_t * frame)
2885 VLIB_REGISTER_NODE (quic_input_node) =
2887 .function = quic_node_fn,
2888 .name = "quic-input",
2889 .vector_size = sizeof (u32),
2890 .type = VLIB_NODE_TYPE_INTERNAL,
2891 .n_errors = ARRAY_LEN (quic_error_strings),
2892 .error_strings = quic_error_strings,
2897 * fd.io coding-style-patch-verification: ON
2900 * eval: (c-set-style "gnu")