api: Implement log_dump/log_details
[vpp.git] / src / vpp / api / api.c
index 70b1042..66857fb 100644 (file)
@@ -2,7 +2,7 @@
  *------------------------------------------------------------------
  * api.c - message handler registration
  *
- * Copyright (c) 2010-2016 Cisco and/or its affiliates.
+ * Copyright (c) 2010-2018 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
@@ -48,6 +48,7 @@
 #include <vnet/api_errno.h>
 #include <vnet/vnet.h>
 
+#include <vlib/log.h>
 #include <vlib/vlib.h>
 #include <vlib/unix/unix.h>
 #include <vlibapi/api.h>
 #undef BIHASH_TYPE
 #undef __included_bihash_template_h__
 
-#include <vpp/stats/stats.h>
+#include <vnet/ip/format.h>
 
 #include <vpp/api/vpe_msg_enum.h>
+#include <vpp/api/types.h>
 
 #define vl_typedefs            /* define message structures */
 #include <vpp/api/vpe_all_api_h.h>
 #define foreach_vpe_api_msg                                             \
 _(CONTROL_PING, control_ping)                                           \
 _(CLI, cli)                                                             \
-_(CLI_INBAND, cli_inband)                                              \
+_(CLI_INBAND, cli_inband)                                                                      \
 _(GET_NODE_INDEX, get_node_index)                                       \
-_(ADD_NODE_NEXT, add_node_next)                                                \
-_(SHOW_VERSION, show_version)                                          \
+_(ADD_NODE_NEXT, add_node_next)                                                                    \
+_(SHOW_VERSION, show_version)                                                              \
+_(SHOW_THREADS, show_threads)                                                              \
 _(GET_NODE_GRAPH, get_node_graph)                                       \
 _(GET_NEXT_INDEX, get_next_index)                                       \
+_(LOG_DUMP, log_dump)                                                   \
+_(SHOW_VPE_SYSTEM_TIME_TICKS, show_vpe_system_time_ticks)
 
 #define QUOTE_(x) #x
 #define QUOTE(x) QUOTE_(x)
@@ -101,9 +106,6 @@ memclnt_delete_callback (u32 client_index)
   vpe_api_main_t *vam = &vpe_api_main;
   vpe_client_registration_t *rp;
   uword *p;
-  int stats_memclnt_delete_callback (u32 client_index);
-
-  stats_memclnt_delete_callback (client_index);
 
 #define _(a)                                                    \
     p = hash_get (vam->a##_registration_hash, client_index);    \
@@ -164,16 +166,16 @@ static void
 vl_api_cli_t_handler (vl_api_cli_t * mp)
 {
   vl_api_cli_reply_t *rp;
-  svm_queue_t *q;
+  vl_api_registration_t *reg;
   vlib_main_t *vm = vlib_get_main ();
   api_main_t *am = &api_main;
   unformat_input_t input;
   u8 *shmem_vec = 0;
   void *oldheap;
 
-  q = vl_api_client_index_to_input_queue (mp->client_index);
-  if (!q)
-    return;
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;;
 
   rp = vl_msg_api_alloc (sizeof (*rp));
   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
@@ -193,7 +195,7 @@ vl_api_cli_t_handler (vl_api_cli_t * mp)
 
   rp->reply_in_shmem = (uword) shmem_vec;
 
-  vl_msg_api_send_shmem (q, (u8 *) & rp);
+  vl_api_send_msg (reg, (u8 *) rp);
 }
 
 static void
@@ -216,16 +218,26 @@ vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
   vlib_main_t *vm = vlib_get_main ();
   unformat_input_t input;
   u8 *out_vec = 0;
+  u32 len = 0;
+
+  if (vl_msg_api_get_msg_length (mp) <
+      vl_api_string_len (&mp->cmd) + sizeof (*mp))
+    {
+      rv = -1;
+      goto error;
+    }
+
+  unformat_init_string (&input, (char *) vl_api_from_api_string (&mp->cmd),
+                       vl_api_string_len (&mp->cmd));
+  rv = vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
 
-  unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
-  vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
+  len = vec_len (out_vec);
 
-  u32 len = vec_len (out_vec);
+error:
   /* *INDENT-OFF* */
   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
   ({
-    rmp->length = htonl (len);
-    clib_memcpy (rmp->reply, out_vec, len);
+    vl_api_to_api_string(len, (const char *)out_vec, &rmp->reply);
   }));
   /* *INDENT-ON* */
   vec_free (out_vec);
@@ -240,18 +252,85 @@ vl_api_show_version_t_handler (vl_api_show_version_t * mp)
   char *vpe_api_get_version (void);
   char *vpe_api_get_build_date (void);
 
