c11 safe string handling support
[vpp.git] / src / vnet / policer / policer.c
index 290a6af..9611fe0 100644 (file)
@@ -16,6 +16,8 @@
 #include <vnet/policer/policer.h>
 #include <vnet/classify/vnet_classify.h>
 
+vnet_policer_main_t vnet_policer_main;
+
 clib_error_t *
 policer_add_del (vlib_main_t * vm,
                 u8 * name,
@@ -33,13 +35,26 @@ policer_add_del (vlib_main_t * vm,
 
   if (is_add == 0)
     {
+      /* free policer config and template */
       if (p == 0)
        {
          vec_free (name);
          return clib_error_return (0, "No such policer configuration");
        }
+      pool_put_index (pm->configs, p[0]);
+      pool_put_index (pm->policer_templates, p[0]);
       hash_unset_mem (pm->policer_config_by_name, name);
+
+      /* free policer */
+      p = hash_get_mem (pm->policer_index_by_name, name);
+      if (p == 0)
+       {
+         vec_free (name);
+         return clib_error_return (0, "No such policer");
+       }
+      pool_put_index (pm->policers, p[0]);
       hash_unset_mem (pm->policer_index_by_name, name);
+
       vec_free (name);
       return 0;
     }
@@ -413,12 +428,13 @@ configure_policer_command_fn (vlib_main_t * vm,
   u8 is_add = 1;
   u8 *name = 0;
   u32 pi;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
     return 0;
 
-  memset (&c, 0, sizeof (c));
+  clib_memset (&c, 0, sizeof (c));
 
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
@@ -433,13 +449,19 @@ configure_policer_command_fn (vlib_main_t * vm,
       foreach_config_param
 #undef _
        else
-       return clib_error_return (0, "unknown input `%U'",
-                                 format_unformat_error, line_input);
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
     }
 
+  error = policer_add_del (vm, name, &c, &pi, is_add);
+
+done:
   unformat_free (line_input);
 
-  return policer_add_del (vm, name, &c, &pi, is_add);
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -493,6 +515,27 @@ VLIB_CLI_COMMAND (show_policer_command, static) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+show_policer_pools_command_fn (vlib_main_t * vm,
+                              unformat_input_t * input,
+                              vlib_cli_command_t * cmd)
+{
+  vnet_policer_main_t *pm = &vnet_policer_main;
+
+  vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
+                  pool_elts (pm->configs),
+                  pool_elts (pm->policer_templates),
+                  pool_elts (pm->policers));
+  return 0;
+}
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
+    .path = "show policer pools",
+    .short_help = "show policer pools",
+    .function = show_policer_pools_command_fn,
+};
+/* *INDENT-ON* */
+
 clib_error_t *
 policer_init (vlib_main_t * vm)
 {