LISP EID virtualization support
[vpp.git] / vnet / vnet / lisp-gpe / lisp_gpe.c
index 6ccc5f0..0eacb38 100644 (file)
@@ -91,27 +91,15 @@ lisp_gpe_rewrite (lisp_gpe_tunnel_t * t)
   return 0;
 }
 
-/* TODO remove */
-int
-vnet_lisp_gpe_add_del_tunnel (vnet_lisp_gpe_add_del_tunnel_args_t *a,
-                              u32 * sw_if_indexp)
-{
-  clib_warning ("UNSUPPORTED! Use vnet_lisp_gpe_add_del_fwd_entry");
-  return 0;
-}
-
 #define foreach_copy_field                      \
 _(encap_fib_index)                              \
 _(decap_fib_index)                              \
 _(decap_next_index)                             \
-_(flags)                                        \
-_(next_protocol)                                \
-_(ver_res)                                      \
-_(res)                                          \
 _(vni)
 
 static u32
-add_del_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u32 * tun_index_res)
+add_del_ip_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a,
+                   u32 * tun_index_res)
 {
   lisp_gpe_main_t * lgm = &lisp_gpe_main;
   lisp_gpe_tunnel_t *t = 0;
@@ -119,6 +107,7 @@ add_del_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u32 * tun_index_res)
   int rv;
   lisp_gpe_tunnel_key_t key;
 
+  /* prepare tunnel key */
   memset(&key, 0, sizeof(key));
   ip_prefix_copy(&key.eid, &gid_address_ippref(&a->deid));
   ip_address_copy(&key.dst_loc, &a->dlocator);
@@ -146,6 +135,17 @@ add_del_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a, u32 * tun_index_res)
       ip_address_copy(&t->src, &a->slocator);
       ip_address_copy(&t->dst, &a->dlocator);
 
+      /* if vni is non-default */
+      if (a->vni)
+        {
+          t->flags = LISP_GPE_FLAGS_I;
+          t->vni = a->vni;
+        }
+
+      t->flags |= LISP_GPE_FLAGS_P;
+      t->next_protocol = ip_prefix_version(&key.eid) == IP4 ?
+          LISP_GPE_NEXT_PROTO_IP4 : LISP_GPE_NEXT_PROTO_IP6;
+
       rv = lisp_gpe_rewrite (t);
 
       if (rv)
@@ -186,13 +186,20 @@ add_del_negative_fwd_entry (lisp_gpe_main_t * lgm,
                             vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
 {
   ip_adjacency_t adj;
+  ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
+  ip_prefix_t * spref = &gid_address_ippref(&a->seid);
+
   /* setup adjacency for eid */
   memset (&adj, 0, sizeof(adj));
   adj.n_adj = 1;
-  adj.explicit_fib_index = ~0;
 
-  ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
-  ip_prefix_t * spref = &gid_address_ippref(&a->seid);
+  /* fill in 'legal' data to avoid issues */
+  adj.lookup_next_index =  (ip_prefix_version(dpref) == IP4) ?
+                                  lgm->ip4_lookup_next_lgpe_ip4_lookup :
+                                  lgm->ip6_lookup_next_lgpe_ip6_lookup;
+
+  adj.rewrite_header.sw_if_index = ~0;
+  adj.rewrite_header.next_index = ~0;
 
   switch (a->action)
     {
@@ -202,16 +209,21 @@ add_del_negative_fwd_entry (lisp_gpe_main_t * lgm,
       /* TODO check if route/next-hop for eid exists in fib and add
        * more specific for the eid with the next-hop found */
     case SEND_MAP_REQUEST:
-      /* TODO insert tunnel that always sends map-request */
+      /* insert tunnel that always sends map-request */
+      adj.explicit_fib_index = (ip_prefix_version(dpref) == IP4) ?
+                               LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP:
+                               LGPE_IP6_LOOKUP_NEXT_LISP_CP_LOOKUP;
+      /* add/delete route for prefix */
+      return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
+                                      a->is_add);
     case DROP:
       /* for drop fwd entries, just add route, no need to add encap tunnel */
-      adj.lookup_next_index =  ip_prefix_version(dpref) == IP4 ?
-              LGPE_IP4_LOOKUP_NEXT_DROP : LGPE_IP6_LOOKUP_NEXT_DROP;
+      adj.explicit_fib_index =  (ip_prefix_version(dpref) == IP4 ?
+              LGPE_IP4_LOOKUP_NEXT_DROP : LGPE_IP6_LOOKUP_NEXT_DROP);
 
       /* add/delete route for prefix */
       return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
                                       a->is_add);
-      break;
     default:
       return -1;
     }
@@ -228,6 +240,12 @@ vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
   uword * lookup_next_index, * lgpe_sw_if_index, * lnip;
   u8 ip_ver;
 
+  if (vnet_lisp_gpe_enable_disable_status() == 0)
+    {
+      clib_warning ("LISP is disabled!");
+      return VNET_API_ERROR_LISP_DISABLED;
+    }
+
   /* treat negative fwd entries separately */
   if (a->is_negative)
     return add_del_negative_fwd_entry (lgm, a);
@@ -236,25 +254,25 @@ vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
   spref = &gid_address_ippref(&a->seid);
   ip_ver = ip_prefix_version(dpref);
 
-  a->next_protocol = ip_ver == IP4 ?
-      LISP_GPE_NEXT_PROTO_IP4 : LISP_GPE_NEXT_PROTO_IP6;
-  a->flags |= LISP_GPE_FLAGS_P;
-
   /* add/del tunnel to tunnels pool and prepares rewrite */
-  rv = add_del_tunnel (a, &tun_index);
+  rv = add_del_ip_tunnel (a, &tun_index);
   if (rv)
     return rv;
 
   /* setup adjacency for eid */
   memset (&adj, 0, sizeof(adj));
   adj.n_adj = 1;
-  adj.explicit_fib_index = ~0;
+
+  /* fill in lookup_next_index with a 'legal' value to avoid problems */
+  adj.lookup_next_index = (ip_ver == IP4) ?
+          lgm->ip4_lookup_next_lgpe_ip4_lookup :
+          lgm->ip6_lookup_next_lgpe_ip6_lookup;
 
   if (a->is_add)
     {
       /* send packets that hit this adj to lisp-gpe interface output node in
        * requested vrf. */
-      lnip = ip_ver == IP4 ?
+      lnip = (ip_ver == IP4) ?
               lgm->lgpe_ip4_lookup_next_index_by_table_id :
               lgm->lgpe_ip6_lookup_next_index_by_table_id;
       lookup_next_index = hash_get(lnip, a->table_id);
@@ -266,8 +284,10 @@ vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
       ASSERT(lookup_next_index != 0);
       ASSERT(lgpe_sw_if_index != 0);
 
-      adj.lookup_next_index = lookup_next_index[0];
-      adj.rewrite_header.node_index = tun_index;
+      /* hijack explicit fib index to store lisp interface node index and
+       * if_address_index for the tunnel index */
+      adj.explicit_fib_index = lookup_next_index[0];
+      adj.if_address_index = tun_index;
       adj.rewrite_header.sw_if_index = lgpe_sw_if_index[0];
     }
 
@@ -285,7 +305,7 @@ vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
                                adj_index);
 
       ASSERT(adjp != 0);
