Add binary API for reading interface/vrf assignment (VPP-439)

Change-Id: I9b7cb90127e464fea520bb1b0a3e93c05a0e9e8e
Signed-off-by: Juraj Sloboda <jsloboda@cisco.com>
diff --git a/vnet/vnet/interface.api b/vnet/vnet/interface.api
index de8ca68..752e79c 100644
--- a/vnet/vnet/interface.api
+++ b/vnet/vnet/interface.api
@@ -181,7 +181,7 @@
   u8 address[16];
 };
 
-/** \brief Reply for interface events registration
+/** \brief Reply to sw_interface_add_del_address
     @param context - returned sender context, to match reply w/ request
     @param retval - return code
 */
@@ -207,7 +207,7 @@
   u32 vrf_id;
 };
 
-/** \brief Reply for interface events registration
+/** \brief Reply to sw_interface_set_table
     @param context - returned sender context, to match reply w/ request
     @param retval - return code
 */
@@ -217,6 +217,30 @@
   i32 retval;
 };
 
+/** \brief Get VRF id assigned to interface
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param sw_if_index - index of the interface
+*/
+define sw_interface_get_table
+{
+  u32 client_index;
+  u32 context;
+  u32 sw_if_index;
+  u8 is_ipv6;
+};
+
+/** \brief Reply to get_sw_interface_vrf
+    @param context - sender context which was passed in the request
+    @param vrf_id - VRF id assigned to the interface
+*/
+define sw_interface_get_table_reply
+{
+  u32 context;
+  i32 retval;
+  u32 vrf_id;
+};
+
 /** \brief Stats counters structure 
     @param vnet_counter_type- such as ip4, ip6, punts, etc
     @param is_combined - rx & tx total (all types) counts   
diff --git a/vnet/vnet/interface_api.c b/vnet/vnet/interface_api.c
index 70b70fb..3529b52 100644
--- a/vnet/vnet/interface_api.c
+++ b/vnet/vnet/interface_api.c
@@ -52,6 +52,7 @@
 _(SW_INTERFACE_DETAILS, sw_interface_details)                   \
 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)   \
 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)               \
+_(SW_INTERFACE_GET_TABLE, sw_interface_get_table)               \
 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)     \
 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)           \
 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)
@@ -327,6 +328,56 @@
   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
 }
 
+static void
+send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
+				   u32 context, int retval, u32 vrf_id)
+{
+  vl_api_sw_interface_get_table_reply_t *mp;
+
+  mp = vl_msg_api_alloc (sizeof (*mp));
+  memset (mp, 0, sizeof (*mp));
+  mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
+  mp->context = context;
+  mp->retval = htonl (retval);
+  mp->vrf_id = htonl (vrf_id);
+
+  vl_msg_api_send_shmem (q, (u8 *) & mp);
+}
+
+static void
+vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
+{
+  unix_shared_memory_queue_t *q;
+  fib_table_t *fib_table = 0;
+  u32 sw_if_index = ~0;
+  u32 fib_index = ~0;
+  u32 table_id = ~0;
+  fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
+  int rv = 0;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    return;
+
+  VALIDATE_SW_IF_INDEX (mp);
+
+  sw_if_index = ntohl (mp->sw_if_index);
+
+  if (mp->is_ipv6)
+    fib_proto = FIB_PROTOCOL_IP6;
+
+  fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
+  if (fib_index != ~0)
+    {
+      fib_table = fib_table_get (fib_index, fib_proto);
+      table_id = fib_table->ft_table_id;
+    }
+
+  BAD_SW_IF_INDEX_LABEL;
+
+  send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
+}
+
 static void vl_api_sw_interface_set_unnumbered_t_handler
   (vl_api_sw_interface_set_unnumbered_t * mp)
 {
diff --git a/vpp-api-test/vat/api_format.c b/vpp-api-test/vat/api_format.c
index 83fe57e..2a64958 100644
--- a/vpp-api-test/vat/api_format.c
+++ b/vpp-api-test/vat/api_format.c
@@ -3921,7 +3921,8 @@
 _(SW_INTERFACE_TAG_ADD_DEL_REPLY, sw_interface_tag_add_del_reply)     	\
 _(L2_XCONNECT_DETAILS, l2_xconnect_details)                             \
 _(SW_INTERFACE_SET_MTU_REPLY, sw_interface_set_mtu_reply)               \
-_(IP_NEIGHBOR_DETAILS, ip_neighbor_details)
+_(IP_NEIGHBOR_DETAILS, ip_neighbor_details)                             \
+_(SW_INTERFACE_GET_TABLE_REPLY, sw_interface_get_table_reply)
 
 /* M: construct, but don't yet send a message */
 
@@ -5105,6 +5106,71 @@
   W;
 }
 
+static void vl_api_sw_interface_get_table_reply_t_handler
+  (vl_api_sw_interface_get_table_reply_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+
+  fformat (vam->ofp, "%d\n", ntohl (mp->vrf_id));
+
+  vam->retval = ntohl (mp->retval);
+  vam->result_ready = 1;
+
+}
+
+static void vl_api_sw_interface_get_table_reply_t_handler_json
+  (vl_api_sw_interface_get_table_reply_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  vat_json_node_t node;
+
+  vat_json_init_object (&node);
+  vat_json_object_add_int (&node, "retval", ntohl (mp->retval));
+  vat_json_object_add_int (&node, "vrf_id", ntohl (mp->vrf_id));
+
+  vat_json_print (vam->ofp, &node);
+  vat_json_free (&node);
+
+  vam->retval = ntohl (mp->retval);
+  vam->result_ready = 1;
+}
+
+static int
+api_sw_interface_get_table (vat_main_t * vam)
+{
+  unformat_input_t *i = vam->input;
+  vl_api_sw_interface_get_table_t *mp;
+  u32 sw_if_index;
+  u8 sw_if_index_set = 0;
+  u8 is_ipv6 = 0;
+  f64 timeout;
+
+  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
+	sw_if_index_set = 1;
+      else if (unformat (i, "sw_if_index %d", &sw_if_index))
+	sw_if_index_set = 1;
+      else if (unformat (i, "ipv6"))
+	is_ipv6 = 1;
+      else
+	break;
+    }
+
+  if (sw_if_index_set == 0)
+    {
+      errmsg ("missing interface name or sw_if_index\n");
+      return -99;
+    }
+
+  M (SW_INTERFACE_GET_TABLE, sw_interface_get_table);
+  mp->sw_if_index = htonl (sw_if_index);
+  mp->is_ipv6 = is_ipv6;
+
+  S;
+  W;
+}
+
 static int
 api_sw_interface_set_vpath (vat_main_t * vam)
 {
@@ -17627,7 +17693,8 @@
 "[disable]")                                                        	\
 _(l2_xconnect_dump, "")                                             	\
 _(sw_interface_set_mtu, "<intfc> | sw_if_index <nn> mtu <nn>")        \
-_(ip_neighbor_dump, "[ip6] <intfc> | sw_if_index <nn>")
+_(ip_neighbor_dump, "[ip6] <intfc> | sw_if_index <nn>")                 \
+_(sw_interface_get_table, "<intfc> | sw_if_index <id> [ipv6]")
 
 /* List of command functions, CLI names map directly to functions */
 #define foreach_cli_function                                    \