STATS: Separate socket for fd exchange.
[vpp.git] / src / vpp / stats / stats.c
index 3fe03e4..452871f 100644 (file)
@@ -48,23 +48,23 @@ stats_main_t stats_main;
 #define foreach_stats_msg                                              \
 _(WANT_STATS, want_stats)                                              \
 _(VNET_INTERFACE_SIMPLE_COUNTERS, vnet_interface_simple_counters)      \
-_(WANT_INTERFACE_SIMPLE_STATS, want_interface_simple_stats)    \
+_(WANT_INTERFACE_SIMPLE_STATS, want_interface_simple_stats)             \
 _(VNET_INTERFACE_COMBINED_COUNTERS, vnet_interface_combined_counters)  \
-_(WANT_INTERFACE_COMBINED_STATS, want_interface_combined_stats)        \
+_(WANT_INTERFACE_COMBINED_STATS, want_interface_combined_stats)         \
 _(WANT_PER_INTERFACE_COMBINED_STATS, want_per_interface_combined_stats)        \
-_(WANT_PER_INTERFACE_SIMPLE_STATS, want_per_interface_simple_stats) \
+_(WANT_PER_INTERFACE_SIMPLE_STATS, want_per_interface_simple_stats)     \
 _(VNET_IP4_FIB_COUNTERS, vnet_ip4_fib_counters)                                \
-_(WANT_IP4_FIB_STATS, want_ip4_fib_stats)            \
+_(WANT_IP4_FIB_STATS, want_ip4_fib_stats)                               \
 _(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters)                                \
-_(WANT_IP6_FIB_STATS, want_ip6_fib_stats)        \
+_(WANT_IP6_FIB_STATS, want_ip6_fib_stats)                               \
 _(WANT_IP4_MFIB_STATS, want_ip4_mfib_stats)                             \
 _(WANT_IP6_MFIB_STATS, want_ip6_mfib_stats)                             \
 _(VNET_IP4_NBR_COUNTERS, vnet_ip4_nbr_counters)                                \
-_(WANT_IP4_NBR_STATS, want_ip4_nbr_stats)            \
-_(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters) \
-_(WANT_IP6_NBR_STATS, want_ip6_nbr_stats) \
-_(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats) \
-_(STATS_GET_POLLER_DELAY, stats_get_poller_delay) \
+_(WANT_IP4_NBR_STATS, want_ip4_nbr_stats)                               \
+_(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters)                         \
+_(WANT_IP6_NBR_STATS, want_ip6_nbr_stats)                               \
+_(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
+_(STATS_GET_POLLER_DELAY, stats_get_poller_delay)                       \
 _(WANT_UDP_ENCAP_STATS, want_udp_encap_stats)
 
 #define vl_msg_name_crc_list
@@ -297,6 +297,8 @@ set_client_for_stat (u32 reg, u32 item, vpe_client_registration_t * client)
     {
       pool_get (sm->stats_registrations[reg], registration);
       registration->item = item;
+      registration->client_hash = NULL;
+      registration->clients = NULL;
       hash_set (sm->stats_registration_hash[reg], item,
                registration - sm->stats_registrations[reg]);
     }
@@ -319,13 +321,39 @@ set_client_for_stat (u32 reg, u32 item, vpe_client_registration_t * client)
   return 1;                    //At least one client is doing something ... poll
 }
 
-int
-clear_client_for_stat (u32 reg, u32 item, u32 client_index)
+static void
+clear_one_client (u32 reg_index, u32 reg, u32 item, u32 client_index)
 {
   stats_main_t *sm = &stats_main;
   vpe_client_stats_registration_t *registration;
   vpe_client_registration_t *client;
   uword *p;
+
+  registration = pool_elt_at_index (sm->stats_registrations[reg], reg_index);
+  p = hash_get (registration->client_hash, client_index);
+
+  if (p)
+    {
+      client = pool_elt_at_index (registration->clients, p[0]);
+      hash_unset (registration->client_hash, client->client_index);
+      pool_put (registration->clients, client);
+
+      /* Now check if that was the last client for that item */
+      if (0 == pool_elts (registration->clients))
+       {
+         hash_unset (sm->stats_registration_hash[reg], item);
+         hash_free (registration->client_hash);
+         pool_free (registration->clients);
+         pool_put (sm->stats_registrations[reg], registration);
+       }
+    }
+}
+
+int
+clear_client_for_stat (u32 reg, u32 item, u32 client_index)
+{
+  stats_main_t *sm = &stats_main;
+  uword *p;
   int i, elts;
 
   /* Clear the client first */
@@ -336,24 +364,35 @@ clear_client_for_stat (u32 reg, u32 item, u32 client_index)
     goto exit;
 
   /* If there is, is our client_index one of them */
-  registration = pool_elt_at_index (sm->stats_registrations[reg], p[0]);
-  p = hash_get (registration->client_hash, client_index);
+  clear_one_client (p[0], reg, item, client_index);
 
-  if (!p)
-    goto exit;
+exit:
+  elts = 0;
+  /* Now check if that was the last item in any of the listened to stats */
+  for (i = 0; i < STATS_REG_N_IDX; i++)
+    {
+      elts += pool_elts (sm->stats_registrations[i]);
+    }
+  return elts;
+}
 
