vlib: address vlib_error_t scaling issue
[vpp.git] / src / plugins / map / map.c
index 8156ec8..2f036da 100644 (file)
@@ -16,9 +16,9 @@
  */
 
 #include <vnet/fib/fib_table.h>
+#include <vnet/fib/fib_entry_track.h>
 #include <vnet/fib/ip6_fib.h>
 #include <vnet/adj/adj.h>
-#include <map/map_dpo.h>
 #include <vppinfra/crc32.h>
 #include <vnet/plugin/plugin.h>
 #include <vpp/app/version.h>
@@ -60,6 +60,49 @@ map_main_t map_main;
  */
 
 
+/*
+ * Save user-assigned MAP domain names ("tags") in a vector of
+ * extra domain information.
+ */
+static void
+map_save_extras (u32 map_domain_index, u8 * tag)
+{
+  map_main_t *mm = &map_main;
+  map_domain_extra_t *de;
+
+  if (map_domain_index == ~0)
+    return;
+
+  vec_validate (mm->domain_extras, map_domain_index);
+  de = vec_elt_at_index (mm->domain_extras, map_domain_index);
+  clib_memset (de, 0, sizeof (*de));
+
+  if (!tag)
+    return;
+
+  de->tag = vec_dup (tag);
+}
+
+
+static void
+map_free_extras (u32 map_domain_index)
+{
+  map_main_t *mm = &map_main;
+  map_domain_extra_t *de;
+  u8 *tag;
+
+  if (map_domain_index == ~0)
+    return;
+
+  de = vec_elt_at_index (mm->domain_extras, map_domain_index);
+  tag = de->tag;
+  if (!tag)
+    return;
+
+  vec_free (tag);
+  de->tag = 0;
+}
+
 
 int
 map_create_domain (ip4_address_t * ip4_prefix,
@@ -70,34 +113,13 @@ map_create_domain (ip4_address_t * ip4_prefix,
                   u8 ip6_src_len,
                   u8 ea_bits_len,
                   u8 psid_offset,
-                  u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
+                  u8 psid_length,
+                  u32 * map_domain_index, u16 mtu, u8 flags, u8 * tag)
 {
   u8 suffix_len, suffix_shift;
   map_main_t *mm = &map_main;
-  dpo_id_t dpo_v4 = DPO_INVALID;
-  dpo_id_t dpo_v6 = DPO_INVALID;
   map_domain_t *d;
 
-  /* Sanity check on the src prefix length */
-  if (flags & MAP_DOMAIN_TRANSLATION)
-    {
-      if (ip6_src_len != 96 && ip6_src_len != 64)
-       {
-         clib_warning ("MAP-T only supports prefix lengths of 64 and 96.");
-         return -1;
-       }
-    }
-  else
-    {
-      if (ip6_src_len != 128)
-       {
-         clib_warning
-           ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
-            "be 128).");
-         return -1;
-       }
-    }
-
   /* How many, and which bits to grab from the IPv4 DA */
   if (ip4_prefix_len + ea_bits_len < 32)
     {
@@ -145,53 +167,17 @@ map_create_domain (ip4_address_t * ip4_prefix,
   d->psid_mask = (1 << d->psid_length) - 1;
   d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
 
-  /* MAP data-plane object */
-  if (d->flags & MAP_DOMAIN_TRANSLATION)
-    map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
-  else
-    map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
-
-  /* Create ip4 route */
-  fib_prefix_t pfx = {
-    .fp_proto = FIB_PROTOCOL_IP4,
-    .fp_len = d->ip4_prefix_len,
-    .fp_addr = {
-               .ip4 = d->ip4_prefix,
-               }
-    ,
-  };
-  fib_table_entry_special_dpo_add (0, &pfx,
-                                  FIB_SOURCE_MAP,
-                                  FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
-  dpo_reset (&dpo_v4);
-
-  /*
-   * construct a DPO to use the v6 domain
-   */
-  if (d->flags & MAP_DOMAIN_TRANSLATION)
-    map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
-  else
-    map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
+  /* Save a user-assigned MAP domain name if provided. */
+  if (tag)
+    map_save_extras (*map_domain_index, tag);
 
-  /*
-   * Multiple MAP domains may share same source IPv6 TEP. Which is just dandy.
-   * We are not tracking the sharing. So a v4 lookup to find the correct
-   * domain post decap/trnaslate is always done
-   *
-   * Create ip6 route. This is a reference counted add. If the prefix
-   * already exists and is MAP sourced, it is now MAP source n+1 times
-   * and will need to be removed n+1 times.
-   */
-  fib_prefix_t pfx6 = {
-    .fp_proto = FIB_PROTOCOL_IP6,
-    .fp_len = d->ip6_src_len,
-    .fp_addr.ip6 = d->ip6_src,
-  };
+  /* MAP longest match lookup table (input feature / FIB) */
+  mm->ip4_prefix_tbl->add (mm->ip4_prefix_tbl, &d->ip4_prefix,
+                          d->ip4_prefix_len, *map_domain_index);
 
-  fib_table_entry_special_dpo_add (0, &pfx6,
-                                  FIB_SOURCE_MAP,
-                                  FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
-  dpo_reset (&dpo_v6);
+  /* Really needed? Or always use FIB? */
+  mm->ip6_src_prefix_tbl->add (mm->ip6_src_prefix_tbl, &d->ip6_src,
+                              d->ip6_src_len, *map_domain_index);
 
   /* Validate packet/byte counters */
   map_domain_counter_lock (mm);
@@ -231,26 +217,13 @@ map_delete_domain (u32 map_domain_index)
     }
 
   d = pool_elt_at_index (mm->domains, map_domain_index);
+  mm->ip4_prefix_tbl->delete (mm->ip4_prefix_tbl, &d->ip4_prefix,
+                             d->ip4_prefix_len);
+  mm->ip6_src_prefix_tbl->delete (mm->ip6_src_prefix_tbl, &d->ip6_src,
+                                 d->ip6_src_len);
 
-  fib_prefix_t pfx = {
-    .fp_proto = FIB_PROTOCOL_IP4,
-    .fp_len = d->ip4_prefix_len,
-    .fp_addr = {
-               .ip4 = d->ip4_prefix,
-               }
-    ,
-  };
-  fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
-
-  fib_prefix_t pfx6 = {
-    .fp_proto = FIB_PROTOCOL_IP6,
-    .fp_len = d->ip6_src_len,
-    .fp_addr = {
-               .ip6 = d->ip6_src,
-               }
-    ,
-  };
-  fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
+  /* Release user-assigned MAP domain name. */
+  map_free_extras (map_domain_index);
 
   /* Deleting rules */
   if (d->rules)
@@ -263,7 +236,7 @@ map_delete_domain (u32 map_domain_index)
 
 int
 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
-                 u8 is_add)
+                 bool is_add)
 {
   map_domain_t *d;
   map_main_t *mm = &map_main;
@@ -308,7 +281,7 @@ map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
 
 #ifdef MAP_SKIP_IP6_LOOKUP
 /**
- * Pre-resolvd per-protocol global next-hops
+ * Pre-resolved per-protocol global next-hops
  */
 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
 
@@ -413,10 +386,8 @@ map_fib_resolve (map_main_pre_resolved_t * pr,
     .fp_addr = *addr,
   };
 
-  pr->fei = fib_table_entry_special_add (0,    // default fib
-                                        &pfx,
-                                        FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
-  pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
+  pr->fei = fib_entry_track (0,        // default fib
+                            &pfx, FIB_NODE_TYPE_MAP_E, proto, &pr->sibling);
   map_stack (pr);
 }
 
@@ -424,24 +395,19 @@ static void
 map_fib_unresolve (map_main_pre_resolved_t * pr,
                   fib_protocol_t proto, u8 len, const ip46_address_t * addr)
 {
-  fib_prefix_t pfx = {
-    .fp_proto = proto,
-    .fp_len = len,
-    .fp_addr = *addr,
-  };
-
-  fib_entry_child_remove (pr->fei, pr->sibling);
+  if (pr->fei != FIB_NODE_INDEX_INVALID)
+    {
+      fib_entry_untrack (pr->fei, pr->sibling);
 
-  fib_table_entry_special_remove (0,   // default fib
-                                 &pfx, FIB_SOURCE_RR);
-  dpo_reset (&pr->dpo);
+      dpo_reset (&pr->dpo);
 
-  pr->fei = FIB_NODE_INDEX_INVALID;
-  pr->sibling = FIB_NODE_INDEX_INVALID;
+      pr->fei = FIB_NODE_INDEX_INVALID;
+      pr->sibling = FIB_NODE_INDEX_INVALID;
+    }
 }
 
 void
-map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, int is_del)
+map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, bool is_del)
 {
   if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
     {
@@ -552,6 +518,7 @@ map_add_domain_command_fn (vlib_main_t * vm,
   u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
   u32 mtu = 0;
   u8 flags = 0;
+  u8 *tag = 0;
   ip6_src_len = 128;
   clib_error_t *error = NULL;
 
@@ -587,10 +554,8 @@ map_add_domain_command_fn (vlib_main_t * vm,
        num_m_args++;
       else if (unformat (line_input, "mtu %d", &mtu))
        num_m_args++;
-      else if (unformat (line_input, "map-t"))
-       flags |= MAP_DOMAIN_TRANSLATION;
-      else if (unformat (line_input, "rfc6052"))
-       flags |= (MAP_DOMAIN_TRANSLATION | MAP_DOMAIN_RFC6052);
+      else if (unformat (line_input, "tag %v", &tag))
+       ;
       else
        {
          error = clib_error_return (0, "unknown input `%U'",
@@ -608,7 +573,7 @@ map_add_domain_command_fn (vlib_main_t * vm,
   map_create_domain (&ip4_prefix, ip4_prefix_len,
                     &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
                     ea_bits_len, psid_offset, psid_length, &map_domain_index,
-                    mtu, flags);
+                    mtu, flags, tag);
 
 done:
   unformat_free (line_input);
@@ -714,7 +679,7 @@ map_pre_resolve_command_fn (vlib_main_t * vm,
   ip4_address_t ip4nh, *p_v4 = NULL;
   ip6_address_t ip6nh, *p_v6 = NULL;
   clib_error_t *error = NULL;
-  int is_del = 0;
+  bool is_del = false;
 
   clib_memset (&ip4nh, 0, sizeof (ip4nh));
   clib_memset (&ip6nh, 0, sizeof (ip6nh));
@@ -731,7 +696,7 @@ map_pre_resolve_command_fn (vlib_main_t * vm,
        if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
        p_v6 = &ip6nh;
       else if (unformat (line_input, "del"))
-       is_del = 1;
+       is_del = true;
       else
        {
          error = clib_error_return (0, "unknown input `%U'",
@@ -938,12 +903,8 @@ done:
 static char *
 map_flags_to_string (u32 flags)
 {
-  if (flags & MAP_DOMAIN_RFC6052)
-    return "rfc6052";
   if (flags & MAP_DOMAIN_PREFIX)
     return "prefix";
-  if (flags & MAP_DOMAIN_TRANSLATION)
-    return "map-t";
   return "";
 }
 
@@ -954,16 +915,20 @@ format_map_domain (u8 * s, va_list * args)
   bool counters = va_arg (*args, int);
   map_main_t *mm = &map_main;
   ip6_address_t ip6_prefix;
+  u32 map_domain_index = d - mm->domains;
+  map_domain_extra_t *de;
 
   if (d->rules)
     clib_memset (&ip6_prefix, 0, sizeof (ip6_prefix));
   else
     ip6_prefix = d->ip6_prefix;
 
+  de = vec_elt_at_index (mm->domain_extras, map_domain_index);
+
   s = format (s,
-             "[%d] ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d ea-bits-len %d "
-             "psid-offset %d psid-len %d mtu %d %s",
-             d - mm->domains,
+             "[%d] tag {%v} ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d "
+             "ea-bits-len %d psid-offset %d psid-len %d mtu %d %s",
+             map_domain_index, de->tag,
              format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
              format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
              format_ip6_address, &d->ip6_src, d->ip6_src_len,
@@ -975,10 +940,10 @@ format_map_domain (u8 * s, va_list * args)
       map_domain_counter_lock (mm);
       vlib_counter_t v;
       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
-                                d - mm->domains, &v);
+                                map_domain_index, &v);
       s = format (s, "  TX: %lld/%lld", v.packets, v.bytes);
       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
-                                d - mm->domains, &v);
+                                map_domain_index, &v);
       s = format (s, "  RX: %lld/%lld", v.packets, v.bytes);
       map_domain_counter_unlock (mm);
     }
@@ -1070,9 +1035,10 @@ show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
 
   if (map_domain_index == ~0)
     {
-    /* *INDENT-OFF* */
-    pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
-    /* *INDENT-ON* */
+      /* *INDENT-OFF* */
+      pool_foreach(d, mm->domains,
+       ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
+      /* *INDENT-ON* */
     }
   else
     {
@@ -1120,7 +1086,7 @@ map_error_counter_get (u32 node_index, map_error_t map_error)
   vlib_node_t *n = vlib_get_node (vm, node_index);
   u32 ci;
 
-  ci = vlib_error_get_code (e);
+  ci = vlib_error_get_code (&vm->node_main, e);
   ASSERT (ci < n->n_errors);
   ci += n->error_heap_index;
 
@@ -1169,6 +1135,9 @@ show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
   else
     vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
 
+  if (mm->tcp_mss)
+    vlib_cli_output (vm, "MAP TCP MSS clamping: %u", mm->tcp_mss);
+
   vlib_cli_output (vm,
                   "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
                   mm->sec_check ? "enabled" : "disabled",
@@ -1329,6 +1298,56 @@ map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
 }
 
 
+static clib_error_t *
+map_if_command_fn (vlib_main_t * vm,
+                  unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  unformat_input_t _line_input, *line_input = &_line_input;
+  clib_error_t *error = NULL;
+  bool is_enable = true, is_translation = false;
+  vnet_main_t *vnm = vnet_get_main ();
+  u32 sw_if_index = ~0;
+
+  /* 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_vnet_sw_interface, vnm, &sw_if_index))
+       ;
+      else if (unformat (line_input, "del"))
+       is_enable = false;
+      else if (unformat (line_input, "map-t"))
+       is_translation = true;
+      else
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
+    }
+
+done:
+  unformat_free (line_input);
+
+  if (sw_if_index == ~0)
+    {
+      error = clib_error_return (0, "unknown interface");
+      return error;
+    }
+
+  int rv = map_if_enable_disable (is_enable, sw_if_index, is_translation);
+  if (rv)
+    {
+      error = clib_error_return (0, "failure enabling MAP on interface");
+    }
+
+  return error;
+}
+
+
 /*
  * packet trace format function
  */
@@ -1913,6 +1932,45 @@ map_ip6_reass_conf_buffers (u32 buffers)
   return 0;
 }
 
+static clib_error_t *
+map_tcp_mss_command_fn (vlib_main_t * vm,
+                       unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  unformat_input_t _line_input, *line_input = &_line_input;
+  clib_error_t *error = NULL;
+  u32 tcp_mss = 0;
+
+  /* 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", &tcp_mss))
+       ;
+      else
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
+    }
+
+  if (tcp_mss >= (0x1 << 16))
+    {
+      error = clib_error_return (0, "invalid value `%u'", tcp_mss);
+      goto done;
+    }
+
+  map_param_set_tcp (tcp_mss);
+
+done:
+  unformat_free (line_input);
+
+  return error;
+}
+
+
 /* *INDENT-OFF* */
 
 /*?
@@ -1948,6 +2006,22 @@ VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
   .function = map_traffic_class_command_fn,
 };
 
+/*?
+ * TCP MSS clamping
+ *
+ * @cliexpar
+ * @cliexstart{map params tcp-mss}
+ *
+ * This command is used to set the TCP MSS in translated
+ * or encapsulated packets.
+ * @cliexend
+ ?*/
+VLIB_CLI_COMMAND(map_tcp_mss_command, static) = {
+  .path = "map params tcp-mss",
+  .short_help = "map params tcp-mss <value>",
+  .function = map_tcp_mss_command_fn,
+};
+
 /*?
  * Bypass IP4/IP6 lookup
  *
@@ -2053,7 +2127,8 @@ VLIB_CLI_COMMAND(map_fragment_command, static) = {
  ?*/
 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
   .path = "map add domain",
-  .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
+  .short_help = "map add domain [tag <tag>] ip4-pfx <ip4-pfx> "
+      "ip6-pfx <ip6-pfx> "
       "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
       "[map-t] [mtu <mtu>]",
   .function = map_add_domain_command_fn,
@@ -2124,9 +2199,19 @@ VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
   .function = show_map_fragments_command_fn,
 };
 
+/*?
+ * Enable MAP processing on interface (input feature)
+ *
+ ?*/
+VLIB_CLI_COMMAND(map_if_command, static) = {
+  .path = "map interface",
+  .short_help = "map interface <interface-name> [map-t] [del]",
+  .function = map_if_command_fn,
+};
+
 VLIB_PLUGIN_REGISTER() = {
   .version = VPP_BUILD_VER,
-  .description = "Mapping of address and port (MAP)",
+  .description = "Mapping of Address and Port (MAP)",
 };
 
 /* *INDENT-ON* */
@@ -2139,6 +2224,9 @@ map_init (vlib_main_t * vm)
 {
   map_main_t *mm = &map_main;
   clib_error_t *error = 0;
+
+  memset (mm, 0, sizeof (*mm));
+
   mm->vnet_main = vnet_get_main ();
   mm->vlib_main = vm;
 
@@ -2172,6 +2260,7 @@ map_init (vlib_main_t * vm)
 
   vlib_validate_simple_counter (&mm->icmp_relayed, 0);
   vlib_zero_simple_counter (&mm->icmp_relayed, 0);
+  mm->icmp_relayed.stat_segment_name = "/map/icmp-relayed";
 
   /* IP4 virtual reassembly */
   mm->ip4_reass_hash_table = 0;
@@ -2208,7 +2297,14 @@ map_init (vlib_main_t * vm)
 #ifdef MAP_SKIP_IP6_LOOKUP
   fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
 #endif
-  map_dpo_module_init ();
+
+  /* LPM lookup tables */
+  mm->ip4_prefix_tbl = lpm_table_init (LPM_TYPE_KEY32);
+  mm->ip6_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
+  mm->ip6_src_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
+
+  mm->bm_trans_enabled_by_sw_if = 0;
+  mm->bm_encap_enabled_by_sw_if = 0;
 
   error = map_plugin_api_hookup (vm);