vhost: support interrupt mode
[vpp.git] / src / vnet / devices / virtio / vhost-user.c
index 3fbcee9..5a5beb1 100644 (file)
@@ -237,7 +237,8 @@ map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
   r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
 
   r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
-  i = __builtin_ctzll (_mm_movemask_epi8 (r));
+  i = __builtin_ctzll (_mm_movemask_epi8 (r) |
+                      (1 << VHOST_MEMORY_MAX_NREGIONS));
 
   if (i < vui->nregions)
     {
@@ -367,14 +368,15 @@ vhost_user_rx_thread_placement ()
   vhost_user_intf_t *vui;
   vhost_cpu_t *vhc;
   u32 *workers = 0;
+  u32 cpu_index;
+  vlib_main_t *vm;
 
   //Let's list all workers cpu indexes
   u32 i;
   for (i = vum->input_cpu_first_index;
        i < vum->input_cpu_first_index + vum->input_cpu_count; i++)
     {
-      vlib_node_set_state (vlib_mains ? vlib_mains[i] : &vlib_global_main,
-                          vhost_user_input_node.index,
+      vlib_node_set_state (vlib_mains[i], vhost_user_input_node.index,
                           VLIB_NODE_STATE_DISABLED);
       vec_add1 (workers, i);
     }
@@ -398,19 +400,59 @@ vhost_user_rx_thread_placement ()
            continue;
 
          i %= vec_len (vui_workers);
-         u32 cpu_index = vui_workers[i];
+         cpu_index = vui_workers[i];
          i++;
          vhc = &vum->cpus[cpu_index];
 
          iaq.qid = qid;
          iaq.vhost_iface_index = vui - vum->vhost_user_interfaces;
          vec_add1 (vhc->rx_queues, iaq);
-         vlib_node_set_state (vlib_mains ? vlib_mains[cpu_index] :
-             &vlib_global_main, vhost_user_input_node.index,
-             VLIB_NODE_STATE_POLLING);
        }
   });
   /* *INDENT-ON* */
+
+  vec_foreach (vhc, vum->cpus)
+  {
+    vhost_iface_and_queue_t *vhiq;
+    u8 mode = VHOST_USER_INTERRUPT_MODE;
+
+    vec_foreach (vhiq, vhc->rx_queues)
+    {
+      vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
+      if (vui->operation_mode == VHOST_USER_POLLING_MODE)
+       {
+         /* At least one interface is polling, cpu is set to polling */
+         mode = VHOST_USER_POLLING_MODE;
+         break;
+       }
+    }
+    vhc->operation_mode = mode;
+  }
+
+  for (cpu_index = vum->input_cpu_first_index;
+       cpu_index < vum->input_cpu_first_index + vum->input_cpu_count;
+       cpu_index++)
+    {
+      vlib_node_state_t state = VLIB_NODE_STATE_POLLING;
+
+      vhc = &vum->cpus[cpu_index];
+      vm = vlib_mains ? vlib_mains[cpu_index] : &vlib_global_main;
+      switch (vhc->operation_mode)
+       {
+       case VHOST_USER_INTERRUPT_MODE:
+         state = VLIB_NODE_STATE_INTERRUPT;
+         break;
+       case VHOST_USER_POLLING_MODE:
+         state = VLIB_NODE_STATE_POLLING;
+         break;
+       default:
+         clib_warning ("BUG: bad operation mode %d", vhc->operation_mode);
+         break;
+       }
+      vlib_node_set_state (vm, vhost_user_input_node.index, state);
+    }
+
+  vec_free (workers);
 }
 
 static int
@@ -485,12 +527,68 @@ vhost_user_update_iface_state (vhost_user_intf_t * vui)
   vhost_user_tx_thread_placement (vui);
 }
 
