FIB table add/delete API
[vpp.git] / src / vnet / mpls / mpls_api.c
index 22fb7d4..38f5b01 100644 (file)
@@ -50,6 +50,7 @@
 #define foreach_vpe_api_msg                                 \
 _(MPLS_IP_BIND_UNBIND, mpls_ip_bind_unbind)                 \
 _(MPLS_ROUTE_ADD_DEL, mpls_route_add_del)                   \
+_(MPLS_TABLE_ADD_DEL, mpls_table_add_del)                   \
 _(MPLS_TUNNEL_ADD_DEL, mpls_tunnel_add_del)                 \
 _(MPLS_TUNNEL_DUMP, mpls_tunnel_dump)                       \
 _(MPLS_FIB_DUMP, mpls_fib_dump)
@@ -57,6 +58,49 @@ _(MPLS_FIB_DUMP, mpls_fib_dump)
 extern void stats_dslock_with_hint (int hint, int tag);
 extern void stats_dsunlock (void);
 
+void
+mpls_table_delete (u32 table_id, u8 is_api)
+{
+  u32 fib_index;
+
+  /*
+   * The MPLS defult table must also be explicitly created via the API.
+   * So in contrast to IP, it gets no special treatment here.
+   *
+   * The API holds only one lock on the table.
+   * i.e. it can be added many times via the API but needs to be
+   * deleted only once.
+   */
+  fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
+
+  if (~0 != fib_index)
+    {
+      fib_table_unlock (fib_index,
+                       FIB_PROTOCOL_MPLS,
+                       (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
+    }
+}
+
+void
+vl_api_mpls_table_add_del_t_handler (vl_api_mpls_table_add_del_t * mp)
+{
+  vl_api_mpls_table_add_del_reply_t *rmp;
+  vnet_main_t *vnm;
+  int rv = 0;
+
+  vnm = vnet_get_main ();
+  vnm->api_errno = 0;
+
+  if (mp->mt_is_add)
+    mpls_table_create (ntohl (mp->mt_table_id), 1);
+  else
+    mpls_table_delete (ntohl (mp->mt_table_id), 1);
+
+  rv = (rv == 0) ? vnm->api_errno : rv;
+
+  REPLY_MACRO (VL_API_MPLS_TABLE_ADD_DEL_REPLY);
+}
+
 static int
 mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
                             vl_api_mpls_ip_bind_unbind_t * mp)
@@ -68,14 +112,7 @@ mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
 
   if (~0 == mpls_fib_index)
     {
-      if (mp->mb_create_table_if_needed)
-       {
-         mpls_fib_index =
-           fib_table_find_or_create_and_lock (FIB_PROTOCOL_MPLS,
-                                              ntohl (mp->mb_mpls_table_id));
-       }
-      else
-       return VNET_API_ERROR_NO_SUCH_FIB;
+      return VNET_API_ERROR_NO_SUCH_FIB;
     }
 
   ip_fib_index = fib_table_find ((mp->mb_is_ip4 ?
@@ -144,14 +181,7 @@ mpls_route_add_del_t_handler (vnet_main_t * vnm,
   };
   if (pfx.fp_eos)
     {
-      if (mp->mr_next_hop_proto_is_ip4)
-       {
-         pfx.fp_payload_proto = DPO_PROTO_IP4;
-       }
-      else
-       {
-         pfx.fp_payload_proto = DPO_PROTO_IP6;
-       }
+      pfx.fp_payload_proto = mp->mr_next_hop_proto;
     }
   else
     {
@@ -161,9 +191,8 @@ mpls_route_add_del_t_handler (vnet_main_t * vnm,
   rv = add_del_route_check (FIB_PROTOCOL_MPLS,
                            mp->mr_table_id,
                            mp->mr_next_hop_sw_if_index,
-                           dpo_proto_to_fib (pfx.fp_payload_proto),
+                           pfx.fp_payload_proto,
                            mp->mr_next_hop_table_id,
-                           mp->mr_create_table_if_needed,
                            mp->mr_is_rpf_id,
                            &fib_index, &next_hop_fib_index);
 
@@ -173,9 +202,9 @@ mpls_route_add_del_t_handler (vnet_main_t * vnm,
   ip46_address_t nh;
   memset (&nh, 0, sizeof (nh));
 
-  if (mp->mr_next_hop_proto_is_ip4)
+  if (DPO_PROTO_IP4 == mp->mr_next_hop_proto)
     memcpy (&nh.ip4, mp->mr_next_hop, sizeof (nh.ip4));
-  else
+  else if (DPO_PROTO_IP6 == mp->mr_next_hop_proto)
     memcpy (&nh.ip6, mp->mr_next_hop, sizeof (nh.ip6));
 
   n_labels = mp->mr_next_hop_n_out_labels;
@@ -202,7 +231,7 @@ mpls_route_add_del_t_handler (vnet_main_t * vnm,
                                   mp->mr_is_interface_rx,
                                   mp->mr_is_rpf_id,
                                   fib_index, &pfx,
-                                  mp->mr_next_hop_proto_is_ip4,
+                                  mp->mr_next_hop_proto,
                                   &nh, ntohl (mp->mr_next_hop_sw_if_index),
                                   next_hop_fib_index,
                                   mp->mr_next_hop_weight,
@@ -228,6 +257,32 @@ vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
   REPLY_MACRO (VL_API_MPLS_ROUTE_ADD_DEL_REPLY);
 }
 
+void
+mpls_table_create (u32 table_id, u8 is_api)
+{
+  u32 fib_index;
+
+  /*
+   * The MPLS defult table must also be explicitly created via the API.
+   * So in contrast to IP, it gets no special treatment here.
+   */
+
+  /*
+   * The API holds only one lock on the table.
+   * i.e. it can be added many times via the API but needs to be
+   * deleted only once.
+   */
+  fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
+
+  if (~0 == fib_index)
+    {
+      fib_table_find_or_create_and_lock (FIB_PROTOCOL_MPLS,
+                                        table_id,
+                                        (is_api ?
+                                         FIB_SOURCE_API : FIB_SOURCE_CLI));
+    }
+}
+
 static void
 vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
 {
@@ -243,13 +298,13 @@ vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
 
   if (mp->mt_next_hop_proto_is_ip4)
     {
-      rpath.frp_proto = FIB_PROTOCOL_IP4;
+      rpath.frp_proto = DPO_PROTO_IP4;
       clib_memcpy (&rpath.frp_addr.ip4,
                   mp->mt_next_hop, sizeof (rpath.frp_addr.ip4));
     }
   else
     {
-      rpath.frp_proto = FIB_PROTOCOL_IP6;
+      rpath.frp_proto = DPO_PROTO_IP6;
       clib_memcpy (&rpath.frp_addr.ip6,
                   mp->mt_next_hop, sizeof (rpath.frp_addr.ip6));
     }
@@ -334,7 +389,8 @@ send_mpls_tunnel_entry (u32 mti, void *arg)
   {
     memset (fp, 0, sizeof (*fp));
 
-    fp->weight = htonl (api_rpath->rpath.frp_weight);
+    fp->weight = api_rpath->rpath.frp_weight;
+    fp->preference = api_rpath->rpath.frp_preference;
     fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
     copy_fib_next_hop (api_rpath, fp);
     fp++;
@@ -393,7 +449,8 @@ send_mpls_fib_details (vpe_api_main_t * am,
   vec_foreach (api_rpath, api_rpaths)
   {
     memset (fp, 0, sizeof (*fp));
-    fp->weight = htonl (api_rpath->rpath.frp_weight);
+    fp->weight = api_rpath->rpath.frp_weight;
+    fp->preference = api_rpath->rpath.frp_preference;
     fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
     copy_fib_next_hop (api_rpath, fp);
     fp++;