2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <sys/socket.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/transport.h>
20 #include <vnet/session/session.h>
21 #include <vlib/unix/plugin.h>
22 #include <vpp/app/version.h>
24 #include <vppinfra/lock.h>
26 #include <quic/quic.h>
27 #include <quic/certs.h>
28 #include <quic/error.h>
29 #include <quic/quic_crypto.h>
31 #include <quicly/constants.h>
32 #include <quicly/defaults.h>
34 static char *quic_error_strings[] = {
35 #define quic_error(n,s) s,
36 #include <quic/quic_error.def>
40 static quic_main_t quic_main;
41 static void quic_update_timer (quic_ctx_t * ctx);
42 static void quic_check_quic_session_connected (quic_ctx_t * ctx);
43 static int quic_reset_connection (u64 udp_session_handle,
44 quic_rx_packet_ctx_t * pctx);
45 static void quic_proto_on_close (u32 ctx_index, u32 thread_index);
47 static quicly_stream_open_t on_stream_open;
48 static quicly_closed_by_peer_t on_closed_by_peer;
49 static quicly_now_t quicly_vpp_now_cb;
54 quic_crypto_context_make_key_from_ctx (clib_bihash_kv_24_8_t * kv,
57 application_t *app = application_get (ctx->parent_app_id);
58 kv->key[0] = ((u64) ctx->ckpair_index) << 32 | (u64) ctx->crypto_engine;
59 kv->key[1] = app->sm_properties.rx_fifo_size - 1;
60 kv->key[2] = app->sm_properties.tx_fifo_size - 1;
64 quic_crypto_context_make_key_from_crctx (clib_bihash_kv_24_8_t * kv,
65 crypto_context_t * crctx)
67 quic_crypto_context_data_t *data =
68 (quic_crypto_context_data_t *) crctx->data;
69 kv->key[0] = ((u64) crctx->ckpair_index) << 32 | (u64) crctx->crypto_engine;
70 kv->key[1] = data->quicly_ctx.transport_params.max_stream_data.bidi_local;
71 kv->key[2] = data->quicly_ctx.transport_params.max_stream_data.bidi_remote;
75 quic_crypto_context_free_if_needed (crypto_context_t * crctx, u8 thread_index)
77 quic_main_t *qm = &quic_main;
78 clib_bihash_kv_24_8_t kv;
79 if (crctx->n_subscribers)
81 quic_crypto_context_make_key_from_crctx (&kv, crctx);
82 clib_bihash_add_del_24_8 (&qm->wrk_ctx[thread_index].crypto_context_hash,
83 &kv, 0 /* is_add */ );
84 clib_mem_free (crctx->data);
85 pool_put (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
89 quic_app_cert_key_pair_delete_callback (app_cert_key_pair_t * ckpair)
91 quic_main_t *qm = &quic_main;
92 crypto_context_t *crctx;
93 clib_bihash_kv_24_8_t kv;
94 vlib_thread_main_t *vtm = vlib_get_thread_main ();
95 int num_threads = 1 /* main thread */ + vtm->n_threads;
98 for (i = 0; i < num_threads; i++)
101 pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
102 if (crctx->ckpair_index == ckpair->cert_key_index)
104 quic_crypto_context_make_key_from_crctx (&kv, crctx);
105 clib_bihash_add_del_24_8 (&qm->wrk_ctx[i].crypto_context_hash, &kv, 0 /* is_add */ );
113 static crypto_context_t *
114 quic_crypto_context_alloc (u8 thread_index)
116 quic_main_t *qm = &quic_main;
117 crypto_context_t *crctx;
120 pool_get (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
121 clib_memset (crctx, 0, sizeof (*crctx));
122 idx = (crctx - qm->wrk_ctx[thread_index].crypto_ctx_pool);
123 crctx->ctx_index = ((u32) thread_index) << 24 | idx;
128 static crypto_context_t *
129 quic_crypto_context_get (u32 cr_index, u32 thread_index)
131 quic_main_t *qm = &quic_main;
132 ASSERT (cr_index >> 24 == thread_index);
133 return pool_elt_at_index (qm->wrk_ctx[thread_index].crypto_ctx_pool,
134 cr_index & 0x00ffffff);
137 static clib_error_t *
138 quic_list_crypto_context_command_fn (vlib_main_t * vm,
139 unformat_input_t * input,
140 vlib_cli_command_t * cmd)
142 quic_main_t *qm = &quic_main;
143 crypto_context_t *crctx;
144 vlib_thread_main_t *vtm = vlib_get_thread_main ();
145 int i, num_threads = 1 /* main thread */ + vtm->n_threads;
146 for (i = 0; i < num_threads; i++)
149 pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
150 vlib_cli_output (vm, "[%d][Q]%U", i, format_crypto_context, crctx);
158 quic_release_crypto_context (u32 crypto_context_index, u8 thread_index)
160 crypto_context_t *crctx;
161 crctx = quic_crypto_context_get (crypto_context_index, thread_index);
162 crctx->n_subscribers--;
163 quic_crypto_context_free_if_needed (crctx, thread_index);
167 quic_init_crypto_context (crypto_context_t * crctx, quic_ctx_t * ctx)
169 quic_main_t *qm = &quic_main;
170 quicly_context_t *quicly_ctx;
171 ptls_iovec_t key_vec;
172 app_cert_key_pair_t *ckpair;
174 quic_crypto_context_data_t *data;
175 ptls_context_t *ptls_ctx;
177 QUIC_DBG (2, "Init quic crctx %d thread %d", crctx->ctx_index,
178 ctx->c_thread_index);
180 data = clib_mem_alloc (sizeof (*data));
181 /* picotls depends on data being zeroed */
182 clib_memset (data, 0, sizeof (*data));
183 crctx->data = (void *) data;
184 quicly_ctx = &data->quicly_ctx;
185 ptls_ctx = &data->ptls_ctx;
187 ptls_ctx->random_bytes = ptls_openssl_random_bytes;
188 ptls_ctx->get_time = &ptls_get_time;
189 ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
190 ptls_ctx->cipher_suites = qm->quic_ciphers[ctx->crypto_engine];
191 ptls_ctx->certificates.list = NULL;
192 ptls_ctx->certificates.count = 0;
193 ptls_ctx->esni = NULL;
194 ptls_ctx->on_client_hello = NULL;
195 ptls_ctx->emit_certificate = NULL;
196 ptls_ctx->sign_certificate = NULL;
197 ptls_ctx->verify_certificate = NULL;
198 ptls_ctx->ticket_lifetime = 86400;
199 ptls_ctx->max_early_data_size = 8192;
200 ptls_ctx->hkdf_label_prefix__obsolete = NULL;
201 ptls_ctx->require_dhe_on_psk = 1;
202 ptls_ctx->encrypt_ticket = &qm->session_cache.super;
203 clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
205 quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
206 quicly_ctx->tls = ptls_ctx;
207 quicly_ctx->stream_open = &on_stream_open;
208 quicly_ctx->closed_by_peer = &on_closed_by_peer;
209 quicly_ctx->now = &quicly_vpp_now_cb;
210 quicly_amend_ptls_context (quicly_ctx->tls);
212 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
213 quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
214 quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
215 quicly_ctx->transport_params.max_idle_timeout = qm->connection_timeout;
217 app = application_get (ctx->parent_app_id);
218 quicly_ctx->transport_params.max_stream_data.bidi_local =
219 app->sm_properties.rx_fifo_size - 1;
220 quicly_ctx->transport_params.max_stream_data.bidi_remote =
221 app->sm_properties.tx_fifo_size - 1;
222 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
224 if (!app->quic_iv_set)
226 ptls_openssl_random_bytes (app->quic_iv, QUIC_IV_LEN - 1);
227 app->quic_iv[QUIC_IV_LEN - 1] = 0;
228 app->quic_iv_set = 1;
231 clib_memcpy (data->cid_key, app->quic_iv, QUIC_IV_LEN);
232 key_vec = ptls_iovec_init (data->cid_key, QUIC_IV_LEN);
233 quicly_ctx->cid_encryptor =
234 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
235 &ptls_openssl_aes128ecb,
236 &ptls_openssl_sha256, key_vec);
238 ckpair = app_cert_key_pair_get_if_valid (crctx->ckpair_index);
239 if (!ckpair || !ckpair->key || !ckpair->cert)
241 QUIC_DBG (1, "Wrong ckpair id %d\n", crctx->ckpair_index);
244 if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
246 QUIC_DBG (1, "failed to read private key from app configuration\n");
249 if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
251 QUIC_DBG (1, "failed to load certificate\n");
259 quic_acquire_crypto_context (quic_ctx_t * ctx)
261 quic_main_t *qm = &quic_main;
262 crypto_context_t *crctx;
263 clib_bihash_kv_24_8_t kv;
265 if (ctx->crypto_engine == CRYPTO_ENGINE_NONE)
267 QUIC_DBG (2, "No crypto engine specified, using %d",
268 qm->default_crypto_engine);
269 ctx->crypto_engine = qm->default_crypto_engine;
271 if (!clib_bitmap_get (qm->available_crypto_engines, ctx->crypto_engine))
273 QUIC_DBG (1, "Quic does not support crypto engine %d",
275 return VNET_API_ERROR_MISSING_CERT_KEY;
278 /* Check for exisiting crypto ctx */
279 quic_crypto_context_make_key_from_ctx (&kv, ctx);
280 if (clib_bihash_search_24_8
281 (&qm->wrk_ctx[ctx->c_thread_index].crypto_context_hash, &kv, &kv) == 0)
283 crctx = quic_crypto_context_get (kv.value, ctx->c_thread_index);
284 QUIC_DBG (2, "Found exisiting crypto context %d", kv.value);
285 ctx->crypto_context_index = kv.value;
286 crctx->n_subscribers++;
290 crctx = quic_crypto_context_alloc (ctx->c_thread_index);
291 ctx->crypto_context_index = crctx->ctx_index;
292 kv.value = crctx->ctx_index;
293 crctx->crypto_engine = ctx->crypto_engine;
294 crctx->ckpair_index = ctx->ckpair_index;
295 if (quic_init_crypto_context (crctx, ctx))
297 if (vnet_app_add_cert_key_interest (ctx->ckpair_index, qm->app_index))
299 crctx->n_subscribers++;
300 clib_bihash_add_del_24_8 (&qm->
301 wrk_ctx[ctx->c_thread_index].crypto_context_hash,
302 &kv, 1 /* is_add */ );
306 quic_crypto_context_free_if_needed (crctx, ctx->c_thread_index);
307 return VNET_API_ERROR_MISSING_CERT_KEY;
310 /* Helper functions */
313 quic_ctx_alloc (u32 thread_index)
315 quic_main_t *qm = &quic_main;
318 pool_get (qm->ctx_pool[thread_index], ctx);
320 clib_memset (ctx, 0, sizeof (quic_ctx_t));
321 ctx->c_thread_index = thread_index;
322 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
323 QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
324 ctx - qm->ctx_pool[thread_index], thread_index);
325 return ctx - qm->ctx_pool[thread_index];
329 quic_ctx_free (quic_ctx_t * ctx)
331 QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
332 u32 thread_index = ctx->c_thread_index;
333 QUIC_ASSERT (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID);
335 clib_memset (ctx, 0xfb, sizeof (*ctx));
336 pool_put (quic_main.ctx_pool[thread_index], ctx);
340 quic_ctx_get (u32 ctx_index, u32 thread_index)
342 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
346 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
348 if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
350 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
354 quic_get_conn_ctx (quicly_conn_t * conn)
357 conn_data = (u64) * quicly_get_data (conn);
358 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
362 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
364 *quicly_get_data (conn) =
365 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
369 quic_ctx_is_stream (quic_ctx_t * ctx)
371 return (ctx->flags & QUIC_F_IS_STREAM);
375 quic_ctx_is_listener (quic_ctx_t * ctx)
377 return (ctx->flags & QUIC_F_IS_LISTENER);
381 quic_ctx_is_conn (quic_ctx_t * ctx)
383 return !(quic_ctx_is_listener (ctx) || quic_ctx_is_stream (ctx));
387 get_stream_session_from_stream (quicly_stream_t * stream)
390 quic_stream_data_t *stream_data;
392 stream_data = (quic_stream_data_t *) stream->data;
393 ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
394 return session_get (ctx->c_s_index, stream_data->thread_index);
398 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
399 const quicly_cid_plaintext_t * id)
401 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
402 kv->key[1] = id->node_id;
406 quic_sendable_packet_count (session_t * udp_session)
409 u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
410 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
411 return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
414 static quicly_context_t *
415 quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
417 crypto_context_t *crctx =
418 quic_crypto_context_get (ctx->crypto_context_index, ctx->c_thread_index);
419 quic_crypto_context_data_t *data =
420 (quic_crypto_context_data_t *) crctx->data;
421 return &data->quicly_ctx;
424 static quicly_context_t *
425 quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
427 session_t *udp_session = session_get_from_handle (udp_session_handle);
429 quic_ctx_get (udp_session->opaque, udp_session->thread_index);
430 return quic_get_quicly_ctx_from_ctx (ctx);
434 quic_set_udp_tx_evt (session_t * udp_session)
437 if (svm_fifo_set_event (udp_session->tx_fifo))
438 rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
440 if (PREDICT_FALSE (rv))
441 clib_warning ("Event enqueue errored %d", rv);
445 quic_stop_ctx_timer (quic_ctx_t * ctx)
447 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
448 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
450 tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
451 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
452 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
453 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
456 /* QUIC protocol actions */
459 quic_ack_rx_data (session_t * stream_session)
464 quicly_stream_t *stream;
465 quic_stream_data_t *stream_data;
467 sctx = quic_ctx_get (stream_session->connection_index,
468 stream_session->thread_index);
469 QUIC_ASSERT (quic_ctx_is_stream (sctx));
470 stream = sctx->stream;
471 stream_data = (quic_stream_data_t *) stream->data;
473 f = stream_session->rx_fifo;
474 max_deq = svm_fifo_max_dequeue (f);
476 QUIC_ASSERT (stream_data->app_rx_data_len >= max_deq);
477 quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
478 QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
479 stream_data->app_rx_data_len = max_deq;
483 quic_disconnect_transport (quic_ctx_t * ctx)
485 QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
486 vnet_disconnect_args_t a = {
487 .handle = ctx->udp_session_handle,
488 .app_index = quic_main.app_index,
491 if (vnet_disconnect_session (&a))
492 clib_warning ("UDP session 0x%lx disconnect errored",
493 ctx->udp_session_handle);
497 quic_connection_delete (quic_ctx_t * ctx)
499 clib_bihash_kv_16_8_t kv;
502 QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
504 QUIC_ASSERT (!quic_ctx_is_stream (ctx));
505 quic_stop_ctx_timer (ctx);
507 /* Delete the connection from the connection map */
510 quic_make_connection_key (&kv, quicly_get_master_id (conn));
511 QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
513 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
515 quic_disconnect_transport (ctx);
518 quicly_free (ctx->conn);
519 session_transport_delete_notify (&ctx->connection);
523 quic_increment_counter (u8 evt, u8 val)
525 vlib_main_t *vm = vlib_get_main ();
526 vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
530 * Called when quicly return an error
531 * This function interacts tightly with quic_proto_on_close
534 quic_connection_closed (quic_ctx_t * ctx)
536 QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
539 /* TODO if connection is not established, just delete the session? */
540 /* Actually should send connect or accept error */
542 switch (ctx->conn_state)
544 case QUIC_CONN_STATE_READY:
545 /* Error on an opened connection (timeout...)
546 This puts the session in closing state, we should receive a notification
547 when the app has closed its session */
548 session_transport_reset_notify (&ctx->connection);
549 /* This ensures we delete the connection when the app confirms the close */
550 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
552 case QUIC_CONN_STATE_PASSIVE_CLOSING:
553 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
554 /* quic_proto_on_close will eventually be called when the app confirms the close
555 , we delete the connection at that point */
557 case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
558 /* App already confirmed close, we can delete the connection */
559 quic_connection_delete (ctx);
561 case QUIC_CONN_STATE_OPENED:
562 case QUIC_CONN_STATE_HANDSHAKE:
563 case QUIC_CONN_STATE_ACTIVE_CLOSING:
564 quic_connection_delete (ctx);
567 QUIC_DBG (0, "BUG %d", ctx->conn_state);
573 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
576 session_dgram_hdr_t hdr;
579 transport_connection_t *tc;
581 len = packet->data.len;
582 f = udp_session->tx_fifo;
583 tc = session_get_transport (udp_session);
584 max_enqueue = svm_fifo_max_enqueue (f);
585 if (max_enqueue < SESSION_CONN_HDR_LEN + len)
587 QUIC_ERR ("Too much data to send, max_enqueue %u, len %u",
588 max_enqueue, len + SESSION_CONN_HDR_LEN);
589 return QUIC_ERROR_FULL_FIFO;
592 /* Build packet header for fifo */
593 hdr.data_length = len;
595 hdr.is_ip4 = tc->is_ip4;
596 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
597 hdr.lcl_port = tc->lcl_port;
599 /* Read dest address from quicly-provided sockaddr */
602 QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET);
603 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
604 hdr.rmt_port = sa4->sin_port;
605 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
609 QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET6);
610 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
611 hdr.rmt_port = sa6->sin6_port;
612 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
615 ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
616 if (ret != sizeof (hdr))
618 QUIC_ERR ("Not enough space to enqueue header");
619 return QUIC_ERROR_FULL_FIFO;
621 ret = svm_fifo_enqueue (f, len, packet->data.base);
624 QUIC_ERR ("Not enough space to enqueue payload");
625 return QUIC_ERROR_FULL_FIFO;
628 quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
634 quic_send_packets (quic_ctx_t * ctx)
636 quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
637 session_t *udp_session;
639 size_t num_packets, i, max_packets;
640 quicly_packet_allocator_t *pa;
643 /* We have sctx, get qctx */
644 if (quic_ctx_is_stream (ctx))
645 ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
647 QUIC_ASSERT (!quic_ctx_is_stream (ctx));
649 udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
658 /* TODO : quicly can assert it can send min_packets up to 2 */
659 if (quic_sendable_packet_count (udp_session) < 2)
662 pa = quic_get_quicly_ctx_from_ctx (ctx)->packet_allocator;
665 max_packets = quic_sendable_packet_count (udp_session);
668 num_packets = max_packets;
669 if ((err = quicly_send (conn, packets, &num_packets)))
672 for (i = 0; i != num_packets; ++i)
674 if ((err = quic_send_datagram (udp_session, packets[i])))
677 pa->free_packet (pa, packets[i]);
680 while (num_packets > 0 && num_packets == max_packets);
683 quic_set_udp_tx_evt (udp_session);
685 QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
686 svm_fifo_max_dequeue (udp_session->rx_fifo));
687 quic_update_timer (ctx);
691 if (err && err != QUICLY_ERROR_PACKET_IGNORED
692 && err != QUICLY_ERROR_FREE_CONNECTION)
693 clib_warning ("Quic error '%U'.", quic_format_err, err);
694 quic_connection_closed (ctx);
698 /* Quicly callbacks */
701 quic_on_stream_destroy (quicly_stream_t * stream, int err)
703 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
704 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
705 stream_data->thread_index);
706 session_t *stream_session = session_get (sctx->c_s_index,
707 sctx->c_thread_index);
708 QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
709 session_handle (stream_session), quic_format_err, err);
711 stream_session->session_state = SESSION_STATE_CLOSED;
712 session_transport_delete_notify (&sctx->connection);
714 quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
715 quic_ctx_free (sctx);
716 clib_mem_free (stream->data);
720 quic_on_stop_sending (quicly_stream_t * stream, int err)
723 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
724 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
725 stream_data->thread_index);
726 session_t *stream_session = session_get (sctx->c_s_index,
727 sctx->c_thread_index);
728 clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
729 session_handle (stream_session), quic_format_err, err);
731 /* TODO : handle STOP_SENDING */
736 quic_on_receive_reset (quicly_stream_t * stream, int err)
738 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
739 quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
740 stream_data->thread_index);
742 session_t *stream_session = session_get (sctx->c_s_index,
743 sctx->c_thread_index);
744 clib_warning ("RESET_STREAM: session 0x%lx (%U)",
745 session_handle (stream_session), quic_format_err, err);
747 session_transport_closing_notify (&sctx->connection);
752 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
755 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
756 u32 max_enq, rlen, rv;
758 session_t *stream_session;
759 app_worker_t *app_wrk;
761 quic_stream_data_t *stream_data;
763 stream_data = (quic_stream_data_t *) stream->data;
764 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
765 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
766 f = stream_session->rx_fifo;
768 max_enq = svm_fifo_max_enqueue_prod (f);
769 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
770 /* Handle duplicate packet/chunk from quicly */
771 if (off < stream_data->app_rx_data_len)
773 QUIC_DBG (3, "Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
774 "DUPLICATE PACKET (max_enq %u, len %u, "
775 "app_rx_data_len %u, off %u, ToBeNQ %u)",
776 stream_session->session_index,
777 stream_session->app_wrk_index,
778 stream_session->thread_index, f,
779 max_enq, len, stream_data->app_rx_data_len, off,
780 off - stream_data->app_rx_data_len + len);
783 if (PREDICT_FALSE ((off - stream_data->app_rx_data_len + len) > max_enq))
785 QUIC_ERR ("Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
786 "RX FIFO IS FULL (max_enq %u, len %u, "
787 "app_rx_data_len %u, off %u, ToBeNQ %u)",
788 stream_session->session_index,
789 stream_session->app_wrk_index,
790 stream_session->thread_index, f,
791 max_enq, len, stream_data->app_rx_data_len, off,
792 off - stream_data->app_rx_data_len + len);
795 if (off == stream_data->app_rx_data_len)
797 /* Streams live on the same thread so (f, stream_data) should stay consistent */
798 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
799 QUIC_DBG (3, "Session [idx %u, app_wrk %u, ti %u, rx-fifo 0x%llx]: "
800 "Enqueuing %u (rlen %u) at off %u in %u space, ",
801 stream_session->session_index,
802 stream_session->app_wrk_index,
803 stream_session->thread_index, f, len, rlen, off, max_enq);
804 stream_data->app_rx_data_len += rlen;
805 QUIC_ASSERT (rlen >= len);
806 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
807 if (PREDICT_TRUE (app_wrk != 0))
809 rv = app_worker_lock_and_send_event (app_wrk, stream_session,
812 QUIC_ERR ("Failed to ping app for RX");
814 quic_ack_rx_data (stream_session);
818 rlen = svm_fifo_enqueue_with_offset (f,
819 off - stream_data->app_rx_data_len,
821 QUIC_ASSERT (rlen == 0);
827 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
829 quic_stream_data_t *stream_data;
830 session_t *stream_session;
834 stream_data = (quic_stream_data_t *) stream->data;
835 stream_session = get_stream_session_from_stream (stream);
836 f = stream_session->tx_fifo;
838 QUIC_ASSERT (stream_data->app_tx_data_len >= delta);
839 stream_data->app_tx_data_len -= delta;
840 rv = svm_fifo_dequeue_drop (f, delta);
841 QUIC_ASSERT (rv == delta);
843 rv = quicly_stream_sync_sendbuf (stream, 0);
848 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
849 size_t * len, int *wrote_all)
851 u32 deq_max, first_deq, max_rd_chunk, rem_offset;
852 quic_stream_data_t *stream_data;
853 session_t *stream_session;
856 stream_data = (quic_stream_data_t *) stream->data;
857 stream_session = get_stream_session_from_stream (stream);
858 f = stream_session->tx_fifo;
860 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
862 deq_max = svm_fifo_max_dequeue_cons (f);
863 QUIC_ASSERT (off <= deq_max);
864 if (off + *len < deq_max)
871 *len = deq_max - off;
873 QUIC_ASSERT (*len > 0);
875 if (off + *len > stream_data->app_tx_data_len)
876 stream_data->app_tx_data_len = off + *len;
878 /* TODO, use something like : return svm_fifo_peek (f, off, *len, dst); */
879 max_rd_chunk = svm_fifo_max_read_chunk (f);
882 if (off < max_rd_chunk)
884 first_deq = clib_min (*len, max_rd_chunk - off);
885 clib_memcpy_fast (dst, svm_fifo_head (f) + off, first_deq);
888 if (max_rd_chunk < off + *len)
890 rem_offset = max_rd_chunk < off ? off - max_rd_chunk : 0;
891 clib_memcpy_fast (dst + first_deq, f->head_chunk->data + rem_offset,
898 static const quicly_stream_callbacks_t quic_stream_callbacks = {
899 .on_destroy = quic_on_stream_destroy,
900 .on_send_shift = quic_fifo_egress_shift,
901 .on_send_emit = quic_fifo_egress_emit,
902 .on_send_stop = quic_on_stop_sending,
903 .on_receive = quic_on_receive,
904 .on_receive_reset = quic_on_receive_reset
908 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
910 /* Return code for this function ends either
911 * - in quicly_receive : if not QUICLY_ERROR_PACKET_IGNORED, will close connection
912 * - in quicly_open_stream, returned directly
915 session_t *stream_session, *quic_session;
916 quic_stream_data_t *stream_data;
917 app_worker_t *app_wrk;
918 quic_ctx_t *qctx, *sctx;
922 QUIC_DBG (2, "on_stream_open called");
923 stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
924 stream->callbacks = &quic_stream_callbacks;
925 /* Notify accept on parent qsession, but only if this is not a locally
926 * initiated stream */
927 if (quicly_stream_is_self_initiated (stream))
930 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
931 qctx = quic_get_conn_ctx (stream->conn);
933 /* Might need to signal that the connection is ready if the first thing the
934 * server does is open a stream */
935 quic_check_quic_session_connected (qctx);
936 /* ctx might be invalidated */
937 qctx = quic_get_conn_ctx (stream->conn);
939 stream_session = session_alloc (qctx->c_thread_index);
940 QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
941 session_handle (stream_session), sctx_id);
942 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
943 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
944 sctx->parent_app_id = qctx->parent_app_id;
945 sctx->quic_connection_ctx_id = qctx->c_c_index;
946 sctx->c_c_index = sctx_id;
947 sctx->c_s_index = stream_session->session_index;
948 sctx->stream = stream;
949 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
950 sctx->flags |= QUIC_F_IS_STREAM;
951 if (quicly_stream_is_unidirectional (stream->stream_id))
952 stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
954 stream_data = (quic_stream_data_t *) stream->data;
955 stream_data->ctx_id = sctx_id;
956 stream_data->thread_index = sctx->c_thread_index;
957 stream_data->app_rx_data_len = 0;
958 stream_data->app_tx_data_len = 0;
960 sctx->c_s_index = stream_session->session_index;
961 stream_session->session_state = SESSION_STATE_CREATED;
962 stream_session->app_wrk_index = sctx->parent_app_wrk_id;
963 stream_session->connection_index = sctx->c_c_index;
964 stream_session->session_type =
965 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
966 quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
967 stream_session->listener_handle = listen_session_get_handle (quic_session);
969 app_wrk = app_worker_get (stream_session->app_wrk_index);
970 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
972 QUIC_ERR ("failed to allocate fifos");
973 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
974 return 0; /* Frame is still valid */
976 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
977 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
978 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
980 if ((rv = app_worker_accept_notify (app_wrk, stream_session)))
982 QUIC_ERR ("failed to notify accept worker app");
983 quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
984 return 0; /* Frame is still valid */
991 quic_on_closed_by_peer (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
992 int code, uint64_t frame_type,
993 const char *reason, size_t reason_len)
995 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
997 session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
998 clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
999 session_handle (quic_session), quic_format_err, code,
1000 reason_len, reason);
1002 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
1003 session_transport_closing_notify (&ctx->connection);
1006 /* Timer handling */
1009 quic_get_thread_time (u8 thread_index)
1011 return quic_main.wrk_ctx[thread_index].time_now;
1015 quic_get_time (quicly_now_t * self)
1017 u8 thread_index = vlib_get_thread_index ();
1018 return quic_get_thread_time (thread_index);
1022 quic_set_time_now (u32 thread_index)
1024 vlib_main_t *vlib_main = vlib_get_main ();
1025 f64 time = vlib_time_now (vlib_main);
1026 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
1027 return quic_main.wrk_ctx[thread_index].time_now;
1030 /* Transport proto callback */
1032 quic_update_time (f64 now, u8 thread_index)
1034 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1036 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1037 quic_set_time_now (thread_index);
1038 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
1042 quic_timer_expired (u32 conn_index)
1045 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
1046 quic_get_time (NULL));
1047 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
1048 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1049 quic_send_packets (ctx);
1053 quic_update_timer (quic_ctx_t * ctx)
1055 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1056 int64_t next_timeout, next_interval;
1057 session_t *quic_session;
1060 /* This timeout is in ms which is the unit of our timer */
1061 next_timeout = quicly_get_first_timeout (ctx->conn);
1062 next_interval = next_timeout - quic_get_time (NULL);
1064 if (next_timeout == 0 || next_interval <= 0)
1066 if (ctx->c_s_index == QUIC_SESSION_INVALID)
1072 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
1073 if (svm_fifo_set_event (quic_session->tx_fifo))
1075 rv = session_send_io_evt_to_thread_custom (quic_session,
1076 quic_session->thread_index,
1077 SESSION_IO_EVT_BUILTIN_TX);
1078 if (PREDICT_FALSE (rv))
1079 QUIC_ERR ("Failed to enqueue builtin_tx %d", rv);
1085 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
1087 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
1088 next_interval, ctx->c_c_index);
1090 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
1092 if (next_timeout == INT64_MAX)
1094 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
1097 ctx->timer_handle = tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index,
1102 if (next_timeout == INT64_MAX)
1104 quic_stop_ctx_timer (ctx);
1107 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
1114 quic_expired_timers_dispatch (u32 * expired_timers)
1118 for (i = 0; i < vec_len (expired_timers); i++)
1120 quic_timer_expired (expired_timers[i]);
1124 /* Transport proto functions */
1127 quic_connect_stream (session_t * quic_session, session_endpoint_cfg_t * sep)
1129 uint64_t quic_session_handle;
1130 session_t *stream_session;
1131 quic_stream_data_t *stream_data;
1132 quicly_stream_t *stream;
1133 quicly_conn_t *conn;
1134 app_worker_t *app_wrk;
1135 quic_ctx_t *qctx, *sctx;
1139 /* Find base session to which the user want to attach a stream */
1140 quic_session_handle = session_handle (quic_session);
1141 QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
1143 if (session_type_transport_proto (quic_session->session_type) !=
1144 TRANSPORT_PROTO_QUIC)
1146 QUIC_ERR ("received incompatible session");
1150 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
1153 QUIC_ERR ("Invalid app worker :(");
1157 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
1158 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
1159 qctx = quic_ctx_get (quic_session->connection_index,
1160 quic_session->thread_index);
1161 if (quic_ctx_is_stream (qctx))
1163 QUIC_ERR ("session is a stream");
1164 quic_ctx_free (sctx);
1168 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
1169 sctx->parent_app_id = qctx->parent_app_id;
1170 sctx->quic_connection_ctx_id = qctx->c_c_index;
1171 sctx->c_c_index = sctx_index;
1172 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1173 sctx->flags |= QUIC_F_IS_STREAM;
1177 if (!conn || !quicly_connection_is_ready (conn))
1181 quicly_open_stream (conn, &stream,
1182 sep->flags & SESSION_F_UNIDIRECTIONAL)))
1184 QUIC_DBG (2, "Stream open failed with %d", rv);
1187 quic_increment_counter (QUIC_ERROR_OPENED_STREAM, 1);
1189 sctx->stream = stream;
1191 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1193 stream_session = session_alloc (qctx->c_thread_index);
1194 QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
1195 session_handle (stream_session), sctx_index);
1196 stream_session->app_wrk_index = app_wrk->wrk_index;
1197 stream_session->connection_index = sctx_index;
1198 stream_session->listener_handle = quic_session_handle;
1199 stream_session->session_type =
1200 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1201 if (sep->flags & SESSION_F_UNIDIRECTIONAL)
1202 stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
1204 sctx->c_s_index = stream_session->session_index;
1205 stream_data = (quic_stream_data_t *) stream->data;
1206 stream_data->ctx_id = sctx->c_c_index;
1207 stream_data->thread_index = sctx->c_thread_index;
1208 stream_data->app_rx_data_len = 0;
1209 stream_data->app_tx_data_len = 0;
1210 stream_session->session_state = SESSION_STATE_READY;
1212 /* For now we only reset streams. Cleanup will be triggered by timers */
1213 if (app_worker_init_connected (app_wrk, stream_session))
1215 QUIC_ERR ("failed to app_worker_init_connected");
1216 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1217 return app_worker_connect_notify (app_wrk, NULL, sep->opaque);
1220 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1221 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1222 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1224 if (app_worker_connect_notify (app_wrk, stream_session, sep->opaque))
1226 QUIC_ERR ("failed to notify app");
1227 quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
1228 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1236 quic_connect_connection (session_endpoint_cfg_t * sep)
1238 vnet_connect_args_t _cargs, *cargs = &_cargs;
1239 quic_main_t *qm = &quic_main;
1241 app_worker_t *app_wrk;
1244 u32 thread_index = vlib_get_thread_index ();
1247 clib_memset (cargs, 0, sizeof (*cargs));
1248 ctx_index = quic_ctx_alloc (thread_index);
1249 ctx = quic_ctx_get (ctx_index, thread_index);
1250 ctx->parent_app_wrk_id = sep->app_wrk_index;
1251 ctx->c_s_index = QUIC_SESSION_INVALID;
1252 ctx->c_c_index = ctx_index;
1253 ctx->udp_is_ip4 = sep->is_ip4;
1254 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1255 ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1256 ctx->client_opaque = sep->opaque;
1257 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1259 ctx->srv_hostname = format (0, "%v", sep->hostname);
1261 /* needed by quic for crypto + determining client / server */
1262 ctx->srv_hostname = format (0, "%U", format_ip46_address,
1263 &sep->ip, sep->is_ip4);
1264 vec_terminate_c_string (ctx->srv_hostname);
1266 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1267 cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1268 cargs->app_index = qm->app_index;
1269 cargs->api_context = ctx_index;
1271 app_wrk = app_worker_get (sep->app_wrk_index);
1272 app = application_get (app_wrk->app_index);
1273 ctx->parent_app_id = app_wrk->app_index;
1274 cargs->sep_ext.ns_index = app->ns_index;
1276 ctx->crypto_engine = sep->crypto_engine;
1277 ctx->ckpair_index = sep->ckpair_index;
1278 if ((error = quic_acquire_crypto_context (ctx)))
1281 if ((error = vnet_connect (cargs)))
1288 quic_connect (transport_endpoint_cfg_t * tep)
1290 QUIC_DBG (2, "Called quic_connect");
1291 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1292 session_t *quic_session;
1293 sep = (session_endpoint_cfg_t *) tep;
1295 quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1297 return quic_connect_stream (quic_session, sep);
1299 return quic_connect_connection (sep);
1303 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1305 quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1309 session_t *stream_session = session_get (ctx->c_s_index,
1310 ctx->c_thread_index);
1311 clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1313 if (quic_ctx_is_stream (ctx))
1315 quicly_stream_t *stream = ctx->stream;
1316 if (!quicly_stream_has_send_side (quicly_is_client (stream->conn),
1319 quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1320 quic_send_packets (ctx);
1324 switch (ctx->conn_state)
1326 case QUIC_CONN_STATE_OPENED:
1327 case QUIC_CONN_STATE_HANDSHAKE:
1328 case QUIC_CONN_STATE_READY:
1329 ctx->conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1330 quicly_conn_t *conn = ctx->conn;
1331 /* Start connection closing. Keep sending packets until quicly_send
1332 returns QUICLY_ERROR_FREE_CONNECTION */
1334 quic_increment_counter (QUIC_ERROR_CLOSED_CONNECTION, 1);
1335 quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1336 /* This also causes all streams to be closed (and the cb called) */
1337 quic_send_packets (ctx);
1339 case QUIC_CONN_STATE_PASSIVE_CLOSING:
1340 ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1341 /* send_packets will eventually return an error, we delete the conn at
1344 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1345 quic_connection_delete (ctx);
1347 case QUIC_CONN_STATE_ACTIVE_CLOSING:
1350 QUIC_ERR ("Trying to close conn in state %d", ctx->conn_state);
1356 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1358 vnet_listen_args_t _bargs, *args = &_bargs;
1359 quic_main_t *qm = &quic_main;
1360 session_handle_t udp_handle;
1361 session_endpoint_cfg_t *sep;
1362 session_t *udp_listen_session;
1363 app_worker_t *app_wrk;
1367 app_listener_t *app_listener;
1370 sep = (session_endpoint_cfg_t *) tep;
1371 app_wrk = app_worker_get (sep->app_wrk_index);
1372 /* We need to call this because we call app_worker_init_connected in
1373 * quic_accept_stream, which assumes the connect segment manager exists */
1374 app_worker_alloc_connects_segment_manager (app_wrk);
1375 app = application_get (app_wrk->app_index);
1376 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1378 clib_memset (args, 0, sizeof (*args));
1379 args->app_index = qm->app_index;
1380 args->sep_ext = *sep;
1381 args->sep_ext.ns_index = app->ns_index;
1382 args->sep_ext.transport_proto = TRANSPORT_PROTO_UDPC;
1383 if ((rv = vnet_listen (args)))
1386 lctx_index = quic_ctx_alloc (0);
1387 udp_handle = args->handle;
1388 app_listener = app_listener_get_w_handle (udp_handle);
1389 udp_listen_session = app_listener_get_session (app_listener);
1390 udp_listen_session->opaque = lctx_index;
1392 lctx = quic_ctx_get (lctx_index, 0);
1393 lctx->flags |= QUIC_F_IS_LISTENER;
1395 clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1396 clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1397 lctx->c_rmt_port = args->sep.peer.port;
1398 lctx->c_lcl_port = args->sep.port;
1399 lctx->c_is_ip4 = args->sep.is_ip4;
1400 lctx->c_fib_index = args->sep.fib_index;
1401 lctx->c_proto = TRANSPORT_PROTO_QUIC;
1402 lctx->parent_app_wrk_id = sep->app_wrk_index;
1403 lctx->parent_app_id = app_wrk->app_index;
1404 lctx->udp_session_handle = udp_handle;
1405 lctx->c_s_index = quic_listen_session_index;
1406 lctx->crypto_engine = sep->crypto_engine;
1407 lctx->ckpair_index = sep->ckpair_index;
1408 if (quic_acquire_crypto_context (lctx))
1411 QUIC_DBG (2, "Listening UDP session 0x%lx",
1412 session_handle (udp_listen_session));
1413 QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1418 quic_stop_listen (u32 lctx_index)
1420 QUIC_DBG (2, "Called quic_stop_listen");
1422 lctx = quic_ctx_get (lctx_index, 0);
1423 QUIC_ASSERT (quic_ctx_is_listener (lctx));
1424 vnet_unlisten_args_t a = {
1425 .handle = lctx->udp_session_handle,
1426 .app_index = quic_main.app_index,
1427 .wrk_map_index = 0 /* default wrk */
1429 if (vnet_unlisten (&a))
1430 clib_warning ("unlisten errored");
1432 quic_release_crypto_context (lctx->crypto_context_index,
1433 0 /* thread_index */ );
1434 quic_ctx_free (lctx);
1438 static transport_connection_t *
1439 quic_connection_get (u32 ctx_index, u32 thread_index)
1442 ctx = quic_ctx_get (ctx_index, thread_index);
1443 return &ctx->connection;
1446 static transport_connection_t *
1447 quic_listener_get (u32 listener_index)
1449 QUIC_DBG (2, "Called quic_listener_get");
1451 ctx = quic_ctx_get (listener_index, 0);
1452 return &ctx->connection;
1456 format_quic_ctx (u8 * s, va_list * args)
1458 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1459 u32 verbose = va_arg (*args, u32);
1464 str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1466 if (quic_ctx_is_listener (ctx))
1467 str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1468 else if (quic_ctx_is_stream (ctx))
1469 str = format (str, "Stream %ld conn %d",
1470 ctx->stream->stream_id, ctx->quic_connection_ctx_id);
1471 else /* connection */
1472 str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1473 ctx->udp_session_handle);
1475 str = format (str, " app %d wrk %d", ctx->parent_app_id,
1476 ctx->parent_app_wrk_id);
1479 s = format (s, "%-50s%-15d", str, ctx->conn_state);
1481 s = format (s, "%s\n", str);
1487 format_quic_connection (u8 * s, va_list * args)
1489 u32 qc_index = va_arg (*args, u32);
1490 u32 thread_index = va_arg (*args, u32);
1491 u32 verbose = va_arg (*args, u32);
1492 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1493 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1498 format_quic_half_open (u8 * s, va_list * args)
1500 u32 qc_index = va_arg (*args, u32);
1501 u32 thread_index = va_arg (*args, u32);
1502 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1503 s = format (s, "[#%d][Q] half-open app %u", thread_index,
1504 ctx->parent_app_id);
1510 format_quic_listener (u8 * s, va_list * args)
1512 u32 tci = va_arg (*args, u32);
1513 u32 thread_index = va_arg (*args, u32);
1514 u32 verbose = va_arg (*args, u32);
1515 quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1516 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1520 /* Session layer callbacks */
1523 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1524 ip46_address_t * addr, u16 port, u8 is_ip4)
1528 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1529 sa4->sin_family = AF_INET;
1530 sa4->sin_port = port;
1531 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1532 *salen = sizeof (struct sockaddr_in);
1536 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1537 sa6->sin6_family = AF_INET6;
1538 sa6->sin6_port = port;
1539 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1540 *salen = sizeof (struct sockaddr_in6);
1545 quic_on_quic_session_connected (quic_ctx_t * ctx)
1547 session_t *quic_session;
1548 app_worker_t *app_wrk;
1549 u32 ctx_id = ctx->c_c_index;
1550 u32 thread_index = ctx->c_thread_index;
1553 quic_session = session_alloc (thread_index);
1555 QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1556 ctx->c_s_index = quic_session->session_index;
1557 quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1558 quic_session->connection_index = ctx->c_c_index;
1559 quic_session->listener_handle = SESSION_INVALID_HANDLE;
1560 quic_session->session_type =
1561 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1563 /* If quic session connected fails, immediatly close connection */
1564 app_wrk = app_worker_get (ctx->parent_app_wrk_id);
1565 if (app_worker_init_connected (app_wrk, quic_session))
1567 QUIC_ERR ("failed to app_worker_init_connected");
1568 quic_proto_on_close (ctx_id, thread_index);
1569 app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1573 quic_session->session_state = SESSION_STATE_CONNECTING;
1574 if ((rv = app_worker_connect_notify (app_wrk, quic_session,
1575 ctx->client_opaque)))
1577 QUIC_ERR ("failed to notify app %d", rv);
1578 quic_proto_on_close (ctx_id, thread_index);
1582 /* If the app opens a stream in its callback it may invalidate ctx */
1583 ctx = quic_ctx_get (ctx_id, thread_index);
1585 * app_worker_connect_notify() might have reallocated pool, reload
1586 * quic_session pointer
1588 quic_session = session_get (ctx->c_s_index, thread_index);
1589 quic_session->session_state = SESSION_STATE_LISTENING;
1593 quic_check_quic_session_connected (quic_ctx_t * ctx)
1595 /* Called when we need to trigger quic session connected
1596 * we may call this function on the server side / at
1599 /* Conn may be set to null if the connection is terminated */
1600 if (!ctx->conn || ctx->conn_state != QUIC_CONN_STATE_HANDSHAKE)
1602 if (!quicly_connection_is_ready (ctx->conn))
1604 ctx->conn_state = QUIC_CONN_STATE_READY;
1605 if (!quicly_is_client (ctx->conn))
1607 quic_on_quic_session_connected (ctx);
1611 quic_update_conn_ctx (quicly_conn_t * conn, quicly_context_t * quicly_context)
1613 /* we need to update the quicly_conn on migrate
1614 * as it contains a pointer to the crypto context */
1615 ptls_context_t **tls;
1616 quicly_context_t **_quicly_context;
1617 _quicly_context = (quicly_context_t **) conn;
1618 *_quicly_context = quicly_context;
1619 tls = (ptls_context_t **) quicly_get_tls (conn);
1620 *tls = quicly_context->tls;
1624 quic_receive_connection (void *arg)
1626 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1627 quic_ctx_t *temp_ctx, *new_ctx;
1628 clib_bihash_kv_16_8_t kv;
1629 quicly_conn_t *conn;
1630 quicly_context_t *quicly_context;
1631 session_t *udp_session;
1634 new_ctx_id = quic_ctx_alloc (thread_index);
1635 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1637 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1640 clib_memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1641 clib_mem_free (temp_ctx);
1643 new_ctx->c_thread_index = thread_index;
1644 new_ctx->c_c_index = new_ctx_id;
1645 quic_acquire_crypto_context (new_ctx);
1647 conn = new_ctx->conn;
1648 quicly_context = quic_get_quicly_ctx_from_ctx (new_ctx);
1649 quic_update_conn_ctx (conn, quicly_context);
1651 quic_store_conn_ctx (conn, new_ctx);
1652 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1653 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1654 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1655 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1656 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1657 quic_update_timer (new_ctx);
1659 /* Trigger write on this connection if necessary */
1660 udp_session = session_get_from_handle (new_ctx->udp_session_handle);
1661 udp_session->opaque = new_ctx_id;
1662 udp_session->flags &= ~SESSION_F_IS_MIGRATING;
1663 if (svm_fifo_max_dequeue (udp_session->tx_fifo))
1664 quic_set_udp_tx_evt (udp_session);
1668 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1670 quic_ctx_t *ctx, *temp_ctx;
1671 u32 thread_index = vlib_get_thread_index ();
1673 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1675 temp_ctx = clib_mem_alloc (sizeof (quic_ctx_t));
1676 QUIC_ASSERT (temp_ctx != NULL);
1677 ctx = quic_ctx_get (ctx_index, thread_index);
1679 clib_memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1681 quic_stop_ctx_timer (ctx);
1682 quic_release_crypto_context (ctx->crypto_context_index, thread_index);
1683 quic_ctx_free (ctx);
1685 /* Send connection to destination thread */
1686 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1691 quic_udp_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1692 session_t * udp_session, u8 is_fail)
1694 QUIC_DBG (2, "QSession is now connected (id %u)",
1695 udp_session->session_index);
1696 /* This should always be called before quic_connect returns since UDP always
1697 * connects instantly. */
1698 clib_bihash_kv_16_8_t kv;
1699 struct sockaddr_in6 sa6;
1700 struct sockaddr *sa = (struct sockaddr *) &sa6;
1702 transport_connection_t *tc;
1703 app_worker_t *app_wrk;
1704 quicly_conn_t *conn;
1706 u32 thread_index = vlib_get_thread_index ();
1708 quicly_context_t *quicly_ctx;
1711 ctx = quic_ctx_get (ctx_index, thread_index);
1715 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1718 api_context = ctx->c_s_index;
1719 app_worker_connect_notify (app_wrk, 0, api_context);
1724 ctx->c_thread_index = thread_index;
1725 ctx->c_c_index = ctx_index;
1727 QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1728 is_fail, thread_index, (ctx) ? ctx_index : ~0);
1730 ctx->udp_session_handle = session_handle (udp_session);
1731 udp_session->opaque = ctx_index;
1733 /* Init QUIC lib connection
1734 * Generate required sockaddr & salen */
1735 tc = session_get_transport (udp_session);
1736 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1738 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1739 ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
1740 sa, NULL, &quic_main.wrk_ctx[thread_index].next_cid,
1741 ptls_iovec_init (NULL, 0), &quic_main.hs_properties,
1743 ++quic_main.wrk_ctx[thread_index].next_cid.master_id;
1744 /* Save context handle in quicly connection */
1745 quic_store_conn_ctx (ctx->conn, ctx);
1748 /* Register connection in connections map */
1750 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1751 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1752 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1753 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1755 /* UDP stack quirk? preemptively transfer connection if that happens */
1756 if (udp_session->thread_index != thread_index)
1757 quic_transfer_connection (ctx_index, udp_session->thread_index);
1759 quic_send_packets (ctx);
1765 quic_udp_session_disconnect_callback (session_t * s)
1767 clib_warning ("UDP session disconnected???");
1771 quic_udp_session_cleanup_callback (session_t * udp_session,
1772 session_cleanup_ntf_t ntf)
1776 if (ntf != SESSION_CLEANUP_SESSION)
1779 ctx = quic_ctx_get (udp_session->opaque, udp_session->thread_index);
1780 quic_stop_ctx_timer (ctx);
1781 quic_release_crypto_context (ctx->crypto_context_index,
1782 ctx->c_thread_index);
1783 quic_ctx_free (ctx);
1787 quic_udp_session_reset_callback (session_t * s)
1789 clib_warning ("UDP session reset???");
1793 quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
1795 u32 new_thread = session_thread_from_handle (new_sh);
1798 QUIC_DBG (2, "Session %x migrated to %lx", s->session_index, new_sh);
1799 QUIC_ASSERT (vlib_get_thread_index () == s->thread_index);
1800 ctx = quic_ctx_get (s->opaque, s->thread_index);
1801 QUIC_ASSERT (ctx->udp_session_handle == session_handle (s));
1803 ctx->udp_session_handle = new_sh;
1805 s->opaque = 0xfeedface;
1807 quic_transfer_connection (ctx->c_c_index, new_thread);
1811 quic_udp_session_accepted_callback (session_t * udp_session)
1813 /* New UDP connection, try to accept it */
1815 quic_ctx_t *ctx, *lctx;
1816 session_t *udp_listen_session;
1817 u32 thread_index = vlib_get_thread_index ();
1819 udp_listen_session =
1820 listen_session_get_from_handle (udp_session->listener_handle);
1822 ctx_index = quic_ctx_alloc (thread_index);
1823 ctx = quic_ctx_get (ctx_index, thread_index);
1824 ctx->c_thread_index = udp_session->thread_index;
1825 ctx->c_c_index = ctx_index;
1826 ctx->c_s_index = QUIC_SESSION_INVALID;
1827 ctx->udp_session_handle = session_handle (udp_session);
1828 QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
1829 ctx->listener_ctx_id = udp_listen_session->opaque;
1830 lctx = quic_ctx_get (udp_listen_session->opaque,
1831 udp_listen_session->thread_index);
1832 ctx->udp_is_ip4 = lctx->c_is_ip4;
1833 ctx->parent_app_id = lctx->parent_app_id;
1834 ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
1835 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1836 ctx->conn_state = QUIC_CONN_STATE_OPENED;
1837 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1839 ctx->crypto_engine = lctx->crypto_engine;
1840 ctx->ckpair_index = lctx->ckpair_index;
1841 quic_acquire_crypto_context (ctx);
1842 udp_session->opaque = ctx_index;
1844 /* TODO timeout to delete these if they never connect */
1849 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1851 /* No-op for builtin */
1856 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1858 /* No-op for builtin */
1863 quic_custom_app_rx_callback (transport_connection_t * tc)
1866 session_t *stream_session = session_get (tc->s_index, tc->thread_index);
1867 QUIC_DBG (3, "Received app READ notification");
1868 quic_ack_rx_data (stream_session);
1869 svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
1871 /* Need to send packets (acks may never be sent otherwise) */
1872 ctx = quic_ctx_get (stream_session->connection_index,
1873 stream_session->thread_index);
1874 quic_send_packets (ctx);
1879 quic_custom_tx_callback (void *s, u32 max_burst_size)
1881 session_t *stream_session = (session_t *) s;
1882 quic_stream_data_t *stream_data;
1883 quicly_stream_t *stream;
1889 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1891 ctx = quic_ctx_get (stream_session->connection_index,
1892 stream_session->thread_index);
1893 if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
1895 goto tx_end; /* Most probably a reschedule */
1898 QUIC_DBG (3, "Stream TX event");
1899 quic_ack_rx_data (stream_session);
1900 stream = ctx->stream;
1901 if (!quicly_sendstate_is_open (&stream->sendstate))
1903 QUIC_ERR ("Warning: tried to send on closed stream");
1907 stream_data = (quic_stream_data_t *) stream->data;
1908 max_deq = svm_fifo_max_dequeue (stream_session->tx_fifo);
1909 QUIC_ASSERT (max_deq >= stream_data->app_tx_data_len);
1910 if (max_deq == stream_data->app_tx_data_len)
1912 QUIC_DBG (3, "TX but no data %d / %d", max_deq,
1913 stream_data->app_tx_data_len);
1916 stream_data->app_tx_data_len = max_deq;
1917 rv = quicly_stream_sync_sendbuf (stream, 1);
1921 quic_send_packets (ctx);
1926 * Returns 0 if a matching connection is found and is on the right thread.
1927 * Otherwise returns -1.
1928 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
1932 quic_find_packet_ctx (quic_rx_packet_ctx_t * pctx, u32 caller_thread_index)
1934 clib_bihash_kv_16_8_t kv;
1935 clib_bihash_16_8_t *h;
1937 u32 index, thread_id;
1939 h = &quic_main.connection_hash;
1940 quic_make_connection_key (&kv, &pctx->packet.cid.dest.plaintext);
1941 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
1943 if (clib_bihash_search_16_8 (h, &kv, &kv))
1945 QUIC_DBG (3, "connection not found");
1946 return QUIC_PACKET_TYPE_NONE;
1949 index = kv.value & UINT32_MAX;
1950 thread_id = kv.value >> 32;
1951 /* Check if this connection belongs to this thread, otherwise
1952 * ask for it to be moved */
1953 if (thread_id != caller_thread_index)
1955 QUIC_DBG (2, "Connection is on wrong thread");
1956 /* Cannot make full check with quicly_is_destination... */
1957 pctx->ctx_index = index;
1958 pctx->thread_index = thread_id;
1959 return QUIC_PACKET_TYPE_MIGRATE;
1961 ctx = quic_ctx_get (index, vlib_get_thread_index ());
1964 QUIC_ERR ("ctx has no conn");
1965 return QUIC_PACKET_TYPE_NONE;
1967 if (!quicly_is_destination (ctx->conn, NULL, &pctx->sa, &pctx->packet))
1968 return QUIC_PACKET_TYPE_NONE;
1970 QUIC_DBG (3, "Connection found");
1971 pctx->ctx_index = index;
1972 pctx->thread_index = thread_id;
1973 return QUIC_PACKET_TYPE_RECEIVE;
1977 quic_accept_connection (quic_rx_packet_ctx_t * pctx)
1979 quicly_context_t *quicly_ctx;
1980 session_t *quic_session;
1981 clib_bihash_kv_16_8_t kv;
1982 app_worker_t *app_wrk;
1983 quicly_conn_t *conn;
1988 /* new connection, accept and create context if packet is valid
1989 * TODO: check if socket is actually listening? */
1990 ctx = quic_ctx_get (pctx->ctx_index, pctx->thread_index);
1991 if (ctx->c_s_index != QUIC_SESSION_INVALID)
1993 QUIC_DBG (2, "already accepted ctx 0x%x", ctx_index);
1997 quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1998 if ((rv = quicly_accept (&conn, quicly_ctx, NULL, &pctx->sa,
1999 &pctx->packet, NULL,
2000 &quic_main.wrk_ctx[pctx->thread_index].next_cid,
2003 /* Invalid packet, pass */
2004 assert (conn == NULL);
2005 QUIC_ERR ("Accept failed with %U", quic_format_err, rv);
2006 /* TODO: cleanup created quic ctx and UDP session */
2009 assert (conn != NULL);
2011 ++quic_main.wrk_ctx[pctx->thread_index].next_cid.master_id;
2012 /* Save ctx handle in quicly connection */
2013 quic_store_conn_ctx (conn, ctx);
2016 quic_session = session_alloc (ctx->c_thread_index);
2017 QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
2018 session_handle (quic_session), ctx->c_c_index);
2019 quic_session->session_state = SESSION_STATE_LISTENING;
2020 ctx->c_s_index = quic_session->session_index;
2022 lctx = quic_ctx_get (ctx->listener_ctx_id, 0);
2024 quic_session->app_wrk_index = lctx->parent_app_wrk_id;
2025 quic_session->connection_index = ctx->c_c_index;
2026 quic_session->session_type =
2027 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
2028 quic_session->listener_handle = lctx->c_s_index;
2030 /* Register connection in connections map */
2031 quic_make_connection_key (&kv, quicly_get_master_id (conn));
2032 kv.value = ((u64) pctx->thread_index) << 32 | (u64) pctx->ctx_index;
2033 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
2034 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
2036 /* If notify fails, reset connection immediatly */
2037 if ((rv = app_worker_init_accepted (quic_session)))
2039 QUIC_ERR ("failed to allocate fifos");
2040 quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2044 app_wrk = app_worker_get (quic_session->app_wrk_index);
2045 if ((rv = app_worker_accept_notify (app_wrk, quic_session)))
2047 QUIC_ERR ("failed to notify accept worker app");
2048 quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2052 ctx->conn_state = QUIC_CONN_STATE_READY;
2056 quic_reset_connection (u64 udp_session_handle, quic_rx_packet_ctx_t * pctx)
2058 /* short header packet; potentially a dead connection. No need to check the
2059 * length of the incoming packet, because loop is prevented by authenticating
2060 * the CID (by checking node_id and thread_id). If the peer is also sending a
2061 * reset, then the next CID is highly likely to contain a non-authenticating
2063 QUIC_DBG (2, "Sending stateless reset");
2065 quicly_datagram_t *dgram;
2066 session_t *udp_session;
2067 quicly_context_t *quicly_ctx;
2068 if (pctx->packet.cid.dest.plaintext.node_id != 0
2069 || pctx->packet.cid.dest.plaintext.thread_id != 0)
2071 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2072 dgram = quicly_send_stateless_reset (quicly_ctx, &pctx->sa, NULL,
2073 &pctx->packet.cid.dest.plaintext);
2076 udp_session = session_get_from_handle (udp_session_handle);
2077 rv = quic_send_datagram (udp_session, dgram);
2078 quic_set_udp_tx_evt (udp_session);
2083 quic_process_one_rx_packet (u64 udp_session_handle, svm_fifo_t * f,
2084 u32 fifo_offset, quic_rx_packet_ctx_t * pctx)
2088 u32 thread_index = vlib_get_thread_index ();
2089 u32 cur_deq = svm_fifo_max_dequeue (f) - fifo_offset;
2090 quicly_context_t *quicly_ctx;
2091 session_t *udp_session;
2094 ret = svm_fifo_peek (f, fifo_offset,
2095 SESSION_CONN_HDR_LEN, (u8 *) & pctx->ph);
2096 QUIC_ASSERT (ret == SESSION_CONN_HDR_LEN);
2097 QUIC_ASSERT (pctx->ph.data_offset == 0);
2098 full_len = pctx->ph.data_length + SESSION_CONN_HDR_LEN;
2099 if (full_len > cur_deq)
2101 QUIC_ERR ("Not enough data in fifo RX");
2105 /* Quicly can read len bytes from the fifo at offset:
2106 * ph.data_offset + SESSION_CONN_HDR_LEN */
2107 ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN + fifo_offset,
2108 pctx->ph.data_length, pctx->data);
2109 if (ret != pctx->ph.data_length)
2111 QUIC_ERR ("Not enough data peeked in RX");
2115 quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
2116 quic_build_sockaddr (&pctx->sa, &pctx->salen, &pctx->ph.rmt_ip,
2117 pctx->ph.rmt_port, pctx->ph.is_ip4);
2118 quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2119 plen = quicly_decode_packet (quicly_ctx, &pctx->packet,
2120 pctx->data, pctx->ph.data_length);
2122 if (plen == SIZE_MAX)
2127 rv = quic_find_packet_ctx (pctx, thread_index);
2128 if (rv == QUIC_PACKET_TYPE_RECEIVE)
2130 pctx->ptype = QUIC_PACKET_TYPE_RECEIVE;
2133 else if (rv == QUIC_PACKET_TYPE_MIGRATE)
2135 pctx->ptype = QUIC_PACKET_TYPE_MIGRATE;
2136 /* Connection found but on wrong thread, ask move */
2138 else if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
2140 pctx->ptype = QUIC_PACKET_TYPE_ACCEPT;
2141 udp_session = session_get_from_handle (udp_session_handle);
2142 pctx->ctx_index = udp_session->opaque;
2143 pctx->thread_index = thread_index;
2147 pctx->ptype = QUIC_PACKET_TYPE_RESET;
2153 quic_udp_session_rx_callback (session_t * udp_session)
2155 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
2156 quic_ctx_t *ctx = NULL, *prev_ctx = NULL;
2157 svm_fifo_t *f = udp_session->rx_fifo;
2159 u64 udp_session_handle = session_handle (udp_session);
2161 u32 thread_index = vlib_get_thread_index ();
2162 u32 cur_deq, fifo_offset, max_packets, i;
2164 quic_rx_packet_ctx_t packets_ctx[QUIC_RCV_MAX_BATCH_PACKETS];
2166 if (udp_session->flags & SESSION_F_IS_MIGRATING)
2168 QUIC_DBG (3, "RX on migrating udp session");
2173 max_deq = svm_fifo_max_dequeue (f);
2178 max_packets = QUIC_RCV_MAX_BATCH_PACKETS;
2181 clib_memset (packets_ctx, 0xfa,
2182 QUIC_RCV_MAX_BATCH_PACKETS * sizeof (quic_rx_packet_ctx_t));
2185 for (i = 0; i < max_packets; i++)
2187 packets_ctx[i].thread_index = UINT32_MAX;
2188 packets_ctx[i].ctx_index = UINT32_MAX;
2189 packets_ctx[i].ptype = QUIC_PACKET_TYPE_DROP;
2191 cur_deq = max_deq - fifo_offset;
2194 max_packets = i + 1;
2197 if (cur_deq < SESSION_CONN_HDR_LEN)
2199 fifo_offset = max_deq;
2200 max_packets = i + 1;
2201 QUIC_ERR ("Fifo %d < header size in RX", cur_deq);
2204 rv = quic_process_one_rx_packet (udp_session_handle, f,
2205 fifo_offset, &packets_ctx[i]);
2206 if (packets_ctx[i].ptype != QUIC_PACKET_TYPE_MIGRATE)
2207 fifo_offset += SESSION_CONN_HDR_LEN + packets_ctx[i].ph.data_length;
2210 max_packets = i + 1;
2215 for (i = 0; i < max_packets; i++)
2217 switch (packets_ctx[i].ptype)
2219 case QUIC_PACKET_TYPE_RECEIVE:
2220 ctx = quic_ctx_get (packets_ctx[i].ctx_index, thread_index);
2221 rv = quicly_receive (ctx->conn, NULL, &packets_ctx[i].sa,
2222 &packets_ctx[i].packet);
2223 if (rv && rv != QUICLY_ERROR_PACKET_IGNORED)
2225 QUIC_ERR ("quicly_receive return error %U",
2226 quic_format_err, rv);
2229 case QUIC_PACKET_TYPE_ACCEPT:
2230 quic_accept_connection (&packets_ctx[i]);
2232 case QUIC_PACKET_TYPE_RESET:
2233 quic_reset_connection (udp_session_handle, &packets_ctx[i]);
2237 ctx = prev_ctx = NULL;
2238 for (i = 0; i < max_packets; i++)
2241 switch (packets_ctx[i].ptype)
2243 case QUIC_PACKET_TYPE_RECEIVE:
2244 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2245 packets_ctx[i].thread_index);
2246 quic_check_quic_session_connected (ctx);
2247 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2248 packets_ctx[i].thread_index);
2250 case QUIC_PACKET_TYPE_ACCEPT:
2251 ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2252 packets_ctx[i].thread_index);
2255 continue; /* this exits the for loop since other packet types are
2256 necessarily the last in the batch */
2258 if (ctx != prev_ctx)
2259 quic_send_packets (ctx);
2262 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
2263 f = udp_session->rx_fifo;
2264 svm_fifo_dequeue_drop (f, fifo_offset);
2266 if (svm_fifo_max_dequeue (f))
2273 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2274 transport_endpoint_t * tep, u8 is_lcl)
2276 session_t *udp_session;
2277 if (!quic_ctx_is_stream (ctx))
2279 udp_session = session_get_from_handle (ctx->udp_session_handle);
2280 session_get_endpoint (udp_session, tep, is_lcl);
2285 quic_get_transport_listener_endpoint (u32 listener_index,
2286 transport_endpoint_t * tep, u8 is_lcl)
2289 app_listener_t *app_listener;
2290 session_t *udp_listen_session;
2291 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2292 if (quic_ctx_is_listener (ctx))
2294 app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2295 udp_listen_session = app_listener_get_session (app_listener);
2296 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2298 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2302 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2303 transport_endpoint_t * tep, u8 is_lcl)
2306 ctx = quic_ctx_get (ctx_index, thread_index);
2307 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2311 static session_cb_vft_t quic_app_cb_vft = {
2312 .session_accept_callback = quic_udp_session_accepted_callback,
2313 .session_disconnect_callback = quic_udp_session_disconnect_callback,
2314 .session_connected_callback = quic_udp_session_connected_callback,
2315 .session_reset_callback = quic_udp_session_reset_callback,
2316 .session_migrate_callback = quic_udp_session_migrate_callback,
2317 .add_segment_callback = quic_add_segment_callback,
2318 .del_segment_callback = quic_del_segment_callback,
2319 .builtin_app_rx_callback = quic_udp_session_rx_callback,
2320 .session_cleanup_callback = quic_udp_session_cleanup_callback,
2321 .app_cert_key_pair_delete_callback = quic_app_cert_key_pair_delete_callback,
2324 static const transport_proto_vft_t quic_proto = {
2325 .connect = quic_connect,
2326 .close = quic_proto_on_close,
2327 .start_listen = quic_start_listen,
2328 .stop_listen = quic_stop_listen,
2329 .get_connection = quic_connection_get,
2330 .get_listener = quic_listener_get,
2331 .update_time = quic_update_time,
2332 .app_rx_evt = quic_custom_app_rx_callback,
2333 .custom_tx = quic_custom_tx_callback,
2334 .format_connection = format_quic_connection,
2335 .format_half_open = format_quic_half_open,
2336 .format_listener = format_quic_listener,
2337 .get_transport_endpoint = quic_get_transport_endpoint,
2338 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2339 .transport_options = {
2340 .tx_type = TRANSPORT_TX_INTERNAL,
2341 .service_type = TRANSPORT_SERVICE_APP,
2346 static quicly_stream_open_t on_stream_open = { quic_on_stream_open };
2347 static quicly_closed_by_peer_t on_closed_by_peer = { quic_on_closed_by_peer };
2348 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
2351 quic_register_cipher_suite (crypto_engine_type_t type,
2352 ptls_cipher_suite_t ** ciphers)
2354 quic_main_t *qm = &quic_main;
2355 vec_validate (qm->quic_ciphers, type);
2356 clib_bitmap_set (qm->available_crypto_engines, type, 1);
2357 qm->quic_ciphers[type] = ciphers;
2361 quic_update_fifo_size ()
2363 quic_main_t *qm = &quic_main;
2364 segment_manager_props_t *seg_mgr_props =
2365 application_get_segment_manager_properties (qm->app_index);
2370 ("error while getting segment_manager_props_t, can't update fifo-size");
2374 seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
2375 seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
2378 static clib_error_t *
2379 quic_init (vlib_main_t * vm)
2381 u32 segment_size = 256 << 20;
2382 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2383 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2384 vnet_app_attach_args_t _a, *a = &_a;
2385 u64 options[APP_OPTIONS_N_OPTIONS];
2386 quic_main_t *qm = &quic_main;
2389 num_threads = 1 /* main thread */ + vtm->n_threads;
2391 clib_memset (a, 0, sizeof (*a));
2392 clib_memset (options, 0, sizeof (options));
2394 a->session_cb_vft = &quic_app_cb_vft;
2395 a->api_client_index = APP_INVALID_INDEX;
2396 a->options = options;
2397 a->name = format (0, "quic");
2398 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2399 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2400 a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
2401 a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
2402 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
2403 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2404 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2405 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2407 if (vnet_application_attach (a))
2409 clib_warning ("failed to attach quic app");
2410 return clib_error_return (0, "failed to attach quic app");
2413 vec_validate (qm->ctx_pool, num_threads - 1);
2414 vec_validate (qm->wrk_ctx, num_threads - 1);
2415 for (i = 0; i < num_threads; i++)
2417 qm->wrk_ctx[i].next_cid.thread_id = i;
2418 tw = &qm->wrk_ctx[i].timer_wheel;
2419 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2420 1e-3 /* timer period 1ms */ , ~0);
2421 tw->last_run_time = vlib_time_now (vlib_get_main ());
2422 clib_bihash_init_24_8 (&qm->wrk_ctx[i].crypto_context_hash,
2423 "quic crypto contexts", 64, 128 << 10);
2426 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2429 qm->app_index = a->app_index;
2430 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2431 / QUIC_TSTAMP_RESOLUTION;
2432 qm->session_cache.super.cb = quic_encrypt_ticket_cb;
2434 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2435 FIB_PROTOCOL_IP4, ~0);
2436 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2437 FIB_PROTOCOL_IP6, ~0);
2439 clib_bitmap_alloc (qm->available_crypto_engines,
2440 app_crypto_engine_n_types ());
2441 quic_register_cipher_suite (CRYPTO_ENGINE_VPP, quic_crypto_cipher_suites);
2442 quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
2443 ptls_openssl_cipher_suites);
2444 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2449 VLIB_INIT_FUNCTION (quic_init);
2451 static clib_error_t *
2452 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2453 unformat_input_t * input,
2454 vlib_cli_command_t * cmd)
2456 quic_main_t *qm = &quic_main;
2457 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
2458 return clib_error_return (0, "unknown input '%U'",
2459 format_unformat_error, input);
2460 if (unformat (input, "vpp"))
2461 qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2462 else if (unformat (input, "picotls"))
2463 qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2465 return clib_error_return (0, "unknown input '%U'",
2466 format_unformat_error, input);
2470 u64 quic_fifosize = 0;
2471 static clib_error_t *
2472 quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
2473 unformat_input_t * input,
2474 vlib_cli_command_t * cmd)
2476 quic_main_t *qm = &quic_main;
2477 unformat_input_t _line_input, *line_input = &_line_input;
2480 if (!unformat_user (input, unformat_line_input, line_input))
2483 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2485 if (unformat (line_input, "%U", unformat_memory_size, &tmp))
2487 if (tmp >= 0x100000000ULL)
2489 return clib_error_return
2490 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
2492 qm->udp_fifo_size = tmp;
2493 quic_update_fifo_size ();
2496 return clib_error_return (0, "unknown input '%U'",
2497 format_unformat_error, line_input);
2504 quic_get_counter_value (u32 event_code)
2508 vlib_error_main_t *em;
2514 vm = vlib_get_main ();
2515 em = &vm->error_main;
2516 n = vlib_get_node (vm, quic_input_node.index);
2519 foreach_vlib_main(({
2520 em = &this_vlib_main->error_main;
2521 i = n->error_heap_index + code;
2522 c = em->counters[i];
2524 if (i < vec_len (em->counters_last_clear))
2525 c -= em->counters_last_clear[i];
2534 quic_show_aggregated_stats (vlib_main_t * vm)
2536 u32 num_workers = vlib_num_workers ();
2537 quic_main_t *qm = &quic_main;
2538 quic_ctx_t *ctx = NULL;
2539 quicly_stats_t st, agg_stats;
2540 u32 i, nconn = 0, nstream = 0;
2542 clib_memset (&agg_stats, 0, sizeof (agg_stats));
2543 for (i = 0; i < num_workers + 1; i++)
2546 pool_foreach (ctx, qm->ctx_pool[i],
2548 if (quic_ctx_is_conn (ctx) && ctx->conn)
2550 quicly_get_stats (ctx->conn, &st);
2551 agg_stats.rtt.smoothed += st.rtt.smoothed;
2552 agg_stats.rtt.minimum += st.rtt.minimum;
2553 agg_stats.rtt.variance += st.rtt.variance;
2554 agg_stats.num_packets.received += st.num_packets.received;
2555 agg_stats.num_packets.sent += st.num_packets.sent;
2556 agg_stats.num_packets.lost += st.num_packets.lost;
2557 agg_stats.num_packets.ack_received += st.num_packets.ack_received;
2558 agg_stats.num_bytes.received += st.num_bytes.received;
2559 agg_stats.num_bytes.sent += st.num_bytes.sent;
2562 else if (quic_ctx_is_stream (ctx))
2567 vlib_cli_output (vm, "-------- Connections --------");
2568 vlib_cli_output (vm, "Current: %u", nconn);
2569 vlib_cli_output (vm, "Opened: %d",
2570 quic_get_counter_value (QUIC_ERROR_OPENED_CONNECTION));
2571 vlib_cli_output (vm, "Closed: %d",
2572 quic_get_counter_value (QUIC_ERROR_CLOSED_CONNECTION));
2573 vlib_cli_output (vm, "---------- Streams ----------");
2574 vlib_cli_output (vm, "Current: %u", nstream);
2575 vlib_cli_output (vm, "Opened: %d",
2576 quic_get_counter_value (QUIC_ERROR_OPENED_STREAM));
2577 vlib_cli_output (vm, "Closed: %d",
2578 quic_get_counter_value (QUIC_ERROR_CLOSED_STREAM));
2579 vlib_cli_output (vm, "---------- Packets ----------");
2580 vlib_cli_output (vm, "RX Total: %d",
2581 quic_get_counter_value (QUIC_ERROR_RX_PACKETS));
2582 vlib_cli_output (vm, "RX 0RTT: %d",
2583 quic_get_counter_value (QUIC_ERROR_ZERO_RTT_RX_PACKETS));
2584 vlib_cli_output (vm, "RX 1RTT: %d",
2585 quic_get_counter_value (QUIC_ERROR_ONE_RTT_RX_PACKETS));
2586 vlib_cli_output (vm, "TX Total: %d",
2587 quic_get_counter_value (QUIC_ERROR_TX_PACKETS));
2588 vlib_cli_output (vm, "----------- Stats -----------");
2589 vlib_cli_output (vm, "Min RTT %f",
2590 nconn > 0 ? agg_stats.rtt.minimum / nconn : 0);
2591 vlib_cli_output (vm, "Smoothed RTT %f",
2592 nconn > 0 ? agg_stats.rtt.smoothed / nconn : 0);
2593 vlib_cli_output (vm, "Variance on RTT %f",
2594 nconn > 0 ? agg_stats.rtt.variance / nconn : 0);
2595 vlib_cli_output (vm, "Packets Received %lu",
2596 agg_stats.num_packets.received);
2597 vlib_cli_output (vm, "Packets Sent %lu", agg_stats.num_packets.sent);
2598 vlib_cli_output (vm, "Packets Lost %lu", agg_stats.num_packets.lost);
2599 vlib_cli_output (vm, "Packets Acks %lu",
2600 agg_stats.num_packets.ack_received);
2601 vlib_cli_output (vm, "RX bytes %lu", agg_stats.num_bytes.received);
2602 vlib_cli_output (vm, "TX bytes %lu", agg_stats.num_bytes.sent);
2606 quic_format_quicly_conn_id (u8 * s, va_list * args)
2608 quicly_cid_plaintext_t *mid = va_arg (*args, quicly_cid_plaintext_t *);
2609 s = format (s, "C%x_%x", mid->master_id, mid->thread_id);
2614 quic_format_quicly_stream_id (u8 * s, va_list * args)
2616 quicly_stream_t *stream = va_arg (*args, quicly_stream_t *);
2618 format (s, "%U S%lx", quic_format_quicly_conn_id,
2619 quicly_get_master_id (stream->conn), stream->stream_id);
2624 quic_format_listener_ctx (u8 * s, va_list * args)
2626 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2627 s = format (s, "[#%d][%x][Listener]", ctx->c_thread_index, ctx->c_c_index);
2632 quic_format_connection_ctx (u8 * s, va_list * args)
2634 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2635 quicly_stats_t quicly_stats;
2637 s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2641 s = format (s, "- no conn -\n");
2644 s = format (s, "[%U]",
2645 quic_format_quicly_conn_id, quicly_get_master_id (ctx->conn));
2646 quicly_get_stats (ctx->conn, &quicly_stats);
2648 s = format (s, "[RTT >%3d, ~%3d, V%3d, last %3d]",
2649 quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
2650 quicly_stats.rtt.variance, quicly_stats.rtt.latest);
2651 s = format (s, " TX:%d RX:%d loss:%d ack:%d",
2652 quicly_stats.num_packets.sent,
2653 quicly_stats.num_packets.received,
2654 quicly_stats.num_packets.lost,
2655 quicly_stats.num_packets.ack_received);
2660 quic_format_stream_ctx (u8 * s, va_list * args)
2662 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2663 session_t *stream_session;
2664 quicly_stream_t *stream = ctx->stream;
2667 s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2668 s = format (s, "[%U]", quic_format_quicly_stream_id, stream);
2670 stream_session = session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
2671 if (!stream_session)
2673 s = format (s, "- no session -\n");
2676 txs = svm_fifo_max_dequeue (stream_session->tx_fifo);
2677 rxs = svm_fifo_max_dequeue (stream_session->rx_fifo);
2678 s = format (s, "[rx %d tx %d]\n", rxs, txs);
2682 static clib_error_t *
2683 quic_show_connections_command_fn (vlib_main_t * vm,
2684 unformat_input_t * input,
2685 vlib_cli_command_t * cmd)
2687 unformat_input_t _line_input, *line_input = &_line_input;
2688 u8 show_listeners = 0, show_conn = 0, show_stream = 0;
2689 u32 num_workers = vlib_num_workers ();
2690 quic_main_t *qm = &quic_main;
2691 clib_error_t *error = 0;
2692 quic_ctx_t *ctx = NULL;
2694 session_cli_return_if_not_enabled ();
2696 if (!unformat_user (input, unformat_line_input, line_input))
2698 quic_show_aggregated_stats (vm);
2702 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2704 if (unformat (line_input, "listener"))
2706 else if (unformat (line_input, "conn"))
2708 else if (unformat (line_input, "stream"))
2712 error = clib_error_return (0, "unknown input `%U'",
2713 format_unformat_error, line_input);
2718 for (int i = 0; i < num_workers + 1; i++)
2721 pool_foreach (ctx, qm->ctx_pool[i],
2723 if (quic_ctx_is_stream (ctx) && show_stream)
2724 vlib_cli_output (vm, "%U", quic_format_stream_ctx, ctx);
2725 else if (quic_ctx_is_listener (ctx) && show_listeners)
2726 vlib_cli_output (vm, "%U", quic_format_listener_ctx, ctx);
2727 else if (quic_ctx_is_conn (ctx) && show_conn)
2728 vlib_cli_output (vm, "%U", quic_format_connection_ctx, ctx);
2734 unformat_free (line_input);
2739 VLIB_CLI_COMMAND (quic_plugin_crypto_command, static) =
2741 .path = "quic set crypto api",
2742 .short_help = "quic set crypto api [picotls, vpp]",
2743 .function = quic_plugin_crypto_command_fn,
2745 VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
2747 .path = "quic set fifo-size",
2748 .short_help = "quic set fifo-size N[K|M|G] (default 64K)",
2749 .function = quic_plugin_set_fifo_size_command_fn,
2751 VLIB_CLI_COMMAND(quic_show_ctx_command, static)=
2753 .path = "show quic",
2754 .short_help = "show quic",
2755 .function = quic_show_connections_command_fn,
2757 VLIB_CLI_COMMAND (quic_list_crypto_context_command, static) =
2759 .path = "show quic crypto context",
2760 .short_help = "list quic crypto contextes",
2761 .function = quic_list_crypto_context_command_fn,
2763 VLIB_PLUGIN_REGISTER () =
2765 .version = VPP_BUILD_VER,
2766 .description = "Quic transport protocol",
2767 .default_disabled = 1,
2771 static clib_error_t *
2772 quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
2774 quic_main_t *qm = &quic_main;
2778 qm->udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
2779 qm->udp_fifo_prealloc = 0;
2780 qm->connection_timeout = QUIC_DEFAULT_CONN_TIMEOUT;
2781 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2783 if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
2785 if (tmp >= 0x100000000ULL)
2787 return clib_error_return (0,
2788 "fifo-size %llu (0x%llx) too large",
2791 qm->udp_fifo_size = tmp;
2793 else if (unformat (input, "conn-timeout %u", &i))
2794 qm->connection_timeout = i;
2795 else if (unformat (input, "fifo-prealloc %u", &i))
2796 qm->udp_fifo_prealloc = i;
2798 return clib_error_return (0, "unknown input '%U'",
2799 format_unformat_error, input);
2805 VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
2808 quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
2809 vlib_frame_t * frame)
2815 VLIB_REGISTER_NODE (quic_input_node) =
2817 .function = quic_node_fn,
2818 .name = "quic-input",
2819 .vector_size = sizeof (u32),
2820 .type = VLIB_NODE_TYPE_INTERNAL,
2821 .n_errors = ARRAY_LEN (quic_error_strings),
2822 .error_strings = quic_error_strings,
2827 * fd.io coding-style-patch-verification: ON
2830 * eval: (c-set-style "gnu")