fib: Allow the creation of new source on the API

Type: feature

an client can dump the existing sources, examine their
priorities, then define thier own source.
Usefull if a client wants to distingusih between say, static,
ospf, bgp, etc routes it has added over the API.

Signed-off-by: Neale Ranns <nranns@cisco.com>
Signed-off-by: Alexander Chernavin <achernavin@netgate.com>
Change-Id: I5158b4fa1ebe87381ff8707bb173217f56ea274a
diff --git a/src/vnet/CMakeLists.txt b/src/vnet/CMakeLists.txt
index eecb18c..1ff2a8c 100644
--- a/src/vnet/CMakeLists.txt
+++ b/src/vnet/CMakeLists.txt
@@ -1214,7 +1214,10 @@
   fib/fib_source.h
 )
 
-list(APPEND VNET_API_FILES fib/fib_types.api)
+list(APPEND VNET_API_FILES
+  fib/fib_types.api
+  fib/fib.api
+)
 
 ##############################################################################
 # ADJ
diff --git a/src/vnet/fib/fib.api b/src/vnet/fib/fib.api
new file mode 100644
index 0000000..ad83b74
--- /dev/null
+++ b/src/vnet/fib/fib.api
@@ -0,0 +1,52 @@
+/* Hey Emacs use -*- mode: C -*- */
+/*
+ * Copyright (c) 2018 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+option version = "1.0.0";
+
+import "vnet/fib/fib_types.api";
+
+typedef fib_source
+{
+    u8 priority;
+    u8 id;
+    string name[64];
+};
+
+define fib_source_add
+{
+    u32 client_index;
+    u32 context;
+    vl_api_fib_source_t src;
+};
+
+define fib_source_add_reply
+{
+    u32 context;
+    i32 retval;
+    u8 id;
+};
+
+define fib_source_dump
+{
+    u32 client_index;
+    u32 context;
+};
+
+define fib_source_details
+{
+    u32 context;
+    vl_api_fib_source_t src;
+};
diff --git a/src/vnet/fib/fib_api.c b/src/vnet/fib/fib_api.c
index d626ae2..0254c55 100644
--- a/src/vnet/fib/fib_api.c
+++ b/src/vnet/fib/fib_api.c
@@ -22,23 +22,13 @@
 #include <vnet/bier/bier_disp_table.h>
 #include <vpp/api/types.h>
 #include <vnet/classify/vnet_classify.h>
+#include <vnet/ip/ip_format_fns.h>
 
-#include <vnet/vnet_msg_enum.h>
+#include <vnet/fib/fib.api_enum.h>
+#include <vnet/fib/fib.api_types.h>
 
-#define vl_typedefs		/* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_typedefs
-
-#define vl_endianfun		/* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_endianfun
-
-/* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-#define vl_printfun
-#include <vnet/vnet_all_api_h.h>
-#undef vl_printfun
-
+static u16 fib_base_msg_id;
+#define REPLY_MSG_ID_BASE fib_base_msg_id
 #include <vlibapi/api_helper_macros.h>
 
 int
@@ -461,6 +451,7 @@
                        u8 is_multipath,
                        u32 fib_index,
                        const fib_prefix_t * prefix,
+                       fib_source_t src,
                        fib_entry_flag_t entry_flags,
                        fib_route_path_t *rpaths)
 {
@@ -473,13 +464,13 @@
         if (is_add)
             fib_table_entry_path_add2 (fib_index,
                                        prefix,
-                                       FIB_SOURCE_API,
+                                       src,
                                        entry_flags,
                                        rpaths);
         else
             fib_table_entry_path_remove2 (fib_index,
                                           prefix,
-                                          FIB_SOURCE_API,
+                                          src,
                                           rpaths);
     }
     else
@@ -492,7 +483,7 @@
             /* path replacement */
             fib_table_entry_update (fib_index,
                                     prefix,
-                                    FIB_SOURCE_API,
+                                    src,
                                     entry_flags,
                                     rpaths);
         }
@@ -500,7 +491,7 @@
             /* entry delete */
             fib_table_entry_delete (fib_index,
                                     prefix,
-                                    FIB_SOURCE_API);
+                                    src);
     }
 
     return (0);
