quic: Use per thread next_cid
[vpp.git] / src / plugins / quic / quic.c
index f51881e..d59504a 100644 (file)
@@ -216,6 +216,12 @@ quic_ctx_is_listener (quic_ctx_t * ctx)
   return (ctx->flags & QUIC_F_IS_LISTENER);
 }
 
+static inline int
+quic_ctx_is_conn (quic_ctx_t * ctx)
+{
+  return !(quic_ctx_is_listener (ctx) || quic_ctx_is_stream (ctx));
+}
+
 static session_t *
 get_stream_session_from_stream (quicly_stream_t * stream)
 {
@@ -335,6 +341,7 @@ quic_connection_delete (quic_ctx_t * ctx)
 
   /*  Delete the connection from the connection map */
   conn = ctx->conn;
+  ctx->conn = NULL;
   quic_make_connection_key (&kv, quicly_get_master_id (conn));
   QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
            kv.key[1]);
@@ -344,7 +351,6 @@ quic_connection_delete (quic_ctx_t * ctx)
 
   if (ctx->conn)
     quicly_free (ctx->conn);
-  ctx->conn = NULL;
   session_transport_delete_notify (&ctx->connection);
 }
 
@@ -549,6 +555,7 @@ quic_on_stream_destroy (quicly_stream_t * stream, int err)
   stream_session->session_state = SESSION_STATE_CLOSED;
   session_transport_delete_notify (&sctx->connection);
 
+  quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
   quic_ctx_free (sctx);
   clib_mem_free (stream->data);
 }
@@ -1017,6 +1024,8 @@ quic_connect_stream (session_t * quic_session, u32 opaque)
       QUIC_DBG (2, "Stream open failed with %d", rv);
       return -1;
     }
+  quic_increment_counter (QUIC_ERROR_OPENED_STREAM, 1);
+
   sctx->stream = stream;
 
   QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
@@ -1053,6 +1062,7 @@ quic_connect_stream (session_t * quic_session, u32 opaque)
   if (app_worker_connect_notify (app_wrk, stream_session, opaque))
     {
       QUIC_ERR ("failed to notify app");
+      quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
       quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
       return -1;
     }
@@ -1157,6 +1167,8 @@ quic_proto_on_close (u32 ctx_index, u32 thread_index)
       quicly_conn_t *conn = ctx->conn;
       /* Start connection closing. Keep sending packets until quicly_send
          returns QUICLY_ERROR_FREE_CONNECTION */
+
+      quic_increment_counter (QUIC_ERROR_CLOSED_CONNECTION, 1);
       quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
       /* This also causes all streams to be closed (and the cb called) */
       quic_send_packets (ctx);
@@ -1545,10 +1557,10 @@ quic_udp_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, NULL, &quic_main.next_cid, ptls_iovec_init (NULL,
-                                                                       0),
-                       &quic_main.hs_properties, NULL);
-  ++quic_main.next_cid.master_id;
+                       sa, NULL, &quic_main.wrk_ctx[thread_index].next_cid,
+                       ptls_iovec_init (NULL, 0), &quic_main.hs_properties,
+                       NULL);
+  ++quic_main.wrk_ctx[thread_index].next_cid.master_id;
   /*  Save context handle in quicly connection */
   quic_store_conn_ctx (ctx->conn, ctx);
   assert (ret == 0);
@@ -1601,7 +1613,7 @@ quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
   u32 new_thread = session_thread_from_handle (new_sh);
   quic_ctx_t *ctx;
 
-  QUIC_ERR ("Session %x migrated to %lx", s->session_index, new_sh);
+  QUIC_DBG (2, "Session %x migrated to %lx", s->session_index, new_sh);
   QUIC_ASSERT (vlib_get_thread_index () == s->thread_index);
   ctx = quic_ctx_get (s->opaque, s->thread_index);
   QUIC_ASSERT (ctx->udp_session_handle == session_handle (s));
@@ -1804,7 +1816,8 @@ quic_accept_connection (u32 ctx_index, quic_rx_packet_ctx_t * pctx)
 
   quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
   if ((rv = quicly_accept (&conn, quicly_ctx, NULL, &pctx->sa,
-                          &pctx->packet, NULL, &quic_main.next_cid, NULL)))
+                          &pctx->packet, NULL,
+                          &quic_main.wrk_ctx[thread_index].next_cid, NULL)))
     {
       /* Invalid packet, pass */
       assert (conn == NULL);
@@ -1814,7 +1827,7 @@ quic_accept_connection (u32 ctx_index, quic_rx_packet_ctx_t * pctx)
     }
   assert (conn != NULL);
 
