LISP: re-fetch mapping before it expires
[vpp.git] / src / vnet / lisp-cp / lisp_cli.c
index bb859ff..5090460 100644 (file)
@@ -25,6 +25,7 @@ lisp_show_adjacencies_command_fn (vlib_main_t * vm,
   vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
   unformat_input_t _line_input, *line_input = &_line_input;
   u32 vni = ~0;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -38,14 +39,14 @@ lisp_show_adjacencies_command_fn (vlib_main_t * vm,
        {
          vlib_cli_output (vm, "parse error: '%U'",
                           format_unformat_error, line_input);
-         return 0;
+         goto done;
        }
     }
 
   if (~0 == vni)
     {
       vlib_cli_output (vm, "error: no vni specified!");
-      return 0;
+      goto done;
     }
 
   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
@@ -57,7 +58,10 @@ lisp_show_adjacencies_command_fn (vlib_main_t * vm,
   }
   vec_free (adjs);
 
-  return 0;
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -77,6 +81,7 @@ lisp_add_del_map_server_command_fn (vlib_main_t * vm,
   u8 is_add = 1, ip_set = 0;
   ip_address_t ip;
   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))
@@ -94,14 +99,14 @@ lisp_add_del_map_server_command_fn (vlib_main_t * vm,
        {
          vlib_cli_output (vm, "parse error: '%U'",
                           format_unformat_error, line_input);
-         return 0;
+         goto done;
        }
     }
 
   if (!ip_set)
     {
       vlib_cli_output (vm, "map-server ip address not set!");
-      return 0;
+      goto done;
     }
 
   rv = vnet_lisp_add_del_map_server (&ip, is_add);
@@ -109,7 +114,10 @@ lisp_add_del_map_server_command_fn (vlib_main_t * vm,
     vlib_cli_output (vm, "failed to %s map-server!",
                     is_add ? "add" : "delete");
 
-  return 0;
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -191,7 +199,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 +221,8 @@ done:
     vec_free (locator_set_name);
   gid_address_free (&a->eid);
   vec_free (a->key);
+  unformat_free (line_input);
+
   return error;
 }
 
@@ -233,6 +243,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 +261,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* */
@@ -378,8 +394,19 @@ lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
 
   /* add as static remote mapping, i.e., not authoritative and infinite
    * ttl */
-  rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
-                                 1 /* is_static */ , 0);
+  if (is_add)
+    {
+      vnet_lisp_add_del_mapping_args_t _map_args, *map_args = &_map_args;
+      memset (map_args, 0, sizeof (map_args[0]));
+      gid_address_copy (&map_args->eid, &eid);
+      map_args->action = action;
+      map_args->is_static = 1;
+      map_args->authoritative = 0;
+      map_args->ttl = ~0;
+      rv = vnet_lisp_add_mapping (map_args, rlocs, NULL, NULL);
+    }
+  else
+    rv = vnet_lisp_del_mapping (&eid, NULL);
 
   if (rv)
     clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
@@ -479,7 +506,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]));
@@ -512,6 +539,7 @@ lisp_map_request_mode_command_fn (vlib_main_t * vm,
 {
   unformat_input_t _i, *i = &_i;
   map_request_mode_t mr_mode = _MR_MODE_MAX;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, i))
@@ -533,12 +561,15 @@ lisp_map_request_mode_command_fn (vlib_main_t * vm,
   if (_MR_MODE_MAX == mr_mode)
     {
       clib_warning ("No LISP map request mode entered!");
-      return 0;
+      goto done;
     }
 
   vnet_lisp_set_map_request_mode (mr_mode);
+
 done:
-  return 0;
+  unformat_free (i);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -630,7 +661,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)
@@ -648,6 +682,8 @@ 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;
 }
 
@@ -771,6 +807,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));
 
@@ -787,8 +824,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",
@@ -799,6 +839,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) ||
@@ -815,7 +858,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,
@@ -824,14 +867,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* */
@@ -850,6 +896,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))
@@ -866,16 +913,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* */
@@ -894,6 +949,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))
@@ -912,18 +968,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* */
@@ -942,6 +1002,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))
@@ -960,18 +1021,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* */
@@ -1019,6 +1084,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))
@@ -1037,14 +1103,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");
@@ -1056,7 +1125,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* */
@@ -1128,6 +1200,8 @@ done:
   vec_free (locators);
   if (locator_set_name)
     vec_free (locator_set_name);
+  unformat_free (line_input);
+
   return error;
 }
 
@@ -1202,6 +1276,8 @@ 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;
 }
 
@@ -1319,6 +1395,8 @@ lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
     }
 
 done:
+  unformat_free (line_input);
+
   return error;
 }
 
@@ -1369,11 +1447,11 @@ lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
                                 is_add ? "add" : "delete");
     }
 
+done:
   vec_free (locator_set_name);
+  unformat_free (line_input);
 
-done:
   return error;
-
 }
 
 /* *INDENT-OFF* */
@@ -1414,6 +1492,111 @@ VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+lisp_use_petr_set_locator_set_command_fn (vlib_main_t * vm,
+                                         unformat_input_t * input,
+                                         vlib_cli_command_t * cmd)
+{
+  u8 is_add = 1, ip_set = 0;
+  unformat_input_t _line_input, *line_input = &_line_input;
+  clib_error_t *error = 0;
+  ip_address_t ip;
+
+  /* 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, "%U", unformat_ip_address, &ip))
+       ip_set = 1;
+      else if (unformat (line_input, "disable"))
+       is_add = 0;
+      else
+       {
+         error = clib_error_return (0, "parse error");
+         goto done;
+       }
+    }
+
+  if (!ip_set)
+    {
+      clib_warning ("No petr IP specified!");
+      goto done;
+    }
+
+  if (vnet_lisp_use_petr (&ip, is_add))
+    {
+      error = clib_error_return (0, "failed to %s petr!",
+                                is_add ? "add" : "delete");
+    }
+
+done:
+  unformat_free (line_input);
+
+  return error;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (lisp_use_petr_set_locator_set_command) = {
+    .path = "lisp use-petr",
+    .short_help = "lisp use-petr [disable] <petr-ip>",
+    .function = lisp_use_petr_set_locator_set_command_fn,
+};
+
+static clib_error_t *
+lisp_show_petr_command_fn (vlib_main_t * vm,
+                           unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
+  mapping_t *m;
+  locator_set_t *ls;
+  locator_t *loc;
+  u8 *tmp_str = 0;
+  u8 use_petr = lcm->flags & LISP_FLAG_USE_PETR;
+  vlib_cli_output (vm, "%=20s%=16s", "petr", use_petr ? "ip" : "");
+
+  if (!use_petr)
+    {
+      vlib_cli_output (vm, "%=20s", "disable");
+      return 0;
+    }
+
+  if (~0 == lcm->petr_map_index)
+    {
+      tmp_str = format (0, "N/A");
+    }
+  else
+    {
+      m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
+      if (~0 != m->locator_set_index)
+        {
+          ls = pool_elt_at_index(lcm->locator_set_pool, m->locator_set_index);
+          loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]);
+          tmp_str = format (0, "%U", format_ip_address, &loc->address);
+        }
+      else
+        {
+          tmp_str = format (0, "N/A");
+        }
+    }
+  vec_add1 (tmp_str, 0);
+
+  vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
+
+  vec_free (tmp_str);
+
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (lisp_show_petr_command) = {
+    .path = "show lisp petr",
+    .short_help = "Show petr",
+    .function = lisp_show_petr_command_fn,
+};
+/* *INDENT-ON* */
+
 /*
  * fd.io coding-style-patch-verification: ON
  *