@@ -569,3 +560,93 @@
     ASSERT(0);
     return (ADDRESS_IP4);
 }
+
+void
+vl_api_fib_source_add_t_handler (vl_api_fib_source_add_t * mp)
+{
+    vl_api_fib_source_add_reply_t *rmp;
+    fib_source_t src;
+    int rv = 0;
+    u8 *name;
+
+    name = format (0, "%s", mp->src.name);
+    vec_add1 (name, 0);
+
+    src = fib_source_allocate((const char *)name,
+                              mp->src.priority,
+                              FIB_SOURCE_BH_API);
+
+    vec_free(name);
+
+    REPLY_MACRO2 (VL_API_FIB_SOURCE_ADD_REPLY,
+    ({
+        rmp->id = src;
+    }));
+}
+
+typedef struct fib_source_dump_ctx_t_
+{
+    vl_api_registration_t * reg;
+    u32 context;
+} fib_source_dump_ctx_t;
+
+static walk_rc_t
+send_fib_source (fib_source_t id,
+                 const char *name,
+                 fib_source_priority_t prio,
+                 fib_source_behaviour_t bh,
+                 void *data)
+{
+    vl_api_fib_source_details_t *mp;
+    fib_source_dump_ctx_t *ctx;
+
+    ctx = data;
+    mp = vl_msg_api_alloc_zero (sizeof (*mp));
+    if (!mp)
+        return WALK_STOP;
+
+    mp->_vl_msg_id = ntohs (VL_API_FIB_SOURCE_DETAILS + REPLY_MSG_ID_BASE);
+    mp->context = ctx->context;
+
+    mp->src.priority = prio;
+    mp->src.id = id;
+    clib_memcpy(mp->src.name, name,
+                clib_min(strlen(name), ARRAY_LEN(mp->src.name)));
+
+    vl_api_send_msg (ctx->reg, (u8 *) mp);
+
+    return (WALK_CONTINUE);
+}
+
+void
+vl_api_fib_source_dump_t_handler (vl_api_fib_source_dump_t * mp)
+{
+    vl_api_registration_t *reg;
+
+    reg = vl_api_client_index_to_registration (mp->client_index);
+    if (!reg)
+        return;
+
+    fib_source_dump_ctx_t ctx = {
+        .reg = reg,
+        .context = mp->context,
+    };
+
+    fib_source_walk(send_fib_source, &ctx);
+}
+
+
+#include <vnet/fib/fib.api.c>
+
+static clib_error_t *
+fib_api_hookup (vlib_main_t * vm)
+{
+  /*
+   * Set up the (msg_name, crc, message-id) table
+   */
+  fib_base_msg_id = setup_message_id_table ();
+
+  return (NULL);
+}
+
+VLIB_API_INIT_FUNCTION (fib_api_hookup);
diff --git a/src/vnet/fib/fib_api.h b/src/vnet/fib/fib_api.h
index caa993b..7fd7d16 100644
--- a/src/vnet/fib/fib_api.h
+++ b/src/vnet/fib/fib_api.h
@@ -45,6 +45,7 @@
                                   u8 is_multipath,
                                   u32 fib_index,
                                   const fib_prefix_t * prefix,
+                                  fib_source_t src,
                                   fib_entry_flag_t entry_flags,
                                   fib_route_path_t *rpaths);
 
diff --git a/src/vnet/fib/fib_table.c b/src/vnet/fib/fib_table.c
index e71e6c3..eaeee5b 100644
--- a/src/vnet/fib/fib_table.c
+++ b/src/vnet/fib/fib_table.c
@@ -1255,6 +1255,42 @@
     }
 }
 
