Vhost-User: Fix bind sockaddr length
[vpp.git] / vnet / vnet / devices / dpdk / vhost_user.c
index a461d39..0df9f0b 100644 (file)
@@ -39,6 +39,8 @@
 #define DBG_SOCK(args...)
 #endif
 
+#if DPDK_VHOST_USER
+
 static const char *vhost_message_str[] __attribute__((unused)) = {
     [VHOST_USER_NONE] = "VHOST_USER_NONE",
     [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
@@ -55,18 +57,14 @@ static const char *vhost_message_str[] __attribute__((unused)) = {
     [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
     [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
     [VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR",
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
     [VHOST_USER_GET_PROTOCOL_FEATURES]  = "VHOST_USER_GET_PROTOCOL_FEATURES",
     [VHOST_USER_SET_PROTOCOL_FEATURES]  = "VHOST_USER_SET_PROTOCOL_FEATURES",
     [VHOST_USER_GET_QUEUE_NUM]  = "VHOST_USER_GET_QUEUE_NUM",
     [VHOST_USER_SET_VRING_ENABLE]  = "VHOST_USER_SET_VRING_ENABLE",
-#endif
 };
 
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
 static int dpdk_vhost_user_set_vring_enable(u32 hw_if_index,
     u8 idx, int enable);
-#endif
 
 /*
  * DPDK vhost-user functions 
@@ -157,21 +155,15 @@ static void stop_processing_packets(u32 hw_if_index, u8 idx)
   dpdk_device_t *xd =
     dpdk_vhost_user_device_from_hw_if_index(hw_if_index);
   assert(xd);
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
-#else
-  xd->vu_is_running = 0;
-#endif
 }
 
 static void disable_interface(dpdk_device_t * xd)
 {
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   u8 idx;
   int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
   for (idx = 0;  idx < numqs; idx++)
     xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
-#endif
 
   xd->vu_is_running = 0;
 }
@@ -203,9 +195,7 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
   int num_qpairs = 1;
   dpdk_vu_intf_t *vui = NULL;
 
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   num_qpairs = dm->use_rss < 1 ? 1 : tm->n_vlib_mains;
-#endif
 
   dpdk_device_t * xd = NULL;
   u8 addr[6];
@@ -274,9 +264,7 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
       xd->dev_type = VNET_DPDK_DEV_VHOST_USER;
       xd->rx_q_used = num_qpairs;
       xd->tx_q_used = num_qpairs;
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
       xd->vu_vhost_dev.virt_qp_nb = num_qpairs;
-#endif
 
       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used, CLIB_CACHE_LINE_BYTES);
 
@@ -392,10 +380,7 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
       if (tm->n_vlib_mains == 1 && dpdk_input_node.state != VLIB_NODE_STATE_POLLING)
         vlib_node_set_state (vm, dpdk_input_node.index, VLIB_NODE_STATE_POLLING);
 
-      if (tm->n_vlib_mains > 1 && tm->main_thread_is_io_node)
-        vlib_node_set_state (vm, dpdk_io_input_node.index, VLIB_NODE_STATE_POLLING);
-
-      if (tm->n_vlib_mains > 1 && !tm->main_thread_is_io_node)
+      if (tm->n_vlib_mains > 1)
         vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
                              VLIB_NODE_STATE_POLLING);
       next_cpu++;
@@ -414,7 +399,6 @@ static long get_huge_page_size(int fd)
 }
 #endif
 
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
 static clib_error_t *
 dpdk_vhost_user_set_protocol_features(u32 hw_if_index, u64 prot_features)
 {
@@ -424,7 +408,6 @@ dpdk_vhost_user_set_protocol_features(u32 hw_if_index, u64 prot_features)
   xd->vu_vhost_dev.protocol_features = prot_features;
   return 0;
 }
-#endif
 
 static clib_error_t *
 dpdk_vhost_user_get_features(u32 hw_if_index, u64 * features)
@@ -469,14 +452,11 @@ dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
 
   int numqs = VIRTIO_QNUM;
   u8 idx;
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   int prot_feature = features &
         (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
   numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
-#endif
   for (idx = 0; idx < numqs; idx++) {
       xd->vu_vhost_dev.virtqueue[idx]->vhost_hlen = hdr_len;
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
       /*
        * Spec says, if F_PROTOCOL_FEATURE is not set by the
        * slave, then all the vrings should start off as
@@ -485,7 +465,6 @@ dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
        */
       if (! prot_feature)
           dpdk_vhost_user_set_vring_enable(hw_if_index, idx, 1);
-#endif
   }
 
   return 0;
@@ -530,8 +509,12 @@ dpdk_vhost_user_set_mem_table(u32 hw_if_index, vhost_user_memory_t * vum, int fd
     mapped_address +=  vum->regions[i].mmap_offset;
     vui->region_addr[i] = mapped_address;
     vui->region_fd[i] = fd[i];
+    vui->region_offset[i] = vum->regions[i].mmap_offset;
     mem->regions[i].address_offset = mapped_address - mem->regions[i].guest_phys_address;
 
+    DBG_SOCK("map memory region %d addr 0x%lx off 0x%lx len 0x%lx",
+      i, vui->region_addr[i], vui->region_offset[i], mapped_size);
+
     if (vum->regions[i].guest_phys_addr == 0) {
       mem->base_address = vum->regions[i].userspace_addr;
       mem->mapped_address = mem->regions[i].address_offset;
@@ -589,6 +572,14 @@ dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, uword desc, \
     clib_warning("falied to set vring addr");
   }
 
+  if (vq->last_used_idx != vq->used->idx) {
+    clib_warning("last_used_idx (%u) and vq->used->idx (%u) mismatches; "
+                 "some packets maybe resent for Tx and dropped for Rx",
+                 vq->last_used_idx, vq->used->idx);
+      vq->last_used_idx     = vq->used->idx;
+      vq->last_used_idx_res = vq->used->idx;
+  }
+
   /*
    * Inform the guest that there is no need to inform (kick) the
    * host when it adds buffers. kick results in vmexit and will
@@ -625,7 +616,6 @@ dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
  * and stop ring upon receiving VHOST_USER_GET_VRING_BASE.
  */
   DBG_SOCK("Stopping vring Q %u of device %d", idx, hw_if_index);
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   dpdk_vu_intf_t *vui = xd->vu_intf;
   vui->vrings[idx].enabled = 0; /* Reset local copy */
   vui->vrings[idx].callfd = -1; /* Reset FD */
@@ -649,12 +639,6 @@ dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
       DBG_SOCK("Device %d disabled", hw_if_index);
       xd->vu_is_running = 0;
   }
-#else
-  vq->desc = NULL;
-  vq->used = NULL;
-  vq->avail = NULL;
-  xd->vu_is_running = 0;
-#endif
 
   return 0;
 }
@@ -686,9 +670,7 @@ dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
 {
   dpdk_main_t * dm = &dpdk_main;
   dpdk_device_t * xd;
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   dpdk_vu_vring *vring;
-#endif
   struct vhost_virtqueue *vq0, *vq1, *vq;
   int index, vu_is_running = 0;
 
@@ -700,29 +682,20 @@ dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
   vq = xd->vu_vhost_dev.virtqueue[idx];
   vq->kickfd = fd;
 
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   vring = &xd->vu_intf->vrings[idx];
   vq->enabled = (vq->desc && vq->avail && vq->used && vring->enabled) ? 1 : 0;
-#endif
 
   /*
    * Set xd->vu_is_running if at least one pair of
    * RX/TX queues are enabled.
    */
   int numqs = VIRTIO_QNUM;
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
-#endif
 
   for (index = 0; index < numqs; index += 2) {
     vq0 = xd->vu_vhost_dev.virtqueue[index]; /* RX */
     vq1 = xd->vu_vhost_dev.virtqueue[index + 1]; /* TX */
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
     if (vq0->enabled && vq1->enabled)
-#else
-    if (vq0->desc && vq0->avail && vq0->used &&
-      vq1->desc && vq1->avail && vq1->used)
-#endif
     {
         vu_is_running = 1;
         break;
@@ -741,7 +714,6 @@ dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
   return 0;
 }
 
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
 static int
 dpdk_vhost_user_set_vring_enable(u32 hw_if_index, u8 idx, int enable)
 {
@@ -781,7 +753,6 @@ dpdk_vhost_user_set_vring_enable(u32 hw_if_index, u8 idx, int enable)
 
   return 0;
 }
-#endif
 
 static clib_error_t * dpdk_vhost_user_callfd_read_ready (unix_file_t * uf)
 {
@@ -857,7 +828,7 @@ dpdk_vhost_user_send_interrupt(vlib_main_t * vm, dpdk_device_t * xd, int idx)
     if((vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
         eventfd_write(vring->callfd, (eventfd_t)1);
         vring->n_since_last_int = 0;
-        vring->int_deadline = vlib_time_now(vm) + dm->vhost_coalesce_time;
+        vring->int_deadline = vlib_time_now(vm) + dm->conf->vhost_coalesce_time;
     }
 }
 
@@ -877,11 +848,7 @@ dpdk_vhost_user_vui_init(vnet_main_t * vnm,
   memset(vui, 0, sizeof(*vui));
 
   vui->unix_fd = sockfd;
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
   vui->num_vrings = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
-#else
-  vui->num_vrings = VIRTIO_QNUM;
-#endif
   DBG_SOCK("dpdk_vhost_user_vui_init VRINGS: %d", vui->num_vrings);
   vui->sock_is_server = is_server;
   strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
@@ -908,12 +875,44 @@ dpdk_vhost_user_vui_register(vlib_main_t * vm, dpdk_device_t *xd)
             xd->vlib_sw_if_index);
 }
 
+static void dpdk_unmap_all_mem_regions(dpdk_device_t * xd)
+{
+  int i, r;
+  dpdk_vu_intf_t *vui = xd->vu_intf;
+  struct virtio_memory * mem = xd->vu_vhost_dev.mem;
+
+  for (i=0; i<mem->nregions; i++) {
+    if (vui->region_addr[i] != -1) {
+
+      long page_sz = get_huge_page_size(vui->region_fd[i]);
+
+      ssize_t map_sz = (mem->regions[i].memory_size +
+        vui->region_offset[i] + page_sz) & ~(page_sz - 1);
+
+      r = munmap((void *)(vui->region_addr[i] - vui->region_offset[i]), map_sz);
+
+      DBG_SOCK("unmap memory region %d addr 0x%lx off 0x%lx len 0x%lx page_sz 0x%x",
+        i, vui->region_addr[i], vui->region_offset[i], map_sz, page_sz);
+
+      vui->region_addr[i]= -1;
+
+      if (r == -1) {
+        clib_unix_warning("failed to unmap memory region");
+      }
+      close(vui->region_fd[i]);
+    }
+  }
+  mem->nregions = 0;
+}
+
 static inline void
 dpdk_vhost_user_if_disconnect(dpdk_device_t * xd)
 {
     dpdk_vu_intf_t *vui = xd->vu_intf;
     vnet_main_t * vnm = vnet_get_main();
     dpdk_main_t * dm = &dpdk_main;
+    struct vhost_virtqueue *vq;
+    int q;
 
     xd->admin_up = 0;
     vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,  0);
@@ -929,6 +928,21 @@ dpdk_vhost_user_if_disconnect(dpdk_device_t * xd)
     vui->unix_fd = -1;
     vui->is_up = 0;
 
+    for (q = 0; q < vui->num_vrings; q++) {
+        vq = xd->vu_vhost_dev.virtqueue[q];
+        vui->vrings[q].enabled = 0; /* Reset local copy */
+        vui->vrings[q].callfd = -1; /* Reset FD */
+        vq->enabled = 0;
+#if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
+        vq->log_guest_addr = 0;
+#endif
+        vq->desc = NULL;
+        vq->used = NULL;
+        vq->avail = NULL;
+    }
+    xd->vu_is_running = 0;
+
+    dpdk_unmap_all_mem_regions(xd);
     DBG_SOCK("interface ifindex %d disconnected", xd->vlib_sw_if_index);
 }
 
@@ -1205,7 +1219,6 @@ static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
         xd->vlib_hw_if_index);
       break;
 
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
     case VHOST_USER_GET_PROTOCOL_FEATURES:
       DBG_SOCK("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
         xd->vlib_hw_if_index);
@@ -1241,7 +1254,6 @@ static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
       msg.u64 = xd->vu_vhost_dev.virt_qp_nb;
       msg.size = sizeof(msg.u64);
       break;
-#endif
 
     default:
       DBG_SOCK("unknown vhost-user message %d received. closing socket",
@@ -1341,8 +1353,8 @@ static clib_error_t * dpdk_vhost_user_socksvr_accept_ready (unix_file_t * uf)
 // init server socket on specified sock_filename
 static int dpdk_vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
 {
-  int rv = 0, len;
-  struct sockaddr_un un;
+  int rv = 0;
+  struct sockaddr_un un = {};
   int fd;
   /* create listening socket */
   fd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -1357,9 +1369,7 @@ static int dpdk_vhost_user_init_server_sock(const char * sock_filename, int *soc
   /* remove if exists */
   unlink( (char *) sock_filename);
 
-  len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
-
-  if (bind(fd, (struct sockaddr *) &un, len) == -1) {
+  if (bind(fd, (struct sockaddr *) &un, sizeof(un)) == -1) {
     rv = VNET_API_ERROR_SYSCALL_ERROR_2;
     goto error;
   }
@@ -1400,7 +1410,7 @@ int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
   int rv = 0;
 
   // using virtio vhost user?
-  if (dm->use_virtio_vhost) {
+  if (dm->conf->use_virtio_vhost) {
       return vhost_user_create_if(vnm, vm, sock_filename, is_server,
               sw_if_index, feature_mask, renumber, custom_dev_instance, hwaddr);
   }
@@ -1446,7 +1456,7 @@ int dpdk_vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
   int rv = 0;
 
   // using virtio vhost user?
-  if (dm->use_virtio_vhost) {
+  if (dm->conf->use_virtio_vhost) {
       return vhost_user_modify_if(vnm, vm, sock_filename, is_server,
               sw_if_index, feature_mask, renumber, custom_dev_instance);
   }
@@ -1490,7 +1500,7 @@ int dpdk_vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
   int rv = 0;
 
   // using virtio vhost user?
-  if (dm->use_virtio_vhost) {
+  if (dm->conf->use_virtio_vhost) {
       return vhost_user_delete_if(vnm, vm, sw_if_index);
   }
 
@@ -1532,7 +1542,7 @@ int dpdk_vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_int
         return -1;
 
     // using virtio vhost user?
-    if (dm->use_virtio_vhost) {
+    if (dm->conf->use_virtio_vhost) {
         return vhost_user_dump_ifs(vnm, vm, out_vuids);
     }
 
@@ -1667,7 +1677,7 @@ dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
   u8 hwaddr[6];
   u8 *hw = NULL;
 
-  if (dm->use_virtio_vhost) {
+  if (dm->conf->use_virtio_vhost) {
       return vhost_user_connect_command_fn(vm, input, cmd);
   }
 
@@ -1722,7 +1732,7 @@ dpdk_vhost_user_delete_command_fn (vlib_main_t * vm,
   unformat_input_t _line_input, * line_input = &_line_input;
   u32 sw_if_index = ~0;
 
-  if (dm->use_virtio_vhost) {
+  if (dm->conf->use_virtio_vhost) {
       return vhost_user_delete_command_fn(vm, input, cmd);
   }
 
@@ -1789,7 +1799,7 @@ show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
   { .str = NULL }
   };
 
-  if (dm->use_virtio_vhost) {
+  if (dm->conf->use_virtio_vhost) {
     return show_vhost_user_command_fn(vm, input, cmd);
   }
 
@@ -1815,7 +1825,7 @@ show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
 
   vlib_cli_output (vm, "DPDK vhost-user interfaces");
   vlib_cli_output (vm, "Global:\n  coalesce frames %d time %e\n\n",
-                   dm->vhost_coalesce_frames, dm->vhost_coalesce_time);
+                   dm->conf->vhost_coalesce_frames, dm->conf->vhost_coalesce_time);
 
   for (i = 0; i < vec_len (hw_if_indices); i++) {
     hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
@@ -1878,17 +1888,10 @@ show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
         vlib_cli_output(vm, "  avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
           vq->avail->flags, vq->avail->idx, vq->used->flags, vq->used->idx);
 
-#if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
       vlib_cli_output(vm, "  kickfd %d callfd %d errfd %d enabled %d\n",
         vq->kickfd, vq->callfd, vui->vrings[q].errfd, vq->enabled);
 
       if (show_descr && vq->enabled) {
-#else
-      vlib_cli_output(vm, "  kickfd %d callfd %d errfd\n",
-        vq->kickfd, vq->callfd, vui->vrings[q].errfd);
-
-      if (show_descr) {
-#endif
         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");
@@ -1914,3 +1917,4 @@ VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
     .short_help = "show vhost-user interface",
     .function = show_dpdk_vhost_user_command_fn,
 };
+#endif