-      ASSERT(adjp->rewrite_header.node_index == tun_index);
+      ASSERT(adjp->if_address_index == tun_index);
     }
 
   return rv;
@@ -303,6 +323,7 @@ lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
   gid_address_t * eids = 0, eid;
   clib_error_t * error = 0;
   u32 i;
+  int rv;
 
   prefp = &gid_address_ippref(&eid);
 
@@ -355,19 +376,23 @@ lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
       a.deid = eids[i];
       a.slocator = slocators[i];
       a.dlocator = dlocators[i];
-      prefp = &gid_address_ippref(&a.deid);
-      a.decap_next_index = (ip_prefix_version(prefp) == IP4) ?
-              LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
-      vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
+      rv = vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
+      if (0 != rv)
+        {
+          error = clib_error_return(0, "failed to %s gpe maptunnel!",
+                                    is_add ? "add" : "delete");
+          break;
+        }
     }
 
  done:
   vec_free(eids);
   vec_free(slocators);
+  vec_free(dlocators);
   return error;
 }
 
-VLIB_CLI_COMMAND (add_del_lisp_gpe_mapping_tunnel_command, static) = {
+VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
   .path = "lisp gpe maptunnel",
   .short_help = "lisp gpe maptunnel eid <eid> sloc <src-locator> "
       "dloc <dst-locator> [del]",
@@ -445,6 +470,14 @@ VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
     .function = show_lisp_gpe_tunnel_command_fn,
 };
 
+u8
+vnet_lisp_gpe_enable_disable_status(void)
+{
+  lisp_gpe_main_t * lgm = &lisp_gpe_main;
+
+  return lgm->is_en;
+}
+
 clib_error_t *
 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
 {
@@ -471,6 +504,8 @@ vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
         {
           /* ask cp to re-add ifaces and defaults */
         }
+
+      lgm->is_en = 1;
     }
   else
     {
@@ -509,6 +544,7 @@ vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
         vnet_lisp_gpe_add_del_iface(ai, 0);
       }
       vec_free(table_ids);
+      lgm->is_en = 0;
     }
 
   return 0;
@@ -548,6 +584,27 @@ VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
   .function = lisp_gpe_enable_disable_command_fn,
 };
 
+static clib_error_t *
+lisp_show_iface_command_fn (vlib_main_t * vm,
+                            unformat_input_t * input,
+                            vlib_cli_command_t * cmd)
+{
+  lisp_gpe_main_t * lgm = &lisp_gpe_main;
+  hash_pair_t * p;
+
+  vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
+  hash_foreach_pair (p, lgm->lisp_gpe_hw_if_index_by_table_id, ({
+    vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
+  }));
+  return 0;
+}
+
+VLIB_CLI_COMMAND (lisp_show_iface_command) = {
+    .path = "show lisp gpe interface",
+    .short_help = "show lisp gpe interface",
+    .function = lisp_show_iface_command_fn,
+};
+
 clib_error_t *
 lisp_gpe_init (vlib_main_t *vm)
 {
@@ -579,4 +636,11 @@ lisp_gpe_init (vlib_main_t *vm)
   return 0;
 }
 
+u8 *
+format_vnet_lisp_gpe_status (u8 * s, va_list * args)
+{
+  lisp_gpe_main_t * lgm = &lisp_gpe_main;
+  return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
+}
+
 VLIB_INIT_FUNCTION(lisp_gpe_init);