fib: Allow the creation of new source on the API
[vpp.git] / src / vnet / fib / fib_table.c
index c20bb25..eaeee5b 100644 (file)
@@ -23,6 +23,8 @@
 #include <vnet/fib/ip6_fib.h>
 #include <vnet/fib/mpls_fib.h>
 
+const static char * fib_table_flags_strings[] = FIB_TABLE_ATTRIBUTES;
+
 fib_table_t *
 fib_table_get (fib_node_index_t index,
               fib_protocol_t proto)
@@ -197,10 +199,18 @@ fib_table_post_insert_actions (fib_table_t *fib_table,
 
         /*
          * inform the covering entry that a new more specific
-         * has been inserted beneath it
+         * has been inserted beneath it.
+         * If the prefix that has been inserted is a host route
+         * then it is not possible that it will be the cover for any
+         * other entry, so we can elide the walk. This is particularly
+         * beneficial since there are often many host entries sharing the
+         * same cover (i.e. ADJ or RR sourced entries).
          */
-       fib_entry_cover_change_notify(fib_entry_cover_index,
-                                     fib_entry_index);
+        if (!fib_entry_is_host(fib_entry_index))
+        {
+            fib_entry_cover_change_notify(fib_entry_cover_index,
+                                          fib_entry_index);
+        }
     }
 }
 
@@ -294,6 +304,21 @@ fib_table_fwding_dpo_remove (u32 fib_index,
     }
 }
 