-  ++quic_main.next_cid.master_id;
+  ++quic_main.wrk_ctx[thread_index].next_cid.master_id;
   /* Save ctx handle in quicly connection */
   quic_store_conn_ctx (conn, ctx);
   ctx->conn = conn;
@@ -1854,9 +1867,12 @@ quic_accept_connection (u32 ctx_index, quic_rx_packet_ctx_t * pctx)
       quic_proto_on_close (ctx_index, thread_index);
       return rv;
     }
-  ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
 
-  return quic_send_packets (ctx);
+  ctx->conn_state = QUIC_CONN_STATE_READY;
+  pctx->ctx_index = ctx_index;
+  pctx->thread_index = thread_index;
+
+  return 0;
 }
 
 static int
@@ -2039,13 +2055,22 @@ rx_start:
     }
   for (i = 0; i < max_packets; i++)
     {
-      if (packets_ctx[i].ptype != QUIC_PACKET_TYPE_RECEIVE)
-       continue;
-      ctx = quic_ctx_get (packets_ctx[i].ctx_index,
-                         packets_ctx[i].thread_index);
-      quic_check_quic_session_connected (ctx);
-      ctx = quic_ctx_get (packets_ctx[i].ctx_index,
-                         packets_ctx[i].thread_index);
+      switch (packets_ctx[i].ptype)
+       {
+       case QUIC_PACKET_TYPE_RECEIVE:
+         ctx = quic_ctx_get (packets_ctx[i].ctx_index,
+                             packets_ctx[i].thread_index);
+         quic_check_quic_session_connected (ctx);
+         ctx = quic_ctx_get (packets_ctx[i].ctx_index,
+                             packets_ctx[i].thread_index);
+         break;
+       case QUIC_PACKET_TYPE_ACCEPT:
+         ctx = quic_ctx_get (packets_ctx[i].ctx_index,
+                             packets_ctx[i].thread_index);
+         break;
+       default:
+         continue;
+       }
       quic_send_packets (ctx);
     }
 
@@ -2204,6 +2229,7 @@ quic_init (vlib_main_t * vm)
   /*  Timer wheels, one per thread. */
   for (i = 0; i < num_threads; i++)
     {
+      qm->wrk_ctx[i].next_cid.thread_id = i;
       tw = &qm->wrk_ctx[i].timer_wheel;
       tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
                                           1e-3 /* timer period 1ms */ , ~0);
@@ -2288,65 +2314,239 @@ quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
   return 0;
 }
 
-static u8 *
-quic_format_ctx_stat (u8 * s, va_list * args)
+static inline u64
+quic_get_counter_value (u32 event_code)
 {
-  quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
-  quicly_stats_t quicly_stats;
+  vlib_node_t *n;
+  vlib_main_t *vm;
+  vlib_error_main_t *em;
 
-  quicly_get_stats (ctx->conn, &quicly_stats);
-
-  s = format (s, "\n\rQUIC conn stats \n\r");
+  u32 code, i;
+  u64 c, sum = 0;
+  int index = 0;
 
-  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);
+  vm = vlib_get_main ();
+  em = &vm->error_main;
+  n = vlib_get_node (vm, quic_input_node.index);
+  code = event_code;
+  /* *INDENT-OFF* */
+  foreach_vlib_main(({
+    em = &this_vlib_main->error_main;
+    i = n->error_heap_index + code;
+    c = em->counters[i];
 
-  return s;
+    if (i < vec_len (em->counters_last_clear))
+       c -= em->counters_last_clear[i];
+    sum += c;
+    index++;
+  }));
+  /* *INDENT-ON* */
+  return sum;
 }
 
