vlib: clean up r2 plugin registration relocator
[vpp.git] / src / plugins / map / map_api.c
index 90dbd1b..1dbff4c 100644 (file)
  *------------------------------------------------------------------
  */
 
+#include <vnet/ip/ip_types_api.h>
 #include <map/map.h>
-#include <map/map_msg_enum.h>
+#include <map/map.api_enum.h>
+#include <map/map.api_types.h>
 #include <vnet/ip/ip.h>
+#include <vnet/ip/reass/ip4_sv_reass.h>
+#include <vnet/ip/reass/ip6_sv_reass.h>
+#include <vnet/ip/reass/ip6_full_reass.h>
 #include <vnet/fib/fib_table.h>
 #include <vlibmemory/api.h>
 
-#define vl_typedefs            /* define message structures */
-#include <map/map_all_api_h.h>
-#undef vl_typedefs
-
-#define vl_endianfun           /* define message structures */
-#include <map/map_all_api_h.h>
-#undef vl_endianfun
-
-/* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-#define vl_printfun
-#include <map/map_all_api_h.h>
-#undef vl_printfun
-
-/* Get the API version number */
-#define vl_api_version(n,v) static u32 api_version=(v);
-#include <map/map_all_api_h.h>
-#undef vl_api_version
-
 #define REPLY_MSG_ID_BASE mm->msg_id_base
 #include <vlibapi/api_helper_macros.h>
 
@@ -54,26 +40,21 @@ vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
   u32 index;
   u8 flags = 0;
 
-  if (mp->is_translation)
-    flags |= MAP_DOMAIN_TRANSLATION;
-
-  if (mp->is_rfc6052)
-    flags |= MAP_DOMAIN_RFC6052;
-
+  mp->tag[ARRAY_LEN (mp->tag) - 1] = '\0';
   rv =
-    map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
-                      (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
-                      (ip6_address_t *) & mp->ip6_src,
-                      mp->ip6_src_prefix_len, mp->ea_bits_len,
-                      mp->psid_offset, mp->psid_length, &index,
-                      ntohs (mp->mtu), flags);
-
-  /* *INDENT-OFF* */
-  REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
+    map_create_domain ((ip4_address_t *) & mp->ip4_prefix.address,
+                      mp->ip4_prefix.len,
+                      (ip6_address_t *) & mp->ip6_prefix.address,
+                      mp->ip6_prefix.len,
+                      (ip6_address_t *) & mp->ip6_src.address,
+                      mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
+                      mp->psid_length, &index, mp->mtu, flags, mp->tag);
+
+  REPLY_MACRO2_END(VL_API_MAP_ADD_DOMAIN_REPLY,
   ({
-    rmp->index = ntohl(index);
+    rmp->index = index;
   }));
-  /* *INDENT-ON* */
+
 }
 
 static void
@@ -97,17 +78,51 @@ vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
 
   rv =
     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
-                     (ip6_address_t *) mp->ip6_dst, mp->is_add);
+                     (ip6_address_t *) mp->ip6_dst, mp->is_add);
 
   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
 }
 
 static void
-vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
+send_domain_details (u32 map_domain_index, vl_api_registration_t * rp,
+                    u32 context)
 {
+  map_main_t *mm = &map_main;
   vl_api_map_domain_details_t *rmp;
+  map_domain_t *d = pool_elt_at_index (mm->domains, map_domain_index);
+
+  /* Make sure every field is initiated (or don't skip the clib_memset()) */
+  map_domain_extra_t *de =
+    vec_elt_at_index (mm->domain_extras, map_domain_index);
+  int tag_len = clib_min (ARRAY_LEN (rmp->tag), vec_len (de->tag) + 1);
+
+  REPLY_MACRO_DETAILS4(VL_API_MAP_DOMAIN_DETAILS, rp, context,
+  ({
+    rmp->domain_index = htonl (map_domain_index);
+    clib_memcpy (&rmp->ip6_prefix.address, &d->ip6_prefix,
+                sizeof (rmp->ip6_prefix.address));
+    clib_memcpy (&rmp->ip4_prefix.address, &d->ip4_prefix,
+                sizeof (rmp->ip4_prefix.address));
+    clib_memcpy (&rmp->ip6_src.address, &d->ip6_src,
+                sizeof (rmp->ip6_src.address));
+    rmp->ip6_prefix.len = d->ip6_prefix_len;
+    rmp->ip4_prefix.len = d->ip4_prefix_len;
+    rmp->ip6_src.len = d->ip6_src_len;
+    rmp->ea_bits_len = d->ea_bits_len;
+    rmp->psid_offset = d->psid_offset;
+    rmp->psid_length = d->psid_length;
+    rmp->flags = d->flags;
+    rmp->mtu = htons (d->mtu);
+    memcpy (rmp->tag, de->tag, tag_len - 1);
+    rmp->tag[tag_len - 1] = '\0';
+  }));
+}
+
+static void
+vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
+{
   map_main_t *mm = &map_main;
-  map_domain_t *d;
+  int i;
   vl_api_registration_t *reg;
 
   if (pool_elts (mm->domains) == 0)
@@ -117,30 +132,24 @@ vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
   if (!reg)
     return;
 
-  /* *INDENT-OFF* */
-  pool_foreach(d, mm->domains,
-  ({
-    /* Make sure every field is initiated (or don't skip the memset()) */
-    rmp = vl_msg_api_alloc (sizeof (*rmp));
-    rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
-    rmp->context = mp->context;
-    rmp->domain_index = htonl(d - mm->domains);
-    clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
-    clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
-    clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
-    rmp->ip6_prefix_len = d->ip6_prefix_len;
-    rmp->ip4_prefix_len = d->ip4_prefix_len;
-    rmp->ip6_src_len = d->ip6_src_len;
-    rmp->ea_bits_len = d->ea_bits_len;
-    rmp->psid_offset = d->psid_offset;
-    rmp->psid_length = d->psid_length;
-    rmp->flags = d->flags;
-    rmp->mtu = htons(d->mtu);
-    rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION); // Redundant
+  pool_foreach_index (i, mm->domains)
+   {
+    send_domain_details(i, reg, mp->context);
+  }
+}
 
-    vl_api_send_msg (reg, (u8 *) rmp);
+static void
+vl_api_map_domains_get_t_handler (vl_api_map_domains_get_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_domains_get_reply_t *rmp;
+
+  i32 rv = 0;
+
+  REPLY_AND_DETAILS_MACRO (VL_API_MAP_DOMAINS_GET_REPLY, mm->domains,
+  ({
+    send_domain_details (cursor, rp, mp->context);
   }));
-  /* *INDENT-ON* */
 }
 
 static void
@@ -175,10 +184,10 @@ vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
          continue;
        }
       rmp = vl_msg_api_alloc (sizeof (*rmp));
-      memset (rmp, 0, sizeof (*rmp));
+      clib_memset (rmp, 0, sizeof (*rmp));
       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
       rmp->psid = htons (i);
-      clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
+      clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
       rmp->context = mp->context;
       vl_api_send_msg (reg, (u8 *) rmp);
     }
@@ -211,8 +220,8 @@ vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
       goto out;
     }
 
-  memset (total_pkts, 0, sizeof (total_pkts));
-  memset (total_bytes, 0, sizeof (total_bytes));
+  clib_memset (total_pkts, 0, sizeof (total_pkts));
+  clib_memset (total_bytes, 0, sizeof (total_bytes));
 
   map_domain_counter_lock (mm);
   vec_foreach (cm, mm->domain_counters)
@@ -251,54 +260,292 @@ out:
   vl_api_send_msg (reg, (u8 *) rmp);
 }
 
-#define foreach_map_plugin_api_msg             \
-_(MAP_ADD_DOMAIN, map_add_domain)              \
-_(MAP_DEL_DOMAIN, map_del_domain)              \
-_(MAP_ADD_DEL_RULE, map_add_del_rule)          \
-_(MAP_DOMAIN_DUMP, map_domain_dump)            \
-_(MAP_RULE_DUMP, map_rule_dump)                        \
-_(MAP_SUMMARY_STATS, map_summary_stats)
 
