nat: fixing cfg file parsing cli issues
[vpp.git] / src / plugins / nat / nat66 / nat66_cli.c
index da96387..0ca4087 100644 (file)
 #include <nat/nat66/nat66.h>
 #include <vnet/fib/fib_table.h>
 
+#define NAT66_EXPECTED_ARGUMENT "expected required argument(s)"
+#define NAT66_PLUGIN_DISABLED  "error plugin disabled"
+
+#define CHECK_ENABLED()                                                       \
+  do                                                                          \
+    {                                                                         \
+      if (PREDICT_FALSE (!nat66_main.enabled))                                \
+       {                                                                     \
+         return clib_error_return (0, NAT66_PLUGIN_DISABLED);                \
+       }                                                                     \
+    }                                                                         \
+  while (0)
+
+static clib_error_t *
+nat66_enable_disable_command_fn (vlib_main_t *vm, unformat_input_t *input,
+                                vlib_cli_command_t *cmd)
+{
+  nat66_main_t *nm = &nat66_main;
+  unformat_input_t _line_input, *line_input = &_line_input;
+  clib_error_t *error = 0;
+
+  u32 outside_vrf = 0;
+  u8 enable_set = 0, enable = 0;
+
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return clib_error_return (0, NAT66_EXPECTED_ARGUMENT);
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (line_input, "outside-vrf %u", &outside_vrf))
+       ;
+      else if (!enable_set)
+       {
+         enable_set = 1;
+         if (unformat (line_input, "disable"))
+           ;
+         else if (unformat (line_input, "enable"))
+           enable = 1;
+       }
+      else
+       {
+         error = clib_error_return (0, "unknown input '%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
+    }
+
+  if (!enable_set)
+    {
+      error = clib_error_return (0, "expected enable | disable");
+      goto done;
+    }
+
+  if (enable)
+    {
+      if (nm->enabled)
+       {
+         error = clib_error_return (0, "already enabled");
+         goto done;
+       }
+
+      if (nat66_plugin_enable (outside_vrf) != 0)
+       error = clib_error_return (0, "enable failed");
+    }
+  else
+    {
+      if (!nm->enabled)
+       {
+         error = clib_error_return (0, "already disabled");
+         goto done;
+       }
+
+      if (nat66_plugin_disable () != 0)
+       error = clib_error_return (0, "disable failed");
+    }
+
+done:
+  unformat_free (line_input);
+  return error;
+}
+
 static clib_error_t *
 nat66_interface_feature_command_fn (vlib_main_t * vm,
                                    unformat_input_t * input,
@@ -34,9 +115,10 @@ nat66_interface_feature_command_fn (vlib_main_t * vm,
   u8 is_add = 1;
   int i, rv;
 
-  /* Get a line of input. */
+  CHECK_ENABLED ();
+
   if (!unformat_user (input, unformat_line_input, line_input))
-    return 0;
+    return clib_error_return (0, NAT66_EXPECTED_ARGUMENT);
 
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
@@ -139,7 +221,6 @@ nat66_cli_interface_walk (nat66_interface_t * i, void *ctx)
 {
   vlib_main_t *vm = ctx;
   vnet_main_t *vnm = vnet_get_main ();
-
   vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
                   vnet_get_sw_interface (vnm, i->sw_if_index),
                   nat66_interface_is_inside (i) ? "in" : "out");
@@ -150,9 +231,9 @@ static clib_error_t *
 nat66_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
                                  vlib_cli_command_t * cmd)
 {
+  CHECK_ENABLED ();
   vlib_cli_output (vm, "NAT66 interfaces:");
   nat66_interfaces_walk (nat66_cli_interface_walk, vm);
-
   return 0;
 }
 
@@ -163,14 +244,15 @@ nat66_add_del_static_mapping_command_fn (vlib_main_t * vm,
 {
   unformat_input_t _line_input, *line_input = &_line_input;
   clib_error_t *error = 0;
-  u8 is_add = 1;
   ip6_address_t l_addr, e_addr;
   u32 vrf_id = 0;
+  u8 is_add = 1;
   int rv;
 
-  /* Get a line of input. */
+  CHECK_ENABLED ();
+
   if (!unformat_user (input, unformat_line_input, line_input))
-    return 0;
+    return clib_error_return (0, NAT66_EXPECTED_ARGUMENT);
 
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
@@ -238,12 +320,29 @@ nat66_show_static_mappings_command_fn (vlib_main_t * vm,
                                       unformat_input_t * input,
                                       vlib_cli_command_t * cmd)
 {
+  CHECK_ENABLED ();
   vlib_cli_output (vm, "NAT66 static mappings:");
   nat66_static_mappings_walk (nat66_cli_static_mapping_walk, vm);
   return 0;
 }
 
-/* *INDENT-OFF* */
+/*?
+ * @cliexpar
+ * @cliexstart{nat66}
+ * To enable NAT66 plugin
+ *  vpp# nat66 enable
+ * To disable NAT66 plugin
+ *  vpp# nat66 disable
+ * To enable NAT66 plugin with outside-vrf id 10
+ *  vpp# nat66 enable outside-vrf 10
+ * @cliexend
+?*/
+VLIB_CLI_COMMAND (nat66_enable_disable_command, static) = {
+  .path = "nat66",
+  .short_help = "nat66 <enable [outside-vrf <vrf-id>]>|disable",
+  .function = nat66_enable_disable_command_fn,
+};
+
 /*?
  * @cliexpar
  * @cliexstart{set interface nat66}