A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / map / map.h
index d38d7f4..b76891b 100644 (file)
 #include <vnet/vnet.h>
 #include <vnet/ip/ip.h>
 #include <vlib/vlib.h>
+#include <vnet/fib/fib_types.h>
+#include <vnet/fib/ip4_fib.h>
+#include <vnet/adj/adj.h>
+#include <vnet/map/map_dpo.h>
+#include <vnet/dpo/load_balance.h>
 
 #define MAP_SKIP_IP6_LOOKUP 1
 
-typedef enum {
+typedef enum
+{
   MAP_SENDER,
   MAP_RECEIVER
 } map_dir_e;
 
-int map_create_domain(ip4_address_t *ip4_prefix, u8 ip4_prefix_len,
-                     ip6_address_t *ip6_prefix, u8 ip6_prefix_len,
-                     ip6_address_t *ip6_src, u8 ip6_src_len,
-                     u8 ea_bits_len, u8 psid_offset, u8 psid_length,
-                     u32 *map_domain_index, u16 mtu, u8 flags);
-int 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);
-u8 *format_map_trace(u8 *s, va_list *args);
-i32 ip4_get_port(ip4_header_t *ip, map_dir_e dir, u16 buffer_len);
-i32 ip6_get_port(ip6_header_t *ip6, map_dir_e dir, u16 buffer_len);
-u16 ip4_map_get_port (ip4_header_t *ip, map_dir_e dir);
-
-typedef enum __attribute__ ((__packed__)) {
-  MAP_DOMAIN_PREFIX        = 1 << 0,
-  MAP_DOMAIN_TRANSLATION   = 1 << 1, // The domain uses MAP-T
+int map_create_domain (ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
+                      ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
+                      ip6_address_t * ip6_src, u8 ip6_src_len,
+                      u8 ea_bits_len, u8 psid_offset, u8 psid_length,
+                      u32 * map_domain_index, u16 mtu, u8 flags);
+int 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);
+u8 *format_map_trace (u8 * s, va_list * args);
+i32 ip4_get_port (ip4_header_t * ip, map_dir_e dir, u16 buffer_len);
+i32 ip6_get_port (ip6_header_t * ip6, map_dir_e dir, u16 buffer_len);
+u16 ip4_map_get_port (ip4_header_t * ip, map_dir_e dir);
+
+typedef enum __attribute__ ((__packed__))
+{
+  MAP_DOMAIN_PREFIX = 1 << 0, MAP_DOMAIN_TRANSLATION = 1 << 1, // The domain uses MAP-T
 } map_domain_flags_e;
 
 /**
@@ -55,16 +62,16 @@ typedef enum __attribute__ ((__packed__)) {
  * In case no structure can be allocated, the fragment is dropped.
  */
 
-#define MAP_IP4_REASS_LIFETIME_DEFAULT (100) /* ms */
+#define MAP_IP4_REASS_LIFETIME_DEFAULT (100)   /* ms */
 #define MAP_IP4_REASS_HT_RATIO_DEFAULT (1.0)
-#define MAP_IP4_REASS_POOL_SIZE_DEFAULT 1024 // Number of reassembly structures
+#define MAP_IP4_REASS_POOL_SIZE_DEFAULT 1024   // Number of reassembly structures
 #define MAP_IP4_REASS_BUFFERS_DEFAULT 2048
 
-#define MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY 5    // Number of fragment per reassembly
+#define MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY 5   // Number of fragment per reassembly
 
-#define MAP_IP6_REASS_LIFETIME_DEFAULT (100) /* ms */
+#define MAP_IP6_REASS_LIFETIME_DEFAULT (100)   /* ms */
 #define MAP_IP6_REASS_HT_RATIO_DEFAULT (1.0)
-#define MAP_IP6_REASS_POOL_SIZE_DEFAULT 1024 // Number of reassembly structures
+#define MAP_IP6_REASS_POOL_SIZE_DEFAULT 1024   // Number of reassembly structures
 #define MAP_IP6_REASS_BUFFERS_DEFAULT 2048
 
 #define MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY 5
