virtio: Native virtio driver

Change-Id: Id7fccf2f805e578fb05032aeb2b649a74c3c0e56
Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
diff --git a/src/vnet/devices/tap/cli.c b/src/vnet/devices/tap/cli.c
index 9d86159..ee57a72 100644
--- a/src/vnet/devices/tap/cli.c
+++ b/src/vnet/devices/tap/cli.c
@@ -172,29 +172,6 @@
   int show_descr = 0;
   clib_error_t *error = 0;
   u32 hw_if_index, *hw_if_indices = 0;
-  virtio_vring_t *vring;
-  int i, j;
-  struct feat_struct
-  {
-    u8 bit;
-    char *str;
-  };
-  struct feat_struct *feat_entry;
-
-  static struct feat_struct feat_array[] = {
-#define _(s,b) { .str = #s, .bit = b, },
-    foreach_virtio_net_features
-#undef _
-    {.str = NULL}
-  };
-
-  struct feat_struct *flag_entry;
-  static struct feat_struct flags_array[] = {
-#define _(b,e,s) { .bit = b, .str = s, },
-    foreach_virtio_if_flag
-#undef _
-    {.str = NULL}
-  };
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -220,81 +197,8 @@
       /* *INDENT-ON* */
     }
 
-  for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
-    {
-      vnet_hw_interface_t *hi =
-	vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
-      vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
-      vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
-		       vnm, vif->sw_if_index);
-      if (vif->host_if_name)
-	vlib_cli_output (vm, "  name \"%s\"", vif->host_if_name);
-      if (vif->net_ns)
-	vlib_cli_output (vm, "  host-ns \"%s\"", vif->net_ns);
-      vlib_cli_output (vm, "  flags 0x%x", vif->flags);
-      flag_entry = (struct feat_struct *) &flags_array;
-      while (flag_entry->str)
-	{
-	  if (vif->flags & (1ULL << flag_entry->bit))
-	    vlib_cli_output (vm, "    %s (%d)", flag_entry->str,
-			     flag_entry->bit);
-	  flag_entry++;
-	}
-      vlib_cli_output (vm, "  fd %d", vif->fd);
-      vlib_cli_output (vm, "  tap-fd %d", vif->tap_fd);
-      vlib_cli_output (vm, "  features 0x%lx", vif->features);
-      feat_entry = (struct feat_struct *) &feat_array;
-      while (feat_entry->str)
-	{
-	  if (vif->features & (1ULL << feat_entry->bit))
-	    vlib_cli_output (vm, "    %s (%d)", feat_entry->str,
-			     feat_entry->bit);
-	  feat_entry++;
-	}
-      vlib_cli_output (vm, "  remote-features 0x%lx", vif->remote_features);
-      feat_entry = (struct feat_struct *) &feat_array;
-      while (feat_entry->str)
-	{
-	  if (vif->remote_features & (1ULL << feat_entry->bit))
-	    vlib_cli_output (vm, "    %s (%d)", feat_entry->str,
-			     feat_entry->bit);
-	  feat_entry++;
-	}
-      vec_foreach_index (i, vif->vrings)
-      {
-	// RX = 0, TX = 1
-	vring = vec_elt_at_index (vif->vrings, i);
-	vlib_cli_output (vm, "  Virtqueue (%s)", (i & 1) ? "TX" : "RX");
-	vlib_cli_output (vm,
-			 "    qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
-			 vring->size, vring->last_used_idx, vring->desc_next,
-			 vring->desc_in_use);
-	vlib_cli_output (vm,
-			 "    avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
-			 vring->avail->flags, vring->avail->idx,
-			 vring->used->flags, vring->used->idx);
-	vlib_cli_output (vm, "    kickfd %d, callfd %d", vring->kick_fd,
-			 vring->call_fd);
-	if (show_descr)
-	  {
-	    vlib_cli_output (vm, "\n  descriptor table:\n");
-	    vlib_cli_output (vm,
-			     "   id          addr         len  flags  next      user_addr\n");
-	    vlib_cli_output (vm,
-			     "  ===== ================== ===== ====== ===== ==================\n");
-	    vring = vif->vrings;
-	    for (j = 0; j < vring->size; j++)
-	      {
-		struct vring_desc *desc = &vring->desc[j];
-		vlib_cli_output (vm,
-				 "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
-				 j, desc->addr,
-				 desc->len,
-				 desc->flags, desc->next, desc->addr);
-	      }
-	  }
-      }
-    }
+  virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TAP);
+
 done:
   vec_free (hw_if_indices);
   return error;
diff --git a/src/vnet/devices/tap/tap.c b/src/vnet/devices/tap/tap.c
index d0ed58c..4f3066a 100644
--- a/src/vnet/devices/tap/tap.c
+++ b/src/vnet/devices/tap/tap.c
@@ -130,7 +130,7 @@
 
   _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
 
-  if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
+  if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
     {
       args->rv = VNET_API_ERROR_UNSUPPORTED;
       args->error = clib_error_return (0, "vhost-net backend doesn't support "
@@ -138,7 +138,8 @@
       goto error;
     }
 
-  if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
+  if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
+      0)
     {
       args->rv = VNET_API_ERROR_UNSUPPORTED;
       args->error = clib_error_return (0, "vhost-net backend doesn't support "
@@ -146,7 +147,7 @@
       goto error;
     }
 
-  if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
+  if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
     {
       args->rv = VNET_API_ERROR_UNSUPPORTED;
       args->error = clib_error_return (0, "vhost-net backend doesn't support "
@@ -154,9 +155,11 @@
       goto error;
     }
 
-  vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
-  vif->features |= 1ULL << VIRTIO_F_VERSION_1;
-  vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
+  vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
+  vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
+  vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
+
+  virtio_set_net_hdr_size (vif);
 
   _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
 
@@ -349,6 +352,8 @@
     }
   vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256;
   vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256;
+  clib_memcpy (vif->mac_addr, args->mac_addr, 6);
+
   vif->host_if_name = args->host_if_name;
   args->host_if_name = 0;
   vif->net_ns = args->host_namespace;
@@ -363,9 +368,10 @@
   if (args->host_ip6_prefix_len)
     clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
 
+  vif->type = VIRTIO_IF_TYPE_TAP;
   args->error = ethernet_register_interface (vnm, virtio_device_class.index,
 					     vif->dev_instance,
-					     args->mac_addr,
+					     vif->mac_addr,
 					     &vif->hw_if_index,
 					     virtio_eth_flag_change);
   if (args->error)
@@ -386,7 +392,6 @@
   vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
 				 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
   vif->per_interface_next_index = ~0;
-  vif->type = VIRTIO_IF_TYPE_TAP;
   vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
   vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
 			       VNET_HW_INTERFACE_FLAG_LINK_UP);
@@ -433,6 +438,9 @@
 
   vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
 
+  if (vif->type != VIRTIO_IF_TYPE_TAP)
+    return VNET_API_ERROR_INVALID_INTERFACE;
+
   /* bring down the interface */
   vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
   vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
@@ -469,6 +477,8 @@
 
   /* *INDENT-OFF* */
   pool_foreach (vif, mm->interfaces,
+    if (vif->type != VIRTIO_IF_TYPE_TAP)
+      continue;
     vec_add2(r_tapids, tapid, 1);
     clib_memset (tapid, 0, sizeof (*tapid));
     tapid->id = vif->id;