-  client = pool_elt_at_index (registration->clients, p[0]);
-  hash_unset (registration->client_hash, client->client_index);
-  pool_put (registration->clients, client);
+static int
+clear_client_for_all_stats (u32 client_index)
+{
+  stats_main_t *sm = &stats_main;
+  u32 reg_index, item, reg;
+  int i, elts;
 
-  /* Now check if that was the last client for that item */
-  if (0 == pool_elts (registration->clients))
+  /* *INDENT-OFF* */
+  vec_foreach_index(reg, sm->stats_registration_hash)
     {
-      hash_unset (sm->stats_registration_hash[reg], item);
-      pool_put (sm->stats_registrations[reg], registration);
+      hash_foreach(item, reg_index, sm->stats_registration_hash[reg],
+      ({
+        clear_one_client(reg_index, reg, item, client_index);
+      }));
     }
+  /* *INDENT-OFF* */
 
-exit:
   elts = 0;
   /* Now check if that was the last item in any of the listened to stats */
   for (i = 0; i < STATS_REG_N_IDX; i++)
@@ -363,6 +402,22 @@ exit:
   return elts;
 }
 
+static clib_error_t *
+want_stats_reaper (u32 client_index)
+{
+  stats_main_t *sm = &stats_main;
+
+  sm->enable_poller = clear_client_for_all_stats (client_index);
+
+  return (NULL);
+}
+
+VL_MSG_API_REAPER_FUNCTION (want_stats_reaper);
+
+
+/*
+ * Return a copy of the clients list.
+ */
 vpe_client_registration_t *
 get_clients_for_stat (u32 reg, u32 item)
 {
@@ -381,10 +436,13 @@ get_clients_for_stat (u32 reg, u32 item)
   registration = pool_elt_at_index (sm->stats_registrations[reg], p[0]);
 
   vec_reset_length (clients);
-  pool_foreach (client, registration->clients, (
-                                                {
-                                                vec_add1 (clients, *client);}
-               ));
+
+  /* *INDENT-OFF* */
+  pool_foreach (client, registration->clients,
+  ({
+    vec_add1 (clients, *client);}
+  ));
+  /* *INDENT-ON* */
   return clients;
 }
 
@@ -608,6 +666,7 @@ static void
          reg_prev = reg;
        }
     }
+  vec_free (clients);
 #if STATS_DEBUG > 0
   fformat (stdout, "%U\n", format_vnet_combined_counters, mp);
 #endif
@@ -848,18 +907,26 @@ do_combined_per_interface_counters (stats_main_t * sm)
          vp->sw_if_index = htonl (reg->item);
 
          im = &vnet_get_main ()->interface_main;
-         cm = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
-         vlib_get_combined_counter (cm, reg->item, &v);
-         clib_mem_unaligned (&vp->rx_packets, u64) =
-           clib_host_to_net_u64 (v.packets);
-         clib_mem_unaligned (&vp->rx_bytes, u64) =
-           clib_host_to_net_u64 (v.bytes);
-         cm = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX;
-         vlib_get_combined_counter (cm, reg->item, &v);
-         clib_mem_unaligned (&vp->tx_packets, u64) =
-           clib_host_to_net_u64 (v.packets);
-         clib_mem_unaligned (&vp->tx_bytes, u64) =
-           clib_host_to_net_u64 (v.bytes);
+
+#define _(X, x)                  \
+          cm = im->combined_sw_if_counters + X; \
+          vlib_get_combined_counter (cm, reg->item, &v); \
+          clib_mem_unaligned (&vp->x##_packets, u64) = \
+            clib_host_to_net_u64 (v.packets); \
+          clib_mem_unaligned (&vp->x##_bytes, u64) = \
+            clib_host_to_net_u64 (v.bytes);
+
+
+         _(VNET_INTERFACE_COUNTER_RX, rx);
+         _(VNET_INTERFACE_COUNTER_TX, tx);
+         _(VNET_INTERFACE_COUNTER_RX_UNICAST, rx_unicast);
+         _(VNET_INTERFACE_COUNTER_TX_UNICAST, tx_unicast);
+         _(VNET_INTERFACE_COUNTER_RX_MULTICAST, rx_multicast);
+         _(VNET_INTERFACE_COUNTER_TX_MULTICAST, tx_multicast);
+         _(VNET_INTERFACE_COUNTER_RX_BROADCAST, rx_broadcast);
+         _(VNET_INTERFACE_COUNTER_TX_BROADCAST, tx_broadcast);
+
+#undef _
 
          vl_api_send_msg (vl_reg, (u8 *) mp);
        }
@@ -2190,14 +2257,74 @@ stats_set_poller_delay (u32 poller_delay_sec)
     }
 }
 
