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/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