Add option to use LISP Proxy-ETR

When enabled, destinations with negative mappings or those not reachable
via underlay have their traffic forwarded to the PETR.

Change-Id: I1056b0959736144f27fcca7b79263f921e7a8bd9
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/lisp-cp/control.c b/src/vnet/lisp-cp/control.c
index 043d60f..c3406d8 100644
--- a/src/vnet/lisp-cp/control.c
+++ b/src/vnet/lisp-cp/control.c
@@ -432,11 +432,13 @@
 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
 {
   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
-  mapping_t *src_map, *dst_map;
+  gid_address_t *rmt_eid, *lcl_eid;
+  mapping_t *lcl_map, *rmt_map;
   u32 sw_if_index;
   uword *feip = 0, *dpid;
   fwd_entry_t *fe;
   u8 type, is_src_dst = 0;
+  int rv;
 
   memset (a, 0, sizeof (*a));
 
@@ -445,33 +447,44 @@
   if (feip)
     dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
 
+  /*
+   * Determine local mapping and eid
+   */
   if (lcm->lisp_pitr)
-    src_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
+    lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
   else
-    src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
-  dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
+    lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
+  lcl_eid = &lcl_map->eid;
 
-  /* insert data plane forwarding entry */
+  /*
+   * Determine remote mapping and eid
+   */
+  rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
+  rmt_eid = &rmt_map->eid;
+
+  /*
+   * Build and insert data plane forwarding entry
+   */
   a->is_add = 1;
 
   if (MR_MODE_SRC_DST == lcm->map_request_mode)
     {
-      if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
+      if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
 	{
-	  gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
-				  &gid_address_sd_dst (&dst_map->eid));
-	  gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
-				  &gid_address_sd_src (&dst_map->eid));
+	  gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
+				  &gid_address_sd_dst (rmt_eid));
+	  gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
+				  &gid_address_sd_src (rmt_eid));
 	}
       else
 	{
-	  gid_address_copy (&a->rmt_eid, &dst_map->eid);
-	  gid_address_copy (&a->lcl_eid, &src_map->eid);
+	  gid_address_copy (&a->rmt_eid, rmt_eid);
+	  gid_address_copy (&a->lcl_eid, lcl_eid);
 	}
       is_src_dst = 1;
     }
   else
-    gid_address_copy (&a->rmt_eid, &dst_map->eid);
+    gid_address_copy (&a->rmt_eid, rmt_eid);
 
   a->vni = gid_address_vni (&a->rmt_eid);
 
@@ -499,17 +512,22 @@
     }
 
   /* find best locator pair that 1) verifies LISP policy 2) are connected */
-  if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
+  rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
+
+  /* Either rmt mapping is negative or we can't find underlay path.
+   * Try again with petr if configured */
+  if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
     {
-      /* negative entry */
-      a->is_negative = 1;
-      a->action = dst_map->action;
+      rmt_map = lisp_get_petr_mapping (lcm);
+      rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
     }
 
-  /* TODO remove */
-  u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
-  a->decap_next_index = (ipver == IP4) ?
-    LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
+  /* negative entry */
+  if (rv == 0)
+    {
+      a->is_negative = 1;
+      a->action = rmt_map->action;
+    }
 
   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
 
@@ -521,7 +539,7 @@
   if (is_src_dst)
     gid_address_copy (&fe->leid, &a->lcl_eid);
   else
-    gid_address_copy (&fe->leid, &src_map->eid);
+    gid_address_copy (&fe->leid, lcl_eid);
 
   fe->is_src_dst = is_src_dst;
   hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
@@ -1191,7 +1209,6 @@
       local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
 	gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
 