+static void
+vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
+{
+  vhost_user_main_t *vum = &vhost_user_main;
+  vhost_cpu_t *vhc;
+  u32 cpu_index;
+  vhost_iface_and_queue_t *vhiq;
+  vlib_main_t *vm;
+  u32 ifq2;
+  u8 done = 0;
+
+  if (vhost_user_intf_ready (vui))
+    {
+      vec_foreach (vhc, vum->cpus)
+      {
+       if (vhc->operation_mode == VHOST_USER_POLLING_MODE)
+         continue;
+
+       vec_foreach (vhiq, vhc->rx_queues)
+       {
+         /*
+          * Match the interface and the virtqueue number
+          */
+         if ((vhiq->vhost_iface_index == (ifq >> 8)) &&
+             (VHOST_VRING_IDX_TX (vhiq->qid) == (ifq & 0xff)))
+           {
+             cpu_index = vhc - vum->cpus;
+             vm = vlib_mains ? vlib_mains[cpu_index] : &vlib_global_main;
+             /*
+              * Convert RX virtqueue number in the lower byte to vring
+              * queue index for the input node process. Top bytes contain
+              * the interface, lower byte contains the queue index.
+              */
+             ifq2 = ((ifq >> 8) << 8) | vhiq->qid;
+             vhc->pending_input_bitmap =
+               clib_bitmap_set (vhc->pending_input_bitmap, ifq2, 1);
+             vlib_node_set_interrupt_pending (vm,
+                                              vhost_user_input_node.index);
+             done = 1;
+             break;
+           }
+       }
+       if (done)
+         break;
+      }
+    }
+}
+
 static clib_error_t *
 vhost_user_callfd_read_ready (unix_file_t * uf)
 {
   __attribute__ ((unused)) int n;
   u8 buff[8];
+  vhost_user_intf_t *vui =
+    pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
+                      uf->private_data >> 8);
+
   n = read (uf->file_descriptor, ((char *) &buff), 8);
+  DBG_SOCK ("if %d CALL queue %d", uf->private_data >> 8,
+           uf->private_data & 0xff);
+  vhost_user_set_interrupt_pending (vui, uf->private_data);
+
   return 0;
 }
 
@@ -503,13 +601,20 @@ vhost_user_kickfd_read_ready (unix_file_t * uf)
     pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
                       uf->private_data >> 8);
   u32 qid = uf->private_data & 0xff;
+
   n = read (uf->file_descriptor, ((char *) &buff), 8);
   DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
 
   vlib_worker_thread_barrier_sync (vlib_get_main ());
-  vui->vrings[qid].started = 1;
-  vhost_user_update_iface_state (vui);
+  if (!vui->vrings[qid].started ||
+      (vhost_user_intf_ready (vui) != vui->is_up))
+    {
+      vui->vrings[qid].started = 1;
+      vhost_user_update_iface_state (vui);
+    }
   vlib_worker_thread_barrier_release (vlib_get_main ());
+
+  vhost_user_set_interrupt_pending (vui, uf->private_data);
   return 0;
 }
 
@@ -583,7 +688,10 @@ vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
       vring->callfd_idx = ~0;
     }
   if (vring->errfd != -1)
-    close (vring->errfd);
+    {
+      close (vring->errfd);
+      vring->errfd = -1;
+    }
   vhost_user_vring_init (vui, qid);
 }
 
@@ -904,8 +1012,12 @@ vhost_user_socket_read (unix_file_t * uf)
        vui->vrings[msg.state.index].last_avail_idx =
        vui->vrings[msg.state.index].used->idx;
 
-      /* tell driver that we don't want interrupts */
-      vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
+      if (vui->operation_mode == VHOST_USER_POLLING_MODE)
+       /* tell driver that we don't want interrupts */
+       vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
+      else
+       /* tell driver that we want interrupts */
+       vui->vrings[msg.state.index].used->flags = 0;
       break;
 
     case VHOST_USER_SET_OWNER:
@@ -1026,12 +1138,16 @@ vhost_user_socket_read (unix_file_t * uf)
          goto close_socket;
        }
 
