quic: make quic fifo size configurable via cli
[vpp.git] / src / plugins / quic / quic.c
index 97f03e5..ff0e0d6 100644 (file)
 
 #include <quicly/defaults.h>
 
+static char *quic_error_strings[] = {
+#define quic_error(n,s) s,
+#include "quic_error.def"
+#undef quic_error
+};
 
 static quic_main_t quic_main;
 static void quic_update_timer (quic_ctx_t * ctx);
@@ -225,6 +230,15 @@ quic_connection_delete (quic_ctx_t * ctx)
   quic_ctx_free (ctx);
 }
 
+void
+quic_increment_counter (u8 evt, u8 val)
+{
+  vlib_main_t *vm = vlib_get_main ();
+  vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
+}
+
+
+
 /**
  * Called when quicly return an error
  * This function interacts tightly with quic_proto_on_close
@@ -301,15 +315,15 @@ quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
   /*  Read dest address from quicly-provided sockaddr */
   if (hdr.is_ip4)
     {
-      ASSERT (packet->sa.sa_family == AF_INET);
-      struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->sa;
+      ASSERT (packet->dest.sa.sa_family == AF_INET);
+      struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
       hdr.rmt_port = sa4->sin_port;
       hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
     }
   else
     {
-      ASSERT (packet->sa.sa_family == AF_INET6);
-      struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->sa;
+      ASSERT (packet->dest.sa.sa_family == AF_INET6);
+      struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
       hdr.rmt_port = sa6->sin6_port;
       clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
     }
@@ -326,6 +340,9 @@ quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
       QUIC_DBG (1, "Not enough space to enqueue payload");
       return QUIC_ERROR_FULL_FIFO;
     }
+
+  quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
+
   return 0;
 }
 
@@ -920,14 +937,11 @@ quic_store_quicly_ctx (application_t * app, u8 is_client)
   quicly_ctx->now = &quicly_vpp_now_cb;
   quicly_amend_ptls_context (quicly_ctx->tls);
 
-  quicly_ctx->event_log.mask = 0;      /* logs */
-  quicly_ctx->event_log.cb = quicly_new_default_event_logger (stderr);
-
   quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
   quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
   quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
-  quicly_ctx->transport_params.max_stream_data.bidi_local = (QUIC_FIFO_SIZE - 1);      /* max_enq is SIZE - 1 */
-  quicly_ctx->transport_params.max_stream_data.bidi_remote = (QUIC_FIFO_SIZE - 1);     /* max_enq is SIZE - 1 */
+  quicly_ctx->transport_params.max_stream_data.bidi_local = (qm->udp_fifo_size - 1);   /* max_enq is SIZE - 1 */
+  quicly_ctx->transport_params.max_stream_data.bidi_remote = (qm->udp_fifo_size - 1);  /* max_enq is SIZE - 1 */
   quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
 
   quicly_ctx->tls->random_bytes (quicly_ctx_data->cid_key, 16);
@@ -937,6 +951,7 @@ quic_store_quicly_ctx (application_t * app, u8 is_client)
                     strlen (quicly_ctx_data->cid_key));
   quicly_ctx->cid_encryptor =
     quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
+                                     &ptls_openssl_aes128ecb,
                                      &ptls_openssl_sha256, key_vec);
   if (is_client)
     return;
@@ -1532,7 +1547,8 @@ quic_session_connected_callback (u32 quic_app_index, u32 ctx_index,
 
   quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
   ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
-                       sa, salen, &quic_main.next_cid,
+                       sa, NULL, &quic_main.next_cid, ptls_iovec_init (NULL,
+                                                                       0),
                        &quic_main.hs_properties, NULL);
   ++quic_main.next_cid.master_id;
   /*  Save context handle in quicly connection */
@@ -1749,7 +1765,7 @@ quic_find_packet_ctx (u32 * ctx_thread, u32 * ctx_index,
        }
       ctx_ = quic_ctx_get (index, vlib_get_thread_index ());
       conn_ = ctx_->conn;
-      if (conn_ && quicly_is_destination (conn_, sa, salen, packet))
+      if (conn_ && quicly_is_destination (conn_, NULL, sa, packet))
        {
          QUIC_DBG (3, "Connection found");
          *ctx_index = index;
@@ -1815,9 +1831,8 @@ quic_create_connection (u32 ctx_index, struct sockaddr *sa,
    * TODO: check if socket is actually listening? */
   ctx = quic_ctx_get (ctx_index, thread_index);
   quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
-  if ((rv = quicly_accept (&conn, quicly_ctx, sa, salen,
-                          &packet, ptls_iovec_init (NULL, 0),
-                          &quic_main.next_cid, NULL)))
+  if ((rv = quicly_accept (&conn, quicly_ctx, NULL, sa,
+                          &packet, NULL, &quic_main.next_cid, NULL)))
     {
       /* Invalid packet, pass */
       assert (conn == NULL);
@@ -1863,7 +1878,7 @@ quic_reset_connection (u64 udp_session_handle,
       || packet.cid.dest.plaintext.thread_id != 0)
     return 0;
   quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
-  dgram = quicly_send_stateless_reset (quicly_ctx, sa, salen,
+  dgram = quicly_send_stateless_reset (quicly_ctx, sa, NULL,
                                       &packet.cid.dest.plaintext);
   if (dgram == NULL)
     return 1;
@@ -1959,6 +1974,7 @@ quic_process_one_rx_packet (u64 udp_session_handle,
       return 1;
     }
 
+  quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
   rv = 0;
   quic_build_sockaddr (sa, &salen, &ph.rmt_ip, ph.rmt_port, ph.is_ip4);
   quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
@@ -1978,7 +1994,7 @@ quic_process_one_rx_packet (u64 udp_session_handle,
   if (err == 0)
     {
       ctx = quic_ctx_get (packet_ctx->ctx_index, thread_index);
-      rv = quicly_receive (ctx->conn, &packet_ctx->packet);
+      rv = quicly_receive (ctx->conn, NULL, sa, &packet_ctx->packet);
       if (rv)
        QUIC_DBG (1, "quicly_receive return error %d", rv);
     }
@@ -2166,6 +2182,24 @@ quic_register_cipher_suite (quic_crypto_engine_t type,
   qm->quic_ciphers[type] = ciphers;
 }
 
+static void
+quic_update_fifo_size ()
+{
+  quic_main_t *qm = &quic_main;
+  segment_manager_props_t *seg_mgr_props =
+    application_get_segment_manager_properties (qm->app_index);
+
+  if (seg_mgr_props)
+    {
+      clib_warning
+       ("error while getting segment_manager_props_t, can't update fifo-size");
+      return;
+    }
+
+  seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
+  seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
+}
+
 static clib_error_t *
 quic_init (vlib_main_t * vm)
 {
@@ -2175,7 +2209,6 @@ quic_init (vlib_main_t * vm)
   vnet_app_attach_args_t _a, *a = &_a;
   u64 options[APP_OPTIONS_N_OPTIONS];
   quic_main_t *qm = &quic_main;
-  u32 fifo_size = QUIC_FIFO_SIZE;
   u32 num_threads, i;
 
   num_threads = 1 /* main thread */  + vtm->n_threads;
@@ -2189,8 +2222,9 @@ quic_init (vlib_main_t * vm)
   a->name = format (0, "quic");
   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
-  a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
-  a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
+  a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
+  a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
+  a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
@@ -2226,7 +2260,7 @@ quic_init (vlib_main_t * vm)
   transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
                               FIB_PROTOCOL_IP6, ~0);
 
-  quic_register_cipher_suite (CRYPTO_ENGINE_VPP, vpp_crypto_cipher_suites);
+  quic_register_cipher_suite (CRYPTO_ENGINE_VPP, quic_crypto_cipher_suites);
   quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
                              ptls_openssl_cipher_suites);
   qm->default_cipher = CRYPTO_ENGINE_PICOTLS;
@@ -2255,6 +2289,70 @@ quic_plugin_crypto_command_fn (vlib_main_t * vm,
   return 0;
 }
 
+u64 quic_fifosize = 0;
+static clib_error_t *
+quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
+                                     unformat_input_t * input,
+                                     vlib_cli_command_t * cmd)
+{
+  unformat_input_t _line_input, *line_input = &_line_input;
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat
+         (line_input, "%U", unformat_data_size, &quic_main.udp_fifo_size))
+       quic_update_fifo_size ();
+      else
+       return clib_error_return (0, "unknown input '%U'",
+                                 format_unformat_error, line_input);
+    }
+
+  return 0;
+}
+
+static u8 *
+quic_format_ctx_stat (u8 * s, va_list * args)
+{
+  quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
+  quicly_stats_t quicly_stats;
+
+  quicly_get_stats (ctx->conn, &quicly_stats);
+
+  s = format (s, "\n\rQUIC conn stats \n\r");
+
+  s =
+    format (s, "RTT: min:%d, smoothed:%d, variance:%d, latest:%d \n\r",
+           quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
+           quicly_stats.rtt.variance, quicly_stats.rtt.latest);
+  s = format (s, "Packet loss:%d \n\r", quicly_stats.num_packets.lost);
+
+  return s;
+}
+
+static clib_error_t *
+quic_plugin_showstats_command_fn (vlib_main_t * vm,
+                                 unformat_input_t * input,
+                                 vlib_cli_command_t * cmd)
+{
+  quic_main_t *qm = &quic_main;
+  quic_ctx_t *ctx = NULL;
+  u32 num_workers = vlib_num_workers ();
+
+  for (int i = 0; i < num_workers + 1; i++)
+    {
+      /* *INDENT-OFF* */
+      pool_foreach (ctx, qm->ctx_pool[i],
+      ({
+        if(!(ctx->flags & QUIC_F_IS_LISTENER) && !(ctx->flags & QUIC_F_IS_STREAM))
+          vlib_cli_output (vm, "%U", quic_format_ctx_stat, ctx);
+      }));
+      /* *INDENT-ON* */
+    }
+  return 0;
+}
+
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND(quic_plugin_crypto_command, static)=
 {
@@ -2262,6 +2360,18 @@ VLIB_CLI_COMMAND(quic_plugin_crypto_command, static)=
   .short_help = "quic set crypto api [picotls, vpp]",
   .function = quic_plugin_crypto_command_fn,
 };
+VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
+{
+  .path = "quic set fifo-size",
+  .short_help = "quic set fifo-size N[Kb|Mb|GB] (default 64K)",
+  .function = quic_plugin_set_fifo_size_command_fn,
+};
+VLIB_CLI_COMMAND(quic_plugin_stats_command, static)=
+{
+  .path = "show quic stats",
+  .short_help = "show quic stats",
+  .function = quic_plugin_showstats_command_fn,
+};
 VLIB_PLUGIN_REGISTER () =
 {
   .version = VPP_BUILD_VER,
@@ -2270,6 +2380,51 @@ VLIB_PLUGIN_REGISTER () =
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
+{
+  quic_main.udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
+  quic_main.udp_fifo_prealloc = 0;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat
+         (input, "fifo-size %U", unformat_data_size,
+          &quic_main.udp_fifo_size))
+       ;
+      else
+       if (unformat
+           (input, "fifo-prealloc %u", &quic_main.udp_fifo_prealloc))
+       ;
+      else
+       return clib_error_return (0, "unknown input '%U'",
+                                 format_unformat_error, input);
+    }
+
+  return 0;
+}
+
+VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
+
+static uword
+quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
+             vlib_frame_t * frame)
+{
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (quic_input_node) =
+{
+  .function = quic_node_fn,
+  .name = "quic-input",
+  .vector_size = sizeof (u32),
+  .type = VLIB_NODE_TYPE_INTERNAL,
+  .n_errors = ARRAY_LEN (quic_error_strings),
+  .error_strings = quic_error_strings,
+};
+/* *INDENT-ON* */
+
 /*
  * fd.io coding-style-patch-verification: ON
  *