VPP-328: Add dump call for listing LISP VNIs
[vpp.git] / vpp / vpp-api / api.c
index 86dd9ac..b10baf0 100644 (file)
@@ -82,6 +82,7 @@
 #include <vnet/policer/policer.h>
 #include <vnet/devices/netmap/netmap.h>
 #include <vnet/flow/flow_report.h>
+#include <vnet/ipsec-gre/ipsec_gre.h>
 
 #undef BIHASH_TYPE
 #undef __included_bihash_template_h__
@@ -91,9 +92,7 @@
 #include <vnet/ipsec/ipsec.h>
 #include <vnet/ipsec/ikev2.h>
 #endif /* IPSEC */
-#if DPDK > 0
 #include <vnet/devices/virtio/vhost-user.h>
-#endif
 
 #include <stats/stats.h>
 #include <oam/oam.h>
@@ -346,6 +345,7 @@ _(LISP_EID_TABLE_DUMP, lisp_eid_table_dump)                             \
 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)                           \
 _(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump)                       \
 _(LISP_EID_TABLE_MAP_DUMP, lisp_eid_table_map_dump)                     \
+_(LISP_EID_TABLE_VNI_DUMP, lisp_eid_table_vni_dump)                     \
 _(SHOW_LISP_STATUS, show_lisp_status)                                   \
 _(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS,                                   \
   lisp_add_del_map_request_itr_rlocs)                                   \
@@ -382,7 +382,9 @@ _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
   ip_source_and_port_range_check_add_del)                               \
 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
-  ip_source_and_port_range_check_interface_add_del)
+  ip_source_and_port_range_check_interface_add_del)                     \
+_(IPSEC_GRE_ADD_DEL_TUNNEL, ipsec_gre_add_del_tunnel)                   \
+_(IPSEC_GRE_TUNNEL_DUMP, ipsec_gre_tunnel_dump)
 
 #define QUOTE_(x) #x
 #define QUOTE(x) QUOTE_(x)
@@ -1452,10 +1454,14 @@ vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
   ip6_main_t *ip6m = &ip6_main;
   ip_lookup_main_t *ip4lm = &ip4m->lookup_main;
   ip_lookup_main_t *ip6lm = &ip6m->lookup_main;
-  ip_config_main_t *rx_cm4u = &ip4lm->rx_config_mains[VNET_UNICAST];
-  ip_config_main_t *rx_cm4m = &ip4lm->rx_config_mains[VNET_MULTICAST];
-  ip_config_main_t *rx_cm6u = &ip6lm->rx_config_mains[VNET_UNICAST];
-  ip_config_main_t *rx_cm6m = &ip6lm->rx_config_mains[VNET_MULTICAST];
+  ip_config_main_t *rx_cm4u =
+    &ip4lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
+  ip_config_main_t *rx_cm4m =
+    &ip4lm->feature_config_mains[VNET_IP_RX_MULTICAST_FEAT];
+  ip_config_main_t *rx_cm6u =
+    &ip6lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
+  ip_config_main_t *rx_cm6m =
+    &ip6lm->feature_config_mains[VNET_IP_RX_MULTICAST_FEAT];
 
   VALIDATE_SW_IF_INDEX (mp);
 
@@ -2880,12 +2886,12 @@ vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
   }
   vnet_interface_counter_unlock (im);
 
-  /* Note: in HOST byte order! */
-  rmp->total_pkts[VLIB_RX] = total_pkts[VLIB_RX];
-  rmp->total_bytes[VLIB_RX] = total_bytes[VLIB_RX];
-  rmp->total_pkts[VLIB_TX] = total_pkts[VLIB_TX];
-  rmp->total_bytes[VLIB_TX] = total_bytes[VLIB_TX];
-  rmp->vector_rate = vlib_last_vector_length_per_node (sm->vlib_main);
+  rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
+  rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
+  rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
+  rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
+  rmp->vector_rate =
+    clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
 
   vl_msg_api_send_shmem (q, (u8 *) & rmp);
 }