-      /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
-      vhost_user_vring_close (vui, msg.state.index);
-
+      /*
+       * Copy last_avail_idx from the vring before closing it because
+       * closing the vring also initializes the vring last_avail_idx
+       */
       msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
       msg.flags |= 4;
       msg.size = sizeof (msg.state);
+
+      /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
+      vhost_user_vring_close (vui, msg.state.index);
       break;
 
     case VHOST_USER_NONE:
@@ -1677,6 +1793,10 @@ vhost_user_if_input (vlib_main_t * vm,
                  if (PREDICT_FALSE
                      (vum->cpus[cpu_index].rx_buffers_len == 0))
                    {
+                     /* Cancel speculation */
+                     to_next--;
+                     n_left_to_next++;
+
                      /*
                       * Checking if there are some left buffers.
                       * If not, just rewind the used buffers and stop.
@@ -1742,7 +1862,7 @@ vhost_user_if_input (vlib_main_t * vm,
 
            /* redirect if feature path enabled */
            vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
-                                               b_head, 0);
+                                               b_head);
 
            u32 bi = to_next[-1];       //Cannot use to_next[-1] in the macro
            vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
@@ -1800,7 +1920,8 @@ vhost_user_if_input (vlib_main_t * vm,
   vhost_user_log_dirty_ring (vui, txvq, idx);
 
   /* interrupt (call) handling */
-  if ((txvq->callfd_idx != ~0) && !(txvq->avail->flags & 1))
+  if ((txvq->callfd_idx != ~0) &&
+      !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
     {
       txvq->n_since_last_int += n_rx_packets;
 
@@ -1814,6 +1935,8 @@ vhost_user_if_input (vlib_main_t * vm,
      + VNET_INTERFACE_COUNTER_RX,
      os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
 
+  vnet_device_increment_rx_packets (cpu_index, n_rx_packets);
+
   return n_rx_packets;
 }
 
@@ -1824,16 +1947,33 @@ vhost_user_input (vlib_main_t * vm,
   vhost_user_main_t *vum = &vhost_user_main;
   uword n_rx_packets = 0;
   u32 cpu_index = os_get_cpu_number ();
+  vhost_iface_and_queue_t *vhiq;
+  vhost_user_intf_t *vui;
+  vhost_cpu_t *vhc;
 
+  vhc = &vum->cpus[cpu_index];
+  if (PREDICT_TRUE (vhc->operation_mode == VHOST_USER_POLLING_MODE))
+    {
+      vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
+      {
+       vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
+       n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
+      }
+    }
+  else
+    {
+      int i;
 
-  vhost_iface_and_queue_t *vhiq;
-  vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
-  {
-    vhost_user_intf_t *vui =
-      &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
-    n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
-  }
+      /* *INDENT-OFF* */
+      clib_bitmap_foreach (i, vhc->pending_input_bitmap, ({
+       int qid = i & 0xff;
 
+       clib_bitmap_set (vhc->pending_input_bitmap, i, 0);
+       vui = pool_elt_at_index (vum->vhost_user_interfaces, i >> 8);
+       n_rx_packets += vhost_user_if_input (vm, vum, vui, qid, node);
+      }));
+      /* *INDENT-ON* */
+    }
   return n_rx_packets;
 }
 
@@ -2228,7 +2368,8 @@ done:
     }
 
   /* interrupt (call) handling */
-  if ((rxvq->callfd_idx != ~0) && !(rxvq->avail->flags & 1))
+  if ((rxvq->callfd_idx != ~0) &&
+      !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
     {
       rxvq->n_since_last_int += frame->n_vectors - n_left;
 
@@ -2326,12 +2467,16 @@ vhost_user_process (vlib_main_t * vm,
                           sizeof (sun.sun_path) - 1);
 
                  /* Avoid hanging VPP if the other end does not accept */
-                 fcntl(sockfd, F_SETFL, O_NONBLOCK);
+                 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
+                      clib_unix_warning ("fcntl");
+
                  if (connect (sockfd, (struct sockaddr *) &sun,
                               sizeof (struct sockaddr_un)) == 0)
                    {
                      /* Set the socket to blocking as it was before */
-                     fcntl(sockfd, F_SETFL, 0);
+                      if (fcntl(sockfd, F_SETFL, 0) < 0)
+                        clib_unix_warning ("fcntl2");
+
                      vui->sock_errno = 0;
                      template.file_descriptor = sockfd;
                      template.private_data =
@@ -2386,12 +2531,19 @@ VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
 static void
 vhost_user_term_if (vhost_user_intf_t * vui)
 {
+  int q;
+
   // Delete configured thread pinning
   vec_reset_length (vui->workers);
   // disconnect interface sockets
   vhost_user_if_disconnect (vui);
   vhost_user_update_iface_state (vui);
 
+  for (q = 0; q < VHOST_VRING_MAX_N; q++)
+    {
+      clib_mem_free ((void *) vui->vring_locks[q]);
+    }
+
   if (vui->unix_server_index != ~0)
     {
       //Close server socket
@@ -2422,9 +2574,6 @@ vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
   // Disable and reset interface
   vhost_user_term_if (vui);
 
-  // Back to pool
-  pool_put (vum->vhost_user_interfaces, vui);
-
   // Reset renumbered iface
   if (hwif->dev_instance <
       vec_len (vum->show_dev_instance_by_real_dev_instance))
@@ -2432,6 +2581,10 @@ vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
 
   // Delete ethernet interface
   ethernet_delete_interface (vnm, vui->hw_if_index);
+
+  // Back to pool
+  pool_put (vum->vhost_user_interfaces, vui);
+
   return rv;
 }
 
@@ -2521,7 +2674,7 @@ vhost_user_vui_init (vnet_main_t * vnm,
                     vhost_user_intf_t * vui,
                     int server_sock_fd,
                     const char *sock_filename,
-                    u64 feature_mask, u32 * sw_if_index)
+                    u64 feature_mask, u32 * sw_if_index, u8 operation_mode)
 {
   vnet_sw_interface_t *sw;
   sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
@@ -2548,6 +2701,7 @@ vhost_user_vui_init (vnet_main_t * vnm,
   vui->feature_mask = feature_mask;
   vui->unix_file_index = ~0;
   vui->log_base_addr = 0;
+  vui->operation_mode = operation_mode;
 
   for (q = 0; q < VHOST_VRING_MAX_N; q++)
     vhost_user_vring_init (vui, q);
@@ -2569,18 +2723,113 @@ vhost_user_vui_init (vnet_main_t * vnm,
   vhost_user_tx_thread_placement (vui);
 }
 
+static uword
+vhost_user_send_interrupt_process (vlib_main_t * vm,
+                                  vlib_node_runtime_t * rt, vlib_frame_t * f)
+{
+  vhost_user_intf_t *vui;
+  f64 timeout = 3153600000.0 /* 100 years */ ;
+  uword event_type, *event_data = 0;
+  vhost_user_main_t *vum = &vhost_user_main;
+  vhost_iface_and_queue_t *vhiq;
+  vhost_cpu_t *vhc;
+  f64 now, poll_time_remaining;
+
+  while (1)
+    {
+      poll_time_remaining =
+       vlib_process_wait_for_event_or_clock (vm, timeout);
+      event_type = vlib_process_get_events (vm, &event_data);
+      vec_reset_length (event_data);
+
+      /*
+       * Use the remaining timeout if it is less than coalesce time to avoid
+       * resetting the existing timer in the middle of expiration
+       */
+      timeout = poll_time_remaining;
+      if (vlib_process_suspend_time_is_zero (timeout) ||
+         (timeout > vum->coalesce_time))
+       timeout = vum->coalesce_time;
+
+      now = vlib_time_now (vm);
+      switch (event_type)
+       {
+       case VHOST_USER_EVENT_START_TIMER:
+         if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
+           break;
+         /* fall through */
+
+       case ~0:
+         vec_foreach (vhc, vum->cpus)
+         {
+           u32 cpu_index = vhc - vum->cpus;
+           f64 next_timeout;
+
+           next_timeout = timeout;
+           vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
+           {
+             vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
+             vhost_user_vring_t *rxvq =
+               &vui->vrings[VHOST_VRING_IDX_RX (vhiq->qid)];
+             vhost_user_vring_t *txvq =
+               &vui->vrings[VHOST_VRING_IDX_TX (vhiq->qid)];
+
+             if (txvq->n_since_last_int)
+               {
+                 if (now >= txvq->int_deadline)
+                   vhost_user_send_call (vm, txvq);
+                 else
+                   next_timeout = txvq->int_deadline - now;
+               }
+
+             if (rxvq->n_since_last_int)
+               {
+                 if (now >= rxvq->int_deadline)
+                   vhost_user_send_call (vm, rxvq);
+                 else
+                   next_timeout = rxvq->int_deadline - now;
+               }
+
+             if ((next_timeout < timeout) && (next_timeout > 0.0))
+               timeout = next_timeout;
+           }
+         }
+         break;
+
+       default:
+         clib_warning ("BUG: unhandled event type %d", event_type);
+         break;
+       }
+    }
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
+    .function = vhost_user_send_interrupt_process,
+    .type = VLIB_NODE_TYPE_PROCESS,
+    .name = "vhost-user-send-interrupt-process",
+};
+/* *INDENT-ON* */
+
 int
 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
                      const char *sock_filename,
                      u8 is_server,
                      u32 * sw_if_index,
                      u64 feature_mask,
-                     u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
+                     u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
+                     u8 operation_mode)
 {
   vhost_user_intf_t *vui = NULL;
   u32 sw_if_idx = ~0;
   int rv = 0;
   int server_sock_fd = -1;
+  vhost_user_main_t *vum = &vhost_user_main;
+
+  if ((operation_mode != VHOST_USER_POLLING_MODE) &&
+      (operation_mode != VHOST_USER_INTERRUPT_MODE))
+    return VNET_API_ERROR_UNIMPLEMENTED;
 
   if (sock_filename == NULL || !(strlen (sock_filename) > 0))
     {
@@ -2600,7 +2849,7 @@ vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
 
   vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
   vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
-                      feature_mask, &sw_if_idx);
+                      feature_mask, &sw_if_idx, operation_mode);
 
   if (renumber)
     vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
@@ -2610,6 +2859,15 @@ vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
 
   // Process node must connect
   vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
+
+  if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
+      !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
+      (vum->coalesce_frames > 0))
+    {
+      vum->interrupt_mode = 1;
+      vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
+                                VHOST_USER_EVENT_START_TIMER, 0);
+    }
   return rv;
 }
 
@@ -2618,7 +2876,8 @@ vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
                      const char *sock_filename,
                      u8 is_server,
                      u32 sw_if_index,
-                     u64 feature_mask, u8 renumber, u32 custom_dev_instance)
+                     u64 feature_mask, u8 renumber, u32 custom_dev_instance,
+                     u8 operation_mode)
 {
   vhost_user_main_t *vum = &vhost_user_main;
   vhost_user_intf_t *vui = NULL;
@@ -2627,6 +2886,9 @@ vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
   int rv = 0;
   vnet_hw_interface_t *hwif;
 
+  if ((operation_mode != VHOST_USER_POLLING_MODE) &&
+      (operation_mode != VHOST_USER_INTERRUPT_MODE))
+    return VNET_API_ERROR_UNIMPLEMENTED;
   if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
       hwif->dev_class_index != vhost_user_dev_class.index)
     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
@@ -2641,16 +2903,42 @@ vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
 
   vhost_user_term_if (vui);
   vhost_user_vui_init (vnm, vui, server_sock_fd,
-                      sock_filename, feature_mask, &sw_if_idx);
+                      sock_filename, feature_mask, &sw_if_idx,
+                      operation_mode);
 
   if (renumber)
     vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
 
   // Process node must connect
   vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