+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,
diff --git a/src/vnet/fib/fib_table.h b/src/vnet/fib/fib_table.h
index 2011707..11137e1 100644
--- a/src/vnet/fib/fib_table.h
+++ b/src/vnet/fib/fib_table.h
@@ -941,6 +941,17 @@
                            void *ctx);
 
 /**
+ * @brief Walk all entries in a FIB table
+ * N.B: This is NOT safe to deletes. If you need to delete walk the whole
+ * table and store elements in a vector, then delete the elements
+ */
+extern void fib_table_walk_w_src(u32 fib_index,
+                                 fib_protocol_t proto,
+                                 fib_source_t src,
+                                 fib_table_walk_fn_t fn,
+                                 void *ctx);
+
+/**
  * @brief Walk all entries in a sub-tree FIB table. The 'root' paraneter
  * is the prefix at the root of the sub-tree.
  * N.B: This is NOT safe to deletes. If you need to delete walk the whole
diff --git a/src/vnet/fib/fib_types.api b/src/vnet/fib/fib_types.api
index 4a5cea7..c5fbcf8 100644
--- a/src/vnet/fib/fib_types.api
+++ b/src/vnet/fib/fib_types.api
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-option version = "2.0.0";
+option version = "2.0.1";
 import "vnet/ip/ip_types.api";
 
 /** \brief MPLS label
diff --git a/src/vnet/ip/ip.api b/src/vnet/ip/ip.api
index f49fc16..c8d4c39 100644
--- a/src/vnet/ip/ip.api
+++ b/src/vnet/ip/ip.api
@@ -20,7 +20,7 @@
     called through a shared memory interface.
 */
 
-option version = "3.0.3";
+option version = "3.1.0";
 
 import "vnet/interface_types.api";
 import "vnet/fib/fib_types.api";
@@ -135,6 +135,8 @@
   @param stats_index The index of the route in the stats segment
   @param prefix the prefix for the route
   @param n_paths The number of paths the route has
+  @param src The entity adding the route. either 0 for default
+             or a value returned from fib_source_sdd.
   @param paths The paths of the route
 */
 typedef ip_route
@@ -145,6 +147,15 @@
   u8 n_paths;
   vl_api_fib_path_t paths[n_paths];
 };
+typedef ip_route_v2
+{
+  u32 table_id;
+  u32 stats_index;
+  vl_api_prefix_t prefix;
+  u8 n_paths;
+  u8 src;
+  vl_api_fib_path_t paths[n_paths];
+};
 
 /** \brief Add / del route request
     @param client_index - opaque cookie to identify the sender
@@ -163,15 +174,33 @@
   bool is_multipath;
   vl_api_ip_route_t route;
 };
+define ip_route_add_del_v2
+{
+  option in_progress;
+  u32 client_index;
+  u32 context;
+  bool is_add [default=true];
+  bool is_multipath;
+  vl_api_ip_route_v2_t route;
+};
 define ip_route_add_del_reply
 {
   u32 context;
   i32 retval;
   u32 stats_index;
 };
+define ip_route_add_del_v2_reply
+{
+  option in_progress;
+  u32 context;
+  i32 retval;
+  u32 stats_index;
+};
 
 /** \brief Dump IP routes from a table
     @param client_index - opaque cookie to identify the sender
+    @param src The entity adding the route. either 0 for default
+               or a value returned from fib_source_sdd.
     @param table - The table from which to dump routes (ony ID an AF are needed)
 */
 define ip_route_dump
@@ -180,6 +209,15 @@
   u32 context;
   vl_api_ip_table_t table;
 };
