fib: doc nitfixes
[vpp.git] / src / vnet / mfib / ip6_mfib.c
index e98ac42..de6cbf3 100644 (file)
 #include <vnet/mfib/mfib_entry.h>
 #include <vnet/fib/ip6_fib.h>
 
-/**
- * The number of bytes in an address/ask key in the radix tree
- * First byte is the length in bytes.
- */
-#define IP6_MFIB_KEY_LEN 33
+ip6_mfib_table_instance_t ip6_mfib_table;
 
 /**
  * Key and mask for radix
  */
-typedef struct ip6_mfib_key_t_
-{
-    u8 key[IP6_MFIB_KEY_LEN];
-    u8 mask[IP6_MFIB_KEY_LEN];
-} ip6_mfib_key_t;
-
-/**
- * An object that is inserted into the radix tree.
- * Since it's in the tree and has pointers, it cannot realloc and so cannot
- * come from a vlib pool.
- */
-typedef struct ip6_mfib_node_t_
-{
-    struct radix_node i6mn_nodes[2];
-    ip6_mfib_key_t i6mn_key;
-    index_t i6mn_entry;
-} ip6_mfib_node_t;
+typedef clib_bihash_kv_40_8_t ip6_mfib_key_t;
 
 static const mfib_prefix_t all_zeros = {
     /* (*,*) */
@@ -163,8 +143,9 @@ ip6_create_mfib_with_table_id (u32 table_id,
         .frp_addr = zero_addr,
         .frp_sw_if_index = 0xffffffff,
         .frp_fib_index = ~0,
-        .frp_weight = 0,
+        .frp_weight = 1,
         .frp_flags = FIB_ROUTE_PATH_LOCAL,
+        .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
     };
 
     pool_get_aligned(ip6_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
@@ -185,11 +166,6 @@ ip6_create_mfib_with_table_id (u32 table_id,
 
     mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP6, src);
 
-    mfib_table->v6.rhead =
-        clib_mem_alloc_aligned (sizeof(*mfib_table->v6.rhead),
-                                CLIB_CACHE_LINE_BYTES);
-    rn_inithead0(mfib_table->v6.rhead, 8);
-
     /*
      * add the special entries into the new FIB
      */
@@ -207,8 +183,7 @@ ip6_create_mfib_with_table_id (u32 table_id,
         mfib_table_entry_path_update(mfib_table->mft_index,
                                      &pfx,
                                      MFIB_SOURCE_SPECIAL,
-                                     &path_for_us,
-                                     MFIB_ITF_FLAG_FORWARD);
+                                     &path_for_us);
     }));
 
     return (mfib_table->mft_index);
@@ -227,7 +202,7 @@ ip6_mfib_table_destroy (ip6_mfib_t *mfib)
         .frp_addr = zero_addr,
         .frp_sw_if_index = 0xffffffff,
         .frp_fib_index = ~0,
-        .frp_weight = 0,
+        .frp_weight = 1,
         .frp_flags = FIB_ROUTE_PATH_LOCAL,
     };
 
@@ -252,7 +227,6 @@ ip6_mfib_table_destroy (ip6_mfib_t *mfib)
     ASSERT(~0 != mfib_table->mft_table_id);
 
     hash_unset (ip6_main.mfib_index_by_table_id, mfib_table->mft_table_id);
-    clib_mem_free(mfib_table->v6.rhead);
     pool_put(ip6_main.mfibs, mfib_table);
 }
 
@@ -264,14 +238,14 @@ ip6_mfib_interface_enable_disable (u32 sw_if_index, int is_enable)
         .frp_addr = zero_addr,
         .frp_sw_if_index = sw_if_index,
         .frp_fib_index = ~0,
-        .frp_weight = 0,
+        .frp_weight = 1,
+        .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
     };
     mfib_prefix_t pfx = {
         .fp_proto = FIB_PROTOCOL_IP6,
     };
     u32 mfib_index;
 
-    vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
     mfib_index = ip6_mfib_table_get_index_for_sw_if_index(sw_if_index);
 
     if (is_enable)
@@ -281,8 +255,7 @@ ip6_mfib_interface_enable_disable (u32 sw_if_index, int is_enable)
             mfib_table_entry_path_update(mfib_index,
                                          &pfx,
                                          MFIB_SOURCE_SPECIAL,
-                                         &path,
-                                         MFIB_ITF_FLAG_ACCEPT);
+                                         &path);
         });
     }
     else
