fib: fib api updates

Enhance the route add/del APIs to take a set of paths rather than just one.
Most unicast routing protocols calcualte all the available paths in one
run of the algorithm so updating all the paths at once is beneficial for the client.
two knobs control the behaviour:
  is_multipath - if set the the set of paths passed will be added to those
                 that already exist, otherwise the set will replace them.
  is_add - add or remove the set

is_add=0, is_multipath=1 and an empty set, results in deleting the route.

It is also considerably faster to add multiple paths at once, than one at a time:

vat# ip_add_del_route 1.1.1.1/32 count 100000 multipath via 10.10.10.11
100000 routes in .572240 secs, 174751.80 routes/sec
vat# ip_add_del_route 1.1.1.1/32 count 100000 multipath via 10.10.10.12
100000 routes in .528383 secs, 189256.54 routes/sec
vat# ip_add_del_route 1.1.1.1/32 count 100000 multipath via 10.10.10.13
100000 routes in .757131 secs, 132077.52 routes/sec
vat# ip_add_del_route 1.1.1.1/32 count 100000 multipath via 10.10.10.14
100000 routes in .878317 secs, 113854.12 routes/sec

vat# ip_route_add_del 1.1.1.1/32 count 100000 multipath via 10.10.10.11 via 10.10.10.12 via 10.10.10.13 via 10.10.10.14
100000 routes in .900212 secs, 111084.93 routes/sec

Change-Id: I416b93f7684745099c1adb0b33edac58c9339c1a
Signed-off-by: Neale Ranns <neale.ranns@cisco.com>
Signed-off-by: Ole Troan <ot@cisco.com>
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
diff --git a/src/vnet/mfib/mfib_entry.c b/src/vnet/mfib/mfib_entry.c
index 1856221..f169dc0 100644
--- a/src/vnet/mfib/mfib_entry.c
+++ b/src/vnet/mfib/mfib_entry.c
@@ -673,21 +673,12 @@
                   &bw_ctx);
 }
 
-static fib_node_index_t
-mfib_entry_src_path_add (mfib_entry_src_t *msrc,
-                         const fib_route_path_t *rpath)
+static fib_node_index_t*
+mfib_entry_src_paths_add (mfib_entry_src_t *msrc,
+                          const fib_route_path_t *rpaths)
 {
-    fib_node_index_t path_index;
-    fib_route_path_t *rpaths;
-
     ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
 
-    /*
-     * path-lists require a vector of paths
-     */
-    rpaths = NULL;
-    vec_add1(rpaths, rpath[0]);
-
     if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
     {
         /* A non-shared path-list */
@@ -696,33 +687,16 @@
         fib_path_list_lock(msrc->mfes_pl);
     }
 
-    path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
-
-    vec_free(rpaths);
-
-    return (path_index);
+    return (fib_path_list_paths_add(msrc->mfes_pl, rpaths));
 }
 
-static fib_node_index_t
-mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
-                            const fib_route_path_t *rpath)
+static fib_node_index_t*
+mfib_entry_src_paths_remove (mfib_entry_src_t *msrc,
+                             const fib_route_path_t *rpaths)
 {
-    fib_node_index_t path_index;
-    fib_route_path_t *rpaths;
-
     ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
 
-    /*
-     * path-lists require a vector of paths
-     */
-    rpaths = NULL;
-    vec_add1(rpaths, rpath[0]);
-
-    path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
-
-    vec_free(rpaths);
-
-    return (path_index);
+    return (fib_path_list_paths_remove(msrc->mfes_pl, rpaths));
 }
 
 static void
@@ -819,7 +793,12 @@
 {
     return ((INDEX_INVALID == msrc->mfes_cover &&
              MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
-             0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
+             0 == fib_path_list_get_n_paths(msrc->mfes_pl)) &&
+            (0 == hash_elts(msrc->mfes_itfs)));
+
+    /* return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags) && */
+    /*         (0 == fib_path_list_get_n_paths(msrc->mfes_pl)) && */
+    /*         (0 == hash_elts(msrc->mfes_itfs))); */
 }
 
 
@@ -931,18 +910,26 @@
     hash_unset(msrc->mfes_itfs, sw_if_index);
 }
 