-static clib_error_t *
-quic_plugin_showstats_command_fn (vlib_main_t * vm,
-                                 unformat_input_t * input,
-                                 vlib_cli_command_t * cmd)
+static void
+quic_show_aggregated_stats (vlib_main_t * vm)
 {
+  u32 num_workers = vlib_num_workers ();
   quic_main_t *qm = &quic_main;
   quic_ctx_t *ctx = NULL;
-  u32 num_workers = vlib_num_workers ();
+  quicly_stats_t st, agg_stats;
+  u32 i, nconn = 0, nstream = 0;
 
-  for (int i = 0; i < num_workers + 1; i++)
+  clib_memset (&agg_stats, 0, sizeof (agg_stats));
+  for (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);
+       if (quic_ctx_is_conn (ctx) && ctx->conn)
+         {
+           quicly_get_stats (ctx->conn, &st);
+           agg_stats.rtt.smoothed += st.rtt.smoothed;
+           agg_stats.rtt.minimum += st.rtt.minimum;
+           agg_stats.rtt.variance += st.rtt.variance;
+           agg_stats.num_packets.received += st.num_packets.received;
+           agg_stats.num_packets.sent += st.num_packets.sent;
+           agg_stats.num_packets.lost += st.num_packets.lost;
+           agg_stats.num_packets.ack_received += st.num_packets.ack_received;
+           agg_stats.num_bytes.received += st.num_bytes.received;
+           agg_stats.num_bytes.sent += st.num_bytes.sent;
+           nconn++;
+         }
+       else if (quic_ctx_is_stream (ctx))
+         nstream++;
       }));
       /* *INDENT-ON* */
     }
-  return 0;
+  vlib_cli_output (vm, "-------- Connections --------");
+  vlib_cli_output (vm, "Current:         %u", nconn);
+  vlib_cli_output (vm, "Opened:          %d",
+                  quic_get_counter_value (QUIC_ERROR_OPENED_CONNECTION));
+  vlib_cli_output (vm, "Closed:          %d",
+                  quic_get_counter_value (QUIC_ERROR_CLOSED_CONNECTION));
+  vlib_cli_output (vm, "---------- Streams ----------");
+  vlib_cli_output (vm, "Current:         %u", nstream);
+  vlib_cli_output (vm, "Opened:          %d",
+                  quic_get_counter_value (QUIC_ERROR_OPENED_STREAM));
+  vlib_cli_output (vm, "Closed:          %d",
+                  quic_get_counter_value (QUIC_ERROR_CLOSED_STREAM));
+  vlib_cli_output (vm, "---------- Packets ----------");
+  vlib_cli_output (vm, "RX Total:        %d",
+                  quic_get_counter_value (QUIC_ERROR_RX_PACKETS));
+  vlib_cli_output (vm, "RX 0RTT:         %d",
+                  quic_get_counter_value (QUIC_ERROR_ZERO_RTT_RX_PACKETS));
+  vlib_cli_output (vm, "RX 1RTT:         %d",
+                  quic_get_counter_value (QUIC_ERROR_ONE_RTT_RX_PACKETS));
+  vlib_cli_output (vm, "TX Total:        %d",
+                  quic_get_counter_value (QUIC_ERROR_TX_PACKETS));
+  vlib_cli_output (vm, "----------- Stats -----------");
+  vlib_cli_output (vm, "Min      RTT     %f",
+                  nconn > 0 ? agg_stats.rtt.minimum / nconn : 0);
+  vlib_cli_output (vm, "Smoothed RTT     %f",
+                  nconn > 0 ? agg_stats.rtt.smoothed / nconn : 0);
+  vlib_cli_output (vm, "Variance on RTT  %f",
+                  nconn > 0 ? agg_stats.rtt.variance / nconn : 0);
+  vlib_cli_output (vm, "Packets Received %lu",
+                  agg_stats.num_packets.received);
+  vlib_cli_output (vm, "Packets Sent     %lu", agg_stats.num_packets.sent);
+  vlib_cli_output (vm, "Packets Lost     %lu", agg_stats.num_packets.lost);
+  vlib_cli_output (vm, "Packets Acks     %lu",
+                  agg_stats.num_packets.ack_received);
+  vlib_cli_output (vm, "RX bytes         %lu", agg_stats.num_bytes.received);
+  vlib_cli_output (vm, "TX bytes         %lu", agg_stats.num_bytes.sent);
+}
+
+static u8 *
+quic_format_quicly_conn_id (u8 * s, va_list * args)
+{
+  quicly_cid_plaintext_t *mid = va_arg (*args, quicly_cid_plaintext_t *);
+  s = format (s, "C%x_%x", mid->master_id, mid->thread_id);
+  return s;
+}
+
+static u8 *
+quic_format_quicly_stream_id (u8 * s, va_list * args)
+{
+  quicly_stream_t *stream = va_arg (*args, quicly_stream_t *);
+  s =
+    format (s, "%U S%lx", quic_format_quicly_conn_id,
+           quicly_get_master_id (stream->conn), stream->stream_id);
+  return s;
+}
+
+static u8 *
+quic_format_listener_ctx (u8 * s, va_list * args)
+{
+  quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
+  s = format (s, "[#%d][%x][Listener]", ctx->c_thread_index, ctx->c_c_index);
+  return s;
+}
+
+static u8 *
+quic_format_connection_ctx (u8 * s, va_list * args)
+{
+  quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
+  quicly_stats_t quicly_stats;
+
+  s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
+
+  if (!ctx->conn)
+    {
+      s = format (s, "- no conn -\n");
+      return s;
+    }
+  s = format (s, "[%U]",
+             quic_format_quicly_conn_id, quicly_get_master_id (ctx->conn));
+  quicly_get_stats (ctx->conn, &quicly_stats);
+
+  s = format (s, "[RTT >%3d, ~%3d, V%3d, last %3d]",
+             quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
+             quicly_stats.rtt.variance, quicly_stats.rtt.latest);
+  s = format (s, " TX:%d RX:%d loss:%d ack:%d",
+             quicly_stats.num_packets.sent,
+             quicly_stats.num_packets.received,
+             quicly_stats.num_packets.lost,
+             quicly_stats.num_packets.ack_received);
+  return s;
+}
+
+static u8 *
+quic_format_stream_ctx (u8 * s, va_list * args)
+{
+  quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
+  session_t *stream_session;
+  quicly_stream_t *stream = ctx->stream;
+  u32 txs, rxs;
+
+  s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
+  s = format (s, "[%U]", quic_format_quicly_stream_id, stream);
+
+  stream_session = session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
+  if (!stream_session)
+    {
+      s = format (s, "- no session -\n");
+      return s;
+    }
+  txs = svm_fifo_max_dequeue (stream_session->tx_fifo);
+  rxs = svm_fifo_max_dequeue (stream_session->rx_fifo);
+  s = format (s, "[rx %d tx %d]\n", rxs, txs);
+  return s;
 }
 
 static clib_error_t *
-quic_show_ctx_command_fn (vlib_main_t * vm, unformat_input_t * input,
-                         vlib_cli_command_t * cmd)
+quic_show_connections_command_fn (vlib_main_t * vm,
+                                 unformat_input_t * input,
+                                 vlib_cli_command_t * cmd)
 {
+  unformat_input_t _line_input, *line_input = &_line_input;
+  u8 show_listeners = 0, show_conn = 0, show_stream = 0;
+  u32 num_workers = vlib_num_workers ();
   quic_main_t *qm = &quic_main;
+  clib_error_t *error = 0;
   quic_ctx_t *ctx = NULL;
-  u32 num_workers = vlib_num_workers ();
+
+  session_cli_return_if_not_enabled ();
+
+  if (!unformat_user (input, unformat_line_input, line_input))
+    {
+      quic_show_aggregated_stats (vm);
+      return 0;
+    }
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (line_input, "listener"))
+       show_listeners = 1;
+      else if (unformat (line_input, "conn"))
+       show_conn = 1;
+      else if (unformat (line_input, "stream"))
+       show_stream = 1;
+      else
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
+    }
 
   for (int i = 0; i < num_workers + 1; i++)
     {
       /* *INDENT-OFF* */
       pool_foreach (ctx, qm->ctx_pool[i],
       ({
-        vlib_cli_output (vm, "%U", format_quic_ctx, ctx, 1);
+        if (quic_ctx_is_stream (ctx) && show_stream)
+          vlib_cli_output (vm, "%U", quic_format_stream_ctx, ctx);
+        else if (quic_ctx_is_listener (ctx) && show_listeners)
+          vlib_cli_output (vm, "%U", quic_format_listener_ctx, ctx);
+       else if (quic_ctx_is_conn (ctx) && show_conn)
+          vlib_cli_output (vm, "%U", quic_format_connection_ctx, ctx);
       }));
       /* *INDENT-ON* */
     }
-  return 0;
+
+done:
+  unformat_free (line_input);
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -2362,17 +2562,11 @@ VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
   .short_help = "quic set fifo-size N[K|M|G] (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_CLI_COMMAND(quic_show_ctx_command, static)=
 {
-  .path = "show quic ctx",
-  .short_help = "show quic ctx",
-  .function = quic_show_ctx_command_fn,
+  .path = "show quic",
+  .short_help = "show quic",
+  .function = quic_show_connections_command_fn,
 };
 VLIB_PLUGIN_REGISTER () =
 {