api: improve api string safety
[vpp.git] / src / vpp / api / api.c
index 66857fb..01527a5 100644 (file)
@@ -86,7 +86,9 @@ _(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)
+_(SHOW_VPE_SYSTEM_TIME, show_vpe_system_time)                          \
+_(GET_F64_ENDIAN_VALUE, get_f64_endian_value)                                                  \
+_(GET_F64_INCREMENT_BY_ONE, get_f64_increment_by_one)                                  \
 
 #define QUOTE_(x) #x
 #define QUOTE(x) QUOTE_(x)
@@ -141,22 +143,19 @@ shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
   u8 **shmem_vecp = (u8 **) arg;
   u8 *shmem_vec;
   void *oldheap;
-  api_main_t *am = &api_main;
   u32 offset;
 
   shmem_vec = *shmem_vecp;
 
   offset = vec_len (shmem_vec);
 
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
 
   vec_validate (shmem_vec, offset + buffer_bytes - 1);
 
   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
 
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   *shmem_vecp = shmem_vec;
 }
@@ -168,7 +167,6 @@ vl_api_cli_t_handler (vl_api_cli_t * mp)
   vl_api_cli_reply_t *rp;
   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;
@@ -185,13 +183,9 @@ vl_api_cli_t_handler (vl_api_cli_t * mp)
 
   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
 
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
-
+  oldheap = vl_msg_push_heap ();
   vec_add1 (shmem_vec, 0);
-
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   rp->reply_in_shmem = (uword) shmem_vec;
 
@@ -218,7 +212,7 @@ 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;
+  u8 *cmd_vec = 0;
 
   if (vl_msg_api_get_msg_length (mp) <
       vl_api_string_len (&mp->cmd) + sizeof (*mp))
@@ -227,20 +221,21 @@ vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
       goto error;
     }
 
-  unformat_init_string (&input, (char *) vl_api_from_api_string (&mp->cmd),
+  cmd_vec = vl_api_from_api_to_new_vec (&mp->cmd);
+
+  unformat_init_string (&input, (char *) cmd_vec,
                        vl_api_string_len (&mp->cmd));
   rv = vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
 
-  len = vec_len (out_vec);
-
 error:
   /* *INDENT-OFF* */
-  REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
+  REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, vec_len (out_vec),
   ({
-    vl_api_to_api_string(len, (const char *)out_vec, &rmp->reply);
+    vl_api_vec_to_api_string(out_vec, &rmp->reply);
   }));
   /* *INDENT-ON* */
   vec_free (out_vec);
+  vec_free (cmd_vec);
 }
 
 static void
@@ -252,21 +247,16 @@ 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,
+  REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
   ({
-    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);
+    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);
   }));
   /* *INDENT-ON* */
 }
@@ -284,7 +274,7 @@ get_thread_data (vl_api_thread_data_t * td, int index)
   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);
+  td->cpu_socket = htonl (w->numa_id);
 }
 
 static void
@@ -442,15 +432,13 @@ vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
 {
   int rv = 0;
   u8 *vector = 0;
-  api_main_t *am = &api_main;
   vlib_main_t *vm = vlib_get_main ();
   void *oldheap;
   vl_api_get_node_graph_reply_t *rmp;
   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);
+  oldheap = vl_msg_push_heap ();
 
   /*
    * Keep the number of memcpy ops to a minimum (e.g. 1).
@@ -465,8 +453,7 @@ vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
   vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */ ,
                                1 /* include stats */ );
 
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   /* *INDENT-OFF* */
   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
@@ -478,28 +465,31 @@ vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
 
 static void
 show_log_details (vl_api_registration_t * reg, u32 context,
-                 f64 timestamp_ticks, u8 * timestamp,
+                 f64 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);
+  int class_len =
+    clib_min (vec_len (msg_class) + 1, ARRAY_LEN (rmp->msg_class));
+  int message_len =
+    clib_min (vec_len (message) + 1, ARRAY_LEN (rmp->message));
+  msg_size = sizeof (*rmp) + class_len + message_len;
 
   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->timestamp = clib_host_to_net_f64 (timestamp);
   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);
+  memcpy (rmp->msg_class, msg_class, class_len - 1);
+  memcpy (rmp->message, message, message_len - 1);
+  /* enforced by memset() above */
+  ASSERT (0 == rmp->msg_class[class_len - 1]);
+  ASSERT (0 == rmp->message[message_len - 1]);
 
   vl_api_send_msg (reg, (u8 *) rmp);
 }
@@ -530,8 +520,6 @@ vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
       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);
@@ -541,19 +529,49 @@ vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
 }
 
 static void
