ip: fix fib and mfib locks
[vpp.git] / src / vnet / interface_api.c
index 5b2f2fc..bbb6168 100644 (file)
 #include <vnet/fib/fib_table.h>
 #include <vnet/mfib/mfib_table.h>
 #include <vnet/l2/l2_vtr.h>
-#include <vnet/vnet_msg_enum.h>
 #include <vnet/fib/fib_api.h>
 #include <vnet/mfib/mfib_table.h>
-
 #include <vlibapi/api_types.h>
 
+#include <vnet/format_fns.h>
 #include <vnet/ip/ip_types_api.h>
 #include <vnet/ethernet/ethernet_types_api.h>
 
-#define vl_typedefs            /* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_typedefs
+#include <interface.api_enum.h>
+#include <interface.api_types.h>
 
-#define vl_endianfun           /* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_endianfun
+#define REPLY_MSG_ID_BASE msg_id_base
+#include <vlibapi/api_helper_macros.h>
 
-/* 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 msg_id_base;
 
-#include <vlibapi/api_helper_macros.h>
 vpe_api_main_t vpe_api_main;
 
 #define foreach_vpe_api_msg                                                   \
@@ -241,7 +233,7 @@ send_sw_interface_details (vpe_api_main_t * am,
 
   vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
+  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_DETAILS);
   mp->sw_if_index = ntohl (swif->sw_if_index);
   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
 
@@ -267,8 +259,8 @@ send_sw_interface_details (vpe_api_main_t * am,
     }
   mp->type = ntohl (mp->type);
 
-  mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
-                    VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
+  mp->link_duplex = ntohl (((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
+                           VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT));
   mp->link_speed = ntohl (hi->link_speed);
   mp->link_mtu = ntohs (hi->max_packet_bytes);
   mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
@@ -469,9 +461,9 @@ vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
   VALIDATE_SW_IF_INDEX (mp);
 
   if (mp->is_ipv6)
-    rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
+    rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id);
   else
-    rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
+    rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id);
 
   BAD_SW_IF_INDEX_LABEL;
 
@@ -479,24 +471,10 @@ vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
 }
 
 int
-ip_table_bind (fib_protocol_t fproto,
-              u32 sw_if_index, u32 table_id, u8 is_api)
+ip_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 table_id)
 {
   CLIB_UNUSED (ip_interface_address_t * ia);
   u32 fib_index, mfib_index;
-  fib_source_t src;
-  mfib_source_t msrc;
-
-  if (is_api)
-    {
-      src = FIB_SOURCE_API;
-      msrc = MFIB_SOURCE_API;
-    }
-  else
-    {
-      src = FIB_SOURCE_CLI;
-      msrc = MFIB_SOURCE_CLI;
-    }
 
   /*
    * This if table does not exist = error is what we want in the end.
@@ -526,9 +504,6 @@ ip_table_bind (fib_protocol_t fproto,
       }));
       /* *INDENT-ON* */
 
-      vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
-      vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
-
       /*
        * tell those that are interested that the binding is changing.
        */
@@ -539,22 +514,20 @@ ip_table_bind (fib_protocol_t fproto,
                      fib_index,
                      ip6_main.fib_index_by_sw_if_index[sw_if_index]);
 
-      if (0 == table_id)
-       {
-         /* reset back to default */
-         if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
-           fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
-                             FIB_PROTOCOL_IP6, src);
-         if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
-           mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
-                              [sw_if_index], FIB_PROTOCOL_IP6, msrc);
+      /* unlock currently assigned tables */
+      if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
+       fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
+                         FIB_PROTOCOL_IP6, FIB_SOURCE_INTERFACE);
+      if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
+       mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index[sw_if_index],
+                          FIB_PROTOCOL_IP6, MFIB_SOURCE_INTERFACE);
 
-       }
-      else
+      if (0 != table_id)
        {
          /* we need to lock the table now it's inuse */
-         fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
-         mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
+         fib_table_lock (fib_index, FIB_PROTOCOL_IP6, FIB_SOURCE_INTERFACE);
+         mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6,
+                          MFIB_SOURCE_INTERFACE);
        }
 
       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
@@ -577,9 +550,6 @@ ip_table_bind (fib_protocol_t fproto,
       }));
       /* *INDENT-ON* */
 
-      vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
-      vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
-
       /*
        * tell those that are interested that the binding is changing.
        */
@@ -590,25 +560,22 @@ ip_table_bind (fib_protocol_t fproto,
                      fib_index,
                      ip4_main.fib_index_by_sw_if_index[sw_if_index]);
 
-      if (0 == table_id)
-       {
-         /* reset back to default */
-         if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
-           fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
-                             FIB_PROTOCOL_IP4, src);
-         if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
-           mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
-                              [sw_if_index], FIB_PROTOCOL_IP4, msrc);
+      /* unlock currently assigned tables */
+      if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
+       fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
+                         FIB_PROTOCOL_IP4, FIB_SOURCE_INTERFACE);
+      if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
+       mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index[sw_if_index],
+                          FIB_PROTOCOL_IP4, MFIB_SOURCE_INTERFACE);
 
-       }
-      else
+      if (0 != table_id)
        {
          /* we need to lock the table now it's inuse */
-         fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
-                                                        table_id, src);
+         fib_index = fib_table_find_or_create_and_lock (
+           FIB_PROTOCOL_IP4, table_id, FIB_SOURCE_INTERFACE);
 