-#define vl_msg_name_crc_list
-#include <map/map_all_api_h.h>
-#undef vl_msg_name_crc_list
+int
+map_param_set_fragmentation (bool inner, bool ignore_df)
+{
+  map_main_t *mm = &map_main;
+
+  mm->frag_inner = ! !inner;
+  mm->frag_ignore_df = ! !ignore_df;
+
+  return 0;
+}
+
+static void
+  vl_api_map_param_set_fragmentation_t_handler
+  (vl_api_map_param_set_fragmentation_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_set_fragmentation_reply_t *rmp;
+  int rv = 0;
+
+  rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
+
+  REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
+}
+
+
+int
+map_param_set_icmp (ip4_address_t * icmp_src_address)
+{
+  map_main_t *mm = &map_main;
+
+  if (icmp_src_address == 0)
+    return -1;
+
+  mm->icmp4_src_address = *icmp_src_address;
+
+  return 0;
+}
+
+
+static void
+vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_set_icmp_reply_t *rmp;
+  int rv;
+
+  rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
+
+  REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
+}
+
+
+int
+map_param_set_icmp6 (u8 enable_unreachable)
+{
+  map_main_t *mm = &map_main;
+
+  mm->icmp6_enabled = ! !enable_unreachable;
+
+  return 0;
+}
+
+static void
+vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_set_icmp6_reply_t *rmp;
+  int rv;
+
+  rv = map_param_set_icmp6 (mp->enable_unreachable);
+
+  REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
+}
+
+
+static void
+  vl_api_map_param_add_del_pre_resolve_t_handler
+  (vl_api_map_param_add_del_pre_resolve_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
+  int rv = 0;
+
+  map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
+                  (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
+
+  REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
+}
+
+int
+map_param_set_security_check (bool enable, bool fragments)
+{
+  map_main_t *mm = &map_main;
+
+  mm->sec_check = ! !enable;
+  mm->sec_check_frag = ! !fragments;
+
+  return 0;
+}
+
+static void
+  vl_api_map_param_set_security_check_t_handler
+  (vl_api_map_param_set_security_check_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_set_security_check_reply_t *rmp;
+  int rv;
+
+  rv = map_param_set_security_check (mp->enable, mp->fragments);
+
+  REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
+}
+
+
+int
+map_param_set_traffic_class (bool copy, u8 tc)
+{
+  map_main_t *mm = &map_main;
+
+  mm->tc_copy = ! !copy;
+  mm->tc = tc;
+
+  return 0;
+}
+
+static void
+  vl_api_map_param_set_traffic_class_t_handler
+  (vl_api_map_param_set_traffic_class_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_set_traffic_class_reply_t *rmp;
+  int rv;
+
+  rv = map_param_set_traffic_class (mp->copy, mp->tc_class);
+
+  REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
+}
+
+
+int
+map_param_set_tcp (u16 tcp_mss)
+{
+  map_main_t *mm = &map_main;
+
+  mm->tcp_mss = tcp_mss;
+
+  return 0;
+}
+
+
+static void
+vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_set_tcp_reply_t *rmp;
+  int rv = 0;
+
+  map_param_set_tcp (ntohs (mp->tcp_mss));
+  REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
+}
+
+
+static void
+vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
+{
+  map_main_t *mm = &map_main;
+  vl_api_map_param_get_reply_t *rmp;
+  vl_api_registration_t *reg;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
+  rmp->context = mp->context;
+  rmp->retval = 0;
+
+  rmp->frag_inner = mm->frag_inner;
+  rmp->frag_ignore_df = mm->frag_ignore_df;
+
+  clib_memcpy (&rmp->icmp_ip4_err_relay_src,
+              &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
+
+  rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
+
+  /*
+   * FIXME: How are these addresses re-extracted from the FIB?
+   * Or should a local map_main copy be kept?
+   */
+  clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
+  clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
+
+  rmp->sec_check_enable = mm->sec_check;
+  rmp->sec_check_fragments = mm->sec_check_frag;
+
+  rmp->tc_copy = mm->tc_copy;
+  rmp->tc_class = mm->tc;
+
+  vl_api_send_msg (reg, (u8 *) rmp);
+}
+
+
+int
+map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
+{
+  map_main_t *mm = &map_main;
+
+  if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
+                         sw_if_index))
+    return VNET_API_ERROR_INVALID_SW_IF_INDEX;
+
+  is_enable = ! !is_enable;
+
+  if (is_translation)
+    {
+      if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
+         == is_enable)
+       return 0;
+    }
+  else
+    {
+      if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
+         == is_enable)
+       return 0;
+    }
+
+  if (is_translation == false)
+    {
+      ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
+      ip6_full_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
+      vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
+                                  is_enable ? 1 : 0, 0, 0);
+      vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
+                                  is_enable ? 1 : 0, 0, 0);
+      mm->bm_encap_enabled_by_sw_if =
+       clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
+                        is_enable);
+    }
+  else
+    {
+      ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
+      ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
+      vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
+                                  is_enable ? 1 : 0, 0, 0);
+      vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
+                                  is_enable ? 1 : 0, 0, 0);
+      mm->bm_trans_enabled_by_sw_if =
+       clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
+                        is_enable);
+    }
+
+  return 0;
+}
+
 
 static void
-setup_message_id_table (map_main_t * mm, api_main_t * am)
+vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
 {
-#define _(id,n,crc)                                                    \
-  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
-  foreach_vl_msg_name_crc_map;
-#undef _
+  map_main_t *mm = &map_main;
+  vl_api_map_if_enable_disable_reply_t *rmp;
+  int rv = 0;
+
+  VALIDATE_SW_IF_INDEX (mp);
+
+  rv =
+    map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
+                          mp->is_translation);
+
+  BAD_SW_IF_INDEX_LABEL;
+  REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
 }
 
+/* API definitions */
+#include <vnet/format_fns.h>
+#include <map/map.api.c>
+
 /* Set up the API message handling tables */
 clib_error_t *
 map_plugin_api_hookup (vlib_main_t * vm)
 {
   map_main_t *mm = &map_main;
-  u8 *name = format (0, "map_%08x%c", api_version, 0);
-
-  /* Ask for a correctly-sized block of API message decode slots */
-  mm->msg_id_base =
-    vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),     \
-                           #n,                                 \
-                           vl_api_##n##_t_handler,              \
-                           vl_noop_handler,                     \
-                           vl_api_##n##_t_endian,               \
-                           vl_api_##n##_t_print,                \
-                           sizeof(vl_api_##n##_t), 1);
-  foreach_map_plugin_api_msg;
-#undef _
 
-  /*
-   * Set up the (msg_name, crc, message-id) table
-   */
-  setup_message_id_table (mm, &api_main);
+  mm->msg_id_base = setup_message_id_table ();
 
-  vec_free (name);
   return 0;
 }