lisp: API cleanup
[vpp.git] / src / vnet / lisp-gpe / lisp_gpe_api.c
index e2c055b..e20347a 100644 (file)
 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
-
+#include <vnet/fib/fib_table.h>
 #include <vnet/vnet_msg_enum.h>
+#include <vnet/ip/ip_types_api.h>
+#include <vnet/ethernet/ethernet_types_api.h>
+#include <vnet/lisp-gpe/lisp_types_api.h>
 
 #define vl_api_gpe_locator_pair_t_endian vl_noop_handler
 #define vl_api_gpe_locator_pair_t_print vl_noop_handler
 
 #include <vlibapi/api_helper_macros.h>
 
-#define foreach_vpe_api_msg                             \
-_(GPE_ADD_DEL_FWD_ENTRY, gpe_add_del_fwd_entry)               \
-_(GPE_FWD_ENTRIES_GET, gpe_fwd_entries_get)                   \
-_(GPE_FWD_ENTRY_PATH_DUMP, gpe_fwd_entry_path_dump)           \
-_(GPE_ENABLE_DISABLE, gpe_enable_disable)                     \
-_(GPE_ADD_DEL_IFACE, gpe_add_del_iface)                       \
-_(GPE_FWD_ENTRY_VNIS_GET, gpe_fwd_entry_vnis_get)             \
-_(GPE_SET_ENCAP_MODE, gpe_set_encap_mode)                     \
-_(GPE_GET_ENCAP_MODE, gpe_get_encap_mode)
+#define foreach_vpe_api_msg                                    \
+_(GPE_ADD_DEL_FWD_ENTRY, gpe_add_del_fwd_entry)                \
+_(GPE_FWD_ENTRIES_GET, gpe_fwd_entries_get)                    \
+_(GPE_FWD_ENTRY_PATH_DUMP, gpe_fwd_entry_path_dump)            \
+_(GPE_ENABLE_DISABLE, gpe_enable_disable)                      \
+_(GPE_ADD_DEL_IFACE, gpe_add_del_iface)                        \
+_(GPE_FWD_ENTRY_VNIS_GET, gpe_fwd_entry_vnis_get)              \
+_(GPE_SET_ENCAP_MODE, gpe_set_encap_mode)                      \
+_(GPE_GET_ENCAP_MODE, gpe_get_encap_mode)                      \
+_(GPE_ADD_DEL_NATIVE_FWD_RPATH, gpe_add_del_native_fwd_rpath)  \
+_(GPE_NATIVE_FWD_RPATHS_GET, gpe_native_fwd_rpaths_get)
 
 static locator_pair_t *
 unformat_gpe_loc_pairs (void *locs, u32 rloc_num)
@@ -72,8 +77,8 @@ unformat_gpe_loc_pairs (void *locs, u32 rloc_num)
     {
       /* local locator */
       r = &((vl_api_gpe_locator_t *) locs)[i];
-      memset (&pair, 0, sizeof (pair));
-      ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
+      clib_memset (&pair, 0, sizeof (pair));
+      ip_address_decode2 (&r->addr, &pair.lcl_loc);
 
       pair.weight = r->weight;
       vec_add1 (pairs, pair);
@@ -84,43 +89,11 @@ unformat_gpe_loc_pairs (void *locs, u32 rloc_num)
       /* remote locators */
       r = &((vl_api_gpe_locator_t *) locs)[i];
       p = &pairs[i - rloc_num];
-      ip_address_set (&p->rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
+      ip_address_decode2 (&r->addr, &p->rmt_loc);
     }
   return pairs;
 }
 
-static int
-unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
-                      u8 len)
-{
-  switch (type)
-    {
-    case 0:                    /* ipv4 */
-      gid_address_type (dst) = GID_ADDR_IP_PREFIX;
-      gid_address_ip_set (dst, src, IP4);
-      gid_address_ippref_len (dst) = len;
-      ip_prefix_normalize (&gid_address_ippref (dst));
-      break;
-    case 1:                    /* ipv6 */
-      gid_address_type (dst) = GID_ADDR_IP_PREFIX;
-      gid_address_ip_set (dst, src, IP6);
-      gid_address_ippref_len (dst) = len;
-      ip_prefix_normalize (&gid_address_ippref (dst));
-      break;
-    case 2:                    /* l2 mac */
-      gid_address_type (dst) = GID_ADDR_MAC;
-      clib_memcpy (&gid_address_mac (dst), src, 6);
-      break;
-    default:
-      /* unknown type */
-      return VNET_API_ERROR_INVALID_VALUE;
-    }
-
-  gid_address_vni (dst) = vni;
-
-  return 0;
-}
-
 static void
   gpe_fwd_entry_path_dump_t_net_to_host
   (vl_api_gpe_fwd_entry_path_dump_t * mp)
