vhost: memory leak upon deleting vhost-user interface
[vpp.git] / src / vnet / devices / virtio / vhost_user.c
index b45b18b..b6e0806 100644 (file)
@@ -37,6 +37,7 @@
 #include <vnet/devices/devices.h>
 #include <vnet/feature/feature.h>
 #include <vnet/interface/rx_queue_funcs.h>
+#include <vnet/interface/tx_queue_funcs.h>
 
 #include <vnet/devices/virtio/vhost_user.h>
 #include <vnet/devices/virtio/vhost_user_inline.h>
@@ -106,50 +107,55 @@ unmap_all_mem_regions (vhost_user_intf_t * vui)
     }
   vui->nregions = 0;
 
-  for (q = 0; q < vui->num_qid; q++)
-    {
-      vq = &vui->vrings[q];
-      vq->avail = 0;
-      vq->used = 0;
-      vq->desc = 0;
-    }
+  FOR_ALL_VHOST_RX_TXQ (q, vui)
+  {
+    vq = &vui->vrings[q];
+    vq->avail = 0;
+    vq->used = 0;
+    vq->desc = 0;
+  }
 }
 
 static_always_inline void
-vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
+vhost_user_tx_thread_placement (vhost_user_intf_t *vui, u32 qid)
 {
-  //Let's try to assign one queue to each thread
-  u32 qid;
-  u32 thread_index = 0;
+  vnet_main_t *vnm = vnet_get_main ();
+  vhost_user_vring_t *rxvq = &vui->vrings[qid];
+  u32 q = qid >> 1, rxvq_count;
 
-  vui->use_tx_spinlock = 0;
-  while (1)
+  ASSERT ((qid & 1) == 0);
+  if (!rxvq->started || !rxvq->enabled)
+    return;
+
+  rxvq_count = (qid >> 1) + 1;
+  if (rxvq->queue_index == ~0)
     {
-      for (qid = 0; qid < vui->num_qid / 2; qid++)
-       {
-         vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
-         if (!rxvq->started || !rxvq->enabled)
-           continue;
-
-         vui->per_cpu_tx_qid[thread_index] = qid;
-         thread_index++;
-         if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
-           return;
-       }
-      //We need to loop, meaning the spinlock has to be used
-      vui->use_tx_spinlock = 1;
-      if (thread_index == 0)
-       {
-         //Could not find a single valid one
-         for (thread_index = 0;
-              thread_index < vlib_get_thread_main ()->n_vlib_mains;
-              thread_index++)
-           {
-             vui->per_cpu_tx_qid[thread_index] = 0;
-           }
-         return;
-       }
+      rxvq->queue_index =
+       vnet_hw_if_register_tx_queue (vnm, vui->hw_if_index, q);
+      rxvq->qid = q;
     }
+
+  FOR_ALL_VHOST_RXQ (q, vui)
+  {
+    vhost_user_vring_t *rxvq = &vui->vrings[q];
+    u32 qi = rxvq->queue_index;
+
+    if (rxvq->queue_index == ~0)
+      break;
+    for (u32 i = 0; i < vlib_get_n_threads (); i++)
+      vnet_hw_if_tx_queue_unassign_thread (vnm, qi, i);
+  }
+
+  for (u32 i = 0; i < vlib_get_n_threads (); i++)
+    {
+      vhost_user_vring_t *rxvq =
+       &vui->vrings[VHOST_VRING_IDX_RX (i % rxvq_count)];
+      u32 qi = rxvq->queue_index;
+
+      vnet_hw_if_tx_queue_assign_thread (vnm, qi, i);
+    }
+
+  vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
 }
 
 /**
@@ -163,16 +169,28 @@ vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
   vnet_main_t *vnm = vnet_get_main ();
   int rv;
   u32 q = qid >> 1;
+  vhost_user_main_t *vum = &vhost_user_main;
 
   ASSERT ((qid & 1) == 1);     // should be odd
   // Assign new queue mappings for the interface
+  if (txvq->queue_index != ~0)
+    return;
   vnet_hw_if_set_input_node (vnm, vui->hw_if_index,
                             vhost_user_input_node.index);
   txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q,
                                                    VNET_HW_IF_RXQ_THREAD_ANY);
+  txvq->thread_index =
+    vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
+
   if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
     /* Set polling as the default */
     txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