+
+  if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
+      !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
+      (vum->coalesce_frames > 0))
+    {
+      vum->interrupt_mode = 1;
+      vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
+                                VHOST_USER_EVENT_START_TIMER, 0);
+    }
   return rv;
 }
 
+static uword
+unformat_vhost_user_operation_mode (unformat_input_t * input, va_list * args)
+{
+  u8 *operation_mode = va_arg (*args, u8 *);
+  uword rc = 1;
+
+  if (unformat (input, "interrupt"))
+    *operation_mode = VHOST_USER_INTERRUPT_MODE;
+  else if (unformat (input, "polling"))
+    *operation_mode = VHOST_USER_POLLING_MODE;
+  else
+    rc = 0;
+
+  return rc;
+}
+
 clib_error_t *
 vhost_user_connect_command_fn (vlib_main_t * vm,
                               unformat_input_t * input,
@@ -2665,6 +2953,8 @@ vhost_user_connect_command_fn (vlib_main_t * vm,
   u32 custom_dev_instance = ~0;
   u8 hwaddr[6];
   u8 *hw = NULL;
+  clib_error_t *error = NULL;
+  u8 operation_mode = VHOST_USER_POLLING_MODE;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -2686,27 +2976,37 @@ vhost_user_connect_command_fn (vlib_main_t * vm,
        {
          renumber = 1;
        }
+      else if (unformat (line_input, "mode %U",
+                        unformat_vhost_user_operation_mode, &operation_mode))
+       ;
       else
-       return clib_error_return (0, "unknown input `%U'",
-                                 format_unformat_error, input);
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
     }
-  unformat_free (line_input);
 
   vnet_main_t *vnm = vnet_get_main ();
 
   int rv;
   if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
                                  is_server, &sw_if_index, feature_mask,
-                                 renumber, custom_dev_instance, hw)))
+                                 renumber, custom_dev_instance, hw,
+                                 operation_mode)))
     {
-      vec_free (sock_filename);
-      return clib_error_return (0, "vhost_user_create_if returned %d", rv);
+      error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
+      goto done;
     }
 