@@ -4020,16 +4026,20 @@ vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
 {
   int rv = 0;
   vl_api_create_vhost_user_if_reply_t *rmp;
-#if DPDK > 0
   u32 sw_if_index = (u32) ~ 0;
 
   vnet_main_t *vnm = vnet_get_main ();
   vlib_main_t *vm = vlib_get_main ();
 
-  rv = vhost_user_create_if (vnm, vm, (char *) mp->sock_filename,
-                            mp->is_server, &sw_if_index, (u64) ~ 0,
-                            mp->renumber, ntohl (mp->custom_dev_instance),
-                            (mp->use_custom_mac) ? mp->mac_address : NULL);
+#if DPDK > 0 && DPDK_VHOST_USER
+  rv = dpdk_vhost_user_create_if (
+#else
+  rv = vhost_user_create_if (
+#endif
+                             vnm, vm, (char *) mp->sock_filename,
+                             mp->is_server, &sw_if_index, (u64) ~ 0,
+                             mp->renumber, ntohl (mp->custom_dev_instance),
+                             (mp->use_custom_mac) ? mp->mac_address : NULL);
 
   /* *INDENT-OFF* */
   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
@@ -4037,10 +4047,6 @@ vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
     rmp->sw_if_index = ntohl (sw_if_index);
   }));
   /* *INDENT-ON* */
-#else
-  rv = VNET_API_ERROR_UNIMPLEMENTED;
-  REPLY_MACRO (VL_API_CREATE_VHOST_USER_IF_REPLY);
-#endif
 }
 
 static void
@@ -4048,19 +4054,19 @@ vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
 {
   int rv = 0;
   vl_api_modify_vhost_user_if_reply_t *rmp;
-#if DPDK > 0 && DPDK_VHOST_USER
   u32 sw_if_index = ntohl (mp->sw_if_index);
 
   vnet_main_t *vnm = vnet_get_main ();
   vlib_main_t *vm = vlib_get_main ();
 
-  rv = dpdk_vhost_user_modify_if (vnm, vm, (char *) mp->sock_filename,
-                                 mp->is_server, sw_if_index, (u64) ~ 0,
-                                 mp->renumber,
-                                 ntohl (mp->custom_dev_instance));
+#if DPDK > 0 && DPDK_VHOST_USER
+  rv = dpdk_vhost_user_modify_if (
 #else
-  rv = VNET_API_ERROR_UNIMPLEMENTED;
+  rv = vhost_user_modify_if (
 #endif
+                             vnm, vm, (char *) mp->sock_filename,
+                             mp->is_server, sw_if_index, (u64) ~ 0,
+                             mp->renumber, ntohl (mp->custom_dev_instance));
   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
 }
 
@@ -4069,14 +4075,17 @@ vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
 {
   int rv = 0;
   vl_api_delete_vhost_user_if_reply_t *rmp;
-#if DPDK > 0 && DPDK_VHOST_USER
   vpe_api_main_t *vam = &vpe_api_main;
   u32 sw_if_index = ntohl (mp->sw_if_index);
 
   vnet_main_t *vnm = vnet_get_main ();
   vlib_main_t *vm = vlib_get_main ();
 
+#if DPDK > 0 && DPDK_VHOST_USER
   rv = dpdk_vhost_user_delete_if (vnm, vm, sw_if_index);
+#else
+  rv = vhost_user_delete_if (vnm, vm, sw_if_index);
+#endif
 
   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
   if (!rv)
@@ -4088,10 +4097,6 @@ vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
 
       send_sw_interface_flags_deleted (vam, q, sw_if_index);
     }
-#else
-  rv = VNET_API_ERROR_UNIMPLEMENTED;
-  REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
-#endif
 }
 
 static void
@@ -4101,7 +4106,6 @@ static void
   clib_warning ("BUG");
 }
 