+  if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
+    {
+      vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
+      /* Keep a polling queue count for each thread */
+      cpu->polling_q_count++;
+    }
   txvq->qid = q;
   rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode);
   if (rv)
@@ -211,27 +229,6 @@ vhost_user_update_iface_state (vhost_user_intf_t * vui)
     }
 }
 
-static void
-vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
-{
-  u32 qid;
-  vnet_main_t *vnm = vnet_get_main ();
-  vhost_user_vring_t *txvq;
-
-  qid = ifq & 0xff;
-  if ((qid & 1) == 0)
-    /* Only care about the odd number, or TX, virtqueue */
-    return;
-
-  // qid >> 1 is to convert virtqueue number to vring queue index
-  qid >>= 1;
-  txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
-  if (vhost_user_intf_ready (vui) &&
-      ((txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
-       (txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT)))
-    vnet_hw_if_rx_queue_set_int_pending (vnm, txvq->queue_index);
-}
-
 static clib_error_t *
 vhost_user_callfd_read_ready (clib_file_t * uf)
 {
@@ -248,37 +245,49 @@ vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
 {
   if (qid & 1)                 // RX is odd, TX is even
     {
-      if (vui->vrings[qid].qid == -1)
+      if (vui->vrings[qid].queue_index == ~0)
        vhost_user_rx_thread_placement (vui, qid);
     }
   else
-    vhost_user_tx_thread_placement (vui);
+    vhost_user_tx_thread_placement (vui, qid);
 }
 
 static clib_error_t *
 vhost_user_kickfd_read_ready (clib_file_t * uf)
 {
-  __attribute__ ((unused)) int n;
+  __attribute__ ((unused)) ssize_t n;
   u8 buff[8];
+  vhost_user_main_t *vum = &vhost_user_main;
   vhost_user_intf_t *vui =
-    pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
-                      uf->private_data >> 8);
+    pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8);
   u32 qid = uf->private_data & 0xff;
+  u32 is_txq = qid & 1;
+  vhost_user_vring_t *vq = &vui->vrings[qid];
+  vnet_main_t *vnm = vnet_get_main ();
 
-  n = read (uf->file_descriptor, ((char *) &buff), 8);
-  vu_log_debug (vui, "if %d KICK queue %d", vui->hw_if_index, qid);
-  if (!vui->vrings[qid].started ||
-      (vhost_user_intf_ready (vui) != vui->is_ready))
+  n = read (uf->file_descriptor, buff, 8);
+  if (vq->started == 0)
     {
-      if (vui->vrings[qid].started == 0)
-       {
-         vui->vrings[qid].started = 1;
-         vhost_user_thread_placement (vui, qid);
-         vhost_user_update_iface_state (vui);
-       }
+      vq->started = 1;
+      vhost_user_thread_placement (vui, qid);
+      vhost_user_update_iface_state (vui);
+      if (is_txq)
+       vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index,
+                                           vq->kickfd_idx);
+    }
+
+  if (is_txq && (vq->mode != VNET_HW_IF_RX_MODE_POLLING) &&
+      vhost_user_intf_ready (vui))
+    {
+      vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, vq->thread_index);
+      /*
+       * If the thread has more than 1 queue and the other queue is in polling
+       * mode, there is no need to trigger an interrupt
+       */
+      if (cpu->polling_q_count == 0)
+       vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index);
     }
 
-  vhost_user_set_interrupt_pending (vui, uf->private_data);
   return 0;
 }
 
@@ -292,6 +301,9 @@ vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
   vring->callfd_idx = ~0;
   vring->errfd = -1;
   vring->qid = -1;
+  vring->queue_index = ~0;
+  vring->thread_index = ~0;
+  vring->mode = VNET_HW_IF_RX_MODE_POLLING;
 
   clib_spinlock_init (&vring->vring_lock);
 
@@ -335,11 +347,16 @@ vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
 
   clib_spinlock_free (&vring->vring_lock);
 
-  // save the qid so that we don't need to unassign and assign_rx_thread
-  // when the interface comes back up. They are expensive calls.
+  // save the needed information in vrings prior to being wiped out
   u16 q = vui->vrings[qid].qid;
