FIB: encode the label stack in the FIB path during table dump
[vpp.git] / src / vnet / mpls / mpls_api.c
index a35e94d..4ac5ea7 100644 (file)
@@ -168,7 +168,8 @@ vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
 
 static int
 mpls_route_add_del_t_handler (vnet_main_t * vnm,
-                             vl_api_mpls_route_add_del_t * mp)
+                             vl_api_mpls_route_add_del_t * mp,
+                             u32 * stats_index)
 {
   fib_mpls_label_t *label_stack = NULL;
   u32 fib_index, next_hop_fib_index;
@@ -201,7 +202,7 @@ mpls_route_add_del_t_handler (vnet_main_t * vnm,
     return (rv);
 
   ip46_address_t nh;
-  memset (&nh, 0, sizeof (nh));
+  clib_memset (&nh, 0, sizeof (nh));
 
   if (DPO_PROTO_IP4 == mp->mr_next_hop_proto)
     memcpy (&nh.ip4, mp->mr_next_hop, sizeof (nh.ip4));
@@ -227,31 +228,36 @@ mpls_route_add_del_t_handler (vnet_main_t * vnm,
     }
 
   /* *INDENT-OFF* */
-  return (add_del_route_t_handler (mp->mr_is_multipath, mp->mr_is_add,
-                                   0,  // mp->is_drop,
-                                  0,   // mp->is_unreach,
-                                  0,   // mp->is_prohibit,
-                                  0,   // mp->is_local,
-                                  mp->mr_is_multicast,
-                                   mp->mr_is_classify,
-                                   mp->mr_classify_table_index,
-                                   mp->mr_is_resolve_host,
-                                   mp->mr_is_resolve_attached,
-                                   mp->mr_is_interface_rx,
-                                   mp->mr_is_rpf_id,
-                                   0,  // l2_bridged
-                                   0,   // is source_lookup
-                                   0,   // is_udp_encap
+  rv = add_del_route_t_handler (mp->mr_is_multipath, mp->mr_is_add,
+                                0,     // mp->is_drop,
+                                0,     // mp->is_unreach,
+                                0,     // mp->is_prohibit,
+                                0,     // mp->is_local,
+                                mp->mr_is_multicast,
+                                mp->mr_is_classify,
+                                mp->mr_classify_table_index,
+                                mp->mr_is_resolve_host,
+                                mp->mr_is_resolve_attached,
+                                mp->mr_is_interface_rx,
+                                mp->mr_is_rpf_id,
+                                0,     // l2_bridged
+                                0,   // is source_lookup
+                                0,   // is_udp_encap
                                   fib_index, &pfx,
-                                  mp->mr_next_hop_proto,
-                                  &nh, ~0, // next_hop_id
-                                   ntohl (mp->mr_next_hop_sw_if_index),
-                                  next_hop_fib_index,
-                                  mp->mr_next_hop_weight,
-                                  mp->mr_next_hop_preference,
-                                  ntohl (mp->mr_next_hop_via_label),
-                                  label_stack));
+                                mp->mr_next_hop_proto,
+                                &nh, ~0, // next_hop_id
+                                ntohl (mp->mr_next_hop_sw_if_index),
+                                next_hop_fib_index,
+                                mp->mr_next_hop_weight,
+                                mp->mr_next_hop_preference,
+                                ntohl (mp->mr_next_hop_via_label),
+                                label_stack);
   /* *INDENT-ON* */
+
+  if (mp->mr_is_add && 0 == rv)
+    *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
+
+  return (rv);
 }
 
 void
@@ -259,16 +265,20 @@ vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
 {
   vl_api_mpls_route_add_del_reply_t *rmp;
   vnet_main_t *vnm;
+  u32 stats_index;
   int rv;
 
   vnm = vnet_get_main ();
-  vnm->api_errno = 0;
+  stats_index = ~0;
 
-  rv = mpls_route_add_del_t_handler (vnm, mp);
+  rv = mpls_route_add_del_t_handler (vnm, mp, &stats_index);
 
-  rv = (rv == 0) ? vnm->api_errno : rv;
-
-  REPLY_MACRO (VL_API_MPLS_ROUTE_ADD_DEL_REPLY);
+  /* *INDENT-OFF* */
+  REPLY_MACRO2 (VL_API_MPLS_ROUTE_ADD_DEL_REPLY,
+  ({
+    rmp->stats_index = htonl (stats_index);
+  }));
+  /* *INDENT-ON* */
 }
 
 void
@@ -301,13 +311,12 @@ mpls_table_create (u32 table_id, u8 is_api, const u8 * name)
 static void
 vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
 {
+  u32 tunnel_sw_if_index = ~0, tunnel_index = ~0, next_hop_via_label;
   vl_api_mpls_tunnel_add_del_reply_t *rmp;
-  int rv = 0;
-  u32 tunnel_sw_if_index;
-  int ii;
   fib_route_path_t rpath, *rpaths = NULL;
+  int ii, rv = 0;
 
-  memset (&rpath, 0, sizeof (rpath));
+  clib_memset (&rpath, 0, sizeof (rpath));
 
   stats_dslock_with_hint (1 /* release hint */ , 5 /* tag */ );
 
@@ -324,7 +333,28 @@ vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
                   mp->mt_next_hop, sizeof (rpath.frp_addr.ip6));
     }
   rpath.frp_sw_if_index = ntohl (mp->mt_next_hop_sw_if_index);