+define ip_route_v2_dump
+{
+  option in_progress;
+  u32 client_index;
+  u32 context;
+  /* vl_api_fib_source_t src; */
+  u8 src;
+  vl_api_ip_table_t table;
+};
 
 /** \brief IP FIB table entry response
     @param route The route entry in the table
@@ -189,6 +227,12 @@
   u32 context;
   vl_api_ip_route_t route;
 };
+define ip_route_v2_details
+{
+  option in_progress;
+  u32 context;
+  vl_api_ip_route_v2_t route;
+};
 
 /** \brief Lookup IP route from a table
     @param client_index - opaque cookie to identify the sender
@@ -204,6 +248,15 @@
   u8 exact;
   vl_api_prefix_t prefix;
 };
+define ip_route_lookup_v2
+{
+  option in_progress;
+  u32 client_index;
+  u32 context;
+  u32 table_id;
+  u8 exact;
+  vl_api_prefix_t prefix;
+};
 
 /** \brief IP FIB table lookup response
     @param retval - return code of the lookup
@@ -215,6 +268,13 @@
   i32 retval;
   vl_api_ip_route_t route;
 };
+define ip_route_lookup_v2_reply
+{
+  option in_progress;
+  u32 context;
+  i32 retval;
+  vl_api_ip_route_v2_t route;
+};
 
 /** \brief Set the ip flow hash config for a fib request
     @param client_index - opaque cookie to identify the sender
diff --git a/src/vnet/ip/ip_api.c b/src/vnet/ip/ip_api.c
index 5b87f7c..91b12e5 100644
--- a/src/vnet/ip/ip_api.c
+++ b/src/vnet/ip/ip_api.c
@@ -72,6 +72,7 @@
   _ (SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable)        \
   _ (IP_TABLE_DUMP, ip_table_dump)                                            \
   _ (IP_ROUTE_DUMP, ip_route_dump)                                            \
+  _ (IP_ROUTE_V2_DUMP, ip_route_v2_dump)                                      \
   _ (IP_MTABLE_DUMP, ip_mtable_dump)                                          \
   _ (IP_MROUTE_DUMP, ip_mroute_dump)                                          \
   _ (IP_MROUTE_ADD_DEL, ip_mroute_add_del)                                    \
@@ -83,7 +84,9 @@
   _ (IP_TABLE_REPLACE_END, ip_table_replace_end)                              \
   _ (IP_TABLE_FLUSH, ip_table_flush)                                          \
   _ (IP_ROUTE_ADD_DEL, ip_route_add_del)                                      \
+  _ (IP_ROUTE_ADD_DEL_V2, ip_route_add_del_v2)                                \
   _ (IP_ROUTE_LOOKUP, ip_route_lookup)                                        \
+  _ (IP_ROUTE_LOOKUP_V2, ip_route_lookup_v2)                                  \
   _ (IP_TABLE_ADD_DEL, ip_table_add_del)                                      \
   _ (IP_PUNT_POLICE, ip_punt_police)                                          \
   _ (IP_PUNT_REDIRECT, ip_punt_redirect)                                      \
@@ -235,6 +238,47 @@
   vec_free (rpaths);
 }
 
+static void
+send_ip_route_v2_details (vpe_api_main_t *am, vl_api_registration_t *reg,
+			  u32 context, fib_node_index_t fib_entry_index)
+{
+  fib_route_path_t *rpaths, *rpath;
+  vl_api_ip_route_v2_details_t *mp;
+  const fib_prefix_t *pfx;
+  vl_api_fib_path_t *fp;
+  int path_count;
+
+  rpaths = NULL;
+  pfx = fib_entry_get_prefix (fib_entry_index);
+  rpaths = fib_entry_encode (fib_entry_index);
+
+  path_count = vec_len (rpaths);
+  mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
+  if (!mp)
+    return;
+  clib_memset (mp, 0, sizeof (*mp));
+  mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_V2_DETAILS);
+  mp->context = context;
+
+  ip_prefix_encode (pfx, &mp->route.prefix);
+  mp->route.table_id = htonl (fib_table_get_table_id (
+    fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
+  mp->route.n_paths = path_count;
+  mp->route.src = fib_entry_get_best_source (fib_entry_index);
+  mp->route.stats_index = htonl (fib_table_entry_get_stats_index (
+    fib_entry_get_fib_index (fib_entry_index), pfx));
+
+  fp = mp->route.paths;
+  vec_foreach (rpath, rpaths)
+    {
+      fib_api_path_encode (rpath, fp);
+      fp++;
+    }
+
+  vl_api_send_msg (reg, (u8 *) mp);
+  vec_free (rpaths);
+}
+
 typedef struct apt_ip6_fib_show_ctx_t_
 {
   fib_node_index_t *entries;
@@ -274,6 +318,45 @@
 }
 
 static void
+vl_api_ip_route_v2_dump_t_handler (vl_api_ip_route_v2_dump_t *mp)
+{
+  vpe_api_main_t *am = &vpe_api_main;
+  fib_node_index_t *fib_entry_index;
+  vl_api_registration_t *reg;
+  fib_protocol_t fproto;
+  fib_source_t src;
+  u32 fib_index;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  vl_api_ip_fib_dump_walk_ctx_t ctx = {
+    .feis = NULL,
+  };
+
+  fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
+  fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
+  src = mp->src;
+
+  if (INDEX_INVALID == fib_index)
+    return;
+
+  if (src)
+    fib_table_walk_w_src (fib_index, fproto, src, vl_api_ip_fib_dump_walk,
+			  &ctx);
+  else
+    fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
+
+  vec_foreach (fib_entry_index, ctx.feis)
+    {
+      send_ip_route_v2_details (am, reg, mp->context, *fib_entry_index);
+    }
+
+  vec_free (ctx.feis);
+}
+
+static void
 send_ip_mtable_details (vl_api_registration_t * reg,
 			u32 context, const mfib_table_t * mfib_table)
 {
@@ -584,9 +667,60 @@
 	goto out;
     }
 
-  rv = fib_api_route_add_del (mp->is_add,
-			      mp->is_multipath,
-			      fib_index, &pfx, entry_flags, rpaths);
+  rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
+			      FIB_SOURCE_API, entry_flags, rpaths);
+
+  if (mp->is_add && 0 == rv)
+    *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
+
+out:
+  vec_free (rpaths);
+
+  return (rv);
+}
+
+static int
+ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp,
+			       u32 *stats_index)
+{
+  fib_route_path_t *rpaths = NULL, *rpath;
+  fib_entry_flag_t entry_flags;
+  vl_api_fib_path_t *apath;
+  fib_source_t src;
+  fib_prefix_t pfx;
+  u32 fib_index;
+  int rv, ii;
+
+  entry_flags = FIB_ENTRY_FLAG_NONE;
+  ip_prefix_decode (&mp->route.prefix, &pfx);
+
+  rv = fib_api_table_id_decode (pfx.fp_proto, ntohl (mp->route.table_id),
+				&fib_index);
+  if (0 != rv)
+    goto out;
+
+  if (0 != mp->route.n_paths)
+    vec_validate (rpaths, mp->route.n_paths - 1);
+
+  for (ii = 0; ii < mp->route.n_paths; ii++)
+    {
+      apath = &mp->route.paths[ii];
+      rpath = &rpaths[ii];
+
+      rv = fib_api_path_decode (apath, rpath);
+
+      if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
+	  (~0 == rpath->frp_sw_if_index))
+	entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
+
+      if (0 != rv)
+	goto out;
+    }
+
+  src = (0 == mp->route.src ? FIB_SOURCE_API : mp->route.src);
+
+  rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
+			      src, entry_flags, rpaths);
 
   if (mp->is_add && 0 == rv)
     *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
@@ -615,6 +749,23 @@
 }
 
 void
+vl_api_ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp)
+{
+  vl_api_ip_route_add_del_v2_reply_t *rmp;
+  u32 stats_index = ~0;
+  int rv;
+
+  rv = ip_route_add_del_v2_t_handler (mp, &stats_index);
+
+  /* clang-format off */
+  REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_V2_REPLY,
+  ({
+    rmp->stats_index = htonl (stats_index);
+  }))
+  /* clang-format on */
+}
+
+void
 vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
 {
   vl_api_ip_route_lookup_reply_t *rmp = NULL;
@@ -671,6 +822,65 @@
 }
 
 void