+  u32 queue_index = vui->vrings[qid].queue_index;
+  u32 mode = vui->vrings[qid].mode;
+  u32 thread_index = vui->vrings[qid].thread_index;
   vhost_user_vring_init (vui, qid);
   vui->vrings[qid].qid = q;
+  vui->vrings[qid].queue_index = queue_index;
+  vui->vrings[qid].mode = mode;
+  vui->vrings[qid].thread_index = thread_index;
 }
 
 static_always_inline void
@@ -358,13 +375,38 @@ vhost_user_if_disconnect (vhost_user_intf_t * vui)
 
   vui->is_ready = 0;
 
-  for (q = 0; q < vui->num_qid; q++)
-    vhost_user_vring_close (vui, q);
+  FOR_ALL_VHOST_RX_TXQ (q, vui) { vhost_user_vring_close (vui, q); }
 
   unmap_all_mem_regions (vui);
   vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
 }
 
+void
+vhost_user_set_operation_mode (vhost_user_intf_t *vui,
+                              vhost_user_vring_t *txvq)
+{
+  if (vhost_user_is_packed_ring_supported (vui))
+    {
+      if (txvq->used_event)
+       {
+         if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
+           txvq->used_event->flags = VRING_EVENT_F_DISABLE;
+         else
+           txvq->used_event->flags = 0;
+       }
+    }
+  else
+    {
+      if (txvq->used)
+       {
+         if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
+           txvq->used->flags = VRING_USED_F_NO_NOTIFY;
+         else
+           txvq->used->flags = 0;
+       }
+    }
+}
+
 static clib_error_t *
 vhost_user_socket_read (clib_file_t * uf)
 {
@@ -511,16 +553,21 @@ vhost_user_socket_read (clib_file_t * uf)
        (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
 
       ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
-      vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
       if (vui->enable_gso &&
          ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
           == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
-       hw->flags |=
-         (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
-          VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
+       {
+         vnet_hw_if_set_caps (vnm, vui->hw_if_index,
+                              VNET_HW_IF_CAP_TCP_GSO |
+                                VNET_HW_IF_CAP_TX_TCP_CKSUM |
+                                VNET_HW_IF_CAP_TX_UDP_CKSUM);
+       }
       else
-       hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
-                      VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
+       {
+         vnet_hw_if_unset_caps (vnm, vui->hw_if_index,
+                                VNET_HW_IF_CAP_TCP_GSO |
+                                  VNET_HW_IF_CAP_L4_TX_CKSUM);
+       }
       vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
       vui->is_ready = 0;
       vhost_user_update_iface_state (vui);
@@ -591,19 +638,19 @@ vhost_user_socket_read (clib_file_t * uf)
        * Re-compute desc, used, and avail descriptor table if vring address
        * is set.
        */
-      for (q = 0; q < vui->num_qid; q++)
-       {
-         if (vui->vrings[q].desc_user_addr &&
-             vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
-           {
-             vui->vrings[q].desc =
-               map_user_mem (vui, vui->vrings[q].desc_user_addr);
-             vui->vrings[q].used =
-               map_user_mem (vui, vui->vrings[q].used_user_addr);
-             vui->vrings[q].avail =
-               map_user_mem (vui, vui->vrings[q].avail_user_addr);
-           }
-       }
+      FOR_ALL_VHOST_RX_TXQ (q, vui)
+      {
+       if (vui->vrings[q].desc_user_addr && vui->vrings[q].used_user_addr &&
+           vui->vrings[q].avail_user_addr)
+         {
+           vui->vrings[q].desc =
+             map_user_mem (vui, vui->vrings[q].desc_user_addr);
+           vui->vrings[q].used =
+             map_user_mem (vui, vui->vrings[q].used_user_addr);
+           vui->vrings[q].avail =
+             map_user_mem (vui, vui->vrings[q].avail_user_addr);
+         }
+      }
       vlib_worker_thread_barrier_release (vm);
       break;
 
@@ -642,9 +689,12 @@ vhost_user_socket_read (clib_file_t * uf)
          goto close_socket;
        }
 
-      vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
-      vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
-      vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
+      vnet_virtio_vring_desc_t *desc =
+       map_user_mem (vui, msg.addr.desc_user_addr);
+      vnet_virtio_vring_used_t *used =
+       map_user_mem (vui, msg.addr.used_user_addr);
+      vnet_virtio_vring_avail_t *avail =
+       map_user_mem (vui, msg.addr.avail_user_addr);
 
       if ((desc == NULL) || (used == NULL) || (avail == NULL))
        {
@@ -677,12 +727,8 @@ vhost_user_socket_read (clib_file_t * uf)
       vui->vrings[msg.state.index].last_kick =
        vui->vrings[msg.state.index].last_used_idx;
 
-      /* tell driver that we don't want interrupts */
-      if (vhost_user_is_packed_ring_supported (vui))
-       vui->vrings[msg.state.index].used_event->flags =
-         VRING_EVENT_F_DISABLE;
-      else
-       vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
+      /* tell driver that we want interrupts or not */
+      vhost_user_set_operation_mode (vui, &vui->vrings[msg.state.index]);
       vlib_worker_thread_barrier_release (vm);
       vhost_user_update_iface_state (vui);
       break;
@@ -1192,6 +1238,7 @@ vhost_user_send_interrupt_process (vlib_main_t * vm,
 
        case VHOST_USER_EVENT_START_TIMER:
          stop_timer = 0;
+         timeout = 1e-3;
          if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
            break;
          /* fall through */
@@ -1200,32 +1247,23 @@ vhost_user_send_interrupt_process (vlib_main_t * vm,
          /* *INDENT-OFF* */
          pool_foreach (vui, vum->vhost_user_interfaces) {
              next_timeout = timeout;
-             for (qid = 0; qid < vui->num_qid / 2; qid += 2)
-               {
-                 vhost_user_vring_t *rxvq = &vui->vrings[qid];
-                 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
-
-                 if (txvq->qid == -1)
-                   continue;
-                 if (txvq->n_since_last_int)
-                   {
-                     if (now >= txvq->int_deadline)
-                       vhost_user_send_call (vm, vui, txvq);
-                     else
-                       next_timeout = txvq->int_deadline - now;
-                   }
-
-                 if (rxvq->n_since_last_int)
-                   {
-                     if (now >= rxvq->int_deadline)
-                       vhost_user_send_call (vm, vui, rxvq);
-                     else
-                       next_timeout = rxvq->int_deadline - now;
-                   }
-
-                 if ((next_timeout < timeout) && (next_timeout > 0.0))
-                   timeout = next_timeout;
-               }
+             FOR_ALL_VHOST_RX_TXQ (qid, vui)
+             {
+               vhost_user_vring_t *vq = &vui->vrings[qid];
+
+               if (vq->started == 0)
+                 continue;
+               if (vq->n_since_last_int)
+                 {
+                   if (now >= vq->int_deadline)
+                     vhost_user_send_call (vm, vui, vq);
+                   else
+                     next_timeout = vq->int_deadline - now;
+                 }
+
+               if ((next_timeout < timeout) && (next_timeout > 0.0))
+                 timeout = next_timeout;
+             }
          }
           /* *INDENT-ON* */
          break;
@@ -1267,7 +1305,6 @@ vhost_user_process (vlib_main_t * vm,
   sun.sun_family = AF_UNIX;
   template.read_function = vhost_user_socket_read;
   template.error_function = vhost_user_socket_error;
-  template.description = format (0, "vhost user process");
 
   while (1)
     {
@@ -1320,6 +1357,7 @@ vhost_user_process (vlib_main_t * vm,
                      template.file_descriptor = sockfd;
                      template.private_data =
                          vui - vhost_user_main.vhost_user_interfaces;
+                     template.description = format (0, "vhost user process");
                      vui->clib_file_index = clib_file_add (&file_main, &template);
                      vui->num_qid = 2;
 
@@ -1376,10 +1414,8 @@ vhost_user_term_if (vhost_user_intf_t * vui)
   vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
   vhost_user_update_iface_state (vui);
 
-  for (q = 0; q < vui->num_qid; q++)
-    {
-      clib_spinlock_free (&vui->vrings[q].vring_lock);
-    }
+  for (q = 0; q < vec_len (vui->vrings); q++)
+    clib_spinlock_free (&vui->vrings[q].vring_lock);
 
   if (vui->unix_server_index != ~0)
     {
@@ -1415,28 +1451,33 @@ vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
   vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
                hwif->name, hwif->dev_instance);
 
-  for (qid = 1; qid < vui->num_qid / 2; qid += 2)
-    {
-      vhost_user_vring_t *txvq = &vui->vrings[qid];
+  FOR_ALL_VHOST_TXQ (qid, vui)
+  {
+    vhost_user_vring_t *txvq = &vui->vrings[qid];
 
-      if (txvq->qid == -1)
-       continue;
-      if ((vum->ifq_count > 0) &&
-         ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
-          (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
-       {
-         vum->ifq_count--;
-         // Stop the timer if there is no more interrupt interface/queue
-         if ((vum->ifq_count == 0) &&
-             (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
-           {
-             vlib_process_signal_event (vm,
-                                        vhost_user_send_interrupt_node.index,
-                                        VHOST_USER_EVENT_STOP_TIMER, 0);
-             break;
-           }
-       }
-    }
+    if ((txvq->mode == VNET_HW_IF_RX_MODE_POLLING) &&
+       (txvq->thread_index != ~0))
+      {
+       vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
+       ASSERT (cpu->polling_q_count != 0);
+       cpu->polling_q_count--;
+      }
+
+    if ((vum->ifq_count > 0) &&
+       ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
+        (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
+      {
+       vum->ifq_count--;
+       // Stop the timer if there is no more interrupt interface/queue
+       if (vum->ifq_count == 0)
+         {
+           vlib_process_signal_event (vm,
+                                      vhost_user_send_interrupt_node.index,
+                                      VHOST_USER_EVENT_STOP_TIMER, 0);
+           break;
+         }
+      }
+  }
 
   // Disable and reset interface
   vhost_user_term_if (vui);
@@ -1521,17 +1562,18 @@ error:
  * Create ethernet interface for vhost user interface.
  */
 static void
-vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
-                           vhost_user_intf_t * vui, u8 * hwaddress)
+vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
+                           vhost_user_intf_t *vui,
+                           vhost_user_create_if_args_t *args)
 {
   vhost_user_main_t *vum = &vhost_user_main;
+  vnet_eth_interface_registration_t eir = {};
   u8 hwaddr[6];
-  clib_error_t *error;
 
   /* create hw and sw interface */
-  if (hwaddress)
+  if (args->use_custom_mac)
     {
-      clib_memcpy (hwaddr, hwaddress, 6);
+      clib_memcpy (hwaddr, args->hwaddr, 6);
     }
   else
     {
@@ -1541,15 +1583,10 @@ vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
       hwaddr[1] = 0xfe;
     }
 
-  error = ethernet_register_interface
-    (vnm,
-     vhost_user_device_class.index,
-     vui - vum->vhost_user_interfaces /* device instance */ ,
-     hwaddr /* ethernet address */ ,
-     &vui->hw_if_index, 0 /* flag change */ );
-
-  if (error)
-    clib_error_report (error);
+  eir.dev_class_index = vhost_user_device_class.index;
+  eir.dev_instance = vui - vum->vhost_user_interfaces /* device instance */,
+  eir.address = hwaddr;
+  vui->hw_if_index = vnet_eth_register_interface (vnm, &eir);
 }
 
 /*
@@ -1563,9 +1600,7 @@ vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
   vnet_sw_interface_t *sw;
   int q;
   vhost_user_main_t *vum = &vhost_user_main;
-  vnet_hw_interface_t *hw;
 
-  hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
   sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
   if (server_sock_fd != -1)
     {
@@ -1618,15 +1653,11 @@ vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
   for (q = 0; q < vec_len (vui->vrings); q++)
     vhost_user_vring_init (vui, q);
 
-  hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
+  vnet_hw_if_set_caps (vnm, vui->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
   vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
 
   if (sw_if_index)
     *sw_if_index = vui->sw_if_index;
-
-  vec_validate (vui->per_cpu_tx_qid,
-               vlib_get_thread_main ()->n_vlib_mains - 1);
-  vhost_user_tx_thread_placement (vui);
 }
 
 int
@@ -1667,7 +1698,7 @@ vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
   /* Protect the uninitialized vui from being dispatched by rx/tx */
   vlib_worker_thread_barrier_sync (vm);
   pool_get (vhost_user_main.vhost_user_interfaces, vui);
-  vhost_user_create_ethernet (vnm, vm, vui, args->hwaddr);
+  vhost_user_create_ethernet (vnm, vm, vui, args);
   vlib_worker_thread_barrier_release (vm);
 
   vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
@@ -1775,7 +1806,7 @@ vhost_user_connect_command_fn (vlib_main_t * vm,
        ;
       else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
                         args.hwaddr))
-       ;
+       args.use_custom_mac = 1;
       else if (unformat (line_input, "renumber %d",
                         &args.custom_dev_instance))
        args.renumber = 1;
@@ -1900,7 +1931,8 @@ format_vhost_user_desc (u8 * s, va_list * args)
 {
   char *fmt = va_arg (*args, char *);
   vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
-  vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
+  vnet_virtio_vring_desc_t *desc_table =
+    va_arg (*args, vnet_virtio_vring_desc_t *);
   int idx = va_arg (*args, int);
   u32 *mem_hint = va_arg (*args, u32 *);
 
@@ -1929,13 +1961,15 @@ vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
   u32 mem_hint = 0;
   u32 idx;
   u32 n_entries;
-  vring_desc_t *desc_table;
+  vnet_virtio_vring_desc_t *desc_table;
   vhost_user_vring_t *vq = &vui->vrings[q];
 
   if (vq->avail && vq->used)
-    vlib_cli_output (vm, "  avail.flags %x avail event idx %u avail.idx %d "
-                    "used event idx %u used.idx %d\n", vq->avail->flags,
-                    vhost_user_avail_event_idx (vq), vq->avail->idx,
+    vlib_cli_output (vm,
+                    "  avail.flags %x avail event idx %u avail.idx %d "
+                    "used.flags %x used event idx %u used.idx %d\n",
+                    vq->avail->flags, vhost_user_avail_event_idx (vq),
+                    vq->avail->idx, vq->used->flags,
                     vhost_user_used_event_idx (vq), vq->used->idx);
 
   vhost_user_show_fds (vm, vq);
@@ -1957,7 +1991,8 @@ vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
                           desc_table, j, &mem_hint);
          if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
            {
-             n_entries = desc_table[j].len / sizeof (vring_desc_t);
+             n_entries =
+               desc_table[j].len / sizeof (vnet_virtio_vring_desc_t);
              desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
              if (desc_table)
                {
@@ -1982,7 +2017,8 @@ format_vhost_user_packed_desc (u8 * s, va_list * args)
 {
   char *fmt = va_arg (*args, char *);
   vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
-  vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
+  vnet_virtio_vring_packed_desc_t *desc_table =
+    va_arg (*args, vnet_virtio_vring_packed_desc_t *);
   int idx = va_arg (*args, int);
   u32 *mem_hint = va_arg (*args, u32 *);
 
@@ -2024,7 +2060,7 @@ vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
   u32 mem_hint = 0;
   u32 idx;
   u32 n_entries;
-  vring_packed_desc_t *desc_table;
+  vnet_virtio_vring_packed_desc_t *desc_table;
   vhost_user_vring_t *vq = &vui->vrings[q];
   u16 off_wrap, event_idx;
 
@@ -2094,7 +2130,6 @@ show_vhost_user_command_fn (vlib_main_t * vm,
   u32 hw_if_index, *hw_if_indices = 0;
   vnet_hw_interface_t *hi;
   u16 qid;
-  u32 ci;
   int i, j, q;
   int show_descr = 0;
   int show_verbose = 0;
@@ -2159,6 +2194,12 @@ show_vhost_user_command_fn (vlib_main_t * vm,
   vlib_cli_output (vm, "  Number of rx virtqueues in interrupt mode: %d",
                   vum->ifq_count);
   vlib_cli_output (vm, "  Number of GSO interfaces: %d", vum->gso_count);
+  for (u32 tid = 0; tid <= vlib_num_workers (); tid++)
+    {
+      vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, tid);
+      vlib_cli_output (vm, "  Thread %u: Polling queue count %u", tid,
+                      cpu->polling_q_count);
+    }
 
   for (i = 0; i < vec_len (hw_if_indices); i++)
     {
@@ -2210,27 +2251,31 @@ show_vhost_user_command_fn (vlib_main_t * vm,
 
       vlib_cli_output (vm, " rx placement: ");
 
-      for (qid = 1; qid < vui->num_qid / 2; qid += 2)
-       {
-         vnet_main_t *vnm = vnet_get_main ();
-         uword thread_index;
-         vhost_user_vring_t *txvq = &vui->vrings[qid];
-
-         if (txvq->qid == -1)
-           continue;
-         thread_index =
-           vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
-         vlib_cli_output (vm, "   thread %d on vring %d, %U\n", thread_index,
-                          qid, format_vnet_hw_if_rx_mode, txvq->mode);
-       }
+      FOR_ALL_VHOST_TXQ (qid, vui)
+      {
+       vhost_user_vring_t *txvq = &vui->vrings[qid];
+
+       if (txvq->qid == -1)
+         continue;
+       vlib_cli_output (vm, "   thread %d on vring %d, %U\n",
+                        txvq->thread_index, qid, format_vnet_hw_if_rx_mode,
+                        txvq->mode);
+      }
 
-      vlib_cli_output (vm, " tx placement: %s\n",
-                      vui->use_tx_spinlock ? "spin-lock" : "lock-free");
+      vlib_cli_output (vm, " tx placement\n");
 
-      vec_foreach_index (ci, vui->per_cpu_tx_qid)
+      FOR_ALL_VHOST_RXQ (qid, vui)
       {
-       vlib_cli_output (vm, "   thread %d on vring %d\n", ci,
-                        VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
+       vhost_user_vring_t *rxvq = &vui->vrings[qid];
+       vnet_hw_if_tx_queue_t *txq;
+
+       if (rxvq->queue_index == ~0)
+         continue;
+       txq = vnet_hw_if_get_tx_queue (vnm, rxvq->queue_index);
+       if (txq->threads)
+         vlib_cli_output (vm, "   threads %U on vring %u: %s\n",
+                          format_bitmap_list, txq->threads, qid,
+                          txq->shared_queue ? "spin-lock" : "lock-free");
       }
 
       vlib_cli_output (vm, "\n");
@@ -2255,29 +2300,29 @@ show_vhost_user_command_fn (vlib_main_t * vm,
                           vui->regions[j].mmap_offset,
                           pointer_to_uword (vui->region_mmap_addr[j]));
        }
-      for (q = 0; q < vui->num_qid; q++)
-       {
-         if (!vui->vrings[q].started)
-           continue;
-
-         vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
-                          (q & 1) ? "RX" : "TX",
-                          vui->vrings[q].enabled ? "" : " disabled");
-
-         vlib_cli_output (vm,
-                          "  qsz %d last_avail_idx %d last_used_idx %d"
-                          " last_kick %u\n",
-                          vui->vrings[q].qsz_mask + 1,
-                          vui->vrings[q].last_avail_idx,
-                          vui->vrings[q].last_used_idx,
-                          vui->vrings[q].last_kick);
-
-         if (vhost_user_is_packed_ring_supported (vui))
-           vhost_user_show_desc_packed (vm, vui, q, show_descr,
-                                        show_verbose);
-         else
-           vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
-       }
+      FOR_ALL_VHOST_RX_TXQ (q, vui)
+      {
+       if (!vui->vrings[q].started)
+         continue;
+
+       vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
+                        (q & 1) ? "RX" : "TX",
+                        vui->vrings[q].enabled ? "" : " disabled");
+       vlib_cli_output (vm, "  global %s queue index %u\n",
+                        (q & 1) ? "RX" : "TX", vui->vrings[q].queue_index);
+
+       vlib_cli_output (
+         vm,
+         "  qsz %d last_avail_idx %d last_used_idx %d"
+         " last_kick %u\n",
+         vui->vrings[q].qsz_mask + 1, vui->vrings[q].last_avail_idx,
+         vui->vrings[q].last_used_idx, vui->vrings[q].last_kick);
+
+       if (vhost_user_is_packed_ring_supported (vui))
+         vhost_user_show_desc_packed (vm, vui, q, show_descr, show_verbose);
+       else
+         vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
+      }
       vlib_cli_output (vm, "\n");
     }
 done:
@@ -2296,23 +2341,25 @@ done:
  *
  * There are several parameters associated with a vHost interface:
  *
- * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
- * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
- * create the socket if it does not already exist. If in '<em>client</em>' mode,
- * hypervisor will create the socket if it does not already exist. The VPP code
- * is indifferent to the file location. However, if SELinux is enabled, then the
- * socket needs to be created in '<em>/var/run/vpp/</em>'.
+ * - <b>socket <socket-filename></b> - Name of the linux socket used by
+ * hypervisor and VPP to manage the vHost interface. If in <em>server</em>
+ * mode, VPP will create the socket if it does not already exist. If in
+ * <em>client</em> mode, hypervisor will create the socket if it does not
+ * already exist. The VPP code is indifferent to the file location. However,
+ * if SELinux is enabled, then the socket needs to be created in
+ * <em>/var/run/vpp/</em>.
  *
- * - <b>server</b> - Optional flag to indicate that VPP should be the server for
- * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
- *  mode, the VM can be reset without tearing down the vHost Interface. In
- * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
- * tearing down the vHost Interface.
+ * - <b>server</b> - Optional flag to indicate that VPP should be the server
+ * for the linux socket. If not provided, VPP will be the client. In
+ * <em>server</em> mode, the VM can be reset without tearing down the vHost
+ * Interface. In <em>client</em> mode, VPP can be reset without bringing down
+ * the VM and tearing down the vHost Interface.
  *
- * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
- * startup. <b>This is intended for degugging only.</b> It is recommended that this
- * parameter not be used except by experienced users. By default, all supported
- * features will be advertised. Otherwise, provide the set of features desired.
+ * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated
+ * at startup. <b>This is intended for degugging only.</b> It is recommended
+ * that this parameter not be used except by experienced users. By default,
+ * all supported features will be advertised. Otherwise, provide the set of
+ * features desired.
  *   - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
  *   - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
  *   - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
@@ -2326,18 +2373,21 @@ done:
  * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
  * X:X:X:X:X:X unix or X.X.X cisco format.
  *
- * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
- * in the name to be specified. If instance already exists, name will be used
- * anyway and multiple instances will have the same name. Use with caution.
+ * - <b>renumber <dev_instance></b> - Optional parameter which allows the
+ * instance in the name to be specified. If instance already exists, name
+ * will be used anyway and multiple instances will have the same name. Use
+ * with caution.
  *
  * @cliexpar
- * Example of how to create a vhost interface with VPP as the client and all features enabled:
+ * Example of how to create a vhost interface with VPP as the client and all
+ * features enabled:
  * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
  * VirtualEthernet0/0/0
  * @cliexend
- * Example of how to create a vhost interface with VPP as the server and with just
- * multiple queues enabled:
- * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
+ * Example of how to create a vhost interface with VPP as the server and with
+ * just multiple queues enabled:
+ * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server
+ * feature-mask 0x40400000}
  * VirtualEthernet0/0/1
  * @cliexend
  * Once the vHost interface is created, enable the interface using:
@@ -2375,9 +2425,9 @@ VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
 
 /*?
  * Display the attributes of a single vHost User interface (provide interface
- * name), multiple vHost User interfaces (provide a list of interface names seperated
- * by spaces) or all Vhost User interfaces (omit an interface name to display all
- * vHost interfaces).
+ * name), multiple vHost User interfaces (provide a list of interface names
+ * separated by spaces) or all Vhost User interfaces (omit an interface name
+ * to display all vHost interfaces).
  *
  * @cliexpar
  * @parblock
@@ -2411,10 +2461,10 @@ VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
  *    thread 2 on vring 0
  *
  * Memory regions (total 2)
- * region fd    guest_phys_addr    memory_size        userspace_addr     mmap_offset        mmap_addr
- * ====== ===== ================== ================== ================== ================== ==================
- *   0     60    0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
- *   1     61    0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
+ * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
+ * ====== == =============== =========== ============== =========== ==========
+ *   0    60  0x00000000     0x000a0000  0xaac00000     0x00000000  0x2b400000
+ *   1    61  0x000c0000     0x3ff40000  0xaacc0000     0x000c0000  0xabcc0000
  *
  *  Virtqueue 0 (TX)
  *   qsz 256 last_avail_idx 0 last_used_idx 0
@@ -2458,8 +2508,9 @@ VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
  *
  * @cliexend
  *
- * The optional '<em>descriptors</em>' parameter will display the same output as
- * the previous example but will include the descriptor table for each queue.
+ * The optional '<em>descriptors</em>' parameter will display the same output
+ * as the previous example but will include the descriptor table for each
+ * queue.
  * The output is truncated below:
  * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
  * Virtio vhost-user interfaces