-  vl_api_show_vpe_system_time_ticks_t_handler
-  (vl_api_show_vpe_system_time_ticks_t * mp)
+vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t * mp)
 {
   int rv = 0;
-  vl_api_show_vpe_system_time_ticks_reply_t *rmp;
+  vl_api_show_vpe_system_time_reply_t *rmp;
   /* *INDENT-OFF* */
-  REPLY_MACRO2(VL_API_SHOW_VPE_SYSTEM_TIME_TICKS_REPLY,
+  REPLY_MACRO2(VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
   ({
-    rmp->vpe_system_time_ticks = clib_host_to_net_f64 (unix_time_now ());
+    rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ());
   }));
   /* *INDENT-ON* */
 }
 
+static void
+vl_api_get_f64_endian_value_t_handler (vl_api_get_f64_endian_value_t * mp)
+{
+  int rv = 0;
+  f64 one = 1.0;
+  vl_api_get_f64_endian_value_reply_t *rmp;
+  if (1.0 != clib_net_to_host_f64 (mp->f64_one))
+    rv = VNET_API_ERROR_API_ENDIAN_FAILED;
+
+  /* *INDENT-OFF* */
+  REPLY_MACRO2(VL_API_GET_F64_ENDIAN_VALUE_REPLY,
+  ({
+    rmp->f64_one_result = clib_host_to_net_f64 (one);
+  }));
+  /* *INDENT-ON* */
+}
+
+static void
+vl_api_get_f64_increment_by_one_t_handler (vl_api_get_f64_increment_by_one_t *
+                                          mp)
+{
+  int rv = 0;
+  vl_api_get_f64_increment_by_one_reply_t *rmp;
+
+  /* *INDENT-OFF* */
+  REPLY_MACRO2(VL_API_GET_F64_INCREMENT_BY_ONE_REPLY,
+  ({
+    rmp->f64_value = clib_host_to_net_f64 (clib_net_to_host_f64(mp->f64_value) + 1.0);
+  }));
+  /* *INDENT-ON* */
+}
 
 #define BOUNCE_HANDLER(nn)                                              \
 static void vl_api_##nn##_t_handler (                                   \
@@ -597,7 +615,7 @@ static void setup_message_id_table (api_main_t * am);
 static clib_error_t *
 vpe_api_hookup (vlib_main_t * vm)
 {
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
 
 #define _(N,n)                                                  \
     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
@@ -777,78 +795,6 @@ get_unformat_vnet_sw_interface (void)
   return (void *) &unformat_vnet_sw_interface;
 }
 
-static u8 *
-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_vl_api_ip4_address, event->ip);
-  return s;
-}
-
-static u8 *
-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_vl_api_ip6_address, event->ip);
-  return s;
-}
-
-static clib_error_t *
-show_ip_arp_nd_events_fn (vlib_main_t * vm,
-                         unformat_input_t * input, vlib_cli_command_t * cmd)
-{
-  vpe_api_main_t *am = &vpe_api_main;
-  vl_api_ip4_arp_event_t *arp_event;
-  vl_api_ip6_nd_event_t *nd_event;
-
-  if (pool_elts (am->arp_events) == 0 && pool_elts (am->nd_events) == 0 &&
-      pool_elts (am->wc_ip4_arp_events_registrations) == 0 &&
-      pool_elts (am->wc_ip6_nd_events_registrations) == 0)
-    {
-      vlib_cli_output (vm, "No active arp or nd event registrations");
-      return 0;
-    }
-
-  /* *INDENT-OFF* */
-  pool_foreach (arp_event, am->arp_events,
-  ({
-    vlib_cli_output (vm, "%U", format_arp_event, arp_event);
-  }));
-
-  vpe_client_registration_t *reg;
-  pool_foreach(reg, am->wc_ip4_arp_events_registrations,
-  ({
-    vlib_cli_output (vm, "pid %d: bd mac/ip4 binding events",
-                     ntohl (reg->client_pid));
-  }));
-
-  pool_foreach (nd_event, am->nd_events,
-  ({
-    vlib_cli_output (vm, "%U", format_nd_event, nd_event);
-  }));
-
-  pool_foreach(reg, am->wc_ip6_nd_events_registrations,
-  ({
-    vlib_cli_output (vm, "pid %d: bd mac/ip6 binding events",
-                     ntohl (reg->client_pid));
-  }));
-  /* *INDENT-ON* */
-
-  return 0;
-}
-
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
-  .path = "show arp-nd-event registrations",
-  .function = show_ip_arp_nd_events_fn,
-  .short_help = "Show ip4 arp and ip6 nd event registrations",
-};
-/* *INDENT-ON* */
-
 #define vl_msg_name_crc_list
 #include <vpp/api/vpe_all_api_h.h>
 #undef vl_msg_name_crc_list