vhost: add Tx zero copy support
cover vhost: add Tx zero copy support
This patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag, RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.
Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
For VM2VM iperf test case, it's even better: about 70% boost.
---
Yuanhan Liu (6):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for Tx
vhost: add Tx zero copy
vhost: add a flag to enable Tx zero copy examples/vhost: add an option to enable Tx zero copy
doc/guides/prog_guide/vhost_lib.rst | 7 +- examples/vhost/main.c | 19 ++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 103 +++++++++---- lib/librte_vhost/vhost_user.c | 297 +++++++++++++++++++++++------------- lib/librte_vhost/virtio_net.c | 188 +++++++++++++++++++---- 8 files changed, 472 insertions(+), 160 deletions(-)
Signature · 1 line
1.9.0
PATCH 1/6 vhost: simplify memory regions handling
Due to history reason (that vhost-cuse comes before vhost-user), some fields for maintaining the vhost-user memory mappings (such as mmapped address and size, with those we then can unmap on destroy) are kept in "orig_region_map" struct, a structure that is defined only in vhost-user source file.
The right way to go is to remove the structure and move all those fields into virtio_memory_region struct. But we simply can't do that before, because it breaks the ABI.
Now, thanks to the ABI refactoring, it's never been a blocking issue any more. And here it goes: this patch removes orig_region_map and redefines virtio_memory_region, to include all necessary info.
With that, we can simplify the guest/host address convert a bit.
--- lib/librte_vhost/vhost.h | 49 ++++++------ lib/librte_vhost/vhost_user.c | 172 +++++++++++++++++------------------------- 2 files changed, 90 insertions(+), 131 deletions(-)
lib/librte_vhost/vhost.h +22 -27
@@ -143,12 +143,14 @@ struct virtio_net {* Information relating to memory regions including offsets to * addresses in QEMUs memory file. */ -struct virtio_memory_regions { -uint64_t guest_phys_address; -uint64_t guest_phys_address_end; -uint64_t memory_size; -uint64_t userspace_address; -uint64_t address_offset; +struct virtio_memory_region { +uint64_t guest_phys_addr; +uint64_t guest_user_addr; +uint64_t host_user_addr; +uint64_t size; +void *mmap_addr; +uint64_t mmap_size; +int fd; };
@@ -156,12 +158,8 @@ struct virtio_memory_regions {* Memory structure includes region and mapping information. */ struct virtio_memory { -/* Base QEMU userspace address of the memory file. */ -uint64_t base_address; -uint64_t mapped_address; -uint64_t mapped_size;uint32_t nregions; -struct virtio_memory_regions regions[0 ]; +struct virtio_memory_region regions[0 ]; };
@@ -200,26 +198,23 @@ extern uint64_t VHOST_FEATURES;#define MAX_VHOST_DEVICE 1024extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE ]; -/** -* Function to convert guest physical addresses to vhost virtual addresses. -* This is used to convert guest virtio buffer addresses. -*/ +/* Convert guest physical Address to host virtual address */ static inline uint64_t __attribute__((always_inline)) -gpa_to_vva (struct virtio_net *dev,uint64_t guest_pa) +gpa_to_vva (struct virtio_net *dev,uint64_t gpa) { -struct virtio_memory_regions *region; -uint32_t regionidx; -uint64_t vhost_va =0 ; - -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((guest_pa >= region->guest_phys_address) && - (guest_pa <= region->guest_phys_address_end)) { - vhost_va = region->address_offset + guest_pa; -break ; +struct virtio_memory_region *reg; +uint32_t i; + +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (gpa >= reg->guest_phys_addr && + gpa < reg->guest_phys_addr + reg->size) { +return gpa - reg->guest_phys_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; }struct virtio_net_device_ops const *notify_ops;
lib/librte_vhost/vhost_user.c +68 -104
250 lines not on this page · raw message
Signature · 1 line
1.9.0
Re: [PATCH 1/6] vhost: simplify memory regions handling
Quoted 14 lines · Yuanhan Liu
Due to history reason (that vhost-cuse comes before vhost-user), some fields for maintaining the vhost-user memory mappings (such as mmapped address and size, with those we then can unmap on destroy) are kept in "
orig_region_map" struct, a structure that is defined only in vhost-user source file.The right way to go is to remove the structure and move all those fields into
virtio_memory_regionstruct. But we simply can't do that before, because it breaks the ABI.Now, thanks to the ABI refactoring, it's never been a blocking issue any more. And here it goes: this patch removes
orig_region_mapand redefinesvirtio_memory_region, to include all necessary info.
Quoted 8 lines
With that, we can simplify the guest/host address convert a bit.
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost.h | 49 ++++++------ lib/librte_vhost/vhost_user.c | 172 +++++++++++++++++------------------------- 2 files changed, 90 insertions(+), 131 deletions(-)
Thanks for explaining the history behind this.
FWIW, the change looks good to me:
Thanks,
Maxime
Re: [PATCH 1/6] vhost: simplify memory regions handling
In reply to Yuanhan LiuI want to apply the patch on the latest DPDK, see below commit ID but failed since no vhost.h and vhost-user.h files. So do you have any dependency on other patches?
commit 28d8abaf250c3fb4dcb6416518f4c54b4ae67205
Author: Deirdre O'Connor <[email protected]> Date: Mon Aug 22 17:20:08 2016 +0100
doc: fix patchwork link
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH 1/6] vhost: simplify memory regions handling
Due to history reason (that vhost-cuse comes before vhost-user), some fields for maintaining the vhost-user memory mappings (such as mmapped address and size, with those we then can unmap on destroy) are kept in "orig_region_map" struct, a structure that is defined only in vhost-user source file.
The right way to go is to remove the structure and move all those fields into virtio_memory_region struct. But we simply can't do that before, because it breaks the ABI.
Now, thanks to the ABI refactoring, it's never been a blocking issue any more. And here it goes: this patch removes orig_region_map and redefines virtio_memory_region, to include all necessary info.
With that, we can simplify the guest/host address convert a bit.
--- lib/librte_vhost/vhost.h | 49 ++++++------ lib/librte_vhost/vhost_user.c | 172 +++++++++++++++++------------------------- 2 files changed, 90 insertions(+), 131 deletions(-)
lib/librte_vhost/vhost.h +22 -27
@@ -143,12 +143,14 @@ struct virtio_net {* Information relating to memory regions including offsets to * addresses in QEMUs memory file. */ -struct virtio_memory_regions { -uint64_t guest_phys_address; -uint64_t guest_phys_address_end; -uint64_t memory_size; -uint64_t userspace_address; -uint64_t address_offset; +struct virtio_memory_region { +uint64_t guest_phys_addr; +uint64_t guest_user_addr; +uint64_t host_user_addr; +uint64_t size; +void *mmap_addr; +uint64_t mmap_size; +int fd; };
@@ -156,12 +158,8 @@ struct virtio_memory_regions {* Memory structure includes region and mapping information. */ struct virtio_memory { -/* Base QEMU userspace address of the memory file. */ -uint64_t base_address; -uint64_t mapped_address; -uint64_t mapped_size;uint32_t nregions; -struct virtio_memory_regions regions[0 ]; +struct virtio_memory_region regions[0 ]; };
@@ -200,26 +198,23 @@ extern uint64_t VHOST_FEATURES;#define MAX_VHOST_DEVICE 1024extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE ]; -/** -* Function to convert guest physical addresses to vhost virtual addresses. -* This is used to convert guest virtio buffer addresses. -*/ +/* Convert guest physical Address to host virtual address */ static inline uint64_t __attribute__((always_inline)) -gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa) +gpa_to_vva(struct virtio_net *dev, uint64_t gpa) { -struct virtio_memory_regions *region; -uint32_t regionidx; -uint64_t vhost_va =0 ; - -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((guest_pa >= region->guest_phys_address) && - (guest_pa <= region->guest_phys_address_end)) { - vhost_va = region->address_offset + guest_pa; -break ; +struct virtio_memory_region *reg; +uint32_t i; + +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (gpa >= reg->guest_phys_addr && + gpa < reg->guest_phys_addr + reg->size) { +return gpa - reg->guest_phys_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; } struct virtio_net_device_ops const *notify_ops; diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c index eee99e9..d2071fd 100644 --- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -74,18 +74,6 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP", };
-struct orig_region_map {
- int fd;
- uint64_t mapped_address;
- uint64_t mapped_size;
- uint64_t blksz;
-};
-
-#define orig_region(ptr, nregions) \
- ((struct orig_region_map *)RTE_PTR_ADD((ptr), \
- sizeof(struct virtio_memory) + \
- sizeof(struct virtio_memory_regions) * (nregions)))
-
static uint64_t
get_blk_size(int fd)
{
@@ -99,18 +87,17 @@ get_blk_size(int fd) static void free_mem_region(struct virtio_net *dev) {
- struct orig_region_map *region;
- unsigned int idx;
+ uint32_t i;
+ struct virtio_memory_region *reg;
if (!dev || !dev->mem)
return;
- region = orig_region(dev->mem, dev->mem->nregions);
- for (idx = 0; idx < dev->mem->nregions; idx++) {
- if (region[idx].mapped_address) {
- munmap((void *)(uintptr_t)region[idx].mapped_address,
- region[idx].mapped_size);
- close(region[idx].fd);
+ for (i = 0; i < dev->mem->nregions; i++) {
+ reg = &dev->mem->regions[i];
+ if (reg->host_user_addr) {
+ munmap(reg->mmap_addr, reg->mmap_size);
+ close(reg->fd);
}
}
}
@@ -120,7 +107,7 @@ vhost_backend_cleanup(struct virtio_net *dev) {
if (dev->mem) {
free_mem_region(dev);
- free(dev->mem);
+ rte_free(dev->mem);
dev->mem = NULL;
}
if (dev->log_addr) {
@@ -286,25 +273,23 @@ numa_realloc(struct virtio_net *dev, int index __rte_unused)
* used to convert the ring addresses to our address space.
*/
static uint64_t
-qva_to_vva(struct virtio_net *dev, uint64_t qemu_va) +qva_to_vva(struct virtio_net *dev, uint64_t qva)
{
- struct virtio_memory_regions *region;
- uint64_t vhost_va = 0;
- uint32_t regionidx = 0;
+ struct virtio_memory_region *reg;
+ uint32_t i;
/* Find the region where the address lives. */
- for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
- region = &dev->mem->regions[regionidx];
- if ((qemu_va >= region->userspace_address) &&
- (qemu_va <= region->userspace_address +
- region->memory_size)) {
- vhost_va = qemu_va + region->guest_phys_address +
- region->address_offset -
- region->userspace_address;
- break;
+ for (i = 0; i < dev->mem->nregions; i++) {
+ reg = &dev->mem->regions[i];
+
+ if (qva >= reg->guest_user_addr &&
+ qva < reg->guest_user_addr + reg->size) {
+ return qva - reg->guest_user_addr +
+ reg->host_user_addr;
}
}
- return vhost_va;
+
+ return 0;
}
/*
@@ -391,11 +376,13 @@ static int
vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) {
struct VhostUserMemory memory = pmsg->payload.memory;
- struct virtio_memory_regions *pregion;
- uint64_t mapped_address, mapped_size;
- unsigned int idx = 0;
- struct orig_region_map *pregion_orig;
+ struct virtio_memory_region *reg;
+ void *mmap_addr;
+ uint64_t mmap_size;
+ uint64_t mmap_offset;
uint64_t alignment;
+ uint32_t i;
+ int fd;
/* Remove from the data plane. */
if (dev->flags & VIRTIO_DEV_RUNNING) { @@ -405,14 +392,12 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
if (dev->mem) {
free_mem_region(dev);
- free(dev->mem);
+ rte_free(dev->mem);
dev->mem = NULL;
}
- dev->mem = calloc(1,
- sizeof(struct virtio_memory) +
- sizeof(struct virtio_memory_regions) * memory.nregions +
- sizeof(struct orig_region_map) * memory.nregions);
+ dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct virtio_memory) +
+ sizeof(struct virtio_memory_region) * memory.nregions, 0);
if (dev->mem == NULL) {
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) failed to allocate memory for dev->mem\n", @@ -421,22 +406,17 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
}
dev->mem->nregions = memory.nregions;
- pregion_orig = orig_region(dev->mem, memory.nregions);
- for (idx = 0; idx < memory.nregions; idx++) {
- pregion = &dev->mem->regions[idx];
- pregion->guest_phys_address =
- memory.regions[idx].guest_phys_addr;
- pregion->guest_phys_address_end =
- memory.regions[idx].guest_phys_addr +
- memory.regions[idx].memory_size;
- pregion->memory_size =
- memory.regions[idx].memory_size;
- pregion->userspace_address =
- memory.regions[idx].userspace_addr;
-
- /* This is ugly */
- mapped_size = memory.regions[idx].memory_size +
- memory.regions[idx].mmap_offset;
+ for (i = 0; i < memory.nregions; i++) {
+ fd = pmsg->fds[i];
+ reg = &dev->mem->regions[i];
+
+ reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
+ reg->guest_user_addr = memory.regions[i].userspace_addr;
+ reg->size = memory.regions[i].memory_size;
+ reg->fd = fd;
+
+ mmap_offset = memory.regions[i].mmap_offset;
+ mmap_size = reg->size + mmap_offset;
/* mmap() without flag of MAP_ANONYMOUS, should be called
* with length argument aligned with hugepagesz at older @@ -446,67 +426,51 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
* to avoid failure, make sure in caller to keep length
* aligned.
*/
- alignment = get_blk_size(pmsg->fds[idx]);
+ alignment = get_blk_size(fd);
if (alignment == (uint64_t)-1) {
RTE_LOG(ERR, VHOST_CONFIG,
"couldn't get hugepage size through fstat\n");
goto err_mmap;
}
- mapped_size = RTE_ALIGN_CEIL(mapped_size, alignment);
+ mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
- mapped_address = (uint64_t)(uintptr_t)mmap(NULL,
- mapped_size,
- PROT_READ | PROT_WRITE, MAP_SHARED,
- pmsg->fds[idx],
- 0);
+ mmap_addr = mmap(NULL, mmap_size,
+ PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- RTE_LOG(INFO, VHOST_CONFIG,
- "mapped region %d fd:%d to:%p sz:0x%"PRIx64" "
- "off:0x%"PRIx64" align:0x%"PRIx64"\n",
- idx, pmsg->fds[idx], (void *)(uintptr_t)mapped_address,
- mapped_size, memory.regions[idx].mmap_offset,
- alignment);
-
- if (mapped_address == (uint64_t)(uintptr_t)MAP_FAILED) {
+ if (mmap_addr == MAP_FAILED) {
RTE_LOG(ERR, VHOST_CONFIG,
- "mmap qemu guest failed.\n");
+ "mmap region %u failed.\n", i);
goto err_mmap;
}
- pregion_orig[idx].mapped_address = mapped_address;
- pregion_orig[idx].mapped_size = mapped_size;
- pregion_orig[idx].blksz = alignment;
- pregion_orig[idx].fd = pmsg->fds[idx];
-
- mapped_address += memory.regions[idx].mmap_offset;
+ reg->mmap_addr = mmap_addr;
+ reg->mmap_size = mmap_size;
+ reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
- pregion->address_offset = mapped_address -
- pregion->guest_phys_address;
-
- if (memory.regions[idx].guest_phys_addr == 0) {
- dev->mem->base_address =
- memory.regions[idx].userspace_addr;
- dev->mem->mapped_address =
- pregion->address_offset;
- }
-
- LOG_DEBUG(VHOST_CONFIG,
- "REGION: %u GPA: %p QEMU VA: %p SIZE (%"PRIu64")\n",
- idx,
- (void *)(uintptr_t)pregion->guest_phys_address,
- (void *)(uintptr_t)pregion->userspace_address,
- pregion->memory_size);
+ RTE_LOG(INFO, VHOST_CONFIG,
+ "guest memory region %u, size: 0x%" PRIx64 "\n"
+ "\t guest physical addr: 0x%" PRIx64 "\n"
+ "\t guest virtual addr: 0x%" PRIx64 "\n"
+ "\t host virtual addr: 0x%" PRIx64 "\n"
+ "\t mmap addr : 0x%" PRIx64 "\n"
+ "\t mmap size : 0x%" PRIx64 "\n"
+ "\t mmap align: 0x%" PRIx64 "\n"
+ "\t mmap off : 0x%" PRIx64 "\n",
+ i, reg->size,
+ reg->guest_phys_addr,
+ reg->guest_user_addr,
+ reg->host_user_addr,
+ (uint64_t)(uintptr_t)mmap_addr,
+ mmap_size,
+ alignment,
+ mmap_offset);
}
return 0;
err_mmap:
- while (idx--) {
- munmap((void *)(uintptr_t)pregion_orig[idx].mapped_address,
- pregion_orig[idx].mapped_size);
- close(pregion_orig[idx].fd);
- }
- free(dev->mem);
+ free_mem_region(dev);
+ rte_free(dev->mem);
dev->mem = NULL;
return -1;
}
--
1.9.0
Re: [PATCH 1/6] vhost: simplify memory regions handling
Yes, it depends on the vhost-cuse removal patchset I sent last week.
--yliu
Quoted 366 lines · Qian Q
I want to apply the patch on the latest DPDK, see below commit ID but failed since no
vhost.handvhost-user.hfiles. So do you have any dependency on other patches?commit
28d8abaf250c3fb4dcb6416518f4c54b4ae67205Author: Deirdre O'Connor <[email protected]> Date: Mon Aug 22 17:20:08 2016 +0100doc: fix patchwork link
Fixes 58abf6e77c6b ("doc: add contributors guide")Reported-by Jon Loeliger <[email protected]>Signed-off-by Deirdre O'Connor <[email protected]>Acked-by John McNamara <[email protected]>-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH 1/6] vhost: simplify memory regions handlingDue to history reason (that vhost-cuse comes before vhost-user), some fields for maintaining the vhost-user memory mappings (such as mmapped address and size, with those we then can unmap on destroy) are kept in "
orig_region_map" struct, a structure that is defined only in vhost-user source file.The right way to go is to remove the structure and move all those fields into
virtio_memory_regionstruct. But we simply can't do that before, because it breaks the ABI.Now, thanks to the ABI refactoring, it's never been a blocking issue any more. And here it goes: this patch removes
orig_region_mapand redefinesvirtio_memory_region, to include all necessary info.With that, we can simplify the guest/host address convert a bit.
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost.h | 49 ++++++------ lib/librte_vhost/vhost_user.c | 172 +++++++++++++++++------------------------- 2 files changed, 90 insertions(+), 131 deletions(-)lib/librte_vhost/vhost.h +22
-26@@ -143,12 +143,14 @@ struct virtio_net {* Information relating to memory regions including offsets to * addresses in QEMUs memory file. */ -struct virtio_memory_regions { -uint64_t guest_phys_address; -uint64_t guest_phys_address_end; -uint64_t memory_size; -uint64_t userspace_address; -uint64_t address_offset; +struct virtio_memory_region { +uint64_t guest_phys_addr; +uint64_t guest_user_addr; +uint64_t host_user_addr; +uint64_t size; +void *mmap_addr; +uint64_t mmap_size; +int fd; };@@ -156,12 +158,8 @@ struct virtio_memory_regions {* Memory structure includes region and mapping information. */ struct virtio_memory { -/* Base QEMU userspace address of the memory file. */ -uint64_t base_address; -uint64_t mapped_address; -uint64_t mapped_size;uint32_t nregions; -struct virtio_memory_regions regions[0 ]; +struct virtio_memory_region regions[0 ]; };@@ -200,26 +198,23 @@ extern uint64_t VHOST_FEATURES;#define MAX_VHOST_DEVICE 1024extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE ]; -/** -* Function to convert guest physical addresses to vhost virtual addresses. -* This is used to convert guest virtio buffer addresses. -*/ +/* Convert guest physical Address to host virtual address */ static inline uint64_t __attribute__((always_inline)) -gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa) +gpa_to_vva(struct virtio_net *dev, uint64_t gpa) { -struct virtio_memory_regions *region; -uint32_t regionidx; -uint64_t vhost_va =0 ; - -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((guest_pa >= region->guest_phys_address) && - (guest_pa <= region->guest_phys_address_end)) { - vhost_va = region->address_offset + guest_pa; -break ; +struct virtio_memory_region *reg; +uint32_t i; + +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (gpa >= reg->guest_phys_addr && + gpa < reg->guest_phys_addr + reg->size) { +return gpa - reg->guest_phys_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; } struct virtio_net_device_ops const *notify_ops; diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c index eee99e9..d2071fd 100644lib/librte_vhost/vhost_user.c +26
-39@@ -74,18 +74,6 @@ static const char *vhost_message_str[VHOST_USER_MAX] = { [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP", }; -struct orig_region_map { -int fd; -uint64_t mapped_address; -uint64_t mapped_size; -uint64_t blksz; -}; - -#define orig_region (ptr, nregions) \ - ((struct orig_region_map *)RTE_PTR_ADD((ptr), \ - sizeof(struct virtio_memory) + \ - sizeof(struct virtio_memory_regions) * (nregions))) -static uint64_t get_blk_size (int fd) {@@ -99,18 +87,17 @@ get_blk_size(int fd) static void free_mem_region(struct virtio_net *dev) { -struct orig_region_map *region; -unsigned int idx; +uint32_t i; +struct virtio_memory_region *reg;if (!dev || !dev->mem)return ; - region =orig_region (dev->mem, dev->mem->nregions); -for (idx =0 ; idx < dev->mem->nregions; idx++) { -if (region[idx].mapped_address) { -munmap ((void *)(uintptr_t )region[idx].mapped_address, - region[idx].mapped_size); -close (region[idx].fd); +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (reg->host_user_addr) { +munmap (reg->mmap_addr, reg->mmap_size); +close (reg->fd); } } }@@ -120,7 +107,7 @@ vhost_backend_cleanup(struct virtio_net *dev) {if (dev->mem) {free_mem_region (dev); -free (dev->mem); +rte_free (dev->mem); dev->mem =NULL ; }if (dev->log_addr) {@@ -286,25 +273,23 @@ numa_realloc(struct virtio_net *dev, int index __rte_unused)* used to convert the ring addresses to our address space. */ static uint64_t -qva_to_vva (struct virtio_net *dev,uint64_t qemu_va) +qva_to_vva (struct virtio_net *dev,uint64_t qva) { -struct virtio_memory_regions *region; -uint64_t vhost_va =0 ; -uint32_t regionidx =0 ; +struct virtio_memory_region *reg; +uint32_t i;/* Find the region where the address lives. */ -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((qemu_va >= region->userspace_address) && - (qemu_va <= region->userspace_address + - region->memory_size)) { - vhost_va = qemu_va + region->guest_phys_address + - region->address_offset - - region->userspace_address; -break ; +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; + +if (qva >= reg->guest_user_addr && + qva < reg->guest_user_addr + reg->size) { +return qva - reg->guest_user_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; }/* @@ -391,11 +376,13 @@ static int vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) { struct VhostUserMemory memory = pmsg->payload.memory; -struct virtio_memory_regions *pregion; -uint64_t mapped_address, mapped_size; -unsigned int idx =0 ; -struct orig_region_map *pregion_orig; +struct virtio_memory_region *reg; +void *mmap_addr; +uint64_t mmap_size; +uint64_t mmap_offset;uint64_t alignment; +uint32_t i; +int fd;/* Remove from the data plane. */ if (dev->flags & VIRTIO_DEV_RUNNING) { @@ -405,14 +392,12 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)lib/librte_vhost/vhost_user.c +42
-66if (dev->mem) {free_mem_region (dev); -free (dev->mem); +rte_free (dev->mem); dev->mem =NULL ; } - dev->mem =calloc (1 , -sizeof (struct virtio_memory ) + -sizeof (struct virtio_memory_regions ) * memory.nregions + -sizeof (struct orig_region_map ) * memory.nregions); + dev->mem =rte_zmalloc ("vhost-mem-table" ,sizeof (struct virtio_memory ) + +sizeof (struct virtio_memory_region ) * memory.nregions,0 );if (dev->mem ==NULL ) {RTE_LOG (ERR ,VHOST_CONFIG , "(%d) failed to allocate memory for dev->mem\n", @@ -421,22 +406,17 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) } dev->mem->nregions = memory.nregions; - pregion_orig = orig_region(dev->mem, memory.nregions); -for (idx =0 ; idx < memory.nregions; idx++) { - pregion = &dev->mem->regions[idx]; - pregion->guest_phys_address = - memory.regions[idx].guest_phys_addr; - pregion->guest_phys_address_end = - memory.regions[idx].guest_phys_addr + - memory.regions[idx].memory_size; - pregion->memory_size = - memory.regions[idx].memory_size; - pregion->userspace_address = - memory.regions[idx].userspace_addr; - -/* This is ugly */ - mapped_size = memory.regions[idx].memory_size + - memory.regions[idx].mmap_offset; +for (i =0 ; i < memory.nregions; i++) { + fd = pmsg->fds[i]; + reg = &dev->mem->regions[i]; + + reg->guest_phys_addr = memory.regions[i].guest_phys_addr; + reg->guest_user_addr = memory.regions[i].userspace_addr; + reg->size = memory.regions[i].memory_size; + reg->fd = fd; + + mmap_offset = memory.regions[i].mmap_offset; + mmap_size = reg->size + mmap_offset;/* mmap() without flag of MAP_ANONYMOUS, should be called * with length argument aligned with hugepagesz at older @@ -446,67 +426,51 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) * to avoid failure, make sure in caller to keep length * aligned. */ - alignment =get_blk_size (pmsg->fds[idx]); + alignment =get_blk_size (fd);if (alignment == (uint64_t )-1 ) {RTE_LOG (ERR ,VHOST_CONFIG ,"couldn't get hugepage size through fstat\n" ); goto err_mmap; } - mapped_size =RTE_ALIGN_CEIL (mapped_size, alignment); + mmap_size =RTE_ALIGN_CEIL (mmap_size, alignment); - mapped_address = (uint64_t )(uintptr_t )mmap (NULL , - mapped_size, -PROT_READ |PROT_WRITE ,MAP_SHARED , - pmsg->fds[idx], -0 ); + mmap_addr =mmap (NULL , mmap_size, +PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); -RTE_LOG (INFO ,VHOST_CONFIG , -"mapped region %d fd:%d to:%p sz:0x%" PRIx64" " -"off:0x%" PRIx64" align:0x%" PRIx64"\n" , - idx, pmsg->fds[idx], (void *)(uintptr_t )mapped_address, - mapped_size, memory.regions[idx].mmap_offset, - alignment); - -if (mapped_address == (uint64_t )(uintptr_t )MAP_FAILED ) { +if (mmap_addr ==MAP_FAILED ) {RTE_LOG (ERR ,VHOST_CONFIG , -"mmap qemu guest failed.\n" ); +"mmap region %u failed.\n" , i); goto err_mmap; } - pregion_orig[idx].mapped_address = mapped_address; - pregion_orig[idx].mapped_size = mapped_size; - pregion_orig[idx].blksz = alignment; - pregion_orig[idx].fd = pmsg->fds[idx]; - - mapped_address += memory.regions[idx].mmap_offset; + reg->mmap_addr = mmap_addr; + reg->mmap_size = mmap_size; + reg->host_user_addr = (uint64_t )(uintptr_t )mmap_addr + mmap_offset; - pregion->address_offset = mapped_address - - pregion->guest_phys_address; - -if (memory.regions[idx].guest_phys_addr ==0 ) { - dev->mem->base_address = - memory.regions[idx].userspace_addr; - dev->mem->mapped_address = - pregion->address_offset; - } - -LOG_DEBUG (VHOST_CONFIG , -"REGION: %u GPA: %p QEMU VA: %p SIZE (%" PRIu64")\n" , - idx, - (void *)(uintptr_t )pregion->guest_phys_address, - (void *)(uintptr_t )pregion->userspace_address, - pregion->memory_size); +RTE_LOG (INFO ,VHOST_CONFIG , +"guest memory region %u, size: 0x%" PRIx64"\n" +"\t guest physical addr: 0x%" PRIx64"\n" +"\t guest virtual addr: 0x%" PRIx64"\n" +"\t host virtual addr: 0x%" PRIx64"\n" +"\t mmap addr : 0x%" PRIx64"\n" +"\t mmap size : 0x%" PRIx64"\n" +"\t mmap align: 0x%" PRIx64"\n" +"\t mmap off : 0x%" PRIx64"\n" , + i, reg->size, + reg->guest_phys_addr, + reg->guest_user_addr, + reg->host_user_addr, + (uint64_t )(uintptr_t )mmap_addr, + mmap_size, + alignment, + mmap_offset); }return 0 ; err_mmap: -while (idx--) { -munmap ((void *)(uintptr_t )pregion_orig[idx].mapped_address, - pregion_orig[idx].mapped_size); -close (pregion_orig[idx].fd); - } -free (dev->mem); +free_mem_region (dev); +rte_free (dev->mem); dev->mem =NULL ;return -1 ; } --1.9.0
Re: [PATCH 1/6] vhost: simplify memory regions handling
OK, it's better to claim that his patchset have the dependency on another one.
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Wednesday, August 24, 2016 3:40 PM
To: Xu, Qian Q <[email protected]>
Cc: [email protected]; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH 1/6] vhost: simplify memory regions handling
Yes, it depends on the vhost-cuse removal patchset I sent last week.
--yliu
Quoted 366 lines · Qian Q
I want to apply the patch on the latest DPDK, see below commit ID but failed since no
vhost.handvhost-user.hfiles. So do you have any dependency on other patches?commit
28d8abaf250c3fb4dcb6416518f4c54b4ae67205Author: Deirdre O'Connor <[email protected]> Date: Mon Aug 22 17:20:08 2016 +0100doc: fix patchwork link
Fixes 58abf6e77c6b ("doc: add contributors guide")Reported-by Jon Loeliger <[email protected]>Signed-off-by Deirdre O'Connor <[email protected]>Acked-by John McNamara <[email protected]>-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH 1/6] vhost: simplify memory regions handlingDue to history reason (that vhost-cuse comes before vhost-user), some fields for maintaining the vhost-user memory mappings (such as mmapped address and size, with those we then can unmap on destroy) are kept in "
orig_region_map" struct, a structure that is defined only in vhost-user source file.The right way to go is to remove the structure and move all those fields into
virtio_memory_regionstruct. But we simply can't do that before, because it breaks the ABI.Now, thanks to the ABI refactoring, it's never been a blocking issue any more. And here it goes: this patch removes
orig_region_mapand redefinesvirtio_memory_region, to include all necessary info.With that, we can simplify the guest/host address convert a bit.
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost.h | 49 ++++++------ lib/librte_vhost/vhost_user.c | 172 +++++++++++++++++------------------------- 2 files changed, 90 insertions(+), 131 deletions(-)lib/librte_vhost/vhost.h +22
-26@@ -143,12 +143,14 @@ struct virtio_net {* Information relating to memory regions including offsets to * addresses in QEMUs memory file. */ -struct virtio_memory_regions { -uint64_t guest_phys_address; -uint64_t guest_phys_address_end; -uint64_t memory_size; -uint64_t userspace_address; -uint64_t address_offset; +struct virtio_memory_region { +uint64_t guest_phys_addr; +uint64_t guest_user_addr; +uint64_t host_user_addr; +uint64_t size; +void *mmap_addr; +uint64_t mmap_size; +int fd; };@@ -156,12 +158,8 @@ struct virtio_memory_regions {* Memory structure includes region and mapping information. */ struct virtio_memory { -/* Base QEMU userspace address of the memory file. */ -uint64_t base_address; -uint64_t mapped_address; -uint64_t mapped_size;uint32_t nregions; -struct virtio_memory_regions regions[0 ]; +struct virtio_memory_region regions[0 ]; };@@ -200,26 +198,23 @@ extern uint64_t VHOST_FEATURES;#define MAX_VHOST_DEVICE 1024extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE ]; -/** -* Function to convert guest physical addresses to vhost virtual addresses. -* This is used to convert guest virtio buffer addresses. -*/ +/* Convert guest physical Address to host virtual address */ static inline uint64_t __attribute__((always_inline)) -gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa) +gpa_to_vva(struct virtio_net *dev, uint64_t gpa) { -struct virtio_memory_regions *region; -uint32_t regionidx; -uint64_t vhost_va =0 ; - -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((guest_pa >= region->guest_phys_address) && - (guest_pa <= region->guest_phys_address_end)) { - vhost_va = region->address_offset + guest_pa; -break ; +struct virtio_memory_region *reg; +uint32_t i; + +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (gpa >= reg->guest_phys_addr && + gpa < reg->guest_phys_addr + reg->size) { +return gpa - reg->guest_phys_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; } struct virtio_net_device_ops const *notify_ops; diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c index eee99e9..d2071fd 100644lib/librte_vhost/vhost_user.c +26
-39@@ -74,18 +74,6 @@ static const char *vhost_message_str[VHOST_USER_MAX] = { [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP", }; -struct orig_region_map { -int fd; -uint64_t mapped_address; -uint64_t mapped_size; -uint64_t blksz; -}; - -#define orig_region (ptr, nregions) \ - ((struct orig_region_map *)RTE_PTR_ADD((ptr), \ - sizeof(struct virtio_memory) + \ - sizeof(struct virtio_memory_regions) * (nregions))) -static uint64_t get_blk_size (int fd) {@@ -99,18 +87,17 @@ get_blk_size(int fd) static void free_mem_region(struct virtio_net *dev) { -struct orig_region_map *region; -unsigned int idx; +uint32_t i; +struct virtio_memory_region *reg;if (!dev || !dev->mem)return ; - region =orig_region (dev->mem, dev->mem->nregions); -for (idx =0 ; idx < dev->mem->nregions; idx++) { -if (region[idx].mapped_address) { -munmap ((void *)(uintptr_t )region[idx].mapped_address, - region[idx].mapped_size); -close (region[idx].fd); +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (reg->host_user_addr) { +munmap (reg->mmap_addr, reg->mmap_size); +close (reg->fd); } } }@@ -120,7 +107,7 @@ vhost_backend_cleanup(struct virtio_net *dev) {if (dev->mem) {free_mem_region (dev); -free (dev->mem); +rte_free (dev->mem); dev->mem =NULL ; }if (dev->log_addr) {@@ -286,25 +273,23 @@ numa_realloc(struct virtio_net *dev, int index __rte_unused)* used to convert the ring addresses to our address space. */ static uint64_t -qva_to_vva (struct virtio_net *dev,uint64_t qemu_va) +qva_to_vva (struct virtio_net *dev,uint64_t qva) { -struct virtio_memory_regions *region; -uint64_t vhost_va =0 ; -uint32_t regionidx =0 ; +struct virtio_memory_region *reg; +uint32_t i;/* Find the region where the address lives. */ -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((qemu_va >= region->userspace_address) && - (qemu_va <= region->userspace_address + - region->memory_size)) { - vhost_va = qemu_va + region->guest_phys_address + - region->address_offset - - region->userspace_address; -break ; +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; + +if (qva >= reg->guest_user_addr && + qva < reg->guest_user_addr + reg->size) { +return qva - reg->guest_user_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; }/* @@ -391,11 +376,13 @@ static int vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) { struct VhostUserMemory memory = pmsg->payload.memory; -struct virtio_memory_regions *pregion; -uint64_t mapped_address, mapped_size; -unsigned int idx =0 ; -struct orig_region_map *pregion_orig; +struct virtio_memory_region *reg; +void *mmap_addr; +uint64_t mmap_size; +uint64_t mmap_offset;uint64_t alignment; +uint32_t i; +int fd;/* Remove from the data plane. */ if (dev->flags & VIRTIO_DEV_RUNNING) { @@ -405,14 +392,12 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)lib/librte_vhost/vhost_user.c +42
-66if (dev->mem) {free_mem_region (dev); -free (dev->mem); +rte_free (dev->mem); dev->mem =NULL ; } - dev->mem =calloc (1 , -sizeof (struct virtio_memory ) + -sizeof (struct virtio_memory_regions ) * memory.nregions + -sizeof (struct orig_region_map ) * memory.nregions); + dev->mem =rte_zmalloc ("vhost-mem-table" ,sizeof (struct virtio_memory ) + +sizeof (struct virtio_memory_region ) * memory.nregions,0 );if (dev->mem ==NULL ) {RTE_LOG (ERR ,VHOST_CONFIG , "(%d) failed to allocate memory for dev->mem\n", @@ -421,22 +406,17 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) } dev->mem->nregions = memory.nregions; - pregion_orig = orig_region(dev->mem, memory.nregions); -for (idx =0 ; idx < memory.nregions; idx++) { - pregion = &dev->mem->regions[idx]; - pregion->guest_phys_address = - memory.regions[idx].guest_phys_addr; - pregion->guest_phys_address_end = - memory.regions[idx].guest_phys_addr + - memory.regions[idx].memory_size; - pregion->memory_size = - memory.regions[idx].memory_size; - pregion->userspace_address = - memory.regions[idx].userspace_addr; - -/* This is ugly */ - mapped_size = memory.regions[idx].memory_size + - memory.regions[idx].mmap_offset; +for (i =0 ; i < memory.nregions; i++) { + fd = pmsg->fds[i]; + reg = &dev->mem->regions[i]; + + reg->guest_phys_addr = memory.regions[i].guest_phys_addr; + reg->guest_user_addr = memory.regions[i].userspace_addr; + reg->size = memory.regions[i].memory_size; + reg->fd = fd; + + mmap_offset = memory.regions[i].mmap_offset; + mmap_size = reg->size + mmap_offset;/* mmap() without flag of MAP_ANONYMOUS, should be called * with length argument aligned with hugepagesz at older @@ -446,67 +426,51 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) * to avoid failure, make sure in caller to keep length * aligned. */ - alignment =get_blk_size (pmsg->fds[idx]); + alignment =get_blk_size (fd);if (alignment == (uint64_t )-1 ) {RTE_LOG (ERR ,VHOST_CONFIG ,"couldn't get hugepage size through fstat\n" ); goto err_mmap; } - mapped_size =RTE_ALIGN_CEIL (mapped_size, alignment); + mmap_size =RTE_ALIGN_CEIL (mmap_size, alignment); - mapped_address = (uint64_t )(uintptr_t )mmap (NULL , - mapped_size, -PROT_READ |PROT_WRITE ,MAP_SHARED , - pmsg->fds[idx], -0 ); + mmap_addr =mmap (NULL , mmap_size, +PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); -RTE_LOG (INFO ,VHOST_CONFIG , -"mapped region %d fd:%d to:%p sz:0x%" PRIx64" " -"off:0x%" PRIx64" align:0x%" PRIx64"\n" , - idx, pmsg->fds[idx], (void *)(uintptr_t )mapped_address, - mapped_size, memory.regions[idx].mmap_offset, - alignment); - -if (mapped_address == (uint64_t )(uintptr_t )MAP_FAILED ) { +if (mmap_addr ==MAP_FAILED ) {RTE_LOG (ERR ,VHOST_CONFIG , -"mmap qemu guest failed.\n" ); +"mmap region %u failed.\n" , i); goto err_mmap; } - pregion_orig[idx].mapped_address = mapped_address; - pregion_orig[idx].mapped_size = mapped_size; - pregion_orig[idx].blksz = alignment; - pregion_orig[idx].fd = pmsg->fds[idx]; - - mapped_address += memory.regions[idx].mmap_offset; + reg->mmap_addr = mmap_addr; + reg->mmap_size = mmap_size; + reg->host_user_addr = (uint64_t )(uintptr_t )mmap_addr + mmap_offset; - pregion->address_offset = mapped_address - - pregion->guest_phys_address; - -if (memory.regions[idx].guest_phys_addr ==0 ) { - dev->mem->base_address = - memory.regions[idx].userspace_addr; - dev->mem->mapped_address = - pregion->address_offset; - } - -LOG_DEBUG (VHOST_CONFIG , -"REGION: %u GPA: %p QEMU VA: %p SIZE (%" PRIu64")\n" , - idx, - (void *)(uintptr_t )pregion->guest_phys_address, - (void *)(uintptr_t )pregion->userspace_address, - pregion->memory_size); +RTE_LOG (INFO ,VHOST_CONFIG , +"guest memory region %u, size: 0x%" PRIx64"\n" +"\t guest physical addr: 0x%" PRIx64"\n" +"\t guest virtual addr: 0x%" PRIx64"\n" +"\t host virtual addr: 0x%" PRIx64"\n" +"\t mmap addr : 0x%" PRIx64"\n" +"\t mmap size : 0x%" PRIx64"\n" +"\t mmap align: 0x%" PRIx64"\n" +"\t mmap off : 0x%" PRIx64"\n" , + i, reg->size, + reg->guest_phys_addr, + reg->guest_user_addr, + reg->host_user_addr, + (uint64_t )(uintptr_t )mmap_addr, + mmap_size, + alignment, + mmap_offset); }return 0 ; err_mmap: -while (idx--) { -munmap ((void *)(uintptr_t )pregion_orig[idx].mapped_address, - pregion_orig[idx].mapped_size); -close (pregion_orig[idx].fd); - } -free (dev->mem); +free_mem_region (dev); +rte_free (dev->mem); dev->mem =NULL ;return -1 ; } --1.9.0
PATCH 2/6 vhost: get guest/host physical address mappings
In reply to Yuanhan LiuSo that we can convert a guest physical address to host physical address, which will be used in later Tx zero copy implementation.
--- lib/librte_vhost/vhost.h | 30 +++++++++++++++ lib/librte_vhost/vhost_user.c | 86 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+)
lib/librte_vhost/vhost.h +30 -0
@@ -114,6 +114,12 @@ struct vhost_virtqueue {#define VIRTIO_F_VERSION_1 32#endif +struct guest_page { +uint64_t guest_phys_addr; +uint64_t host_phys_addr; +uint64_t size; +}; +/** * Device structure contains all configuration information relating * to the device.
@@ -137,6 +143,10 @@ struct virtio_net {uint64_t log_addr;struct ether_addr mac; +uint32_t nr_guest_pages; +uint32_t max_guest_pages; +struct guest_page *guest_pages; + } __rte_cache_aligned;/**
@@ -217,6 +227,26 @@ gpa_to_vva(struct virtio_net *dev, uint64_t gpa)return 0 ; } +/* Convert guest physical address to host physical address */ +static inline phys_addr_t __attribute__((always_inline)) +gpa_to_hpa (struct virtio_net *dev,uint64_t gpa,uint64_t size) +{ +uint32_t i; +struct guest_page *page; + +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + +if (gpa >= page->guest_phys_addr && + gpa + size < page->guest_phys_addr + page->size) { +return gpa - page->guest_phys_addr + + page->host_phys_addr; + } + } + +return 0 ; +} +struct virtio_net_device_ops const *notify_ops;struct virtio_net *get_device (int vid);
lib/librte_vhost/vhost_user.c +86 -0
110 lines not on this page · raw message
Signature · 1 line
1.9.0
Re: [PATCH 2/6] vhost: get guest/host physical address mappings
Quoted 43 lines · Yuanhan Liu
So that we can convert a guest physical address to host physical address, which will be used in later Tx zero copy implementation.
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost.h | 30 +++++++++++++++ lib/librte_vhost/vhost_user.c | 86 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+)lib/librte_vhost/vhost.h +12
-0@@ -114,6 +114,12 @@ struct vhost_virtqueue {#define VIRTIO_F_VERSION_1 32#endif +struct guest_page { +uint64_t guest_phys_addr; +uint64_t host_phys_addr; +uint64_t size; +}; +/** * Device structure contains all configuration information relating * to the device. @@ -137,6 +143,10 @@ struct virtio_net {uint64_t log_addr;struct ether_addr mac; +uint32_t nr_guest_pages; +uint32_t max_guest_pages; +struct guest_page *guest_pages; + } __rte_cache_aligned;/** @@ -217,6 +227,26 @@ gpa_to_vva(struct virtio_net *dev, uint64_t gpa)return 0 ; } +/* Convert guest physical address to host physical address */ +static inline phys_addr_t __attribute__((always_inline))
lib/librte_vhost/vhost.h gpa_to_vva(struct virtio_net *dev, uint64_t gpa) · line 241
Quoted 10 lines
lib/librte_vhost/vhost.h +10
-0+gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) +{ + uint32_t i; +struct guest_page *page; + +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + +if (gpa >= page->guest_phys_addr && + gpa + size < page->guest_phys_addr + page->size) {
Shouldn't be '<=' here?
Quoted 19 lines
lib/librte_vhost/vhost.h +8
-0+return gpa - page->guest_phys_addr + + page->host_phys_addr; + } + } + +return 0 ; +} +struct virtio_net_device_ops const *notify_ops;struct virtio_net *get_device (int vid);lib/librte_vhost/vhost_user.c +0
-0@@ -372,6 +372,81 @@ vhost_user_set_vring_base(struct virtio_net *dev,return 0 ; }
lib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 384
Quoted 10 lines
lib/librte_vhost/vhost_user.c +10
-0+static void +add_one_guest_page (struct virtio_net *dev,uint64_t guest_phys_addr, +uint64_t host_phys_addr,uint64_t size) +{ +struct guest_page *page; + +if (dev->nr_guest_pages == dev->max_guest_pages) { + dev->max_guest_pages *=2 ; + dev->guest_pages =realloc (dev->guest_pages, + dev->max_guest_pages *sizeof (*page));
Maybe realloc return could be checked?
Quoted 31 lines
lib/librte_vhost/vhost_user.c +31
-0+ } + + page = &dev->guest_pages[dev->nr_guest_pages++]; + page->guest_phys_addr = guest_phys_addr; + page->host_phys_addr = host_phys_addr; + page->size = size; +} + +static void +add_guest_pages (struct virtio_net *dev,struct virtio_memory_region *reg, +uint64_t page_size) +{ +uint64_t reg_size = reg->size; +uint64_t host_user_addr = reg->host_user_addr; +uint64_t guest_phys_addr = reg->guest_phys_addr; +uint64_t host_phys_addr; +uint64_t size; +uint32_t pre_read; + + pre_read = *((uint32_t *)(uintptr_t )host_user_addr); + host_phys_addr =rte_mem_virt2phy ((void *)(uintptr_t )host_user_addr); + size = page_size - (guest_phys_addr & (page_size -1 )); + size =RTE_MIN (size, reg_size); + +add_one_guest_page (dev, guest_phys_addr, host_phys_addr, size); + host_user_addr += size; + guest_phys_addr += size; + reg_size -= size; + +while (reg_size >0 ) { + pre_read += *((uint32_t *)(uintptr_t )host_user_addr);
lib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 425
Quoted 10 lines
lib/librte_vhost/vhost_user.c +10
-0+ host_phys_addr =rte_mem_virt2phy ((void *)(uintptr_t )host_user_addr); +add_one_guest_page (dev, guest_phys_addr, host_phys_addr, page_size); + + host_user_addr += page_size; + guest_phys_addr += page_size; + reg_size -= page_size; + } + +/* FIXME */ +RTE_LOG (INFO ,VHOST_CONFIG ,":: %u ::\n" , pre_read);
For my information, what is the purpose of pre_read?
Quoted 60 lines
lib/librte_vhost/vhost_user.c +35
-0+} + +/* TODO: enable it only in debug mode? */ +static void +dump_guest_pages (struct virtio_net *dev) +{ +uint32_t i; +struct guest_page *page; + +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + +RTE_LOG (INFO ,VHOST_CONFIG , +"guest physical page region %u\n" +"\t guest_phys_addr: %" PRIx64"\n" +"\t host_phys_addr : %" PRIx64"\n" +"\t size : %" PRIx64"\n" , + i, + page->guest_phys_addr, + page->host_phys_addr, + page->size); + } +} +static int vhost_user_set_mem_table (struct virtio_net *dev,struct VhostUserMsg *pmsg) {@@ -396,6 +471,13 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) dev->mem =NULL ; } + dev->nr_guest_pages =0 ; +if (!dev->guest_pages) { + dev->max_guest_pages =8 ; + dev->guest_pages =malloc (dev->max_guest_pages * +sizeof (struct guest_page )); + } + dev->mem =rte_zmalloc ("vhost-mem-table" ,sizeof (struct virtio_memory ) +sizeof (struct virtio_memory_region ) * memory.nregions,0 );if (dev->mem ==NULL ) {@@ -447,6 +529,8 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) reg->mmap_size = mmap_size; reg->host_user_addr = (uint64_t )(uintptr_t )mmap_addr + mmap_offset; +add_guest_pages (dev, reg, alignment); +RTE_LOG (INFO ,VHOST_CONFIG ,"guest memory region %u, size: 0x%" PRIx64"\n" "\t guest physical addr: 0x%" PRIx64 "\n"@@ -466,6 +550,8 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) mmap_offset); } +dump_guest_pages (dev); +return 0 ; err_mmap:
Re: [PATCH 2/6] vhost: get guest/host physical address mappings
Quoted 14 lines · Maxime Coquelin
lib/librte_vhost/vhost.h gpa_to_vva(struct virtio_net *dev, uint64_t gpa) · line 241
Quoted 13 lines
lib/librte_vhost/vhost.h +12
-0+/* Convert guest physical address to host physical address */ +static inline phys_addr_t __attribute__((always_inline)) +gpa_to_hpa (struct virtio_net *dev,uint64_t gpa,uint64_t size) +{ +uint32_t i; +struct guest_page *page; + +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + +if (gpa >= page->guest_phys_addr && + gpa + size < page->guest_phys_addr + page->size) {Shouldn't be '<=' here?
Oops, you are right.
Quoted 19 lines
Quoted 19 lines
lib/librte_vhost/vhost.h +8
-0+return gpa - page->guest_phys_addr + + page->host_phys_addr; + } + } + +return 0 ; +} +struct
virtio_net_device_opsconst*notify_ops;
structvirtio_net*get_device(int vid);lib/librte_vhost/vhost_user.c +0
-0@@ -372,6 +372,81 @@ vhost_user_set_vring_base(struct virtio_net *dev,return 0 ; }
Quoted 12 lines
lib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 384
Quoted 10 lines
lib/librte_vhost/vhost_user.c +10
-0+static void +add_one_guest_page (struct virtio_net *dev,uint64_t guest_phys_addr, +uint64_t host_phys_addr,uint64_t size) +{ +struct guest_page *page; + +if (dev->nr_guest_pages == dev->max_guest_pages) { + dev->max_guest_pages *=2 ; + dev->guest_pages =realloc (dev->guest_pages, + dev->max_guest_pages *sizeof (*page));Maybe realloc return could be checked?
Yes, I should have done that. Besides, I also forgot to free it at somewhere. Will fix it.
Quoted 35 lines
Quoted 35 lines
lib/librte_vhost/vhost_user.c +34
-0+ } + + page = &dev->guest_pages[dev->nr_guest_pages++]; + page->guest_phys_addr = guest_phys_addr; + page->host_phys_addr = host_phys_addr; + page->size = size; +} + +static void +add_guest_pages (struct virtio_net *dev,struct virtio_memory_region *reg, +uint64_t page_size) +{ +uint64_t reg_size = reg->size; +uint64_t host_user_addr = reg->host_user_addr; +uint64_t guest_phys_addr = reg->guest_phys_addr; +uint64_t host_phys_addr; +uint64_t size; +uint32_t pre_read; + + pre_read = *((uint32_t *)(uintptr_t )host_user_addr); + host_phys_addr =rte_mem_virt2phy ((void *)(uintptr_t )host_user_addr); + size = page_size - (guest_phys_addr & (page_size -1 )); + size =RTE_MIN (size, reg_size); + +add_one_guest_page (dev, guest_phys_addr, host_phys_addr, size); + host_user_addr += size; + guest_phys_addr += size; + reg_size -= size; + +while (reg_size >0 ) { + pre_read += *((uint32_t *)(uintptr_t )host_user_addr); + host_phys_addr =rte_mem_virt2phy ((void *)(uintptr_t )host_user_addr); +add_one_guest_page (dev, guest_phys_addr, host_phys_addr, page_size); +
Quoted 8 lines
lib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 425
Quoted 7 lines
lib/librte_vhost/vhost_user.c +7
-0+ host_user_addr += page_size; + guest_phys_addr += page_size; + reg_size -= page_size; + } + +/* FIXME */ +RTE_LOG (INFO ,VHOST_CONFIG ,":: %u ::\n" , pre_read);For my information, what is the purpose of
pre_read?
Again, I put a FIXME here, but I forgot to add some explanation.
Here is the thing: the read will make sure the kernel populate the corresponding PTE entry, so that rte_mem_virt2phy() will return proper physical address, otherwise, invalid value is returned.
I can't simply do the read but do not actually reference/consume it. Otherwise, the compiler will treat it as some noops and remove it.
An ugly RTE_LOG will make sure the read operation is not eliminated. I'm seeking a more proper way to achieve that. Maybe I can add a new field in virtio_net structure and store it there.
Or, do you have better ideas?
--yliu
Re: [PATCH 2/6] vhost: get guest/host physical address mappings
Quoted 10 lines · Yuanhan Liu
Quoted 4 lines
Quoted 4 lines
+
Quoted 2 lines
lib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 425
Quoted 2 lines
lib/librte_vhost/vhost_user.c +2
-0+/* FIXME */ +RTE_LOG (INFO ,VHOST_CONFIG ,":: %u ::\n" , pre_read);For my information, what is the purpose of
pre_read?Again, I put a FIXME here, but I forgot to add some explanation.
Here is the thing: the read will make sure the kernel populate the corresponding PTE entry, so that
rte_mem_virt2phy()will return proper physical address, otherwise, invalid value is returned.
Quoted 8 lines
I can't simply do the read but do not actually reference/consume it. Otherwise, the compiler will treat it as some noops and remove it.
An ugly
RTE_LOGwill make sure the read operation is not eliminated. I'm seeking a more proper way to achieve that. Maybe I can add a new field invirtio_netstructure and store it there.Or, do you have better ideas?
This behavior is pretty twisted, no?
Shouldn't be rte_mem_virt2phy() role to ensure returning a valid value?
I have no better idea for now, but I will think about it.
Regards,
Maxime
Re: [PATCH 2/6] vhost: get guest/host physical address mappings
Quoted 16 lines · Maxime Coquelin
Quoted 13 lines · Yuanhan Liu
Quoted 4 lines
Quoted 4 lines
+
Quoted 2 lines
lib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 425
Quoted 2 lines
lib/librte_vhost/vhost_user.c +2
-0+/* FIXME */ +RTE_LOG (INFO ,VHOST_CONFIG ,":: %u ::\n" , pre_read);For my information, what is the purpose of
pre_read?Again, I put a FIXME here, but I forgot to add some explanation.
Here is the thing: the read will make sure the kernel populate the corresponding PTE entry, so that
rte_mem_virt2phy()will return proper physical address, otherwise, invalid value is returned.I can't simply do the read but do not actually reference/consume it. Otherwise, the compiler will treat it as some noops and remove it.
Quoted 7 lines
Quoted 5 lines
An ugly
RTE_LOGwill make sure the read operation is not eliminated. I'm seeking a more proper way to achieve that. Maybe I can add a new field invirtio_netstructure and store it there.Or, do you have better ideas?
This behavior is pretty twisted, no?
I have to say, yes, kind of.
Quoted 1 line
Shouldn't be
rte_mem_virt2phy()role to ensure returning a valid value?
Not exactly. I think rte_mem_virt2phy() is more likely to fetch the physical address of huge pages. And for those huge pages, EAL makes sure they will be populated: it used to do a zero memset before to achieve that. Since 5ce3ace1de45 ("eal: remove unnecessary hugepage zero-filling"), it uses MAP_POPULATE option instead.
So, thank you that you just remind me of the MAP_POPULATE option. I just had a quick try, it worked like a charm :)
--yliu
Quoted 4 lines
I have no better idea for now, but I will think about it.
Regards,
Maxime
Re: [PATCH 2/6] vhost: get guest/host physical address mappings
Quoted 29 lines · Yuanhan Liu
Quoted 16 lines · Maxime Coquelin
Quoted 13 lines · Yuanhan Liu
Quoted 4 lines
Quoted 4 lines
+
Quoted 2 lines
lib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 425
Quoted 2 lines
lib/librte_vhost/vhost_user.c +2
-0+/* FIXME */ +RTE_LOG (INFO ,VHOST_CONFIG ,":: %u ::\n" , pre_read);For my information, what is the purpose of
pre_read?Again, I put a FIXME here, but I forgot to add some explanation.
Here is the thing: the read will make sure the kernel populate the corresponding PTE entry, so that
rte_mem_virt2phy()will return proper physical address, otherwise, invalid value is returned.I can't simply do the read but do not actually reference/consume it. Otherwise, the compiler will treat it as some noops and remove it.
Quoted 7 lines
Quoted 5 lines
An ugly
RTE_LOGwill make sure the read operation is not eliminated. I'm seeking a more proper way to achieve that. Maybe I can add a new field invirtio_netstructure and store it there.Or, do you have better ideas?
This behavior is pretty twisted, no?
I have to say, yes, kind of.
Quoted 1 line
Shouldn't be
rte_mem_virt2phy()role to ensure returning a valid value?
Quoted 8 lines
Not exactly. I think
rte_mem_virt2phy()is more likely to fetch the physical address of huge pages. And for those huge pages, EAL makes sure they will be populated: it used to do a zero memset before to achieve that. Since5ce3ace1de45("eal: remove unnecessary hugepage zero-filling"), it usesMAP_POPULATEoption instead.So, thank you that you just remind me of the
MAP_POPULATEoption. I just had a quick try, it worked like a charm :)
Excellent! :)
PATCH 3/6 vhost: introduce last avail idx for Tx
In reply to Yuanhan LiuSo far, we retrieve both the used ring avail ring idx by last_used_idx var; it won't be a problem because we used ring is updated immediately after those avail entries are consumed.
But that's not true when Tx zero copy is enabled, that used ring is updated only when the mbuf is consumed. Thus, we need use another var to note the last avail ring idx we have consumed.
Therefore, last_avail_idx is introduced.
--- lib/librte_vhost/vhost.h | 2 +- lib/librte_vhost/virtio_net.c | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-)
lib/librte_vhost/vhost.h +1 -1
@@ -70,7 +70,7 @@ struct vhost_virtqueue {struct vring_used *used;uint32_t size; -/* Last index used on the available ring */ +uint16_t last_avail_idx;volatile uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
lib/librte_vhost/virtio_net.c +11 -8
@@ -846,16 +846,17 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, } } - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - free_entries = avail_idx - vq->last_used_idx; + free_entries = *((volatile uint16_t *)&vq->avail->idx) - + vq->last_avail_idx;if (free_entries ==0 ) goto out;LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -/* Prefetch available ring to retrieve head indexes. */ - used_idx = vq->last_used_idx & (vq->size -1 ); -rte_prefetch0 (&vq->avail->ring[used_idx]); +/* Prefetch available and used ring */ + avail_idx = vq->last_avail_idx & (vq->size -1 ); + used_idx = vq->last_used_idx & (vq->size -1 ); +rte_prefetch0 (&vq->avail->ring[avail_idx]);rte_prefetch0 (&vq->used->ring[used_idx]); count =RTE_MIN (count,MAX_PKT_BURST );
@@ -865,8 +866,9 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,/* Retrieve all of the head indexes first to avoid caching issues. */ for (i =0 ; i < count; i++) { - used_idx = (vq->last_used_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; + avail_idx = (vq->last_avail_idx + i) & (vq->size -1 ); + used_idx = (vq->last_used_idx + i) & (vq->size -1 ); + desc_indexes[i] = vq->avail->ring[avail_idx]; vq->used->ring[used_idx].id = desc_indexes[i]; vq->used->ring[used_idx].len =0 ;
@@ -900,7 +902,8 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,rte_smp_wmb ();rte_smp_rmb (); vq->used->idx += i; - vq->last_used_idx += i; + vq->last_avail_idx += i; + vq->last_used_idx += i;vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx),sizeof (vq->used->idx));
Signature · 1 line
1.9.0
Re: [PATCH 3/6] vhost: introduce last avail idx for Tx
Quoted 8 lines · Yuanhan Liu
So far, we retrieve both the used ring avail ring idx by
last_used_idxvar; it won't be a problem because we used ring is updated immediately after those avail entries are consumed.But that's not true when Tx zero copy is enabled, that used ring is updated only when the mbuf is consumed. Thus, we need use another var to note the last avail ring idx we have consumed.
Quoted 7 lines
Therefore,
last_avail_idxis introduced.Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost.h | 2 +- lib/librte_vhost/virtio_net.c | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-)
Looks good to me:
Thanks,
Maxime
PATCH 4/6 vhost: add Tx zero copy
In reply to Yuanhan LiuThe basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.
Tx zero copy is introduced for performance reason, and some rough tests show about 40% perfomance boost for packet size 1400B. FOr small packets, (e.g. 64B), it actually slows a bit down. That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
--- lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 21 ++++++ lib/librte_vhost/vhost_user.c | 41 +++++++++- lib/librte_vhost/virtio_net.c | 169 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 214 insertions(+), 19 deletions(-)
lib/librte_vhost/vhost.c +2 -0
@@ -141,6 +141,8 @@ init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)/* always set the default vq pair to enabled */ if (qp_idx ==0 ) vq->enabled =1 ; + +TAILQ_INIT (&vq->zmbuf_list); } static void
lib/librte_vhost/vhost.h +21 -0
@@ -36,6 +36,7 @@#include <stdint.h> #include <stdio.h> #include <sys/types.h> +#include <sys/queue.h> #include <unistd.h> #include <linux/vhost.h>
@@ -61,6 +62,19 @@ struct buf_vector {uint32_t desc_idx; }; +/* +* A structure to hold some fields needed in zero copy code path, +* mainly for associating an mbuf with the right desc_idx. +*/ +struct zcopy_mbuf { +struct rte_mbuf *mbuf; +uint32_t desc_idx; +uint16_t in_use; + +TAILQ_ENTRY (zcopy_mbuf) next; +}; +TAILQ_HEAD (zcopy_mbuf_list, zcopy_mbuf); +/** * Structure contains variables relevant to RX/TX virtqueues. */
@@ -85,6 +99,12 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +uint16_t nr_zmbuf; +uint16_t zmbuf_size; +uint16_t last_zmbuf_idx; +struct zcopy_mbuf *zmbufs; +struct zcopy_mbuf_list zmbuf_list; } __rte_cache_aligned;/* Old kernels have no such macro defined */
@@ -147,6 +167,7 @@ struct virtio_net {uint32_t max_guest_pages;struct guest_page *guest_pages; +int tx_zero_copy; } __rte_cache_aligned;/**
lib/librte_vhost/vhost_user.c +40 -1
64 lines not on this page · raw message
lib/librte_vhost/virtio_net.c +151 -18
214 lines not on this page · raw message
Signature · 1 line
1.9.0
Re: [PATCH 4/6] vhost: add Tx zero copy
Quoted 39 lines · Yuanhan Liu
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of
rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the
rte_vhost_dequeue_burstentrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.- "struct
zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.Tx zero copy is introduced for performance reason, and some rough tests show about 40% perfomance boost for packet size 1400B. FOr small packets, (e.g. 64B), it actually slows a bit down. That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 21 ++++++ lib/librte_vhost/vhost_user.c | 41 +++++++++- lib/librte_vhost/virtio_net.c | 169 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 214 insertions(+), 19 deletions(-)
...
Quoted 15 lines
rte_vhost_dequeue_burst(int vid,
uint16_tqueue_id,lib/librte_vhost/virtio_net.c +9
-0struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)@@ -823,6 +943,30 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,if (unlikely (vq->enabled ==0 ))return 0 ; +if (dev->tx_zero_copy) { +struct zcopy_mbuf *zmbuf, *next; +int nr_updated =0 ; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +if (mbuf_is_consumed (zmbuf->mbuf)) {
lib/librte_vhost/virtio_net.c rte_vhost_dequeue_burst(int vid, uint16_t queue_id, · line 964
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+ used_idx = vq->last_used_idx++ & (vq->size -1 ); +update_used_ring (dev, vq, used_idx, + zmbuf->desc_idx); + nr_updated +=1 ; + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + }
Shouldn't you break the loop here as soon as a mbuf is not consumed? Indeed, they might not be consumed sequentially, and would cause last_used_idx to be incremented whereas it shouldn't.
Re: [PATCH 4/6] vhost: add Tx zero copy
Quoted 62 lines · Maxime Coquelin
Quoted 39 lines · Yuanhan Liu
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of
rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the
rte_vhost_dequeue_burstentrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.- "struct
zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.Tx zero copy is introduced for performance reason, and some rough tests show about 40% perfomance boost for packet size 1400B. FOr small packets, (e.g. 64B), it actually slows a bit down. That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
Signed-off-by Yuanhan Liu <[email protected]>---
lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 21 ++++++ lib/librte_vhost/vhost_user.c | 41 +++++++++- lib/librte_vhost/virtio_net.c | 169 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 214 insertions(+), 19 deletions(-)...
Quoted 18 lines
rte_vhost_dequeue_burst(int vid,
uint16_tqueue_id,
structrte_mempool*mbuf_pool, structrte_mbuf**pkts,uint16_tcount)lib/librte_vhost/virtio_net.c +12
-0@@ -823,6 +943,30 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,if (unlikely (vq->enabled ==0 ))return 0 ; +if (dev->tx_zero_copy) { +struct zcopy_mbuf *zmbuf, *next; +int nr_updated =0 ; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +if (mbuf_is_consumed (zmbuf->mbuf)) { + used_idx = vq->last_used_idx++ & (vq->size -1 ); +update_used_ring (dev, vq, used_idx, + zmbuf->desc_idx);
Quoted 8 lines
lib/librte_vhost/virtio_net.c rte_vhost_dequeue_burst(int vid, uint16_t queue_id, · line 964
Quoted 7 lines
lib/librte_vhost/virtio_net.c +7
-0+ nr_updated +=1 ; + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + }Shouldn't you break the loop here as soon as a mbuf is not consumed?
I have thought of that as well, as a micro optimization. But I was wondering what if a heading mbuf is pin locked by the DPDK APP? Then the whole chain would be blocked. This should be rare, but I think we should think of the worst case.
Besides that, the performance boost I got is quite decent, that I think we could drop this micro optimization.
Quoted 2 lines
Indeed, they might not be consumed sequentially, and would cause
last_used_idxto be incremented whereas it shouldn't.
I think the out of order used vring update won't be an issue here. Well, there might be some problems for reconnect. The trick the commit 0823c1cb0a73 ("vhost: workaround stale vring base") introduced assumes that used vring will always be updated in order.
--yliu
Re: [PATCH 4/6] vhost: add Tx zero copy
Quoted 72 lines · Yuanhan Liu
Quoted 62 lines · Maxime Coquelin
Quoted 39 lines · Yuanhan Liu
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of
rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the
rte_vhost_dequeue_burstentrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.- "struct
zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.Tx zero copy is introduced for performance reason, and some rough tests show about 40% perfomance boost for packet size 1400B. FOr small packets, (e.g. 64B), it actually slows a bit down. That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
Signed-off-by Yuanhan Liu <[email protected]>---
lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 21 ++++++ lib/librte_vhost/vhost_user.c | 41 +++++++++- lib/librte_vhost/virtio_net.c | 169 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 214 insertions(+), 19 deletions(-)...
Quoted 18 lines
rte_vhost_dequeue_burst(int vid,
uint16_tqueue_id,
structrte_mempool*mbuf_pool, structrte_mbuf**pkts,uint16_tcount)lib/librte_vhost/virtio_net.c +12
-0@@ -823,6 +943,30 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,if (unlikely (vq->enabled ==0 ))return 0 ; +if (dev->tx_zero_copy) { +struct zcopy_mbuf *zmbuf, *next; +int nr_updated =0 ; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +if (mbuf_is_consumed (zmbuf->mbuf)) { + used_idx = vq->last_used_idx++ & (vq->size -1 ); +update_used_ring (dev, vq, used_idx, + zmbuf->desc_idx);Quoted 8 lines
lib/librte_vhost/virtio_net.c rte_vhost_dequeue_burst(int vid, uint16_t queue_id, · line 964
Quoted 7 lines
lib/librte_vhost/virtio_net.c +7
-0+ nr_updated +=1 ; + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + }Shouldn't you break the loop here as soon as a mbuf is not consumed?
Quoted 7 lines
I have thought of that as well, as a micro optimization. But I was wondering what if a heading mbuf is pin locked by the DPDK APP? Then the whole chain would be blocked. This should be rare, but I think we should think of the worst case.
Besides that, the performance boost I got is quite decent, that I think we could drop this micro optimization.
Forget my comment, this was a misunderstanding of the code on my side.
Regards,
Maxime
PATCH 5/6 vhost: add a flag to enable Tx zero copy
In reply to Yuanhan LiuAdd a new flag ``RTE_VHOST_USER_TX_ZERO_COPY`` to explictily enable Tx zero copy. If not given, Tx zero copy is disabled by default.
--- doc/guides/prog_guide/vhost_lib.rst | 7 ++++++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 5 files changed, 23 insertions(+), 1 deletion(-)
doc/guides/prog_guide/vhost_lib.rst +6 -1
@@ -79,7 +79,7 @@ The following is an overview of the Vhost API functions: ``/dev/path`` character device file will be created. For vhost-user server mode, a Unix domain socket file ``path`` will be created. - Currently two flags are supported (these are valid for vhost-user only): + Currently supported flags are (these are valid for vhost-user only): - ``RTE_VHOST_USER_CLIENT``
@@ -97,6 +97,11 @@ The following is an overview of the Vhost API functions: This reconnect option is enabled by default. However, it can be turned off by setting this flag. + - ``RTE_VHOST_USER_TX_ZERO_COPY`` + + Tx zero copy will be enabled when this flag is set. It is disabled by + default. + * ``rte_vhost_driver_session_start()`` This function starts the vhost session loop to handle vhost messages. It
lib/librte_vhost/rte_virtio_net.h +1 -0
@@ -53,6 +53,7 @@#define RTE_VHOST_USER_CLIENT (1ULL << 0)#define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) +#define RTE_VHOST_USER_TX_ZERO_COPY (1ULL << 2)/* Enum for virtqueue management. */ enum {VIRTIO_RXQ ,VIRTIO_TXQ ,VIRTIO_QNUM };
lib/librte_vhost/socket.c +5 -0
@@ -62,6 +62,7 @@ struct vhost_user_socket {int connfd;bool is_server;bool reconnect; +bool tx_zero_copy; };struct vhost_user_connection {
@@ -203,6 +204,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket) size =strnlen (vsocket->path,PATH_MAX );vhost_set_ifname (vid, vsocket->path, size); +if (vsocket->tx_zero_copy) +vhost_enable_tx_zero_copy (vid); +RTE_LOG (INFO ,VHOST_CONFIG ,"new device, handle is %d\n" , vid); vsocket->connfd = fd;
@@ -499,6 +503,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)memset (vsocket,0 ,sizeof (struct vhost_user_socket )); vsocket->path =strdup (path); vsocket->connfd =-1 ; + vsocket->tx_zero_copy = flags &RTE_VHOST_USER_TX_ZERO_COPY ;if ((flags &RTE_VHOST_USER_CLIENT ) !=0 ) { vsocket->reconnect = !(flags &RTE_VHOST_USER_NO_RECONNECT );
lib/librte_vhost/vhost.c +10 -0
@@ -290,6 +290,16 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) dev->ifname[sizeof (dev->ifname) -1 ] ='\0' ; } +void +vhost_enable_tx_zero_copy (int vid) +{ +struct virtio_net *dev =get_device (vid); + +if (dev ==NULL ) +return ; + + dev->tx_zero_copy =1 ; +}int rte_vhost_get_numa_node (int vid)
lib/librte_vhost/vhost.h +1 -0
@@ -279,6 +279,7 @@ void vhost_destroy_device(int);int alloc_vring_queue_pair (struct virtio_net *dev,uint32_t qp_idx);void vhost_set_ifname (int ,const char *if_name,unsigned int if_len); +void vhost_enable_tx_zero_copy (int vid);/* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
Signature · 1 line
1.9.0
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Just curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin; Yuanhan Liu
Subject: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Add a new flag ``RTE_VHOST_USER_TX_ZERO_COPY`` to explictily enable Tx zero copy. If not given, Tx zero copy is disabled by default.
--- doc/guides/prog_guide/vhost_lib.rst | 7 ++++++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 5 files changed, 23 insertions(+), 1 deletion(-)
doc/guides/prog_guide/vhost_lib.rst +6 -1
@@ -79,7 +79,7 @@ The following is an overview of the Vhost API functions: ``/dev/path`` character device file will be created. For vhost-user server mode, a Unix domain socket file ``path`` will be created. - Currently two flags are supported (these are valid for vhost-user only): + Currently supported flags are (these are valid for vhost-user only): - ``RTE_VHOST_USER_CLIENT``
@@ -97,6 +97,11 @@ The following is an overview of the Vhost API functions: This reconnect option is enabled by default. However, it can be turned off by setting this flag. + - ``RTE_VHOST_USER_TX_ZERO_COPY`` + + Tx zero copy will be enabled when this flag is set. It is disabled by + default. + * ``rte_vhost_driver_session_start()`` This function starts the vhost session loop to handle vhost messages. It
lib/librte_vhost/rte_virtio_net.h +1 -0
@@ -53,6 +53,7 @@#define RTE_VHOST_USER_CLIENT (1ULL << 0)#define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) +#define RTE_VHOST_USER_TX_ZERO_COPY (1ULL << 2)/* Enum for virtqueue management. */ enum {VIRTIO_RXQ ,VIRTIO_TXQ ,VIRTIO_QNUM };
lib/librte_vhost/socket.c +5 -0
@@ -62,6 +62,7 @@ struct vhost_user_socket {int connfd;bool is_server;bool reconnect; +bool tx_zero_copy; };struct vhost_user_connection {
@@ -203,6 +204,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket) size =strnlen (vsocket->path,PATH_MAX );vhost_set_ifname (vid, vsocket->path, size); +if (vsocket->tx_zero_copy) +vhost_enable_tx_zero_copy (vid); +RTE_LOG (INFO ,VHOST_CONFIG ,"new device, handle is %d\n" , vid); vsocket->connfd = fd;
@@ -499,6 +503,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)memset (vsocket,0 ,sizeof (struct vhost_user_socket )); vsocket->path =strdup (path); vsocket->connfd =-1 ; + vsocket->tx_zero_copy = flags &RTE_VHOST_USER_TX_ZERO_COPY ;if ((flags &RTE_VHOST_USER_CLIENT ) !=0 ) { vsocket->reconnect = !(flags &RTE_VHOST_USER_NO_RECONNECT );
lib/librte_vhost/vhost.c +10 -0
@@ -290,6 +290,16 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) dev->ifname[sizeof (dev->ifname) -1 ] ='\0' ; } +void +vhost_enable_tx_zero_copy (int vid) +{ +struct virtio_net *dev =get_device (vid); + +if (dev ==NULL ) +return ; + + dev->tx_zero_copy =1 ; +}int rte_vhost_get_numa_node (int vid)
lib/librte_vhost/vhost.h +1 -0
@@ -279,6 +279,7 @@ void vhost_destroy_device(int);int alloc_vring_queue_pair (struct virtio_net *dev,uint32_t qp_idx);void vhost_set_ifname (int ,const char *if_name,unsigned int if_len); +void vhost_enable_tx_zero_copy (int vid);/* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
Signature · 1 line
1.9.0
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Another interesting thing to me is the ZERO-COPY settings. If I have 2 vhost, and 1 is set as Zero-copy=0, and another is set zero-copy=1, so the vhost will take it as Zero-copy Enabled for all vhost, or for one vhost. Does the vhost allow such usage? Or we need Enforce all vhost zero-copy to be a same number.
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Xu, Qian Q
Sent: Tuesday, September 06, 2016 5:00 PM
To: Yuanhan Liu; [email protected]
Cc: Maxime Coquelin
Subject: Re: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Just curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin; Yuanhan Liu
Subject: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Add a new flag ``RTE_VHOST_USER_TX_ZERO_COPY`` to explictily enable Tx zero copy. If not given, Tx zero copy is disabled by default.
--- doc/guides/prog_guide/vhost_lib.rst | 7 ++++++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 5 files changed, 23 insertions(+), 1 deletion(-)
doc/guides/prog_guide/vhost_lib.rst +6 -1
@@ -79,7 +79,7 @@ The following is an overview of the Vhost API functions: ``/dev/path`` character device file will be created. For vhost-user server mode, a Unix domain socket file ``path`` will be created. - Currently two flags are supported (these are valid for vhost-user only): + Currently supported flags are (these are valid for vhost-user only): - ``RTE_VHOST_USER_CLIENT``
@@ -97,6 +97,11 @@ The following is an overview of the Vhost API functions: This reconnect option is enabled by default. However, it can be turned off by setting this flag. + - ``RTE_VHOST_USER_TX_ZERO_COPY`` + + Tx zero copy will be enabled when this flag is set. It is disabled by + default. + * ``rte_vhost_driver_session_start()`` This function starts the vhost session loop to handle vhost messages. It
lib/librte_vhost/rte_virtio_net.h +1 -0
@@ -53,6 +53,7 @@#define RTE_VHOST_USER_CLIENT (1ULL << 0)#define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) +#define RTE_VHOST_USER_TX_ZERO_COPY (1ULL << 2)/* Enum for virtqueue management. */ enum {VIRTIO_RXQ ,VIRTIO_TXQ ,VIRTIO_QNUM };
lib/librte_vhost/socket.c +5 -0
@@ -62,6 +62,7 @@ struct vhost_user_socket {int connfd;bool is_server;bool reconnect; +bool tx_zero_copy; };struct vhost_user_connection {
@@ -203,6 +204,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket) size =strnlen (vsocket->path,PATH_MAX );vhost_set_ifname (vid, vsocket->path, size); +if (vsocket->tx_zero_copy) +vhost_enable_tx_zero_copy (vid); +RTE_LOG (INFO ,VHOST_CONFIG ,"new device, handle is %d\n" , vid); vsocket->connfd = fd;
@@ -499,6 +503,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)memset (vsocket,0 ,sizeof (struct vhost_user_socket )); vsocket->path =strdup (path); vsocket->connfd =-1 ; + vsocket->tx_zero_copy = flags &RTE_VHOST_USER_TX_ZERO_COPY ;if ((flags &RTE_VHOST_USER_CLIENT ) !=0 ) { vsocket->reconnect = !(flags &RTE_VHOST_USER_NO_RECONNECT );
lib/librte_vhost/vhost.c +10 -0
@@ -290,6 +290,16 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) dev->ifname[sizeof (dev->ifname) -1 ] ='\0' ; } +void +vhost_enable_tx_zero_copy (int vid) +{ +struct virtio_net *dev =get_device (vid); + +if (dev ==NULL ) +return ; + + dev->tx_zero_copy =1 ; +}int rte_vhost_get_numa_node (int vid)
lib/librte_vhost/vhost.h +1 -0
@@ -279,6 +279,7 @@ void vhost_destroy_device(int);int alloc_vring_queue_pair (struct virtio_net *dev,uint32_t qp_idx);void vhost_set_ifname (int ,const char *if_name,unsigned int if_len); +void vhost_enable_tx_zero_copy (int vid);/* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
Signature · 1 line
1.9.0
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Quoted 3 lines · Qian Q
Another interesting thing to me is the ZERO-COPY settings. If I have 2 vhost, and 1 is set as Zero-copy=0, and another is set zero-copy=1, so the vhost will take it as Zero-copy Enabled for all vhost, or for one vhost.
The flag is per vhost-user socket file path.
If you have two vhost interfaces attached on the same socket files (when it acts as the server), the two vhost interface will both with zero copy enabled.
If the two vhost interfaces are attached on different socket file, zero-copy will be only enabled when the option is given for corresponding socket file.
Quoted 1 line
Does the vhost allow such usage? Or we need
Yes, it's allowd.
Quoted 1 line
Enforce all vhost zero-copy to be a same number.
Nope.
--yliu
Quoted 133 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Xu, Qian Q
Sent: Tuesday, September 06, 2016 5:00 PM
To: Yuanhan Liu; [email protected]
Cc: Maxime Coquelin
Subject: Re: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copyJust curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin; Yuanhan Liu
Subject: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copyAdd a new flag ``RTE_VHOST_USER_TX_ZERO_COPY`` to explictily enable Tx zero copy. If not given, Tx zero copy is disabled by default.
Signed-off-by Yuanhan Liu <[email protected]>--- doc/guides/prog_guide/vhost_lib.rst | 7 ++++++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 5 files changed, 23 insertions(+), 1 deletion(-)doc/guides/prog_guide/vhost_lib.rst +6
-1@@ -79,7 +79,7 @@ The following is an overview of the Vhost API functions: ``/dev/path`` character device file will be created. For vhost-user server mode, a Unix domain socket file ``path`` will be created. - Currently two flags are supported (these are valid for vhost-user only): + Currently supported flags are (these are valid for vhost-user only): - ``RTE_VHOST_USER_CLIENT``@@ -97,6 +97,11 @@ The following is an overview of the Vhost API functions: This reconnect option is enabled by default. However, it can be turned off by setting this flag. + - ``RTE_VHOST_USER_TX_ZERO_COPY`` + + Tx zero copy will be enabled when this flag is set. It is disabled by + default. + * ``rte_vhost_driver_session_start()`` This function starts the vhost session loop to handle vhost messages. Itlib/librte_vhost/rte_virtio_net.h +1
-0@@ -53,6 +53,7 @@#define RTE_VHOST_USER_CLIENT (1ULL << 0)#define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) +#define RTE_VHOST_USER_TX_ZERO_COPY (1ULL << 2)/* Enum for virtqueue management. */ enum {VIRTIO_RXQ ,VIRTIO_TXQ ,VIRTIO_QNUM };lib/librte_vhost/socket.c +5
-0@@ -62,6 +62,7 @@ struct vhost_user_socket {int connfd;bool is_server;bool reconnect; +bool tx_zero_copy; };struct vhost_user_connection {@@ -203,6 +204,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket) size =strnlen (vsocket->path,PATH_MAX );vhost_set_ifname (vid, vsocket->path, size); +if (vsocket->tx_zero_copy) +vhost_enable_tx_zero_copy (vid); +RTE_LOG (INFO ,VHOST_CONFIG ,"new device, handle is %d\n" , vid); vsocket->connfd = fd;@@ -499,6 +503,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)memset (vsocket,0 ,sizeof (struct vhost_user_socket )); vsocket->path =strdup (path); vsocket->connfd =-1 ; + vsocket->tx_zero_copy = flags &RTE_VHOST_USER_TX_ZERO_COPY ;if ((flags &RTE_VHOST_USER_CLIENT ) !=0 ) { vsocket->reconnect = !(flags &RTE_VHOST_USER_NO_RECONNECT );lib/librte_vhost/vhost.c +10
-0@@ -290,6 +290,16 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) dev->ifname[sizeof (dev->ifname) -1 ] ='\0' ; } +void +vhost_enable_tx_zero_copy (int vid) +{ +struct virtio_net *dev =get_device (vid); + +if (dev ==NULL ) +return ; + + dev->tx_zero_copy =1 ; +}int rte_vhost_get_numa_node (int vid)lib/librte_vhost/vhost.h +1
-0@@ -279,6 +279,7 @@ void vhost_destroy_device(int);int alloc_vring_queue_pair (struct virtio_net *dev,uint32_t qp_idx);void vhost_set_ifname (int ,const char *if_name,unsigned int if_len); +void vhost_enable_tx_zero_copy (int vid);/* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user. Signature · 1 line
1.9.0
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Thx for the clarification. As to the naming, although it's a little confusing, if people are fine with it, I'm fine.
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Tuesday, September 6, 2016 6:02 PM
To: Xu, Qian Q <[email protected]>
Cc: [email protected]; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Quoted 3 lines · Qian Q
Another interesting thing to me is the ZERO-COPY settings. If I have 2 vhost, and 1 is set as Zero-copy=0, and another is set zero-copy=1, so the vhost will take it as Zero-copy Enabled for all vhost, or for one vhost.
The flag is per vhost-user socket file path.
If you have two vhost interfaces attached on the same socket files (when it acts as the server), the two vhost interface will both with zero copy enabled.
If the two vhost interfaces are attached on different socket file, zero-copy will be only enabled when the option is given for corresponding socket file.
Quoted 1 line
Does the vhost allow such usage? Or we need
Yes, it's allowd.
Quoted 1 line
Enforce all vhost zero-copy to be a same number.
Nope.
--yliu
Quoted 136 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Xu, Qian Q
Sent: Tuesday, September 06, 2016 5:00 PM
To: Yuanhan Liu; [email protected]
Cc: Maxime Coquelin
Subject: Re: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copyJust curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin; Yuanhan Liu
Subject: [dpdk-dev] [PATCH 5/6] vhost: add a flag to enable Tx zero copyAdd a new flag ``RTE_VHOST_USER_TX_ZERO_COPY`` to explictily enable Tx zero copy. If not given, Tx zero copy is disabled by default.
Signed-off-by Yuanhan Liu <[email protected]>--- doc/guides/prog_guide/vhost_lib.rst | 7 ++++++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 5 files changed, 23 insertions(+), 1 deletion(-)doc/guides/prog_guide/vhost_lib.rst +6
-1@@ -79,7 +79,7 @@ The following is an overview of the Vhost API functions: ``/dev/path`` character device file will be created. For vhost-user server mode, a Unix domain socket file ``path`` will be created. - Currently two flags are supported (these are valid for vhost-user only): + Currently supported flags are (these are valid for vhost-user only): - ``RTE_VHOST_USER_CLIENT``@@ -97,6 +97,11 @@ The following is an overview of the Vhost API functions: This reconnect option is enabled by default. However, it can be turned off by setting this flag. + - ``RTE_VHOST_USER_TX_ZERO_COPY`` + + Tx zero copy will be enabled when this flag is set. It is disabled by + default. + * ``rte_vhost_driver_session_start()`` This function starts the vhost session loop to handle vhostmessages. It diff --git
a/lib/librte_vhost/rte_virtio_net.hb/lib/librte_vhost/rte_virtio_net.h
index 9caa622..5e437c6 100644lib/librte_vhost/rte_virtio_net.h +1
-0@@ -53,6 +53,7 @@#define RTE_VHOST_USER_CLIENT (1ULL << 0)#define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) +#define RTE_VHOST_USER_TX_ZERO_COPY (1ULL << 2)/* Enum for virtqueue management. */ enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM}; diff --git
a/lib/librte_vhost/socket.cb/lib/librte_vhost/socket.cindex bf03f84..5c3962d 100644lib/librte_vhost/socket.c +5
-1@@ -62,6 +62,7 @@ struct vhost_user_socket {int connfd;bool is_server;bool reconnect; +bool tx_zero_copy; };struct vhost_user_connection {@@ -203,6 +204,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket) size =strnlen (vsocket->path,PATH_MAX );vhost_set_ifname (vid, vsocket->path, size); +if (vsocket->tx_zero_copy) +vhost_enable_tx_zero_copy (vid); +RTE_LOG (INFO ,VHOST_CONFIG ,"new device, handle is %d\n" , vid); vsocket->connfd = fd;@@ -499,6 +503,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)memset (vsocket,0 ,sizeof (struct vhost_user_socket )); vsocket->path =strdup (path); vsocket->connfd =-1 ; + vsocket->tx_zero_copy = flags &RTE_VHOST_USER_TX_ZERO_COPY ;if ((flags &RTE_VHOST_USER_CLIENT ) !=0 ) { vsocket->reconnect = !(flags &RTE_VHOST_USER_NO_RECONNECT ); diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c indexab25649..5461e5b 100644
lib/librte_vhost/vhost.c +10
-0@@ -290,6 +290,16 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) dev->ifname[sizeof (dev->ifname) -1 ] ='\0' ; } +void +vhost_enable_tx_zero_copy (int vid) +{ +struct virtio_net *dev =get_device (vid); + +if (dev ==NULL ) +return ; + + dev->tx_zero_copy =1 ; +}int rte_vhost_get_numa_node (int vid)lib/librte_vhost/vhost.h index renamed from lib/librte_vhost/vhost.h +0
-0718133e..3081180 100644
lib/librte_vhost/vhost.h +1
-1@@ -279,6 +279,7 @@ void vhost_destroy_device(int); int alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx);void vhost_set_ifname (int ,const char *if_name,unsigned int if_len); +void vhost_enable_tx_zero_copy (int vid);/* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user. -- 1.9.0
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
In reply to Xu, Qian QQuoted 3 lines · Qian Q
Just curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
It's just that "Tx zero copy" looks more nature to me (yes, I took the name from the virtio point of view).
Besides that, naming it to "vhost Rx zero copy" would be a little weird, based on we have functions like "virtio_dev_rx" in the enqueue path while here we just touch dequeue path.
OTOH, I seldome say "vhost-user Tx zero copy"; I normally mention it as "Tx zero copy", without mentioning "vhost-user". For the flag RTE_VHOST_USER_TX_ZERO_COPY, all vhost-user flags start with "RTE_VHOST_USER_" prefix.
--yliu
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
2016-09-06 17:55, Yuanhan Liu:
Quoted 8 lines · Qian Q
Quoted 3 lines
Just curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
It's just that "Tx zero copy" looks more nature to me (yes, I took the name from the virtio point of view).
Quoted 8 lines
Besides that, naming it to "vhost Rx zero copy" would be a little weird, based on we have functions like "
virtio_dev_rx" in the enqueue path while here we just touch dequeue path.OTOH, I seldome say "vhost-user Tx zero copy"; I normally mention it as "Tx zero copy", without mentioning "vhost-user". For the flag
RTE_VHOST_USER_TX_ZERO_COPY, all vhost-user flags start with "RTE_VHOST_USER_" prefix.
I agree that the naming in vhost code is quite confusing.
It would be better to define a terminology and stop mixing virtio/vhost directions as well as Rx/Tx and enqueue/dequeue.
Or at least, it should be documented.
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
Quoted 13 lines · Thomas Monjalon
2016-09-06 17:55, Yuanhan Liu:
Quoted 12 lines · Qian Q
Quoted 3 lines
Just curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
It's just that "Tx zero copy" looks more nature to me (yes, I took the name from the virtio point of view).
Besides that, naming it to "vhost Rx zero copy" would be a little weird, based on we have functions like "
virtio_dev_rx" in the enqueue path while here we just touch dequeue path.
Quoted 8 lines
Quoted 4 lines
OTOH, I seldome say "vhost-user Tx zero copy"; I normally mention it as "Tx zero copy", without mentioning "vhost-user". For the flag
RTE_VHOST_USER_TX_ZERO_COPY, all vhost-user flags start with "RTE_VHOST_USER_" prefix.I agree that the naming in vhost code is quite confusing.
It would be better to define a terminology and stop mixing virtio/vhost directions as well as Rx/Tx and enqueue/dequeue.
I think we could/should avoid using Rx/Tx in vhost, but we should keep the enqueue/dequeue: that's how the two key vhost API named.
Quoted 1 line
Or at least, it should be documented.
Or, how about renaming it to RTE_VHOST_USER_DEQUEUE_ZERO_COPY, to align with the function name rte_vhost_dequeue_burst?
--yliu
Re: [PATCH 5/6] vhost: add a flag to enable Tx zero copy
2016-09-08 15:21, Yuanhan Liu:
Quoted 23 lines · Thomas Monjalon
Quoted 13 lines
2016-09-06 17:55, Yuanhan Liu:
Quoted 12 lines · Qian Q
Quoted 3 lines
Just curious about the naming: vhost USER TX Zero copy. In fact, it's Vhost RX zero-copy For virtio, it's Virtio TX zero-copy. So, I wonder why we call it as Vhost TX ZERO-COPY, Any comments?
It's just that "Tx zero copy" looks more nature to me (yes, I took the name from the virtio point of view).
Besides that, naming it to "vhost Rx zero copy" would be a little weird, based on we have functions like "
virtio_dev_rx" in the enqueue path while here we just touch dequeue path.Quoted 8 lines
Quoted 4 lines
OTOH, I seldome say "vhost-user Tx zero copy"; I normally mention it as "Tx zero copy", without mentioning "vhost-user". For the flag
RTE_VHOST_USER_TX_ZERO_COPY, all vhost-user flags start with "RTE_VHOST_USER_" prefix.I agree that the naming in vhost code is quite confusing.
It would be better to define a terminology and stop mixing virtio/vhost directions as well as Rx/Tx and enqueue/dequeue.
Quoted 7 lines
I think we could/should avoid using Rx/Tx in vhost, but we should keep the enqueue/dequeue: that's how the two key vhost API named.
Quoted 1 line
Or at least, it should be documented.
Or, how about renaming it to
RTE_VHOST_USER_DEQUEUE_ZERO_COPY, to align with the function namerte_vhost_dequeue_burst?
Seems reasonable, yes.
PATCH 6/6 examples/vhost: add an option to enable Tx zero copy
In reply to Yuanhan LiuAdd an option, --tx-zero-copy, to enable Tx zero copy.
One thing worth noting while using Tx zero copy is the nb_tx_desc has to be small enough so that the eth driver will hit the mbuf free threshold easily and thus free mbuf more frequently.
The reason behind that is, when Tx zero copy is enabled, guest Tx used vring will be updated only when corresponding mbuf is freed. If mbuf is not freed frequently, the guest Tx vring could be starved.
--- examples/vhost/main.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
examples/vhost/main.c +18 -1
@@ -130,6 +130,7 @@ static uint32_t enable_tx_csum;static uint32_t enable_tso;static int client_mode; +static int tx_zero_copy;/* Specify timeout (in useconds) between retries on RX. */ static uint32_t burst_rx_delay_time =BURST_RX_WAIT_US ;
@@ -297,6 +298,17 @@ port_init(uint8_t port) rx_ring_size =RTE_TEST_RX_DESC_DEFAULT ; tx_ring_size =RTE_TEST_TX_DESC_DEFAULT ; + +/* +* When Tx zero copy is enabled, guest Tx used vring will be updated +* only when corresponding mbuf is freed. Thus, the nb_tx_desc +* (tx_ring_size here) must be small enough so that the driver will +* hit the free threshold easily and free mbufs timely. Otherwise, +* guest Tx vring would be starved. +*/ +if (tx_zero_copy) + tx_ring_size =64 ; + tx_rings = (uint16_t )rte_lcore_count (); retval =validate_num_devices (MAX_DEVICES );
@@ -474,7 +486,8 @@ us_vhost_usage(const char *prgname)" --socket-file: The path of the socket file.\n" " --tx-csum [0|1] disable/enable TX checksum offload.\n" " --tso [0|1] disable/enable TCP segment offload.\n" -" --client register a vhost-user socket as client mode.\n" , +" --client register a vhost-user socket as client mode.\n" +" --tx-zero-copy enables Tx zero copy\n" , prgname); }
@@ -500,6 +513,7 @@ us_vhost_parse_args(int argc, char **argv) {"tx-csum", required_argument, NULL, 0}, {"tso", required_argument, NULL, 0}, {"client", no_argument, &client_mode, 1}, + {"tx-zero-copy", no_argument, &tx_zero_copy, 1}, {NULL, 0, 0, 0}, };
@@ -1531,6 +1545,9 @@ main(int argc, char *argv[])if (client_mode) flags |=RTE_VHOST_USER_CLIENT ; +if (tx_zero_copy) + flags |=RTE_VHOST_USER_TX_ZERO_COPY ; +/* Register vhost user driver to handle vhost messages. */ for (i =0 ; i < nb_sockets; i++) { ret = rte_vhost_driver_register
Signature · 1 line
1.9.0
Re: [PATCH 6/6] examples/vhost: add an option to enable Tx zero copy
2016-08-23 16:10, Yuanhan Liu:
Quoted 7 lines
One thing worth noting while using Tx zero copy is the
nb_tx_deschas to be small enough so that the eth driver will hit the mbuf free threshold easily and thus free mbuf more frequently.The reason behind that is, when Tx zero copy is enabled, guest Tx used vring will be updated only when corresponding mbuf is freed. If mbuf is not freed frequently, the guest Tx vring could be starved.
I think you should explain this behaviour in the doc of the vhost flag.
Re: [PATCH 6/6] examples/vhost: add an option to enable Tx zero copy
Quoted 10 lines · Thomas Monjalon
2016-08-23 16:10, Yuanhan Liu:
Quoted 7 lines
One thing worth noting while using Tx zero copy is the
nb_tx_deschas to be small enough so that the eth driver will hit the mbuf free threshold easily and thus free mbuf more frequently.The reason behind that is, when Tx zero copy is enabled, guest Tx used vring will be updated only when corresponding mbuf is freed. If mbuf is not freed frequently, the guest Tx vring could be starved.
I think you should explain this behaviour in the doc of the vhost flag.
Agreed. Will do it in v2.
--yliu
Re: [PATCH 6/6] examples/vhost: add an option to enable Tx zero copy
In reply to Yuanhan LiuQuoted 31 lines · Yuanhan Liu
Add an option, --tx-zero-copy, to enable Tx zero copy.
One thing worth noting while using Tx zero copy is the
nb_tx_deschas to be small enough so that the eth driver will hit the mbuf free threshold easily and thus free mbuf more frequently.The reason behind that is, when Tx zero copy is enabled, guest Tx used vring will be updated only when corresponding mbuf is freed. If mbuf is not freed frequently, the guest Tx vring could be starved.
Signed-off-by Yuanhan Liu <[email protected]>--- examples/vhost/main.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)examples/vhost/main.c +1
-0@@ -130,6 +130,7 @@ static uint32_t enable_tx_csum;static uint32_t enable_tso;static int client_mode; +static int tx_zero_copy;/* Specify timeout (in useconds) between retries on RX. */ static uint32_t burst_rx_delay_time =BURST_RX_WAIT_US ;@@ -297,6 +298,17 @@ port_init(uint8_t port) rx_ring_size =RTE_TEST_RX_DESC_DEFAULT ; tx_ring_size =RTE_TEST_TX_DESC_DEFAULT ;
examples/vhost/main.c port_init(uint8_t port) · line 310
Quoted 10 lines
examples/vhost/main.c +10
-0+ +/* +* When Tx zero copy is enabled, guest Tx used vring will be updated +* only when corresponding mbuf is freed. Thus, the nb_tx_desc +* (tx_ring_size here) must be small enough so that the driver will +* hit the free threshold easily and free mbufs timely. Otherwise, +* guest Tx vring would be starved. +*/ +if (tx_zero_copy) + tx_ring_size =64 ;
I have a concern about more complex applications, where the mbufs might not be consumed sequentially.
If one mbuf gets stuck for a while, whereas all others are consumed, we would face starvation.
For example, the packet is to be routed to a VM, which is paused, and the routing thread keeps retrying to enqueue the packet for a while.
Anyway, this feature is optional and off by default, so having the feature applied is not a blocker.
Thanks!
Maxime
Re: [PATCH 6/6] examples/vhost: add an option to enable Tx zero copy
Quoted 13 lines · Maxime Coquelin
Quoted 13 lines
/* Specify timeout (in useconds) between retries on RX. */ static
uint32_tburst_rx_delay_time=BURST_RX_WAIT_US;examples/vhost/main.c +7
-0@@ -297,6 +298,17 @@ port_init(uint8_t port) rx_ring_size =RTE_TEST_RX_DESC_DEFAULT ; tx_ring_size =RTE_TEST_TX_DESC_DEFAULT ; + +/* +* When Tx zero copy is enabled, guest Tx used vring will be updated +* only when corresponding mbuf is freed. Thus, the nb_tx_desc +* (tx_ring_size here) must be small enough so that the driver will +* hit the free threshold easily and free mbufs timely. Otherwise, +* guest Tx vring would be starved.
Quoted 8 lines
examples/vhost/main.c port_init(uint8_t port) · line 310
Quoted 3 lines
examples/vhost/main.c +3
-0+*/ +if (tx_zero_copy) + tx_ring_size =64 ;I have a concern about more complex applications, where the mbufs might not be consumed sequentially.
If one mbuf gets stuck for a while, whereas all others are consumed, we would face starvation.
I guess that exactly belongs to the worst case I mentioned in another email. That's why I think we should not break the loop when a head mbuf is not consumed.
--yliu
Quoted 8 lines
For example, the packet is to be routed to a VM, which is paused, and the routing thread keeps retrying to enqueue the packet for a while.
Anyway, this feature is optional and off by default, so having the feature applied is not a blocker.
Thanks!
Maxime
Quoted 17 lines · Yuanhan Liu
This patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag,
RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
Quoted 7 lines
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
For VM2VM iperf test case, it's even better: about 70% boost.
This is indeed impressive.
Somewhere else, you mention that there is a small regression with small packets. Do you have some figures to share?
Also, with this feature OFF, do you see some regressions for both small and bigger packets?
Thanks,
Maxime
Quoted 20 lines
---
Yuanhan Liu (6):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for Tx
vhost: add Tx zero copy
vhost: add a flag to enable Tx zero copy examples/vhost: add an option to enable Tx zero copydoc/guides/prog_guide/vhost_lib.rst | 7 +- examples/vhost/main.c | 19 ++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 103 +++++++++---- lib/librte_vhost/vhost_user.c | 297 +++++++++++++++++++++++------------- lib/librte_vhost/virtio_net.c | 188 +++++++++++++++++++---- 8 files changed, 472 insertions(+), 160 deletions(-)
Quoted 26 lines · Maxime Coquelin
Quoted 23 lines · Yuanhan Liu
This patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag,
RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
Quoted 5 lines
Quoted 1 line
For VM2VM iperf test case, it's even better: about 70% boost.
This is indeed impressive.
Somewhere else, you mention that there is a small regression with small packets. Do you have some figures to share?
It could be 15% drop for PVP case with 64B packet size. The test topo is:
nic 0 --> VM Rx --> VM Tx --> nic 0
Put simply, I run vhost-switch example in the host and run testpmd in the guest.
Though the number looks big, I don't think it's an issue. First of all, it's disabled by default. Secondly, if you want to enable it, you should be certain that the packet size is normally big, otherwise, you should not bother to try with zero copy.
Quoted 2 lines
Also, with this feature OFF, do you see some regressions for both small and bigger packets?
Good question. I didn't check it on purpose, but I did try when it's disabled, the number I got is pretty the same as the one I got without this feature. So, I would say I don't see regressions. Anyway, I could do more tests to make sure.
--yliu
BTW, I really appreicate your efforts on reviewing this patchset.
It would be great if you could take some time to review my another patchset :)
[PATCH 0/7] vhost: vhost-cuse removal and code path refactoring
It touchs a large of code base, that I wish I could apply it ASAP. So that the chance a later patch will introduce conflicts is small.
--yliu
Quoted 54 lines · Yuanhan Liu
Quoted 26 lines · Maxime Coquelin
Quoted 23 lines · Yuanhan Liu
This patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag,
RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
Quoted 5 lines
Quoted 1 line
For VM2VM iperf test case, it's even better: about 70% boost.
This is indeed impressive.
Somewhere else, you mention that there is a small regression with small packets. Do you have some figures to share?It could be 15% drop for PVP case with 64B packet size. The test topo is:
nic 0 --> VM Rx --> VM Tx --> nic 0Put simply, I run vhost-switch example in the host and run testpmd in the guest.
Though the number looks big, I don't think it's an issue. First of all, it's disabled by default. Secondly, if you want to enable it, you should be certain that the packet size is normally big, otherwise, you should not bother to try with zero copy.
Quoted 2 lines
Also, with this feature OFF, do you see some regressions for both small and bigger packets?
Good question. I didn't check it on purpose, but I did try when it's disabled, the number I got is pretty the same as the one I got without this feature. So, I would say I don't see regressions. Anyway, I could do more tests to make sure.
--yliu
Quoted 9 lines · Yuanhan Liu
BTW, I really appreicate your efforts on reviewing this patchset.
It would be great if you could take some time to review my another patchset :)
[PATCH 0/7] vhost: vhost-cuse removal and code path refactoring
It touchs a large of code base, that I wish I could apply it ASAP. So that the chance a later patch will introduce conflicts is small.
Sure, I will try to review it by tomorrow morning (CET).
REgards,
Maxime
Quoted 58 lines
--yliuQuoted 54 lines · Yuanhan Liu
Quoted 26 lines · Maxime Coquelin
Quoted 23 lines · Yuanhan Liu
This patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag,
RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
Quoted 5 lines
Quoted 1 line
For VM2VM iperf test case, it's even better: about 70% boost.
This is indeed impressive.
Somewhere else, you mention that there is a small regression with small packets. Do you have some figures to share?It could be 15% drop for PVP case with 64B packet size. The test topo is:
nic 0 --> VM Rx --> VM Tx --> nic 0Put simply, I run vhost-switch example in the host and run testpmd in the guest.
Though the number looks big, I don't think it's an issue. First of all, it's disabled by default. Secondly, if you want to enable it, you should be certain that the packet size is normally big, otherwise, you should not bother to try with zero copy.
Quoted 2 lines
Also, with this feature OFF, do you see some regressions for both small and bigger packets?
Good question. I didn't check it on purpose, but I did try when it's disabled, the number I got is pretty the same as the one I got without this feature. So, I would say I don't see regressions. Anyway, I could do more tests to make sure.
--yliu
I just ran a PVP test, nic receive packets then forwards to vhost PMD, and virtio user interface. I didn't see any performance gains in this scenario. All packet size from 64B to 1518B performance haven't got benefit from this patchset, and in fact, the performance dropped a lot before 1280B, and similar at 1518B.
The TX/RX desc setting is " txd=64, rxd=128" for TX-zero-copy enabled case. For TX-zero-copy disabled case, I just ran default testpmd(txd=512, rxd=128) without the patch. Could you help check if NIC2VM case?
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH 0/6] vhost: add Tx zero copy support
This patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag, RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.
Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
For VM2VM iperf test case, it's even better: about 70% boost.
---
Yuanhan Liu (6):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for Tx
vhost: add Tx zero copy
vhost: add a flag to enable Tx zero copy examples/vhost: add an option to enable Tx zero copy
doc/guides/prog_guide/vhost_lib.rst | 7 +- examples/vhost/main.c | 19 ++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 103 +++++++++---- lib/librte_vhost/vhost_user.c | 297 +++++++++++++++++++++++------------- lib/librte_vhost/virtio_net.c | 188 +++++++++++++++++++---- 8 files changed, 472 insertions(+), 160 deletions(-)
--
1.9.0
Btw, some good news: if I run a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields ~50% performance boost for packet size 1518B, but this case is without NIC. And similar case as vhost<-->virtio loopback, we can see ~10% performance gains at 1518B without NIC.
Some bad news: If with the patch, I noticed a 3%-7% performance drop if zero-copy=0 compared with current DPDK(e.g: 16.07) at vhost/virtio loopback and vhost RX only + virtio TX only. Seems the patch will Impact the zero-copy=0 performance a little.
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Xu, Qian Q
Sent: Monday, August 29, 2016 4:33 PM
To: Yuanhan Liu <[email protected]>; [email protected]
Cc: Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH 0/6] vhost: add Tx zero copy support
I just ran a PVP test, nic receive packets then forwards to vhost PMD, and virtio user interface. I didn't see any performance gains in this scenario. All packet size from 64B to 1518B performance haven't got benefit from this patchset, and in fact, the performance dropped a lot before 1280B, and similar at 1518B. The TX/RX desc setting is " txd=64, rxd=128" for TX-zero-copy enabled case. For TX-zero-copy disabled case, I just ran default testpmd(txd=512, rxd=128) without the patch.
Could you help check if NIC2VM case?
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH 0/6] vhost: add Tx zero copy support
This patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag, RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.
Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
For VM2VM iperf test case, it's even better: about 70% boost.
---
Yuanhan Liu (6):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for Tx
vhost: add Tx zero copy
vhost: add a flag to enable Tx zero copy examples/vhost: add an option to enable Tx zero copy
doc/guides/prog_guide/vhost_lib.rst | 7 +- examples/vhost/main.c | 19 ++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 103 +++++++++---- lib/librte_vhost/vhost_user.c | 297 +++++++++++++++++++++++------------- lib/librte_vhost/virtio_net.c | 188 +++++++++++++++++++---- 8 files changed, 472 insertions(+), 160 deletions(-)
--
1.9.0
Quoted 5 lines · Qian Q
Btw, some good news: if I run a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields ~50% performance boost for packet size 1518B, but this case is without NIC. And similar case as vhost<-->virtio loopback, we can see ~10% performance gains at 1518B without NIC.
Some bad news: If with the patch, I noticed a 3%-7% performance drop if zero-copy=0 compared with current DPDK(e.g: 16.07) at vhost/virtio loopback and vhost RX only + virtio TX only. Seems the patch will Impact the zero-copy=0 performance a little.
There are some follow up discussion internally, the 3%-7% drop reported by Qian when zero-copy is not enabled is acutally due to the fluctuation. So, a false alarm.
--yliu
Quoted 2 lines · Qian Q
I just ran a PVP test, nic receive packets then forwards to vhost PMD, and virtio user interface. I didn't see any performance gains in this scenario. All packet size from 64B to 1518B performance haven't got benefit from this patchset, and in fact, the performance dropped a lot before 1280B, and similar at 1518B.
40G nic?
Quoted 1 line
The TX/RX desc setting is " txd=64, rxd=128"
Try it with "txd=128", you should be able to set that value since the vhost Tx indirect patch is merged.
--yliu
Quoted 48 lines
for TX-zero-copy enabled case. For TX-zero-copy disabled case, I just ran default
testpmd(txd=512, rxd=128)without the patch. Could you help check if NIC2VM case?-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Tuesday, August 23, 2016 4:11 PM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH 0/6] vhost: add Tx zero copy supportThis patch set enables vhost Tx zero copy. The majority work goes to patch 4: vhost: add Tx zero copy.
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag,
RTE_VHOST_USER_TX_ZERO_COPY, to enable Tx zero copy, which is disabled by default.Few more TODOs are left, including handling a desc buf that is across two physical pages, updating release note, etc. Those will be fixed in later version. For now, here is a simple one that hopefully it shows the idea clearly.
I did some quick tests, the performance gain is quite impressive.
For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 40+% performance boost for packet size 1400B.
For VM2VM iperf test case, it's even better: about 70% boost.
---
Yuanhan Liu (6):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for Tx
vhost: add Tx zero copy
vhost: add a flag to enable Tx zero copy examples/vhost: add an option to enable Tx zero copydoc/guides/prog_guide/vhost_lib.rst | 7 +- examples/vhost/main.c | 19 ++- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 103 +++++++++---- lib/librte_vhost/vhost_user.c | 297 +++++++++++++++++++++++------------- lib/librte_vhost/virtio_net.c | 188 +++++++++++++++++++---- 8 files changed, 472 insertions(+), 160 deletions(-)--
1.9.0
v2 cover vhost: add dequeue zero copy support
In reply to Yuanhan Liuv2: - renamed "tx zero copy" to "dequeue zero copy", to reduce confusions.
- hnadle the case that a desc buf might across 2 host phys pages
- use MAP_POPULATE to let kernel populate the page tables
- updated release note
- doc-ed the limitations for the vm2nic case
- merge 2 continuous guest phys memory region
- and few more trivial changes, please see them in the corresponding patches
This patch set enables vhost dequeue zero copy. The majority work goes to patch 4: "vhost: add dequeue zero copy".
The basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag, RTE_VHOST_USER_DEQUEUE_ZERO_COPY, to enable dequeue zero copy, which is disabled by default.
The performance gain is quite impressive. For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 50+% performance boost for packet size 1500B. For VM2VM iperf test case, it's even better: about 70% boost.
For small packets, the performance is worse (it's expected, as the extra overhead introduced by zero copy outweighs the benefits from saving few bytes copy).
---
Yuanhan Liu (7):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for dequeue
vhost: add dequeue zero copy
vhost: add a flag to enable dequeue zero copy examples/vhost: add an option to enable dequeue zero copy net/vhost: add an option to enable dequeue zero copy
doc/guides/prog_guide/vhost_lib.rst | 35 +++- doc/guides/rel_notes/release_16_11.rst | 11 ++ drivers/net/vhost/rte_eth_vhost.c | 13 ++ examples/vhost/main.c | 19 +- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 102 ++++++++--- lib/librte_vhost/vhost_user.c | 315 ++++++++++++++++++++++----------- lib/librte_vhost/virtio_net.c | 192 +++++++++++++++++--- 10 files changed, 543 insertions(+), 162 deletions(-)
Signature · 1 line
1.9.0
PATCH v2 1/7 vhost: simplify memory regions handling
Due to history reason (that vhost-cuse comes before vhost-user), some fields for maintaining the vhost-user memory mappings (such as mmapped address and size, with those we then can unmap on destroy) are kept in "orig_region_map" struct, a structure that is defined only in vhost-user source file.
The right way to go is to remove the structure and move all those fields into virtio_memory_region struct. But we simply can't do that before, because it breaks the ABI.
Now, thanks to the ABI refactoring, it's never been a blocking issue any more. And here it goes: this patch removes orig_region_map and redefines virtio_memory_region, to include all necessary info.
With that, we can simplify the guest/host address convert a bit.
--- lib/librte_vhost/vhost.h | 49 ++++++------ lib/librte_vhost/vhost_user.c | 173 +++++++++++++++++------------------------- 2 files changed, 91 insertions(+), 131 deletions(-)
lib/librte_vhost/vhost.h +22 -27
@@ -143,12 +143,14 @@ struct virtio_net {* Information relating to memory regions including offsets to * addresses in QEMUs memory file. */ -struct virtio_memory_regions { -uint64_t guest_phys_address; -uint64_t guest_phys_address_end; -uint64_t memory_size; -uint64_t userspace_address; -uint64_t address_offset; +struct virtio_memory_region { +uint64_t guest_phys_addr; +uint64_t guest_user_addr; +uint64_t host_user_addr; +uint64_t size; +void *mmap_addr; +uint64_t mmap_size; +int fd; };
@@ -156,12 +158,8 @@ struct virtio_memory_regions {* Memory structure includes region and mapping information. */ struct virtio_memory { -/* Base QEMU userspace address of the memory file. */ -uint64_t base_address; -uint64_t mapped_address; -uint64_t mapped_size;uint32_t nregions; -struct virtio_memory_regions regions[0 ]; +struct virtio_memory_region regions[0 ]; };
@@ -200,26 +198,23 @@ extern uint64_t VHOST_FEATURES;#define MAX_VHOST_DEVICE 1024extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE ]; -/** -* Function to convert guest physical addresses to vhost virtual addresses. -* This is used to convert guest virtio buffer addresses. -*/ +/* Convert guest physical Address to host virtual address */ static inline uint64_t __attribute__((always_inline)) -gpa_to_vva (struct virtio_net *dev,uint64_t guest_pa) +gpa_to_vva (struct virtio_net *dev,uint64_t gpa) { -struct virtio_memory_regions *region; -uint32_t regionidx; -uint64_t vhost_va =0 ; - -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((guest_pa >= region->guest_phys_address) && - (guest_pa <= region->guest_phys_address_end)) { - vhost_va = region->address_offset + guest_pa; -break ; +struct virtio_memory_region *reg; +uint32_t i; + +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (gpa >= reg->guest_phys_addr && + gpa < reg->guest_phys_addr + reg->size) { +return gpa - reg->guest_phys_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; }struct virtio_net_device_ops const *notify_ops;
lib/librte_vhost/vhost_user.c +69 -104
251 lines not on this page · raw message
Signature · 1 line
1.9.0
PATCH v2 2/7 vhost: get guest/host physical address mappings
In reply to Yuanhan LiuSo that we can convert a guest physical address to host physical address, which will be used in later Tx zero copy implementation.
MAP_POPULATE is set while mmaping guest memory regions, to make sure the page tables are setup and then rte_mem_virt2phy() could yield proper physical address.
---
v2: - use MAP_POPULATE option to make sure the page table will
be already setup while getting the phys address
- do a simple merge if the last 2 pages are continuous
- dump guest pages only in debug mode
--- lib/librte_vhost/vhost.h | 30 +++++++++++++ lib/librte_vhost/vhost_user.c | 100 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-)
lib/librte_vhost/vhost.h +30 -0
@@ -114,6 +114,12 @@ struct vhost_virtqueue {#define VIRTIO_F_VERSION_1 32#endif +struct guest_page { +uint64_t guest_phys_addr; +uint64_t host_phys_addr; +uint64_t size; +}; +/** * Device structure contains all configuration information relating * to the device.
@@ -137,6 +143,10 @@ struct virtio_net {uint64_t log_addr;struct ether_addr mac; +uint32_t nr_guest_pages; +uint32_t max_guest_pages; +struct guest_page *guest_pages; + } __rte_cache_aligned;/**
@@ -217,6 +227,26 @@ gpa_to_vva(struct virtio_net *dev, uint64_t gpa)return 0 ; } +/* Convert guest physical address to host physical address */ +static inline phys_addr_t __attribute__((always_inline)) +gpa_to_hpa (struct virtio_net *dev,uint64_t gpa,uint64_t size) +{ +uint32_t i; +struct guest_page *page; + +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + +if (gpa >= page->guest_phys_addr && + gpa + size < page->guest_phys_addr + page->size) { +return gpa - page->guest_phys_addr + + page->host_phys_addr; + } + } + +return 0 ; +} +struct virtio_net_device_ops const *notify_ops;struct virtio_net *get_device (int vid);
lib/librte_vhost/vhost_user.c +98 -2
130 lines not on this page · raw message
Signature · 1 line
1.9.0
Re: [PATCH v2 2/7] vhost: get guest/host physical address mappings
Quoted 13 lines · Yuanhan Liu
So that we can convert a guest physical address to host physical address, which will be used in later Tx zero copy implementation.
MAP_POPULATEis set while mmaping guest memory regions, to make sure the page tables are setup and thenrte_mem_virt2phy()could yield proper physical address.Signed-off-by Yuanhan Liu <[email protected]>---
v2: - use
MAP_POPULATEoption to make sure the page table will
be already setup while getting the phys address
Quoted 7 lines
- do a simple merge if the last 2 pages are continuous
- dump guest pages only in debug mode
--- lib/librte_vhost/vhost.h | 30 +++++++++++++ lib/librte_vhost/vhost_user.c | 100 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-)
Thanks,
Maxime
PATCH v2 3/7 vhost: introduce last avail idx for dequeue
In reply to Yuanhan LiuSo far, we retrieve both the used ring and avail ring idx by the var last_used_idx; it won't be a problem because the used ring is updated immediately after those avail entries are consumed.
But that's not true when dequeue zero copy is enabled, that used ring is updated only when the mbuf is consumed. Thus, we need use another var to note the last avail ring idx we have consumed.
Therefore, last_avail_idx is introduced.
--- lib/librte_vhost/vhost.h | 2 +- lib/librte_vhost/vhost_user.c | 6 ++++-- lib/librte_vhost/virtio_net.c | 19 +++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-)
lib/librte_vhost/vhost.h +1 -1
@@ -70,7 +70,7 @@ struct vhost_virtqueue {struct vring_used *used;uint32_t size; -/* Last index used on the available ring */ +uint16_t last_avail_idx;volatile uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
lib/librte_vhost/vhost_user.c +4 -2
@@ -343,7 +343,8 @@ vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)"last_used_idx (%u) and vq->used->idx (%u) mismatches; " "some packets maybe resent for Tx and dropped for Rx\n" , vq->last_used_idx, vq->used->idx); - vq->last_used_idx = vq->used->idx; + vq->last_used_idx = vq->used->idx; + vq->last_avail_idx = vq->used->idx; } vq->log_guest_addr = addr->log_guest_addr;
@@ -367,7 +368,8 @@ static int vhost_user_set_vring_base(struct virtio_net *dev, struct vhost_vring_state *state) { - dev->virtqueue[state->index]->last_used_idx = state->num; + dev->virtqueue[state->index]->last_used_idx = state->num; + dev->virtqueue[state->index]->last_avail_idx = state->num;return 0 ; }
lib/librte_vhost/virtio_net.c +11 -8
@@ -846,16 +846,17 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, } } - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - free_entries = avail_idx - vq->last_used_idx; + free_entries = *((volatile uint16_t *)&vq->avail->idx) - + vq->last_avail_idx;if (free_entries ==0 ) goto out;LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -/* Prefetch available ring to retrieve head indexes. */ - used_idx = vq->last_used_idx & (vq->size -1 ); -rte_prefetch0 (&vq->avail->ring[used_idx]); +/* Prefetch available and used ring */ + avail_idx = vq->last_avail_idx & (vq->size -1 ); + used_idx = vq->last_used_idx & (vq->size -1 ); +rte_prefetch0 (&vq->avail->ring[avail_idx]);rte_prefetch0 (&vq->used->ring[used_idx]); count =RTE_MIN (count,MAX_PKT_BURST );
@@ -865,8 +866,9 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,/* Retrieve all of the head indexes first to avoid caching issues. */ for (i =0 ; i < count; i++) { - used_idx = (vq->last_used_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; + avail_idx = (vq->last_avail_idx + i) & (vq->size -1 ); + used_idx = (vq->last_used_idx + i) & (vq->size -1 ); + desc_indexes[i] = vq->avail->ring[avail_idx]; vq->used->ring[used_idx].id = desc_indexes[i]; vq->used->ring[used_idx].len =0 ;
@@ -900,7 +902,8 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,rte_smp_wmb ();rte_smp_rmb (); vq->used->idx += i; - vq->last_used_idx += i; + vq->last_avail_idx += i; + vq->last_used_idx += i;vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx),sizeof (vq->used->idx));
Signature · 1 line
1.9.0
PATCH v2 4/7 vhost: add dequeue zero copy
In reply to Yuanhan LiuThe basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough tests show about 50% perfomance boost for packet size 1500B. For small packets, (e.g. 64B), it actually slows a bit down (well, it could up to 15%). That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
---
v2: - use unlikely/likely for dequeue_zero_copy check, as it's not enabled
by default, as well as it has some limitations in vm2nic case.
- handle the case that a desc buf might across 2 host phys pages
- reset nr_zmbuf to 0 at set_vring_num
- set the zmbuf_size to vq->size, but not the double of it.
--- lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 22 +++++- lib/librte_vhost/vhost_user.c | 42 +++++++++- lib/librte_vhost/virtio_net.c | 173 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 219 insertions(+), 20 deletions(-)
lib/librte_vhost/vhost.c +2 -0
@@ -141,6 +141,8 @@ init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)/* always set the default vq pair to enabled */ if (qp_idx ==0 ) vq->enabled =1 ; + +TAILQ_INIT (&vq->zmbuf_list); } static void
lib/librte_vhost/vhost.h +21 -1
@@ -36,6 +36,7 @@#include <stdint.h> #include <stdio.h> #include <sys/types.h> +#include <sys/queue.h> #include <unistd.h> #include <linux/vhost.h>
@@ -61,6 +62,19 @@ struct buf_vector {uint32_t desc_idx; }; +/* +* A structure to hold some fields needed in zero copy code path, +* mainly for associating an mbuf with the right desc_idx. +*/ +struct zcopy_mbuf { +struct rte_mbuf *mbuf; +uint32_t desc_idx; +uint16_t in_use; + +TAILQ_ENTRY (zcopy_mbuf) next; +}; +TAILQ_HEAD (zcopy_mbuf_list, zcopy_mbuf); +/** * Structure contains variables relevant to RX/TX virtqueues. */
@@ -85,6 +99,12 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +uint16_t nr_zmbuf; +uint16_t zmbuf_size; +uint16_t last_zmbuf_idx; +struct zcopy_mbuf *zmbufs; +struct zcopy_mbuf_list zmbuf_list; } __rte_cache_aligned;/* Old kernels have no such macro defined */
@@ -135,6 +155,7 @@ struct virtio_net {/* to tell if we need broadcast rarp packet */ rte_atomic16_t broadcast_rarp;uint32_t virt_qp_nb; +int dequeue_zero_copy;struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS *2 ];#define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)char ifname[IF_NAME_SZ ];
@@ -146,7 +167,6 @@ struct virtio_net {uint32_t nr_guest_pages;uint32_t max_guest_pages;struct guest_page *guest_pages; - } __rte_cache_aligned;/**
lib/librte_vhost/vhost_user.c +41 -1
65 lines not on this page · raw message
lib/librte_vhost/virtio_net.c +155 -18
219 lines not on this page · raw message
Signature · 1 line
1.9.0
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
Quoted 41 lines · Yuanhan Liu
The basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of
rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the
rte_vhost_dequeue_burstentrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.- "struct
zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.Dequeue zero copy is introduced for performance reason, and some rough tests show about 50% perfomance boost for packet size 1500B. For small packets, (e.g. 64B), it actually slows a bit down (well, it could up to 15%). That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
Signed-off-by Yuanhan Liu <[email protected]>---
v2: - use unlikely/likely for
dequeue_zero_copycheck, as it's not enabled
by default, as well as it has some limitations in vm2nic case.- handle the case that a desc buf might across 2 host phys pages
- reset
nr_zmbufto 0 atset_vring_num
Quoted 7 lines
- set the
zmbuf_sizetovq->size, but not the double of it.--- lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 22 +++++- lib/librte_vhost/vhost_user.c | 42 +++++++++- lib/librte_vhost/virtio_net.c | 173 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 219 insertions(+), 20 deletions(-)
Thanks,
Maxime
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
In reply to Yuanhan LiuSee the bottom.
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Friday, September 23, 2016 5:13 AM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 4/7] vhost: add dequeue zero copy
The basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough tests show about 50% perfomance boost for packet size 1500B. For small packets, (e.g. 64B), it actually slows a bit down (well, it could up to 15%). That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
---
v2: - use unlikely/likely for dequeue_zero_copy check, as it's not enabled
by default, as well as it has some limitations in vm2nic case.
- handle the case that a desc buf might across 2 host phys pages
- reset nr_zmbuf to 0 at set_vring_num
- set the zmbuf_size to vq->size, but not the double of it.
--- lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 22 +++++- lib/librte_vhost/vhost_user.c | 42 +++++++++- lib/librte_vhost/virtio_net.c | 173 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 219 insertions(+), 20 deletions(-)
lib/librte_vhost/vhost.c +2 -0
@@ -141,6 +141,8 @@ init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)/* always set the default vq pair to enabled */ if (qp_idx ==0 ) vq->enabled =1 ; + +TAILQ_INIT (&vq->zmbuf_list); } static void
lib/librte_vhost/vhost.h +20 -0
@@ -36,6 +36,7 @@#include <stdint.h> #include <stdio.h> #include <sys/types.h> +#include <sys/queue.h> #include <unistd.h> #include <linux/vhost.h>
@@ -61,6 +62,19 @@ struct buf_vector {uint32_t desc_idx; }; +/* +* A structure to hold some fields needed in zero copy code path, +* mainly for associating an mbuf with the right desc_idx. +*/ +struct zcopy_mbuf { +struct rte_mbuf *mbuf; +uint32_t desc_idx; +uint16_t in_use; + +TAILQ_ENTRY (zcopy_mbuf) next; +}; +TAILQ_HEAD (zcopy_mbuf_list, zcopy_mbuf); +/** * Structure contains variables relevant to RX/TX virtqueues. */
@@ -85,6 +99,12 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +uint16_t nr_zmbuf; +uint16_t zmbuf_size; +uint16_t last_zmbuf_idx; +struct zcopy_mbuf *zmbufs; +struct zcopy_mbuf_list zmbuf_list; } __rte_cache_aligned; /* Old kernels have no such macro defined */ @@ -135,6 +155,7 @@ struct virtio_net {
/* to tell if we need broadcast rarp packet */
rte_atomic16_t broadcast_rarp;
uint32_t virt_qp_nb;
+ int dequeue_zero_copy;
struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
#define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
char ifname[IF_NAME_SZ];
@@ -146,7 +167,6 @@ struct virtio_net {
uint32_t nr_guest_pages;
uint32_t max_guest_pages;
struct guest_page *guest_pages;
-
} __rte_cache_aligned;
/**
lib/librte_vhost/vhost_user.c +40 -1
64 lines not on this page · raw message
lib/librte_vhost/virtio_net.c +35 -0
@@ -678,6 +678,43 @@ make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)return 0 ; } +static inline struct zcopy_mbuf *__attribute__((always_inline)) +get_zmbuf(struct vhost_virtqueue *vq) { +uint16_t i; +uint16_t last; +int tries =0 ; + +/* search [last_zmbuf_idx, zmbuf_size) */ + i = vq->last_zmbuf_idx; + last = vq->zmbuf_size; + +again: +for (; i < last; i++) { +if (vq->zmbufs[i].in_use ==0 ) { + vq->last_zmbuf_idx = i +1 ; + vq->zmbufs[i].in_use =1 ; +return &vq->zmbufs[i]; + } + } + + tries++; +if (tries ==1 ) { +/* search [0, last_zmbuf_idx) */ + i =0 ; + last = vq->last_zmbuf_idx; + goto again; + } + +return NULL ; +} + +static inline void __attribute__((always_inline))put_zmbuf (struct +zcopy_mbuf *zmbuf) { + zmbuf->in_use =0 ; +} + static inline int __attribute__((always_inline)) copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mbuf *m, uint16_t desc_idx, @@ -701,6 +738,27 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, if (unlikely(!desc_addr))
return -1;
+ if (unlikely(dev->dequeue_zero_copy)) {
+ struct zcopy_mbuf *zmbuf;
+
+ zmbuf = get_zmbuf(vq);
+ if (!zmbuf)
+ return -1;
+ zmbuf->mbuf = m;
+ zmbuf->desc_idx = desc_idx;
+
+ /*
+ * Pin lock the mbuf; we will check later to see whether
+ * the mbuf is freed (when we are the last user) or not.
+ * If that's the case, we then could update the used ring
+ * safely.
+ */
+ rte_mbuf_refcnt_update(m, 1);
+
+ vq->nr_zmbuf += 1;
+ TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
+ }
+
hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr); rte_prefetch0(hdr);
this function copy_desc_to_mbuf has changed on the dpdk-next-virtio repo. Based on current dpdk-next-virtio repo, the commit ID is as below:
commit b4f7b43cd9d3b6413f41221051d03a23bc5f5fbe
Author: Zhiyong Yang <[email protected]> Date: Thu Sep 29 20:35:49 2016 +0800
Then you will find the parameter "struct vhost_virtqueue *vq" is removed, so if apply your patch on that commit ID, the build will fail, since no vq definition but we used it in the function. Could you check? Thx.
== Build lib/librte_table/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c: In function 'copy_desc_to_mbuf':/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c:745:21: error: 'vq' undeclared (first use in this function)
zmbuf = get_zmbuf(vq);
^/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c:745:21: note: each undeclared identifier is reported only once for each function it appears in /home/qxu10/dpdk-zero/mk/internal/rte.compile-pre.mk:138: recipe for target 'virtio_net.o' failed
make[5]: *** [virtio_net.o] Error 1/home/qxu10/dpdk-zero/mk/rte.subdir.mk:61: recipe for target 'librte_vhost' failed
make[4]: *** [librte_vhost] Error 2
make[4]: *** Waiting for unfinished jobs....
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
Quoted 7 lines · Qian Q
this function
copy_desc_to_mbufhas changed on the dpdk-next-virtio repo. Based on current dpdk-next-virtio repo, the commit ID is as below:
commitb4f7b43cd9d3b6413f41221051d03a23bc5f5fbeAuthor: Zhiyong Yang <[email protected]> Date: Thu Sep 29 20:35:49 2016 +0800Then you will find the parameter "struct
vhost_virtqueue*vq" is removed, so if apply your patch on that commit ID, the build will fail, since no vq definition but we used it in the function. Could you check? Thx.
I knew that: a rebase is needed, and I have done the rebase (locally); just haven't sent it out yet.
--yliu
Quoted 13 lines
== Build
lib/librte_table/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c: In function 'copy_desc_to_mbuf':/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c:745:21: error: 'vq' undeclared (first use in this function)zmbuf = get_zmbuf(vq); ^
/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c:745:21: note: each undeclared identifier is reported only once for each function it appears in/home/qxu10/dpdk-zero/mk/internal/rte.compile-pre.mk:138: recipe for target 'virtio_net.o' failed
make[5]: *** [virtio_net.o] Error 1/home/qxu10/dpdk-zero/mk/rte.subdir.mk:61: recipe for target 'librte_vhost' failed
make[4]: *** [librte_vhost] Error 2
make[4]: *** Waiting for unfinished jobs....
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
Good to know. I will try v3. BTW, on the master branch, seems vhost PMD is broken. When we run --vdev 'eth_vhost0,xxxx', then it will report the error that the driver is not supported. V16.07 is OK, but I haven't got time to do git bisect.
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Sunday, October 9, 2016 3:03 AM
To: Xu, Qian Q <[email protected]>
Cc: [email protected]; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v2 4/7] vhost: add dequeue zero copy
Quoted 7 lines · Qian Q
this function
copy_desc_to_mbufhas changed on the dpdk-next-virtio repo. Based on current dpdk-next-virtio repo, the commit ID is as below:
commitb4f7b43cd9d3b6413f41221051d03a23bc5f5fbeAuthor: Zhiyong Yang <[email protected]> Date: Thu Sep 29 20:35:49 2016 +0800Then you will find the parameter "struct
vhost_virtqueue*vq" is removed, so if apply your patch on that commit ID, the build will fail, since no vq definition but we used it in the function. Could you check? Thx.
I knew that: a rebase is needed, and I have done the rebase (locally); just haven't sent it out yet.
--yliu
Quoted 17 lines
== Build
lib/librte_table/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c: In function 'copy_desc_to_mbuf':/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c:745:21: error: 'vq' undeclared (first use in this function)zmbuf = get_zmbuf(vq); ^
/home/qxu10/dpdk-zero/lib/librte_vhost/virtio_net.c:745:21: note: each undeclared identifier is reported only once for each function it appears in/home/qxu10/dpdk-zero/mk/internal/rte.compile-pre.mk:138: recipe for target 'virtio_net.o' failed
make[5]: *** [virtio_net.o] Error 1/home/qxu10/dpdk-zero/mk/rte.subdir.mk:61: recipe for target 'librte_vhost' failed
make[4]: *** [librte_vhost] Error 2
make[4]: *** Waiting for unfinished jobs....
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
Hi Xu,
Quoted 1 line · Qian Q
Good to know. I will try v3. BTW, on the master branch, seems vhost PMD is broken. When we run --vdev 'eth_vhost0,xxxx', then it will report the error that the driver is not supported. V16.07 is OK, but I haven't got time to do git bisect.
Name has been chenged to net_vhost0 for consistency with other PMDs.
Maxime
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
Oh, thx for the info, it's better to have some documentation update in R16.11 release notes.
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, October 10, 2016 11:14 AM
To: Xu, Qian Q <[email protected]>; Yuanhan Liu <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2 4/7] vhost: add dequeue zero copy
Hi Xu,
Quoted 1 line · Qian Q
Good to know. I will try v3. BTW, on the master branch, seems vhost PMD is broken. When we run --vdev 'eth_vhost0,xxxx', then it will report the error that the driver is not supported. V16.07 is OK, but I haven't got time to do git bisect.
Name has been chenged to net_vhost0 for consistency with other PMDs.
Maxime
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
I'm a little concerned if it's a correct way to change the name from release to release, some users may use eth_vhost for the driver, and found it was not working. Do we need also make the consistency b/w releases? What's the name difference b/w eth and net? Other PMDs are not virtual devices. Virtio_user is also a virtual device, do we need to change the name,too?
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Xu, Qian Q
Sent: Monday, October 10, 2016 11:23 AM
To: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2 4/7] vhost: add dequeue zero copy
Oh, thx for the info, it's better to have some documentation update in R16.11 release notes.
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, October 10, 2016 11:14 AM
To: Xu, Qian Q <[email protected]>; Yuanhan Liu <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2 4/7] vhost: add dequeue zero copy
Hi Xu,
Quoted 1 line · Qian Q
Good to know. I will try v3. BTW, on the master branch, seems vhost PMD is broken. When we run --vdev 'eth_vhost0,xxxx', then it will report the error that the driver is not supported. V16.07 is OK, but I haven't got time to do git bisect.
Name has been chenged to net_vhost0 for consistency with other PMDs.
Maxime
Re: [PATCH v2 4/7] vhost: add dequeue zero copy
In reply to Xu, Qian QQuoted 1 line · Qian Q
Oh, thx for the info, it's better to have some documentation update in R16.11 release notes.
I'm not the author of this change, just faced the same situation as yours a a user.
But I agree that if documentation is not aligned, it should be updated.
Maxime
Quoted 16 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, October 10, 2016 11:14 AM
To: Xu, Qian Q <[email protected]>; Yuanhan Liu <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v2 4/7] vhost: add dequeue zero copyHi Xu,
Quoted 1 line · Qian Q
Good to know. I will try v3. BTW, on the master branch, seems vhost PMD is broken. When we run --vdev 'eth_vhost0,xxxx', then it will report the error that the driver is not supported. V16.07 is OK, but I haven't got time to do git bisect.
Name has been chenged to
net_vhost0for consistency with other PMDs.Maxime
PATCH v2 5/7 vhost: add a flag to enable dequeue zero copy
In reply to Yuanhan LiuDequeue zero copy is disabled by default. Here add a new flag ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` to explictily enable it.
---
v2: - update release log
- doc dequeue zero copy in detail
--- doc/guides/prog_guide/vhost_lib.rst | 35 +++++++++++++++++++++++++++++++++- doc/guides/rel_notes/release_16_11.rst | 11 +++++++++++ lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 6 files changed, 62 insertions(+), 1 deletion(-)
doc/guides/prog_guide/vhost_lib.rst +34 -1
@@ -79,7 +79,7 @@ The following is an overview of the Vhost API functions: ``/dev/path`` character device file will be created. For vhost-user server mode, a Unix domain socket file ``path`` will be created. - Currently two flags are supported (these are valid for vhost-user only): + Currently supported flags are (these are valid for vhost-user only): - ``RTE_VHOST_USER_CLIENT``
@@ -97,6 +97,39 @@ The following is an overview of the Vhost API functions: This reconnect option is enabled by default. However, it can be turned off by setting this flag. + - ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` + + Dequeue zero copy will be enabled when this flag is set. It is disabled by + default. + + There are some truths (including limitations) you might want to know while + setting this flag: + + * zero copy is not good for small packets (typically for packet size below + 512). + + * zero copy is really good for VM2VM case. For iperf between two VMs, the + boost could be above 70% (when TSO is enableld). + + * for VM2NIC case, the ``nb_tx_desc`` has to be small enough: <= 64 if virtio + indirect feature is not enabled and <= 128 if it is enabled. + + The is because when dequeue zero copy is enabled, guest Tx used vring will + be updated only when corresponding mbuf is freed. Thus, the nb_tx_desc + has to be small enough so that the PMD driver will run out of available + Tx descriptors and free mbufs timely. Otherwise, guest Tx vring would be + starved. + + * Guest memory should be backended with huge pages to achieve better + performance. Using 1G page size is the best. + + When dequeue zero copy is enabled, the guest phys address and host phys + address mapping has to be established. Using non-huge pages means far + more page segments. To make it simple, DPDK vhost does a linear search + of those segments, thus the fewer the segments, the quicker we will get + the mapping. NOTE: we may speed it by using radix tree searching in + future. + * ``rte_vhost_driver_session_start()`` This function starts the vhost session loop to handle vhost messages. It
doc/guides/rel_notes/release_16_11.rst +11 -0
@@ -36,6 +36,17 @@ New Features This section is a comment. Make sure to start the actual text at the margin. + * **Added vhost-user dequeue zero copy support** + + The copy in dequeue path is saved, which is meant to improve the performance. + In the VM2VM case, the boost is quite impressive. The bigger the packet size, + the bigger performance boost you may get. However, for VM2NIC case, there + are some limitations, yet the boost is not that impressive as VM2VM case. + It may even drop quite a bit for small packets. + + For such reason, this feature is disabled by default. It can be enabled when + ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` flag is given. Check the vhost section + at programming guide for more information. Resolved Issues ---------------
lib/librte_vhost/rte_virtio_net.h +1 -0
@@ -53,6 +53,7 @@#define RTE_VHOST_USER_CLIENT (1ULL << 0)#define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) +#define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)/* Enum for virtqueue management. */ enum {VIRTIO_RXQ ,VIRTIO_TXQ ,VIRTIO_QNUM };
lib/librte_vhost/socket.c +5 -0
@@ -62,6 +62,7 @@ struct vhost_user_socket {int connfd;bool is_server;bool reconnect; +bool dequeue_zero_copy; };struct vhost_user_connection {
@@ -203,6 +204,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket) size =strnlen (vsocket->path,PATH_MAX );vhost_set_ifname (vid, vsocket->path, size); +if (vsocket->dequeue_zero_copy) +vhost_enable_dequeue_zero_copy (vid); +RTE_LOG (INFO ,VHOST_CONFIG ,"new device, handle is %d\n" , vid); vsocket->connfd = fd;
@@ -499,6 +503,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)memset (vsocket,0 ,sizeof (struct vhost_user_socket )); vsocket->path =strdup (path); vsocket->connfd =-1 ; + vsocket->dequeue_zero_copy = flags &RTE_VHOST_USER_DEQUEUE_ZERO_COPY ;if ((flags &RTE_VHOST_USER_CLIENT ) !=0 ) { vsocket->reconnect = !(flags &RTE_VHOST_USER_NO_RECONNECT );
lib/librte_vhost/vhost.c +10 -0
@@ -290,6 +290,16 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) dev->ifname[sizeof (dev->ifname) -1 ] ='\0' ; } +void +vhost_enable_dequeue_zero_copy (int vid) +{ +struct virtio_net *dev =get_device (vid); + +if (dev ==NULL ) +return ; + + dev->dequeue_zero_copy =1 ; +}int rte_vhost_get_numa_node (int vid)
lib/librte_vhost/vhost.h +1 -0
@@ -278,6 +278,7 @@ void vhost_destroy_device(int);int alloc_vring_queue_pair (struct virtio_net *dev,uint32_t qp_idx);void vhost_set_ifname (int ,const char *if_name,unsigned int if_len); +void vhost_enable_dequeue_zero_copy (int vid);/* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
Signature · 1 line
1.9.0
Re: [PATCH v2 5/7] vhost: add a flag to enable dequeue zero copy
Quoted 8 lines · Yuanhan Liu
Dequeue zero copy is disabled by default. Here add a new flag ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` to explictily enable it.
Signed-off-by Yuanhan Liu <[email protected]>---
v2: - update release log
- doc dequeue zero copy in detail
Quoted 8 lines
--- doc/guides/prog_guide/vhost_lib.rst | 35 +++++++++++++++++++++++++++++++++- doc/guides/rel_notes/release_16_11.rst | 11 +++++++++++ lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 6 files changed, 62 insertions(+), 1 deletion(-)
Thanks,
Maxime
PATCH v2 6/7 examples/vhost: add an option to enable dequeue zero copy
In reply to Yuanhan LiuAdd an option, --dequeue-zero-copy, to enable dequeue zero copy.
One thing worth noting while using dequeue zero copy is the nb_tx_desc has to be small enough so that the eth driver will hit the mbuf free threshold easily and thus free mbuf more frequently.
The reason behind that is, when dequeue zero copy is enabled, guest Tx used vring will be updated only when corresponding mbuf is freed. If mbuf is not freed frequently, the guest Tx vring could be starved.
--- examples/vhost/main.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
examples/vhost/main.c +18 -1
@@ -127,6 +127,7 @@ static uint32_t enable_tx_csum;static uint32_t enable_tso;static int client_mode; +static int dequeue_zero_copy;/* Specify timeout (in useconds) between retries on RX. */ static uint32_t burst_rx_delay_time =BURST_RX_WAIT_US ;
@@ -294,6 +295,17 @@ port_init(uint8_t port) rx_ring_size =RTE_TEST_RX_DESC_DEFAULT ; tx_ring_size =RTE_TEST_TX_DESC_DEFAULT ; + +/* +* When dequeue zero copy is enabled, guest Tx used vring will be +* updated only when corresponding mbuf is freed. Thus, the nb_tx_desc +* (tx_ring_size here) must be small enough so that the driver will +* hit the free threshold easily and free mbufs timely. Otherwise, +* guest Tx vring would be starved. +*/ +if (dequeue_zero_copy) + tx_ring_size =64 ; + tx_rings = (uint16_t )rte_lcore_count (); retval =validate_num_devices (MAX_DEVICES );
@@ -470,7 +482,8 @@ us_vhost_usage(const char *prgname)" --socket-file: The path of the socket file.\n" " --tx-csum [0|1] disable/enable TX checksum offload.\n" " --tso [0|1] disable/enable TCP segment offload.\n" -" --client register a vhost-user socket as client mode.\n" , +" --client register a vhost-user socket as client mode.\n" +" --dequeue-zero-copy enables Tx zero copy\n" , prgname); }
@@ -495,6 +508,7 @@ us_vhost_parse_args(int argc, char **argv) {"tx-csum", required_argument, NULL, 0}, {"tso", required_argument, NULL, 0}, {"client", no_argument, &client_mode, 1}, + {"dequeue-zero-copy", no_argument, &dequeue_zero_copy, 1}, {NULL, 0, 0, 0}, };
@@ -1501,6 +1515,9 @@ main(int argc, char *argv[])if (client_mode) flags |=RTE_VHOST_USER_CLIENT ; +if (dequeue_zero_copy) + flags |=RTE_VHOST_USER_DEQUEUE_ZERO_COPY ; +/* Register vhost user driver to handle vhost messages. */ for (i =0 ; i < nb_sockets; i++) { ret = rte_vhost_driver_register
Signature · 1 line
1.9.0
Re: [PATCH v2 6/7] examples/vhost: add an option to enable dequeue zero copy
Quoted 6 lines · Yuanhan Liu
Add an option, --dequeue-zero-copy, to enable dequeue zero copy.
One thing worth noting while using dequeue zero copy is the
nb_tx_deschas to be small enough so that the eth driver will hit the mbuf free threshold easily and thus free mbuf more frequently.
Quoted 8 lines
The reason behind that is, when dequeue zero copy is enabled, guest Tx used vring will be updated only when corresponding mbuf is freed. If mbuf is not freed frequently, the guest Tx vring could be starved.
Signed-off-by Yuanhan Liu <[email protected]>--- examples/vhost/main.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
Thanks,
Maxime
PATCH v2 7/7 net/vhost: add an option to enable dequeue zero copy
In reply to Yuanhan LiuAdd an option, dequeue-zero-copy, to enable this feature in vhost-pmd.
--- drivers/net/vhost/rte_eth_vhost.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/net/vhost/rte_eth_vhost.c +13 -0
@@ -51,6 +51,7 @@#define ETH_VHOST_IFACE_ARG "iface"#define ETH_VHOST_QUEUES_ARG "queues"#define ETH_VHOST_CLIENT_ARG "client" +#define ETH_VHOST_DEQUEUE_ZERO_COPY "dequeue-zero-copy"static const char *drivername ="VHOST PMD" ;
@@ -58,6 +59,7 @@ static const char *valid_arguments[] = {ETH_VHOST_IFACE_ARG ,ETH_VHOST_QUEUES_ARG ,ETH_VHOST_CLIENT_ARG , +ETH_VHOST_DEQUEUE_ZERO_COPY , NULL };
@@ -831,6 +833,7 @@ rte_pmd_vhost_devinit(const char *name, const char *params)uint16_t queues;uint64_t flags =0 ;int client_mode =0 ; +int dequeue_zero_copy =0 ;RTE_LOG (INFO ,PMD ,"Initializing pmd_vhost for %s\n" , name);
@@ -867,6 +870,16 @@ rte_pmd_vhost_devinit(const char *name, const char *params) flags |=RTE_VHOST_USER_CLIENT ; } +if (rte_kvargs_count (kvlist,ETH_VHOST_DEQUEUE_ZERO_COPY ) ==1 ) { + ret =rte_kvargs_process (kvlist,ETH_VHOST_DEQUEUE_ZERO_COPY , + &open_int, &dequeue_zero_copy); +if (ret <0 ) + goto out_free; + +if (dequeue_zero_copy) + flags |=RTE_VHOST_USER_DEQUEUE_ZERO_COPY ; + } +eth_dev_vhost_create (name, iface_name, queues,rte_socket_id (), flags); out_free:
Signature · 1 line
1.9.0
Re: [PATCH v2 7/7] net/vhost: add an option to enable dequeue zero copy
Quoted 6 lines · Yuanhan Liu
Add an option, dequeue-zero-copy, to enable this feature in vhost-pmd.
Signed-off-by Yuanhan Liu <[email protected]>--- drivers/net/vhost/rte_eth_vhost.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Thanks,
Maxime
v3 cover vhost: add dequeue zero copy support
In reply to Yuanhan LiuThis patch set enables vhost dequeue zero copy. The majority work goes to patch 4: "vhost: add dequeue zero copy".
The basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag, RTE_VHOST_USER_DEQUEUE_ZERO_COPY, to enable dequeue zero copy, which is disabled by default.
The performance gain is quite impressive. For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 50+% performance boost for packet size 1500B. For VM2VM iperf test case, it's even better: about 70% boost.
For small packets, the performance is worse (it's expected, as the extra overhead introduced by zero copy outweighs the benefits from saving few bytes copy).
v3: - rebase: mainly for removing conflicts with the Tx indirect patch
- don't update last_used_idx twice for zero-copy mode
- handle two mssiing "Tx -> dequeue" renames in log and usage
v2: - renamed "tx zero copy" to "dequeue zero copy", to reduce confusions.
- hnadle the case that a desc buf might across 2 host phys pages
- use MAP_POPULATE to let kernel populate the page tables
- updated release note
- doc-ed the limitations for the vm2nic case
- merge 2 continuous guest phys memory region
- and few more trivial changes, please see them in the corresponding patches
---
Yuanhan Liu (7):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for dequeue
vhost: add dequeue zero copy
vhost: add a flag to enable dequeue zero copy examples/vhost: add an option to enable dequeue zero copy net/vhost: add an option to enable dequeue zero copy
doc/guides/prog_guide/vhost_lib.rst | 35 +++- doc/guides/rel_notes/release_16_11.rst | 13 ++ drivers/net/vhost/rte_eth_vhost.c | 13 ++ examples/vhost/main.c | 19 +- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 102 ++++++++--- lib/librte_vhost/vhost_user.c | 315 ++++++++++++++++++++++----------- lib/librte_vhost/virtio_net.c | 196 +++++++++++++++++--- 10 files changed, 549 insertions(+), 162 deletions(-)
Signature · 1 line
1.9.0
PATCH v3 1/7 vhost: simplify memory regions handling
Due to history reason (that vhost-cuse comes before vhost-user), some fields for maintaining the vhost-user memory mappings (such as mmapped address and size, with those we then can unmap on destroy) are kept in "orig_region_map" struct, a structure that is defined only in vhost-user source file.
The right way to go is to remove the structure and move all those fields into virtio_memory_region struct. But we simply can't do that before, because it breaks the ABI.
Now, thanks to the ABI refactoring, it's never been a blocking issue any more. And here it goes: this patch removes orig_region_map and redefines virtio_memory_region, to include all necessary info.
With that, we can simplify the guest/host address convert a bit.
--- lib/librte_vhost/vhost.h | 49 ++++++------ lib/librte_vhost/vhost_user.c | 173 +++++++++++++++++------------------------- 2 files changed, 91 insertions(+), 131 deletions(-)
lib/librte_vhost/vhost.h +22 -27
@@ -143,12 +143,14 @@ struct virtio_net {* Information relating to memory regions including offsets to * addresses in QEMUs memory file. */ -struct virtio_memory_regions { -uint64_t guest_phys_address; -uint64_t guest_phys_address_end; -uint64_t memory_size; -uint64_t userspace_address; -uint64_t address_offset; +struct virtio_memory_region { +uint64_t guest_phys_addr; +uint64_t guest_user_addr; +uint64_t host_user_addr; +uint64_t size; +void *mmap_addr; +uint64_t mmap_size; +int fd; };
@@ -156,12 +158,8 @@ struct virtio_memory_regions {* Memory structure includes region and mapping information. */ struct virtio_memory { -/* Base QEMU userspace address of the memory file. */ -uint64_t base_address; -uint64_t mapped_address; -uint64_t mapped_size;uint32_t nregions; -struct virtio_memory_regions regions[0 ]; +struct virtio_memory_region regions[0 ]; };
@@ -200,26 +198,23 @@ extern uint64_t VHOST_FEATURES;#define MAX_VHOST_DEVICE 1024extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE ]; -/** -* Function to convert guest physical addresses to vhost virtual addresses. -* This is used to convert guest virtio buffer addresses. -*/ +/* Convert guest physical Address to host virtual address */ static inline uint64_t __attribute__((always_inline)) -gpa_to_vva (struct virtio_net *dev,uint64_t guest_pa) +gpa_to_vva (struct virtio_net *dev,uint64_t gpa) { -struct virtio_memory_regions *region; -uint32_t regionidx; -uint64_t vhost_va =0 ; - -for (regionidx =0 ; regionidx < dev->mem->nregions; regionidx++) { - region = &dev->mem->regions[regionidx]; -if ((guest_pa >= region->guest_phys_address) && - (guest_pa <= region->guest_phys_address_end)) { - vhost_va = region->address_offset + guest_pa; -break ; +struct virtio_memory_region *reg; +uint32_t i; + +for (i =0 ; i < dev->mem->nregions; i++) { + reg = &dev->mem->regions[i]; +if (gpa >= reg->guest_phys_addr && + gpa < reg->guest_phys_addr + reg->size) { +return gpa - reg->guest_phys_addr + + reg->host_user_addr; } } -return vhost_va; + +return 0 ; }struct virtio_net_device_ops const *notify_ops;
lib/librte_vhost/vhost_user.c +69 -104
251 lines not on this page · raw message
Signature · 1 line
1.9.0
PATCH v3 2/7 vhost: get guest/host physical address mappings
In reply to Yuanhan LiuSo that we can convert a guest physical address to host physical address, which will be used in later Tx zero copy implementation.
MAP_POPULATE is set while mmaping guest memory regions, to make sure the page tables are setup and then rte_mem_virt2phy() could yield proper physical address.
---
v2: - use MAP_POPULATE option to make sure the page table will
be already setup while getting the phys address
- do a simple merge if the last 2 pages are continuous
- dump guest pages only in debug mode
--- lib/librte_vhost/vhost.h | 30 +++++++++++++ lib/librte_vhost/vhost_user.c | 100 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-)
lib/librte_vhost/vhost.h +30 -0
@@ -114,6 +114,12 @@ struct vhost_virtqueue {#define VIRTIO_F_VERSION_1 32#endif +struct guest_page { +uint64_t guest_phys_addr; +uint64_t host_phys_addr; +uint64_t size; +}; +/** * Device structure contains all configuration information relating * to the device.
@@ -137,6 +143,10 @@ struct virtio_net {uint64_t log_addr;struct ether_addr mac; +uint32_t nr_guest_pages; +uint32_t max_guest_pages; +struct guest_page *guest_pages; + } __rte_cache_aligned;/**
@@ -217,6 +227,26 @@ gpa_to_vva(struct virtio_net *dev, uint64_t gpa)return 0 ; } +/* Convert guest physical address to host physical address */ +static inline phys_addr_t __attribute__((always_inline)) +gpa_to_hpa (struct virtio_net *dev,uint64_t gpa,uint64_t size) +{ +uint32_t i; +struct guest_page *page; + +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + +if (gpa >= page->guest_phys_addr && + gpa + size < page->guest_phys_addr + page->size) { +return gpa - page->guest_phys_addr + + page->host_phys_addr; + } + } + +return 0 ; +} +struct virtio_net_device_ops const *notify_ops;struct virtio_net *get_device (int vid);
lib/librte_vhost/vhost_user.c +98 -2
130 lines not on this page · raw message
Signature · 1 line
1.9.0
Re: [PATCH v3 2/7] vhost: get guest/host physical address mappings
lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg) · line 490
Quoted 7 lines · Yuanhan Liu
lib/librte_vhost/vhost_user.c +7
-0+ dev->nr_guest_pages =0 ; +if (!dev->guest_pages) { + dev->max_guest_pages =8 ; + dev->guest_pages =malloc (dev->max_guest_pages * +sizeof (struct guest_page )); + } +
when to free guest_pages ?
Re: [PATCH v3 2/7] vhost: get guest/host physical address mappings
In reply to Yuanhan Liulib/librte_vhost/vhost_user.c vhost_user_set_vring_base(struct virtio_net *dev, · line 415
Quoted 13 lines · Yuanhan Liu
lib/librte_vhost/vhost_user.c +13
-0+static void +add_guest_pages (struct virtio_net *dev,struct virtio_memory_region *reg, +uint64_t page_size) +{ +uint64_t reg_size = reg->size; +uint64_t host_user_addr = reg->host_user_addr; +uint64_t guest_phys_addr = reg->guest_phys_addr; +uint64_t host_phys_addr; +uint64_t size; + + host_phys_addr =rte_mem_virt2phy ((void *)(uintptr_t )host_user_addr); + size = page_size - (guest_phys_addr & (page_size -1 )); + size =RTE_MIN (size, reg_size);
Have you use 1G hugepage to create a VM with 25G memory? When I try, vhost crashed, use below code fixed:
- size = page_size - (guest_phys_addr & (page_size - 1));
- size = RTE_MIN(size, reg_size);
+ size = reg_size % page_size;
PATCH v3 3/7 vhost: introduce last avail idx for dequeue
In reply to Yuanhan LiuSo far, we retrieve both the used ring and avail ring idx by the var last_used_idx; it won't be a problem because the used ring is updated immediately after those avail entries are consumed.
But that's not true when dequeue zero copy is enabled, that used ring is updated only when the mbuf is consumed. Thus, we need use another var to note the last avail ring idx we have consumed.
Therefore, last_avail_idx is introduced.
--- lib/librte_vhost/vhost.h | 2 +- lib/librte_vhost/vhost_user.c | 6 ++++-- lib/librte_vhost/virtio_net.c | 19 +++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-)
lib/librte_vhost/vhost.h +1 -1
@@ -70,7 +70,7 @@ struct vhost_virtqueue {struct vring_used *used;uint32_t size; -/* Last index used on the available ring */ +uint16_t last_avail_idx;volatile uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
lib/librte_vhost/vhost_user.c +4 -2
@@ -343,7 +343,8 @@ vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)"last_used_idx (%u) and vq->used->idx (%u) mismatches; " "some packets maybe resent for Tx and dropped for Rx\n" , vq->last_used_idx, vq->used->idx); - vq->last_used_idx = vq->used->idx; + vq->last_used_idx = vq->used->idx; + vq->last_avail_idx = vq->used->idx; } vq->log_guest_addr = addr->log_guest_addr;
@@ -367,7 +368,8 @@ static int vhost_user_set_vring_base(struct virtio_net *dev, struct vhost_vring_state *state) { - dev->virtqueue[state->index]->last_used_idx = state->num; + dev->virtqueue[state->index]->last_used_idx = state->num; + dev->virtqueue[state->index]->last_avail_idx = state->num;return 0 ; }
lib/librte_vhost/virtio_net.c +11 -8
@@ -851,16 +851,17 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, } } - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - free_entries = avail_idx - vq->last_used_idx; + free_entries = *((volatile uint16_t *)&vq->avail->idx) - + vq->last_avail_idx;if (free_entries ==0 ) goto out;LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -/* Prefetch available ring to retrieve head indexes. */ - used_idx = vq->last_used_idx & (vq->size -1 ); -rte_prefetch0 (&vq->avail->ring[used_idx]); +/* Prefetch available and used ring */ + avail_idx = vq->last_avail_idx & (vq->size -1 ); + used_idx = vq->last_used_idx & (vq->size -1 ); +rte_prefetch0 (&vq->avail->ring[avail_idx]);rte_prefetch0 (&vq->used->ring[used_idx]); count =RTE_MIN (count,MAX_PKT_BURST );
@@ -870,8 +871,9 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,/* Retrieve all of the head indexes first to avoid caching issues. */ for (i =0 ; i < count; i++) { - used_idx = (vq->last_used_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; + avail_idx = (vq->last_avail_idx + i) & (vq->size -1 ); + used_idx = (vq->last_used_idx + i) & (vq->size -1 ); + desc_indexes[i] = vq->avail->ring[avail_idx]; vq->used->ring[used_idx].id = desc_indexes[i]; vq->used->ring[used_idx].len =0 ;
@@ -921,7 +923,8 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,rte_smp_wmb ();rte_smp_rmb (); vq->used->idx += i; - vq->last_used_idx += i; + vq->last_avail_idx += i; + vq->last_used_idx += i;vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx),sizeof (vq->used->idx));
Signature · 1 line
1.9.0
PATCH v3 4/7 vhost: add dequeue zero copy
In reply to Yuanhan LiuThe basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring at the end of rte_vhost_dequeue_burst. Because we don't do the copy here, an update of the used ring would let the driver to reclaim the desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance, to see if the mbuf is freed (when refcnt equals to 1). If that happens, it means we are the last user of this mbuf and we are safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough tests show about 50% perfomance boost for packet size 1500B. For small packets, (e.g. 64B), it actually slows a bit down (well, it could up to 15%). That is expected because this patch introduces some extra works, and it outweighs the benefit from saving few bytes copy.
---
v3: - rebase on top of the indirect enabling patch
- don't update last_used_idx twice for zero-copy
- handle the missing "tx -> dequeue" rename in log message
v2: - rename "Tx zero copy" to "dequeue zero copy" for reducing confusions
- use unlikely/likely for dequeue_zero_copy check, as it's not enabled by default, as well as it has some limitations in vm2nic case.
- handle the case that a desc buf might across 2 host phys pages
- reset nr_zmbuf to 0 at set_vring_num
- set the zmbuf_size to vq->size, but not the double of it.
--- lib/librte_vhost/vhost.c | 2 + lib/librte_vhost/vhost.h | 22 +++++- lib/librte_vhost/vhost_user.c | 42 +++++++++- lib/librte_vhost/virtio_net.c | 179 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 224 insertions(+), 21 deletions(-)
lib/librte_vhost/vhost.c +2 -0
@@ -142,6 +142,8 @@ init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)/* always set the default vq pair to enabled */ if (qp_idx ==0 ) vq->enabled =1 ; + +TAILQ_INIT (&vq->zmbuf_list); } static void
lib/librte_vhost/vhost.h +21 -1
@@ -36,6 +36,7 @@#include <stdint.h> #include <stdio.h> #include <sys/types.h> +#include <sys/queue.h> #include <unistd.h> #include <linux/vhost.h>
@@ -61,6 +62,19 @@ struct buf_vector {uint32_t desc_idx; }; +/* +* A structure to hold some fields needed in zero copy code path, +* mainly for associating an mbuf with the right desc_idx. +*/ +struct zcopy_mbuf { +struct rte_mbuf *mbuf; +uint32_t desc_idx; +uint16_t in_use; + +TAILQ_ENTRY (zcopy_mbuf) next; +}; +TAILQ_HEAD (zcopy_mbuf_list, zcopy_mbuf); +/** * Structure contains variables relevant to RX/TX virtqueues. */
@@ -85,6 +99,12 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +uint16_t nr_zmbuf; +uint16_t zmbuf_size; +uint16_t last_zmbuf_idx; +struct zcopy_mbuf *zmbufs; +struct zcopy_mbuf_list zmbuf_list; } __rte_cache_aligned;/* Old kernels have no such macro defined */
@@ -135,6 +155,7 @@ struct virtio_net {/* to tell if we need broadcast rarp packet */ rte_atomic16_t broadcast_rarp;uint32_t virt_qp_nb; +int dequeue_zero_copy;struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS *2 ];#define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)char ifname[IF_NAME_SZ ];
@@ -146,7 +167,6 @@ struct virtio_net {uint32_t nr_guest_pages;uint32_t max_guest_pages;struct guest_page *guest_pages; - } __rte_cache_aligned;/**
lib/librte_vhost/vhost_user.c +41 -1
65 lines not on this page · raw message
lib/librte_vhost/virtio_net.c +160 -19
224 lines not on this page · raw message
Signature · 1 line
1.9.0
PATCH v3 5/7 vhost: add a flag to enable dequeue zero copy
In reply to Yuanhan LiuDequeue zero copy is disabled by default. Here add a new flag ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` to explictily enable it.
---
v2: - update release log
- doc dequeue zero copy in detail
--- doc/guides/prog_guide/vhost_lib.rst | 35 +++++++++++++++++++++++++++++++++- doc/guides/rel_notes/release_16_11.rst | 13 +++++++++++++ lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 +++++ lib/librte_vhost/vhost.c | 10 ++++++++++ lib/librte_vhost/vhost.h | 1 + 6 files changed, 64 insertions(+), 1 deletion(-)
doc/guides/prog_guide/vhost_lib.rst +34 -1
@@ -79,7 +79,7 @@ The following is an overview of the Vhost API functions: ``/dev/path`` character device file will be created. For vhost-user server mode, a Unix domain socket file ``path`` will be created. - Currently two flags are supported (these are valid for vhost-user only): + Currently supported flags are (these are valid for vhost-user only): - ``RTE_VHOST_USER_CLIENT``
@@ -97,6 +97,39 @@ The following is an overview of the Vhost API functions: This reconnect option is enabled by default. However, it can be turned off by setting this flag. + - ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` + + Dequeue zero copy will be enabled when this flag is set. It is disabled by + default. + + There are some truths (including limitations) you might want to know while + setting this flag: + + * zero copy is not good for small packets (typically for packet size below + 512). + + * zero copy is really good for VM2VM case. For iperf between two VMs, the + boost could be above 70% (when TSO is enableld). + + * for VM2NIC case, the ``nb_tx_desc`` has to be small enough: <= 64 if virtio + indirect feature is not enabled and <= 128 if it is enabled. + + The is because when dequeue zero copy is enabled, guest Tx used vring will + be updated only when corresponding mbuf is freed. Thus, the nb_tx_desc + has to be small enough so that the PMD driver will run out of available + Tx descriptors and free mbufs timely. Otherwise, guest Tx vring would be + starved. + + * Guest memory should be backended with huge pages to achieve better + performance. Using 1G page size is the best. + + When dequeue zero copy is enabled, the guest phys address and host phys + address mapping has to be established. Using non-huge pages means far + more page segments. To make it simple, DPDK vhost does a linear search + of those segments, thus the fewer the segments, the quicker we will get + the mapping. NOTE: we may speed it by using radix tree searching in + future. + * ``rte_vhost_driver_session_start()`` This function starts the vhost session loop to handle vhost messages. It
doc/guides/rel_notes/release_16_11.rst +13 -0
@@ -36,6 +36,19 @@ New Features This section is a comment. Make sure to start the actual text at the margin. +* **Added vhost-user dequeue zero copy support** + + The copy in dequeue path is saved, which is meant to improve the performance. + In the VM2VM case, the boost is quite impressive. The bigger the packet size, + the bigger performance boost you may get. However, for VM2NIC case, there + are some limitations, yet the boost is not that impressive as VM2VM case. + It may even drop quite a bit for small packets. + + For such reason, this feature is disabled by default. It can be enabled when + ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` flag is given. Check the vhost section + at programming guide for more information. + + * **Added vhost-user indirect descriptors support.** If indirect descriptor feature is negotiated, each packet sent by the guest
lib/librte_vhost/rte_virtio_net.h +1 -0
@@ -53,6 +53,7 @@#define RTE_VHOST_USER_CLIENT (1ULL << 0)#define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) +#define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)/* Enum for virtqueue management. */ enum {VIRTIO_RXQ ,VIRTIO_TXQ ,VIRTIO_QNUM };
lib/librte_vhost/socket.c +5 -0
@@ -62,6 +62,7 @@ struct vhost_user_socket {int connfd;bool is_server;bool reconnect; +bool dequeue_zero_copy; };struct vhost_user_connection {
@@ -203,6 +204,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket) size =strnlen (vsocket->path,PATH_MAX );vhost_set_ifname (vid, vsocket->path, size); +if (vsocket->dequeue_zero_copy) +vhost_enable_dequeue_zero_copy (vid); +RTE_LOG (INFO ,VHOST_CONFIG ,"new device, handle is %d\n" , vid); vsocket->connfd = fd;
@@ -499,6 +503,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)memset (vsocket,0 ,sizeof (struct vhost_user_socket )); vsocket->path =strdup (path); vsocket->connfd =-1 ; + vsocket->dequeue_zero_copy = flags &RTE_VHOST_USER_DEQUEUE_ZERO_COPY ;if ((flags &RTE_VHOST_USER_CLIENT ) !=0 ) { vsocket->reconnect = !(flags &RTE_VHOST_USER_NO_RECONNECT );
lib/librte_vhost/vhost.c +10 -0
@@ -291,6 +291,16 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) dev->ifname[sizeof (dev->ifname) -1 ] ='\0' ; } +void +vhost_enable_dequeue_zero_copy (int vid) +{ +struct virtio_net *dev =get_device (vid); + +if (dev ==NULL ) +return ; + + dev->dequeue_zero_copy =1 ; +}int rte_vhost_get_numa_node (int vid)
lib/librte_vhost/vhost.h +1 -0
@@ -278,6 +278,7 @@ void vhost_destroy_device(int);int alloc_vring_queue_pair (struct virtio_net *dev,uint32_t qp_idx);void vhost_set_ifname (int ,const char *if_name,unsigned int if_len); +void vhost_enable_dequeue_zero_copy (int vid);/* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
Signature · 1 line
1.9.0
PATCH v3 6/7 examples/vhost: add an option to enable dequeue zero copy
In reply to Yuanhan LiuAdd an option, --dequeue-zero-copy, to enable dequeue zero copy.
One thing worth noting while using dequeue zero copy is the nb_tx_desc has to be small enough so that the eth driver will hit the mbuf free threshold easily and thus free mbuf more frequently.
The reason behind that is, when dequeue zero copy is enabled, guest Tx used vring will be updated only when corresponding mbuf is freed. If mbuf is not freed frequently, the guest Tx vring could be starved.
---
v3: - handle missing "Tx --> dequeue" renaming in usage
--- examples/vhost/main.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
examples/vhost/main.c +18 -1
@@ -127,6 +127,7 @@ static uint32_t enable_tx_csum;static uint32_t enable_tso;static int client_mode; +static int dequeue_zero_copy;/* Specify timeout (in useconds) between retries on RX. */ static uint32_t burst_rx_delay_time =BURST_RX_WAIT_US ;
@@ -294,6 +295,17 @@ port_init(uint8_t port) rx_ring_size =RTE_TEST_RX_DESC_DEFAULT ; tx_ring_size =RTE_TEST_TX_DESC_DEFAULT ; + +/* +* When dequeue zero copy is enabled, guest Tx used vring will be +* updated only when corresponding mbuf is freed. Thus, the nb_tx_desc +* (tx_ring_size here) must be small enough so that the driver will +* hit the free threshold easily and free mbufs timely. Otherwise, +* guest Tx vring would be starved. +*/ +if (dequeue_zero_copy) + tx_ring_size =64 ; + tx_rings = (uint16_t )rte_lcore_count (); retval =validate_num_devices (MAX_DEVICES );
@@ -470,7 +482,8 @@ us_vhost_usage(const char *prgname)" --socket-file: The path of the socket file.\n" " --tx-csum [0|1] disable/enable TX checksum offload.\n" " --tso [0|1] disable/enable TCP segment offload.\n" -" --client register a vhost-user socket as client mode.\n" , +" --client register a vhost-user socket as client mode.\n" +" --dequeue-zero-copy enables dequeue zero copy\n" , prgname); }
@@ -495,6 +508,7 @@ us_vhost_parse_args(int argc, char **argv) {"tx-csum", required_argument, NULL, 0}, {"tso", required_argument, NULL, 0}, {"client", no_argument, &client_mode, 1}, + {"dequeue-zero-copy", no_argument, &dequeue_zero_copy, 1}, {NULL, 0, 0, 0}, };
@@ -1501,6 +1515,9 @@ main(int argc, char *argv[])if (client_mode) flags |=RTE_VHOST_USER_CLIENT ; +if (dequeue_zero_copy) + flags |=RTE_VHOST_USER_DEQUEUE_ZERO_COPY ; +/* Register vhost user driver to handle vhost messages. */ for (i =0 ; i < nb_sockets; i++) { ret = rte_vhost_driver_register
Signature · 1 line
1.9.0
PATCH v3 7/7 net/vhost: add an option to enable dequeue zero copy
In reply to Yuanhan LiuAdd an option, dequeue-zero-copy, to enable this feature in vhost-pmd.
--- drivers/net/vhost/rte_eth_vhost.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/net/vhost/rte_eth_vhost.c +13 -0
@@ -51,6 +51,7 @@#define ETH_VHOST_IFACE_ARG "iface"#define ETH_VHOST_QUEUES_ARG "queues"#define ETH_VHOST_CLIENT_ARG "client" +#define ETH_VHOST_DEQUEUE_ZERO_COPY "dequeue-zero-copy"static const char *drivername ="VHOST PMD" ;
@@ -58,6 +59,7 @@ static const char *valid_arguments[] = {ETH_VHOST_IFACE_ARG ,ETH_VHOST_QUEUES_ARG ,ETH_VHOST_CLIENT_ARG , +ETH_VHOST_DEQUEUE_ZERO_COPY , NULL };
@@ -1132,6 +1134,7 @@ rte_pmd_vhost_probe(const char *name, const char *params)uint16_t queues;uint64_t flags =0 ;int client_mode =0 ; +int dequeue_zero_copy =0 ;RTE_LOG (INFO ,PMD ,"Initializing pmd_vhost for %s\n" , name);
@@ -1168,6 +1171,16 @@ rte_pmd_vhost_probe(const char *name, const char *params) flags |=RTE_VHOST_USER_CLIENT ; } +if (rte_kvargs_count (kvlist,ETH_VHOST_DEQUEUE_ZERO_COPY ) ==1 ) { + ret =rte_kvargs_process (kvlist,ETH_VHOST_DEQUEUE_ZERO_COPY , + &open_int, &dequeue_zero_copy); +if (ret <0 ) + goto out_free; + +if (dequeue_zero_copy) + flags |=RTE_VHOST_USER_DEQUEUE_ZERO_COPY ; + } +eth_dev_vhost_create (name, iface_name, queues,rte_socket_id (), flags); out_free:
Signature · 1 line
1.9.0
Re: [PATCH v3 0/7] vhost: add dequeue zero copy support
In reply to Yuanhan Liu- Apply patch to dpdk-next-virtio: Pass
- Compile: Pass
- OS: Ubuntu16.04 4.4.0-34-generic
- GCC: 5.4.0
Test Case - Pass, over 20% performance gain for big packet(1024B), and it's designed to improve big packet performance.
- Test case: Without NIC, Vhost dequeuer, virtio TXonly, mergeable=on, then see ~28% performance gains for packet size 1518B; for small packet 64B, similar performance as zero-copy=0.
- Test case: With Intel FVL 40G NIC, run PVP case, txd=128, mergeable=on, for packet size over 1K(1024B), we can see the performance benefits, for example, 1024 will get 18% performance gains; 1518B will get 26% performance gain compared with zero-copy=0, for small packet such as 64B, we will get 15% performance drop which is reasonable, and vhost zero-copy is not applicable for the small packet performance.
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Yuanhan Liu
Sent: Sunday, October 9, 2016 8:28 AM
To: [email protected]
Cc: Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v3 0/7] vhost: add dequeue zero copy support
This patch set enables vhost dequeue zero copy. The majority work goes to patch 4: "vhost: add dequeue zero copy".
The basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag, RTE_VHOST_USER_DEQUEUE_ZERO_COPY, to enable dequeue zero copy, which is disabled by default.
The performance gain is quite impressive. For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 50+% performance boost for packet size 1500B. For VM2VM iperf test case,
it's even better: about 70% boost.
For small packets, the performance is worse (it's expected, as the extra overhead introduced by zero copy outweighs the benefits from saving few bytes copy).
v3: - rebase: mainly for removing conflicts with the Tx indirect patch
- don't update last_used_idx twice for zero-copy mode
- handle two mssiing "Tx -> dequeue" renames in log and usage
v2: - renamed "tx zero copy" to "dequeue zero copy", to reduce confusions.
- hnadle the case that a desc buf might across 2 host phys pages
- use MAP_POPULATE to let kernel populate the page tables
- updated release note
- doc-ed the limitations for the vm2nic case
- merge 2 continuous guest phys memory region
- and few more trivial changes, please see them in the corresponding patches
---
Yuanhan Liu (7):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for dequeue
vhost: add dequeue zero copy
vhost: add a flag to enable dequeue zero copy examples/vhost: add an option to enable dequeue zero copy net/vhost: add an option to enable dequeue zero copy
doc/guides/prog_guide/vhost_lib.rst | 35 +++- doc/guides/rel_notes/release_16_11.rst | 13 ++ drivers/net/vhost/rte_eth_vhost.c | 13 ++ examples/vhost/main.c | 19 +- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 102 ++++++++--- lib/librte_vhost/vhost_user.c | 315 ++++++++++++++++++++++----------- lib/librte_vhost/virtio_net.c | 196 +++++++++++++++++--- 10 files changed, 549 insertions(+), 162 deletions(-)
--
1.9.0
Yuanhan Liu
AppliedRe: [PATCH v3 0/7] vhost: add dequeue zero copy support
In reply to Yuanhan LiuQuoted 2 lines · Yuanhan Liu
This patch set enables vhost dequeue zero copy. The majority work goes to patch 4: "vhost: add dequeue zero copy".
Applied to dpdk-next-virtio.
--yliu
Quoted 56 lines
The basic idea of dequeue zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
The major issue behind that is how and when to update the used ring. You could check the commit log of patch 4 for more details.
Patch 5 introduces a new flag,
RTE_VHOST_USER_DEQUEUE_ZERO_COPY, to enable dequeue zero copy, which is disabled by default.The performance gain is quite impressive. For a simple dequeue workload (running rxonly in vhost-pmd and runnin txonly in guest testpmd), it yields 50+% performance boost for packet size 1500B. For VM2VM iperf test case, it's even better: about 70% boost.
For small packets, the performance is worse (it's expected, as the extra overhead introduced by zero copy outweighs the benefits from saving few bytes copy).
v3: - rebase: mainly for removing conflicts with the Tx indirect patch
- don't updatelast_used_idxtwice for zero-copy mode
- handle two mssiing "Tx -> dequeue" renames in log and usagev2: - renamed "tx zero copy" to "dequeue zero copy", to reduce confusions.
- hnadle the case that a desc buf might across 2 host phys pages
- useMAP_POPULATEto let kernel populate the page tables
- updated release note
- doc-ed the limitations for the vm2nic case
- merge 2 continuous guest phys memory region
- and few more trivial changes, please see them in the corresponding patches---
Yuanhan Liu (7):
vhost: simplify memory regions handling
vhost: get guest/host physical address mappings
vhost: introduce last avail idx for dequeue
vhost: add dequeue zero copy
vhost: add a flag to enable dequeue zero copy examples/vhost: add an option to enable dequeue zero copy net/vhost: add an option to enable dequeue zero copydoc/guides/prog_guide/vhost_lib.rst | 35 +++- doc/guides/rel_notes/release_16_11.rst | 13 ++ drivers/net/vhost/rte_eth_vhost.c | 13 ++ examples/vhost/main.c | 19 +- lib/librte_vhost/rte_virtio_net.h | 1 + lib/librte_vhost/socket.c | 5 + lib/librte_vhost/vhost.c | 12 ++ lib/librte_vhost/vhost.h | 102 ++++++++--- lib/librte_vhost/vhost_user.c | 315 ++++++++++++++++++++++----------- lib/librte_vhost/virtio_net.c | 196 +++++++++++++++++--- 10 files changed, 549 insertions(+), 162 deletions(-)Signature · 1 line
1.9.0
Quoted 2 lines · Yuanhan Liu
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Is there problem when push vlan to the mbuf which reference the desc buf addr directly? We know if guest use virtio_net(kernel) maybe skb has no headroom.
Quoted 5 lines · linhaifeng
Quoted 2 lines · Yuanhan Liu
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Is there problem when push vlan to the mbuf which reference the desc buf addr directly?
Yes, you can't do that when zero copy is enabled, due to following code piece:
+ if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
+ desc->addr + desc_offset, cpy_len)))) {
+ cur->data_len = cpy_len;
==> + cur->data_off = 0;
+ cur->buf_addr = (void *)(uintptr_t)desc_addr;
+ cur->buf_physaddr = hpa;The marked line basically makes the mbuf has no headroom to use.
--yliu
Quoted 1 line
We know if guest use
virtio_net(kernel)maybe skb has no headroom.
Quoted 17 lines · Yuanhan Liu
Quoted 5 lines · linhaifeng
Quoted 2 lines · Yuanhan Liu
The basic idea of Tx zero copy is, instead of copying data from the desc buf, here we let the mbuf reference the desc buf addr directly.
Is there problem when push vlan to the mbuf which reference the desc buf addr directly?
Yes, you can't do that when zero copy is enabled, due to following code piece:
+ if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
+desc->addr+desc_offset,cpy_len)))) {
+cur->data_len=cpy_len;==> + cur->data_off = 0; + cur->buf_addr = (void *)(uintptr_t)desc_addr; + cur->buf_physaddr = hpa;
Quoted 8 lines
The marked line basically makes the mbuf has no headroom to use.
--yliuQuoted 1 line
We know if guest use
virtio_net(kernel)maybe skb has no headroom..
It ok to set data_off zero.
But we also can use 128 bytes headromm when guest use virtio_net PMD but not for virtio_net kernel driver.
I think it's better to add headroom size to desc and kernel dirver support set headroom size.