+static int
+mfib_entry_path_itf_based (const fib_route_path_t *rpath)
+{
+    return (!(rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP) &&
+            ~0 != rpath->frp_sw_if_index);
+}
+
 void
 mfib_entry_path_update (fib_node_index_t mfib_entry_index,
                         mfib_source_t source,
-                        const fib_route_path_t *rpath,
-                        mfib_itf_flags_t itf_flags)
+                        const fib_route_path_t *rpaths)
 {
-    fib_node_index_t path_index;
+    fib_node_index_t* path_indices, path_index;
+    const fib_route_path_t *rpath;
     mfib_source_t current_best;
     mfib_path_ext_t *path_ext;
     mfib_entry_t *mfib_entry;
     mfib_entry_src_t *msrc;
     mfib_itf_flags_t old;
+    u32 ii;
 
     mfib_entry = mfib_entry_get(mfib_entry_index);
     ASSERT(NULL != mfib_entry);
@@ -953,61 +940,73 @@
      * add the path to the path-list. If it's a duplicate we'll get
      * back the original path.
      */
-    path_index = mfib_entry_src_path_add(msrc, rpath);
+    path_indices = mfib_entry_src_paths_add(msrc, rpaths);
 
-    /*
-     * find the path extension for that path
-     */
-    path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
-
-    if (NULL == path_ext)
+    vec_foreach_index(ii, path_indices)
     {
-        old = MFIB_ITF_FLAG_NONE;
-        path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
-    }
-    else
-    {
-        old = path_ext->mfpe_flags;
-        path_ext->mfpe_flags = itf_flags;
-    }
+        path_index = path_indices[ii];
+        rpath = &rpaths[ii];
 
-    /*
-     * Has the path changed its contribution to the input interface set.
-     * Which only paths with interfaces can do...
-     */
-    if (~0 != rpath[0].frp_sw_if_index)
-    {
-        mfib_itf_t *mfib_itf;
+        if (FIB_NODE_INDEX_INVALID == path_index)
+            continue;
 
-        if (old != itf_flags)
+        /*
+         * find the path extension for that path
+         */
+        path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
+
+        if (NULL == path_ext)
         {
-            /*
-             * change of flag contributions
-             */
-            mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
-                                           rpath[0].frp_sw_if_index);
+            old = MFIB_ITF_FLAG_NONE;
+            path_ext = mfib_path_ext_add(msrc, path_index,
+                                         rpath->frp_mitf_flags);
+        }
+        else
+        {
+            old = path_ext->mfpe_flags;
+            path_ext->mfpe_flags = rpath->frp_mitf_flags;
+        }
 
-            if (NULL == mfib_itf)
+        /*
+         * Has the path changed its contribution to the input interface set.
+         * Which only paths with interfaces can do...
+         */
+        if (mfib_entry_path_itf_based(rpath))
+        {
+            mfib_itf_t *mfib_itf;
+
+            if (old != rpath->frp_mitf_flags)
             {
-                mfib_entry_itf_add(msrc,
-                                   rpath[0].frp_sw_if_index,
-                                   mfib_itf_create(path_index, itf_flags));
-            }
-            else
-            {
-                if (mfib_itf_update(mfib_itf,
-                                    path_index,
-                                    itf_flags))
+                /*
+                 * change of flag contributions
+                 */
+                mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
+                                               rpath->frp_sw_if_index);
+
+                if (NULL == mfib_itf)
                 {
-                    /*
-                     * no more interface flags on this path, remove
-                     * from the data-plane set
-                     */
-                    mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
+                    mfib_entry_itf_add(msrc,
+                                       rpath->frp_sw_if_index,
+                                       mfib_itf_create(path_index,
+                                                       rpath->frp_mitf_flags));
+                }
+                else
+                {
+                    if (mfib_itf_update(mfib_itf,
+                                        path_index,
+                                        rpath->frp_mitf_flags))
+                    {
+                        /*
+                         * no more interface flags on this path, remove
+                         * from the data-plane set
+                         */
+                        mfib_entry_itf_remove(msrc, rpath->frp_sw_if_index);
+                    }
                 }
             }
         }
     }
+    vec_free(path_indices);
 
     mfib_entry_recalculate_forwarding(mfib_entry, current_best);
 }
@@ -1021,12 +1020,14 @@
 int
 mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
                         mfib_source_t source,
