Add startup conf options to set per-interface descriptor ring size
[vpp.git] / vnet / vnet / devices / dpdk / vhost_user.c
index 735c175..b1615b5 100644 (file)
@@ -106,11 +106,11 @@ static int dpdk_vhost_user_set_vring_enable(u32 hw_if_index,
  */
 
 
-static uint64_t
-qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
+static uword
+qva_to_vva(struct virtio_net *dev, uword qemu_va)
 {
   struct virtio_memory_regions *region;
-  uint64_t vhost_va = 0;
+  uword vhost_va = 0;
   uint32_t regionidx = 0;
 
   /* Find the region where the address lives. */
@@ -176,7 +176,7 @@ static void disable_interface(dpdk_device_t * xd)
   xd->vu_is_running = 0;
 }
 
-static inline void * map_guest_mem(dpdk_device_t * xd, u64 addr)
+static inline void * map_guest_mem(dpdk_device_t * xd, uword addr)
 {
   dpdk_vu_intf_t * vui = xd->vu_intf;
   struct virtio_memory * mem = xd->vu_vhost_dev.mem;
@@ -184,7 +184,7 @@ static inline void * map_guest_mem(dpdk_device_t * xd, u64 addr)
   for (i=0; i<mem->nregions; i++) {
     if ((mem->regions[i].guest_phys_address <= addr) &&
        ((mem->regions[i].guest_phys_address + mem->regions[i].memory_size) > addr)) {
-         return (void *) (vui->region_addr[i] + addr - mem->regions[i].guest_phys_address);
+         return (void *) ((uword)vui->region_addr[i] + addr - (uword)mem->regions[i].guest_phys_address);
        }
   }
   DBG_SOCK("failed to map guest mem addr %lx", addr);
@@ -201,6 +201,7 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
   clib_error_t * error;
   dpdk_device_and_queue_t * dq;
   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;
@@ -238,11 +239,14 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
           xd->vu_if_id = if_id;
 
       // reset virtqueues
+      vui = xd->vu_intf;
       for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
           memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
           xd->vu_vhost_dev.virtqueue[j]->kickfd = -1; 
           xd->vu_vhost_dev.virtqueue[j]->callfd = -1; 
           xd->vu_vhost_dev.virtqueue[j]->backend = -1; 
+          vui->vrings[j].packets = 0;
+          vui->vrings[j].bytes = 0;
        }
 
       // reset lockp
@@ -283,7 +287,7 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
 
       xd->device_index = xd - dm->devices;
       xd->per_interface_next_index = ~0;
-      xd->vu_intf = NULL;
+      xd->vu_intf = clib_mem_alloc (sizeof(*(xd->vu_intf)));
 
       xd->vu_vhost_dev.mem = clib_mem_alloc (sizeof(struct virtio_memory) +
                                              VHOST_MEMORY_MAX_NREGIONS *
@@ -296,12 +300,15 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
        * New virtqueue structure is an array of VHOST_MAX_QUEUE_PAIRS * 2
        * We need to allocate numq pairs.
        */
+      vui = xd->vu_intf;
       for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
           xd->vu_vhost_dev.virtqueue[j] = clib_mem_alloc (sizeof(struct vhost_virtqueue));
           memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
           xd->vu_vhost_dev.virtqueue[j]->kickfd = -1; 
           xd->vu_vhost_dev.virtqueue[j]->callfd = -1; 
           xd->vu_vhost_dev.virtqueue[j]->backend = -1; 
+          vui->vrings[j].packets = 0;
+          vui->vrings[j].bytes = 0;
       }
 
       dpdk_device_lock_init(xd);
@@ -335,14 +342,14 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
    * Generate random MAC address for the interface
    */
   if (hwaddr) {
-    memcpy(addr, hwaddr, sizeof(addr));
+    clib_memcpy(addr, hwaddr, sizeof(addr));
   } else {
     f64 now = vlib_time_now(vm);
     u32 rnd;
     rnd = (u32) (now * 1e6);
     rnd = random_u32 (&rnd);
 
-    memcpy (addr+2, &rnd, sizeof(rnd));
+    clib_memcpy (addr+2, &rnd, sizeof(rnd));
     addr[0] = 2;
     addr[1] = 0xfe;
   }
@@ -361,9 +368,6 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
   sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
   xd->vlib_sw_if_index = sw->sw_if_index;
 
-  if (!xd->vu_intf)
-      xd->vu_intf = clib_mem_alloc (sizeof(*(xd->vu_intf)));
-
   *hw_if_index = xd->vlib_hw_if_index;
 
   DBG_SOCK("xd->device_index: %d, dm->input_cpu_count: %d, "
@@ -401,6 +405,15 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
   return 0;
 }
 
+#if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
+static long get_huge_page_size(int fd)
+{
+  struct statfs s;
+  fstatfs(fd, &s);
+  return s.f_bsize;
+}
+#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)
@@ -418,6 +431,21 @@ dpdk_vhost_user_get_features(u32 hw_if_index, u64 * features)
 {
   *features = rte_vhost_feature_get();
 
+#if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
+#define OFFLOAD_FEATURES ((1ULL << VIRTIO_NET_F_HOST_TSO4) | \
+               (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
+               (1ULL << VIRTIO_NET_F_CSUM)    | \
+               (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
+               (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
+               (1ULL << VIRTIO_NET_F_GUEST_TSO6))
+
+  /* These are not suppoted as bridging/tunneling VHOST
+   * interfaces with hardware interfaces/drivers that does
+   * not support offloading breaks L4 traffic.
+   */
+  *features &= (~OFFLOAD_FEATURES);
+#endif
+
   DBG_SOCK("supported features: 0x%lx", *features);
   return 0;
 }
@@ -491,9 +519,9 @@ dpdk_vhost_user_set_mem_table(u32 hw_if_index, vhost_user_memory_t * vum, int fd
     mem->regions[i].userspace_address      = vum->regions[i].userspace_addr;
 
     mapped_size = mem->regions[i].memory_size + vum->regions[i].mmap_offset;
-    mapped_address = (uint64_t)(uintptr_t)mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
+    mapped_address = pointer_to_uword(mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0));
 
-    if ((void *)mapped_address == MAP_FAILED)
+    if (uword_to_pointer(mapped_address, void*) == MAP_FAILED)
     {
       clib_warning("mmap error");
       return 0;
@@ -535,13 +563,14 @@ dpdk_vhost_user_set_vring_num(u32 hw_if_index, u8 idx, u32 num)
 }
 
 static clib_error_t *
-dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, u64 used, u64 avail)
+dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, uword desc, \
+    uword used, uword avail, uword log)
 {
   dpdk_device_t * xd;
   struct vhost_virtqueue *vq;
 
-  DBG_SOCK("idx %u desc 0x%lx used 0x%lx avail 0x%lx",
-    idx, desc, used, avail);
+  DBG_SOCK("idx %u desc 0x%lx used 0x%lx avail 0x%lx log 0x%lx",
+    idx, desc, used, avail, log);
 
   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
     clib_warning("not a vhost-user interface");
@@ -552,11 +581,31 @@ dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, u64 used, u64
   vq->desc = (struct vring_desc *) qva_to_vva(&xd->vu_vhost_dev, desc);
   vq->used = (struct vring_used *) qva_to_vva(&xd->vu_vhost_dev, used);
   vq->avail = (struct vring_avail *) qva_to_vva(&xd->vu_vhost_dev, avail);
+#if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
+  vq->log_guest_addr = log;
+#endif
 
   if (!(vq->desc && vq->used && vq->avail)) {
     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
+   * incur performance degradation.
+   *
+   * The below function sets a flag in used table. Therefore,
+   * should be initialized after initializing vq->used.
+   */
+  rte_vhost_enable_guest_notification(&xd->vu_vhost_dev, idx, 0);
   stop_processing_packets(hw_if_index, idx);
 
   return 0;
@@ -592,6 +641,9 @@ dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
   vq->desc = NULL;
   vq->used = NULL;
   vq->avail = NULL;
+#if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
+  vq->log_guest_addr = 0;
+#endif
 
   /* Check if all Qs are disabled */
   int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
@@ -813,7 +865,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;
     }
 }
 
@@ -945,7 +997,7 @@ static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
       (cmsg->cmsg_type == SCM_RIGHTS) &&
       (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
         number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
-        memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
+        clib_memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
   }
 
   /* version 1, no reply bit set*/
@@ -1020,7 +1072,8 @@ static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
       dpdk_vhost_user_set_vring_addr(xd->vlib_hw_if_index, msg.state.index,
                                     msg.addr.desc_user_addr,
                                     msg.addr.used_user_addr,
-                                    msg.addr.avail_user_addr);
+                                    msg.addr.avail_user_addr,
+                                    msg.addr.log_guest_addr);
       break;
 
     case VHOST_USER_SET_OWNER:
@@ -1114,8 +1167,45 @@ static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
       break;
 
     case VHOST_USER_SET_LOG_BASE:
+#if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
       DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
         xd->vlib_hw_if_index);
+
+      if (msg.size != sizeof(msg.log)) {
+        DBG_SOCK("invalid msg size for VHOST_USER_SET_LOG_BASE: %u instead of %lu",
+                 msg.size, sizeof(msg.log));
+        goto close_socket;
+      }
+
+      if (!(xd->vu_vhost_dev.protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD))) {
+        DBG_SOCK("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
+        goto close_socket;
+      }
+
+      fd = fds[0];
+      /* align size to 2M page */
+      long page_sz = get_huge_page_size(fd);
+      ssize_t map_sz = (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
+
+      void *addr = mmap(0, map_sz, PROT_READ | PROT_WRITE,
+                                MAP_SHARED, fd, 0);
+
+      DBG_SOCK("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped %p",
+               map_sz, msg.log.offset, fd, addr);
+
+      if (addr == MAP_FAILED) {
+        clib_warning("failed to map memory. errno is %d", errno);
+        goto close_socket;
+      }
+
+      xd->vu_vhost_dev.log_base += pointer_to_uword(addr) + msg.log.offset;
+      xd->vu_vhost_dev.log_size = msg.log.size;
+      msg.flags |= VHOST_USER_REPLY_MASK;
+      msg.size = sizeof(msg.u64);
+#else
+      DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE Not-Implemented",
+        xd->vlib_hw_if_index);
+#endif
       break;
 
     case VHOST_USER_SET_LOG_FD:
@@ -1318,7 +1408,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);
   }
@@ -1364,7 +1454,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);
   }
@@ -1408,7 +1498,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);
   }
 
@@ -1450,7 +1540,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);
     }
 
@@ -1585,7 +1675,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);
   }
 
@@ -1620,6 +1710,7 @@ dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
                             renumber, custom_dev_instance, hw);
 
   vec_free(sock_filename);
+  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
   return 0;
 }
 
@@ -1639,7 +1730,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);
   }
 
@@ -1706,7 +1797,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);
   }
 
@@ -1732,7 +1823,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]);
@@ -1816,7 +1907,7 @@ show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
             vq->desc[j].len,
             vq->desc[j].flags,
             vq->desc[j].next,
-            (u64) map_guest_mem(xd, vq->desc[j].addr));}
+            pointer_to_uword(map_guest_mem(xd, vq->desc[j].addr)));}
       }
     }
     vlib_cli_output (vm, "\n");