+  u32 program_len = strnlen_s ("vpe", 32);
+  u32 version_len = strnlen_s (vpe_api_get_version (), 32);
+  u32 build_date_len = strnlen_s (vpe_api_get_build_date (), 32);
+  u32 build_directory_len = strnlen_s (vpe_api_get_build_directory (), 256);
+
+  u32 n = program_len + version_len + build_date_len + build_directory_len;
+
+  /* *INDENT-OFF* */
+  REPLY_MACRO3(VL_API_SHOW_VERSION_REPLY, n,
+  ({
+    char *p = (char *)&rmp->program;
+    p += vl_api_to_api_string(program_len, "vpe", (vl_api_string_t *)p);
+    p += vl_api_to_api_string(version_len, vpe_api_get_version(), (vl_api_string_t *)p);
+    p += vl_api_to_api_string(build_date_len, vpe_api_get_build_date(), (vl_api_string_t *)p);
+    vl_api_to_api_string(build_directory_len, vpe_api_get_build_directory(), (vl_api_string_t *)p);
+  }));
+  /* *INDENT-ON* */
+}
+
+static void
+get_thread_data (vl_api_thread_data_t * td, int index)
+{
+  vlib_worker_thread_t *w = vlib_worker_threads + index;
+  td->id = htonl (index);
+  if (w->name)
+    strncpy ((char *) td->name, (char *) w->name, ARRAY_LEN (td->name) - 1);
+  if (w->registration)
+    strncpy ((char *) td->type, (char *) w->registration->name,
+            ARRAY_LEN (td->type) - 1);
+  td->pid = htonl (w->lwp);
+  td->cpu_id = htonl (w->cpu_id);
+  td->core = htonl (w->core_id);
+  td->cpu_socket = htonl (w->socket_id);
+}
+
+static void
+vl_api_show_threads_t_handler (vl_api_show_threads_t * mp)
+{
+  int count = 0;
+
+#if !defined(__powerpc64__)
+  vl_api_registration_t *reg;
+  vl_api_show_threads_reply_t *rmp;
+  vl_api_thread_data_t *td;
+  int i, msg_size = 0;
+  count = vec_len (vlib_worker_threads);
+  if (!count)
+    return;
+
+  msg_size = sizeof (*rmp) + sizeof (rmp->thread_data[0]) * count;
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  rmp = vl_msg_api_alloc (msg_size);
+  clib_memset (rmp, 0, msg_size);
+  rmp->_vl_msg_id = htons (VL_API_SHOW_THREADS_REPLY);
+  rmp->context = mp->context;
+  rmp->count = htonl (count);
+  td = rmp->thread_data;
+
+  for (i = 0; i < count; i++)
+    {
+      get_thread_data (&td[i], i);
+    }
+
+  vl_api_send_msg (reg, (u8 *) rmp);
+#else
+
+  /* unimplemented support */
+  rv = -9;
+  clib_warning ("power pc does not support show threads api");
   /* *INDENT-OFF* */
-  REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
+  REPLY_MACRO2(VL_API_SHOW_THREADS_REPLY,
   ({
-    strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
-    strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
-             ARRAY_LEN(rmp->build_directory)-1);
-    strncpy ((char *) rmp->version, vpe_api_get_version(),
-             ARRAY_LEN(rmp->version)-1);
-    strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
-             ARRAY_LEN(rmp->build_date)-1);
+    rmp->count = htonl(count);
   }));
   /* *INDENT-ON* */
+#endif
 }
 
 static void
@@ -367,7 +446,8 @@ vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
   vlib_main_t *vm = vlib_get_main ();
   void *oldheap;
   vl_api_get_node_graph_reply_t *rmp;
-  vlib_node_t ***node_dups;
+  static vlib_node_t ***node_dups;
+  static vlib_main_t **stat_vms;
 
   pthread_mutex_lock (&am->vlib_rp->mutex);
   oldheap = svm_push_data_heap (am->vlib_rp);
@@ -378,9 +458,10 @@ vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
   vec_validate (vector, 16384);
   vec_reset_length (vector);
 
-  /* $$$$ FIXME */
-  node_dups = vlib_node_get_nodes (vm, (u32) ~ 0 /* all threads */ ,
-                                  1 /* include stats */ );
+  vlib_node_get_nodes (vm, 0 /* main threads */ ,
+                      0 /* include stats */ ,
+                      1 /* barrier sync */ ,
+                      &node_dups, &stat_vms);
   vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */ ,
                                1 /* include stats */ );
 
@@ -395,6 +476,85 @@ vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
   /* *INDENT-ON* */
 }
 