@@ -133,16 +106,7 @@ lisp_api_set_locator (vl_api_gpe_locator_t * loc,
                      const ip_address_t * addr, u8 weight)
 {
   loc->weight = weight;
-  if (IP4 == ip_addr_version (addr))
-    {
-      loc->is_ip4 = 1;
-      memcpy (loc->addr, addr, 4);
-    }
-  else
-    {
-      loc->is_ip4 = 0;
-      memcpy (loc->addr, addr, 16);
-    }
+  ip_address_encode2 (addr, &loc->addr);
 }
 
 static void
@@ -152,13 +116,13 @@ static void
   lisp_fwd_path_t *path;
   vl_api_gpe_fwd_entry_path_details_t *rmp = NULL;
   lisp_gpe_main_t *lgm = &lisp_gpe_main;
-  unix_shared_memory_queue_t *q = NULL;
+  vl_api_registration_t *reg;
   lisp_gpe_fwd_entry_t *lfe;
 
   gpe_fwd_entry_path_dump_t_net_to_host (mp);
 
-  q = vl_api_client_index_to_input_queue (mp->client_index);
-  if (q == 0)
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
     return;
 
   if (pool_is_free_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index))
@@ -172,7 +136,7 @@ static void
   vec_foreach (path, lfe->paths)
   {
     rmp = vl_msg_api_alloc (sizeof (*rmp));
-    memset (rmp, 0, sizeof (*rmp));
+    clib_memset (rmp, 0, sizeof (*rmp));
     const lisp_gpe_tunnel_t *lgt;
 
     rmp->_vl_msg_id =
@@ -185,7 +149,7 @@ static void
     lisp_api_set_locator (&rmp->lcl_loc, &lgt->key->lcl, path->weight);
 
     rmp->context = mp->context;
-    vl_msg_api_send_shmem (q, (u8 *) & rmp);
+    vl_api_send_msg (reg, (u8 *) rmp);
   }
 }
 
@@ -198,7 +162,7 @@ gpe_fwd_entries_copy (vl_api_gpe_fwd_entry_t * dst,
 
   vec_foreach (e, src)
   {
-    memset (&dst[i], 0, sizeof (*dst));
+    clib_memset (&dst[i], 0, sizeof (*dst));
     dst[i].dp_table = e->dp_table;
     dst[i].fwd_entry_index = e->fwd_entry_index;
     dst[i].vni = e->vni;
@@ -206,25 +170,20 @@ gpe_fwd_entries_copy (vl_api_gpe_fwd_entry_t * dst,
     switch (fid_addr_type (&e->leid))
       {
       case FID_ADDR_IP_PREF:
-       if (IP4 == ip_prefix_version (&fid_addr_ippref (&e->leid)))
-         {
-           memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 4);
-           memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 4);
-           dst[i].eid_type = 0;
-         }
-       else
-         {
-           memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 16);
-           memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 16);
-           dst[i].eid_type = 1;
-         }
-       dst[i].leid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->leid));
-       dst[i].reid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->reid));
+       dst[i].leid.type = EID_TYPE_API_PREFIX;
+       dst[i].reid.type = EID_TYPE_API_PREFIX;
+       ip_prefix_encode2 (&fid_addr_ippref (&e->leid),
+                          &dst[i].leid.address.prefix);
+       ip_prefix_encode2 (&fid_addr_ippref (&e->reid),
+                          &dst[i].reid.address.prefix);
        break;
       case FID_ADDR_MAC:
-       memcpy (&dst[i].leid, fid_addr_mac (&e->leid), 6);
-       memcpy (&dst[i].reid, fid_addr_mac (&e->reid), 6);
-       dst[i].eid_type = 2;
+       mac_address_encode ((mac_address_t *) fid_addr_mac (&e->leid),
+                           dst[i].leid.address.mac);
+       mac_address_encode ((mac_address_t *) fid_addr_mac (&e->reid),
+                           dst[i].reid.address.mac);
+       dst[i].leid.type = EID_TYPE_API_MAC;
+       dst[i].reid.type = EID_TYPE_API_MAC;
        break;
       default:
        clib_warning ("unknown fid type %d!", fid_addr_type (&e->leid));