-         mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
-                                                          table_id, msrc);
+         mfib_index = mfib_table_find_or_create_and_lock (
+           FIB_PROTOCOL_IP4, table_id, MFIB_SOURCE_INTERFACE);
        }
 
       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
@@ -626,7 +593,8 @@ send_sw_interface_get_table_reply (vl_api_registration_t * reg,
 
   mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
+  mp->_vl_msg_id =
+    ntohs (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_GET_TABLE_REPLY);
   mp->context = context;
   mp->retval = htonl (retval);
   mp->vrf_id = htonl (vrf_id);
@@ -695,8 +663,8 @@ static void vl_api_sw_interface_set_unnumbered_t_handler
       goto done;
     }
 
-  vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
-                                      sw_if_index, mp->is_add);
+  rv = vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
+                                           sw_if_index, mp->is_add);
 done:
   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
 }
@@ -769,7 +737,7 @@ send_sw_interface_event (vpe_api_main_t * am,
 
   mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
+  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_EVENT);
   mp->sw_if_index = ntohl (sw_if_index);
   mp->client_index = reg->client_index;
   mp->pid = reg->client_pid;
@@ -1020,7 +988,8 @@ static void vl_api_sw_interface_get_mac_address_t_handler
   if (!reg)
     return;
   rmp = vl_msg_api_alloc (sizeof (*rmp));
-  rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
+  rmp->_vl_msg_id =
+    htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
   rmp->context = mp->context;
   rmp->retval = htonl (rv);
   if (!rv && eth_if)
@@ -1028,6 +997,39 @@ static void vl_api_sw_interface_get_mac_address_t_handler
   vl_api_send_msg (reg, (u8 *) rmp);
 }
 
+static void
+vl_api_sw_interface_set_interface_name_t_handler (
+  vl_api_sw_interface_set_interface_name_t *mp)
+{
+  vl_api_sw_interface_set_interface_name_reply_t *rmp;
+  vnet_main_t *vnm = vnet_get_main ();
+  u32 sw_if_index = ntohl (mp->sw_if_index);
+  vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
+  clib_error_t *error;
+  int rv = 0;
+
+  if (mp->name[0] == 0)
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto out;
+    }
+  if (si == 0)
+    {
+      rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
+      goto out;
+    }
+
+  error = vnet_rename_interface (vnm, si->hw_if_index, (char *) mp->name);
+  if (error)
+    {
+      clib_error_free (error);
+      rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
+    }
+
+out:
+  REPLY_MACRO (VL_API_SW_INTERFACE_SET_INTERFACE_NAME_REPLY);
+}
+
 static void vl_api_sw_interface_set_rx_mode_t_handler
   (vl_api_sw_interface_set_rx_mode_t * mp)
 {
@@ -1076,7 +1078,8 @@ send_interface_rx_placement_details (vpe_api_main_t * am,
   mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
 
-  mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_RX_PLACEMENT_DETAILS);
+  mp->_vl_msg_id =
+    htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_RX_PLACEMENT_DETAILS);
   mp->sw_if_index = htonl (sw_if_index);
   mp->queue_id = htonl (queue_id);
   mp->worker_id = htonl (worker_id);
@@ -1261,7 +1264,7 @@ out:
     return;
 
   rmp = vl_msg_api_alloc (sizeof (*rmp));
-  rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
+  rmp->_vl_msg_id = htons (REPLY_MSG_ID_BASE + VL_API_CREATE_VLAN_SUBIF_REPLY);
   rmp->context = mp->context;
   rmp->retval = htonl (rv);
   rmp->sw_if_index = htonl (sw_if_index);
@@ -1428,39 +1431,20 @@ static void
  * added the client registration handlers.
  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
  */
-#define vl_msg_name_crc_list
-#include <vnet/interface.api.h>
-#undef vl_msg_name_crc_list
-
-static void
-setup_message_id_table (api_main_t * am)
-{
-#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
-  foreach_vl_msg_name_crc_interface;
-#undef _
-}
 
 pub_sub_handler (interface_events, INTERFACE_EVENTS);
 
+#include <vnet/interface.api.c>
 static clib_error_t *
 interface_api_hookup (vlib_main_t * vm)
 {
   api_main_t *am = vlibapi_get_main ();
 
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
-                           vl_api_##n##_t_handler,              \
-                           vl_noop_handler,                     \
-                           vl_api_##n##_t_endian,               \
-                           vl_api_##n##_t_print,                \
-                           sizeof(vl_api_##n##_t), 1);
-  foreach_vpe_api_msg;
-#undef _
-
   /* Mark these APIs as mp safe */
   am->is_mp_safe[VL_API_SW_INTERFACE_DUMP] = 1;
   am->is_mp_safe[VL_API_SW_INTERFACE_DETAILS] = 1;
   am->is_mp_safe[VL_API_SW_INTERFACE_TAG_ADD_DEL] = 1;
+  am->is_mp_safe[VL_API_SW_INTERFACE_SET_INTERFACE_NAME] = 1;
 
   /* Do not replay VL_API_SW_INTERFACE_DUMP messages */
   am->api_trace_cfg[VL_API_SW_INTERFACE_DUMP].replay_enable = 0;
@@ -1468,7 +1452,7 @@ interface_api_hookup (vlib_main_t * vm)
   /*
    * Set up the (msg_name, crc, message-id) table
    */
-  setup_message_id_table (am);
+  REPLY_MSG_ID_BASE = setup_message_id_table ();
 
   return 0;
 }