-
       if (GID_LOOKUP_MISS == local_mi)
 	{
 	  clib_warning ("Local eid %U not found. Cannot add adjacency!",
@@ -1273,6 +1290,69 @@
   return 0;
 }
 
+/**
+ * Configure Proxy-ETR
+ *
+ * @param ip PETR's IP address
+ * @param is_add Flag that indicates if this is an addition or removal
+ *
+ * return 0 on success
+ */
+int
+vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
+{
+  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
+  u32 ls_index = ~0;
+  mapping_t *m;
+  vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
+  locator_t loc;
+
+  if (vnet_lisp_enable_disable_status () == 0)
+    {
+      clib_warning ("LISP is disabled!");
+      return VNET_API_ERROR_LISP_DISABLED;
+    }
+
+  memset (ls_args, 0, sizeof (*ls_args));
+
+  if (is_add)
+    {
+      /* Create dummy petr locator-set */
+      gid_address_from_ip (&loc.address, ip);
+      loc.priority = 1;
+      loc.state = loc.weight = 1;
+
+      ls_args->is_add = 1;
+      ls_args->index = ~0;
+      vec_add1 (ls_args->locators, loc);
+      vnet_lisp_add_del_locator_set (ls_args, &ls_index);
+
+      /* Add petr mapping */
+      pool_get (lcm->mapping_pool, m);
+      m->locator_set_index = ls_index;
+      lcm->petr_map_index = m - lcm->mapping_pool;
+
+      /* Enable use-petr */
+      lcm->flags |= LISP_FLAG_USE_PETR;
+    }
+  else
+    {
+      m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
+
+      /* Remove petr locator */
+      ls_args->is_add = 0;
+      ls_args->index = m->locator_set_index;
+      vnet_lisp_add_del_locator_set (ls_args, 0);
+
+      /* Remove petr mapping */
+      pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
+
+      /* Disable use-petr */
+      lcm->flags &= ~LISP_FLAG_USE_PETR;
+    }
+  return 0;
+}
+
 /* cleans locator to locator-set data and removes locators not part of
  * any locator-set */
 static void
@@ -2883,21 +2963,21 @@
 			       m->authoritative, m->ttl,
 			       1, 0 /* is_static */ , &dst_map_index);
 
+    if (dst_map_index == (u32) ~ 0)
+      continue;
+
     /* try to program forwarding only if mapping saved or updated */
-    if ((u32) ~ 0 != dst_map_index)
-      {
-	vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
-	memset (adj_args, 0, sizeof (adj_args[0]));
+    vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
+    memset (adj_args, 0, sizeof (adj_args[0]));
 
-	gid_address_copy (&adj_args->leid, &pmr->src);
-	gid_address_copy (&adj_args->reid, &m->eid);
-	adj_args->is_add = 1;
-	if (vnet_lisp_add_del_adjacency (adj_args))
-	  clib_warning ("failed to add adjacency!");
+    gid_address_copy (&adj_args->leid, &pmr->src);
+    gid_address_copy (&adj_args->reid, &m->eid);
+    adj_args->is_add = 1;
+    if (vnet_lisp_add_del_adjacency (adj_args))
+      clib_warning ("failed to add adjacency!");
 
-	if ((u32) ~ 0 != m->ttl)
-	  mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
-      }
+    if ((u32) ~ 0 != m->ttl)
+      mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
   }
 
   /* remove pending map request entry */
@@ -3442,6 +3522,7 @@
   lcm->vnet_main = vnet_get_main ();
   lcm->mreq_itr_rlocs = ~0;
   lcm->lisp_pitr = 0;
+  lcm->flags = 0;
   memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
 
   gid_dictionary_init (&lcm->mapping_index_by_gid);
diff --git a/src/vnet/lisp-cp/control.h b/src/vnet/lisp-cp/control.h
index aa76a42..14f3bae 100644
--- a/src/vnet/lisp-cp/control.h
+++ b/src/vnet/lisp-cp/control.h
@@ -94,8 +94,27 @@
   _MR_MODE_MAX
 } map_request_mode_t;
 
+#define foreach_lisp_flag_bit       \
+  _(USE_PETR, "Use Proxy-ETR")
+
+typedef enum lisp_flag_bits
+{
+#define _(sym, str) LISP_FLAG_BIT_##sym,
+  foreach_lisp_flag_bit
+#undef _
+} lisp_flag_bits_e;
+
+typedef enum lisp_flags
+{
+#define _(sym, str) LISP_FLAG_##sym = 1 << LISP_FLAG_BIT_##sym,
+  foreach_lisp_flag_bit
+#undef _
+} lisp_flags_e;
+
 typedef struct
 {
+  u32 flags;
+
   /* LISP feature status */
   u8 is_enabled;
 
@@ -170,9 +189,12 @@
   /* track l2 and l3 interfaces that have been created for vni */
   uword *l2_dp_intf_by_vni;
 
-  /* Proxy ETR map index */
+  /* Proxy ITR map index */
   u32 pitr_map_index;
 
+  /** Proxy ETR map index */
+  u32 petr_map_index;
+
   /* LISP PITR mode */
   u8 lisp_pitr;
 
@@ -280,6 +302,7 @@
 u8 vnet_lisp_enable_disable_status (void);
 
 int vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add);