+vl_api_ip_route_lookup_v2_t_handler (vl_api_ip_route_lookup_v2_t *mp)
+{
+  vl_api_ip_route_lookup_v2_reply_t *rmp = NULL;
+  fib_route_path_t *rpaths = NULL, *rpath;
+  const fib_prefix_t *pfx = NULL;
+  fib_prefix_t lookup;
+  vl_api_fib_path_t *fp;
+  fib_node_index_t fib_entry_index;
+  u32 fib_index;
+  int npaths = 0;
+  fib_source_t src = 0;
+  int rv;
+
+  ip_prefix_decode (&mp->prefix, &lookup);
+  rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
+				&fib_index);
+  if (PREDICT_TRUE (!rv))
+    {
+      if (mp->exact)
+	fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
+      else
+	fib_entry_index = fib_table_lookup (fib_index, &lookup);
+      if (fib_entry_index == FIB_NODE_INDEX_INVALID)
+	rv = VNET_API_ERROR_NO_SUCH_ENTRY;
+      else
+	{
+	  pfx = fib_entry_get_prefix (fib_entry_index);
+	  rpaths = fib_entry_encode (fib_entry_index);
+	  npaths = vec_len (rpaths);
+	  src = fib_entry_get_best_source (fib_entry_index);
+	}
+    }
+
+  /* clang-format off */
+  REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_V2_REPLY,
+                    npaths * sizeof (*fp),
+  ({
+    if (!rv)
+      {
+        ip_prefix_encode (pfx, &rmp->route.prefix);
+        rmp->route.table_id = mp->table_id;
+        rmp->route.n_paths = npaths;
+        rmp->route.src = src;
+        rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
+        rmp->route.stats_index = htonl (rmp->route.stats_index);
+
+        fp = rmp->route.paths;
+        vec_foreach (rpath, rpaths)
+          {
+            fib_api_path_encode (rpath, fp);
+            fp++;
+          }
+      }
+  }));
+  /* clang-format on */
+  vec_free (rpaths);
+}
+
+void
 ip_table_create (fib_protocol_t fproto,
 		 u32 table_id, u8 is_api, const u8 * name)
 {
@@ -1797,6 +2007,8 @@
    */
   am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
   am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
+  am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2] = 1;
+  am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2_REPLY] = 1;
 
   /*
    * Set up the (msg_name, crc, message-id) table
diff --git a/src/vnet/mpls/mpls_api.c b/src/vnet/mpls/mpls_api.c
index 25bd353..97928a7 100644
--- a/src/vnet/mpls/mpls_api.c
+++ b/src/vnet/mpls/mpls_api.c
@@ -192,13 +192,11 @@
 	goto out;
     }
 
-  rv = fib_api_route_add_del (mp->mr_is_add,
-			      mp->mr_is_multipath,
-			      fib_index,
-			      &pfx,
-			      (mp->mr_route.mr_is_multicast ?
-			       FIB_ENTRY_FLAG_MULTICAST :
-			       FIB_ENTRY_FLAG_NONE), rpaths);
+  rv = fib_api_route_add_del (
+    mp->mr_is_add, mp->mr_is_multipath, fib_index, &pfx, FIB_SOURCE_API,
+    (mp->mr_route.mr_is_multicast ? FIB_ENTRY_FLAG_MULTICAST :
+				    FIB_ENTRY_FLAG_NONE),
+    rpaths);
 
   if (mp->mr_is_add && 0 == rv)
     *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
diff --git a/test/test_ip6.py b/test/test_ip6.py
index 1672679..db90b84 100644
--- a/test/test_ip6.py
+++ b/test/test_ip6.py
@@ -28,7 +28,7 @@
     VppMRoutePath, VppMplsIpBind, \
     VppMplsRoute, VppMplsTable, VppIpTable, FibPathType, FibPathProto, \
     VppIpInterfaceAddress, find_route_in_dump, find_mroute_in_dump, \
-    VppIp6LinkLocalAddress
+    VppIp6LinkLocalAddress, VppIpRouteV2
 from vpp_neighbor import find_nbr, VppNeighbor
 from vpp_ipip_tun_interface import VppIpIpTunInterface
 from vpp_pg_interface import is_ipv6_misc
@@ -3269,5 +3269,107 @@
         self.send_and_expect(self.pg0, [p_1k], self.pg1)
 
 
+class TestIPFibSource(VppTestCase):
+    """ IPv6 Table FibSource """
+
+    @classmethod
+    def setUpClass(cls):
+        super(TestIPFibSource, cls).setUpClass()
+
+    @classmethod
+    def tearDownClass(cls):
+        super(TestIPFibSource, cls).tearDownClass()
+
+    def setUp(self):
+        super(TestIPFibSource, self).setUp()
+
+        self.create_pg_interfaces(range(2))
+
+        for i in self.pg_interfaces:
+            i.admin_up()
+            i.config_ip6()
+            i.resolve_arp()
+            i.generate_remote_hosts(2)
+            i.configure_ipv6_neighbors()
+
+    def tearDown(self):
+        super(TestIPFibSource, self).tearDown()
+        for i in self.pg_interfaces:
+            i.admin_down()
+            i.unconfig_ip4()
+
+    def test_fib_source(self):
+        """ IP Table FibSource """
+
+        routes = self.vapi.ip_route_v2_dump(0, True)
+
+        # 2 interfaces (4 routes) + 2 specials + 4 neighbours = 10 routes
+        self.assertEqual(len(routes), 10)
+
+        # dump all the sources in the FIB
+        sources = self.vapi.fib_source_dump()
+        for source in sources:
+            if (source.src.name == "API"):
+                api_source = source.src
+            if (source.src.name == "interface"):
+                intf_source = source.src
+            if (source.src.name == "adjacency"):
+                adj_source = source.src
+            if (source.src.name == "special"):
+                special_source = source.src
+            if (source.src.name == "default-route"):
+                dr_source = source.src
+
+        # dump the individual route types
+        routes = self.vapi.ip_route_v2_dump(0, True, src=adj_source.id)
+        self.assertEqual(len(routes), 4)
+        routes = self.vapi.ip_route_v2_dump(0, True, src=intf_source.id)
+        self.assertEqual(len(routes), 4)
+        routes = self.vapi.ip_route_v2_dump(0, True, src=special_source.id)
+        self.assertEqual(len(routes), 1)
+        routes = self.vapi.ip_route_v2_dump(0, True, src=dr_source.id)
+        self.assertEqual(len(routes), 1)
+
+        # add a new soure that'a better than the API
+        self.vapi.fib_source_add(src={'name': "bgp",
+                                      "priority": api_source.priority - 1})
+
+        # dump all the sources to check our new one is there
+        sources = self.vapi.fib_source_dump()
+
+        for source in sources:
+            if (source.src.name == "bgp"):
+                bgp_source = source.src
+
+        self.assertTrue(bgp_source)
+        self.assertEqual(bgp_source.priority,
+                         api_source.priority - 1)
+
+        # add a route with the default API source
+        r1 = VppIpRouteV2(
+            self, "2001::1", 128,
+            [VppRoutePath(self.pg0.remote_ip6,
+                          self.pg0.sw_if_index)]).add_vpp_config()
+
+        r2 = VppIpRouteV2(self, "2001::1", 128,
+                          [VppRoutePath(self.pg1.remote_ip6,
+                                        self.pg1.sw_if_index)],
+                          src=bgp_source.id).add_vpp_config()
+
+        # ensure the BGP source takes priority
+        p = (Ether(src=self.pg0.remote_mac,
+                   dst=self.pg0.local_mac) /
+             IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
+             inet6.UDP(sport=1234, dport=1234) /
+             Raw(b'\xa5' * 100))
+
+        self.send_and_expect(self.pg0, [p], self.pg1)
+
+        r2.remove_vpp_config()
+        r1.remove_vpp_config()
+
+        self.assertFalse(find_route(self, "2001::1", 128))
+
+
 if __name__ == '__main__':
     unittest.main(testRunner=VppTestRunner)
diff --git a/test/vpp_ip_route.py b/test/vpp_ip_route.py
index a50693c..b605948 100644
--- a/test/vpp_ip_route.py
+++ b/test/vpp_ip_route.py
@@ -606,6 +606,110 @@
         return c[0][self.stats_index]
 
 
+class VppIpRouteV2(VppObject):
+    """
+    IP Route V2
+    """
+
+    def __init__(self, test, dest_addr,
+                 dest_addr_len, paths, table_id=0,
+                 register=True, src=0):
+        self._test = test
+        self.paths = paths
+        self.table_id = table_id
+        self.prefix = mk_network(dest_addr, dest_addr_len)
+        self.register = register
+        self.stats_index = None
+        self.modified = False
+        self.src = src
+
+        self.encoded_paths = []
+        for path in self.paths:
+            self.encoded_paths.append(path.encode())
+
+    def __eq__(self, other):
+        if self.table_id == other.table_id and \
+           self.prefix == other.prefix:
+            return True
+        return False
+
+    def modify(self, paths):
+        self.paths = paths
+        self.encoded_paths = []
+        for path in self.paths:
+            self.encoded_paths.append(path.encode())
+        self.modified = True
+
+        self._test.vapi.ip_route_add_del_v2(route={'table_id': self.table_id,
+                                                   'prefix': self.prefix,
+                                                   'src': self.src,
+                                                   'n_paths': len(
+                                                       self.encoded_paths),
+                                                   'paths': self.encoded_paths,
+                                                   },
+                                            is_add=1,
+                                            is_multipath=0)
+
+    def add_vpp_config(self):
+        r = self._test.vapi.ip_route_add_del_v2(
+            route={'table_id': self.table_id,
+                   'prefix': self.prefix,
+                   'n_paths': len(self.encoded_paths),
+                   'paths': self.encoded_paths,
+                   'src': self.src,
+                   },
+            is_add=1,
+            is_multipath=0)
+        self.stats_index = r.stats_index
+        if self.register:
+            self._test.registry.register(self, self._test.logger)
+        return self
+
+    def remove_vpp_config(self):
+        # there's no need to issue different deletes for modified routes
+        # we do this only to test the two different ways to delete routes
+        # eiter by passing all the paths to remove and mutlipath=1 or
+        # passing no paths and multipath=0
+        if self.modified:
+            self._test.vapi.ip_route_add_del_v2(
+                route={'table_id': self.table_id,
+                       'prefix': self.prefix,
+                       'src': self.src,
+                       'n_paths': len(
+                           self.encoded_paths),
+                       'paths': self.encoded_paths},
+                is_add=0,
+                is_multipath=1)
+        else:
+            self._test.vapi.ip_route_add_del_v2(
+                route={'table_id': self.table_id,
+                       'prefix': self.prefix,
+                       'src': self.src,
+                       'n_paths': 0},
+                is_add=0,
+                is_multipath=0)
+
+    def query_vpp_config(self):
+        return find_route(self._test,
+                          self.prefix.network_address,
+                          self.prefix.prefixlen,
+                          self.table_id)
+
+    def object_id(self):
+        return ("%s:table-%d-%s" % (
+            'ip6-route' if self.prefix.version == 6 else 'ip-route',
+                self.table_id,
+                self.prefix))
+
+    def get_stats_to(self):
+        c = self._test.statistics.get_counter("/net/route/to")
+        return c[0][self.stats_index]
+
+    def get_stats_via(self):
+        c = self._test.statistics.get_counter("/net/route/via")
+        return c[0][self.stats_index]
+
+
 class VppIpMRoute(VppObject):
     """
     IP Multicast Route
diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py
index da693c7..d677ab3 100644
--- a/test/vpp_papi_provider.py
+++ b/test/vpp_papi_provider.py
@@ -48,7 +48,6 @@
                          'classify_table_index': 4294967295, 'is_add': 1, },
     'ip_mroute_add_del': {'is_add': 1, },
     'ip_neighbor_add_del': {'is_add': 1, },
-    'ip_route_add_del': {'is_add': 1, },
     'ipsec_interface_add_del_spd': {'is_add': 1, },
     'ipsec_spd_add_del': {'is_add': 1, },
     'ipsec_spd_dump': {'sa_id': 4294967295, },
@@ -410,6 +409,16 @@
                             'is_ip6': is_ip6
                         }})
 
+    def ip_route_v2_dump(self, table_id, is_ip6=False, src=0):
+        return self.api(self.papi.ip_route_v2_dump,
+                        {
+                            'src': src,
+                            'table': {
+                                'table_id': table_id,
+                                'is_ip6': is_ip6
+                            }
+                        })
+
     def ip_neighbor_add_del(self,
                             sw_if_index,
                             mac_address,