@@ -330,12 +289,10 @@ vl_api_gpe_add_del_fwd_entry_t_handler (vl_api_gpe_add_del_fwd_entry_t * mp)
   int rv = 0;
 
   gpe_add_del_fwd_entry_t_net_to_host (mp);
-  memset (a, 0, sizeof (a[0]));
+  clib_memset (a, 0, sizeof (a[0]));
 
-  rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
-                             mp->rmt_eid, mp->rmt_len);
-  rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
-                              mp->lcl_eid, mp->lcl_len);
+  rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, &mp->rmt_eid);
+  rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, &mp->lcl_eid);
 
   if (mp->loc_num % 2 != 0)
     {
@@ -358,7 +315,12 @@ vl_api_gpe_add_del_fwd_entry_t_handler (vl_api_gpe_add_del_fwd_entry_t * mp)
   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
   vec_free (pairs);
 send_reply:
-  REPLY_MACRO (VL_API_GPE_ADD_DEL_FWD_ENTRY_REPLY);
+  /* *INDENT-OFF* */
+  REPLY_MACRO2 (VL_API_GPE_ADD_DEL_FWD_ENTRY_REPLY,
+  {
+    rmp->fwd_entry_index = clib_host_to_net_u32 (a->fwd_entry_index);
+  });
+  /* *INDENT-ON* */
 }
 
 static void
@@ -368,7 +330,7 @@ vl_api_gpe_enable_disable_t_handler (vl_api_gpe_enable_disable_t * mp)
   int rv = 0;
   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
 
-  a->is_en = mp->is_en;
+  a->is_en = mp->is_enable;
   vnet_lisp_gpe_enable_disable (a);
 
   REPLY_MACRO (VL_API_GPE_ENABLE_DISABLE_REPLY);
@@ -379,28 +341,30 @@ vl_api_gpe_add_del_iface_t_handler (vl_api_gpe_add_del_iface_t * mp)
 {
   vl_api_gpe_add_del_iface_reply_t *rmp;
   int rv = 0;
+  u32 vni, dp_table;
+
+  vni = clib_net_to_host_u32 (mp->vni);
+  dp_table = clib_net_to_host_u32 (mp->dp_table);
 
   if (mp->is_l2)
     {
       if (mp->is_add)
        {
-         if (~0 ==
-             lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
+         if (~0 == lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table))
            rv = 1;
        }
       else
-       lisp_gpe_tenant_l2_iface_unlock (mp->vni);
+       lisp_gpe_tenant_l2_iface_unlock (vni);
     }
   else
     {
       if (mp->is_add)
        {
-         if (~0 ==
-             lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
+         if (~0 == lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table, 1))
            rv = 1;
        }
       else
-       lisp_gpe_tenant_l3_iface_unlock (mp->vni);
+       lisp_gpe_tenant_l3_iface_unlock (vni);
     }
 
   REPLY_MACRO (VL_API_GPE_ADD_DEL_IFACE_REPLY);
@@ -412,7 +376,7 @@ vl_api_gpe_set_encap_mode_t_handler (vl_api_gpe_set_encap_mode_t * mp)
   vl_api_gpe_set_encap_mode_reply_t *rmp;
   int rv = 0;
 
-  rv = vnet_gpe_set_encap_mode (mp->mode);
+  rv = vnet_gpe_set_encap_mode (mp->is_vxlan);
   REPLY_MACRO (VL_API_GPE_SET_ENCAP_MODE_REPLY);
 }
 
@@ -430,10 +394,112 @@ vl_api_gpe_get_encap_mode_t_handler (vl_api_gpe_get_encap_mode_t * mp)
   /* *INDENT-ON* */
 }
 
