LISP: make statistics thread safe
[vpp.git] / src / vnet / lisp-cp / one_cli.c
index 0701070..109aca2 100644 (file)
@@ -38,9 +38,11 @@ lisp_show_adjacencies_command_fn (vlib_main_t * vm,
        {
          vlib_cli_output (vm, "parse error: '%U'",
                           format_unformat_error, line_input);
+         unformat_free (line_input);
          return 0;
        }
     }
+  unformat_free (line_input);
 
   if (~0 == vni)
     {
@@ -94,9 +96,11 @@ lisp_add_del_map_server_command_fn (vlib_main_t * vm,
        {
          vlib_cli_output (vm, "parse error: '%U'",
                           format_unformat_error, line_input);
+         unformat_free (line_input);
          return 0;
        }
     }
+  unformat_free (line_input);
 
   if (!ip_set)
     {
@@ -191,7 +195,7 @@ lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
   if (key && (0 == key_id))
     {
       vlib_cli_output (vm, "invalid key_id!");
-      return 0;
+      goto done;
     }
 
   gid_address_copy (&a->eid, &eid);
@@ -213,6 +217,7 @@ done:
     vec_free (locator_set_name);
   gid_address_free (&a->eid);
   vec_free (a->key);
+  unformat_free (line_input);
   return error;
 }
 
@@ -233,6 +238,7 @@ lisp_eid_table_map_command_fn (vlib_main_t * vm,
   u8 is_add = 1, is_l2 = 0;
   u32 vni = 0, dp_id = 0;
   unformat_input_t _line_input, *line_input = &_line_input;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -250,11 +256,16 @@ lisp_eid_table_map_command_fn (vlib_main_t * vm,
        is_l2 = 1;
       else
        {
-         return unformat_parse_error (line_input);
+         error = unformat_parse_error (line_input);
+         goto done;
        }
     }
   vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
-  return 0;
+
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -341,7 +352,7 @@ lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
        }
     }
 
-  if (!eid_set)
+  if (!del_all && !eid_set)
     {
       clib_warning ("missing eid!");
       goto done;
@@ -361,8 +372,6 @@ lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
       goto done;
     }
 
-  /* TODO build src/dst with seid */
-
   /* if it's a delete, clean forwarding */
   if (!is_add)
     {
@@ -482,7 +491,7 @@ lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
          != ip_prefix_version (leid_ippref)))
     {
       clib_warning ("remote and local EIDs are of different types!");
-      return error;
+      goto done;
     }
 
   memset (a, 0, sizeof (a[0]));
@@ -536,11 +545,14 @@ lisp_map_request_mode_command_fn (vlib_main_t * vm,
   if (_MR_MODE_MAX == mr_mode)
     {
       clib_warning ("No map request mode entered!");
-      return 0;
+      goto done;
     }
 
   vnet_lisp_set_map_request_mode (mr_mode);
+
 done:
+  unformat_free (i);
+
   return 0;
 }
 
@@ -633,7 +645,10 @@ lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
       else if (unformat (line_input, "disable"))
        is_add = 0;
       else
-       return clib_error_return (0, "parse error");
+       {
+         error = clib_error_return (0, "parse error");
+         goto done;
+       }
     }
 
   if (!locator_name_set)
@@ -651,6 +666,7 @@ lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
 done:
   if (locator_set_name)
     vec_free (locator_set_name);
+  unformat_free (line_input);
   return error;
 }
 