@@ -325,29 +298,24 @@ ip6_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
     return (ip6_main.mfib_index_by_sw_if_index[sw_if_index]);
 }
 
-#define IP6_MFIB_MK_KEY(_grp, _src, _key)                           \
-{                                                                   \
-    (_key)->key[0] = 33;                                            \
-    memcpy((_key)->key+1, _grp, 16);                                \
-    memcpy((_key)->key+17, _src, 16);                               \
-}
+#define IPV6_MFIB_GRP_LEN(_len)                 \
+    (_len > 128 ? 128 : _len)
 
-#define IP6_MFIB_MK_KEY_MASK(_grp, _src, _len, _key)                \
-{                                                                   \
-    IP6_MFIB_MK_KEY(_grp, _src, _key);                              \
-                                                                    \
-    (_key)->mask[0] = 33;                                           \
-    if (_len <= 128)                                                \
-    {                                                               \
-        memcpy((_key)->mask+1, &ip6_main.fib_masks[_len], 16);      \
-        clib_memset((_key)->mask+17, 0, 16);                             \
-    }                                                               \
-    else                                                            \
-    {                                                               \
-        ASSERT(_len == 256);                                        \
-        memcpy((_key)->mask+1, &ip6_main.fib_masks[128], 16);       \
-        memcpy((_key)->mask+17, &ip6_main.fib_masks[128], 16);      \
-    }                                                               \
+#define IP6_MFIB_MK_KEY(_mfib, _grp, _src, _len, _key)                  \
+{                                                                       \
+    _key.key[0] = (_grp->as_u64[0] &                                    \
+                   ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[0]); \
+    _key.key[1] = (_grp->as_u64[1] &                                    \
+                   ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[1]); \
+    if (_len == 256) {                                                  \
+        _key.key[2] = _src->as_u64[0];                                  \
+        _key.key[3] = _src->as_u64[1];                                  \
+    } else {                                                            \
+        _key.key[2] = 0;                                                \
+        _key.key[3] = 0;                                                \
+    }                                                                   \
+    _key.key[4] = _mfib->index;                                         \
+    _key.key[4] = (_key.key[4] << 32) | len;                            \
 }
 
 /*
@@ -361,68 +329,137 @@ ip6_mfib_table_lookup_exact_match (const ip6_mfib_t *mfib,
                                    const ip6_address_t *src,
                                    u32 len)
 {
-    ip6_mfib_node_t *i6mn;
-    ip6_mfib_key_t key;
-
-    IP6_MFIB_MK_KEY_MASK(grp, src, len, &key);
+    ip6_mfib_key_t key, value;
+    int rv;
 
-    i6mn = (ip6_mfib_node_t*) rn_lookup(key.key, key.mask,
-                                        (struct radix_node_head *)mfib->rhead);
+    IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
 
-    if (NULL == i6mn)
-    {
-        return (INDEX_INVALID);
-    }
+    rv = clib_bihash_search_inline_2_40_8(&ip6_mfib_table.ip6_mhash,
+                                          &key, &value);
+    if (rv == 0)
+       return value.value;
 
-    return (i6mn->i6mn_entry);
+    return (FIB_NODE_INDEX_INVALID);
 }
 
 /*
  * ip6_fib_table_lookup
  *
- * Longest prefix match
+ * Longest prefix match for the forwarding plane (no mask given)
  */
 fib_node_index_t
-ip6_mfib_table_lookup (const ip6_mfib_t *mfib,
-                       const ip6_address_t *src,
-                       const ip6_address_t *grp,
-                       u32 len)
+ip6_mfib_table_fwd_lookup (const ip6_mfib_t *mfib,
+                           const ip6_address_t *src,
+                           const ip6_address_t *grp)
 {
-    ip6_mfib_node_t *i6mn;
-    ip6_mfib_key_t key;
+    ip6_mfib_table_instance_t *table;
+    ip6_mfib_key_t key, value;
+    int i, n, len;
+    int rv;
+
+    table = &ip6_mfib_table;
+    n = vec_len (table->prefix_lengths_in_search_order);
+
+    for (i = 0; i < n; i++)
+    {
+       len = table->prefix_lengths_in_search_order[i];
+
+       ASSERT(len >= 0 && len <= 256);
+        IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
+       rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
+       if (rv == 0)
+           return value.value;
+    }
+
+    return (FIB_NODE_INDEX_INVALID);
+}
 
-    IP6_MFIB_MK_KEY_MASK(grp, src, len, &key);
 
-    i6mn = (ip6_mfib_node_t*) rn_search_m(key.key,
-                                          mfib->rhead->rnh_treetop,
-                                          key.mask);
+fib_node_index_t
+ip6_mfib_table_get_less_specific (const ip6_mfib_t *mfib,
+                                  const ip6_address_t *src,
+                                  const ip6_address_t *grp,
+                                  u32 len)
+{
+    u32 mask_len;
 
-    ASSERT(NULL != i6mn);
+    /*
+     * in the absence of a tree structure for the table that allows for an O(1)
+     * parent get, a cheeky way to find the cover is to LPM for the prefix with
+     * mask-1.
+     * there should always be a cover, though it may be the default route. the
+     * default route's cover is the default route.
+     */
+    if (len == 256)
+    {
+        /* go from (S,G) to (*,G*) */
+        mask_len = 128;
+    }
+    else if (len != 0)
+    {
+       mask_len = len - 1;
+    }
+    else
+    {
+        mask_len = len;
+    }
 
-    return (i6mn->i6mn_entry);
+    return (ip6_mfib_table_lookup(mfib, src, grp, mask_len));
 }
 
 /*
  * ip6_fib_table_lookup
  *
- * Longest prefix match no mask
+ * Longest prefix match
  */
 fib_node_index_t
-ip6_mfib_table_lookup2 (const ip6_mfib_t *mfib,
-                        const ip6_address_t *src,
-                        const ip6_address_t *grp)
+ip6_mfib_table_lookup (const ip6_mfib_t *mfib,
+                       const ip6_address_t *src,
+                       const ip6_address_t *grp,
+                       u32 len)
 {
-    ip6_mfib_node_t *i6mn;
-    ip6_mfib_key_t key;
+    ip6_mfib_table_instance_t *table;
+    ip6_mfib_key_t key, value;
+    int i, n, rv;
 
-    IP6_MFIB_MK_KEY(grp, src, &key);
+    table = &ip6_mfib_table;
+    n = vec_len (table->prefix_lengths_in_search_order);
 
-    i6mn = (ip6_mfib_node_t*) rn_match(key.key,
-                                       (struct radix_node_head *)mfib->rhead); // const cast
+    /*
+     * start search from a mask length same length or shorter.
+     * we don't want matches longer than the mask passed
+     */
+    i = 0;
+    while (i < n && table->prefix_lengths_in_search_order[i] > len)
+    {
+        i++;
+    }
+
+    for (; i < n; i++)
+    {
+       len = table->prefix_lengths_in_search_order[i];
+
+       ASSERT(len <= 256);
+        IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
 
-    ASSERT(NULL != i6mn);
+       rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
+       if (rv == 0)
+           return value.value;
+    }
+
+    return (FIB_NODE_INDEX_INVALID);
+}
 
-    return (i6mn->i6mn_entry);
+static void
+compute_prefix_lengths_in_search_order (ip6_mfib_table_instance_t *table)
+{
+    int i;
+    vec_reset_length (table->prefix_lengths_in_search_order);
+    /* Note: bitmap reversed so this is in fact a longest prefix match */
+    clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap)
+     {
+       vec_add1(table->prefix_lengths_in_search_order, (256 - i));
+    }
 }
 
 void
@@ -432,19 +469,21 @@ ip6_mfib_table_entry_insert (ip6_mfib_t *mfib,
                              u32 len,
                              fib_node_index_t mfib_entry_index)
 {
-    ip6_mfib_node_t *i6mn = clib_mem_alloc(sizeof(*i6mn));
+    ip6_mfib_table_instance_t *table;
+    ip6_mfib_key_t key;
 
-    clib_memset(i6mn->i6mn_nodes, 0, sizeof(i6mn->i6mn_nodes));
+    table = &ip6_mfib_table;
+    IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
+    key.value = mfib_entry_index;
 
-    IP6_MFIB_MK_KEY_MASK(grp, src, len, &i6mn->i6mn_key);
-    i6mn->i6mn_entry = mfib_entry_index;
+    clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 1);
 
-    if (NULL == rn_addroute(i6mn->i6mn_key.key,
-                            i6mn->i6mn_key.mask,
-                            mfib->rhead,
-                            i6mn->i6mn_nodes))
+    if (0 == table->dst_address_length_refcounts[len]++)
     {
-        ASSERT(0);
+        table->non_empty_dst_address_length_bitmap =
+            clib_bitmap_set (table->non_empty_dst_address_length_bitmap, 
+                             256 - len, 1);
+        compute_prefix_lengths_in_search_order (table);
     }
 }
 
@@ -454,14 +493,22 @@ ip6_mfib_table_entry_remove (ip6_mfib_t *mfib,
                              const ip6_address_t *src,
                              u32 len)
 {
-    ip6_mfib_node_t *i6mn;
+    ip6_mfib_table_instance_t *table;
     ip6_mfib_key_t key;
 
-    IP6_MFIB_MK_KEY_MASK(grp, src, len, &key);
+    IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
 
-    i6mn = (ip6_mfib_node_t*) rn_delete(key.key, key.mask, mfib->rhead);
+    table = &ip6_mfib_table;
+    clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 0);
 
-    clib_mem_free(i6mn);
+    ASSERT (table->dst_address_length_refcounts[len] > 0);
+    if (--table->dst_address_length_refcounts[len] == 0)
+    {
+       table->non_empty_dst_address_length_bitmap =
+            clib_bitmap_set (table->non_empty_dst_address_length_bitmap, 
+                             256 - len, 0);
+       compute_prefix_lengths_in_search_order (table);
+    }
 }
 
 static clib_error_t *
@@ -475,9 +522,14 @@ VLIB_INIT_FUNCTION(ip6_mfib_module_init);
 u8 *
 format_ip6_mfib_table_memory (u8 * s, va_list * args)
 {
-    s = format(s, "%=30s %=6d %=8s\n",
+    u64 bytes_inuse;
+
+    bytes_inuse = alloc_arena_next(&(ip6_mfib_table.ip6_mhash));
+
+    s = format(s, "%=30s %=6d %=12ld\n",
                "IPv6 multicast",
-               pool_elts(ip6_main.mfibs), "???");
+               pool_elts(ip6_main.mfibs),
+               bytes_inuse);
 
     return (s);
 }
@@ -487,12 +539,23 @@ ip6_mfib_table_show_one (ip6_mfib_t *mfib,
                          vlib_main_t * vm,
                          ip6_address_t *src,
                          ip6_address_t *grp,
-                         u32 mask_len)
+                         u32 mask_len,
+                         u32 cover)
 {
-    vlib_cli_output(vm, "%U",
-                    format_mfib_entry,
-                    ip6_mfib_table_lookup(mfib, src, grp, mask_len),
-                    MFIB_ENTRY_FORMAT_DETAIL);
+    if (cover)
+    {
+        vlib_cli_output(vm, "%U",
+                        format_mfib_entry,
+                        ip6_mfib_table_get_less_specific(mfib, src, grp, mask_len),
+                        MFIB_ENTRY_FORMAT_DETAIL);
+    }
+    else
+    {
+        vlib_cli_output(vm, "%U",
+                        format_mfib_entry,
+                        ip6_mfib_table_lookup(mfib, src, grp, mask_len),
+                        MFIB_ENTRY_FORMAT_DETAIL);
+    }
 }
 
 typedef struct ip6_mfib_show_ctx_t_ {
@@ -500,14 +563,14 @@ typedef struct ip6_mfib_show_ctx_t_ {
 } ip6_mfib_show_ctx_t;
 
 
-static int
+static walk_rc_t
 ip6_mfib_table_collect_entries (fib_node_index_t mfei, void *arg)
 {
     ip6_mfib_show_ctx_t *ctx = arg;
 
     vec_add1(ctx->entries, mfei);
 
-    return (0);
+    return (WALK_CONTINUE);
 }
 
 static void
@@ -536,39 +599,45 @@ ip6_mfib_table_show_all (ip6_mfib_t *mfib,
     vec_free(ctx.entries);
 }
 
-typedef struct ip6_mfib_radix_walk_ctx_t_
+/**
+ * @brief Context when walking the IPv6 table. Since all VRFs are in the
+ * same hash table, we need to filter only those we need as we walk
+ */
+typedef struct ip6_mfib_walk_ctx_t_
 {
-    mfib_table_walk_fn_t user_fn;
-    void *user_ctx;
-} ip6_mfib_radix_walk_ctx_t;
+    u32 i6w_mfib_index;
+    mfib_table_walk_fn_t i6w_fn;
+    void *i6w_ctx;
+} ip6_mfib_walk_ctx_t;
 
 static int
-ip6_mfib_table_radix_walk (struct radix_node *rn,
-                           void *arg)
+ip6_mfib_walk_cb (clib_bihash_kv_40_8_t * kvp,
+                 void *arg)
 {
-    ip6_mfib_radix_walk_ctx_t *ctx = arg;
-    ip6_mfib_node_t *i6mn;
-
-    i6mn = (ip6_mfib_node_t*) rn;
+    ip6_mfib_walk_ctx_t *ctx = arg;
 
-    ctx->user_fn(i6mn->i6mn_entry, ctx->user_ctx);
-
-    return (0);
+    if ((kvp->key[4] >> 32) == ctx->i6w_mfib_index)
+    {
+        ctx->i6w_fn(kvp->value, ctx->i6w_ctx);
+    }
+    return (BIHASH_WALK_CONTINUE);
 }
 
 void
 ip6_mfib_table_walk (ip6_mfib_t *mfib,
                      mfib_table_walk_fn_t fn,
-                     void *ctx)
+                     void *arg)
 {
-    ip6_mfib_radix_walk_ctx_t rn_ctx = {
-        .user_fn = fn,
-        .user_ctx = ctx,
+    ip6_mfib_walk_ctx_t ctx = {
+        .i6w_mfib_index = mfib->index,
+        .i6w_fn = fn,
+        .i6w_ctx = arg,
     };
 
-    rn_walktree(mfib->rhead,
-                ip6_mfib_table_radix_walk,
-                &rn_ctx);
+    clib_bihash_foreach_key_value_pair_40_8(
+        &ip6_mfib_table.ip6_mhash,
+        ip6_mfib_walk_cb,
+        &ctx);
 }
 
 static clib_error_t *
@@ -580,11 +649,12 @@ ip6_show_mfib (vlib_main_t * vm,
     mfib_table_t *mfib_table;
     int verbose, matching;
     ip6_address_t grp, src = {{0}};
-    u32 mask = 32;
+    u32 mask = 128, cover;
     int table_id = -1, fib_index = ~0;
 
     verbose = 1;
     matching = 0;
+    cover = 0;
 
     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -614,12 +684,14 @@ ip6_show_mfib (vlib_main_t * vm,
             ;
         else if (unformat (input, "index %d", &fib_index))
             ;
+        else if (unformat (input, "cover"))
+            cover = 1;
         else
             break;
     }
 
-    pool_foreach (mfib_table, im6->mfibs,
-    ({
+    pool_foreach (mfib_table, im6->mfibs)
+     {
         ip6_mfib_t *mfib = &mfib_table->v6;
 
         if (table_id >= 0 && table_id != (int)mfib->table_id)
@@ -651,19 +723,20 @@ ip6_show_mfib (vlib_main_t * vm,
         }
         else
         {
-            ip6_mfib_table_show_one(mfib, vm, &src, &grp, mask);
+            ip6_mfib_table_show_one(mfib, vm, &src, &grp, mask, cover);
         }
-    }));
+    }
 
     return 0;
 }
 
-/*
+/* clang-format off */
+/*?
  * This command displays the IPv6 MulticasrFIB Tables (VRF Tables) and
  * the route entries for each table.
  *
  * @note This command will run for a long time when the FIB tables are
- * comprised of millions of entries. For those senarios, consider displaying
+ * comprised of millions of entries. For those scenarios, consider displaying
  * a single table or summary mode.
  *
  * @cliexpar
@@ -699,11 +772,26 @@ ip6_show_mfib (vlib_main_t * vm,
  *                   24               2
  *                   32               4
  * @cliexend
- */
-/* *INDENT-OFF* */
?*/
+/* clang-format on */
 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
     .path = "show ip6 mfib",
     .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
     .function = ip6_show_mfib,
 };
-/* *INDENT-ON* */
+
+static clib_error_t *
+ip6_mfib_init (vlib_main_t * vm)
+{
+    clib_bihash_init_40_8 (&ip6_mfib_table.ip6_mhash,
+                           "ip6 mFIB table",
+                           IP6_MFIB_DEFAULT_HASH_NUM_BUCKETS,
+                           IP6_MFIB_DEFAULT_HASH_MEMORY_SIZE);
+
+    return (NULL);
+}
+
+VLIB_INIT_FUNCTION (ip6_mfib_init) =
+{
+  .runs_before = VLIB_INITS("ip6_lookup_init"),
+};