quic: Add Tx, Rx and packet drop counters
[vpp.git] / src / plugins / quic / quic.c
index 42a839d..3d0da25 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,13 @@ 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
@@ -326,6 +338,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;
 }
 
@@ -1085,16 +1100,12 @@ quic_connect_new_connection (session_endpoint_cfg_t * sep)
   ctx->client_opaque = sep->opaque;
   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
   if (sep->hostname)
-    {
-      ctx->srv_hostname = format (0, "%v", sep->hostname);
-      vec_terminate_c_string (ctx->srv_hostname);
-    }
+    ctx->srv_hostname = format (0, "%v", sep->hostname);
   else
-    {
-      /*  needed by quic for crypto + determining client / server */
-      ctx->srv_hostname =
-       format (0, "%U", format_ip46_address, &sep->ip, sep->is_ip4);
-    }
+    /*  needed by quic for crypto + determining client / server */
+    ctx->srv_hostname =
+      format (0, "%U", format_ip46_address, &sep->ip, sep->is_ip4);
+  vec_terminate_c_string (ctx->srv_hostname);
 
   clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
   cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
@@ -1408,6 +1419,11 @@ quic_on_client_connected (quic_ctx_t * ctx)
 
   /*  If the app opens a stream in its callback it may invalidate ctx */
   ctx = quic_ctx_get (ctx_id, thread_index);
+  /*
+   * app_worker_connect_notify() might have reallocated pool, reload
+   * quic_session pointer
+   */
+  quic_session = session_get (ctx->c_s_index, thread_index);
   quic_session->session_state = SESSION_STATE_LISTENING;
 
   return 0;
@@ -1958,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);
@@ -2001,10 +2018,10 @@ quic_process_one_rx_packet (u64 udp_session_handle,
            {
              /*  Right ctx found, create conn & remove from pool */
              quic_create_connection(*ctx_index_ptr, sa, salen, packet_ctx->packet);
-             pool_put (opening_ctx_pool, ctx_index_ptr);
              *max_packet = packet_n + 1;
              packet_ctx->thread_index = thread_index;
              packet_ctx->ctx_index = *ctx_index_ptr;
+             pool_put (opening_ctx_pool, ctx_index_ptr);
              goto updateOffset;
            }
        }));
@@ -2225,7 +2242,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;
@@ -2254,6 +2271,47 @@ quic_plugin_crypto_command_fn (vlib_main_t * vm,
   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)=
 {
@@ -2261,12 +2319,36 @@ 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_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,
   .description = "Quic transport protocol",
   .default_disabled = 1,
 };
+
+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* */
 
 /*