+static void
+fib_table_source_count_inc (fib_table_t *fib_table,
+                            fib_source_t source)
+{
+    vec_validate (fib_table->ft_src_route_counts, source);
+    fib_table->ft_src_route_counts[source]++;
+}
+
+static void
+fib_table_source_count_dec (fib_table_t *fib_table,
+                            fib_source_t source)
+{
+    vec_validate (fib_table->ft_src_route_counts, source);
+    fib_table->ft_src_route_counts[source]--;
+}
 
 fib_node_index_t
 fib_table_entry_special_dpo_add (u32 fib_index,
@@ -315,7 +340,7 @@ fib_table_entry_special_dpo_add (u32 fib_index,
                                                   dpo);
 
        fib_table_entry_insert(fib_table, prefix, fib_entry_index);
-        fib_table->ft_src_route_counts[source]++;
+        fib_table_source_count_inc(fib_table, source);
     }
     else
     {
@@ -326,7 +351,7 @@ fib_table_entry_special_dpo_add (u32 fib_index,
 
         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
         {
-            fib_table->ft_src_route_counts[source]++;
+        fib_table_source_count_inc(fib_table, source);
         }
     }
 
@@ -354,7 +379,7 @@ fib_table_entry_special_dpo_update (u32 fib_index,
                                                   dpo);
 
        fib_table_entry_insert(fib_table, prefix, fib_entry_index);
-        fib_table->ft_src_route_counts[source]++;
+        fib_table_source_count_inc(fib_table, source);
     }
     else
     {
@@ -369,7 +394,7 @@ fib_table_entry_special_dpo_update (u32 fib_index,
 
         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
         {
-            fib_table->ft_src_route_counts[source]++;
+            fib_table_source_count_inc(fib_table, source);
         }
     }
 
@@ -451,7 +476,7 @@ fib_table_entry_special_remove (u32 fib_index,
         */
         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
         {
-            fib_table->ft_src_route_counts[source]--;
+            fib_table_source_count_dec(fib_table, source);
         }
 
        fib_entry_unlock(fib_entry_index);
@@ -473,7 +498,7 @@ fib_table_entry_special_remove (u32 fib_index,
  */
 static void
 fib_table_route_path_fixup (const fib_prefix_t *prefix,
-                            fib_entry_flag_t eflags,
+                            fib_entry_flag_t *eflags,
                            fib_route_path_t *path)
 {
     /*
@@ -485,10 +510,11 @@ fib_table_route_path_fixup (const fib_prefix_t *prefix,
         (~0 == path->frp_sw_if_index) &&
         (0 == ip46_address_cmp(&path->frp_addr, &prefix->fp_addr)))
     {
-        /* Prefix recurses via itse;f */
+        /* Prefix recurses via itself */
        path->frp_flags |= FIB_ROUTE_PATH_DROP;
     }
-    if (fib_prefix_is_host(prefix) &&
+    if (!(path->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
+        fib_prefix_is_host(prefix) &&
        ip46_address_is_zero(&path->frp_addr) &&
        path->frp_sw_if_index != ~0 &&
         path->frp_proto != DPO_PROTO_ETHERNET)
@@ -496,18 +522,36 @@ fib_table_route_path_fixup (const fib_prefix_t *prefix,
        path->frp_addr = prefix->fp_addr;
         path->frp_flags |= FIB_ROUTE_PATH_ATTACHED;
     }
-    if (eflags & FIB_ENTRY_FLAG_DROP)
+    else if ((*eflags & FIB_ENTRY_FLAG_CONNECTED) &&
+             !(*eflags & FIB_ENTRY_FLAG_LOCAL))
+    {
+        if (ip46_address_is_zero(&path->frp_addr))
+        {
+            path->frp_flags |= FIB_ROUTE_PATH_GLEAN;
+            fib_prefix_normalize(prefix, &path->frp_connected);
+        }
+    }
+    if (*eflags & FIB_ENTRY_FLAG_DROP)
     {
        path->frp_flags |= FIB_ROUTE_PATH_DROP;
     }
-    if (eflags & FIB_ENTRY_FLAG_LOCAL)
+    if (*eflags & FIB_ENTRY_FLAG_LOCAL)
     {
        path->frp_flags |= FIB_ROUTE_PATH_LOCAL;
     }
-    if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
+    if (*eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
     {
        path->frp_flags |= FIB_ROUTE_PATH_EXCLUSIVE;
     }
+    if (path->frp_flags & FIB_ROUTE_PATH_LOCAL)
+    {
+        *eflags |= FIB_ENTRY_FLAG_LOCAL;
+
+        if (path->frp_sw_if_index != ~0)
+        {
+            *eflags |= FIB_ENTRY_FLAG_CONNECTED;
+        }
+    }
 }
 
 fib_node_index_t
@@ -520,7 +564,7 @@ fib_table_entry_path_add (u32 fib_index,
                          u32 next_hop_sw_if_index,
                          u32 next_hop_fib_index,
                          u32 next_hop_weight,
-                         mpls_label_t *next_hop_labels,
+                         fib_mpls_label_t *next_hop_labels,
                          fib_route_path_flags_t path_flags)
 {
     fib_route_path_t path = {
@@ -530,6 +574,7 @@ fib_table_entry_path_add (u32 fib_index,
        .frp_fib_index = next_hop_fib_index,
        .frp_weight = next_hop_weight,
        .frp_flags = path_flags,
+        .frp_rpf_id = INDEX_INVALID,
        .frp_label_stack = next_hop_labels,
     };
     fib_node_index_t fib_entry_index;
@@ -544,12 +589,19 @@ fib_table_entry_path_add (u32 fib_index,
     return (fib_entry_index);
 }
 
+static int
+fib_route_path_cmp_for_sort (void * v1,
+                            void * v2)
+{
+    return (fib_route_path_cmp(v1, v2));
+}
+
 fib_node_index_t
 fib_table_entry_path_add2 (u32 fib_index,
                           const fib_prefix_t *prefix,
                           fib_source_t source,
                           fib_entry_flag_t flags,
-                          fib_route_path_t *rpath)
+                          fib_route_path_t *rpaths)
 {
     fib_node_index_t fib_entry_index;
     fib_table_t *fib_table;
@@ -558,30 +610,35 @@ fib_table_entry_path_add2 (u32 fib_index,
     fib_table = fib_table_get(fib_index, prefix->fp_proto);
     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
 
-    for (ii = 0; ii < vec_len(rpath); ii++)
+    for (ii = 0; ii < vec_len(rpaths); ii++)
     {
-       fib_table_route_path_fixup(prefix, flags, &rpath[ii]);
+       fib_table_route_path_fixup(prefix, &flags, &rpaths[ii]);
     }
+    /*
+     * sort the paths provided by the control plane. this means
+     * the paths and the extension on the entry will be sorted.
+     */
+    vec_sort_with_function(rpaths, fib_route_path_cmp_for_sort);
 
     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
     {
        fib_entry_index = fib_entry_create(fib_index, prefix,
                                           source, flags,
-                                          rpath);
+                                          rpaths);
 
        fib_table_entry_insert(fib_table, prefix, fib_entry_index);
-        fib_table->ft_src_route_counts[source]++;
+        fib_table_source_count_inc(fib_table, source);
     }
     else
     {
         int was_sourced;
 
         was_sourced = fib_entry_is_sourced(fib_entry_index, source);
-       fib_entry_path_add(fib_entry_index, source, flags, rpath);;
+       fib_entry_path_add(fib_entry_index, source, flags, rpaths);;
 
         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
         {
-            fib_table->ft_src_route_counts[source]++;
+            fib_table_source_count_inc(fib_table, source);
         }
     }
 
@@ -592,7 +649,7 @@ void
 fib_table_entry_path_remove2 (u32 fib_index,
                              const fib_prefix_t *prefix,
                              fib_source_t source,
-                             fib_route_path_t *rpath)
+                             fib_route_path_t *rpaths)
 {
     /*
      * 1 is it present
@@ -601,8 +658,8 @@ fib_table_entry_path_remove2 (u32 fib_index,
      *      no => cover walk
      */
     fib_node_index_t fib_entry_index;
+    fib_route_path_t *rpath;
     fib_table_t *fib_table;
-    u32 ii;
 
     fib_table = fib_table_get(fib_index, prefix->fp_proto);
     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
@@ -632,16 +689,16 @@ fib_table_entry_path_remove2 (u32 fib_index,
         */
        fib_entry_lock(fib_entry_index);
 
-        for (ii = 0; ii < vec_len(rpath); ii++)
+        vec_foreach(rpath, rpaths)
         {
-            fib_table_route_path_fixup(
-                prefix,
-                fib_entry_get_flags_for_source(fib_entry_index,
-                                               source),
-                &rpath[ii]);
+            fib_entry_flag_t eflags;
+
+            eflags = fib_entry_get_flags_for_source(fib_entry_index,
+                                                    source);
+            fib_table_route_path_fixup(prefix, &eflags, rpath);
         }
 
-       src_flag = fib_entry_path_remove(fib_entry_index, source, rpath);
+       src_flag = fib_entry_path_remove(fib_entry_index, source, rpaths);
 
        if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag))
        {
@@ -663,7 +720,7 @@ fib_table_entry_path_remove2 (u32 fib_index,
         */
         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
         {
-            fib_table->ft_src_route_counts[source]--;
+            fib_table_source_count_dec(fib_table, source);
         }
 
        fib_entry_unlock(fib_entry_index);
@@ -704,13 +761,6 @@ fib_table_entry_path_remove (u32 fib_index,
     vec_free(paths);
 }
 
-static int
-fib_route_path_cmp_for_sort (void * v1,
-                            void * v2)
-{
-    return (fib_route_path_cmp(v1, v2));
-}
-
 fib_node_index_t
 fib_table_entry_update (u32 fib_index,
                        const fib_prefix_t *prefix,
@@ -727,7 +777,7 @@ fib_table_entry_update (u32 fib_index,
 
     for (ii = 0; ii < vec_len(paths); ii++)
     {
-       fib_table_route_path_fixup(prefix, flags, &paths[ii]);
+       fib_table_route_path_fixup(prefix, &flags, &paths[ii]);
     }
     /*
      * sort the paths provided by the control plane. this means
@@ -742,7 +792,7 @@ fib_table_entry_update (u32 fib_index,
                                           paths);
 
        fib_table_entry_insert(fib_table, prefix, fib_entry_index);
-        fib_table->ft_src_route_counts[source]++;
+        fib_table_source_count_inc(fib_table, source);
     }
     else
     {
@@ -753,7 +803,7 @@ fib_table_entry_update (u32 fib_index,
 
         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
         {
-            fib_table->ft_src_route_counts[source]++;
+            fib_table_source_count_inc(fib_table, source);
         }
     }
 
@@ -770,7 +820,7 @@ fib_table_entry_update_one_path (u32 fib_index,
                                 u32 next_hop_sw_if_index,
                                 u32 next_hop_fib_index,
                                 u32 next_hop_weight,
-                                mpls_label_t *next_hop_labels,
+                                fib_mpls_label_t *next_hop_labels,
                                 fib_route_path_flags_t path_flags)
 {
     fib_node_index_t fib_entry_index;
@@ -835,7 +885,7 @@ fib_table_entry_delete_i (u32 fib_index,
      */
     if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
     {
-        fib_table->ft_src_route_counts[source]--;
+        fib_table_source_count_dec(fib_table, source);
     }
 
     fib_entry_unlock(fib_entry_index);
@@ -869,12 +919,20 @@ void
 fib_table_entry_delete_index (fib_node_index_t fib_entry_index,
                              fib_source_t source)
 {
-    fib_prefix_t prefix;
+    const fib_prefix_t *prefix;
 
-    fib_entry_get_prefix(fib_entry_index, &prefix);
+    prefix = fib_entry_get_prefix(fib_entry_index);
 
     fib_table_entry_delete_i(fib_entry_get_fib_index(fib_entry_index),
-                             fib_entry_index, &prefix, source);
+                             fib_entry_index, prefix, source);
+}
+
+u32
+fib_table_entry_get_stats_index (u32 fib_index,
+                                 const fib_prefix_t *prefix)
+{
+    return (fib_entry_get_stats_index(
+                fib_table_lookup_exact_match(fib_index, prefix)));
 }
 
 fib_node_index_t
@@ -1033,6 +1091,17 @@ fib_table_get_table_id_for_sw_if_index (fib_protocol_t proto,
     return ((NULL != fib_table ? fib_table->ft_table_id : ~0));
 }
 
+u32
+fib_table_get_table_id (u32 fib_index,
+                        fib_protocol_t proto)
+{
+    fib_table_t *fib_table;
+
+    fib_table = fib_table_get(fib_index, proto);
+
+    return ((NULL != fib_table ? fib_table->ft_table_id : ~0));
+}
+
 u32
 fib_table_find (fib_protocol_t proto,
                u32 table_id)
@@ -1121,7 +1190,6 @@ fib_table_create_and_lock (fib_protocol_t proto,
     fib_node_index_t fi;
     va_list ap;
 
-    va_start(ap, fmt);
 
     switch (proto)
     {
@@ -1129,7 +1197,7 @@ fib_table_create_and_lock (fib_protocol_t proto,
        fi = ip4_fib_table_create_and_lock(src);
         break;
     case FIB_PROTOCOL_IP6:
-       fi = ip6_fib_table_create_and_lock(src);
+       fi = ip6_fib_table_create_and_lock(src, FIB_TABLE_FLAG_NONE, NULL);
         break;
      case FIB_PROTOCOL_MPLS:
        fi = mpls_fib_table_create_and_lock(src);
@@ -1140,6 +1208,8 @@ fib_table_create_and_lock (fib_protocol_t proto,
 
     fib_table = fib_table_get(fi, proto);
 
+    va_start(ap, fmt);
+
     fib_table->ft_desc = va_format(fib_table->ft_desc, fmt, &ap);
 
     va_end(ap);
@@ -1185,6 +1255,42 @@ fib_table_walk (u32 fib_index,
     }
 }
 
+typedef struct fib_table_walk_w_src_ctx_t_
+{
+    fib_table_walk_fn_t fn;
+    void *data;
+    fib_source_t src;
+} fib_table_walk_w_src_cxt_t;
+
+static fib_table_walk_rc_t
+fib_table_walk_w_src_cb (fib_node_index_t fei,
+                         void *arg)
+{
+    fib_table_walk_w_src_cxt_t *ctx = arg;
+
+    if (ctx->src == fib_entry_get_best_source(fei))
+    {
+        return (ctx->fn(fei, ctx->data));
+    }
+    return (FIB_TABLE_WALK_CONTINUE);
+}
+
+void
+fib_table_walk_w_src (u32 fib_index,
+                      fib_protocol_t proto,
+                      fib_source_t src,
+                      fib_table_walk_fn_t fn,
+                      void *data)
+{
+    fib_table_walk_w_src_cxt_t ctx = {
+        .fn = fn,
+        .src = src,
+        .data = data,
+    };
+
+    fib_table_walk(fib_index, proto, fib_table_walk_w_src_cb, &ctx);
+}
+
 void
 fib_table_sub_tree_walk (u32 fib_index,
                          fib_protocol_t proto,
@@ -1205,6 +1311,27 @@ fib_table_sub_tree_walk (u32 fib_index,
     }
 }
 
+static void
+fib_table_lock_dec (fib_table_t *fib_table,
+                    fib_source_t source)
+{
+    vec_validate(fib_table->ft_locks, source);
+
+    fib_table->ft_locks[source]--;
+    fib_table->ft_total_locks--;
+}
+
+static void
+fib_table_lock_inc (fib_table_t *fib_table,
+                    fib_source_t source)
+{
+    vec_validate(fib_table->ft_locks, source);
+
+    ASSERT(fib_table->ft_total_locks < (0xffffffff - 1));
+    fib_table->ft_locks[source]++;
+    fib_table->ft_total_locks++;
+}
+
 void
 fib_table_unlock (u32 fib_index,
                  fib_protocol_t proto,
@@ -1213,19 +1340,9 @@ fib_table_unlock (u32 fib_index,
     fib_table_t *fib_table;
 
     fib_table = fib_table_get(fib_index, proto);
-    fib_table->ft_locks[source]--;
-    fib_table->ft_locks[FIB_TABLE_TOTAL_LOCKS]--;
+    fib_table_lock_dec(fib_table, source);
 
-    if (0 == fib_table->ft_locks[source])
-    {
-        /*
-         * The source no longer needs the table. flush any routes
-         * from it just in case
-         */
-        fib_table_flush(fib_index, proto, source);
-    }
-
-    if (0 == fib_table->ft_locks[FIB_TABLE_TOTAL_LOCKS])
+    if (0 == fib_table->ft_total_locks)
     {
         /*
          * no more locak from any source - kill it
@@ -1242,8 +1359,8 @@ fib_table_lock (u32 fib_index,
     fib_table_t *fib_table;
 
     fib_table = fib_table_get(fib_index, proto);
-    fib_table->ft_locks[source]++;
-    fib_table->ft_locks[FIB_TABLE_TOTAL_LOCKS]++;
+
+    fib_table_lock_inc(fib_table, source);
 }
 
 u32
@@ -1272,6 +1389,26 @@ format_fib_table_name (u8* s, va_list* ap)
     return (s);
 }
 
+u8*
+format_fib_table_flags (u8 *s, va_list *args)
+{
+    fib_table_flags_t flags = va_arg(*args, int);
+    fib_table_attribute_t attr;
+
+    if (!flags)
+    {
+        return format(s, "none");
+    }
+
+    FOR_EACH_FIB_TABLE_ATTRIBUTE(attr) {
+        if (1 << attr & flags) {
+            s = format(s, "%s", fib_table_flags_strings[attr]);
+        }
+    }
+
+    return (s);
+}
+
 /**
  * @brief Table flush context. Store the indicies of matching FIB entries
  * that need to be removed.
@@ -1302,7 +1439,6 @@ fib_table_flush_cb (fib_node_index_t fib_entry_index,
     return (FIB_TABLE_WALK_CONTINUE);
 }
 
-
 void
 fib_table_flush (u32 fib_index,
                 fib_protocol_t proto,
@@ -1326,6 +1462,79 @@ fib_table_flush (u32 fib_index,
     vec_free(ctx.ftf_entries);
 }
 
+static fib_table_walk_rc_t
+fib_table_mark_cb (fib_node_index_t fib_entry_index,
+                   void *arg)
+{
+    fib_table_flush_ctx_t *ctx = arg;
+
+    if (fib_entry_is_sourced(fib_entry_index, ctx->ftf_source))
+    {
+        fib_entry_mark(fib_entry_index, ctx->ftf_source);
+    }
+    return (FIB_TABLE_WALK_CONTINUE);
+}
+
+void
+fib_table_mark (u32 fib_index,
+                fib_protocol_t proto,
+                fib_source_t source)
+{
+    fib_table_flush_ctx_t ctx = {
+        .ftf_source = source,
+    };
+    fib_table_t *fib_table;
+
+    fib_table = fib_table_get(fib_index, proto);
+
+    fib_table->ft_epoch++;
+    fib_table->ft_flags |= FIB_TABLE_FLAG_RESYNC;
+
+    fib_table_walk(fib_index, proto,
+                   fib_table_mark_cb,
+                   &ctx);
+}
+
+static fib_table_walk_rc_t
+fib_table_sweep_cb (fib_node_index_t fib_entry_index,
+                    void *arg)
+{
+    fib_table_flush_ctx_t *ctx = arg;
+
+    if (fib_entry_is_marked(fib_entry_index, ctx->ftf_source))
+    {
+        vec_add1(ctx->ftf_entries, fib_entry_index);
+    }
+    return (FIB_TABLE_WALK_CONTINUE);
+}
+
+void
+fib_table_sweep (u32 fib_index,
+                 fib_protocol_t proto,
+                 fib_source_t source)
+{
+    fib_table_flush_ctx_t ctx = {
+        .ftf_source = source,
+    };
+    fib_node_index_t *fib_entry_index;
+    fib_table_t *fib_table;
+
+    fib_table = fib_table_get(fib_index, proto);
+
+    fib_table->ft_flags &= ~FIB_TABLE_FLAG_RESYNC;
+
+    fib_table_walk(fib_index, proto,
+                   fib_table_sweep_cb,
+                   &ctx);
+
+    vec_foreach(fib_entry_index, ctx.ftf_entries)
+    {
+        fib_table_entry_delete_index(*fib_entry_index, source);
+    }
+
+    vec_free(ctx.ftf_entries);
+}
+
 u8 *
 format_fib_table_memory (u8 *s, va_list *args)
 {