+/*
+ * Accept connection on the socket and exchange the fd for the shared
+ * memory segment.
+ */
+static clib_error_t *
+stats_socket_accept_ready (clib_file_t * uf)
+{
+  stats_main_t *sm = &stats_main;
+  ssvm_private_t *ssvmp = &sm->stat_segment;
+  clib_error_t *err;
+  clib_socket_t client = { 0 };
+
+  err = clib_socket_accept (sm->socket, &client);
+  if (err)
+    {
+      clib_error_report (err);
+      return err;
+    }
+
+  /* Send the fd across and close */
+  err = clib_socket_sendmsg (&client, 0, 0, &ssvmp->fd, 1);
+  if (err)
+    clib_error_report (err);
+  clib_socket_close (&client);
+
+  return 0;
+}
+
+static void
+stats_segment_socket_init (void)
+{
+  stats_main_t *sm = &stats_main;
+  clib_error_t *error;
+  clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
+
+  s->config = (char *) sm->socket_name;
+  s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
+    CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
+  if ((error = clib_socket_init (s)))
+    {
+      clib_error_report (error);
+      return;
+    }
+
+  clib_file_t template = { 0 };
+  clib_file_main_t *fm = &file_main;
+  template.read_function = stats_socket_accept_ready;
+  template.file_descriptor = s->fd;
+  template.description =
+    format (0, "stats segment listener %s", STAT_SEGMENT_SOCKET_FILE);
+  clib_file_add (fm, &template);
+
+  sm->socket = s;
+}
+
 static clib_error_t *
 stats_config (vlib_main_t * vm, unformat_input_t * input)
 {
+  stats_main_t *sm = &stats_main;
   u32 sec;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "interval %u", &sec))
+      if (unformat (input, "socket-name %s", &sm->socket_name))
+       ;
+      else if (unformat (input, "default"))
+       sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
+      else if (unformat (input, "interval %u", &sec))
        {
          int rv = stats_set_poller_delay (sec);
          if (rv)
@@ -2206,7 +2333,6 @@ stats_config (vlib_main_t * vm, unformat_input_t * input)
                                        "`stats_set_poller_delay' API call failed, rv=%d:%U",
                                        (int) rv, format_vnet_api_errno, rv);
            }
-         return 0;
        }
       else
        {
@@ -2214,6 +2340,10 @@ stats_config (vlib_main_t * vm, unformat_input_t * input)
                                    format_unformat_error, input);
        }
     }
+
+  if (sm->socket_name)
+    stats_segment_socket_init ();
+
   return 0;
 }
 
@@ -2272,10 +2402,12 @@ stats_thread_fn (void *arg)
       ip46_fib_stats_delay (sm, sm->stats_poll_interval_in_seconds,
                            0 /* nsec */ );
 
+      /* Always update stats segment data */
+      do_stat_segment_updates (sm);
+
       if (!(sm->enable_poller))
-       {
-         continue;
-       }
+       continue;
+
       if (pool_elts
          (sm->stats_registrations[IDX_PER_INTERFACE_COMBINED_COUNTERS]))
        do_combined_per_interface_counters (sm);
@@ -2347,6 +2479,7 @@ static void
          continue;
        }
     }
+  vec_free (clients);
 
 #if STATS_DEBUG > 0
   fformat (stdout, "%U\n", format_vnet_simple_counters, mp);
@@ -2400,6 +2533,7 @@ vl_api_vnet_ip4_fib_counters_t_handler (vl_api_vnet_ip4_fib_counters_t * mp)
          continue;
        }
     }
+  vec_free (clients);
 
   if (reg_prev && vl_api_can_send_msg (reg_prev))
     {
@@ -2449,6 +2583,7 @@ vl_api_vnet_ip4_nbr_counters_t_handler (vl_api_vnet_ip4_nbr_counters_t * mp)
          continue;
        }
     }
+  vec_free (clients);
 
   /* *INDENT-ON* */
   if (reg_prev && vl_api_can_send_msg (reg_prev))
@@ -2499,6 +2634,8 @@ vl_api_vnet_ip6_fib_counters_t_handler (vl_api_vnet_ip6_fib_counters_t * mp)
          continue;
        }
     }
+  vec_free (clients);
+
   /* *INDENT-ON* */
   if (reg_prev && vl_api_can_send_msg (reg_prev))
     {
@@ -2548,6 +2685,8 @@ vl_api_vnet_ip6_nbr_counters_t_handler (vl_api_vnet_ip6_nbr_counters_t * mp)
          continue;
        }
     }
+  vec_free (clients);
+
   /* *INDENT-ON* */
   if (reg_prev && vl_api_can_send_msg (reg_prev))
     {