-  vec_free (sock_filename);
   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
                   sw_if_index);
-  return 0;
+
+done:
+  vec_free (sock_filename);
+  unformat_free (line_input);
+
+  return error;
 }
 
 clib_error_t *
@@ -2717,6 +3017,7 @@ vhost_user_delete_command_fn (vlib_main_t * vm,
   unformat_input_t _line_input, *line_input = &_line_input;
   u32 sw_if_index = ~0;
   vnet_main_t *vnm = vnet_get_main ();
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -2734,15 +3035,25 @@ vhost_user_delete_command_fn (vlib_main_t * vm,
            vnet_get_sup_hw_interface (vnm, sw_if_index);
          if (hwif == NULL ||
              vhost_user_dev_class.index != hwif->dev_class_index)
-           return clib_error_return (0, "Not a vhost interface");
+           {
+             error = clib_error_return (0, "Not a vhost interface");
+             goto done;
+           }
        }
       else
-       return clib_error_return (0, "unknown input `%U'",
-                                 format_unformat_error, input);
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
     }
-  unformat_free (line_input);
+
   vhost_user_delete_if (vnm, vm, sw_if_index);
-  return 0;
+
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 int
@@ -2772,10 +3083,12 @@ vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
       vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
 
       vec_add2 (r_vuids, vuid, 1);