@@ -734,8 +750,9 @@ format_eid_entry (u8 * s, va_list * args)
 
   if (vec_len (ls->locator_indices) == 0)
     {
-      s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
-                 type, ttl, aut);
+      s = format (s, "%-35U%-20saction:%-30U%-20u%-u", format_gid_address,
+                 gid, type, format_negative_mapping_action, mapit->action,
+                 ttl, aut);
     }
   else
     {
@@ -774,6 +791,7 @@ lisp_show_eid_table_command_fn (vlib_main_t * vm,
   gid_address_t eid;
   u8 print_all = 1;
   u8 filter = 0;
+  clib_error_t *error = NULL;
 
   memset (&eid, 0, sizeof (eid));
 
@@ -790,8 +808,11 @@ lisp_show_eid_table_command_fn (vlib_main_t * vm,
       else if (unformat (line_input, "remote"))
        filter = 2;
       else
-       return clib_error_return (0, "parse error: '%U'",
-                                 format_unformat_error, line_input);
+       {
+         error = clib_error_return (0, "parse error: '%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
     }
 
   vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
@@ -802,6 +823,9 @@ lisp_show_eid_table_command_fn (vlib_main_t * vm,
       /* *INDENT-OFF* */
       pool_foreach (mapit, lcm->mapping_pool,
       ({
+        if (mapit->pitr_set)
+          continue;
+
         locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
                                                 mapit->locator_set_index);
         if (filter && !((1 == filter && ls->local) ||
@@ -818,7 +842,7 @@ lisp_show_eid_table_command_fn (vlib_main_t * vm,
     {
       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
       if ((u32) ~ 0 == mi)
-       return 0;
+       goto done;
 
       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
       locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
@@ -827,14 +851,17 @@ lisp_show_eid_table_command_fn (vlib_main_t * vm,
       if (filter && !((1 == filter && ls->local) ||
                      (2 == filter && !ls->local)))
        {
-         return 0;
+         goto done;
        }
 
       vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
                       lcm, mapit, ls);
     }
 
-  return 0;
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -853,6 +880,7 @@ lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
   unformat_input_t _line_input, *line_input = &_line_input;
   u8 is_enabled = 0;
   u8 is_set = 0;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -869,16 +897,24 @@ lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
        is_set = 1;
       else
        {
-         return clib_error_return (0, "parse error: '%U'",
-                                   format_unformat_error, line_input);
+         error = clib_error_return (0, "parse error: '%U'",
+                                    format_unformat_error, line_input);
+         goto done;
        }
     }
 
   if (!is_set)
-    return clib_error_return (0, "state not set");
+    {
+      error = clib_error_return (0, "state not set");
+      goto done;
+    }
 
   vnet_lisp_enable_disable (is_enabled);
-  return 0;
+
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -897,6 +933,7 @@ lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
   unformat_input_t _line_input, *line_input = &_line_input;
   u8 is_enabled = 0;
   u8 is_set = 0;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -915,18 +952,22 @@ lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
        {
          vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
                           line_input);
-         return 0;
+         goto done;
        }
     }
 
   if (!is_set)
     {
       vlib_cli_output (vm, "state not set!");
-      return 0;
+      goto done;
     }
 
   vnet_lisp_map_register_enable_disable (is_enabled);
-  return 0;
+
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -945,6 +986,7 @@ lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
   unformat_input_t _line_input, *line_input = &_line_input;
   u8 is_enabled = 0;
   u8 is_set = 0;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -963,18 +1005,22 @@ lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
        {
          vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
                           line_input);
-         return 0;
+         goto done;
        }
     }
 
   if (!is_set)
     {
       vlib_cli_output (vm, "state not set!");
-      return 0;
+      goto done;
     }
 
   vnet_lisp_rloc_probe_enable_disable (is_enabled);
-  return 0;
+
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -1022,6 +1068,7 @@ lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
   uword *vni_table = 0;
   u8 is_l2 = 0;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -1040,14 +1087,17 @@ lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
          is_l2 = 0;
        }
       else
-       return clib_error_return (0, "parse error: '%U'",
-                                 format_unformat_error, line_input);
+       {
+         error = clib_error_return (0, "parse error: '%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
     }
 
   if (!vni_table)
     {
       vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
-      return 0;
+      goto done;
     }
 
   vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
@@ -1059,7 +1109,10 @@ lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
   }));
   /* *INDENT-ON* */
 
-  return 0;
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -1131,6 +1184,7 @@ done:
   vec_free (locators);
   if (locator_set_name)
     vec_free (locator_set_name);
+  unformat_free (line_input);
   return error;
 }
 
@@ -1205,6 +1259,7 @@ lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
 done:
   vec_free (locators);
   vec_free (locator_set_name);
+  unformat_free (line_input);
   return error;
 }
 