-                        const fib_route_path_t *rpath)
+                        const fib_route_path_t *rpaths)
 {
-    fib_node_index_t path_index;
+    fib_node_index_t path_index, *path_indices;
+    const fib_route_path_t *rpath;
     mfib_source_t current_best;
     mfib_entry_t *mfib_entry;
     mfib_entry_src_t *msrc;
+    u32 ii;
 
     mfib_entry = mfib_entry_get(mfib_entry_index);
     ASSERT(NULL != mfib_entry);
@@ -1042,23 +1043,29 @@
     }
 
     /*
-     * remove the path from the path-list. If it's not there we'll get
-     * back invalid
+     * remove the paths from the path-list. If it's not there we'll get
+     * back an empty vector
      */
-    path_index = mfib_entry_src_path_remove(msrc, rpath);
+    path_indices = mfib_entry_src_paths_remove(msrc, rpaths);
 
-    if (FIB_NODE_INDEX_INVALID != path_index)
+    vec_foreach_index(ii, path_indices)
     {
+        path_index = path_indices[ii];
+        rpath = &rpaths[ii];
+
+        if (FIB_NODE_INDEX_INVALID == path_index)
+            continue;
+      
         /*
          * don't need the extension, nor the interface anymore
          */
         mfib_path_ext_remove(msrc, path_index);
-        if (~0 != rpath[0].frp_sw_if_index)
+        if (mfib_entry_path_itf_based(rpath))
         {
             mfib_itf_t *mfib_itf;
 
             mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
-                                           rpath[0].frp_sw_if_index);
+                                           rpath->frp_sw_if_index);
 
             if (mfib_itf_update(mfib_itf,
                                 path_index,
@@ -1068,19 +1075,20 @@
                  * no more interface flags on this path, remove
                  * from the data-plane set
                  */
-                mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
+                mfib_entry_itf_remove(msrc, rpath->frp_sw_if_index);
             }
         }
-    }
 
-    if (mfib_entry_src_ok_for_delete(msrc))
-    {
-        /*
-         * this source has no interfaces and no flags.
-         * it has nothing left to give - remove it
-         */
-        mfib_entry_src_remove(mfib_entry, source);
+        if (mfib_entry_src_ok_for_delete(msrc))
+        {
+            /*
+             * this source has no interfaces and no flags.
+             * it has nothing left to give - remove it
+             */
+            mfib_entry_src_remove(mfib_entry, source);
+        }
     }
+    vec_free(path_indices);
 
     mfib_entry_recalculate_forwarding(mfib_entry, current_best);
 
@@ -1321,12 +1329,14 @@
     mfib_entry_logger = vlib_log_register_class("mfib", "entry");
 }
 
-void
-mfib_entry_encode (fib_node_index_t mfib_entry_index,
-                  fib_route_path_encode_t **api_rpaths)
+fib_route_path_t*
+mfib_entry_encode (fib_node_index_t mfib_entry_index)
 {
-    fib_route_path_encode_t *api_rpath;
+    fib_path_encode_ctx_t ctx = {
+        .rpaths = NULL,
+    };
     mfib_entry_t *mfib_entry;
+    fib_route_path_t *rpath;
     mfib_entry_src_t *bsrc;
 
     mfib_entry = mfib_entry_get(mfib_entry_index);
@@ -1337,20 +1347,22 @@
         fib_path_list_walk_w_ext(bsrc->mfes_pl,
                                  NULL,
                                  fib_path_encode,
-                                 api_rpaths);
+                                 &ctx);
     }
 
-    vec_foreach(api_rpath, *api_rpaths)
+    vec_foreach(rpath, ctx.rpaths)
     {
         mfib_itf_t *mfib_itf;
 
         mfib_itf = mfib_entry_itf_find(bsrc->mfes_itfs,
-                                       api_rpath->rpath.frp_sw_if_index);
+                                       rpath->frp_sw_if_index);
         if (mfib_itf)
         {
-            api_rpath->rpath.frp_mitf_flags = mfib_itf->mfi_flags;
+            rpath->frp_mitf_flags = mfib_itf->mfi_flags;
         }
     }
+
+    return (ctx.rpaths);
 }
 
 const mfib_prefix_t *