+      vuid->operation_mode = vui->operation_mode;
       vuid->sw_if_index = vui->sw_if_index;
       vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
       vuid->features = vui->features;
       vuid->num_regions = vui->nregions;
+      vuid->is_server = vui->unix_server_index != ~0;
       vuid->sock_errno = vui->sock_errno;
       strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
               ARRAY_LEN (vuid->sock_filename) - 1);
@@ -2795,6 +3108,25 @@ vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
   return rv;
 }
 
+static u8 *
+format_vhost_user_operation_mode (u8 * s, va_list * va)
+{
+  int operation_mode = va_arg (*va, int);
+
+  switch (operation_mode)
+    {
+    case VHOST_USER_POLLING_MODE:
+      s = format (s, "%s", "polling");
+      break;
+    case VHOST_USER_INTERRUPT_MODE:
+      s = format (s, "%s", "interrupt");
+      break;
+    default:
+      s = format (s, "%s", "invalid");
+    }
+  return s;
+}
+
 clib_error_t *
 show_vhost_user_command_fn (vlib_main_t * vm,
                            unformat_input_t * input,
@@ -2903,14 +3235,22 @@ show_vhost_user_command_fn (vlib_main_t * vm,
                       (vui->unix_server_index != ~0) ? "server" : "client",
                       strerror (vui->sock_errno));
 
+      vlib_cli_output (vm, " configured mode: %U\n",
+                      format_vhost_user_operation_mode, vui->operation_mode);
       vlib_cli_output (vm, " rx placement: ");
       vec_foreach (vhc, vum->cpus)
       {
        vec_foreach (vhiq, vhc->rx_queues)
        {
          if (vhiq->vhost_iface_index == vui - vum->vhost_user_interfaces)
-           vlib_cli_output (vm, "   thread %d on vring %d\n",
-                            vhc - vum->cpus, VHOST_VRING_IDX_TX (vhiq->qid));
+           {
+             vlib_cli_output (vm, "   thread %d on vring %d\n",
+                              vhc - vum->cpus,
+                              VHOST_VRING_IDX_TX (vhiq->qid));
+             vlib_cli_output (vm, "   mode: %U\n",
+                              format_vhost_user_operation_mode,
+                              vhc->operation_mode);
+           }
        }
       }
 
@@ -3040,6 +3380,9 @@ done:
  * 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>mode [interrupt | polling]</b> - Optional parameter specifying
+ * the input thread polling policy.
+ *
  * @cliexpar
  * Example of how to create a vhost interface with VPP as the client and all features enabled:
  * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
@@ -3056,7 +3399,9 @@ done:
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
     .path = "create vhost-user",
-    .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>]",
+    .short_help = "create vhost-user socket <socket-filename> [server] "
+    "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] "
+    "[mode {interrupt | polling}]",
     .function = vhost_user_connect_command_fn,
 };
 /* *INDENT-ON* */
@@ -3268,6 +3613,7 @@ vhost_thread_command_fn (vlib_main_t * vm,
   u32 sw_if_index;
   u8 del = 0;
   int rv;
+  clib_error_t *error = NULL;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -3277,9 +3623,9 @@ vhost_thread_command_fn (vlib_main_t * vm,
       (line_input, "%U %d", unformat_vnet_sw_interface, vnet_get_main (),
        &sw_if_index, &worker_thread_index))
     {
-      unformat_free (line_input);
-      return clib_error_return (0, "unknown input `%U'",
-                               format_unformat_error, input);
+      error = clib_error_return (0, "unknown input `%U'",
+                                format_unformat_error, line_input);
+      goto done;
     }
 
   if (unformat (line_input, "del"))
@@ -3287,9 +3633,16 @@ vhost_thread_command_fn (vlib_main_t * vm,
 
   if ((rv =
        vhost_user_thread_placement (sw_if_index, worker_thread_index, del)))
-    return clib_error_return (0, "vhost_user_thread_placement returned %d",
-                             rv);
-  return 0;
+    {
+      error = clib_error_return (0, "vhost_user_thread_placement returned %d",
+                                rv);
+      goto done;
+    }
+
+done:
+  unformat_free (line_input);
+
+  return error;
 }