-#if DPDK > 0 && DPDK_VHOST_USER
 static void
 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
                                      unix_shared_memory_queue_t * q,
@@ -4128,13 +4132,11 @@ send_sw_interface_vhost_user_details (vpe_api_main_t * am,
 
   vl_msg_api_send_shmem (q, (u8 *) & mp);
 }
-#endif
 
 static void
   vl_api_sw_interface_vhost_user_dump_t_handler
   (vl_api_sw_interface_vhost_user_dump_t * mp)
 {
-#if DPDK > 0 && DPDK_VHOST_USER
   int rv = 0;
   vpe_api_main_t *am = &vpe_api_main;
   vnet_main_t *vnm = vnet_get_main ();
@@ -4147,7 +4149,11 @@ static void
   if (q == 0)
     return;
 
+#if DPDK > 0 && DPDK_VHOST_USER
   rv = dpdk_vhost_user_dump_ifs (vnm, vm, &ifaces);
+#else
+  rv = vhost_user_dump_ifs (vnm, vm, &ifaces);
+#endif
   if (rv)
     return;
 
@@ -4156,7 +4162,6 @@ static void
     send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
   }
   vec_free (ifaces);
-#endif
 }
 
 static void
@@ -5153,7 +5158,12 @@ vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
   vec_free (locator_name);
   vec_free (a->locators);
 
-  REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY);
+  /* *INDENT-OFF* */
+  REPLY_MACRO2 (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY,
+  ({
+    rmp->ls_index = clib_host_to_net_u32 (ls_index);
+  }));
+  /* *INDENT-ON* */
 }
 
 static void
@@ -5496,7 +5506,7 @@ static void
   /* NOTE: for now this works as a static remote mapping, i.e.,
    * not authoritative and ttl infinite. */
   rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
-                                 mp->is_add, 0);
+                                 mp->is_add, 1 /* is_static */ , 0);
 
   if (mp->del_all)
     vnet_lisp_clear_all_remote_adjacencies ();
@@ -5744,9 +5754,8 @@ vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
     {
       memset (eid, 0, sizeof (*eid));
 
-      unformat_lisp_eid_api (eid, mp->eid_type,
-                            clib_net_to_host_u32 (mp->vni), mp->eid,
-                            mp->prefix_length);
+      unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
+                            mp->eid_type, mp->eid, mp->prefix_length);
 
       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
       if ((u32) ~ 0 == mi)
@@ -5861,8 +5870,8 @@ static void
 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
 {
   unix_shared_memory_queue_t *q = NULL;
-  lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
-  map_resolver_t * mr;
+  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
+  map_resolver_t *mr;
 
   q = vl_api_client_index_to_input_queue (mp->client_index);
   if (q == 0)
@@ -5870,9 +5879,9 @@ vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
       return;
     }
 
-  vec_foreach(mr, lcm->map_resolvers)
+  vec_foreach (mr, lcm->map_resolvers)
   {
-    send_lisp_map_resolver_details(&mr->address, q, mp->context);
+    send_lisp_map_resolver_details (&mr->address, q, mp->context);
   }
 }
 
@@ -5913,6 +5922,54 @@ vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
   /* *INDENT-ON* */
 }
 
+static void
+send_eid_table_vni (u32 vni, unix_shared_memory_queue_t * q, u32 context)
+{
+  vl_api_lisp_eid_table_vni_details_t *rmp = 0;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_VNI_DETAILS);
+  rmp->context = context;
+  rmp->vni = clib_host_to_net_u32 (vni);
+  vl_msg_api_send_shmem (q, (u8 *) & rmp);
+}
+
+static void
+vl_api_lisp_eid_table_vni_dump_t_handler (vl_api_lisp_eid_table_vni_dump_t *
+                                         mp)
+{
+  hash_pair_t *p;
+  u32 *vnis = 0;
+  unix_shared_memory_queue_t *q = 0;
+  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    {
+      return;
+    }
+
+  /* *INDENT-OFF* */
+  hash_foreach_pair (p, lcm->table_id_by_vni,
+  ({
+    hash_set (vnis, p->key, 0);
+  }));
+
+  hash_foreach_pair (p, lcm->bd_id_by_vni,
+  ({
+    hash_set (vnis, p->key, 0);
+  }));
+
+  hash_foreach_pair (p, vnis,
+  ({
+    send_eid_table_vni (p->key, q, mp->context);
+  }));
+  /* *INDENT-ON* */
+
+  hash_free (vnis);
+}
+
 static void
 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
 {
@@ -7987,6 +8044,97 @@ reply:
   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
 }
 