+static void
+  vl_api_gpe_add_del_native_fwd_rpath_t_handler
+  (vl_api_gpe_add_del_native_fwd_rpath_t * mp)
+{
+  vl_api_gpe_add_del_native_fwd_rpath_reply_t *rmp;
+  vnet_gpe_native_fwd_rpath_args_t _a, *a = &_a;
+  int rv = 0;
+
+  clib_memset (a, 0, sizeof (a[0]));
+
+  if (mp->nh_addr.af)
+    clib_memcpy (&a->rpath.frp_addr.ip6, mp->nh_addr.un.ip6,
+                sizeof (ip6_address_t));
+  else
+    clib_memcpy (&a->rpath.frp_addr.ip4, mp->nh_addr.un.ip4,
+                sizeof (ip4_address_t));
+
+  a->is_add = mp->is_add;
+  a->rpath.frp_proto = mp->nh_addr.af ? DPO_PROTO_IP6 : DPO_PROTO_IP4;
+  a->rpath.frp_fib_index =
+    fib_table_find (dpo_proto_to_fib (a->rpath.frp_proto),
+                   clib_net_to_host_u32 (mp->table_id));
+  if (~0 == a->rpath.frp_fib_index)
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto done;
+    }
+
+  a->rpath.frp_sw_if_index = clib_net_to_host_u32 (mp->nh_sw_if_index);
+  a->rpath.frp_weight = 1;
+
+  rv = vnet_gpe_add_del_native_fwd_rpath (a);
+done:
+  REPLY_MACRO (VL_API_GPE_ADD_DEL_NATIVE_FWD_RPATH_REPLY);
+}
+
+static void
+gpe_native_fwd_rpaths_copy (vl_api_gpe_native_fwd_rpath_t * dst,
+                           fib_route_path_t * src)
+{
+  fib_route_path_t *e;
+  fib_table_t *table;
+  u32 i = 0;
+
+  vec_foreach (e, src)
+  {
+    clib_memset (&dst[i], 0, sizeof (*dst));
+    table = fib_table_get (e->frp_fib_index, dpo_proto_to_fib (e->frp_proto));
+    dst[i].fib_index = table->ft_table_id;
+    dst[i].nh_sw_if_index = e->frp_sw_if_index;
+    ip_address_encode (&e->frp_addr, IP46_TYPE_ANY, &dst[i].nh_addr);
+    i++;
+  }
+}
+
+static void
+gpe_native_fwd_rpath_t_host_to_net (vl_api_gpe_native_fwd_rpath_t * e)
+{
+  e->fib_index = clib_host_to_net_u32 (e->fib_index);
+  e->nh_sw_if_index = clib_host_to_net_u32 (e->nh_sw_if_index);
+}
+
+static void
+  gpe_native_fwd_rpaths_get_reply_t_host_to_net
+  (vl_api_gpe_native_fwd_rpaths_get_reply_t * mp)
+{
+  u32 i;
+  vl_api_gpe_native_fwd_rpath_t *e;
+
+  for (i = 0; i < mp->count; i++)
+    {
+      e = &mp->entries[i];
+      gpe_native_fwd_rpath_t_host_to_net (e);
+    }
+  mp->count = clib_host_to_net_u32 (mp->count);
+}
+
+static void
+vl_api_gpe_native_fwd_rpaths_get_t_handler (vl_api_gpe_native_fwd_rpaths_get_t
+                                           * mp)
+{
+  lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
+  vl_api_gpe_native_fwd_rpaths_get_reply_t *rmp;
+  u32 size = 0;
+  int rv = 0;
+
+  u8 rpath_index = mp->is_ip4 ? 1 : 0;
+
+  size = vec_len (lgm->native_fwd_rpath[rpath_index])
+    * sizeof (vl_api_gpe_native_fwd_rpath_t);
+
+  /* *INDENT-OFF* */
+  REPLY_MACRO4 (VL_API_GPE_NATIVE_FWD_RPATHS_GET_REPLY, size,
+  {
+    rmp->count = vec_len (lgm->native_fwd_rpath[rpath_index]);
+    gpe_native_fwd_rpaths_copy (rmp->entries,
+                               lgm->native_fwd_rpath[rpath_index]);
+    gpe_native_fwd_rpaths_get_reply_t_host_to_net (rmp);
+  });
+  /* *INDENT-ON* */
+}
+
 /*
  * gpe_api_hookup
  * Add vpe's API message handlers to the table.
- * vlib has alread mapped shared memory and
+ * vlib has already mapped shared memory and
  * added the client registration handlers.
  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
  */
@@ -452,7 +518,7 @@ setup_message_id_table (api_main_t * am)
 static clib_error_t *
 gpe_api_hookup (vlib_main_t * vm)
 {
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
 
 #define _(N,n)                                                  \
     vl_msg_api_set_handlers(VL_API_##N, #n,                     \