+static void
+show_log_details (vl_api_registration_t * reg, u32 context,
+                 f64 timestamp_ticks, u8 * timestamp,
+                 vl_api_log_level_t * level, u8 * msg_class, u8 * message)
+{
+  u32 msg_size;
+
+  vl_api_log_details_t *rmp;
+  msg_size =
+    sizeof (*rmp) + vec_len (timestamp) + vec_len (msg_class) +
+    vec_len (message);
+
+  rmp = vl_msg_api_alloc (msg_size);
+  clib_memset (rmp, 0, msg_size);
+  rmp->_vl_msg_id = ntohs (VL_API_LOG_DETAILS);
+
+  rmp->context = context;
+  rmp->timestamp_ticks = clib_host_to_net_f64 (timestamp_ticks);
+  rmp->level = htonl (*level);
+  char *p = (char *) &rmp->timestamp;
+
+  p += vl_api_vec_to_api_string (timestamp, (vl_api_string_t *) p);
+  p += vl_api_vec_to_api_string (msg_class, (vl_api_string_t *) p);
+  p += vl_api_vec_to_api_string (message, (vl_api_string_t *) p);
+
+  vl_api_send_msg (reg, (u8 *) rmp);
+}
+
+static void
+vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
+{
+
+  /* from log.c */
+  vlib_log_main_t *lm = &log_main;
+  vlib_log_entry_t *e;
+  int i = last_log_entry ();
+  int count = lm->count;
+  f64 time_offset, start_time;
+  vl_api_registration_t *reg;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (reg == 0)
+    return;
+
+  start_time = clib_net_to_host_f64 (mp->start_timestamp);
+
+  time_offset = (f64) lm->time_zero_timeval.tv_sec
+    + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
+
+  while (count--)
+    {
+      e = vec_elt_at_index (lm->entries, i);
+      if (start_time <= e->timestamp + time_offset)
+       show_log_details (reg, mp->context, e->timestamp + time_offset,
+                         format (0, "%U", format_time_float, 0,
+                                 e->timestamp + time_offset),
+                         (vl_api_log_level_t *) & e->level,
+                         format (0, "%U", format_vlib_log_class, e->class),
+                         e->string);
+      i = (i + 1) % lm->size;
+    }
+
+}
+
+static void
+  vl_api_show_vpe_system_time_ticks_t_handler
+  (vl_api_show_vpe_system_time_ticks_t * mp)
+{
+  int rv = 0;
+  vl_api_show_vpe_system_time_ticks_reply_t *rmp;
+  /* *INDENT-OFF* */
+  REPLY_MACRO2(VL_API_SHOW_VPE_SYSTEM_TIME_TICKS_REPLY,
+  ({
+    rmp->vpe_system_time_ticks = clib_host_to_net_f64 (unix_time_now ());
+  }));
+  /* *INDENT-ON* */
+}
+
+
 #define BOUNCE_HANDLER(nn)                                              \
 static void vl_api_##nn##_t_handler (                                   \
     vl_api_##nn##_t *mp)                                                \
@@ -430,7 +590,7 @@ static void setup_message_id_table (api_main_t * am);
 /*
  * vpe_api_hookup
  * Add vpe's API message handlers to the table.
- * vlib has alread mapped shared memory and
+ * vlib has already mapped shared memory and
  * added the client registration handlers.
  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
  */
@@ -453,13 +613,15 @@ vpe_api_hookup (vlib_main_t * vm)
    * Trace space for classifier mask+match
    */
   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
-  am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
-    += 5 * sizeof (u32x4);
+  am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size +=
+    5 * sizeof (u32x4);
 
   /*
    * Thread-safe API messages
    */
-  am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
+  am->is_mp_safe[VL_API_CONTROL_PING] = 1;
+  am->is_mp_safe[VL_API_CONTROL_PING_REPLY] = 1;
+  am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
 
   /*
@@ -472,7 +634,7 @@ vpe_api_hookup (vlib_main_t * vm)
 
 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
 
-static clib_error_t *
+clib_error_t *
 vpe_api_init (vlib_main_t * vm)
 {
   vpe_api_main_t *am = &vpe_api_main;
@@ -490,9 +652,6 @@ vpe_api_init (vlib_main_t * vm)
   return 0;
 }
 
-VLIB_INIT_FUNCTION (vpe_api_init);
-
-
 static clib_error_t *
 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
 {
@@ -576,7 +735,8 @@ api_segment_config (vlib_main_t * vm, unformat_input_t * input)
          /* lookup the group name */
          grp = NULL;
          while (((rv =
-                  getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
+                  getgrnam_r (s, &_grp, buf, vec_len (buf),
+                              &grp)) == ERANGE)
                 && (vec_len (buf) <= max_buf_size))
            {
              vec_resize (buf, vec_len (buf) * 2);
@@ -623,7 +783,7 @@ format_arp_event (u8 * s, va_list * args)
   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
 
   s = format (s, "pid %d: ", ntohl (event->pid));
-  s = format (s, "resolution for %U", format_ip4_address, &event->address);
+  s = format (s, "resolution for %U", format_vl_api_ip4_address, event->ip);
   return s;
 }
 
@@ -633,7 +793,7 @@ format_nd_event (u8 * s, va_list * args)
   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
 
   s = format (s, "pid %d: ", ntohl (event->pid));
-  s = format (s, "resolution for %U", format_ip6_address, event->address);
+  s = format (s, "resolution for %U", format_vl_api_ip6_address, event->ip);
   return s;
 }