-  rpath.frp_weight = 1;
+  rpath.frp_weight = mp->mt_next_hop_weight;
+  rpath.frp_preference = mp->mt_next_hop_preference;
+
+  next_hop_via_label = ntohl (mp->mt_next_hop_via_label);
+  if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
+    {
+      rpath.frp_proto = DPO_PROTO_MPLS;
+      rpath.frp_local_label = next_hop_via_label;
+      rpath.frp_eos = MPLS_NON_EOS;
+    }
+
+  if (rpath.frp_sw_if_index == ~0)
+    {                          /* recursive path, set fib index */
+      rpath.frp_fib_index =
+       fib_table_find (dpo_proto_to_fib (rpath.frp_proto),
+                       ntohl (mp->mt_next_hop_table_id));
+      if (rpath.frp_fib_index == ~0)
+       {
+         rv = VNET_API_ERROR_NO_SUCH_FIB;
+         goto out;
+       }
+    }
 
   if (mp->mt_is_add)
     {
@@ -351,9 +381,12 @@ vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
        tunnel_sw_if_index = vnet_mpls_tunnel_create (mp->mt_l2_only,
                                                      mp->mt_is_multicast);
       vnet_mpls_tunnel_path_add (tunnel_sw_if_index, rpaths);
+
+      tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
     }
   else
     {
+      tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
       tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
       if (!vnet_mpls_tunnel_path_remove (tunnel_sw_if_index, rpaths))
        vnet_mpls_tunnel_del (tunnel_sw_if_index);
@@ -363,10 +396,12 @@ vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
 
   stats_dsunlock ();
 
+out:
   /* *INDENT-OFF* */
   REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
   ({
     rmp->sw_if_index = ntohl(tunnel_sw_if_index);
+    rmp->tunnel_index = ntohl(tunnel_index);
   }));
   /* *INDENT-ON* */
 }
@@ -391,7 +426,7 @@ static void
 typedef struct mpls_tunnel_send_walk_ctx_t_
 {
   vl_api_registration_t *reg;
-  u32 index;
+  u32 sw_if_index;
   u32 context;
 } mpls_tunnel_send_walk_ctx_t;
 
@@ -407,22 +442,25 @@ send_mpls_tunnel_entry (u32 mti, void *arg)
 
   ctx = arg;
 
-  if (~0 != ctx->index && mti != ctx->index)
+  mt = mpls_tunnel_get (mti);
+
+  if (~0 != ctx->sw_if_index && mt->mt_sw_if_index != ctx->sw_if_index)
     return;
 
-  mt = mpls_tunnel_get (mti);
   n = fib_path_list_get_n_paths (mt->mt_path_list);
 
   mp = vl_msg_api_alloc (sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
-  memset (mp, 0, sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
+  clib_memset (mp, 0, sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
 
   mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS);
   mp->context = ctx->context;
 
   mp->mt_tunnel_index = ntohl (mti);
+  mp->mt_sw_if_index = ntohl (mt->mt_sw_if_index);
   mp->mt_count = ntohl (n);
 
-  fib_path_list_walk (mt->mt_path_list, fib_path_encode, &api_rpaths);
+  fib_path_list_walk_w_ext (mt->mt_path_list,
+                           &mt->mt_path_exts, fib_path_encode, &api_rpaths);
 
   fp = mp->mt_paths;
   vec_foreach (api_rpath, api_rpaths)
@@ -431,11 +469,6 @@ send_mpls_tunnel_entry (u32 mti, void *arg)
     fp++;
   }
 
-  // FIXME
-  // memcpy (mp->mt_next_hop_out_labels,
-  //   mt->mt_label_stack, nlabels * sizeof (u32));
-
-
   vl_api_send_msg (ctx->reg, (u8 *) mp);
 }
 
@@ -450,7 +483,7 @@ vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp)
 
   mpls_tunnel_send_walk_ctx_t ctx = {
     .reg = reg,
-    .index = ntohl (mp->tunnel_index),
+    .sw_if_index = ntohl (mp->sw_if_index),
     .context = mp->context,
   };
   mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx);
@@ -472,7 +505,7 @@ send_mpls_fib_details (vpe_api_main_t * am,
   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
   if (!mp)
     return;
-  memset (mp, 0, sizeof (*mp));
+  clib_memset (mp, 0, sizeof (*mp));
   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DETAILS);
   mp->context = context;