@@ -1322,6 +1377,7 @@ lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
     }
 
 done:
+  unformat_free (line_input);
   return error;
 }
 
@@ -1372,9 +1428,9 @@ lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
                                 is_add ? "add" : "delete");
     }
 
-  vec_free (locator_set_name);
-
 done:
+  vec_free (locator_set_name);
+  unformat_free (line_input);
   return error;
 
 }
@@ -1438,7 +1494,10 @@ lisp_use_petr_set_locator_set_command_fn (vlib_main_t * vm,
       else if (unformat (line_input, "disable"))
        is_add = 0;
       else
-       return clib_error_return (0, "parse error");
+       {
+         error = clib_error_return (0, "parse error");
+         goto done;
+       }
     }
 
   if (!ip_set)
@@ -1454,6 +1513,7 @@ lisp_use_petr_set_locator_set_command_fn (vlib_main_t * vm,
     }
 
 done:
+  unformat_free (line_input);
   return error;
 }
 
@@ -1584,6 +1644,113 @@ VLIB_CLI_COMMAND (one_show_rloc_probe_state_command) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+lisp_show_stats_command_fn (vlib_main_t * vm,
+                           unformat_input_t * input,
+                           vlib_cli_command_t * cmd)
+{
+  u8 is_enabled = vnet_lisp_stats_enable_disable_state ();
+  vlib_cli_output (vm, "%s\n", is_enabled ? "enabled" : "disabled");
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (one_show_stats_command) = {
+    .path = "show one statistics status",
+    .short_help = "show ONE statistics enable/disable status",
+    .function = lisp_show_stats_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+lisp_show_stats_details_command_fn (vlib_main_t * vm,
+                                   unformat_input_t * input,
+                                   vlib_cli_command_t * cmd)
+{
+  lisp_api_stats_t *stat, *stats = vnet_lisp_get_stats ();
+
+  if (vec_len (stats) > 0)
+    vlib_cli_output (vm,
+                    "[src-EID, dst-EID] [loc-rloc, rmt-rloc] count bytes\n");
+  else
+    vlib_cli_output (vm, "No statistics found.\n");
+
+  vec_foreach (stat, stats)
+  {
+    vlib_cli_output (vm, "[%U, %U] [%U, %U] %7u %7u\n",
+                    format_fid_address, &stat->seid,
+                    format_fid_address, &stat->deid,
+                    format_ip_address, &stat->loc_rloc,
+                    format_ip_address, &stat->rmt_rloc,
+                    stat->counters.packets, stat->counters.bytes);
+  }
+  vec_free (stats);
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (one_show_stats_details_command) = {
+    .path = "show one statistics details",
+    .short_help = "show ONE statistics",
+    .function = lisp_show_stats_details_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+lisp_stats_enable_disable_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 enable = 0;
+
+  /* Get a line of input. */
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (line_input, "enable"))
+       enable = 1;
+      else if (unformat (line_input, "disable"))
+       enable = 0;
+      else
+       {
+         clib_warning ("Error: expected enable/disable!");
+         goto done;
+       }
+    }
+  vnet_lisp_stats_enable_disable (enable);
+done:
+  unformat_free (line_input);
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (one_stats_enable_disable_command) = {
+    .path = "one statistics",
+    .short_help = "enable/disable ONE statistics collecting",
+    .function = lisp_stats_enable_disable_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+lisp_stats_flush_command_fn (vlib_main_t * vm,
+                            unformat_input_t * input,
+                            vlib_cli_command_t * cmd)
+{
+  vnet_lisp_flush_stats ();
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (one_stats_flush_command) = {
+    .path = "one statistics flush",
+    .short_help = "Flush ONE statistics",
+    .function = lisp_stats_flush_command_fn,
+};
+/* *INDENT-ON* */
+
 /*
  * fd.io coding-style-patch-verification: ON
  *