+int vnet_lisp_use_petr (ip_address_t * ip, u8 is_add);
 
 typedef struct
 {
@@ -303,6 +326,12 @@
 u8 vnet_lisp_map_register_state_get (void);
 u8 vnet_lisp_rloc_probe_state_get (void);
 
+always_inline mapping_t *
+lisp_get_petr_mapping (lisp_cp_main_t * lcm)
+{
+  return pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
+}
+
 #endif /* VNET_CONTROL_H_ */
 
 /*
diff --git a/src/vnet/lisp-cp/lisp.api b/src/vnet/lisp-cp/lisp.api
index 20c17aa..f0feafe 100644
--- a/src/vnet/lisp-cp/lisp.api
+++ b/src/vnet/lisp-cp/lisp.api
@@ -220,6 +220,57 @@
   i32 retval;
 };
 
+/** \brief configure or disable use of PETR
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param is_ip4 - Address is IPv4 if set and IPv6 otherwise
+    @param address - PETR IP address
+    @param is_add - add locator set if non-zero, else disable pitr
+*/
+define lisp_use_petr
+{
+  u32 client_index;
+  u32 context;
+  u8 is_ip4;
+  u8 address[16];
+  u8 is_add;
+};
+
+/** \brief Reply for lisp_pitr_set_locator_set
+    @param context - returned sender context, to match reply w/ request
+    @param retval - return code
+*/
+define lisp_use_petr_reply
+{
+  u32 context;
+  i32 retval;
+};
+
+/** \brief Request for LISP PETR status
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+*/
+define show_lisp_use_petr
+{
+  u32 client_index;
+  u32 context;
+};
+
+/** \brief LISP PETR status, enable or disable
+    @param context - sender context, to match reply w/ request
+    @param status - LISP PETR enable if non-zero, else disable
+    @param is_ip4 - Address is IPv4 if non-zero, else IPv6
+    @param address - PETR IP address
+*/
+define show_lisp_use_petr_reply
+{
+  u32 context;
+  i32 retval;
+  u8 status;
+  u8 is_ip4;
+  u8 address[16];
+};
+
 /** \brief Get state of LISP RLOC probing
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
diff --git a/src/vnet/lisp-cp/lisp_api.c b/src/vnet/lisp-cp/lisp_api.c
index d3fc462..6e07a5c 100644
--- a/src/vnet/lisp-cp/lisp_api.c
+++ b/src/vnet/lisp-cp/lisp_api.c
@@ -73,6 +73,8 @@
 _(LISP_GET_MAP_REQUEST_ITR_RLOCS, lisp_get_map_request_itr_rlocs)       \
 _(SHOW_LISP_PITR, show_lisp_pitr)                                       \
 _(SHOW_LISP_MAP_REQUEST_MODE, show_lisp_map_request_mode)               \
+_(LISP_USE_PETR, lisp_use_petr)                                         \
+_(SHOW_LISP_USE_PETR, show_lisp_use_petr)                               \
 
 /** Used for transferring locators via VPP API */
 /* *INDENT-OFF* */
@@ -399,6 +401,53 @@
 }
 
 static void
+vl_api_lisp_use_petr_t_handler (vl_api_lisp_use_petr_t * mp)
+{
+  vl_api_lisp_use_petr_reply_t *rmp;
+  int rv = 0;
+  ip_address_t addr;
+
+  ip_address_set (&addr, &mp->address, mp->is_ip4 ? IP4 : IP6);
+  rv = vnet_lisp_use_petr (&addr, mp->is_add);
+
+  REPLY_MACRO (VL_API_LISP_USE_PETR_REPLY);
+}
+
+static void
+vl_api_show_lisp_use_petr_t_handler (vl_api_show_lisp_use_petr_t * mp)
+{
+  unix_shared_memory_queue_t *q = NULL;
+  vl_api_show_lisp_use_petr_reply_t *rmp = NULL;
+  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
+  mapping_t *m;
+  locator_set_t *ls = 0;
+  int rv = 0;
+  locator_t *loc;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    {
+      return;
+    }
+
+  rmp->status = lcm->flags & LISP_FLAG_USE_PETR;
+  if (rmp->status)
+    {
+      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]);
+	  gid_address_put (rmp->address, &loc->address);
+	  rmp->is_ip4 = (gid_address_ip_version (&loc->address) == IP4);
+	}
+    }
+
+  REPLY_MACRO (VL_API_SHOW_LISP_USE_PETR_REPLY);
+}
+
+static void
   vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
   (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
 {
diff --git a/src/vnet/lisp-cp/lisp_cli.c b/src/vnet/lisp-cp/lisp_cli.c
index bb859ff..15e6acb 100644
--- a/src/vnet/lisp-cp/lisp_cli.c
+++ b/src/vnet/lisp-cp/lisp_cli.c
@@ -1414,6 +1414,108 @@
 };
 /* *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
+	return clib_error_return (0, "parse error");
+    }
+
+  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:
+  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* */
+
+/* *INDENT-ON* */
 /*
  * fd.io coding-style-patch-verification: ON
  *