+static void
+vl_api_ipsec_gre_add_del_tunnel_t_handler (vl_api_ipsec_gre_add_del_tunnel_t *
+                                          mp)
+{
+  vl_api_ipsec_gre_add_del_tunnel_reply_t *rmp;
+  int rv = 0;
+  vnet_ipsec_gre_add_del_tunnel_args_t _a, *a = &_a;
+  u32 sw_if_index = ~0;
+
+  /* Check src & dst are different */
+  if (memcmp (mp->src_address, mp->dst_address, 4) == 0)
+    {
+      rv = VNET_API_ERROR_SAME_SRC_DST;
+      goto out;
+    }
+
+  memset (a, 0, sizeof (*a));
+
+  /* ip addresses sent in network byte order */
+  clib_memcpy (&(a->src), mp->src_address, 4);
+  clib_memcpy (&(a->dst), mp->dst_address, 4);
+  a->is_add = mp->is_add;
+  a->lsa = ntohl (mp->local_sa_id);
+  a->rsa = ntohl (mp->remote_sa_id);
+
+  rv = vnet_ipsec_gre_add_del_tunnel (a, &sw_if_index);
+
+out:
+    /* *INDENT-OFF* */
+    REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
+    ({
+        rmp->sw_if_index = ntohl (sw_if_index);
+    }));
+    /* *INDENT-ON* */
+}
+
+static void send_ipsec_gre_tunnel_details
+  (ipsec_gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
+{
+  vl_api_ipsec_gre_tunnel_details_t *rmp;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_IPSEC_GRE_TUNNEL_DETAILS);
+  clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
+  clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
+  rmp->sw_if_index = htonl (t->sw_if_index);
+  rmp->local_sa_id = htonl (t->local_sa_id);
+  rmp->remote_sa_id = htonl (t->remote_sa_id);
+  rmp->context = context;
+
+  vl_msg_api_send_shmem (q, (u8 *) & rmp);
+}
+
+static void vl_api_ipsec_gre_tunnel_dump_t_handler
+  (vl_api_ipsec_gre_tunnel_dump_t * mp)
+{
+  unix_shared_memory_queue_t *q;
+  ipsec_gre_main_t *igm = &ipsec_gre_main;
+  ipsec_gre_tunnel_t *t;
+  u32 sw_if_index;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    {
+      return;
+    }
+
+  sw_if_index = ntohl (mp->sw_if_index);
+
+  if (~0 == sw_if_index)
+    {
+        /* *INDENT-OFF* */
+        pool_foreach (t, igm->tunnels,
+        ({
+            send_ipsec_gre_tunnel_details(t, q, mp->context);
+        }));
+        /* *INDENT-ON* */
+    }
+  else
+    {
+      if ((sw_if_index >= vec_len (igm->tunnel_index_by_sw_if_index)) ||
+         (~0 == igm->tunnel_index_by_sw_if_index[sw_if_index]))
+       {
+         return;
+       }
+      t = &igm->tunnels[igm->tunnel_index_by_sw_if_index[sw_if_index]];
+      send_ipsec_gre_tunnel_details (t, q, mp->context);
+    }
+}
+
 #define BOUNCE_HANDLER(nn)                                              \
 static void vl_api_##nn##_t_handler (                                   \
     vl_api_##nn##_t *mp)                                                \