@@ -78,7 +85,8 @@ typedef enum __attribute__ ((__packed__)) {
  * This structure _MUST_ be no larger than a single cache line (64 bytes).
  * If more space is needed make a union of ip6_prefix and *rules, those are mutually exclusive.
  */
-typedef struct {
+typedef struct
+{
   ip6_address_t ip6_src;
   ip6_address_t ip6_prefix;
   ip6_address_t *rules;
@@ -102,11 +110,15 @@ typedef struct {
   u8 ip4_prefix_len;
 } map_domain_t;
 
+_Static_assert ((sizeof (map_domain_t) <= CLIB_CACHE_LINE_BYTES),
+               "MAP domain fits in one cacheline");
+
 #define MAP_REASS_INDEX_NONE ((u16)0xffff)
 
 /*
  * Hash key, padded out to 16 bytes for fast compare
  */
+/* *INDENT-OFF* */
 typedef union {
   CLIB_PACKED (struct {
     ip4_address_t src;
@@ -117,8 +129,10 @@ typedef union {
   u64 as_u64[2];
   u32 as_u32[4];
 } map_ip4_reass_key_t;
+/* *INDENT-ON* */
 
-typedef struct {
+typedef struct
+{
   map_ip4_reass_key_t key;
   f64 ts;
 #ifdef MAP_IP4_REASS_COUNT_BYTES
@@ -136,7 +150,8 @@ typedef struct {
 /*
  * MAP domain counters
  */
-typedef enum {
+typedef enum
+{
   /* Simple counters */
   MAP_DOMAIN_IPV4_FRAGMENT = 0,
   /* Combined counters */
@@ -148,6 +163,7 @@ typedef enum {
 /*
  * main_main_t
  */
+/* *INDENT-OFF* */
 typedef union {
   CLIB_PACKED (struct {
     ip6_address_t src;
@@ -158,6 +174,7 @@ typedef union {
   u64 as_u64[5];
   u32 as_u32[10];
 } map_ip6_reass_key_t;
+/* *INDENT-OFF* */
 
 typedef struct {
   u32 pi; //Cached packet or ~0
@@ -296,18 +313,18 @@ typedef struct {
 
 map_main_t map_main;
 
-vlib_node_registration_t ip4_map_node;
-vlib_node_registration_t ip6_map_node;
+extern vlib_node_registration_t ip4_map_node;
+extern vlib_node_registration_t ip6_map_node;
 
-vlib_node_registration_t ip4_map_t_node;
-vlib_node_registration_t ip4_map_t_fragmented_node;
-vlib_node_registration_t ip4_map_t_tcp_udp_node;
-vlib_node_registration_t ip4_map_t_icmp_node;
+extern vlib_node_registration_t ip4_map_t_node;
+extern vlib_node_registration_t ip4_map_t_fragmented_node;
+extern vlib_node_registration_t ip4_map_t_tcp_udp_node;
+extern vlib_node_registration_t ip4_map_t_icmp_node;
 
-vlib_node_registration_t ip6_map_t_node;
-vlib_node_registration_t ip6_map_t_fragmented_node;
-vlib_node_registration_t ip6_map_t_tcp_udp_node;
-vlib_node_registration_t ip6_map_t_icmp_node;
+extern vlib_node_registration_t ip6_map_t_node;
+extern vlib_node_registration_t ip6_map_t_fragmented_node;
+extern vlib_node_registration_t ip6_map_t_tcp_udp_node;
+extern vlib_node_registration_t ip6_map_t_icmp_node;
 
 /*
  * map_get_pfx
@@ -349,7 +366,7 @@ map_get_sfx (map_domain_t *d, u32 addr, u16 port)
 
   /* IPv4 prefix */
   if (d->flags & MAP_DOMAIN_PREFIX)
-    return (u64) (addr & ~d->suffix_mask) << 16;
+    return (u64) (addr & (0xFFFFFFFF << d->suffix_shift)) << 16;
 
   /* Shared or full IPv4 address */
   return ((u64) addr << 16) | psid;
@@ -372,16 +389,17 @@ map_get_ip4 (ip6_address_t *addr)
  * Get the MAP domain from an IPv4 lookup adjacency.
  */
 static_always_inline map_domain_t *
-ip4_map_get_domain (u32 adj_index, u32 *map_domain_index)
+ip4_map_get_domain (u32 mdi,
+                   u32 *map_domain_index)
 {
   map_main_t *mm = &map_main;
-  ip_lookup_main_t *lm = &ip4_main.lookup_main;
-  ip_adjacency_t *adj = ip_get_adjacency(lm, adj_index);
-  ASSERT(adj);
-  uword *p = (uword *)adj->rewrite_data;
-  ASSERT(p);
-  *map_domain_index = p[0];
-  return pool_elt_at_index(mm->domains, p[0]);
+  map_dpo_t *md;
+
+  md = map_dpo_get(mdi);
+
+  ASSERT(md);
+  *map_domain_index = md->md_domain;
+  return pool_elt_at_index(mm->domains, *map_domain_index);
 }
 
 /*
@@ -390,36 +408,34 @@ ip4_map_get_domain (u32 adj_index, u32 *map_domain_index)
  * The IPv4 address is used otherwise.
  */
 static_always_inline map_domain_t *
-ip6_map_get_domain (u32 adj_index, ip4_address_t *addr,
+ip6_map_get_domain (u32 mdi, ip4_address_t *addr,
                     u32 *map_domain_index, u8 *error)
 {
   map_main_t *mm = &map_main;
-  ip4_main_t *im4 = &ip4_main;
-  ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
+  map_dpo_t *md;
 
   /*
    * Disable direct MAP domain lookup on decap, until the security check is updated to verify IPv4 SA.
    * (That's done implicitly when MAP domain is looked up in the IPv4 FIB)
    */
 #ifdef MAP_NONSHARED_DOMAIN_ENABLED
-  ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
-  ip_adjacency_t *adj = ip_get_adjacency(lm6, adj_index);
-  ASSERT(adj);
-  uword *p = (uword *)adj->rewrite_data;
-  ASSERT(p);
-  *map_domain_index = p[0];
-  if (p[0] != ~0)
-    return pool_elt_at_index(mm->domains, p[0]);
-#endif
+  md = map_dpo_get(mdi);
 
-  u32 ai = ip4_fib_lookup_with_table(im4, 0, addr, 0);
-  ip_adjacency_t *adj4 = ip_get_adjacency (lm4, ai);
-  if (PREDICT_TRUE(adj4->lookup_next_index == IP_LOOKUP_NEXT_MAP ||
-                  adj4->lookup_next_index == IP_LOOKUP_NEXT_MAP_T)) {
-    uword *p = (uword *)adj4->rewrite_data;
-    *map_domain_index = p[0];
+  ASSERT(md);
+  *map_domain_index = md->md_domain;
+  if (*map_domain_index != ~0)
     return pool_elt_at_index(mm->domains, *map_domain_index);
-  }
+#endif
+
+  u32 lbi = ip4_fib_forwarding_lookup(0, addr);
+  const dpo_id_t *dpo = load_balance_get_bucket(lbi, 0);
+  if (PREDICT_TRUE(dpo->dpoi_type == map_dpo_type ||
+                  dpo->dpoi_type == map_t_dpo_type))
+    {
+      md = map_dpo_get(dpo->dpoi_index);
+     *map_domain_index = md->md_domain;
+      return pool_elt_at_index(mm->domains, *map_domain_index);
+    }
   *error = MAP_ERROR_NO_DOMAIN;
   return NULL;
 }
@@ -565,3 +581,11 @@ map_send_all_to_node(vlib_main_t *vm, u32 *pi_vector,
     vlib_put_next_frame(vm, node, next_index, n_left_to_next);
   }
 }
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */