fib: doc nitfixes
[vpp.git] / src / vnet / fib / ip6_fib.c
index 60d1365..708fddc 100644 (file)
 #include <vppinfra/bihash_24_8.h>
 #include <vppinfra/bihash_template.c>
 
+ip6_fib_table_instance_t ip6_fib_table[IP6_FIB_NUM_TABLES];
+
+/* ip6 lookup table config parameters */
+u32 ip6_fib_table_nbuckets;
+uword ip6_fib_table_size;
+
 static void
 vnet_ip6_fib_init (u32 fib_index)
 {
@@ -151,11 +157,15 @@ ip6_fib_table_destroy (u32 fib_index)
     fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
     fib_source_t source;
 
-     /*
+    /*
      * validate no more routes.
      */
-    ASSERT(0 == fib_table->ft_total_route_counts);
-    FOR_EACH_FIB_SOURCE(source)
+#if CLIB_DEBUG > 0
+    if (0 != fib_table->ft_total_route_counts)
+        fib_table_assert_empty(fib_table);
+#endif
+
+    vec_foreach_index(source, fib_table->ft_src_route_counts)
     {
        ASSERT(0 == fib_table->ft_src_route_counts[source]);
     }
@@ -164,6 +174,7 @@ ip6_fib_table_destroy (u32 fib_index)
     {
        hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
     }
+    vec_free(fib_table->ft_src_route_counts);
     pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
     pool_put(ip6_main.fibs, fib_table);
 }
@@ -178,7 +189,7 @@ ip6_fib_table_lookup (u32 fib_index,
     int i, n_p, rv;
     u64 fib;
 
-    table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
+    table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
     n_p = vec_len (table->prefix_lengths_in_search_order);
 
     kv.key[0] = addr->as_u64[0];
@@ -225,7 +236,7 @@ ip6_fib_table_lookup_exact_match (u32 fib_index,
     u64 fib;
     int rv;
 
-    table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
+    table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
     mask = &ip6_main.fib_masks[len];
     fib = ((u64)((fib_index))<<32);
 
@@ -243,14 +254,29 @@ ip6_fib_table_lookup_exact_match (u32 fib_index,
 static void
 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
 {
+    u8 *old, *prefix_lengths_in_search_order = NULL;
     int i;
-    vec_reset_length (table->prefix_lengths_in_search_order);
+
+    /*
+     * build the list in a scratch space then cutover so the workers
+     * can continue uninterrupted.
+     */
+    old = 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,
-    ({
+    clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap)
+     {
        int dst_address_length = 128 - i;
-       vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
-    }));
+       vec_add1(prefix_lengths_in_search_order, dst_address_length);
+    }
+
+    table->prefix_lengths_in_search_order = prefix_lengths_in_search_order;
+
+    /*
+     * let the workers go once round the track before we free the old set
+     */
+    vlib_worker_wait_one_loop();
+    vec_free(old);
 }
 
 void
@@ -263,7 +289,7 @@ ip6_fib_table_entry_remove (u32 fib_index,
     ip6_address_t *mask;
     u64 fib;
 
-    table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
+    table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
     mask = &ip6_main.fib_masks[len];
     fib = ((u64)((fib_index))<<32);
 
@@ -295,7 +321,7 @@ ip6_fib_table_entry_insert (u32 fib_index,
     ip6_address_t *mask;
     u64 fib;
 
-    table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
+    table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
     mask = &ip6_main.fib_masks[len];
     fib = ((u64)((fib_index))<<32);
 
@@ -306,12 +332,13 @@ ip6_fib_table_entry_insert (u32 fib_index,
 
     clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
 
-    table->dst_address_length_refcounts[len]++;
-
-    table->non_empty_dst_address_length_bitmap =
-        clib_bitmap_set (table->non_empty_dst_address_length_bitmap, 
-                        128 - len, 1);
-    compute_prefix_lengths_in_search_order (table);
+    if (0 == table->dst_address_length_refcounts[len]++)
+    {
+        table->non_empty_dst_address_length_bitmap =
+            clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
+                             128 - len, 1);
+        compute_prefix_lengths_in_search_order (table);
+    }
 }
 
 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
@@ -319,7 +346,7 @@ u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
                                               const ip6_address_t * dst)
 {
     u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
-    return ip6_fib_table_fwding_lookup(im, fib_index, dst);
+    return ip6_fib_table_fwding_lookup(fib_index, dst);
 }
 
 u32
@@ -347,7 +374,7 @@ ip6_fib_table_fwding_dpo_update (u32 fib_index,
     ip6_address_t *mask;
     u64 fib;
 
-    table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
+    table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
     mask = &ip6_main.fib_masks[len];
     fib = ((u64)((fib_index))<<32);
 
@@ -358,12 +385,13 @@ ip6_fib_table_fwding_dpo_update (u32 fib_index,
 
     clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
 
-    table->dst_address_length_refcounts[len]++;
-
-    table->non_empty_dst_address_length_bitmap =
-        clib_bitmap_set (table->non_empty_dst_address_length_bitmap, 
-                        128 - len, 1);
-    compute_prefix_lengths_in_search_order (table);
+    if (0 == table->dst_address_length_refcounts[len]++)
+    {
+        table->non_empty_dst_address_length_bitmap =
+            clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
+                             128 - len, 1);
+        compute_prefix_lengths_in_search_order (table);
+    }
 }
 
 void
@@ -377,7 +405,7 @@ ip6_fib_table_fwding_dpo_remove (u32 fib_index,
     ip6_address_t *mask;
     u64 fib;
 
-    table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
+    table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
     mask = &ip6_main.fib_masks[len];
     fib = ((u64)((fib_index))<<32);
 
@@ -489,7 +517,7 @@ ip6_fib_table_walk (u32 fib_index,
     };
 
     clib_bihash_foreach_key_value_pair_24_8(
-        &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
+        &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
         ip6_fib_walk_cb,
         &ctx);
 
@@ -510,7 +538,7 @@ ip6_fib_table_sub_tree_walk (u32 fib_index,
     };
 
     clib_bihash_foreach_key_value_pair_24_8(
-        &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
+        &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
         ip6_fib_walk_cb,
         &ctx);
 }
@@ -573,16 +601,10 @@ format_ip6_fib_table_memory (u8 * s, va_list * args)
 {
     uword bytes_inuse;
 
-    bytes_inuse = 
-        alloc_arena_next 
-        (&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash))
-        - alloc_arena (&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash));
-
-    bytes_inuse += 
-        alloc_arena_next(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash))
-        - alloc_arena(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash));
+    bytes_inuse = (alloc_arena_next(&(ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash)) +
+                   alloc_arena_next(&(ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash)));
 
-    s = format(s, "%=30s %=6d %=8ld\n",
+    s = format(s, "%=30s %=6d %=12ld\n",
                "IPv6 unicast",
                pool_elts(ip6_main.fibs),
                bytes_inuse);
@@ -594,7 +616,7 @@ typedef struct {
   u64 count_by_prefix_length[129];
 } count_routes_in_fib_at_prefix_length_arg_t;
 
-static void
+static int
 count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp,
                                       void *arg)
 {
@@ -602,11 +624,13 @@ count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp,
   int mask_width;
 
   if ((kvp->key[2]>>32) != ap->fib_index)
-    return;
+      return (BIHASH_WALK_CONTINUE);
 
   mask_width = kvp->key[2] & 0xFF;
 
   ap->count_by_prefix_length[mask_width]++;
+
+  return (BIHASH_WALK_CONTINUE);
 }
 
 static clib_error_t *
@@ -623,6 +647,7 @@ ip6_show_fib (vlib_main_t * vm,
     u32 mask_len  = 128;
     int table_id = -1, fib_index = ~0;
     int detail = 0;
+    int hash = 0;
 
     verbose = 1;
     matching = 0;
@@ -638,6 +663,11 @@ ip6_show_fib (vlib_main_t * vm,
                  unformat (input, "det"))
            detail = 1;
 
+       else if (unformat (input, "hash") ||
+                 unformat (input, "mem") ||
+                 unformat (input, "memory"))
+           hash = 1;
+
        else if (unformat (input, "%U/%d",
                           unformat_ip6_address, &matching_address, &mask_len))
            matching = 1;
@@ -653,8 +683,21 @@ ip6_show_fib (vlib_main_t * vm,
            break;
     }
 
-    pool_foreach (fib_table, im6->fibs,
-    ({
+    if (hash)
+    {
+        vlib_cli_output (vm, "IPv6 Non-Forwarding Hash Table:\n%U\n",
+                         BV (format_bihash),
+                         &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
+                         detail);
+        vlib_cli_output (vm, "IPv6 Forwarding Hash Table:\n%U\n",
+                         BV (format_bihash),
+                         &ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash,
+                         detail);
+        return (NULL);
+    }
+
+    pool_foreach (fib_table, im6->fibs)
+     {
         fib_source_t source;
         u8 *s = NULL;
 
@@ -666,13 +709,16 @@ ip6_show_fib (vlib_main_t * vm,
         if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
             continue;
 
-       s = format(s, "%U, fib_index:%d, flow hash:[%U] locks:[",
+       s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[",
                    format_fib_table_name, fib->index,
                    FIB_PROTOCOL_IP6,
                    fib->index,
                    format_ip_flow_hash_config,
-                   fib_table->ft_flow_hash_config);
-       FOR_EACH_FIB_SOURCE(source)
+                   fib_table->ft_flow_hash_config,
+                   fib_table->ft_epoch,
+                   format_fib_table_flags, fib_table->ft_flags);
+
+        vec_foreach_index(source, fib_table->ft_locks)
         {
             if (0 != fib_table->ft_locks[source])
             {
@@ -688,7 +734,7 @@ ip6_show_fib (vlib_main_t * vm,
        /* Show summary? */
        if (! verbose)
        {
-           clib_bihash_24_8_t * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
+           clib_bihash_24_8_t * h = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
            int len;
 
            vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
@@ -716,7 +762,7 @@ ip6_show_fib (vlib_main_t * vm,
        {
            ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
        }
-    }));
+    }
 
     return 0;
 }
@@ -726,7 +772,7 @@ ip6_show_fib (vlib_main_t * vm,
  * 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
  * in summary mode.
  *
  * @cliexpar
@@ -822,3 +868,55 @@ VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
     .function = ip6_show_fib,
 };
 /* *INDENT-ON* */
+
+static clib_error_t *
+ip6_config (vlib_main_t * vm, unformat_input_t * input)
+{
+  uword heapsize = 0;
+  u32 nbuckets = 0;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "hash-buckets %d", &nbuckets))
+          ;
+      else if (unformat (input, "heap-size %U",
+                        unformat_memory_size, &heapsize))
+       ;
+      else
+       return clib_error_return (0, "unknown input '%U'",
+                                 format_unformat_error, input);
+    }
+
+  ip6_fib_table_nbuckets = nbuckets;
+  ip6_fib_table_size = heapsize;
+
+  return 0;
+}
+
+VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
+
+static clib_error_t *
+ip6_fib_init (vlib_main_t * vm)
+{
+    if (ip6_fib_table_nbuckets == 0)
+        ip6_fib_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
+
+    ip6_fib_table_nbuckets = 1 << max_log2 (ip6_fib_table_nbuckets);
+
+    if (ip6_fib_table_size == 0)
+        ip6_fib_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
+
+    clib_bihash_init_24_8 (&(ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash),
+                           "ip6 FIB fwding table",
+                           ip6_fib_table_nbuckets, ip6_fib_table_size);
+    clib_bihash_init_24_8 (&ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
+                           "ip6 FIB non-fwding table",
+                           ip6_fib_table_nbuckets, ip6_fib_table_size);
+
+    return (NULL);
+}
+
+VLIB_INIT_FUNCTION (ip6_fib_init) =
+{
+  .runs_before = VLIB_INITS("ip6_lookup_init"),
+};