vhost: low performance in interrupt mode in some cases
[vpp.git] / src / vnet / devices / virtio / vhost_user.c
index daa1260..3217c72 100644 (file)
@@ -36,6 +36,7 @@
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/devices/devices.h>
 #include <vnet/feature/feature.h>
+#include <vnet/interface/rx_queue_funcs.h>
 
 #include <vnet/devices/virtio/vhost_user.h>
 #include <vnet/devices/virtio/vhost_user_inline.h>
@@ -165,17 +166,19 @@ vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
 
   ASSERT ((qid & 1) == 1);     // should be odd
   // Assign new queue mappings for the interface
-  vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
-                                   vhost_user_input_node.index);
-  vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, q, ~0);
+  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);
   if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
     /* Set polling as the default */
     txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
   txvq->qid = q;
-  rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, q, txvq->mode);
+  rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode);
   if (rv)
     vu_log_warn (vui, "unable to set rx mode for interface %d, "
                 "queue %d: rc=%d", vui->hw_if_index, q, rv);
+  vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
 }
 
 /** @brief Returns whether at least one TX and one RX vring are enabled */
@@ -208,22 +211,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 ();
-
-  qid = ifq & 0xff;
-  if ((qid & 1) == 0)
-    /* Only care about the odd number, or TX, virtqueue */
-    return;
-
-  if (vhost_user_intf_ready (vui))
-    // qid >> 1 is to convert virtqueue number to vring queue index
-    vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
-}
-
 static clib_error_t *
 vhost_user_callfd_read_ready (clib_file_t * uf)
 {
@@ -250,27 +237,32 @@ vhost_user_thread_placement (vhost_user_intf_t * vui, u32 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);
     }
 
-  vhost_user_set_interrupt_pending (vui, uf->private_data);
+  if (is_txq && (vhost_user_intf_ready (vui) &&
+                ((vq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
+                 (vq->mode == VNET_HW_IF_RX_MODE_INTERRUPT))))
+    vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index);
+
   return 0;
 }
 
@@ -507,12 +499,16 @@ vhost_user_socket_read (clib_file_t * uf)
       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);
+       {
+         hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
+                      VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
+                      VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
+       }
       else
-       hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
-                      VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
+       {
+         hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
+                       VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
+       }
       vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
       vui->is_ready = 0;
       vhost_user_update_iface_state (vui);
@@ -1259,7 +1255,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)
     {
@@ -1312,6 +1307,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;
 
@@ -1370,24 +1366,6 @@ vhost_user_term_if (vhost_user_intf_t * vui)
 
   for (q = 0; q < vui->num_qid; q++)
     {
-      // Remove existing queue mapping for the interface
-      if (q & 1)
-       {
-         int rv;
-         vnet_main_t *vnm = vnet_get_main ();
-         vhost_user_vring_t *txvq = &vui->vrings[q];
-
-         if (txvq->qid != -1)
-           {
-             rv = vnet_hw_interface_unassign_rx_thread (vnm,
-                                                        vui->hw_if_index,
-                                                        q >> 1);
-             if (rv)
-               vu_log_warn (vui, "unable to unassign interface %d, "
-                            "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
-           }
-       }
-
       clib_spinlock_free (&vui->vrings[q].vring_lock);
     }
 
@@ -1531,17 +1509,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;
   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
     {
@@ -1628,7 +1607,7 @@ 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;
+  hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
   vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
 
   if (sw_if_index)
@@ -1677,7 +1656,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);
@@ -1785,7 +1764,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;
@@ -2224,19 +2203,14 @@ show_vhost_user_command_fn (vlib_main_t * vm,
        {
          vnet_main_t *vnm = vnet_get_main ();
          uword thread_index;
-         vnet_hw_if_rx_mode mode;
          vhost_user_vring_t *txvq = &vui->vrings[qid];
 
          if (txvq->qid == -1)
            continue;
          thread_index =
-           vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
-                                               qid >> 1);
-         vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
-                                        &mode);
-         vlib_cli_output (vm, "   thread %d on vring %d, %U\n",
-                          thread_index, qid,
-                          format_vnet_hw_if_rx_mode, mode);
+           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);
        }
 
       vlib_cli_output (vm, " tx placement: %s\n",